-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmlplay.html
635 lines (610 loc) · 23.1 KB
/
mmlplay.html
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
<HTML>
<HEAD>
<TITLE>Macro Music Language in AudioContext</TITLE>
</HEAD>
<SCRIPT TYPE="text/javascript">
const noteFrequences = [...Array(88).keys()].map(i => 440 * Math.pow(2, (i-49)/12));
const noteName2Idx = {
"C" : 0, "D" : 2, "E" : 4, "F" : 5, "G" : 7, "A": 9, "B" : 11,
"C+" : 1, "D-" : 1, "D+" : 3, "E-" : 3, "F+" : 6, "G-": 6,
"G+" : 8, "A-" : 8, "A+" : 10, "B-" : 10};
// Tokenizes string into upper case chars and numbers
class StringTokenizer {
constructor(str) {
this.str = str.toUpperCase();
this.pos = 0;
this._skipSpaces();
}
ended() { return this.pos >= this.str.length; }
_skipSpaces() {
const str = this.str;
let pos = this.pos;
while (pos < str.length && str[pos] === ' ')
pos++;
this.pos = pos;
}
nextToken() {
const str = this.str;
if (this.ended()) throw "EOF";
const start = this.pos;
while (!this.ended() && !isNaN(str[this.pos])) this.pos++;
let rc = start == this.pos ? str[this.pos++] : str.substring(start, this.pos);
this._skipSpaces();
return isNaN(rc) ? rc : Number(rc);
}
peekToken() {
if (this.ended()) return "";
const pos = this.pos;
const rc = this.nextToken();
this.pos = pos;
return rc;
}
};
class MMLTokenizer {
constructor(str) {
this.pos = 0;
this.tokens = [];
const stok = new StringTokenizer(str);
while(!stok.ended()) {
const tok = stok.nextToken();
const rc = {type: tok};
// Single letter commands
if ('<>&'.indexOf(tok) >= 0) {
this.tokens.push(rc);
continue;
}
// Letter + Number commands
if ('LOSTV'.indexOf(tok) >= 0) {
const num = stok.nextToken();
if (typeof num !== 'number') throw "Parsing error";
rc.value = num;
this.tokens.push(rc);
continue;
}
if ('ABCDEFGR'.indexOf(tok) >= 0) {
let nt = stok.peekToken();
if (nt.length > 0 && '+-#'.indexOf(nt) >= 0) {
stok.nextToken();
if (tok === 'R') throw "Rest can't have pitch modifier";
rc.type += nt === '-' ? '-' : '+';
nt = stok.peekToken();
}
if (typeof nt === 'number') {
stok.nextToken();
rc.length = nt;
nt = stok.peekToken();
}
while (nt === '.') {
stok.nextToken();
rc.modifier += nt;
nt = stok.peekToken();
}
this.tokens.push(rc);
continue;
}
throw "Unexpected token " + tok;
}
}
ended() { return this.pos >= this.tokens.length; }
nextToken() {
if (this.ended()) throw "EOF";
return this.tokens[this.pos++];
}
tokens() { return Array(this.tokens);}
};
function clamp(x, min, max) {
return x > min ? x < max ? x : max : min;
}
function newAudioContext() {
const rc = new (typeof webkitAudioContext === 'function' ? webkitAudioContext : AudioContext)();
const compressor = rc.createDynamicsCompressor();
compressor.connect(rc.destination);
rc.compressorNode = compressor;
return rc;
}
class AudioChannel {
constructor(ctx) {
this.tempo = 120;
this.noteLength = 4;
this.octave = 4;
this.playbackEnabled = false;
this._createOscillatorAndGain(ctx);
}
_createOscillatorAndGain(ctx) {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.connect(gain);
gain.gain.value = .53;
osc.start();
this.gain = gain; this.osc = osc;
this.endOfStreamTime = ctx.currentTime;
}
// Measure is four qurter notes
measureLength() { return 4 * 60.0 / this.tempo; }
time() { return this.osc.context.currentTime;}
enablePlayback(enable) {
if (enable == this.playbackEnabled) {
return;
}
const ctx = this.osc.context;
if (enable) {
const destination = ctx.hasOwnProperty('compressorNode') ? ctx.compressorNode : ctx.destination;
this.gain.connect(destination);
} else {
this.gain.disconnect();
if (this.endOfStreamTime > this.time()) {
const oscType = this.osc.type;
this._createOscillatorAndGain(ctx);
this.osc.type = oscType;
}
}
this.playbackEnabled = enable;
}
stop() { this.enablePlayback(false); }
start() { this.enablePlayback(true); }
setVolume(val, time) {
let gain = this.gain.gain;
time = time || this.time();
gain.setValueAtTime(clamp(val /15.0, gain.minValue, gain.maxValue), time);
}
setFreq(val, time) {
let freq = this.osc.frequency;
time = time || this.time();
freq.setValueAtTime(clamp(val, freq.minValue, freq.maxValue), time);
}
playNote(n) {
const measureLength = this.measureLength();
let noteLength = measureLength / this.noteLength;
let noteFreq = 0;
if (typeof n === 'object') {
if (n.hasOwnProperty('length'))
noteLength = measureLength / n.length;
if (n.hasOwnProperty('modifier'))
noteLength *= 1.5;
n = n.type;
}
if (typeof n === 'string') {
n = n.toUpperCase();
if (!noteName2Idx.hasOwnProperty(n)) throw "Unknown note";
n = (this.octave - 1)*12 + 4 + noteName2Idx[n];
}
if (typeof n === 'number') {
noteFreq = noteFrequences[clamp(n, 0, noteFrequences.length)];
}
const time = Math.max(this.time(), this.endOfStreamTime)
this.setFreq(noteFreq, time);
this.endOfStreamTime = time + noteLength;
}
play(str) {
const tokenStream = new MMLTokenizer(str);
while (!tokenStream.ended()) {
const tok = tokenStream.nextToken();
if (tok.type === '>') this.octave++;
if (tok.type === '<') this.octave--;
if (tok.type === 'O') this.octave = tok.value;
if (tok.type === 'T') this.tempo = tok.value;
if (tok.type === 'L') this.noteLength = tok.value;
if (tok.type === 'R') {
const pauseLength = tok.hasOwnProperty('length') ? tok.length : this.noteLength;
let pauseTime = this.measureLength() / pauseLength;
if (tok.hasOwnProperty('modifier')) pauseTime *= 1.5;
this.setFreq(0, this.endOfStreamTime);
this.endOfStreamTime += pauseTime;
}
if (noteName2Idx.hasOwnProperty(tok.type)) {
const self = this;
this.playNote(tok);
}
}
this.setFreq(0, this.endOfStreamTime);
this.start();
}
};
function getMazeOfGaliousMML() {
const mml = ['', '', ''];
mml[0] += 'V15T140L4D4.L16FGL4AO5D';
mml[1] += 'V14T140L4O3AL8AL16O4DEL4FA';
mml[2] += 'v15T140L8O3DL16DDL8CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'CO4G2.';
mml[1] += 'GE2.';
mml[2] += 'L8O3CL16CCL8O2GL16GGL8O3CL16CCL8O2GL16GG';
mml[0] += 'A#L8A#L16AGL4A#L8A#L16AG';
mml[1] += 'GL8GL16FD#L4GL8GL16FD#';
mml[2] += 'L8O3D#L16D#D#L8O2A#L16A#A#L8O3D#L16D#D#L8O2A#L16A#A#';
mml[0] += 'L4AD2.';
mml[1] += 'L4FO3A2.';
mml[2] += 'L8O3DL16DDL8CL16CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'DL8DL16FGL4AO5D';
mml[1] += 'AL8AL16O4DEL4FG';
mml[2] += 'L8O3DL16DDL8CL16CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'EO4A2.';
mml[1] += 'AR8L16EDC#DEFGR16O5CR16';
mml[2] += 'L8O3CL16CCL8O2A#L16A#A#L8AL16AAL8GL16GG';
mml[0] += 'A#L8A#L16AGL4A#L8A#L16AG';
mml[1] += 'L4O4GL8GL16FD#L4GL8GL16FD#';
mml[2] += 'L8GL16GGL8AL16AAL8A#L16A#A#L8O3CL16CC';
mml[0] += 'L4AO5D2.';
mml[1] += 'L4F4R2';
mml[2] += 'L8DL16DDL8DL16DDL8DL16DDL8DL16DD';
mml[0] += 'O4A#8.R16L16A#AGFL4GA#';
mml[1] += 'GR4D#G';
mml[2] += 'D#D#D#D#R4L8D#L16D#D#L8D#L16D#D#';
mml[0] += 'AL2DL16EFL8G';
mml[1] += 'FL2O3AR4';
mml[2] += 'L8DL16DDL8CL16CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'L4G#8.R16L16G#GFD#L4FG#';
mml[1] += 'L4O4FR4D#F';
mml[2] += 'G#G#G#G#R4L8G#L16G#G#L8G#L16G#G#';
mml[0] += 'GC2.';
mml[1] += 'CL2O3GR4';
mml[2] += 'L8O3CL16CCL8DL16DDL8D#L16D#D#L8FL16FF';
mml[0] += 'A#8.R16L16A#AGFL4GA#';
mml[1] += 'L4O4GR4D#G';
mml[2] += 'D#D#D#D#R4L8D#L16D#D#L8D#L16D#D#';
mml[0] += 'A8.R16L16AGFEL4FA';
mml[1] += 'FR4DF';
mml[2] += 'DDDDR4L8DL16DDL8DL16DD';
mml[0] += 'G8.R16L16GFEDL4G#8.R16L16G#GFE';
mml[1] += 'ER4FR4';
mml[2] += 'O2GGGGR4G#G#G#G#R4';
mml[0] += 'A2.EFGA';
mml[1] += 'EC#O3AR4';
mml[2] += 'AO3AO2AAL8AL16AAL8AL16AAAAAA';
mml[0] += 'L4O5DEL8FL16EDL8EO4A';
mml[1] += 'L1O4A';
mml[2] += 'L8O3DL16DDL8CL16CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'L4O5CDL8EL16DCL8DO4G';
mml[1] += 'L8AGG2.';
mml[2] += 'L8O3CL16CCL8O2GL16GGL8O3CL16CCL8O2GL16GG';
mml[0] += 'L4O5D#FL8GL16FD#L8FO4A#';
mml[1] += 'GA#A#2.';
mml[2] += 'L8O3D#L16D#D#L8O2A#L16A#A#L8O3D#L16D#D#L8O2A#L16A#A#';
mml[0] += 'L16O5DEFGV14DEFGV13DEFGV12DEFG';
mml[1] += 'V14L16R16O5DEFGV13DEFGV12DEFGV11DEFV14';
mml[2] += 'L8O3DL16DDL8CL16CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'V15L4DEL8FL16EDL8EO4A';
mml[1] += 'V14O4L1A';
mml[2] += 'L8O3DL16DDL8CL16CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'L4O5CDL8EL16DCL8DO4G';
mml[1] += 'L8AGG2.';
mml[2] += 'L8O3CL16CCL8O2GL16GGL8O3CL16CCL8O2GL16GG';
mml[0] += 'L4O5D#FL8GL16FD#L8FO4A#';
mml[1] += 'GA#A#2.';
mml[2] += 'L8O3D#L16D#D#L8O2A#L16A#A#L8O3D#L16D#D#L8O2A#L16A#A#';
mml[0] += 'L16O5DEFGV14DEFGV13DEFGV12DEFG';
mml[1] += 'V14L16R16O5DEFGV13DEFGV12DEFGV11DEFV14';
mml[2] += 'L8O3DL16DDL8CL16CCL8O2A#L16A#A#L8AL16AA';
mml[0] += 'S0L8O4DL16DDL8DL16DDDO3AO4CFEO3AO4CD';
mml[1] += 'S0O2D8L16DDL8DL16DDDO1AO2CFEO1AO2CD';
mml[2] += 'L8O3DL16DDL8DL16DDDO2AO3CFEO2AO3CD';
mml[0] += 'L8O3GL16GGL8GL16GGO4GDFA#ADFG';
mml[1] += 'L8O1GL16GGL8GL16GGO2GDFA#ADFG';
mml[2] += 'L8O2GL16GGL8GL16GGO3GDFA#ADFG';
mml[0] += 'L8DL16DDL8DL16DDDO3AO4CFEO3AO4CD';
mml[1] += 'L8DL16DDL8DL16DDDO1AO2CFEO1AO2CD';
mml[2] += 'L8DL16DDL8DL16DDDO2AO3CFEO2AO3CD';
return mml;
}
function getFurEliseMML() {
const mml = ['l16t100v15o5', 'l16t100v15o5', 'l16t100v15o5'];
mml[0] += 'o5 ed#';
mml[1] += 'o3 r8';
mml[2] += 'o3 r8';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16eg#b >c8r16<e>ed#';
mml[1] += 'o4 r4. r4. r4. r4.';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b a4';
mml[1] += 'o4 r4. r4. r4. r4';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ear16';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16eg#b >c8r16<e>ed#';
mml[1] += 'o4 r4. r4. r4. r4.';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b a8r16b>cd';
mml[1] += 'o4 r4. r4. r4. r4.';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
//* 2nd of first repeat *
mml[0] += 'o5 e8.<g>fe d8.<f>ed c8.<e>dc <b8r16e>er16 r16e>er16r16<d#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o3 cg>c<r16r8 <g>gbr16r8 <a>ear16r8 <e>e>er16r16e >er16r16d#er16';
mml[0] += 'o5 er16r16d#ed# ed#e<b>dc <a8r16cea b8r16eg#b >c8r16<e>ed#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o5 r16d#er16r8 r4. o3 <a>ear16r8 <e>eg#r16r8 <a>ear16r8 ';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b a8r16b>cd';
mml[1] += 'o4 r4. r4. r4. r4.';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
mml[0] += 'o5 e8.<g>fe d8.<f>ed c8.<e>dc <b8r16e>er16 r16e>er16r16<d#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o3 cg>c<r16r8 <g>gbr16r8 <a>ear16r8 <e>e>er16r16e >er16r16d#er16';
mml[0] += 'o5 er16r16d#ed# ed#e<b>dc <a8r16cea b8r16eg#b >c8r16<e>ed#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o5 r16d#er16r8 r4. o3 <a>ear16r8 <e>eg#r16r8 <a>ear16r8 ';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b a8r16';
mml[1] += 'o4 r4. r4. r4. r16r16r16';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ea';
mml[0] += 'o5 <efg';
mml[1] += 'o4 cc<b';
mml[2] += 'o3 b-ag';
//* 2nd of second repeat; first non-theme phrase: *
mml[0] += 'o5 c4f.e32 e8d8b-.a32 agfedc <b-8a8a32g32a32b-32 >c4dd#';
mml[1] += 'o3 r4. r4. r8gr16gr16 r4. r4.';
mml[2] += 'o3 fa>c<a>c<a fb->d<b->d<b- f>e<b->e<b->e fa>c<a>c<a fa>c<a>c<a';
mml[0] += 'o5 e8.ef<a >c8d32c32<b32>c32d.b32 ';
mml[1] += 'o3 r4dr16 r4.';
mml[2] += 'o3 ea>c<a>b<f g>e<g>e<g>f';
mml[0] += 'o5 l32 cg<g>g<a>g<b>gcgdg eg>c<bagfedgfd';
mml[1] += 'o4 l16 e8r16ggf e8<a8b8';
mml[2] += 'o3 >c8r16fed c8<f8g8';
mml[0] += 'o5 l32 cg<g>g<a>g<b>gcgdg eg>c<bagfedgfd efed#e<b>ede<b>ed';
mml[1] += 'o4 l16 r8r16ggf e8<a8b8 b8r4';
mml[2] += 'o3 >c8r16fed c8<f8g8 g#8r4';
mml[0] += 'o5 l16e8.<b>ed# e8.<b>er16 r16d#er16r16d# ed#e<b>dc ';
mml[1] += 'o4 r4. r4. r4. r4.';
mml[2] += 'o3 r4. r4r16 o5 d# er16r16d#er16 r4.';
//* return to main theme *
mml[0] += 'o5 <a8r16cea b8r16eg#b >c8r16<e>ed# ';
mml[1] += 'o4 r4. r4. r4.';
mml[2] += 'o3 <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b a8r16b>cd';
mml[1] += 'o4 r4. r4. r4. r4.';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
mml[0] += 'o5 e8.<g>fe d8.<f>ed c8.<e>dc <b8r16e>er16 r16e>er16r16<d#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o3 cg>c<r16r8 <g>gbr16r8 <a>ear16r8 <e>e>er16r16e >er16r16d#er16';
mml[0] += 'o5 er16r16d#ed# ed#e<b>dc <a8r16cea b8r16eg#b >c8r16<e>ed#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o5 r16d#er16r8 r4. o3 <a>ear16r8 <e>eg#r16r8 <a>ear16r8 ';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b ';
mml[1] += 'o4 r4. r4. r4. ';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 ';
//* second non-theme phrase: *
mml[0] += 'o4 a4. >c#4. d4ef f4f8 e4.';
mml[1] += 'o4 r4. g4. a4>c#d <g#4g#8 >c4.';
mml[2] += 'o3 <aaaaaa aaaaaa aaaaaa aaaaaa aaaaaa';
mml[0] += 'o5 d4c<b a4a8 a8>c8<b8 a4.';
mml[1] += 'o3 <aaaaaa aaaaaa aaaag#g# >>c4. ';
mml[2] += 'o3 <dddddd d#d#d#d#d#d# eeeeee aaaaaa';
mml[0] += 'o5 c#4. d4ef f4f8 f4.';
mml[1] += 'o4 g4. a4>c#d d4d8 d4.';
mml[2] += 'o3 <aaaaaa aaaaaa aaaaaa b-b-b-b-b-b-';
mml[0] += 'o5 e-4dc <b-4a8 g#4g#8 a4r8';
mml[1] += 'o4 g4fe- d4d8 f4f8 e4r8';
mml[2] += 'o3 b-b-b-b-b-b- b-b-b-b-b-b- bbbbbb >c4r8';
mml[0] += 'o5 <d8r8r8 l21 <a>c32ea>c32edc32<b a>c32ea>c32edc32<b a>c32ea>c32edc32<b';
mml[1] += 'o4 <g#8r8r8> l8 <r16r16c> cr16c cr16c';
mml[2] += 'o3 e8r8r8 <<a>>r16a ar16a ar16a';
mml[0] += 'o6 l21 b-a32g#gf#32fed#32d c#c32<bb-a32g#gf#32f l16 ed#e<b>dc ';
mml[1] += 'o4 l8 cr16r16 r16r16r16 r16r16r16';
mml[2] += 'o3 ar16r16 r16r16r16 r16r16r16';
//* final return to theme: *
mml[0] += 'l16';
mml[1] += 'L16';
mml[2] += 'l16';
mml[0] += 'o5 <a8r16cea b8r16eg#b >c8r16<e>ed# ';
mml[1] += 'o4 r4. r4. r4.';
mml[2] += 'o3 <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b a8r16b>cd';
mml[1] += 'o4 r4. r4. r4. r4.';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 <a>ear16r8';
mml[0] += 'o5 e8.<g>fe d8.<f>ed c8.<e>dc <b8r16e>er16 r16e>er16r16<d#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o3 cg>c<r16r8 <g>gbr16r8 <a>ear16r8 <e>e>er16r16e >er16r16d#er16';
mml[0] += 'o5 er16r16d#ed# ed#e<b>dc <a8r16cea b8r16eg#b >c8r16<e>ed#';
mml[1] += 'o4 r4. r4. r4. r4. r4.';
mml[2] += 'o5 r16d#er16r8 r4. o3 <a>ear16r8 <e>eg#r16r8 <a>ear16r8 ';
mml[0] += 'o5 ed#e<b>dc <a8r16cea b8r16e>c<b a4';
mml[1] += 'o4 r4. r4. r4. c4';
mml[2] += 'o3 r4. <a>ear16r8 <e>eg#r16r8 >a4';
return mml.map(s => s.replace(/ /g,''));
}
function getHiNoToriMML() {
const mml = ['l4t110v15o5', 'l4t110v15o5', 'l4t110v15o3'];
mml[0] += 'V14L16E2&E8R16EDC<B>C';
mml[1] += 'V12L16C2&C8R16C<BAGA';
mml[2] += 'V13L16AA>A<AAA>A<AAA>A<AAA>A<A';
mml[0] += 'D8C<A2>EDC<B>C';
mml[1] += 'B8AF2>C<BAGA';
mml[2] += 'FF>F<FFF>F<FFF>F<FFF>F<F';
mml[0] += 'D8C<A&A4A8>C<B8.GE';
mml[1] += 'B8GD&D4&D8AG8.DC';
mml[2] += 'GG>G<GGG>G<GGG>G8<G#G#>G#8';
mml[0] += 'E8BA&A4EGAB>DEGA';
mml[1] += 'C8GE&E4<B>DEGAB>DE';
mml[2] += '<AA>A<AAA>A<AEGAB>DEGA';
mml[0] += 'E2&E8R16AEDCC#';
mml[1] += 'C2&C8R16EC<BGA';
mml[2] += '<AA>A<AAA>A<AAA>A<AAA>A<A';
mml[0] += 'D8C<A2>EDC<B>C';
mml[1] += 'B8AF2>C<BAGA';
mml[2] += 'FF>F<FFF>F<FFF>F<FFF>F<F';
mml[0] += 'D8C<A&A4A8>C<B8.GE';
mml[1] += 'B8GD&D4D8AG8.DC';
mml[2] += 'GG>G<GGG>G<GGG>G8<G#G#>G#8';
mml[0] += 'E8BA&A4R2';
mml[1] += 'C8GE&E4R2';
mml[2] += '<AA>A<AAA>A<AA>A<G>G<E>E<G>G';
mml[0] += 'RAL8B>C<AB>C';
mml[1] += 'RFL8GAFGA';
mml[2] += '<F>CF<FFFFFFF>CF<FFF';
mml[0] += '<B4R16B16>CD<B>CD';
mml[1] += 'G4R16G16ABGAB';
mml[2] += 'GG>DG<GGGGGGG>DG<GGG';
mml[0] += 'C4R16C16DECDE';
mml[1] += 'A4R16A16B>C<AB>C';
mml[2] += 'AA>EA<AAAAAAA>EA<AAA>';
mml[0] += 'L16G&G2.FEDC';
mml[1] += 'L16E&E2.DC<BA';
mml[2] += 'CCC>C<CCCCCCC>C<CC<BAG';
mml[0] += '<A4.GA4R16AB>C<A';
mml[1] += 'F4.EF4R16FGAF';
mml[2] += 'F>CF<FFFFFFF>CF<FFFG';
mml[0] += 'B4>D4<G#B>DFDFG#B';
mml[1] += 'G4B4FG#B>D<B>DFG#';
mml[2] += 'GG>G<GGGGGG#G>G#8<G#G#>G#8';
mml[0] += 'AAAAR8GA&A4CDEG';
mml[1] += 'EEEER8DE&E4<AB>CE';
mml[2] += '<AA>A<AAA>A<AAA>A<AAA>A<A';
mml[0] += 'AAAAR8GA&A4CDEF';
mml[1] += 'FFFFR8EF&F4<AB>CD';
mml[2] += 'FF>F<FFF>F<FFF>F<FFF>F<F';
mml[0] += 'GGD<B>G2FEFD';
mml[1] += 'DD<BG>D2DC#D<B';
mml[2] += 'GG>G<GGG>G8<GG>G8<G#G#>G#8';
mml[0] += 'E2FC<A>FGD<B>G';
mml[1] += '>C2C<AF>CD<BG>D';
mml[2] += '<AA>A<AAA>A<AFF>F<FGG>G<G';
mml[0] += 'AAAAR8GA&A4CDEG';
mml[1] += 'EEEER8DE&E4<AB>CE';
mml[2] += 'AA>A<AAA>A<AAA>A<AAA>A<A';
mml[0] += 'AAAAR8GA&A4CDEF';
mml[1] += 'FFFFR8EF&F4<AB>CD';
mml[2] += 'FF>F<FFF>F<FFF>F<FFF>F<F';
mml[0] += 'GGD<B>GGD<B>G#G#D<B>G#G#D<B';
mml[1] += 'DD<BG>DD<BG>DD<BG#>DD<BG#';
mml[2] += 'GG>G<GGG>G<GG#G#>G#<G#G#G#>G#<G#';
mml[0] += '>A>C<AGA2CDEG';
mml[1] += 'R32>A>C<AGA2CDE';
mml[2] += 'A>A<A>A<A>A<A>A<A>A<A>A<A>A<G>G';
mml[0] += 'A>C<AGA2CDEF';
mml[1] += 'R32A>C<AGA2CDE';
mml[2] += '<F>F<F>F<F>F<F>F<F>F<F>F<F>F<F#>F#';
mml[0] += 'GGD<B>GGR16G4GFEFD';
mml[1] += 'V13R16DDBDDR16D4DR32FEF';
mml[2] += '<G>G<G>G<G>G<G>G<G>G<G>G<G#>G#<G#>G#';
mml[0] += 'E2FC<A>FGD<B>G';
mml[1] += 'R16E4.&EV11C<AF>CD<BG>D';
mml[2] += '<A>A<A>A<A>A<G>G<F>F<F>F<G>G<G>G';
mml[0] += 'A>CV13<AGV11A>CV10<AGV14A4CDEG';
mml[1] += 'R16V11A>CV10<AGV8A>C<V7AGV11A4CDE';
mml[2] += '<A>A<A>A<A>A<A>A<A>A<A>A<A>A<G>G';
mml[0] += 'A>CV13<AGV11A>CV10<AGV14A4CDEF';
mml[1] += 'R16A>CV10<AGV8A>C<V7AGV11A4CDE';
mml[2] += '<F>F<F>F<F>F<F>F<F>F<F>F<F>F<F#>F#';
mml[0] += 'GGD<B>GGD<B>G#G#D<B>G#G#D<B';
mml[1] += 'V13DD<BG>DD<BG>DD<BG#>DD<BG#';
mml[2] += '<G>G<G>G<G>G<G>G<G#>G#<G#>G#<G#>G#<G#>G#';
return mml;
}
function updateProgress() {
const chA = audioChannels[0];
if (chA.time() > chA.endOfStreamTime) {
for(let i = 0; i < audioChannels.length;++i) {
audioChannels[i].stop();
}
clearInterval(intervalID);
document.getElementById('button').value = 'Play';
return;
}
const progress = document.getElementById('progress');
progress.value = chA.time();
const chAFreq = document.getElementById('chA.freq');
const chBFreq = document.getElementById('chB.freq');
const chCFreq = document.getElementById('chC.freq');
chAFreq.value = chA.osc.frequency.value.toFixed(2);
chBFreq.value = audioChannels[1].osc.frequency.value.toFixed(2);
chCFreq.value = audioChannels[2].osc.frequency.value.toFixed(2);
}
var ctx = newAudioContext();
var audioChannels = [...Array(4).keys()].map(function() {return new AudioChannel(ctx)});
var intervalID = -1;
function onButtonClicked() {
const button = document.getElementById('button');
if (button.value === 'Play') {
const startTime = ctx.currentTime;
const mmls = [
document.getElementById('chA.MML').value,
document.getElementById('chB.MML').value,
document.getElementById('chC.MML').value,
];
for(let i = 0; i < mmls.length;++i)
audioChannels[i].play(mmls[i], startTime);
const progress = document.getElementById('progress');
progress.min = startTime;
progress.value = startTime;
progress.max = audioChannels[0].endOfStreamTime;
intervalID = setInterval(updateProgress, 10);
button.value = 'Stop';
} else {
button.value = 'Play';
clearInterval(intervalID);
for(let i = 0; i < audioChannels.length; i++)
audioChannels[i].stop();
}
}
function onSelectionChanged() {
const value = document.getElementById('musicSelect').value;
const mmls = value === 'furelise' ? getFurEliseMML() : value === 'hinotori' ? getHiNoToriMML() : getMazeOfGaliousMML();
document.getElementById('chA.MML').value = mmls[0];
document.getElementById('chB.MML').value = mmls[1];
document.getElementById('chC.MML').value = mmls[2];
}
function onWaveformChanged(chId, chNo) {
const value = document.getElementById(chId).value;
audioChannels[chNo].osc.type = value;
}
function onLoad() {
onSelectionChanged();
const runningOniOS = navigator.platform && /iPhone|iPad/.test(navigator.platform);
if (!runningOniOS) {
onButtonClicked();
}
}
</SCRIPT>
<BODY onLoad="onLoad();">
<H4>MSX Music Macro Language Interpreter using WebAudio</H4>
<TABLE>
<TR><TD>Progress</TD><TD><INPUT TYPE="range" id="progress" style="width:100%" min="0" max="10000" value="0" /></TD></TR>
<TR><TD>Channel A</TD><TD>Freq: <INPUT id="chA.freq" /></TD></TR>
<TR><TD>Channel B</TD><TD>Freq: <INPUT id="chB.freq" /></TD></TR>
<TR><TD>Channel C</TD><TD>Freq: <INPUT id="chC.freq" /></TD></TR>
</TABLE>
<TABLE>
<TR><TH>Channel</TH><TH>MML</TH><TH>Waveform</TH></TR>
<TR>
<TD>A</TD>
<TD><TEXTAREA id="chA.MML" rows="3" cols="50"/></TEXTAREA></TD>
<TD>
<SELECT id="chA.WF" onChange="onWaveformChanged('chA.WF', 0)">
<OPTION value="sine">Sine</OPTION>
<OPTION value="square"/>Square</OPTION>
<OPTION value="sawtooth">Sawtooth</OPTION>
<OPTION value="triangle">Triangle</OPTION>
</SELECT>
</TD>
</TR>
<TR>
<TD>B</TD>
<TD><TEXTAREA id="chB.MML" rows="3" cols="50"/></TEXTAREA></TD>
<TD>
<SELECT id="chB.WF" onChange="onWaveformChanged('chB.WF', 1)">
<OPTION value="sine">Sine</OPTION>
<OPTION value="square"/>Square</OPTION>
<OPTION value="sawtooth">Sawtooth</OPTION>
<OPTION value="triangle">Triangle</OPTION>
</SELECT>
</TD>
</TR>
<TR>
<TD>C</TD>
<TD><TEXTAREA id="chC.MML" rows="3" cols="50"/></TEXTAREA></TD>
<TD>
<SELECT id="chC.WF" onChange="onWaveformChanged('chC.WF', 2)">
<OPTION value="sine">Sine</OPTION>
<OPTION value="square"/>Square</OPTION>
<OPTION value="sawtooth">Sawtooth</OPTION>
<OPTION value="triangle">Triangle</OPTION>
</SELECT>
</TD>
</TR>
<TR>
<TD><INPUT TYPE="submit" id="button" value="Play" onCLick="onButtonClicked()"></TD>
<TD>
<select id="musicSelect" onChange="onSelectionChanged()" >
<option value="mgaTheme">Maze of Galious</option>
<option value="furelise">Fur Elise</option>
<option value="hinotori">Firebird</option>
</select>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>