-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
melody_recognition_exercise.es6
653 lines (567 loc) · 15.9 KB
/
melody_recognition_exercise.es6
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
import * as React from "react"
import {classNames, MersenneTwister, NoSleep} from "lib"
import * as types from "prop-types"
import {SongNoteList} from "st/song_note_list"
import Slider from "st/components/slider"
import Select from "st/components/select"
import {noteName, parseNote} from "st/music"
import SongParser from "st/song_parser"
import {isMobile} from "st/browser"
import {IconShuffle} from "st/components/icons"
import {setTitle} from "st/globals"
export default class MelodyRecognitionExercise extends React.Component {
static exerciseName = "Interval Melodies"
static exerciseId = "melody_recognition"
static melodies = [
{
interval: "m2",
direction: "asc",
song: "m2_asc",
title: "Jaws"
}, {
interval: "m2",
direction: "desc",
song: "m2_desc",
title: "Joy To The World"
}, {
interval: "M2",
direction: "asc",
song: "M2_asc",
title: "Silent Night"
}, {
interval: "M2",
direction: "desc",
song: "M2_desc",
title: "Mary Had A Little Lamb"
}, {
interval: "m3",
direction: "asc",
song: "m3_asc",
title: "Greensleves",
}, {
interval: "m3",
direction: "desc",
song: "m3_desc",
title: "Hey Jude",
}, {
interval: "M3",
direction: "asc",
song: "M3_asc",
title: "On When The Saints",
}, {
interval: "M3",
direction: "desc",
song: "M3_desc",
title: "Swing Low Sweet Chariot",
}, {
interval: "P4",
direction: "asc",
song: "P4_asc",
title: "Here Comes The Bride",
}, {
interval: "P4",
direction: "desc",
song: "P4_desc",
title: "I've Been Working On The Rail Road",
}, {
interval: "T",
direction: "asc",
song: "T_asc",
title: "The Simpsons",
}, {
interval: "P5",
direction: "asc",
song: "P5_asc",
title: "Star Wars",
}, {
interval: "P5",
direction: "desc",
song: "P5_desc",
title: "Flintstones",
}, {
interval: "m6",
direction: "asc",
song: "m6_asc",
title: "Entertainer",
}, {
interval: "M6",
direction: "asc",
song: "M6_asc",
title: "NBC",
}, {
interval: "m7",
direction: "asc",
song: "m7_asc",
title: "Star Trek",
}, {
interval: "M7",
direction: "asc",
song: "M7_asc",
title: "Take On Me",
}, {
interval: "P8",
direction: "asc",
song: "P8_asc",
title: "Somewhere Over The Rainbow",
}, {
interval: "P8",
direction: "desc",
song: "P8_desc",
title: "To Zanarkand",
}
]
static melodyCache = {}
static fetchMelody(name) {
if (!this.melodyCache[name]) {
this.melodyCache[name] = new Promise((resolve, reject) => {
let request = new XMLHttpRequest()
request.open("GET", `/static/music/interval_melodies/${name}.lml?${+new Date()}`)
request.onerror = () => reject(request.statusText)
request.onload = (e) => {
let songText = request.responseText
let song
try {
song = SongParser.load(songText)
} catch (e) {
console.log(e)
return reject(`Failed to parse: ${name}`)
}
// transpose to middle c
let root = parseNote(song[0].note)
song = song.transpose(60 - root)
resolve(song)
}
request.send()
})
}
return this.melodyCache[name]
}
constructor(props) {
super(props)
this.state = {
loading: true,
playbackBpm: 90,
playbackTranspose: 0,
enabledIntervals: {},
rand: new MersenneTwister(),
autoplayRandomizeRoot: true,
autoplayIntervalOrder: "default",
}
}
componentDidMount() {
let loadingCount = 0
setTitle("Learn Intervals Ear Training Exercise")
this.setState({
loading: true
})
let melodySongs = {}
let enabled = {}
MelodyRecognitionExercise.melodies.forEach((m) => {
loadingCount += 1
MelodyRecognitionExercise.fetchMelody(m.song).then(song => {
loadingCount -= 1
melodySongs[m.song] = song
enabled[`${m.interval}-${m.direction}`] = true
if (loadingCount == 0) {
this.setState({
loading: false,
melodySongs,
enabledIntervals: enabled
})
}
}).catch(e => console.warn(e))
})
}
componentWillUnmount() {
if (this.state.playingTimer) {
this.state.playingTimer.stop()
}
if (this.state.autoplayTimer) {
this.state.autoplayTimer.stop()
}
if (this.nosleep && this.nosleepEnabled) {
this.nosleep.disable()
this.nosleepEnabled = false
}
}
nextMelody(fn) {
let intervals = MelodyRecognitionExercise.melodies.filter(m =>
this.state.enabledIntervals[`${m.interval}-${m.direction}`]
)
let interval = intervals[this.state.rand.int() % intervals.length]
this.setState({
currentMelody: interval
}, fn)
}
playCurrentRoot() {
let current = this.state.currentMelody
if (!current) {
return
}
let song = this.state.melodySongs[current.song]
let first = new SongNoteList()
let note = song[0].clone()
note.duration = 1
first.push(note)
return this.playSong(first)
}
playCurrentInterval() {
let current = this.state.currentMelody
if (!current) {
return
}
let song = this.state.melodySongs[current.song]
let first = new SongNoteList()
let note1 = song[0].clone()
let note2 = song[1].clone()
note1.duration = 1
note2.duration = 1
if (this.state.autoplayIntervalOrder == "reverse") {
note1.start = 1
note2.start = 0
} else if (this.state.autoplayIntervalOrder == "harmonic") {
note1.start = 0
note2.start = 0
} else {
note1.start = 0
note2.start = 1
}
first.push(note1)
first.push(note2)
return this.playSong(first)
}
playCurrentSong() {
let current = this.state.currentMelody
if (!current) {
return
}
return this.playSong(this.state.melodySongs[current.song])
}
playSong(song) {
song = song.transpose(this.state.playbackTranspose)
let timer = song.play(this.props.midiOutput, {
bpm: this.state.playbackBpm
})
this.setState({
playing: true,
playingTimer: timer
})
timer.getPromise().then(() => {
this.setState({
playing: false,
playingTimer: null,
})
})
return timer
}
autoplayDelay(time, fn) {
let timer
let t = window.setTimeout(() => {
if (this.state.autoplayTimer == timer) {
this.setState({
autoplayTimer: undefined
})
}
fn()
}, time)
timer = {
stop: (reason) => {
window.clearTimeout(t)
if (reason == "skip") {
fn()
}
}
}
this.setState({
autoplayTimer: timer
})
}
autoplayNextInterval() {
if (isMobile() && !this.nosleepEnabled) {
this.nosleep = this.nosleep || new NoSleep()
this.nosleep.enable()
this.nosleepEnabled = true
}
if (this.state.autoplayRandomizeRoot) {
this.setState({
playbackTranspose: (this.state.rand.int() % 36) - 18
})
}
this.nextMelody(() => {
let timer = this.playCurrentInterval()
this.setState({
autoplayTimer: timer,
autoplayState: "playingInterval"
})
timer.getPromise().then((reason) => {
if (reason == "stop") {
return
}
this.autoplayDelay(2000, () => {
let timer = this.playCurrentSong()
this.setState({
autoplayTimer: timer,
autoplayState: "playingMelody"
})
timer.getPromise().then((reason) => {
if (reason == "stop") {
return
}
this.autoplayDelay(2000, () => {
this.autoplayNextInterval()
})
})
})
})
})
}
render() {
return <div className="melody_recognition_exercise">
<div className="exercise_header">
{this.props.toggleSidebarButton}
<h1 className="exercise_label">Interval Recognition</h1>
</div>
{this.state.loading ?
<div className="page_container">Loading</div>
:
<div className="page_container">
{this.renderSongPlayer()}
{this.renderIntervalSettings()}
{this.renderAutoplayer()}
</div>
}
</div>
}
renderAutoplayer() {
let skipButton
if (this.state.autoplayTimer) {
skipButton = <button
onClick={(e) => {
this.state.autoplayTimer.stop("skip")
}}
>Skip</button>
}
return <section className="auto_player">
<h3>Autoplay Mode</h3>
<p>Repeatedly plays a random interval, a pause, then the associated melody. No input required, listen along and try to identify the intervals.</p>
<fieldset className="autoplay_options">
<legend>Autoplay options</legend>
<ul >
<li>
<label>
<input
checked={this.state.autoplayRandomizeRoot}
onChange={e => {
this.setState({
autoplayRandomizeRoot: e.target.checked
})
}}
type="checkbox" />
<span className="input_label">Randomly transpose</span>
</label>
</li>
<li>
<label>
<span className="input_label">Playback mode</span>
<Select
value={this.state.autoplayIntervalOrder}
onChange={(v) => this.setState({ autoplayIntervalOrder: v })}
options={[
{name: "In order", value: "default"},
{name: "Reverse", value: "reverse"},
{name: "Harmonic", value: "harmonic"},
]}
/>
</label>
</li>
</ul>
</fieldset>
<p>
<button
onClick={(e) => {
e.preventDefault()
if (this.state.autoplayTimer) {
this.state.autoplayTimer.stop()
if (this.nosleep && this.nosleepEnabled) {
this.nosleep.disable()
this.nosleepEnabled = false
}
this.setState({
autoplayTimer: undefined,
autoplayState: undefined,
})
} else {
this.autoplayNextInterval()
}
}}
>{this.state.autoplayTimer ? "Stop" : "Start autoplay"}</button>
{" "}
{skipButton}
</p>
</section>
}
renderSongPlayer() {
let current = this.state.currentMelody
let currentSongTools
if (current) {
let currentSong = this.state.melodySongs[current.song]
let stopSong
if (this.state.playingTimer && !this.state.autoplayTimer) {
stopSong = <button
type="button"
onClick={e => this.state.playingTimer.stop() }>Stop</button>
}
let firstNote = noteName(parseNote(currentSong[0].note) + this.state.playbackTranspose)
let disabled = !!(this.state.playing || this.state.autoplayTimer)
let title = `${current.interval} - ${current.title} (${firstNote})`
if (this.state.autoplayState == "playingInterval") {
title = "Listen to interval..."
}
currentSongTools = <div className="current_song">
<div className="song_title">{title}</div>
<div className="song_controls">
<button
disabled={disabled}
type="button"
onClick={e => {
this.playCurrentRoot()
}}>Play root</button>
<button
disabled={disabled}
type="button"
onClick={e => {
this.playCurrentInterval()
}}
>Play interval</button>
<button
type="button"
disabled={disabled}
onClick={e => {
this.playCurrentSong()
}}>Play melody</button>
{stopSong}
</div>
</div>
} else {
currentSongTools = <div className="current_song">
Press <strong>Next melody</strong> to randomly pick a interval to practice
</div>
}
let disabled = !!(this.state.playing || this.state.autoplayTimer)
return <div className="song_selector">
<div className="global_controls">
<button
disabled={disabled}
onClick={(e) => { this.nextMelody() }}>Next melody</button>
<label className="slider_group">
<span>BPM</span>
<Slider
min={40}
max={160}
onChange={(value) => {
this.setState({ playbackBpm: value })
}}
value={this.state.playbackBpm} />
<code>{this.state.playbackBpm}</code>
</label>
<label className="slider_group">
<span>Transpose</span>
<Slider
min={-24}
max={24}
onChange={(value) => {
this.setState({ playbackTranspose: value })
}}
value={this.state.playbackTranspose} />
<code>{this.state.playbackTranspose}</code>
<button
type="button"
title="Randomize Transpose"
onClick={e=>
this.setState({
playbackTranspose: (this.state.rand.int() % 36) - 18
})
}
className="shuffle_button">
<IconShuffle width={16} height={16} />
</button>
</label>
</div>
{currentSongTools}
</div>
}
renderIntervalSettings() {
let inputs = MelodyRecognitionExercise.melodies.map((m) => {
let key = `${m.interval}-${m.direction}`
return <li key={key} title={m.title}>
<label>
<input
type="checkbox"
onChange={e => {
this.setState({
enabledIntervals: {
...this.state.enabledIntervals,
[key]: e.target.checked,
}
})
}}
checked={this.state.enabledIntervals[key] || false} />
{" "}
<span className="label">{m.interval} {m.name} ({m.direction})</span>
</label>
</li>
})
let enabledFiltered = fn => {
let keys = MelodyRecognitionExercise.melodies
.filter(fn)
.map(m => `${m.interval}-${m.direction}`)
let enabled = {}
for (let key of keys) {
enabled[key] = true
}
return enabled
}
let toggleAllButton = null
// if it's empty add a toggle all button
if (Object.keys(this.state.enabledIntervals || {}).length == 0) {
toggleAllButton = <button
type="button"
onClick={e => this.setState({ enabledIntervals: enabledFiltered(m => true)})}
>All on</button>
} else {
toggleAllButton = <button
type="button"
onClick={e => this.setState({ enabledIntervals: {} })}
>All off</button>
}
return <section className="interval_settings">
<fieldset className="enabled_intervals">
<legend>Intervals</legend>
<ul>
{inputs}
</ul>
<div className="button_toggles">
{toggleAllButton}
{" "}
<button
type="button"
onClick={e =>
this.setState({
enabledIntervals: enabledFiltered(m => m.direction == "asc")
})
}
>All Ascending</button>
{" "}
<button
type="button"
onClick={e =>
this.setState({
enabledIntervals: enabledFiltered(m => m.direction == "desc")
})
}
>All Descending</button>
</div>
</fieldset>
</section>
}
}