Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arkham/elm-rttl
Parse ringtones written using RTTL and Nokia Composer in Elm
https://github.com/arkham/elm-rttl
composer elm hacktoberfest nokia rttl
Last synced: 1 day ago
JSON representation
Parse ringtones written using RTTL and Nokia Composer in Elm
- Host: GitHub
- URL: https://github.com/arkham/elm-rttl
- Owner: Arkham
- License: bsd-3-clause
- Created: 2020-02-15T17:06:19.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-29T15:58:51.000Z (about 3 years ago)
- Last Synced: 2025-01-16T22:15:43.234Z (1 day ago)
- Topics: composer, elm, hacktoberfest, nokia, rttl
- Language: Elm
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Elm RTTL
Parse ringtones written using RTTL and Nokia Composer.
## Installation
`elm install Arkham/elm-rttl`
## Example
Here's an example of what you could do:
```elm
port module Example exposing (main)import Browser
import Html exposing (Html)
import Html.Attributes as Attr
import Html.Events as Events
import Json.Encode as Encode
import RTTLport sendRingtone : Encode.Value -> Cmd msg
type alias Model =
{ userInput : String
, alertMessage : String
}type Msg
= UserInput String
| PlayinitialModel : Model
initialModel =
{ userInput = "32c2 32f2 32c2 32f2 4c2 8#g1 8#a1 4f1 8- 32c2 32f2 32c2 32f2 4c2 8#g1 8#a1 4#d2 8- 32c2 32- 32#d2 4f2 16#g2 32- 16g2 32- 32f2 32- 4#d2 8- 32c2 32f2 32c2 32f2 4c2 8#a1 4f1"
, alertMessage = ""
}main : Program () Model Msg
main =
Browser.element
{ init = \_ -> ( initialModel, Cmd.none )
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}view : Model -> Html Msg
view model =
Html.div []
[ Html.textarea
[ Attr.value model.userInput
, Events.onInput UserInput
]
[]
, Html.button [ Events.onClick Play ] [ Html.text "Play" ]
, Html.text model.alertMessage
]update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UserInput newInput ->
( { model | userInput = newInput }, Cmd.none )Play ->
case RTTL.parseComposer { tempo = 64 } model.userInput of
Ok value ->
( { model | alertMessage = Debug.toString value }
, sendRingtone (RTTL.encode value)
)Err _ ->
( { model | alertMessage = "Could not parse ringtone, sorry!" }, Cmd.none )
```Paired us with this HTML
```
var app = Elm.Example.init({ node: document.querySelector('main') })app.ports.sendRingtone.subscribe(function(data) {
console.log(data);
var context = new AudioContext();var ac = new AudioContext();
var osc = ac.createOscillator();data.reduce(function(now, elem) {
oscillator = context.createOscillator();
oscillator.connect(context.destination);
oscillator.frequency.value = elem.frequency;
oscillator.start(context.currentTime + now);
oscillator.stop(context.currentTime + now + elem.duration - 0.002);
return(now + elem.duration);
}, 0);
});
```