-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtab-injectors.js
10004 lines (7830 loc) · 288 KB
/
tab-injectors.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
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
//
// app-anglo-fingerings.js
//
// ABC Muscial Notation Converter for Anglo Concertina
//
// This version for integration into the ABC Transcription Tools
//
// Annotates an ABC format tune with semi-optimal fingerings
// for the Anglo Concertina.
//
// This code is a fork of the original Anglo Concertina fingering utility at:
//
// https://jvandonsel.github.io/fingering/fingering.html
//
// Updated and extended 19 July 2023 by:
//
// Michael Eskin
// https://michaeleskin.com
//
// Copyright (c) 2013 James van Donsel
//
// Released under CC0 - No Rights Reserved
// https://creativecommons.org/share-your-work/public-domain/cc0/
//
//
// Put the whole thing in a function for isolation
//
var angloFingeringsGenerator = function (theABC, callback){
var gAngloVerbose = false;
var abcOutput = "";
// Button constructor
var Button = function(name, notes, cost, finger) {
this.name = name;
this.notes = notes;
this.cost = cost;
this.finger = finger;
};
// Button maps
// Lower cost means button is preferred
// Notes are [PUSH, DRAW].
var PUSH_INDEX = 0;
var DRAW_INDEX = 1;
var baseMapOriginal = {
// Top row, LH
"L1a": new Button("L1a", ["E,", "F,"], 10, "l4"),
"L2a": new Button("L2a", ["A,", "_B,"], 10, "l4"),
"L3a": new Button("L3a", ["^C", "_E"], 10, "l3"),
"L4a": new Button("L4a", ["A", "G"], 10, "l2"),
"L5a": new Button("L5a", ["^G", "_B"], 10, "l1"),
// Middle row, LH
"L1": new Button("L1", ["C,", "G,"], 1, "l4"),
"L2": new Button("L2", ["G,", "B,"], 1, "l4"),
"L3": new Button("L3", ["C", "D"], 1, "l3"),
"L4": new Button("L4", ["E", "F"], 1, "l2"),
"L5": new Button("L5", ["G", "A"], 1, "l1"),
// Bottom row, LH
"L6": new Button("L6", ["B,", "A,"], 1, "l4"),
"L7": new Button("L7", ["D", "^F"], 2, "l4"),
"L8": new Button("L8", ["G", "A"], 2, "l3"),
"L9": new Button("L9", ["B", "c"], 2, "l2"),
"L10": new Button("L10", ["d", "e"], 2, "l1"),
// Middle row, RH
"R1": new Button("R1", ["c", "B"], 1, "r1"),
"R2": new Button("R2", ["e", "d"], 1, "r2"),
"R3": new Button("R3", ["g", "f"], 2, "r3"),
"R4": new Button("R4", ["c'", "a"], 2, "r4"),
"R5": new Button("R5", ["e'", "b"], 2, "r4"),
// Bottom row, RH
"R6": new Button("R6", ["g", "^f"], 1, "r1"),
"R7": new Button("R7", ["b", "a"], 1, "r2"),
"R8": new Button("R8", ["d'", "c'"], 1, "r3"),
"R9": new Button("R9", ["g'", "e'"], 1, "r4"),
"R10": new Button("R10", ["b'", "^f'"], 1, "r4")
};
//
// Cross-row map for Wheatstone
//
// Original values commented out
//
// Prefers:
// Press d' on the left G row
// Draw e' on the left G row
//
//
var baseMapCrossRowWheatstone = {
// Top row, LH
"L1a": new Button("L1a", ["E,", "F,"], 10, "l4"),
"L2a": new Button("L2a", ["A,", "_B,"], 10, "l4"),
"L3a": new Button("L3a", ["^C", "_E"], 10, "l3"),
"L4a": new Button("L4a", ["A", "G"], 10, "l2"),
"L5a": new Button("L5a", ["^G", "_B"], 10, "l1"),
// Middle row, LH
"L1": new Button("L1", ["C,", "G,"], 1, "l4"),
"L2": new Button("L2", ["G,", "B,"], 1, "l4"),
"L3": new Button("L3", ["C", "D"], 1, "l3"),
"L4": new Button("L4", ["E", "F"], 1, "l2"),
"L5": new Button("L5", ["G", "A"], 1, "l1"),
// Bottom row, LH
"L6": new Button("L6", ["B,", "A,"], 1, "l4"),
"L7": new Button("L7", ["D", "^F"], 2, "l4"),
"L8": new Button("L8", ["G", "A"], 2, "l3"),
"L9": new Button("L9", ["B", "c"], 2, "l2"),
"L10": new Button("L10", ["d", "e"], 1, "l1"),
// Middle row, RH
"R1": new Button("R1", ["c", "B"], 1, "r1"),
"R2": new Button("R2", ["e", "d"], 2, "r2"),
"R3": new Button("R3", ["g", "f"], 2, "r3"),
"R4": new Button("R4", ["c'", "a"], 2, "r4"),
"R5": new Button("R5", ["e'", "b"], 2, "r4"),
// Bottom row, RH
"R6": new Button("R6", ["g", "^f"], 1, "r1"),
"R7": new Button("R7", ["b", "a"], 1, "r2"),
"R8": new Button("R8", ["d'", "c'"], 1, "r3"),
"R9": new Button("R9", ["g'", "e'"], 1, "r4"),
"R10": new Button("R10", ["b'", "^f'"], 1, "r4")
};
//
// My preferred fingering map for Cross-Row playing
//
// Prefers:
// Draw B on right C row
// Draw c' on the left G row
// Press d' on the left G row
// Draw e' on the left G row
//
var baseMapCrossRow = {
// Top row, LH
"L1a": new Button("L1a", ["E,", "F,"], 10, "l4"),
"L2a": new Button("L2a", ["A,", "_B,"], 10, "l4"),
"L3a": new Button("L3a", ["^C", "_E"], 10, "l3"),
"L4a": new Button("L4a", ["A", "G"], 10, "l2"),
"L5a": new Button("L5a", ["^G", "_B"], 10, "l1"),
// Middle row, LH
"L1": new Button("L1", ["C,", "G,"], 1, "l4"),
"L2": new Button("L2", ["G,", "B,"], 1, "l4"),
"L3": new Button("L3", ["C", "D"], 1, "l3"),
"L4": new Button("L4", ["E", "F"], 1, "l2"),
"L5": new Button("L5", ["G", "A"], 1, "l1"),
// Bottom row, LH
"L6": new Button("L6", ["B,", "A,"], 1, "l4"),
"L7": new Button("L7", ["D", "^F"], 2, "l4"),
"L8": new Button("L8", ["G", "A"], 2, "l3"),
"L9": new Button("L9", ["B", "z"], 2, "l2"),
"L9a": new Button("L9", ["z", "c"], 1, "l2"),
"L10": new Button("L10", ["d", "e"], 1, "l1"),
// Middle row, RH
"R1": new Button("R1", ["c", "x"], 2, "r1"),
"R1x": new Button("R1",["x", "B"], 1, "r1"),
"R2": new Button("R2", ["e", "d"], 2, "r2"),
"R3": new Button("R3", ["g", "f"], 2, "r3"),
"R4": new Button("R4", ["c'", "a"], 2, "r4"),
"R5": new Button("R5", ["e'", "b"], 2, "r4"),
// Bottom row, RH
"R6": new Button("R6", ["g", "^f"], 1, "r1"),
"R7": new Button("R7", ["b", "a"], 1, "r2"),
"R8": new Button("R8", ["d'", "c'"], 1, "r3"),
"R9": new Button("R9", ["g'", "e'"], 1, "r4"),
"R10": new Button("R10", ["b'", "^f'"], 1, "r4")
};
var jeffriesRxMapOriginal = {
// Top row, RH
"R1a": new Button("R1a", ["^d", "^c"], 1, "r1"),
"R2a": new Button("R2a", ["^c", "^d"], 10, "r2"),
"R3a": new Button("R3a", ["^g", "g"], 10, "r3"),
"R4a": new Button("R4a", ["^c'", "_b"], 10, "r4"),
"R5a": new Button("R5a", ["a", "d'"], 10, "r4")
};
// Prefers a R2a press C#
var jeffriesRxMapAlt = {
// Top row, RH
"R1a": new Button("R1a", ["^d", "^c"], 10, "r1"),
"R2a": new Button("R2a", ["^c", "^d"], 1, "r2"),
"R3a": new Button("R3a", ["^g", "g"], 10, "r3"),
"R4a": new Button("R4a", ["^c'", "_b"], 10, "r4"),
"R5a": new Button("R5a", ["a", "d'"], 10, "r4")
};
var wheatstoneRxMapOriginal = {
// Top row, RH
"R1a": new Button("R1a", ["^c", "^d"], 10, "r1"),
"R2a": new Button("R2a", ["a", "g"], 10, "r2"),
"R3a": new Button("R3a", ["^g", "_b"], 10, "r3"),
"R4a": new Button("R4a", ["^c'", "_e'"], 10, "r4"),
"R5a": new Button("R5a", ["a", "f'"], 10, "r4"),
};
var gAngloButtonNames_GaryCoover = [
// Top row, LH
"1a",
"2a",
"3a",
"4a",
"5a",
// Top row, RH
"1a",
"2a",
"3a",
"4a",
"5a",
// Middle row, LH
"1",
"2",
"3",
"4",
"5",
// Middle row, RH
"1",
"2",
"3",
"4",
"5",
// Bottom row, LH
"6",
"7",
"8",
"9",
"10",
// Bottom row, RH
"6",
"7",
"8",
"9",
"10"
];
// Initialization of the original button map
var jeffriesMap = null;
var wheatstoneMap = null;
var buttonToNoteMap = null;
var buttonMapIndex = null;
//
// Process the button naming matrix at tab build time
//
function processButtonNoteNames(map){
var originalButtonNames = gAngloButtonNames;
if (gInjectTab_GaryCoover){
gAngloButtonNames = gAngloButtonNames_GaryCoover;
}
// Top row
map["L1a"].name = gAngloButtonNames[0];
map["L2a"].name = gAngloButtonNames[1];
map["L3a"].name = gAngloButtonNames[2];
map["L4a"].name = gAngloButtonNames[3];
map["L5a"].name = gAngloButtonNames[4];
map["R1a"].name = gAngloButtonNames[5];
map["R2a"].name = gAngloButtonNames[6];
map["R3a"].name = gAngloButtonNames[7];
map["R4a"].name = gAngloButtonNames[8];
map["R5a"].name = gAngloButtonNames[9];
// Middle row
map["L1"].name = gAngloButtonNames[10];
map["L2"].name = gAngloButtonNames[11];
map["L3"].name = gAngloButtonNames[12];
map["L4"].name = gAngloButtonNames[13];
map["L5"].name = gAngloButtonNames[14];
map["R1"].name = gAngloButtonNames[15];
// Special case
if (map["R1x"]){
map["R1x"].name = gAngloButtonNames[15];
}
map["R2"].name = gAngloButtonNames[16];
map["R3"].name = gAngloButtonNames[17];
map["R4"].name = gAngloButtonNames[18];
map["R5"].name = gAngloButtonNames[19];
// Bottom row
map["L6"].name = gAngloButtonNames[20];
map["L7"].name = gAngloButtonNames[21];
map["L8"].name = gAngloButtonNames[22];
map["L9"].name = gAngloButtonNames[23];
// Special case
if (map["L9a"]){
map["L9a"].name = gAngloButtonNames[23];
}
map["L10"].name = gAngloButtonNames[24];
map["R6"].name = gAngloButtonNames[25];
map["R7"].name = gAngloButtonNames[26];
map["R8"].name = gAngloButtonNames[27];
map["R9"].name = gAngloButtonNames[28];
map["R10"].name = gAngloButtonNames[29];
// Restore the original map
if (gInjectTab_GaryCoover){
gAngloButtonNames = originalButtonNames;
}
return map;
}
//
// Called at solution generation time
//
function setButtonToNoteMap() {
var baseMapIndex = parseInt(gInjectTab_ConcertinaFingering);
jeffriesMap = [];
wheatstoneMap = [];
switch (baseMapIndex) {
// Original
case 0:
for (var x in jeffriesRxMapAlt) {
jeffriesMap[x] = jeffriesRxMapAlt[x];
}
for (var x in wheatstoneRxMapOriginal) {
wheatstoneMap[x] = wheatstoneRxMapOriginal[x];
}
for (var x in baseMapOriginal) {
jeffriesMap[x] = baseMapOriginal[x];
wheatstoneMap[x] = baseMapOriginal[x];
}
break;
// Cross-row
case 1:
for (var x in jeffriesRxMapAlt) {
jeffriesMap[x] = jeffriesRxMapAlt[x];
}
for (var x in wheatstoneRxMapOriginal) {
wheatstoneMap[x] = wheatstoneRxMapOriginal[x];
}
for (var x in baseMapCrossRow) {
jeffriesMap[x] = baseMapCrossRow[x];
}
for (var x in baseMapCrossRow) {
wheatstoneMap[x] = baseMapCrossRow[x];
}
break;
}
if (parseInt(gInjectTab_ConcertinaStyle) == 0){
buttonToNoteMap = jeffriesMap;
}
else{
buttonToNoteMap = wheatstoneMap;
}
// Inject custom button names
buttonToNoteMap = processButtonNoteNames(buttonToNoteMap);
}
// Globals
var keySignature = null;
var bestCost;
var bestPathFromState = null;
var stateCount = 0;
function angloLog(s) {
if (gAngloVerbose)
console.log(s);
}
function generateAngloFingerings(abcInput) {
angloLog("Got input:" + abcInput);
// Make sure the button note map is up to date
setButtonToNoteMap();
// Find the key signature in the input
keySignature = findKeySignature(abcInput);
if (keySignature == null) {
return ("ERROR: Unknown or unsupported key signature");
}
// Generate an array of note objects. Each
var notes = getAbcNotes(abcInput);
// Generate the inverse mapping
var noteToButtonMap = generateNoteToButtonMap(buttonToNoteMap);
// Sort the inverse mapping table with the least costly buttons first.
// This speeds up tree pruning later
sortButtonMap(noteToButtonMap);
// Generate a state tree
var stateTree = generateStateTree(notes, noteToButtonMap);
var path = chooseFingerings(stateTree);
if (path == null) {
angloLog("No fingerings generated!");
return abcOutput;
}
// Merge the chosen fingerings with the ABC notation
abcOutput = mergeFingerings(abcInput, path, notes);
return abcOutput;
}
// Path constructor.
//
// buttons: list of States
// cost: integer cost of total set of buttons, lower is better
var Path = function(states, cost) {
this.states = states;
this.cost = cost;
};
// Note constructor
var Note = function(index, unNormalizedValue, normalizedValue) {
this.index = index; // Index of this note in the original ABC input string
// These values an ABC string like "G" or "^A'"
// Unnormalized means it's the literal note string from the ABC source.
this.unNormalizedValue = unNormalizedValue;
// Normalized means it's adjusted by the key signature and extra decorations are removed.
this.normalizedValue = normalizedValue;
};
// State constructor
var State = function(note, button, direction) {
this.note = note;
this.button = button;
this.direction = direction;
this.nextStates = [];
this.id = stateCount++;
// This toString() function is needed to make sure this object is unique
// in a hash map. JS can only use strings as keys in hashes (objects).
this.toString = function() {
var s = "[" + this.id + "] Note:";
if (this.note) {
s += this.note.normalizedValue;
} else {
s += "none";
}
s += " Button:" + this.button.name;
return s;
};
};
// Determines the key signature
// abcInput: ABC input string
// returns: key signature map to use, or null on error.
function findKeySignature(abcInput) {
var myMap = null;
var keyMatch = abcInput.match(/^K: *([A-G])([b#])? *(.*?)$/m);
if (keyMatch == null || keyMatch.length < 3) {
return null;
}
var keySignatureBase;
var keyExtra;
if (keyMatch[2] == undefined) {
keySignatureBase = keyMatch[1];
} else {
keySignatureBase = keyMatch[1] + keyMatch[2];
}
keyExtra = keyMatch[3].toLowerCase();
// Strip any trailing comments
var searchExp = /%.*/
keyExtra = keyExtra.replace(searchExp,"");
keyExtra = keyExtra.trim();
angloLog("Got base key of '" + keySignatureBase + "' and extra of '" + keyExtra + "'");
// Determine musical mode
if (keyExtra == "" ||
keyExtra.search("maj") != -1 ||
keyExtra.search("ion") != -1) {
angloLog("Mode: Ionian (major)");
myMap = keySignatureMap(keySignatureBase, 0);
} else if (keyExtra.search("mix") != -1) {
angloLog("Mode: Mixolydian");
myMap = keySignatureMap(keySignatureBase, 1);
} else if (keyExtra.search("dor") != -1) {
angloLog("Mode: Dorian");
myMap = keySignatureMap(keySignatureBase, 2);
} else if ((keyExtra.search("m") != -1 && keyExtra.search("mix") == -1) ||
keyExtra.search("min") != -1 ||
keyExtra.search("aeo") != -1) {
angloLog("Mode: Aeolian (minor)");
myMap = keySignatureMap(keySignatureBase, 3);
} else if (keyExtra.search("phr") != -1) {
angloLog("Mode: Phrygian");
myMap = keySignatureMap(keySignatureBase, 4);
} else if (keyExtra.search("loc") != -1) {
angloLog("Mode: Locrian");
myMap = keySignatureMap(keySignatureBase, 5);
} else if (keyExtra.search("lyd") != -1) {
angloLog("Mode: Lydian");
myMap = keySignatureMap(keySignatureBase, -1);
} else if (keyExtra.search("exp") != -1) {
angloLog("(Accidentals to be explicitly specified)");
myMap = keySignatureMap("C", 0);
} else {
// Unknown
angloLog("Failed to determine key signature mode");
myMap = null;
}
if (myMap == null) {
return myMap;
}
//Handle explicit accidentals
var explicitFlats = keyExtra.match(/_./g);
var explicitSharps = keyExtra.match(/\^./g);
for (note in explicitFlats) {
myMap.flats += explicitFlats[note][1].toUpperCase();
}
for (note in explicitSharps) {
myMap.sharps += explicitSharps[note][1].toUpperCase();
}
return myMap;
}
// Calculates a key signature map given a tonic and a mode
function keySignatureMap(tonic, modeFlatness) {
var circleOfFifths = "FCGDAEB";
var signature = {
sharps: "",
flats: ""
};
var baseSharpness = circleOfFifths.indexOf(tonic[0]) - 1;
if (baseSharpness == -2) {
angloLog("Bad tonic: " + tonic);
return null;
}
if (tonic.slice(1) == "b") {
baseSharpness -= 7;
} else if (tonic.slice(1) == "#") {
baseSharpness += 7;
}
var totalSharpness = baseSharpness - modeFlatness;
if (totalSharpness > 7) {
angloLog("Too many sharps: " + totalSharpness);
return null;
} else if (totalSharpness < -7) {
angloLog("Too many flats: " + (totalSharpness * -1));
return null;
} else if (totalSharpness > 0) {
signature.sharps = circleOfFifths.slice(0, totalSharpness);
} else if (totalSharpness < 0) {
signature.flats = circleOfFifths.slice(totalSharpness);
}
signature.accidentalSharps = "";
signature.accidentalFlats = "";
signature.accidentalNaturals = "";
return signature;
}
// Merges an array of Button objects with an array of Notes
// with the original string input.
// Returns a merged string.
function mergeFingerings(input, path, notes) {
// Drop the first state of the path - it's a dummy root state
path.states.shift();
if (path.states.length != notes.length) {
return "ERROR: Internal error. Length mismatch";
}
var result = input;
var insertedTotal = 0;
var location = parseInt(gInjectTab_TabLocation);
// Button name is in path.states[i].button.finger first character
for (var i = 0; i < path.states.length; ++i) {
var index = notes[i].index + insertedTotal;
var fingering = path.states[i].button.name;
if (gInjectTab_GaryCoover){
var finger = path.states[i].button.finger;
var isLeftSide = (finger.indexOf("l") == 0);
// Left side is below the staff
if (isLeftSide){
if (path.states[i].direction == PUSH_NAME){
// Add double quotes to fingering, to be rendered below the note
fingering = "\"_" + " " + ";" + fingering + "\"";
}
else{
var len = fingering.length;
var theBar = "";
for (var j=0;j<len;++j){
theBar += "_";
}
var origFingering = fingering;
fingering = "\"_" + theBar + ";";
fingering = fingering + origFingering + "\"";
}
}
else{
if (path.states[i].direction == PUSH_NAME){
// Add double quotes to fingering, to be rendered above the note
fingering = "\"^" + fingering + "\"";
}
else{
var len = fingering.length;
var theBar = "";
for (var j=0;j<len;++j){
theBar += "_";
}
var origFingering = fingering;
fingering = "\"^" + theBar + ";";
fingering = fingering + origFingering + "\"";
}
}
}
else{
if (gInjectTab_UseBarForDraw){
switch (location){
// Above
case 0:
if (path.states[i].direction == PUSH_NAME){
// Add double quotes to fingering, to be rendered above the note
fingering = "\"^" + fingering + "\"";
}
else{
var len = fingering.length;
var theBar = "";
for (var j=0;j<len;++j){
theBar += "_";
}
var origFingering = fingering;
fingering = "\"^" + theBar + ";";
fingering = fingering + origFingering + "\"";
}
break;
// Below
case 1:
if (path.states[i].direction == PUSH_NAME){
// Add double quotes to fingering, to be rendered below the note
fingering = "\"_" + " " + ";" + fingering + "\"";
}
else{
var len = fingering.length;
var theBar = "";
for (var j=0;j<len;++j){
theBar += "_";
}
var origFingering = fingering;
fingering = "\"_" + theBar + ";";
fingering = fingering + origFingering + "\"";
}
break;
}
}
else{
switch (location){
// Above
case 0:
// Add double quotes to fingering, to be rendered above the note
fingering = "\"^" + fingering + ";";
// Optionally append bellows direction, to be rendered below the button number.
fingering = fingering + path.states[i].direction + "\"";
break;
// Below
case 1:
// Add double quotes to fingering, to be rendered below the note
fingering = "\"_" + fingering + ";";
// Optionally append bellows direction, to be rendered below the button number.
fingering = fingering + path.states[i].direction + "\"";
break;
}
}
}
var fingLen = fingering.length;
//angloLog("Merge["+i+"] index="+index+" fingLen="+fingLen+" insertedTotal="+insertedTotal);
result = result.substr(0, index) + fingering + result.substr(index);
insertedTotal += fingLen;
}
return result;
}
// Determines the bellows direction (PUSH/DRAW) given
// a button and note.
function findBellowsDirection(note, button) {
if (note.normalizedValue == button.notes[PUSH_INDEX]) {
return PUSH_NAME;
} else if (note.normalizedValue == button.notes[DRAW_INDEX]) {
return DRAW_NAME;
} else {
var respelled = respellNormalized(note.normalizedValue);
if (respelled == button.notes[PUSH_INDEX]) {
return PUSH_NAME;
} else if (respelled == button.notes[DRAW_INDEX]) {
return DRAW_NAME;
} else {
return " "; // Better than saying "null"
}
}
}
// Determines if these two buttons would be a hop if
// played back-to-back
function isHop(button1, button2) {
// Check for finger hops (i.e. same finger, different button)
return (button1.finger == button2.finger &&
button1.name != button2.name);
}
// Generate a state tree from the notes.
// Each note will generate several possible states, each representing
// a possible button. All states corresponding to a note will be
// cross-connected to all states in the next note.
// Returns an initial (dummy) State, with next states corresponding to
// the first note of the tune.
function generateStateTree(notes, noteToButtonMap) {
var firstNoteStates = null;
var lastNoteStates = null;
for (var i = 0; i < notes.length; ++i) {
var note = notes[i];
var normalizedValue = note.normalizedValue;
var buttons = noteToButtonMap[normalizedValue];
if (buttons == null || buttons.length < 1) {
angloLog("Failed to find button for note " + normalizedValue);
angloLog("Attempting respell...");
var otherName = respell(normalizedValue);
angloLog("Respelled as " + otherName);
buttons = noteToButtonMap[otherName];
if (buttons == null || buttons.length < 1) {
angloLog("Respelling as " + otherName + " failed to find a button.");
abcOutput = "ERROR:Failed to find button for note '" + normalizedValue + "'";
return null;
}
}
var states = [];
// Create a state per button
buttons.forEach(function(button) {
states.push(new State(note, button, findBellowsDirection(note, button)));
});
// Remember the start of the tree - that's what we will return
if (firstNoteStates == null) {
firstNoteStates = states;
}
// Cross-connect the last set of states to these new states
if (lastNoteStates != null) {
lastNoteStates.forEach(function(lastState) {
lastState.nextStates = states;
});
}
lastNoteStates = states;
}
// Create a dummy state as our tree root
var rootState = new State(null, new Button("root", [], 0, "none"));
rootState.nextStates = firstNoteStates;
return rootState;
} // end generateStateTree
// Chooses fingerings.
// returns: a Path object with the best button choices
//
// This is the guts of this program. Uses various
// heuristics to choose semi-optimal fingerings
// for the given note sequence.
//
// Recursively chooses the best fingering from
// all possible fingerings.
function chooseFingerings(stateTree) {
bestCost = 100000000;
bestPathFromState = {};
return chooseFingeringsRecursive(stateTree);
}
// Choose best fingerings for current state.
// Returns a Path
function chooseFingeringsRecursive(state) {
if (state.nextStates.length == 0) {
// Done with notes. Bubble back up.
angloLog("Last state, note=" + state.note.normalizedValue + " button=" + state.button.name + ". Popping up the stack");
return new Path([state], state.button.cost);
}
var note = state.note;
var unNormalizedValue = null;
var normalizedValue = null;
if (note != null) {
unNormalizedValue = note.unNormalizedValue;
normalizedValue = note.normalizedValue;
}
angloLog("Choosing: current note=" + normalizedValue + " button=" + state.button.name);
if (bestPathFromState[state]) {
angloLog("Already visited state note=" + state.note.normalizedValue + " button=" + state.button.name);
return bestPathFromState[state];
}
var bestPath = new Path([], 10000000);
// Consider all the possible next states
for (var i = 0; i < state.nextStates.length; ++i) {
var nextState = state.nextStates[i];
angloLog("Trying next [" + i + "/" + state.nextStates.length + "] note=" + nextState.note.normalizedValue + " button=" + nextState.button.name);
// Recurse! Find the fingering for the rest of the tune.
var path = chooseFingeringsRecursive(nextState);
if (path == null) {
angloLog("Could not get fingerings");
return null;
}
var myCost = state.button.cost;
var HOP_COST = 100;
if (path.states.length != 0) {
var nextButton = path.states[0].button;
var nextFinger = path.states[0].button.finger;
// Check for finger hops (i.e. same finger, different button)
if (isHop(state.button, nextButton)) {
// Penalize finger hops (i.e. same finger, different button)
angloLog("Penalizing finger hop for note " + normalizedValue);
myCost += HOP_COST;
}
}
angloLog("path had cost of " + path.cost + " my cost=" + myCost);
if (path.cost + myCost < bestPath.cost) {
// Best choice so far.
// Prepend this State to the list
var newStateList = path.states.slice(0); // clone array
newStateList.unshift(state);
bestPath = new Path(newStateList, path.cost + myCost);
angloLog("New best path for note[" + normalizedValue + "], cost=" + bestPath.cost);
}
} // end for nextState