-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
361 lines (275 loc) · 7.93 KB
/
Main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
port module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
import Regex exposing (..)
import String exposing (..)
import List.Extra exposing (getAt)
import Time exposing (..)
import Update.Extra.Infix exposing ((:>))
main =
Html.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
--MODEL
type alias Model =
{ initialNotes : String
, notesToSend : List Note
, index : Int
, bpm : Int
, waveType : String
, numberOfInputs : Int
, addDelay : Bool
}
type alias Note =
{ hz : Float
, duration : Float
, octave : Int
}
type alias PlayBundle =
{ note : Maybe Note
, tempo : Float
, waveType : String
, addDelay : Bool
}
model =
{ initialNotes = ""
, notesToSend = []
, index = 0
, bpm = 80
, waveType = "square"
, numberOfInputs = 1
, addDelay = False
}
init =
( model, Cmd.none )
--UPDATE
port send : PlayBundle -> Cmd msg
type Msg
= AcceptNotes String
| Play
| ResetIndex
| SendNotes
| ChangeBPM String
| ChangeWaveType String
| ToggleDelay
update msg model =
case msg of
AcceptNotes text ->
( { model
| initialNotes = text
, notesToSend = parseNotes model.initialNotes
}
, Cmd.none
)
Play ->
(model, Cmd.none)
:> update ResetIndex
:> update SendNotes
ResetIndex ->
( { model | index = 0 }, Cmd.none)
SendNotes ->
let
note =
Just (Note (frequencies "c") (sustain "q") (octave 3))
note2 =
Just (Note (frequencies "a") (sustain "q") (octave 3))
in
( { model | index = model.index +1}
, Cmd.batch [send (PlayBundle (getAt model.index model.notesToSend) (tempo model.bpm) model.waveType model.addDelay)
, send (PlayBundle note (tempo model.bpm) model.waveType model.addDelay)
, send (PlayBundle note2 (tempo model.bpm) model.waveType model.addDelay)])
ChangeBPM text ->
( { model | bpm = Result.withDefault 128 (String.toInt text) }, Cmd.none )
ChangeWaveType text ->
( { model | waveType = text }, Cmd.none )
ToggleDelay ->
( { model | addDelay = not model.addDelay }, Cmd.none)
--subscriptions =
subscriptions : Model -> Sub Msg
subscriptions model =
if model.index > 0 && model.index < List.length model.notesToSend
then
let
sustain =
Maybe.withDefault 1 ((getAt (model.index - 1) model.notesToSend)
|> Maybe.map .duration)
speed =
tempo model.bpm
in
Time.every ((sustain * speed) * second) (always SendNotes)
else Sub.none
parseNotes : String -> List Note
parseNotes string =
toLower string
|> find All (regex "([a-g,r]+#|[a-g,r])([whqes]+.|[whqes])(\\d)")
|> List.map .match
|> List.map noteSorter
noteSorter : String -> Note
noteSorter string =
let
_ = Debug.log "Note_" string
in
case (String.length string) of
3 ->
Note (frequencies (slice 0 1 string)) (sustain (slice 1 2 string)) (octave (Result.withDefault 0 (toInt (slice 2 3 string))))
4 ->
if slice 1 2 string == "#" then
Note (frequencies (slice 0 2 string)) (sustain (slice 2 3 string)) (octave (Result.withDefault 0 (toInt (slice 3 4 string))))
else
Note (frequencies (slice 0 1 string)) (sustain (slice 1 3 string)) (octave (Result.withDefault 0 (toInt (slice 3 4 string))))
5 ->
Note (frequencies (slice 0 2 string)) (sustain (slice 2 4 string)) (octave (Result.withDefault 0 (toInt (slice 4 5 string))))
_ ->
Note 0.0 0.0 0
sustain : String -> Float
sustain duration =
case duration of
"w" ->
4.0
"h" ->
2.0
"q" ->
1.0
"e" ->
0.5
"s" ->
0.25
"w." ->
6.0
"h." ->
3.0
"q." ->
1.5
"e." ->
0.75
"s." ->
0.375
_ ->
0.0
octave : Int -> Int
octave num =
case num of
1 ->
1
_ ->
2 ^ (num - 1)
frequencies : String -> Float
frequencies note =
case note of
"c" ->
130.81
"c#" ->
139.0
"d" ->
146.83
"d#" ->
156.0
"e" ->
164.81
"f" ->
174.61
"f#" ->
185.0
"g" ->
196.0
"g#" ->
208.0
"a" ->
220.0
"a#" ->
233.0
"b" ->
246.94
"r" ->
0.0
_ ->
0.0
--Formula for determining frequencies in hz
--110 * (1.059463..)^n
tempo : Int -> Float
tempo bpm =
(Basics.toFloat 60 / Basics.toFloat bpm) * 0.5
-- VIEW
view : Model -> Html Msg
view model =
div [ style [ ( "textAlign", "center" ), ("color", "#555") ] ]
[ h1 [ style [ ( "textDecoration", "underline" ), ( "margin", "150px auto 50px" ) ] ] [ text "Elm Melody Maker" ]
, noteInputField
, bpmInput
, waveSelectMenu
, button [ onClick Play, myStyles ] [ text "Play Notes" ]
, delay
, div [] [ text "NOTES TO BE PLAYED" ]
, div [ style [ ( "color", "#4d4dff" ), ( "fontSize", "0.85 rem" ) ] ] [ text (toString model.notesToSend) ]
, div [ style [ ( "margin", "1rem auto" ) ] ] [ instructions ]
]
noteInputField : Html Msg
noteInputField =
input
[ type_ "text"
, placeholder "Enter notes to play"
, onInput AcceptNotes
, myStyles
, style
[ ("width", "80%") ]
]
[]
waveSelectMenu =
select
[ onInput ChangeWaveType
, myStyles
, style
[ ("width", "15%") ]
]
[ option [ value "square" ] [ text "square" ]
, option [ value "sine" ] [ text "sine" ]
, option [ value "triangle" ] [ text "triangle" ]
, option [ value "sawtooth" ] [ text "sawtooth" ]
]
bpmInput =
input
[ type_ "number"
, placeholder "BPM"
, onInput ChangeBPM
, value (toString model.bpm)
, myStyles
, style
[ ("width", "15%") ]
]
[ text "Beats per minute" ]
delay =
label
[ style
[("display", "inline-block"), ("color", "#4d4dff")]
]
[ input
[ type_ "checkbox", onClick ToggleDelay ] []
, text "DELAY"
]
instructions =
ul [ instructionSytles ]
[ li [] [ text "Enter notes in the format: CW3 where ..." ]
, li [] [ text "C is the name of the note to be played (sharps are allowed but no flats yet)" ]
, li [] [ text "W is the note duration, where W = whole, H = eigth, Q = quarter, E = eigth, & S = sixteenth. Add . for dotted" ]
, li [] [ text "3 equals octave to be played (range of 1 - 9)" ]
, li [] [ text "EXAMPLES: gq2gq2gq2d#e.2a#s3gq2d#e.2a#s3gh2 dq3dq3dq3d#e.3a#s.2f#q2d#e.2a#s3gh2 gq3ge.2gs2gq3f#e.3fs3es3d#s3eq3 OR dq.4eq.4fq.4gq.4 aq.4fq.4gq.4dq.4 "]
]
myStyles =
style
[ ("backgroundColor", "#111")
, ("color", "#4d4dff")
, ("border", "1px solid #4d4dff")
, ("margin", " 1rem 20px")
]
instructionSytles =
style
[ ( "listStyle", "none" )
, ( "margin", "0 auto" )
, ( "color", "#555")
, ( "textAlign", "left")
, ( "width", "80%")
]