-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWalkingBass.qml
1270 lines (1034 loc) · 38.1 KB
/
WalkingBass.qml
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
import MuseScore 3.0
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.2
import QtQuick.Dialogs 1.2
import Qt.labs.settings 1.0
//=============================================================================
// MuseScore 3.3+
// MuseScore 4.x
//
// WalkingBass v1.2
// A plugin to compose a reasonable walking bass line, based on Chords
//
// (C) 2023 Phil Kan
// PitDad Music. All Rights Reserved.
// With thanks to Jojo-Schmitz for initial MuseScore 4 implementation
// See: https://github.com/Jojo-Schmitz/WalkingBass/tree/main
//
// Restrictions / Assumptions / Checks
// - 4/4 time
// - Notes are in bass clef, from low E (28) with a 2.5 octave range
// - Quarter notes walking only
// - Requires a score to be open, and some bars with chords to be selected
//
// - If a chord lasts for just one beat, then the root will always be used
// - A pattern consists of 4 or 2 notes, so used for a complete bar, or two beats only
// - Patterns consist of scale notes, plus a few "approach" notes.
// - - a / b indicate to use a semitone approach note above or below the next note
// - - v indicates to a use the fifth of the target chord (so, should be a b5 for a diminished)
// - Ensure that last note of a pattern is not the same note of the next pattern, as this
// will really obscure the change of chords
// - Optionally write the pattern being used below the first note
// - Optionally turn the notes into slashes. Will still play as expected, but appear as a '/'
// - Optionally also include patterns that don't start on the root (e.g. 3-2-1-a)
//
// - todo: Add patterns for 2 bars (8 beats) of the one chord...
//
// Change History
// v1.0 - initial release
// v1.1 - Fixed height of panel. Added octave jumps for x-x in a pattern
// v1.2 - Added Support for MuseScore 4.x, as a dialog,
// -
//
//=============================================================================
MuseScore
{
version: "1.2"
menuPath: "Plugins.WalkingBass"
description: "This plug-in generates a walking bass line for given chord changes."
Component.onCompleted : {
if (mscoreMajorVersion >= 4) {
title = qsTr("Walking Bass");
thumbnailName = "WalkingBassIcon.png";
categoryCode = qsTr("PitDad Tools");
}
}
pluginType: mscoreMajorVersion >= 4 ? "dialog" : "dock";
dockArea: "left";
implicitHeight: 480;
implicitWidth: 260;
//=============================================================================
// configuration options. These can be set in the UI
property var lowestPitchText: "E1" // E below C below C below middle C (concert)
property var lowestPitch: 28
property var octaveRange: 2.5 // octave range to use
property var flipPercent: 10 // percentage chance that the next note is
// not the closest in the octave
property bool includePatternText: true // if true, then the current pattern is written beneath the first note
property bool useSlashes: false // if true write notes as stemless slashes. if false, writes as actual notes
property bool useNonRootPatterns: false // if true use patterns that don't start on the root
property int nonRootPercent: 10 // percentage to use non root patterns
property int octavesPercent: 50 // when an interval is repeated in a pattern,
// the percentage chance it will jump an octave
//=============================================================================
// Layout
//
GridLayout
{
id: 'walkingBassMainLayout'
columns: 2
anchors.fill: parent
anchors.margins: 10
Label
{
id: lowestPitchLabel
text: "Lowest Pitch"
}
TextField
{
id: lowestPitchField
placeholderText: lowestPitchText
horizontalAlignment: TextInput.AlignRight
Keys.onReturnPressed: isValidLowestNote()
}
Label
{
id: lowestPitchFieldHelp
Layout.columnSpan:2
font.italic: true
text: "Lowest pitch available, from C0 to B4.\n(Typically E1)"
bottomPadding: 10
}
Label
{
id: octaveRangeLabel
text: "Octave Range"
}
TextField
{
id: octaveRangeField
implicitHeight: 24
placeholderText: octaveRange
horizontalAlignment: TextInput.AlignRight
Keys.onReturnPressed: isValidOctaveRange()
}
Label
{
id: octaveRangeLabelHelp
Layout.columnSpan:2
font.italic: true
text: "Range in octaves. Typically 2, 2.5 or 3"
bottomPadding: 10
}
Label
{
id: flipPercentLabel
text: "Flip Percentage"
}
TextField
{
id: flipPercentField
implicitHeight: 24
placeholderText: flipPercent
horizontalAlignment: TextInput.AlignRight
Keys.onReturnPressed: isValidFlipPercent()
}
Label
{
id: flipPercentLabelHelp
Layout.columnSpan: 2
font.italic: true
text: "Percentage that a 3rd, 4th, 5th, or 6th\nis furthest not closest"
bottomPadding: 10
}
Label
{
id: includePatternTextLabel
text: "Patterns Text"
}
CheckBox
{
id: includePatternTextCheck
checked: includePatternText
}
Label
{
id: includePatternTextHelp
Layout.columnSpan:2
font.italic: true
text: "Include the pattern text below the first note"
bottomPadding: 10
}
Label
{
id: useSlashesLabel
text: "Slashes"
}
CheckBox
{
id: useSlashesCheck
checked: useSlashes
}
Label
{
id: useSlashesHelp
font.italic: true
bottomPadding: 10
text: "Use slashes instead of notes"
Layout.columnSpan:2
}
Label
{
id: useNonRootPatternslabel
text: "Use non-root patterns (%)"
}
TextField
{
id: nonRootPercentField
placeholderText: nonRootPercent
horizontalAlignment: TextInput.AlignRight
Keys.onReturnPressed: isValidNonRootPercent()
}
Label
{
id: octavesPercentLabel
text: "Repeat-note Octaves (%)"
}
TextField
{
id: octavesPercentField
placeholderText: octavesPercent
horizontalAlignment: TextInput.AlignRight
Keys.onReturnPressed: isValidOctavesPercent()
}
Button
{
id: applyButton
Layout.columnSpan: 2
text: qsTranslate("PrefsDialogBase", "Apply")
onClicked: applyBassLine()
}
Label
{
id: errorLabel
visible: false
Layout.columnSpan:2
}
}
//=============================================================================
function isValidLowestNote()
{
if (!(/^[A-G]{1}(b|#)?[0-4]{1}$/.test(lowestPitchField.text)) )
{
inputError.text = "Lowest pitch must be a valid note & octave. e.g. E3"
inputError.open();
}
parseLowestNote();
}
//=============================================================================
function parseLowestNote()
{
var idx = 0;
var adjustPitch = 0;
var root = lowestPitchField.text[idx].toUpperCase();
idx++;
// could be #or b as well..
if ('#b'.includes(lowestPitchField.text[idx]))
{
adjustPitch = lowestPitchField.text[idx] == "b" ? -1 : 1;
idx++;
}
var octave = parseInt(lowestPitchField.text[idx]);
lowestPitch = c0 + (12 * octave) + letterToSemitone[root] + adjustPitch;
}
//=============================================================================
function isValidOctaveRange()
{
if (!(/^(\d)*(\.)?([0-9]{1})?$/.test(octaveRangeField.text)) )
{
displayError("Octave Range must be a number from 0 to 4.");
return false;
}
octaveRange = parseFloat(octaveRangeField.text);
if (octaveRange > 4)
{
displayError("Octave Range must be a number from 0 to 4.");
return false;
}
return true;
}
function isValidFlipPercent()
{
if (! (/^\d+$/.test(flipPercentField.text) ))
{
displayError("Not a valid Non-roots percentage.\nUse a whole number between 0 and 100 ");
return false;
}
flipPercent = parseInt(flipPercentField.text);
if (flipPercent < 0 || flipPercent > 100)
{
displayError("Not a valid Non-roots percentage.\nUse a whole number between 0 and 100 ");
return false;
}
return true;
}
function isValidNonRootPercent()
{
if (! (/^\d+$/.test(nonRootPercentField.text)) )
{
displayError("Not a valid Non-roots percentage.\nUse a whole number between 0 and 100 ");
return false;
}
nonRootPercent = parseInt(nonRootPercentField.text);
if (nonRootPercent < 0 || nonRootPercent > 100)
{
displayError("Not a valid Non-roots percentage.\nUse a whole number between 0 and 100 ");
return false;
}
return true;
}
function isValidOctavesPercent()
{
if (! (/^\d+$/.test(octavesPercentField.text)) )
{
displayError("Not a valid flip percentage.\nUse a whole number between 0 and 100 ");
return false;
}
octavesPercent = parseInt(octavesPercentField.text);
if (octavesPercent < 0 || octavesPercent > 100)
{
displayError("Not a valid flip percentage.\nUse a whole number between 0 and 100 ");
return false;
}
return true;
}
//=============================================================================
function displayError(message)
{
inputError.text = message;
inputError.open();
}
MessageDialog
{
id: inputError
visible: false
title: "Numeric input error"
text: "Lowest Note and Range must be numeric values"
onAccepted: {
close();
}
}
//=============================================================================
MessageDialog
{
id: versionError
visible: false
title: qsTr("Unsupported MuseScore Version")
text: qsTr("This plugin needs MuseScore 3.3 or later")
onAccepted: {
Qt.quit()
}
}
//=============================================================================
// internal globals
property int highestPitch: lowestPitch + (12 * octaveRange);
// based on the major scale
property var intervalToSemitone: {'1':0, '2':2, '3':4, '4':5, '5':7, '6':9, '7':11};
// Now based on C0
property var letterToSemitone: {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11};
property var c0: 12 // midi for C0
// [b]elow, [a]bove [v]fifth
property var approachSemitones: { "b": -1, "a": 1, "v": 7, };
property var patterns2:
[
// doubling up on some root & approach note versions so they have a greater chance of getting used
"1-1", "1-3", "1-5", "1-6", "1-7",
"1-3", "1-5", "1-7",
"1-3", "1-5", "1-7",
"1-a", "1-b", "1-v",
"1-a", "1-b", "1-v",
"1-a", "1-b", "1-v",
]
property var patterns2NonRoot:
[
"3-1", "3-5", "3-a", "3-b", "3-v",
"5-1", "5-3", "5-a", "5-b", "5-v",
]
property var patterns:
[
"1-a-3-1", "1-a-3-5", "1-a-3-a", "1-a-3-b", "1-a-3-v",
"1-b-2-1", "1-b-2-3", "1-b-2-a", "1-b-2-b", "1-b-2-v",
"1-1-3-5", "1-1-3-a", "1-1-3-b", "1-1-3-v",
"1-1-5-1", "1-1-5-3", "1-1-5-5", "1-1-5-a", "1-1-5-b", "1-1-5-v",
"1-2-3-1", "1-2-3-5", "1-2-3-a", "1-2-3-b", "1-2-3-v",
"1-3-1-5", "1-3-1-7", "1-3-1-a", "1-3-1-b", "1-3-1-v",
"1-3-5-1", "1-3-5-7", "1-3-5-a", "1-3-5-b", "1-3-5-v",
"1-3-6-1", "1-3-6-7", "1-3-6-a", "1-3-6-b", "1-3-6-v",
"1-5-1-5", "1-5-1-3", "1-5-1-a", "1-5-1-b", "1-5-1-v",
"1-5-3-1", "1-5-3-5", "1-5-3-a", "1-5-3-b", "1-5-3-v",
"1-5-7-1", "1-5-7-5", "1-5-7-a", "1-5-7-b", "1-5-7-v",
"1-6-5-3", "1-6-5-6", "1-6-5-a", "1-6-5-b", "1-6-5-v",
"1-6-7-1", "1-6-7-3", "1-6-7-6", "1-6-7-5", "1-6-7-1", "1-6-7-b", "1-6-7-v",
"1-7-6-7", "1-7-6-5", "1-7-6-3", "1-7-6-1", "1-7-6-a", "1-7-6-b", "1-7-6-v",
"1-7-5-7", "1-7-5-6", "1-7-5-3", "1-7-5-a", "1-7-5-b", "1-7-5-v",
]
property var patternsNonRoot:
[
"3-2-1-1", "3-2-1-5", "3-2-1-7", "3-2-1-a", "3-2-1-b", "3-2-1-v",
"3-5-1-1", "3-5-1-5", "3-5-1-7", "3-5-1-a", "3-5-1-b", "3-5-1-v",
"3-7-1-1", "3-7-1-5", "3-7-1-7", "3-7-1-a", "3-7-1-b", "3-7-1-v",
"3-a-1-1", "3-a-1-5", "3-a-1-7", "3-a-1-a", "3-a-1-b", "3-a-1-v",
"3-b-1-1", "3-b-1-5", "3-b-1-7", "3-b-1-a", "3-b-1-b", "3-b-1-v",
"5-3-1-1", "5-3-1-5", "5-3-1-7", "5-3-1-a", "5-3-1-b", "5-3-1-v",
]
property int quarterNoteDuration: division;
property int previousPitch: -1;
property var approachPattern: "";
property var approachTick: 0;
property var notes: [];
//=============================================================================
// some faux enums as this seems to be best way to achieve this in qml
QtObject
{
id: triadType
property int major: 0
property int minor: 1
property int diminshed: 2
property int augmented: 3
property int dominant: 4
property int halfDiminished: 5
}
//=============================================================================
function applyBassLine()
{
errorLabel.text = "";
errorLabel.visible = false;
var cursor = getCursor();
if (!cursor.segment ) // no selection
{
console.log("Error: Nothing is selected.")
errorLabel.text = "Error: Nothing is selected.\nPlease select one staff of bars with chords";
errorLabel.visible = true;
return;
}
if (curScore.selection.endStaff - curScore.selection.startStaff > 1)
{
console.log("More than one staff selected")
errorLabel.text = "Error: More than one staff is selected\nPlease select only one staff, with chords";
errorLabel.visible = true;
return;
}
if (!isValidOctaveRange()) return;
if (!isValidOctavesPercent()) return;
if (!isValidNonRootPercent()) return;
previousPitch = -1;
approachPattern = "";
approachTick = 0;
parseLowestNote();
octaveRange = parseFloat(octaveRangeField.text);
highestPitch = lowestPitch + (12 * octaveRange);
flipPercent = parseInt(flipPercentField.text);
includePatternText = includePatternTextCheck.checked;
useSlashes = useSlashesCheck.checked;
nonRootPercent = parseInt(nonRootPercentField.text);
// a random starting point
previousPitch = lowestPitch + Math.floor(Math.random() * (highestPitch - lowestPitch));
curScore.startCmd()
addBassLine(cursor);
curScore.endCmd()
console.log("----------------------------------------");
}
//=============================================================================
// Helper function for determining extensions
// returns true if the provided character is a number (1-9)
// 0 is excluded, as that is used to indicate half diminished, and not used in extensions
function isNumber(ch)
{
// and is NOT used any extensions..
return "123456789".includes(ch);
}
//=============================================================================
// parse a provided chord into its component parts
// root: the root note of the chord
// triad: the chord quality. One of the triadType values
// extensions: a list of the extensions to the chord (e.g. #5, b9, etc)
// bass: if its a slash chord, the bass note (after the slash)
function parseChord(chord)
{
// letter
var idx = 0;
if ("()".includes(chord[idx])) idx++; // just ignore brackets!
console.log(idx + " - " + chord + " - " + chord[idx])
var root = chord[idx].toUpperCase();
idx++;
// could be #or b as well..
if ('#b'.includes(chord[idx]))
{
root += chord[idx];
idx++;
}
// triadType
var triad = -1;
var triadWord = "";
while ( (idx < chord.length) && !isNumber(chord[idx]) && chord[idx] != "/")
{
triadWord = triadWord + chord[idx++]
}
switch (triadWord)
{
case "": // there is no triad type - just nothing, or straight to numbers
if (idx == chord.length) {
triad = triadType.major; // just letter, so major
}
else if (chord[idx] == "6")
{
triad = triadType.major; // 6, or 6/9 == major
idx++;
} else
triad = triadType.dominant; // anything else is dominant = 7, 9, 13 etc.
break;
//
case "^": triad = triadType.major; break;
case "∆": triad = triadType.major; break;
case "Major": triad = triadType.major; break;
case "major": triad = triadType.major; break;
case "Maj": triad = triadType.major; break;
case "maj": triad = triadType.major; break;
case "Ma": triad = triadType.major; break;
case "ma": triad = triadType.major; break;
case "M": triad = triadType.major; break;
case "j": triad = triadType.major; break;
//
case "minor": triad = triadType.minor; break;
case "min": triad = triadType.minor; break;
case "mi": triad = triadType.minor; break;
case "m": triad = triadType.minor; break;
case "-": triad = triadType.minor; break;
case "-": triad = triadType.minor; break;
//
case "o": triad = triadType.diminished; break;
case "O": triad = triadType.diminished; break;
case "dim": triad = triadType.diminished; break;
case "°": triad = triadType.diminished; break;
//
case "0": triad = triadType.halfDiminished; break;
case "ø": triad = triadType.halfDiminished; break;
//
case "+": triad = triadType.augmented; break;
case "aug": triad = triadType.augmented; break;
//
}
// everything else is extensions.. until possibly a different bass note
var extensionWord = "";
var extensions = [];
while ( (idx < chord.length) && (chord[idx] != "/") )
{
if ("()".includes(extensionWord[c])) continue; // just ignore it!
extensionWord = extensionWord + chord[idx++]
}
var ex = "";
if (extensionWord.includes("alt")) // its an altered chord, we'll use a 7#5#9
{
extensionWord = "7#5#9";
triad = triadType.dominant; // just to be sure!
}
for (var c in extensionWord)
{
if ("()".includes(extensionWord[c])) continue; // just ignore it!
if ("#b".includes(extensionWord[c])) // if its a sharp or flat, that's the end of the current extension
{
extensions.push(ex);
ex = extensionWord[c];
continue;
}
ex = ex + extensionWord[c];
}
extensions.push(ex);
// we have a slash chord! the rest of the chordSymbol will be the bass note
var bassWord = "";
if (chord[idx] == "/")
{
idx++;
while ( idx < chord.length)
{
bassWord = bassWord + chord[idx++]
}
}
var bass = bassWord;
return {root: root, triad: triad, extensions: extensions, bass: bass}
}
//=============================================================================
// get a random pattern to use, based on the number of quarter notes required
function getPattern(quarterNotes)
{
var nonRoot = (Math.random() * 100) < nonRootPercent
if (quarterNotes == 1)
{
return "1";
}
if (quarterNotes == 2)
{
return nonRoot ?
patterns2NonRoot[Math.floor(Math.random() * patterns2NonRoot.length)] :
patterns2[Math.floor(Math.random() * patterns2.length)]
}
// for anything else, just return 4 at a time
return nonRoot ?
patternsNonRoot[Math.floor(Math.random() * patternsNonRoot.length)] :
patterns[Math.floor(Math.random() * patterns.length)];
}
//=============================================================================
// add a basseline - this is where it all starts!
function addBassLine(cursor)
{
var endTick = getEndTick(cursor);
cursor.rewind(Cursor.SELECTION_START);
var segment = curScore.selection.startSegment
var selectedStaff = curScore.selection.startStaff;
var chordSymbols = findAllChordSymbols(segment, selectedStaff, endTick);
chordSymbols.sort(compareChordSymbols);
previousPitch = -1;
cursor.setDuration(1, 4);
for (var c in chordSymbols)
{
addNotes(cursor, chordSymbols[c]);
}
}
//=============================================================================
// Sorting function for chord symbol structures, to sort by the tick value
// this helps when the user has selected a bunch of bars in reverse order
function compareChordSymbols(chord1, chord2)
{
if ( chord1.tick < chord2.tick ){
return -1;
}
if ( chord1.tick > chord2.tick){
return 1;
}
return 0;
}
//=============================================================================
// Search the current score for all the chord symbols in the current staff / track
// Returns an array of score chord symbol objects like {tick: 1440, duration: 960, text: "Db7"}.
function findAllChordSymbols(segment, staff, endTick)
{
var chords = {};
while (segment && (segment.tick < endTick))
{
var annotations = segment.annotations;
for (var a in annotations) {
var annotation = annotations[a];
if ((annotation.name == "Harmony") && (annotation.track / 4 == staff))
{
chords[segment.tick] = {tick: segment.tick, text: annotation.text, };
}
}
segment = segment.next;
}
// Calculate the duration of each chord = start time of next chord - start time of this chord.
// Also, copy all the chords to an Array, we no longer need them to be in an Object.
var result = [];
for (var i in chords)
{
var chord = chords[i];
if (result.length > 0)
{
result[result.length - 1].duration = chord.tick - result[result.length - 1].tick;
}
result.push(chord);
}
if (result.length > 0)
{
var lastItem = result[result.length - 1];
lastItem.duration = endTick - lastItem.tick;
}
return result;
}
//=============================================================================
// add notes at the current cursor.
// chordText: the full text of the chord to use (e.g. "Cm7b9/G")
// quarterNotes: the number of quarter notes to add
// chordSymbols[c].text, chordSymbols[c].duration / quarterNoteDuration
function addNotes(cursor, chordSymbol)
{
cursor.rewindToTick(chordSymbol.tick);
var chord = parseChord(chordSymbol.text);
var rootPitch = getLetterPitch(chord.root);
var quarterNotes = chordSymbol.duration / quarterNoteDuration;
// ensure we do enough quarterNotes
for (var i = 0; i < quarterNotes; i++)
{
// Get a random pattern to use, either in part or in whole
var pattern = getPattern(quarterNotes);
console.log(chordSymbol.text + ":"
+ (chordSymbol.text.length < 3 ? "\t" : "") + "\t"
+ pattern);
// for each note in the pattern
for (var j = 0; j < pattern.length; j++)
{
// ignore the spacers in the pattern
if (pattern[j] == "-") continue;
// if the pattern note is an approach note, then save that information for later
// and make it look like we've added a note it by advancing the number of notes (i)
if (("abv").includes(pattern[j]))
{
approachPattern = pattern[j]
approachTick = cursor.tick;
cursor.next()
i++;
continue;
}
var notePitch = -1;
if (j == 0 && chord.bass != "")
{
// a specified bass note will be used as the first note
notePitch = getLetterPitch(chord.bass)
}
else
{
notePitch = getIntervalPitch(rootPitch, pattern[j]);
// adjust the tones based on the chord quality
notePitch = adjustPitchToChordQuality(notePitch, pattern[j], chord.triad, cursor)
notePitch = adjustForExtensions(notePitch, pattern[j], chord.extensions)
}
// adjust the note octave so its the closest one to the previous note
notePitch = adjustPitchToBeClosestToPreviousPitch(notePitch, previousPitch)
// if this is the first note in the pattern, and there is no approach note
// then avoid duplicating the previous pitch
// this works by moving the previous pitch up or down
//if (j == 0 && (approachPattern == ""))
//{
// avoidFirstNoteIdenticalToLastNote(notePitch, previousPitch, cursor)
//}
// if we're duplicating a note, then,
// if this is the first note of the pattern, then adjust the last note of last pattern
// otherwise scale it an octave as specified in the options
if (notePitch == previousPitch)
{
if (j == 0)
{
avoidFirstNoteIdenticalToLastNote(notePitch, previousPitch, cursor)
}
else if ((Math.random() * 100) < octavesPercent)
{
notePitch += (Math.random() * 100) < 50 ? -12 : 12;
}
}
// make sure we haven't gone too far!
notePitch = ensurePitchIsInRange(notePitch)
// handle the pending approach note
// this might change the actual note, if its at the top or bottom of the range
if (approachPattern != "")
{
notePitch = insertApproachNote (notePitch, chord.triad, cursor)
approachPattern = "";
}
// finally insert the current actual note!
var noteTick = cursor.tick;
addNote(cursor, notePitch)
// add the pattern text to the first note
if (j == 0 && includePatternText)
{
addPatternTextToPreviousNote(cursor, pattern)
}
notes.push(notePitch);
previousPitch = notePitch;
// advance the note counter
i++;
}
}
}
//=============================================================================
// get a list of the pitches in the given key signature
// the list is the pitch values in the range from 0-11
// where 0 is C, 1 is C# etc.
function pitchesInKey(keySig)
{
// map the keySignature (number of flats or sharps) to a pitch note in the 0-11 pitch range
var root = 0;
if (keySig < 0) root = (keySig*(-5)) % 12;
if (keySig > 0) root = (keySig * 7) % 12;
// get the relevant pitches of the major scale
var notes = [];
for (var i = 1; i < 8; i++)
{
var note = (12 + root + intervalToSemitone[i]) % 12;
notes.push(note)
}
return notes;
}
//=============================================================================
// adjust the given pitch to the chord quality provided
// pitch: the starting pitch to use (e.g. 47 (== B2)
// interval: the interval the pitch is supposed to be in the chord (e.g. 3, the third)
// triad: the type of triad (eg Minor).
// In this case, the 47 would be adjusted to a 46, so it become a Bb, which is the _minor_ 3rd
function adjustPitchToChordQuality(pitch, interval, triad, keySig)
{
var newPitch = pitch;
switch (triad)
{
case triadType.major:
break;
case triadType.minor: // assume dorian - flat 3, 7
if (("37").includes(interval)) newPitch--
break;
case triadType.diminished: // assume HW scale... 1 b2 b3, #4, b5, 6, (bb7)
if (("2357").includes(interval)) newPitch--;
if (interval == "7") pitch--; // double flat == 6!
if (interval == "4") pitch++;
break;
case triadType.augmented: // assume whole tone scale 1, 2, 3, #4 #5, #6 7
if (("2456").includes(interval)) newPitch++;
break;
case triadType.dominant:
if (interval == "7") newPitch--;
break;
case triadType.halfDiminished: // assume locrian - flat 2, 3, 5, 6, 7
if (("23567").includes(interval)) newPitch--;
break;
}
return newPitch;
}
//=============================================================================
// adjust the given pitch according to the extensions of the chord
// e.g. if the chord is a C7b9, and we have a 2nd, then it should become a b2
function adjustForExtensions(pitch, interval, extensions)
{
var flat = true;
var extensionInterval = 0;
for (var ex in extensions)
{
if (extensions[ex].length < 2) continue; // its got to be at least 2 chars (eg. #5, not just 7)
flat = (extensions[ex][0] == "b"); // if not, then its a sharp
extensionInterval = extensions[ex].substring(1); // get the actual extension
if (extensionInterval > 8) extensionInterval -= 7; // 9->2, 11-> 4, 13->6
if (extensionInterval <=0) continue;
if (extensionInterval == interval)
{
pitch += flat ? -1 : 1;
break;
}
}
return pitch;
}
//=============================================================================
// adjust the current pitch so its in the closest octave position to the
// previous pitch. This helps to ensure a smoothly moving bass line
// e.g D->F should move a minor 3rd up, rather than a major 6th down
// If the distance between the notes is a 4th, a tritone, or a 5th,
// then randomly flip the octave, because it kind of doesn't matter that much.
function adjustPitchToBeClosestToPreviousPitch(notePitch, previousPitch)
{