-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMusicMaker.ts
660 lines (527 loc) · 19.1 KB
/
MusicMaker.ts
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
654
655
656
657
658
659
660
var Util = require('jsmidgen').Util;
export var beat = 128;
export class DrumNotes {
static BassDrum1: number = 35;
static SideStick: number = 37;
static SnareDrum: number = 38;
static HandClap: number = 39;
static ElectricSnare: number = 40;
static LowFloorTom: number = 41;
static HighFloorTom: number = 43;
static LowTom: number = 45;
static LowMidTom: number = 47;
static HiMidTom: number = 48;
static Tambourine: number = 54;
static ClosedHighHat: number = 42;
static OpenHighHat: number = 46;
static Bongo1: number = 60;
static Bongo2: number = 61;
static Congo1: number = 62;
static Congo2: number = 63;
static Congo3: number = 64;
static Timbale1: number = 65;
static Timbale2: number = 66;
static CowBell: number = 56;
}
export class Instruments {
static AcousticGrandPiano: number = 0;
static BrightAcousticPiano: number = 1;
static ElectricGrandPiano: number = 2;
static Honk3tonkPiano: number = 3;
static ElectricPiano1: number = 4;
static ElectricPiano2: number = 5;
static Harpsichord: number = 6;
static Clavi: number = 7;
static Celesta: number = 8;
static Glockenspiel: number = 9;
static MusicBox: number = 10;
static Vibraphone: number = 11;
static Marimba: number = 12;
static X13lophone: number = 13;
static TubularBells: number = 14;
static Dulcimer: number = 15;
static DrawbarOrgan: number = 16;
static PercussiveOrgan: number = 17;
static RockOrgan: number = 18;
static ChurchOrgan: number = 19;
static ReedOrgan: number = 20;
static Accordion: number = 21;
static Harmonica: number = 22;
static TangoAccordion: number = 23;
static AcousticGuitarN24lon: number = 24;
static AcousticGuitarSteel: number = 25;
static ElectricGuitarJazz: number = 26;
static ElectricGuitarClean: number = 27;
static ElectricGuitarMuted: number = 28;
static OverdrivenGuitar: number = 29;
static DistortionGuitar: number = 30;
static Guitarharmonics: number = 31;
static AcousticBass: number = 32;
static ElectricBassfinger: number = 33;
static ElectricBasspick: number = 34;
static FretlessBass: number = 35;
static SlapBass1: number = 36;
static SlapBass2: number = 37;
static S38nthBass1: number = 38;
static S39nthBass2: number = 39;
static Violin: number = 40;
static Viola: number = 41;
static Cello: number = 42;
static Contrabass: number = 43;
static TremoloStrings: number = 44;
static PizzicatoStrings: number = 45;
static OrchestralHarp: number = 46;
static Timpani: number = 47;
static StringEnsemble1: number = 48;
static StringEnsemble2: number = 49;
static S50nthStrings1: number = 50;
static S51nthStrings2: number = 51;
static ChoirAahs: number = 52;
static VoiceOohs: number = 53;
static S54nthVoice: number = 54;
static OrchestraHit: number = 55;
static Trumpet: number = 56;
static Trombone: number = 57;
static Tuba: number = 58;
static MutedTrumpet: number = 59;
static FrenchHorn: number = 60;
static BrassSection: number = 61;
static S62nthBrass1: number = 62;
static S63nthBrass2: number = 63;
static SopranoSax: number = 64;
static AltoSax: number = 65;
static TenorSax: number = 66;
static BaritoneSax: number = 67;
static Oboe: number = 68;
static EnglishHorn: number = 69;
static Bassoon: number = 70;
static Clarinet: number = 71;
static Piccolo: number = 72;
static Flute: number = 73;
static Recorder: number = 74;
static PanFlute: number = 75;
static BlownBottle: number = 76;
static Shakuhachi: number = 77;
static Whistle: number = 78;
static Ocarina: number = 79;
static Lead1square: number = 80;
static Lead2sawtooth: number = 81;
static Lead3calliope: number = 82;
static Lead4chiff: number = 83;
static Lead5charang: number = 84;
static Lead6voice: number = 85;
static Lead7fifths: number = 86;
static Lead8BassLead: number = 87;
static Pad1newage: number = 88;
static Pad2warm: number = 89;
static Pad3pol90s90nth: number = 90;
static Pad4choir: number = 91;
static Pad5bowed: number = 92;
static Pad6metallic: number = 93;
static Pad7halo: number = 94;
static Pad8sweep: number = 95;
static FX1rain: number = 96;
static FX2soundtrack: number = 97;
static FX3cr98stal: number = 98;
static FX4atmosphere: number = 99;
static FX5brightness: number = 100;
static FX6goblins: number = 101;
static FX7echoes: number = 102;
static FX8scifi: number = 103;
static Sitar: number = 104;
static Banjo: number = 105;
static Shamisen: number = 106;
static Koto: number = 107;
static Kalimba: number = 108;
static Bagpipe: number = 109;
static Fiddle: number = 110;
static Shanai: number = 111;
static TinkleBell: number = 112;
static Agogo: number = 113;
static SteelDrums: number = 114;
static Woodblock: number = 115;
static TaikoDrum: number = 116;
static MelodicTom: number = 117;
static S118nthDrum: number = 118;
static ReverseC119mbal: number = 119;
static GuitarFretNoise: number = 120;
static BreathNoise: number = 121;
static Seashore: number = 122;
static BirdTweet: number = 123;
static TelephoneRing: number = 124;
static Helicopter: number = 125;
static Applause: number = 126;
static Gunshot: number = 127;
}
export function GetNoteNumber(aNote) {
if (typeof (aNote) == 'number') {
return aNote
} else {
var note = Util.midiPitchFromNote(aNote);
note = parseInt(note);
return note;
}
}
export enum ChordType {
Major, Minor, M7, Major7, Minor7
}
export class ChordChange {
public Chord;
public Length: number;
constructor(chord, length: number) {
this.Chord = chord;
this.Length = length;
}
}
export function MakeChord(root, type: ChordType) {
var intRoot = GetNoteNumber(root);
var aChord = new Array();
if (type == ChordType.Major) {
aChord.push(intRoot);
aChord.push(intRoot + 4);
aChord.push(intRoot + 7);
}
else if (type == ChordType.Minor) {
aChord.push(intRoot);
aChord.push(intRoot + 3);
aChord.push(intRoot + 7);
}
else if (type == ChordType.Major7) {
aChord.push(intRoot);
aChord.push(intRoot + 4);
aChord.push(intRoot + 7);
aChord.push(intRoot + 11);
}
else if (type == ChordType.M7) {
aChord.push(intRoot);
aChord.push(intRoot + 4);
aChord.push(intRoot + 7);
aChord.push(intRoot + 10);
}
else if (type == ChordType.Minor7) {
aChord.push(intRoot);
aChord.push(intRoot + 3);
aChord.push(intRoot + 7);
aChord.push(intRoot + 10);
}
else {
throw new Error("Help...Handle new chord type");
}
return aChord;
}
export function SelectRandom(myArray) {
var rand = myArray[Math.floor(Math.random() * myArray.length)];
return rand;
}
export function Repeat(count: number, functionToRepeat) {
var k;
for (k = 0; k < count; k++) {
functionToRepeat();
}
}
export function AddRhythmPattern(track, strPattern: string, note: number) {
var i = 0;
for (i = 0; i < strPattern.length; i++) {
var currentChar = strPattern[i];
if (currentChar == "x") {
track.addNote(9, note, beat / 4)
}
else if (currentChar != "|") {
track.addNote(9, 0, beat / 4, 0);
}
}
}
export enum ScaleType {
Major, Minor, MinorPentatonic, MajorPentatonic, Blues, Spanish
}
export function MakeScale(note, type: ScaleType, octaves: number) {
var intStartNote = GetNoteNumber(note);
var scale = new Array();
if (type == ScaleType.Major) {
var currentNote = intStartNote;
scale.push(currentNote);
var k;
for (k = 0; k < octaves; k++) {
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 1;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 1;
scale.push(currentNote);
}
}
else if (type == ScaleType.Minor) {
var currentNote = intStartNote;
scale.push(currentNote);
var k;
for (k = 0; k < octaves; k++) {
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 1;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 1;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
}
}
else if (type == ScaleType.Spanish) {
var currentNote = intStartNote;
scale.push(currentNote);
var k;
for (k = 0; k < octaves; k++) {
currentNote = currentNote + 1;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
currentNote = currentNote + 1;
scale.push(currentNote);
currentNote = currentNote + 2;
scale.push(currentNote);
}
}
else if (type == ScaleType.MinorPentatonic) {
var currentNote = intStartNote;
scale.push(currentNote);
var k;
for (k = 0; k < octaves; k++) {
currentNote = currentNote + 3; //b3
scale.push(currentNote);
currentNote = currentNote + 2; //4
scale.push(currentNote);
currentNote = currentNote + 2; // 5
scale.push(currentNote);
currentNote = currentNote + 3; // b7
scale.push(currentNote);
currentNote = currentNote + 2; // root
scale.push(currentNote);
}
}
else if (type == ScaleType.Blues) {
var currentNote = intStartNote;
scale.push(currentNote);
var k;
for (k = 0; k < octaves; k++) {
currentNote = currentNote + 3; //b3
scale.push(currentNote);
currentNote = currentNote + 2; //4
scale.push(currentNote);
scale.push(currentNote + 1);
currentNote = currentNote + 2; // 5
scale.push(currentNote);
currentNote = currentNote + 3; // b7
scale.push(currentNote);
currentNote = currentNote + 2; // root
scale.push(currentNote);
}
}
else if (type == ScaleType.MajorPentatonic) {
var currentNote = intStartNote;
scale.push(currentNote);
var k;
for (k = 0; k < octaves; k++) {
currentNote = currentNote + 4; //2
scale.push(currentNote);
currentNote = currentNote + 1; //3
scale.push(currentNote);
currentNote = currentNote + 2; //5
scale.push(currentNote);
currentNote = currentNote + 4; //6
scale.push(currentNote);
currentNote = currentNote + 1; // root
scale.push(currentNote);
}
}
else {
throw new Error("Scale type not handled")
}
return scale
}
export abstract class AbstractChordPlayer {
abstract PlayFourBarPattern(track, channel, chordChange);
abstract PlayThreeBarPattern(track, channel, chordChange);
abstract PlayTwoBarPattern(track, channel, chordChange);
abstract PlayOneBarPattern(track, channel, chordChange);
PlayFromChordChanges(track, chordList, channel) {
for (var chordChange of chordList) {
if (chordChange.Length == 2) {
this.PlayTwoBarPattern(track, channel, chordChange);
}
else if (chordChange.Length == 4) {
this.PlayFourBarPattern(track, channel, chordChange);
}
else if (chordChange.Length == 3) {
this.PlayFourBarPattern(track, channel, chordChange);
}
else if (chordChange.Length == 1) {
this.PlayOneBarPattern(track, channel, chordChange);
} else {
track.addChord(channel, chordChange.Chord, chordChange.Length * 128);
}
}
}
}
export class Arpeggio1 extends AbstractChordPlayer {
PlayFourBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0], beat / 2);
track.addNote(channel, chordChange.Chord[0] + 2, beat / 2);
track.addNote(channel, chordChange.Chord[1], beat / 2);
track.addNote(channel, chordChange.Chord[2], beat / 2);
track.addNote(channel, chordChange.Chord[0], beat / 2);
track.addNote(channel, chordChange.Chord[0] + 2, beat / 2);
track.addNote(channel, chordChange.Chord[1], beat / 2);
track.addNote(channel, chordChange.Chord[2], beat / 2);
}
PlayThreeBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0], beat / 2);
track.addNote(channel, chordChange.Chord[0] + 2, beat / 2);
track.addNote(channel, chordChange.Chord[1], beat / 2);
track.addNote(channel, chordChange.Chord[2], beat / 2);
track.addNote(channel, chordChange.Chord[0], beat / 2);
track.addNote(channel, chordChange.Chord[0] + 2, beat / 2);
}
PlayTwoBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0], beat / 2);
track.addNote(channel, chordChange.Chord[0] + 2, beat / 2);
track.addNote(channel, chordChange.Chord[1], beat / 2);
track.addNote(channel, chordChange.Chord[2], beat / 2);
}
PlayOneBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0], beat / 2);
track.addNote(channel, chordChange.Chord[2], beat / 2);
}
}
export class BassPLayer1 extends AbstractChordPlayer {
noteDelta = 24;
PlayFourBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat * 3);
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat);
}
PlayThreeBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat * 3);
}
PlayTwoBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat * 2);
}
PlayOneBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat * 1);
}
}
export class BassPLayer2 extends AbstractChordPlayer {
noteDelta = 24;
PlayFourBarPattern(track, channel, chordChange) {
var i = 0;
for (i = 0; i < 8; i++)
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat / 2);
}
PlayThreeBarPattern(track, channel, chordChange) {
var i = 0;
for (i = 0; i < 6; i++)
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat / 2);
}
PlayTwoBarPattern(track, channel, chordChange) {
var i;
for (i = 0; i < 4; i++)
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat / 2);
}
PlayOneBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat / 2);
track.addNote(channel, chordChange.Chord[0] - this.noteDelta, beat / 2);
}
}
export class RandomPlayer extends AbstractChordPlayer {
PlayFourBarPattern(track, channel, chordChange) {
var i = 0;
for (i = 0; i < 8; i++) {
track.addNote(channel, SelectRandom(chordChange.Chord), beat / 2);
}
}
PlayThreeBarPattern(track, channel, chordChange) {
var i = 0;
for (i = 0; i < 6; i++) {
track.addNote(channel, SelectRandom(chordChange.Chord), beat / 2);
}
}
PlayTwoBarPattern(track, channel, chordChange) {
var i;
for (i = 0; i < 4; i++) {
track.addNote(channel, SelectRandom(chordChange.Chord), beat / 2);
}
}
PlayOneBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0], beat / 2);
track.addNote(channel, chordChange.Chord[0] + 12, beat / 2);
}
}
export class BassPLayer3 extends AbstractChordPlayer {
PlayFourBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - 12, beat * 1.5);
track.addNote(channel, chordChange.Chord[0] - 12, beat * 1.5);
track.addNote(channel, chordChange.Chord[0] - 12, beat);
}
PlayThreeBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - 12, beat * 1.5);
track.addNote(channel, chordChange.Chord[0] - 12, beat * 1.5);
track.addNote(channel, chordChange.Chord[0] - 12, beat);
}
PlayTwoBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - 12, beat * 2);
}
PlayOneBarPattern(track, channel, chordChange) {
track.addNote(channel, chordChange.Chord[0] - 12, beat);
}
}
export class SimplePlayer extends AbstractChordPlayer {
PlayFourBarPattern(track, channel, chordChange) {
track.addChord(channel, chordChange.Chord, chordChange.Length * beat);
}
PlayThreeBarPattern(track, channel, chordChange) {
track.addChord(channel, chordChange.Chord, chordChange.Length * beat);
}
PlayTwoBarPattern(track, channel, chordChange) {
track.addChord(channel, chordChange.Chord, chordChange.Length * beat);
}
PlayOneBarPattern(track, channel, chordChange) {
track.addChord(channel, chordChange.Chord, chordChange.Length * beat);
}
}
export class OffBeatPlayer extends AbstractChordPlayer {
PlayFourBarPattern(track, channel, chordChange) {
track.addNote(channel, 0, beat, 0)
track.addChord(channel, chordChange.Chord, beat);
track.addNote(channel, 0, beat, 0)
track.addChord(channel, chordChange.Chord, beat);
}
PlayThreeBarPattern(track, channel, chordChange) {
track.addNote(channel, 0, beat, 0)
track.addChord(channel, chordChange.Chord, beat);
track.addNote(channel, 0, beat, 0)
}
PlayTwoBarPattern(track, channel, chordChange) {
track.addNote(channel, 0, beat, 0)
track.addChord(channel, chordChange.Chord, beat);
}
PlayOneBarPattern(track, channel, chordChange) {
track.addChord(channel, chordChange.Chord, beat);
}
}