-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhConnect.js
938 lines (842 loc) · 27.6 KB
/
hConnect.js
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
// MODULATIONS
// CYCLE OF FIFTHS
/*const cycleF = ["C", "G", "D", "A", "E", "B", "Gb", "Db", "Ab", "Eb", "Bb", "F",
"C", "G", "D", "A", "E", "B", "Gb", "Db", "Ab", "Eb", "Bb", "F",
"A-", "E-", "B-", "Gb-", "Db-", "Ab-", "Eb-", "Bb-", "F-", "C-", "G-", "D-",
"A-", "E-", "B-", "Gb-", "Db-", "Ab-", "Eb-", "Bb-", "F-", "C-", "G-", "D-"
]*/
const cycleF = [
'C',
'G',
'D',
'A',
'E',
'B',
'Gb',
'Db',
'Ab',
'Eb',
'Bb',
'F',
];
const keys = [
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
];
const chroma = [
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
];
const modes = ['M', 'm', 'm', 'M', 'M', 'm', 'dim'];
// There are 4 comon chords for two keys next to each other, if I jump one key, only two are in common
// (1) first, third, 4th, fifth, (2) third, fifth
// Pre dom IV & ii (ii is the relative minor in the next key!)
// I start the modulation in the two chord, because it becomes the relative minor in the new key
// Pre dom are the common chords for modulation
// Scheletro diatonic (common chord) modulation
exports.diaMod = function (song1, song2) {
currentFifthIndex = cycleF.indexOf(song1.key);
currentKeyIndex = keys.indexOf(song1.key);
n1 = cycleF.indexOf(song1.key);
n2 = cycleF.indexOf(song2.key);
n = cofDist(song1.key, song2.key);
let chords = [];
if (n >= 3 || song1.key.includes('-') || song2.key.includes('-')) {
return { chords: chords, maxJump: 100 };
}
let nextFifthIndex;
let firstChordModulation;
let secondChordModulation;
let thirdChordModulation;
nextFifthIndex = currentFifthIndex + n;
if (n == 1 || n == 2 || n == -11 || n == 12) {
//nextFifthIndex = currentFifthIndex + n;
firstChordModulation = keys[currentKeyIndex + 4]; //iii-
secondChordModulation = keys[currentKeyIndex + 9]; //vi- which is ii- in next key
currentKeyIndex = keys.indexOf(song2.key);
thirdChordModulation = keys[currentKeyIndex + 7]; //V now you can tell the key is changed
chords = [
firstChordModulation + '-',
secondChordModulation + '-',
thirdChordModulation + '^',
];
} else if (n == -1 || n == -2 || n == 11 || n == -12) {
if (currentKeyIndex + n < 0) {
currentKeyIndex = currentKeyIndex + 12;
}
//nextFifthIndex = currentFifthIndex + n;
firstChordModulation = keys[currentKeyIndex + 7];
currentKeyIndex = keys.indexOf(song2.key);
secondChordModulation = keys[currentKeyIndex + 9];
thirdChordModulation = keys[currentKeyIndex + 4];
chords = [
firstChordModulation + '^',
secondChordModulation + '-',
thirdChordModulation + '-',
];
} else if (n == 0) {
firstChordModulation = keys[currentKeyIndex + 2];
secondChordModulation = keys[currentKeyIndex + 7];
thirdChordModulation = keys[currentKeyIndex];
chords = [
firstChordModulation + '-',
secondChordModulation + '^',
thirdChordModulation + '^',
];
} else if (n == 24) {
// may an I IV V vi could be used before that
currentKeyIndex = keys.indexOf(song2.key);
firstChordModulation = keys[currentKeyIndex + 2];
secondChordModulation = keys[currentKeyIndex + 7];
thirdChordModulation = keys[currentKeyIndex];
chords = [
firstChordModulation + '-',
secondChordModulation + '^',
thirdChordModulation + '^',
];
}
return { chords: chords, maxJump: n, name: 'Diatonic Modulation' };
};
function cofDist(key1, key2) {
let index1;
let index2;
if (key1.includes('-')) {
let keyTemp1 = key1.slice(0, -1);
index1 = cycleF.indexOf(keyTemp1.toString());
index1 -= 3;
if (index1 < 0) index1 += 12;
} else {
index1 = cycleF.indexOf(key1);
}
if (key2.includes('-')) {
let keyTemp2 = key2.slice(0, -1);
index2 = cycleF.indexOf(keyTemp2);
index2 -= 3;
if (index2 < 0) index2 += 12;
} else {
index2 = cycleF.indexOf(key2);
}
let ris = Math.abs(index1 - index2);
if (ris > 6) ris = 12 - ris;
return ris;
}
// Chormatic Pivot CHORD
// From C to E minor
// Chromatic pivot chord is a chord not contained in the first key
// I vii vii (next chord) i
// I, II, iii, IV, V, vi, vii dim
// VI, vii, i, ii#, III, iv, v
exports.pivotChord = function (song1, song2) {
let chords = [];
n1 = cycleF.indexOf(song1.key);
n2 = cycleF.indexOf(song2.key);
n = cofDist(song1.key, song2.key);
if (song1.key.includes('-') || !song2.key.includes('-')) {
return { chords: chords, maxJump: 100 };
}
currentKeyIndex = keys.indexOf(song1.key);
nextKeyIndex = keys.indexOf(song2.key);
firstChordModulation = keys[currentKeyIndex + 11];
currentKeyIndex = nextKeyIndex;
secondChordModulation = keys[currentKeyIndex + 11];
secondChordModulation = secondChordModulation.replace('-', '');
chords = [firstChordModulation + 'o', secondChordModulation + 'o7'];
return { chords: chords, maxJump: 1.5, name: 'Chromatic Pivot Chord' };
};
// Enharmonic dominant
// if the next key dominant equals the augmented sixth of the current key
// I just need this chord as modulation
// it should work only in the case that I'm changin of half-step (is that what sarti asked?)
// we re-spell an augmented sixth as a dominanth 7th and use that as a five chord to cadence to one chord.
//typically augmented sith resolve to 1-6-4 chord
exports.enharmonicDominant = function (song1, song2) {
n = cofDist(song1.key, song2.key);
let chords = [];
if (n != 5 || song1.key.includes('-') || song2.key.includes('-')) {
return { chords: chords, maxJump: 100 };
}
currentKeyIndex = keys.indexOf(song1.key);
nextKeyIndex = keys.indexOf(song2.key);
firstChordModulation = keys[currentKeyIndex + 9];
//secondChordModulation = keys[currentKeyIndex + 11]
//secondChordModulation = keys[currentKeyIndex + 8]
currentKeyIndex = nextKeyIndex;
secondChordModulation = keys[currentKeyIndex + 7]; // +7?
chords = [firstChordModulation + '+', secondChordModulation + '7'];
return { chords: chords, maxJump: n, name: 'Enharmonic Dominant' };
};
//console.log(deceptiveCadence(s1))
// dominant chord goes to the sith chord
exports.deceptiveCadenceEx = function (song1, song2, isMinor = false) {
n = cofDist(song1.key, song2.key);
if (
n != 0 ||
(song1.key.includes('-') && song2.key.includes('-')) ||
(!song1.key.includes('-') && !song2.key.includes('-'))
) {
return { chords: [], maxJump: 100 };
}
let currentKey = song1.key.replace('-', '');
let keyIndex = chroma.indexOf(currentKey);
let chords = [];
let chord1 = keys[keyIndex] + '^';
let chord2 = mod(chroma, keyIndex, 5) + '^';
let chord3 = mod(chroma, keyIndex, 7) + '7';
let chord4 = mod(chroma, keyIndex, 9);
let nextKeyIndex = chroma.indexOf(chord4);
chord4 = mod(chroma, keyIndex, 9) + '-'; //or -7?
let chord5 = mod(chroma, nextKeyIndex, 3) + '-7b5';
let chord6 = mod(chroma, nextKeyIndex, 7) + '7b9';
let chord7 = mod(chroma, nextKeyIndex, 0) + '-'; //or -7?
if (isMinor) {
chords = [chord4, chord5, chord6, chord7, chord1, chord2, chord3];
} else {
chords = [chord1, chord2, chord3, chord4, chord5, chord6, chord7];
}
return { chords: chords, maxJump: n, name: 'Deceptive Cadence' };
};
function deceptiveCadence(song1, song2, isMinor = false) {
let currentKey = song1.key.replace('-', '');
let keyIndex = chroma.indexOf(currentKey);
let chord1 = keys[keyIndex] + '^';
let chord2 = mod(chroma, keyIndex, 5) + '^';
let chord3 = mod(chroma, keyIndex, 7) + '7';
let chord4 = mod(chroma, keyIndex, 9);
let nextKeyIndex = chroma.indexOf(chord4);
chord4 = mod(chroma, keyIndex, 9) + '-'; //or -7?
let chord5 = mod(chroma, nextKeyIndex, 3) + '-7b5';
let chord6 = mod(chroma, nextKeyIndex, 7) + '7b9';
let chord7 = mod(chroma, nextKeyIndex, 0) + '-'; //or -7?
if (isMinor) {
chords = [chord4, chord5, chord6, chord7, chord1, chord2, chord3];
} else {
chords = [chord1, chord2, chord3, chord4, chord5, chord6, chord7];
}
return chords;
}
// Deceptive Cadence
// Dominant chord in a major key goes to the sixth
// If from a major I wanto to go to relative minor I play IV, V, vi (that is our next key i)
// vii, iii (that is ii, v) and then (now I'm in the new key) i of relative minor
/*exports.deceptiveCadence = function (song) {
currentkey = song.key
let isMinor = false
if (currentKey.includes('-')) {
minorKey = currentKey
currentKey = currentKey.replace('-', '')
console.log(currentKey)
isMinor = true
}
keyIndex = keys.indexOf(currentKey)
let chords = []
chord1 = keys[keyIndex]
chord2 = keys[keyIndex + 5]
chord3 = keys[keyIndex + 7]
chord4 = keys[keyIndex + 9]
nextKeyIndex = keys.indexOf(chord4)
chord5 = keys[nextKeyIndex + 3]
chord6 = keys[nextKeyIndex + 7]
chord7 = keys[nextKeyIndex]
if (isMinor) {
chords = [chord4, chord5, chord6, chord7, chord1, chord2, chord3]
} else {
chords = [chord1, chord2, chord3, chord4, chord5, chord6, chord7]
}
return chords
}*/
// Enhamronic dim7
// Major I, V, vii, I, then I go to relative minor vii dim, i (nexy key relative minor)
exports.enharmonicDimSeven = function (song1, song2) {
n = cofDist(song1.key, song2.key);
let chords = [];
if (n != 0 || !song2.key.includes('-')) {
return { chords: [], maxJump: 100 };
}
currentKeyIndex = keys.indexOf(song1.key);
firstKeyIndex = currentKeyIndex;
nextKeyIndex = keys.indexOf(song2.key);
let firstChordModulation = keys[currentKeyIndex];
let secondChordModulation = keys[currentKeyIndex + 7] + '7';
let thirdChordModulation = keys[currentKeyIndex + 11] + 'o7';
// now I'm in relative minor
currentKeyIndex = nextKeyIndex;
let chord_4 = keys[currentKeyIndex + 11] + 'o7/' + keys[firstKeyIndex + 11];
chords = [
firstChordModulation,
secondChordModulation,
thirdChordModulation,
firstChordModulation,
chord_4,
];
return { chords: chords, maxJump: n, name: 'Enharmonic Dim7' };
};
// dim7 to Dom7
// if I take any dim7, I have just to low the root down of halfstep
// from Cmin to Eb: i, iv, i, vii (dim), V, I.
// In Bdim chord if I go down halfstep on the root (B) I get Bb chord (a V) of the next key
// metterei un bel if nextKeyIndex - currentKeyIndex = 3
exports.dimSevenDomSeven = function (song1, song2) {
n = cofDist(song1.key, song2.key);
let chords = [];
if (n != 0 || !song1.key.includes('-') || song2.key.includes('-')) {
return { chords: chords, maxJump: 100 };
}
currentKeyIndex = keys.indexOf(song1.key);
nextKeyIndex = keys.indexOf(song2.key);
let chord_1 = keys[currentKeyIndex];
let chord_2 = keys[currentKeyIndex + 5];
let chord_3 = keys[currentKeyIndex + 11];
chord_3 = chord_3.replace('-', 'o7');
currentKeyIndex = nextKeyIndex;
let chord_4 = keys[currentKeyIndex + 7] + '7';
chords = [chord_1, chord_2, chord_3, chord_4];
return { chords: chords, maxJump: n, name: 'Dim7 Dom7' };
};
// Chromatic Mediant
// Mediant = iii, Submediant = vi (in Major)
// Mediant = III, Submediant = VI (in Minor)
// I think there is no use for harmonic connect
// Maybe I can use it to change from a I to iii or vi
// Common/Pivot NOTE
// Includes chromatic Mediant! The important is to have a common note between the two keys
// Emin = EGB, EbGC (Cmin) <- mediant relationship
// FAC#E (F), DbGbAbC (Db)
// Direct/Linear Modulation
// No common chords between the two. C, B
//if (Chord.getNotesChord(song1.key).filter(value => Chord.getNotesChord(song2.key).includes(value)) == [])
function directModulation(song1, song2) {
n1 = cycleF.indexOf(song1.key);
n2 = cycleF.indexOf(song2.key);
n = cofDist(song1.key, song2.key);
let chords = [];
if (song1.key.includes('-') || song2.key.includes('-')) {
return { chords: [], maxJump: 100 };
}
currentKey = song1.key;
keyIndex = keys.indexOf(currentKey);
nextKey = song2.key;
nextKeyIndex = keys.indexOf(nextKey);
chord1 = keys[keyIndex + 7];
chord2 = keys[nextKeyIndex + 7];
chords = [chord1, chord2];
return { chords: chords, maxJump: n, name: 'Direct Modulation' };
}
// IV = vii and vicevaersa, two common notes (if Major)
// II = VI and vicevarsa, two common notes (if minor)
// I first need to do a triton substitution
// potrei provare dall'ultimo accord della prima canzone, il suo tritono e poi la quinta della prossima chiave
// e cosi tornare nella prima della prossima chiave. Senno: ii-V, poi ii-V del tritono e poi ii-V-I della nuova chiave
// preceduto da un if (song1.key == Mayor && (song2.key == song1.key + 5 || song2.key == song1.key + 11)
// || song1.key = minor && (song2.key == song1.key + 2 || song2.key == song1.key + 8))
function tritonModulation(song1, song2) {
n = cofDist(song1.key, song2.key);
let chords = [];
if (
(!song1.key.includes('-') && (n == 5 || n == 11)) ||
(song1.key.includes('-') && (n == 2 || n == 8))
) {
return { chords: [], maxJump: 100 };
}
currentKey = song1.key;
nextKey = song2.key;
keyIndex = keys.indexOf(currentKey);
tritonKey = keys.indexOf(currentKey) + 6;
nextKeyIndex = keys.indexOf(nextKey);
nextKey = song2.key;
chord1 = keys[currentKey];
chord2 = keys[currentKey + 2];
chord3 = keys[currentKey + 7];
chord4 = keys[tritonKey + 2];
chord5 = keys[tritonKey + 7];
chord6 = keys[nextKey + 2];
chord7 = keys[nextKey + 7];
chord8 = keys[nextKey];
chords = [chord1, chord2, chord3, chord4, chord5, chord6, chord7, chord8];
return { chords: chords, maxJump: n, name: 'Triton Modulation' };
}
// la noChordModulation means that there are no chord in commono between the two keys, which means that
// there are at least three stepes in the cycle of fiths.
// So also here I would put something like if abs(song_1.key - song_2.key) ≥ 3
exports.noChordModulation = function (song1, song2) {
dist = cofDist(song1.key, song2.key);
let chords;
if (dist < 3) {
return { chords: [], maxJump: 100 };
}
n = Math.ceil(Math.random() * 2);
if (n == 1) {
chords = directModulation(song1, song2);
} else if (n == 2) {
chords = tritonModulation(song1, song2);
}
return chords;
};
// Chain modulation I go around the circleof fifths clock or counterclockwise and I do V, V7 in each step
// till I get to play the next song key tonal
exports.chainModulation = function (song1, song2) {
let dist = cofDist(song1.key, song2.key);
let firstIsMinor = false;
let secondIsMinor = false;
let firstRelMaj;
let secondRelMaj;
let root1;
let root2;
if (song1.key.includes('-')) {
root1 = song1.key;
root1 = root1.replace('-', '');
root1 = chromaSharpNormalization(root1);
firstIsMinor = true;
firstRelMaj = mod(chromaSharp, chromaSharp.indexOf(root1), 3);
}
if (song2.key.includes('-')) {
root2 = song2.key;
root2 = root2.replace('-', '');
root2 = chromaSharpNormalization(root2);
secondIsMinor = true;
secondRelMaj = mod(chromaSharp, chromaSharp.indexOf(root2), 3);
}
let chords = [];
let firstKey = song1.key;
let firstFifthIndex = cycleF.indexOf(firstKey.replace('-', ''));
let secondKey = song2.key;
let secondFifthIndex = cycleF.indexOf(secondKey.replace('-', ''));
let n = secondFifthIndex - firstFifthIndex;
if (
(firstIsMinor == true && secondIsMinor == true) ||
(firstIsMinor == false && secondIsMinor == false)
) {
if (n > 0) {
if (n <= 6) {
for (let i = 0; i <= n; i++) {
console.log(chords, typeof chords);
chords.push(mod(cycleF, firstFifthIndex, i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7');
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7');
}
}
}
} else {
n = -n;
if (n <= 6) {
for (let i = 0; i <= n; i++) {
console.log(chords, typeof chords);
chords.push(mod(cycleF, firstFifthIndex, -i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7');
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7');
}
}
}
}
if (firstIsMinor == true && secondIsMinor == true) {
for (let i = 0; i < chords.length; i++) {
chords[i] = chords[i].replace('^', '-');
chords[i] = chords[i].replace('7', '-7');
}
}
} else {
if (firstIsMinor == true) {
let st1 = {};
st1.key = firstRelMaj;
chords = deceptiveCadence(song1, song2, true);
firstKey = firstRelMaj;
firstFifthIndex = cycleF.indexOf(firstKey);
n = secondFifthIndex - firstFifthIndex;
if (n > 0) {
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7');
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7');
}
}
}
} else {
n = -n;
if (n <= 6) {
for (let i = 0; i <= n; i++) {
console.log(chords, typeof chords);
chords.push(mod(cycleF, firstFifthIndex, -i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7');
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7');
}
}
}
}
} else if (secondIsMinor == true) {
secondKey = secondRelMaj;
secondFifthIndex = cycleF.indexOf(secondKey); //perche' qui c'era firstKey??
n = secondFifthIndex - firstFifthIndex;
if (n > 0) {
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7');
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7');
}
}
}
} else {
n = -n;
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7');
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^');
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7');
}
}
}
}
console.log(chords);
let st2 = {};
st2.key = secondRelMaj;
console.log(secondRelMaj);
let temp = deceptiveCadence(song1, song2);
console.log(temp);
for (let i = 0; i < temp.length; i++) {
chords.push(temp[i]);
}
}
}
let final = chords.slice();
return { chords: final, maxJump: dist, name: 'Chain Modulation' };
};
//TEST
let chromaSharp = [
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
];
function chromaSharpNormalization(note) {
switch (note) {
case 'C#':
return 'Db';
case 'D#':
return 'Eb';
case 'F#':
return 'Gb';
case 'G#':
return 'Ab';
case 'A#':
return 'Bb';
}
return note;
}
function mod(arr, num, index) {
let sum = index + num;
if (sum >= arr.length) return arr[sum % arr.length];
else if (sum < 0) {
sum = sum + arr.length;
return arr[sum];
}
return arr[sum];
}
//TEST
let s1 = { key: 'C-' };
let s2 = { key: 'C-' };
//console.log(chainModulation(s1,s2))
/* function chainModulation(song1, song2) {
let firstIsMinor = false
let secondIsMinor = false
let firstRelMaj
let secondRelMaj
let root1
let root2
if (song1.key.includes('-')) {
root1 = song1.key
root1 = root1.replace('-', '')
root1 = chromaSharpNormalization(root1)
firstIsMinor = true
firstRelMaj = mod(chromaSharp, chromaSharp.indexOf(root1), 3)
}
if (song2.key.includes('-')) {
root2 = song2.key
root2 = root2.replace('-', '')
root2 = chromaSharpNormalization(root2)
secondIsMinor = true
secondRelMaj = mod(chromaSharp, chromaSharp.indexOf(root2), 3)
}
let chords = []
let firstKey = song1.key
let firstFifthIndex = cycleF.indexOf(firstKey.replace("-", ""))
let secondKey = song2.key
let secondFifthIndex = cycleF.indexOf(secondKey.replace("-", ""))
let n = secondFifthIndex - firstFifthIndex
if ((firstIsMinor == true && secondIsMinor == true) || (firstIsMinor == false && secondIsMinor == false)) {
if (n > 0) {
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7')
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7')
}
}
}
} else {
n = -n
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7')
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7')
}
}
}
}
if (firstIsMinor == true && secondIsMinor == true) {
for (let i = 0; i < chords.length; i++) {
chords[i] = chords[i].replace("^", "-")
chords[i] = chords[i].replace("7", "-7")
}
}
} else {
if (firstIsMinor == true) {
let st1 = {}
st1.key = firstRelMaj
chords = deceptiveCadence(st1, true)
firstKey = firstRelMaj
firstFifthIndex = cycleF.indexOf(firstKey)
n = secondFifthIndex - firstFifthIndex
if (n > 0) {
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7')
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7')
}
}
}
} else {
n = -n
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7')
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7')
}
}
}
}
} else if (secondIsMinor == true) {
secondKey = secondRelMaj
secondFifthIndex = cycleF.indexOf(secondKey) //perche' qui c'era firstKey??
n = secondFifthIndex - firstFifthIndex
if (n > 0) {
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7')
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7')
}
}
}
} else {
n = -n
if (n <= 6) {
for (let i = 0; i <= n; i++) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, -i) + '7')
}
}
} else {
for (let i = 0; i <= cycleF.length - n; i++) {
chords.push(mod(cycleF, firstFifthIndex, i) + '^')
if (i != n) {
chords.push(mod(cycleF, firstFifthIndex, i) + '7')
}
}
}
}
console.log(chords)
console.log(secondRelMaj)
let st2 = {}
st2.key = secondRelMaj
let temp = deceptiveCadence(st2)
for (let i = 0; i < temp.length; i++) {
chords.push(temp[i])
}
}
}
let final = chunkArray(chords, 2)
return final
}
*/
function chunkArray(myArray, chunk_size) {
var index = 0;
var arrayLength = myArray.length;
var tempArray = [];
for (index = 0; index < arrayLength; index += chunk_size) {
myChunk = myArray.slice(index, index + chunk_size);
// Do something if you want with the group
tempArray.push(myChunk);
}
return tempArray;
}
exports.parallelMod = function (song1, song2) {
n1 = cycleF.indexOf(song1.key);
n2 = cycleF.indexOf(song2.key);
n = cofDist(song1.key, song2.key);
console.log(n);
let chords = [];
if (
n != 3 ||
(!song1.key.includes('-') && !song2.key.includes('-')) ||
(song1.key.includes('-') && song2.key.includes('-'))
) {
return { chords: chords, maxJump: 100 };
}
key = song1.key;
keyIndex = keys.indexOf(key);
chord1 = key;
chord2 = chord1.slice();
if (chord1.includes('-')) chord2 = chord2.replace('-', '^');
else {
chord2 = chord2 + '-';
chord1 = chord1 + '^';
}
chords = [chord1, chord2];
return { chords: chords, maxJump: n, name: 'Parallel Modulation' };
};
// || (or)