项目作者: Arkham

项目描述 :
Parse ringtones written using RTTL and Nokia Composer in Elm
高级语言: Elm
项目地址: git://github.com/Arkham/elm-rttl.git
创建时间: 2020-02-15T17:06:19Z
项目社区:https://github.com/Arkham/elm-rttl

开源协议:BSD 3-Clause "New" or "Revised" License

下载


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:

  1. port module Example exposing (main)
  2. import Browser
  3. import Html exposing (Html)
  4. import Html.Attributes as Attr
  5. import Html.Events as Events
  6. import Json.Encode as Encode
  7. import RTTL
  8. port sendRingtone : Encode.Value -> Cmd msg
  9. type alias Model =
  10. { userInput : String
  11. , alertMessage : String
  12. }
  13. type Msg
  14. = UserInput String
  15. | Play
  16. initialModel : Model
  17. initialModel =
  18. { 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"
  19. , alertMessage = ""
  20. }
  21. main : Program () Model Msg
  22. main =
  23. Browser.element
  24. { init = \_ -> ( initialModel, Cmd.none )
  25. , view = view
  26. , update = update
  27. , subscriptions = \_ -> Sub.none
  28. }
  29. view : Model -> Html Msg
  30. view model =
  31. Html.div []
  32. [ Html.textarea
  33. [ Attr.value model.userInput
  34. , Events.onInput UserInput
  35. ]
  36. []
  37. , Html.button [ Events.onClick Play ] [ Html.text "Play" ]
  38. , Html.text model.alertMessage
  39. ]
  40. update : Msg -> Model -> ( Model, Cmd Msg )
  41. update msg model =
  42. case msg of
  43. UserInput newInput ->
  44. ( { model | userInput = newInput }, Cmd.none )
  45. Play ->
  46. case RTTL.parseComposer { tempo = 64 } model.userInput of
  47. Ok value ->
  48. ( { model | alertMessage = Debug.toString value }
  49. , sendRingtone (RTTL.encode value)
  50. )
  51. Err _ ->
  52. ( { model | alertMessage = "Could not parse ringtone, sorry!" }, Cmd.none )

Paired us with this HTML

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="app.js"></script>
  5. </head>
  6. <body>
  7. <main></main>
  8. <script>
  9. var app = Elm.Example.init({ node: document.querySelector('main') })
  10. app.ports.sendRingtone.subscribe(function(data) {
  11. console.log(data);
  12. var context = new AudioContext();
  13. var ac = new AudioContext();
  14. var osc = ac.createOscillator();
  15. data.reduce(function(now, elem) {
  16. oscillator = context.createOscillator();
  17. oscillator.connect(context.destination);
  18. oscillator.frequency.value = elem.frequency;
  19. oscillator.start(context.currentTime + now);
  20. oscillator.stop(context.currentTime + now + elem.duration - 0.002);
  21. return(now + elem.duration);
  22. }, 0);
  23. });
  24. </script>
  25. </body>
  26. </html>