This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Piano.elm
166 lines (128 loc) · 4.41 KB
/
Piano.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module Piano exposing (..)
import Html exposing (Html, Attribute, map, div, p, text)
import WebMidi exposing (Model, init, update, subscriptions)
import WebMidi.Msg exposing (..)
import WebMidi.Ports exposing (requestAccess)
import WebMidi.Subscriptions exposing (eventSub)
import Midi.Types exposing (MidiEvent(..))
import SoundFont.Ports exposing (..)
import SoundFont.Types exposing (..)
import SoundFont.Msg exposing (..)
import SoundFont.Subscriptions exposing (..)
import Debug exposing (log)
{- This program allows you to attach a MIDI keyboard and then play it as
a simple piano through the browser. It only responds to MIDI note on events.
-}
main =
Html.program
{ init = init, update = update, view = view, subscriptions = subscriptions }
type Msg
= MidiMsg WebMidi.Msg.Msg
| SoundFontMsg SoundFont.Msg.Msg
type alias Model =
{ audioContext : Maybe AudioContext
, fontsLoaded : Bool
, webMidi : WebMidi.Model
}
init : ( Model, Cmd Msg )
init =
let
( webMidi, webMidiCmd ) =
WebMidi.init
in
{ audioContext = Nothing
, fontsLoaded = False
, webMidi = webMidi
}
! [ Cmd.map MidiMsg webMidiCmd
, initialiseAudioContext ()
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SoundFontMsg soundFontMsg ->
case soundFontMsg of
ResponseAudioContext context ->
( { model | audioContext = Just context }
, requestLoadPianoFonts "soundfonts"
)
ResponseFontsLoaded loaded ->
( { model | fontsLoaded = loaded }
, requestAccess False
)
-- ignore any other messages
_ ->
( model, Cmd.none )
MidiMsg midiMsg ->
case midiMsg of
Event _ _ midiEvent ->
( model
, playNote midiEvent model
)
-- route any other messages to the WebMid module
_ ->
let
( newWebMidi, cmd ) =
WebMidi.update midiMsg model.webMidi
in
{ model | webMidi = newWebMidi } ! [ Cmd.map MidiMsg cmd ]
{- just respond to NoteOn events by playing them -}
playNote : Result String MidiEvent -> Model -> Cmd Msg
playNote event model =
case event of
Ok midiEvent ->
case midiEvent of
NoteOn channel pitch velocity ->
let
-- this is the utter maximum velocity
volumeCeiling =
127.0
-- this is the fraction that it is scaled back by the volume control
volumeScale =
Basics.toFloat model.webMidi.maxVolume / volumeCeiling
-- and this is what's left of the note
gain =
Basics.toFloat velocity * volumeScale / volumeCeiling
note =
-- MidiNote
{ id = pitch, timeOffset = 0.0, gain = gain }
-- _ = log "gain" gain
in
requestPlayNote note
_ ->
Cmd.none
Err msg ->
let
_ =
log "note error" msg
in
Cmd.none
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Sub.map MidiMsg (WebMidi.subscriptions model.webMidi)
, Sub.map SoundFontMsg audioContextSub
, Sub.map SoundFontMsg fontsLoadedSub
]
viewAudioContext : Model -> String
viewAudioContext model =
case model.audioContext of
Just context ->
"Audio Context got"
_ ->
"No Audio Context yet"
initialisationStatus : Model -> String
initialisationStatus model =
case model.audioContext of
Just context ->
if model.fontsLoaded then
"Ready to play"
else
"SoundFonts not loaded"
_ ->
"No Audio Context"
view : Model -> Html Msg
view model =
div []
[ p [] [ text (initialisationStatus model) ]
]