-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaleDegree.js
More file actions
1245 lines (1132 loc) · 43.4 KB
/
scaleDegree.js
File metadata and controls
1245 lines (1132 loc) · 43.4 KB
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// scaleDegree.js
//
// import {mergeDurationVelocityAndPitch} from '../PracticeRoom/Rhythm.js';
// to create documentation:
// %: cd ~/Documents/Github/scaleDegreeJS
// %: documentation build scaleDegree.js -f html -o docs
/**
* @overview This module is a IIFE (Immediately Invoked Function Expression) and
* it returns a var with the name of scaleDegree. This document contains its public API.
* <br><br>This module uses the jazz method of describing a major scale: 1 2 3 4 5 6 7 8
* and it uses # and b as modifiers. They must be a string (not numbers) so that
* sharps (#) or flats (b) can be used to modify
* <br>i.e. '1 b2 b3 4 5 b6 b7 8' represents a phrygian scale.
* These patterns can be transposed to any starting location with a MIDI number offset.
* The output of this library can be used with Tone.js to play notes and chords.
* @example
* <script src="scaleDegree.js"></script>
*...
* var myChordProg = scaleDegree.makeII_V_I_Major("A4")
* var myBassline = scaleDegree.makeII_V_I_Major_WalkBass("A4")
* these can be used with Tone.js
*
* @module scaleDegree.js
* @returns {object} scaleDegree object
*/
var scaleDegree = (function() {
// Expanded to include chord extension terminolgy
var scaleDegreeToHalfSteps = {
'b1' : -1,
'1' : 0,
'#1': 1,
'b2': 1,
'sus2': 2,
'2': 2,
'#2': 3,
'b3': 3,
'3': 4,
'#3': 5,
'b4': 4,
'sus': 5,
'4': 5,
'#4': 6,
'b5': 6,
'5': 7,
'#5': 8,
'b6': 8,
'6' : 9,
'#6': 10,
'd7': 9,
'b7': 10,
'7': 11,
'#7': 12,
'b8': 11,
'8': 12,
'#8': 13,
'b9': 13,
'9': 14,
'#9': 15,
'b10': 15,
'10': 16,
'#10': 17,
'b11': 16,
'11': 17,
'#11': 18,
'b12': 18,
'12': 19,
'#12': 20,
'b13': 20,
'13': 21,
'b14': 22,
'14': 23,
'15': 24
};
var SHARP_NAMES = ['B#', 'C#', 'Cx', 'D#', 'E', 'E#', 'F#', 'Fx', 'G#', 'Gx', 'A#', 'B'];
var FLAT_NAMES = ['C', 'Db', 'D', 'Eb', 'Fb', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'Cb'];
var OTHER_NAMES = ['Dbb', 'Bx', 'Ebb', 'Fbb', 'Dx', 'Gbb', 'Ex', 'Abb', 'Ab', 'Bbb', 'Cbb', 'Ax'];
var MIDI_SHARP_NAMES = ['B#_0', 'C#_1', 'Cx_1', 'D#_1', 'E_1', 'E#_1', 'F#_1', 'Fx_1', 'G#_1', 'Gx_1', 'A#_1', 'B_1',
'B#_1', 'C#0', 'Cx0', 'D#0', 'E0', 'E#0', 'F#0', 'Fx0', 'G#0', 'Gx0', 'A#0', 'B0',
'B#0', 'C#1', 'Cx1', 'D#1', 'E1', 'E#1', 'F#1', 'Fx1', 'G#1', 'Gx1', 'A#1', 'B1',
'B#1', 'C#2', 'Cx2', 'D#2', 'E2', 'E#2', 'F#2', 'Fx2', 'G#2', 'Gx2', 'A#2', 'B2',
'B#2', 'C#3', 'Cx3', 'D#3', 'E3', 'E#3', 'F#3', 'Fx3', 'G#3', 'Gx3', 'A#3', 'B3',
'B#3', 'C#4', 'Cx4', 'D#4', 'E4', 'E#4', 'F#4', 'Fx4', 'G#4', 'Gx4', 'A#4', 'B4',
'B#4', 'C#5', 'Cx5', 'D#5', 'E5', 'E#5', 'F#5', 'Fx5', 'G#5', 'Gx5', 'A#5', 'B5',
'B#5', 'C#6', 'Cx6', 'D#6', 'E6', 'E#6', 'F#6', 'Fx6', 'G#6', 'Gx6', 'A#6', 'B6',
'B#6', 'C#7', 'Cx7', 'D#7', 'E7', 'E#7', 'F#7', 'Fx7', 'G#7', 'Gx7', 'A#7', 'B7',
'B#7', 'C#8', 'Cx8', 'D#8', 'E8', 'E#8', 'F#8', 'Fx8', 'G#8', 'Gx8', 'A#8', 'B8',
'B#8', 'C#9', 'Cx9', 'D#9', 'E9', 'E#9', 'F#9', 'Fx9'];
var MIDI_FLAT_NAMES = ['C_1', 'Db_1', 'D_1', 'Eb_1', 'Fb_1', 'F_1', 'Gb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nat_minor_pattern = '1 2 b3 4 5 b6 b7 8';
harm_minor_pattern = '1 2 b3 4 5 b6 7 8';
mel_minor_pattern = '1 2 b3 4 5 6 7 8';
ionian = '1 2 3 4 5 6 7 8';
dorian = '1 2 b3 4 5 6 b7 8';
phrygian = '1 b2 b3 4 5 b6 b7 8';
lydian = '1 2 3 #4 5 6 7 8';
mixolydian = '1 2 3 4 5 6 b7 8';
aeolian = '1 2 b3 4 5 b6 b7 8';
locrian = '1 b2 b3 4 b5 b6 b7 8';
rel_dorian = '2 3 4 5 6 7 8 9';
rel_phrygian = '3 4 5 6 7 8 9 10';
rel_lydian = '4 5 6 7 8 9 10 11';
rel_mixolydian = ',5 ,6 ,7 1 2 3 4 5';
rel_aeolian = ',6 ,7 1 2 3 4 5 6';
rel_locrian = ',7 1 2 3 4 5 6 7';
min_pentatonic = '1 b3 4 5 b7 8';
maj_pentatonic = '1 2 3 5 6 8';
min_blues = '1 b3 4 b5 5 b7 8';
maj_blues = '1 2 b3 3 5 6 8';
var octaveUp = 'u';
var octaveDown = ','
// walking basslines
var DomWalkPattern2bars_v1 = '1 3 5 6 8 6 5 3';
var DomWalkPattern2bars_v2 = '1 3 5 6 b7 6 5 3';
var DomWalkPattern2bars_v3 = '8 b7 6 b6 5 4 b3 3';
var DomWalkPattern2bars_v4 = '1 8 b7 5 1 5 4 3';
var DomWalkPattern2bars_v5 = '1 3 4 #4 5 4 b3 2';
var DomWalkPattern1bar_v1 = '1 8 b7 5';
var DomWalkPattern1bar_v2 = '1 2 b3 3';
var DomWalkPattern1bar_v3 = '1 3 4 5';
var DomWalkPattern1bar_v4 = '1 3 5 6';
var BluesTurnaround_v1 = '1 b7 6 b6 5 4 b3 2';
var BluesTurnaround_v2 = '1 b7 6 b6 5 b3 2 b2';
var BluesTurnaround_v3 = '1 3 4 #4 5 6 b7 7';
var BluesTurnaround_v4 = '1 b7 6 b3 2 b6 5 b2';
// preceeding comma i.e. ',8' means an octave lower
var BluesTurnaround_v5 = '1 #5 6 #1 2 #4 5 '+octaveDown+'7';
// chord voicings
var Dom9_v1 = '1 3 b7 9 12';
var Dom9_v2 = '1 3 b7 9';
var Dom9_v3 = '3 b7 9 12';
var Dom9_v4 = 'b7 10 12 15';
var Dom13_v1 = '1 b7 10 13';
var Dom13_v2 = 'b7 10 13 15';
var Dom13_v3 = '3 b7 9 13';
var Dom13_v4 = 'b7 10 13';
// preceeding u i.e. 'u9' means an octave higher
var Dom13_v4 = 'b7 10 13 '+octaveUp+'9';
var II_V_I_MAJOR_WALKBASS = [
'2 4 6 4 5 4 3 2 1 2 3 5 8 7 5 3',
'2 1 ,7 ,6 ,5 ,6 ,b7 ,7 1 2 b3 3 1 ,7 ,6 ,5',
'2 3 4 #4 5 6 b7 7 8 7 6 5 4 3 2 1',
'2 1 ,7 ,6 ,5 4 3 2 1 ,7 ,6 ,5 ,6 3 2 1',
'2 4 6 b6 5 4 2 ,7 1 3 5 4 3 2 1 3'
];
var II_V_I_MINOR_WALKBASS = [
'2 4 b6 4 5 4 b3 2 1 2 b3 5 8 5 4 b3',
'2 4 5 b6 5 4 b3 2 1 b3 5 4 b3 2 1 ,5',
'2 b3 4 #4 5 6 b7 7 8 b7 b6 5 4 b3 2 1',
'2 1 ,b7 ,b6 ,5 4 b3 2 1 ,5 1 2 b3 5 4 b3',
'2 4 b6 #4 5 4 2 ,7 1 b3 5 4 b3 2 1 b3'
];
function getRandom(low, high) {
diceMin = Math.ceil(low);
diceMax = Math.floor(high);
return Math.floor(Math.random() * (diceMax - diceMin + 1)) + diceMin; //The maximum is inclusive and the minimum is inclusive
};
//------------------------------------
// I-V-I progression templates
// major key
/**
* this function creates a II-V-I voicing in major
* @public
* @param {string} key
* @param {string} optionalStartTime
* @param {string} optionalRhythm
* @param {string} optionalVoicing
* @returns {object} array used with Tone.js to play chord progression
*/
function makeII_V_I_Major(key, optionalStartTime, optionalRhythm, optionalVoicing) {
var chordRhythm;
var myStartTime;
var chordProg;
if(optionalStartTime == undefined)
myStartTime = "0";
else
myStartTime = optionalStartTime.toString();
// choose one:
var whichVoicing = getRandom(0,2);
if(whichVoicing == 0) {
chordProg = scaleDegree.II_V_I_MAJOR_v1(key);
} else if(whichVoicing == 1) {
chordProg = scaleDegree.II_V_I_MAJOR_v2(key);
} else {
chordProg = scaleDegree.II_V_I_MAJOR_v3(key);
}
if(optionalRhythm == undefined) {
chordRhythm = ['2n+4n','8n','|','8n+4n','8nr','8n','2nr','|','4n','8nr','8n','2nr','2n+4n','4nr'];
} else {
chordRhythm = optionalRhythm;
}
var rhythmAndChords = makeRhythmAndChordProg(chordRhythm, chordProg);
var notesAndRhythm = Rhythm.mergeDurationVelocityAndPitch(rhythmAndChords[0], rhythmAndChords[1], 0, myStartTime);
return notesAndRhythm;
}
/**
* this function creates a II-V-I voicing in minor
* @public
* @param {string} key
* @param {string} optionalStartTime
* @param {string} optionalRhythm
* @param {string} optionalVoicing
* @returns {object} array used with Tone.js to play chord progression
*/
function makeII_V_I_Minor(key, optionalStartTime, optionalRhythm, optionalVoicing) {
var chordRhythm;
var myStartTime;
var chordProg;
if(optionalStartTime == undefined)
myStartTime = "0";
else
myStartTime = optionalStartTime.toString();
// choose one:
var whichVoicing = getRandom(0,2);
if(whichVoicing == 0) {
chordProg = scaleDegree.II_V_I_MINOR_v1(key);
} else if(whichVoicing == 1) {
chordProg = scaleDegree.II_V_I_MINOR_v2(key);
} else {
chordProg = scaleDegree.II_V_I_MINOR_v3(key);
}
if(optionalRhythm == undefined) {
chordRhythm = ['2n+4n','8n','|','8n+4n','8nr','8n','2nr','|','4n','8nr','8n','2nr','2n+4n','4nr'];
} else {
chordRhythm = optionalRhythm;
}
var rhythmAndChords = makeRhythmAndChordProg(chordRhythm, chordProg);
var notesAndRhythm = Rhythm.mergeDurationVelocityAndPitch(rhythmAndChords[0], rhythmAndChords[1], 0, myStartTime);
return notesAndRhythm;
}
/**
* this function creates a II-V-I voicing array in major
* @public
* @param {string} key
* @returns {string} array of [name, root, voicing] for each of the II V and I chords
*/
function II_V_I_MAJOR_v1(key) {
// var IChordRootOctave = '3';
// var IIChordRootOctave = '3';
// var VChordRootOctave = '3';
var IChordRootOctave = '2';
var IIChordRootOctave = '2';
var VChordRootOctave = '2';
if( key.includes('C') || key.includes('D') || key.includes('E') ) {
IChordRootOctave = '3';
IIChordRootOctave = '3';
VChordRootOctave = '2';
} else if( key.includes('B') ) {
IChordRootOctave = '2';
IIChordRootOctave = '3';
VChordRootOctave = '2'
} else if( key.includes('A') ) {
IChordRootOctave = '2';
IIChordRootOctave = '2';
VChordRootOctave = '2'
}
var voicingArray = [[getRoot(key, '2')+'m7', getRoot(key, '2')+IIChordRootOctave, '1 b3 b7 9'], [getRoot(key, '5')+'7', getRoot(key, '5')+VChordRootOctave, '1 b7 3 13'], [getRoot(key, '1')+'ma7', getRoot(key, '1')+IChordRootOctave, '1 3 7 9']];
return voicingArray;
}
/**
* this function creates a II-V-I voicing array in major
* @public
* @param {string} key
* @returns {string} array of [name, root, voicing] for each of the II V and I chords
*/
function II_V_I_MAJOR_v2(key) {
// var IChordRootOctave = '3';
// var IIChordRootOctave = '3';
// var VChordRootOctave = '3';
var IChordRootOctave = '2';
var IIChordRootOctave = '2';
var VChordRootOctave = '2';
if( key.includes('C') || key.includes('D') || key.includes('E') ) {
IChordRootOctave = '3';
IIChordRootOctave = '3';
VChordRootOctave = '2';
} else if( key.includes('B') ) {
IChordRootOctave = '2';
IIChordRootOctave = '3';
VChordRootOctave = '2'
} else if( key.includes('A') ) {
IChordRootOctave = '2';
IIChordRootOctave = '2';
VChordRootOctave = '2'
}
var voicingArray = [[getRoot(key, '2')+'m7', getRoot(key, '2')+IIChordRootOctave, '1 b7 b3 5'], [getRoot(key, '5')+'7', getRoot(key, '5')+VChordRootOctave, '8 3 b7 9'], [getRoot(key, '1')+'ma7', getRoot(key, '1')+IChordRootOctave, '1 7 3 5']];
return voicingArray;
}
/**
* this function creates a II-V-I voicing array in major
* @public
* @param {string} key
* @returns {string} array of [name, root, voicing] for each of the II V and I chords
*/
function II_V_I_MAJOR_v3(key) {
// var IChordRootOctave = '3';
// var IIChordRootOctave = '3';
// var VChordRootOctave = '3';
var IChordRootOctave = '2';
var IIChordRootOctave = '2';
var VChordRootOctave = '2';
if( key.includes('C') || key.includes('D') || key.includes('E') ) {
IChordRootOctave = '3';
IIChordRootOctave = '3';
VChordRootOctave = '2';
} else if( key.includes('B') ) {
IChordRootOctave = '2';
IIChordRootOctave = '3';
VChordRootOctave = '2'
} else if( key.includes('A') ) {
IChordRootOctave = '2';
IIChordRootOctave = '2';
VChordRootOctave = '2'
}
var voicingArray = [[getRoot(key, '2')+'m7', getRoot(key, '2')+IIChordRootOctave, '1 5 b7 b3'], [getRoot(key, '5')+'7', getRoot(key, '5')+VChordRootOctave, '1 8 3 b7'], [getRoot(key, '1')+'ma7', getRoot(key, '1')+IChordRootOctave, '1 5 7 3']];
return voicingArray;
}
// minor key
/**
* this function creates a II-V-I voicing array in minor
* @public
* @param {string} key
* @returns {string} array of [name, root, voicing] for each of the II V and I chords
*/
function II_V_I_MINOR_v1(key) {
var IChordRootOctave = '3';
var IIChordRootOctave = '3';
var VChordRootOctave = '3';
if( key.includes('C') || key.includes('D') || key.includes('E') ) {
IChordRootOctave = '3';
IIChordRootOctave = '3';
VChordRootOctave = '2';
} else if( key.includes('B') ) {
IChordRootOctave = '2';
IIChordRootOctave = '3';
VChordRootOctave = '2'
} else if( key.includes('A') ) {
IChordRootOctave = '2';
IIChordRootOctave = '2';
VChordRootOctave = '2'
}
var voicingArray = [[getRoot(key, '2')+'m7b5', getRoot(key, '2')+IIChordRootOctave, '1 b5 b7 b3'], [getRoot(key, '5')+'7', getRoot(key, '5')+VChordRootOctave, '1 b7 3 b13'], [getRoot(key, '1')+'mi6', getRoot(key, '1')+IChordRootOctave, '1 b3 6 9']];
return voicingArray;
}
/**
* this function creates a II-V-I voicing array in minor
* @public
* @param {string} key
* @returns {string} array of [name, root, voicing] for each of the II V and I chords
*/
function II_V_I_MINOR_v2(key) {
var IChordRootOctave = '3';
var IIChordRootOctave = '3';
var VChordRootOctave = '3';
if( key.includes('C') || key.includes('D') || key.includes('E') ) {
IChordRootOctave = '3';
IIChordRootOctave = '3';
VChordRootOctave = '2';
} else if( key.includes('B') ) {
IChordRootOctave = '2';
IIChordRootOctave = '3';
VChordRootOctave = '2'
} else if( key.includes('A') ) {
IChordRootOctave = '2';
IIChordRootOctave = '2';
VChordRootOctave = '2'
}
var voicingArray = [[getRoot(key, '2')+'m7b5', getRoot(key, '2')+IIChordRootOctave, '1 b7 b3 b5'], [getRoot(key, '5')+'7', getRoot(key, '5')+VChordRootOctave, '8 3 b7 b9'], [getRoot(key, '1')+'mi6', getRoot(key, '1')+IChordRootOctave, '1 6 b3 5']];
return voicingArray;
}
/**
* this function creates a II-V-I voicing array in minor
* @public
* @param {string} key
* @returns {string} array of [chordName, root, voicing] for each of the II V and I chords
*/
function II_V_I_MINOR_v3(key) {
// var IChordRootOctave = '3';
// var IIChordRootOctave = '3';
// var VChordRootOctave = '3';
var IChordRootOctave = '2';
var IIChordRootOctave = '2';
var VChordRootOctave = '2';
if( key.includes('C') || key.includes('D') || key.includes('E') ) {
IChordRootOctave = '3';
IIChordRootOctave = '3';
VChordRootOctave = '2';
} else if( key.includes('B') ) {
IChordRootOctave = '2';
IIChordRootOctave = '3';
VChordRootOctave = '2'
} else if( key.includes('A') ) {
IChordRootOctave = '2';
IIChordRootOctave = '2';
VChordRootOctave = '2'
}
var voicingArray = [[getRoot(key, '2')+'m7', getRoot(key, '2')+IIChordRootOctave, '1 b5 b7 b3'], [getRoot(key, '5')+'7', getRoot(key, '5')+VChordRootOctave, '1 8 3 b7'], [getRoot(key, '1')+'ma7', getRoot(key, '1')+IChordRootOctave, '1 5 6 b3']];
return voicingArray;
}
/**
* this function creates a II-V-I walking bassline in major
* @public
* @param {string} key
* @param {string} optionalStartTime
* @param {string} optionalRhythm
* @param {string} optionalScaleDegrees
* @returns {object} array used with Tone.js to play walking bass pattern
*/
function makeII_V_I_Major_WalkBass(key, optionalStartTime, optionalRhythm, optionalScaleDegrees) {
var basslineRootOctave = '3';
var bassScaleDegrees;
var bassRhythm;
var myStartTime;
if(optionalStartTime == undefined)
myStartTime = "0";
else
myStartTime = optionalStartTime.toString();
if(optionalRhythm == undefined) {
bassRhythm = ['4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n'];
} else {
bassRhythm = optionalRhythm;
}
var upperLimit = II_V_I_MAJOR_WALKBASS.length-1;
if(optionalScaleDegrees == undefined) {
bassScaleDegrees = II_V_I_MAJOR_WALKBASS[getRandom(0, upperLimit)];
} else {
bassScaleDegrees = optionalScaleDegrees;
}
console.log(bassScaleDegrees);
// var bass = scaleDegree.makeFreqArray(bassScaleDegrees, key+basslineRootOctave);
var bass = scaleDegree.makeNoteArray(bassScaleDegrees, key+basslineRootOctave);
var myBass = Rhythm.mergeDurationVelocityAndPitch(bassRhythm, bass, 0, myStartTime);
return myBass;
}
/**
* this function creates a II-V-I walking bassline in minor
* @public
* @param {string} key
* @param {string} optionalStartTime
* @param {string} optionalRhythm
* @param {string} optionalScaleDegrees
* @returns {object} array used with Tone.js to play walking bass pattern
*/
function makeII_V_I_Minor_WalkBass(key, optionalStartTime, optionalRhythm, optionalScaleDegrees) {
var basslineRootOctave = '3';
var bassScaleDegrees;
var bassRhythm;
var myStartTime;
if(optionalStartTime == undefined)
myStartTime = "0";
else
myStartTime = optionalStartTime.toString();
if(optionalRhythm == undefined) {
bassRhythm = ['4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n','4n'];
} else {
bassRhythm = optionalRhythm;
}
var upperLimit = II_V_I_MINOR_WALKBASS.length-1;
if(optionalScaleDegrees == undefined) {
bassScaleDegrees = II_V_I_MINOR_WALKBASS[getRandom(0, upperLimit)];
} else {
bassScaleDegrees = optionalScaleDegrees;
}
// var bass = scaleDegree.makeFreqArray(bassScaleDegrees, key+basslineRootOctave);
var bass = scaleDegree.makeNoteArray(bassScaleDegrees, key+basslineRootOctave);
var myBass = Rhythm.mergeDurationVelocityAndPitch(bassRhythm, bass, 0, myStartTime);
return myBass;
}
//---------------------------------------*/
/**
* this function translates a note name to a MIDI number
* @private
* @param {string} noteName
* @returns {number} MIDI number of the note name
*/
function noteNameToMIDI(noteName) {
var i;
var MIDInumber = -1; // default if not found
for(i=0; i < MIDI_SHARP_NAMES.length; i++) {
if( noteName == MIDI_SHARP_NAMES[i] ||
noteName == MIDI_FLAT_NAMES[i] ||
noteName == MIDI_OTHER_NAMES[i] ) {
MIDInumber = i; // found it
break;
}
}
return MIDInumber;
}
var ALPHA_NAMES = ['A','B','C','D','E','F','G'];
/**
* this function finds the index of the letter name in the ALPHA_NAMES arrayOfNotes
* @private
* @param {string} letter
* @returns {number} index of the letter in the ALPHA_NAMES array, -1 if not found
*/
function findAlphaIndex(letter) {
// look for letter in ALPHA_NAMES
var i;
for (i=0; i<ALPHA_NAMES.length; i++)
if ( letter == ALPHA_NAMES[i] )
return i;
// didn't find it
return -1;
}
/**
* this function create an alpha list with the rootName as it's first element
* @private
* @param {string} rootName
* @returns {string} alpha_list with the rootName as the first element
*/
function makeAlphaList(rootName) {
// This function reorders the ALPHA_NAMES list so that rootName is the first element
var i = 0;
var letter = rootName[0];
// console.log('makeAlphaList(): rootName='+rootName);
var startIndex = findAlphaIndex(letter);
if (startIndex < 0)
console.log("startIndex < 0");
var newList = [];
var index = startIndex;
for (i=0; i<ALPHA_NAMES.length; i++) {
// newList.push(ALPHA_NAMES[index+i % ALPHA_NAMES.length]);
if ( index+i > 6 )
index = index - 7;
newList.push(ALPHA_NAMES[index+i]);
}
newList.push(ALPHA_NAMES[startIndex]);
return newList;
}
/**
* this function creates an alpha list from scaleDegrees and root
* @private
* @param {string} scaleDegrees
* @param {string} root
* @returns {string} alpha list
*/
function makeAlphaListFromFormula(scaleDegrees, root) {
var i = 0;
var oneScaleDegree;
var letter = root[0];
// console.log('makeAlphaListFromFormula(): root='+root);
var startIndex = findAlphaIndex(letter);
if (startIndex < 0)
console.log("startIndex < 0");
var newList = [];
var index = startIndex;
for (i=0; i<scaleDegrees.length; i++) {
if ( (scaleDegrees[i].includes('b') || scaleDegrees[i].includes('#'))
&& (scaleDegrees[i].includes(',') || scaleDegrees[i].includes('u')) ) {
oneScaleDegree = scaleDegrees[i].slice(2);
} else if ( scaleDegrees[i].includes('b') || scaleDegrees[i].includes('#')) {
oneScaleDegree = scaleDegrees[i].slice(1);
} else if ( scaleDegrees[i].includes(',') || scaleDegrees[i].includes('u')) {
oneScaleDegree = scaleDegrees[i].slice(1);
} else {
oneScaleDegree = scaleDegrees[i].slice();
}
/*------------------------------
// newList.push(ALPHA_NAMES[(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length]);
if ( (index + Number(oneScaleDegree)-1) > 6 ) {
index = index - 7;
} else if( index < 0) {
// what to do?
}
//------------------------------------*/
// console.log('(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length='+(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length);
newList.push(ALPHA_NAMES[(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length]);
}
newList.push(ALPHA_NAMES[startIndex]);
return newList;
}
/**
* this function creates an alpha list from scaleDegree and root
* @private
* @param {string} scaleDegree
* @param {string} root
* @returns {string} alpha list
*/
function makeAlphaLetterFromFormula(scaleDegree, root) {
var i = 0;
var oneScaleDegree;
var letter = root[0];
// console.log('makeAlphaLetterFromFormula(): root='+root);
var startIndex = findAlphaIndex(letter);
if (startIndex < 0)
console.log("startIndex < 0");
var alphaLetter = '';
//-------------------------------
var index = startIndex;
if ( (scaleDegree.includes('b') || scaleDegree.includes('#'))
&& (scaleDegree.includes(',') || scaleDegree.includes('u')) ) {
oneScaleDegree = scaleDegree.slice(2);
} else if ( scaleDegree.includes('b') || scaleDegree.includes('#')) {
oneScaleDegree = scaleDegree.slice(1);
} else if ( scaleDegree.includes(',') || scaleDegree.includes('u')) {
oneScaleDegree = scaleDegree.slice(1);
} else {
oneScaleDegree = scaleDegree.slice();
}
// console.log('(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length='+(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length);
alphaLetter = ALPHA_NAMES[(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length];
// newList.push(ALPHA_NAMES[(index + Number(oneScaleDegree)-1) % ALPHA_NAMES.length]);
//-----------------------------------*/
// console.log('alphaLetter='+alphaLetter);
// alphaLetter = ALPHA_NAMES[startIndex]
return alphaLetter;
}
/**
* this function creates a scale formula from scaleString
* @private
* @param {string} scaleString
* @returns {number} array scale formula
*/
function makeScaleFormula(scaleString) {
// console.log('typeof(scaleString)='+ typeof(scaleString) +' scaleString='+scaleString);
// var scaleArray = scaleString.split(' ');
var scaleArray = scaleString.split(/\s+/g); // split at white space
var scaleFormula = [];
var scaleValue = '';
var octaveSymbol = '';
var octaveOffset = 0;
for(let i=0; i < scaleArray.length; i++) {
octaveOffset = 0;
octaveSymbol = '';
if(scaleArray[i].includes(octaveUp) || scaleArray[i].includes(octaveDown) ) {
// strip off the octaveSymbol
octaveSymbol = scaleArray[i].slice(0,1);
scaleValue = scaleArray[i].slice(1);
} else {
scaleValue = scaleArray[i];
}
if(octaveSymbol !== '') {
if(octaveSymbol === octaveDown) {
octaveOffset = -12;
} else if(octaveSymbol === octaveUp) {
octaveOffset = 12;
}
}
scaleFormula.push(scaleDegreeToHalfSteps[scaleValue] + octaveOffset);
}
return scaleFormula;
}
// this function assumes chordString in the form '5 9 3 7' is from low to high
// the 5th is the lowest note and it will put the 3rd above the 9th. The 7th will be the highest note.
/**
* this function creates a chord formula from chordString
* assumes chordString in the form '5 9 3 7' is from low to high.
* the 5th is the lowest note and it will put the 3rd above the 9th.
* The 7th will be the highest note.
* @private
* @param {string} chordString
* @returns {number} array chord formula
*/
function makeChordFormula(chordString) {
// console.log('typeof(chordString)='+ typeof(chordString) +' chordString='+chordString);
// var chordArray = chordString.split(' ');
var chordArray = chordString.split(/\s+/g); // split at white space
var chordFormula = [];
var chordValue = '';
var octaveSymbol = '';
var octaveOffset = 0;
var currValue = 0;
var prevValue = 0;
for(let i=0; i < chordArray.length; i++) {
octaveOffset = 0;
octaveSymbol = '';
if(chordArray[i].includes(octaveUp) || chordArray[i].includes(octaveDown) ) {
// strip off the octaveSymbol
octaveSymbol = scaleArray[i].slice(0,1);
chordValue = chordArray[i].slice(1);
} else {
chordValue = chordArray[i];
}
if(octaveSymbol !== '') {
if(octaveSymbol === octaveDown) {
octaveOffset = -12;
} else if(octaveSymbol === octaveUp) {
octaveOffset = 12;
}
}
currValue = scaleDegreeToHalfSteps[chordValue] + octaveOffset;
while(currValue < prevValue) {
currValue += 12;
}
chordFormula.push(currValue);
prevValue = currValue;
}
return chordFormula;
}
/**
* this function makes an array of objects of a chord progression
* @public
* @param {string} rhythmArray
* @param {string} chordArray
* @returns {object} array used with Tone.js to play chord progression.
*/
function makeRhythmAndChordProg(rhythmArray, chordArray) {
/*
chordArray format:
[['Dm7', 'D3', 'b3 b7 9'], ['G7', 'G2', 'b7 3 13 b9'], ['Cma7', 'C3', '3 7 9 5'],
['Am7', 'A2', '5 9 b3 b7'] ];
rhythmArray format: '|' indicates a chord change
['4n','8nr','8n','2nr','|','4n','8nr','8n','2nr','|','4n','8nr','8n','2nr','|','4n','8nr','8n','2nr']
*/
// do sanity check of chordArray.length-1 == number of '|' in rhythmArray
var newRhythmArray = [];
var newChordArray = [];
var rhythmAndChordArray = []
var oneChord = [];
var chordPlayedCount = 0;
var chordArrayIndex = 0;
var i, j;
// loop thru the rhythmArray
for(i=0; i<rhythmArray.length; i++) {
// push each element into newRhythmArray except the '|' elements
if(rhythmArray[i] != '|') {
newRhythmArray.push(rhythmArray[i]);
// count the number of non-rests in each section
if( !rhythmArray[i].includes('r') ) {
chordPlayedCount = chordPlayedCount + 1;
}
} else { // found '|' element
oneChord = makeOneChordArray(chordArray[chordArrayIndex][2], chordArray[chordArrayIndex][1])
// push to the newChordArray the number of oneChord as needed for that section
for(j=0; j<chordPlayedCount; j++) {
newChordArray.push(oneChord);
}
chordPlayedCount = 0;
chordArrayIndex = chordArrayIndex + 1;
oneChord = [];
}
}
if(chordPlayedCount > 0) {
oneChord = makeOneChordArray(chordArray[chordArrayIndex][2], chordArray[chordArrayIndex][1])
// push to the newChordArray the number of oneChord as needed for that section
for(j=0; j<chordPlayedCount; j++) {
newChordArray.push(oneChord);
}
}
// flatten arrays
// console.log('newRhythmArray='+newRhythmArray);
// console.log('newChordArray='+newChordArray);
rhythmAndChordArray.push(newRhythmArray);
rhythmAndChordArray.push(newChordArray);
return rhythmAndChordArray;
}
/*-------------------------------------------
function makeChordFormula(chordString) {
var chordFormula = makeScaleFormula(chordString);
console.log('chordString='+chordString+' chordFormula='+chordFormula);
return chordFormula;
}
//-----------------------------------------*/
/**
* this function makes a chord array of MIDI numbers
* @private
* @param {string} chordFormulaArray
* @param {string} startingMidiNoteOrPitchOctave
* @returns {number} array chord array of MIDI numbers
*/
function makeMIDIChordArray(chordFormulaArray, startingMidiNoteOrPitchOctave) {
var chordProgMIDI = [];
var aChordMIDI = [];
if( !Number.isInteger(startingMidiNoteOrPitchOctave) ) {
var startingMidiNote = noteNameToMIDI(startingMidiNoteOrPitchOctave)
} else {
var startingMidiNote = startingMidiNoteOrPitchOctave;
}
chordFormulaArray.forEach( function(chord) {
aChordMIDI = makeMIDIArray(chord, startingMidiNote)
chordProgMIDI.push( aChordMIDI );
});
return chordProgMIDI;
}
/**
* this function makes a scale array of MIDI numbers
* @private
* @param {string} scaleString
* @param {string} startingMidiNoteOrPitchOctave
* @returns {number} array of MIDI numbers
*/
function makeMIDIArray(scaleString, startingMidiNoteOrPitchOctave) {
var midiArray = [];
var formula = makeScaleFormula(scaleString);
if( !Number.isInteger(startingMidiNoteOrPitchOctave) ) {
var startingMidiNote = noteNameToMIDI(startingMidiNoteOrPitchOctave)
} else {
var startingMidiNote = startingMidiNoteOrPitchOctave;
}
for(let i=0; i < formula.length; i++) {
midiArray[i] = formula[i] + startingMidiNote;
}
return midiArray;
}
/**
* this function makes an array frequencies from MIDI number inputs
* @private
* dependent on Tone.js library
* @param {number} array midiArray
* @returns {number} array of frequency numbers
*/
function makeFrequencyArray(midiArray) {
var freqArray = [];
for(let i=0; i < midiArray.length; i++) {
freqArray[i] = Tone.Frequency(midiArray[i], 'midi').toFrequency();
}
return freqArray;
}
/**
* this function makes an array noteName from MIDI number inputs
* @private
* dependent on Tone.js library
* NOTE: may not return desired enharmonic
* @param [{number}] midiArray
* @returns {string} array of noteNames
*/
function makePitchOctaveArray(midiArray) {
var noteArray = [];
for(let i=0; i < midiArray.length; i++) {
noteArray[i] = Tone.Frequency(midiArray[i], 'midi').toNote();
}
return noteArray;
}
/**
* this function makes a Frequency array from a scaleFormula and root
* @public
* @param {string} scaleDegreeFormula
* @param {string} root
* @returns {number} array of frequencies
*/
function makeFreqArray(scaleDegreeFormula, root) {
var i;
var midiArray = makeMIDIArray(scaleDegreeFormula, root);
// console.log('midiArray='+midiArray)
var frequencyArray = makeFrequencyArray(midiArray);
return frequencyArray;
}
/**
* this function makes a noteName array from a scaleFormula and root
* @public
* @param {string} scaleDegreeFormula
* @param {string} root
* @returns {object} array of noteNames
* @example
* var harm_minor = '1 2 b3 4 5 b6 7 8';
* var my_root = "G4";
* var g_minor = scaleDegree.makeNoteArray(harm_minor, my_root);
* // g_minor = ["G4","A4","Bb4","C5","D5","Eb5","F#5","G5"]
*/
function makeNoteArray(scaleDegreeFormula, root) {
var i, startIndex;
var MIDIArray = makeMIDIArray(scaleDegreeFormula, root);
// console.log('scaleDegreeFormula='+scaleDegreeFormula+' MIDIArray='+MIDIArray);
var myAlphaNames = [];
var noteArray = [];
var alphaNames = ["A","B","C","D","E","F","G"];
var MIDIRoot = Tone.Frequency(root).toMidi();;
// var scaleDegrees = scaleDegreeFormula.split(' ');
var scaleDegrees = scaleDegreeFormula.split(/\s+/g); // split at white space
myAlphaNames = makeAlphaListFromFormula(scaleDegrees, root);
// console.log('myAlphaNames='+myAlphaNames);
// translate the MIDIArray into appropriate
// LetterName/OctaveNumber format in relation to root and scaleDegreeFormula
// choose appropriate letter name for each MIDIArray element
// using myAlphaNames array.
for(i=0; i<MIDIArray.length; i++) {
noteArray.push(getLetterOctave(MIDIArray[i], myAlphaNames[i]));
}
return noteArray;
}
/**
* this function makes a noteName chord array from a scaleDegreeChordArray and root
* @public
* @param {string} scaleDegreeChordArray
* @param {string} root
* @returns {object} array of noteName
* @example
* var my_triad = '1 b3 5';
* var my_root = "C#4";
* var my_chord = scaleDegree.makeChordArray(my_triad, my_root);
* // my_chord = ["C#4","E4","G#4"]
*/
function makeChordArray(scaleDegreeChordArray, root) {