-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathModelUtils.ts
More file actions
1133 lines (1001 loc) · 39.9 KB
/
Copy pathModelUtils.ts
File metadata and controls
1133 lines (1001 loc) · 39.9 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
import { AccidentalType } from '@coderline/alphatab/model/AccidentalType';
import { Automation, AutomationType } from '@coderline/alphatab/model/Automation';
import { Bar } from '@coderline/alphatab/model/Bar';
import { Beat } from '@coderline/alphatab/model/Beat';
import { Duration } from '@coderline/alphatab/model/Duration';
import type { KeySignature } from '@coderline/alphatab/model/KeySignature';
import { KeySignatureType } from '@coderline/alphatab/model/KeySignatureType';
import { MasterBar } from '@coderline/alphatab/model/MasterBar';
import { NoteAccidentalMode } from '@coderline/alphatab/model/NoteAccidentalMode';
import { HeaderFooterStyle, type Score, ScoreStyle, type ScoreSubElement } from '@coderline/alphatab/model/Score';
import type { Track } from '@coderline/alphatab/model/Track';
import { Voice } from '@coderline/alphatab/model/Voice';
import type { Settings } from '@coderline/alphatab/Settings';
import { SynthConstants } from '@coderline/alphatab/synth/SynthConstants';
/**
* @internal
*/
export class TuningParseResult {
public note: string | null = null;
public tone: TuningParseResultTone = new TuningParseResultTone();
public octave: number = 0;
public get realValue(): number {
return this.octave * 12 + this.tone.noteValue;
}
}
/**
* @internal
*/
export class TuningParseResultTone {
public noteValue: number;
public accidentalMode: NoteAccidentalMode;
public constructor(noteValue: number = 0, accidentalMode: NoteAccidentalMode = NoteAccidentalMode.Default) {
this.noteValue = noteValue;
this.accidentalMode = accidentalMode;
}
}
/**
* @internal
* @record
*/
export interface ResolvedSpelling {
degree: number;
accidentalOffset: number;
chroma: number;
octave: number;
}
/**
* @internal
* @record
*/
interface SpellingBase {
degree: number;
accidentalOffset: number;
}
/**
* This public class contains some utilities for working with model public classes
* @partial
* @internal
*/
export class ModelUtils {
private static readonly _durationIndices = ModelUtils._buildDurationIndices();
private static _buildDurationIndices() {
return new Map<Duration, number>(
Object.values(Duration)
.filter<any>((k: any) => typeof k === 'number')
.map(d => [d as number as Duration, (d as number) < 0 ? 0 : Math.log2(d as number) | 0])
);
}
public static getIndex(duration: Duration): number {
return ModelUtils._durationIndices.get(duration)!;
}
public static keySignatureIsFlat(ks: number): boolean {
return ks < 0;
}
public static keySignatureIsNatural(ks: number): boolean {
return ks === 0;
}
public static keySignatureIsSharp(ks: number): boolean {
return ks > 0;
}
public static applyPitchOffsets(settings: Settings, score: Score): void {
for (let i: number = 0; i < score.tracks.length; i++) {
if (i < settings.notation.displayTranspositionPitches.length) {
for (const staff of score.tracks[i].staves) {
staff.displayTranspositionPitch = -settings.notation.displayTranspositionPitches[i];
}
}
if (i < settings.notation.transpositionPitches.length) {
for (const staff of score.tracks[i].staves) {
staff.transpositionPitch = -settings.notation.transpositionPitches[i];
}
}
}
}
/**
* Checks if the given string is a tuning inticator.
* @param name
*/
public static isTuning(name: string): boolean {
return !!ModelUtils.parseTuning(name);
}
/**
* @internal
*/
public static readonly tuningLetters = new Set<number>([
0x43 /* C */, 0x44 /* D */, 0x45 /* E */, 0x46 /* F */, 0x47 /* G */, 0x41 /* A */, 0x42 /* B */, 0x63 /* c */,
0x64 /* d */, 0x65 /* e */, 0x66 /* f */, 0x67 /* g */, 0x61 /* a */, 0x62 /* b */, 0x23 /* # */
]);
public static parseTuning(name: string): TuningParseResult | null {
let note: string = '';
let octave: string = '';
for (let i: number = 0; i < name.length; i++) {
const c: number = name.charCodeAt(i);
if (c >= 0x30 && c <= 0x39 /* 0-9 */) {
// number without note?
if (!note) {
return null;
}
octave += String.fromCharCode(c);
} else if (note.length === 0) {
if (ModelUtils.tuningLetters.has(c)) {
note += String.fromCharCode(c);
} else {
return null;
}
} else {
note += String.fromCharCode(c);
}
}
if (!octave || !note) {
return null;
}
const result: TuningParseResult = new TuningParseResult();
result.octave = Number.parseInt(octave, 10) + 1;
result.note = note.toLowerCase();
const tone = ModelUtils.getToneForText(result.note);
if (tone === null) {
return null;
}
result.tone = tone;
// if tone.noteValue is negative (eg. on Cb note)
// we adjust roll-over to a lower octave
if (result.tone.noteValue < 0) {
result.octave--;
result.tone.noteValue += 12;
}
return result;
}
public static getTuningForText(str: string): number {
const result: TuningParseResult | null = ModelUtils.parseTuning(str);
if (!result) {
return -1;
}
return result.realValue;
}
public static getToneForText(note: string): TuningParseResultTone | null {
const noteName = note.substring(0, 1);
const accidental = note.substring(1);
let noteValue: number;
let noteAccidenalMode: NoteAccidentalMode;
switch (noteName.toLowerCase()) {
case 'c':
noteValue = 0;
break;
case 'd':
noteValue = 2;
break;
case 'e':
noteValue = 4;
break;
case 'f':
noteValue = 5;
break;
case 'g':
noteValue = 7;
break;
case 'a':
noteValue = 9;
break;
case 'b':
noteValue = 11;
break;
default:
return null;
}
if (!ModelUtils.accidentalModeMapping.has(accidental)) {
return null;
}
noteAccidenalMode = ModelUtils.parseAccidentalMode(accidental);
switch (noteAccidenalMode) {
case NoteAccidentalMode.Default:
break;
case NoteAccidentalMode.ForceNone:
break;
case NoteAccidentalMode.ForceNatural:
break;
case NoteAccidentalMode.ForceSharp:
noteValue++;
break;
case NoteAccidentalMode.ForceDoubleSharp:
noteValue += 2;
break;
case NoteAccidentalMode.ForceFlat:
noteValue--;
break;
case NoteAccidentalMode.ForceDoubleFlat:
noteValue -= 2;
break;
}
return new TuningParseResultTone(noteValue, noteAccidenalMode);
}
/**
* @internal
*/
public static readonly reverseAccidentalModeMapping = new Map<NoteAccidentalMode, string>([
[NoteAccidentalMode.Default, 'd'],
[NoteAccidentalMode.ForceNone, 'forcenone'],
[NoteAccidentalMode.ForceNatural, 'forcenatural'],
[NoteAccidentalMode.ForceSharp, '#'],
[NoteAccidentalMode.ForceDoubleSharp, 'x'],
[NoteAccidentalMode.ForceFlat, 'b'],
[NoteAccidentalMode.ForceDoubleFlat, 'bb']
]);
/**
* @internal
*/
public static readonly accidentalModeMapping = new Map<string, NoteAccidentalMode>([
['default', NoteAccidentalMode.Default],
['d', NoteAccidentalMode.Default],
['', NoteAccidentalMode.Default],
['forcenone', NoteAccidentalMode.ForceNone],
['-', NoteAccidentalMode.ForceNone],
['forcenatural', NoteAccidentalMode.ForceNatural],
['n', NoteAccidentalMode.ForceNatural],
['forcesharp', NoteAccidentalMode.ForceSharp],
['#', NoteAccidentalMode.ForceSharp],
['forcedoublesharp', NoteAccidentalMode.ForceDoubleSharp],
['##', NoteAccidentalMode.ForceDoubleSharp],
['x', NoteAccidentalMode.ForceDoubleSharp],
['forceflat', NoteAccidentalMode.ForceFlat],
['b', NoteAccidentalMode.ForceFlat],
['forcedoubleflat', NoteAccidentalMode.ForceDoubleFlat],
['bb', NoteAccidentalMode.ForceDoubleFlat]
]);
public static parseAccidentalMode(data: string): NoteAccidentalMode {
const key = data.toLowerCase();
if (ModelUtils.accidentalModeMapping.has(key)) {
return ModelUtils.accidentalModeMapping.get(key)!;
}
return NoteAccidentalMode.Default;
}
public static newGuid(): string {
return `${
Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1) +
Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)
}-${Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)}-${Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)}-${Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)}-${Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)}${Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)}${Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)}`;
}
public static isAlmostEqualTo(a: number, b: number): boolean {
return Math.abs(a - b) < 0.00001;
}
public static toHexString(n: number, digits: number = 0): string {
let s: string = '';
const hexChars: string = '0123456789ABCDEF';
do {
s = String.fromCharCode(hexChars.charCodeAt(n & 15)) + s;
n = n >> 4;
} while (n > 0);
while (s.length < digits) {
s = `0${s}`;
}
return s;
}
/**
* Gets the list of alternate endings on which the master bar is played.
* @param bitflag The alternate endings bitflag.
*/
public static getAlternateEndingsList(bitflag: number): number[] {
const endings: number[] = [];
for (let i: number = 0; i < MasterBar.MaxAlternateEndings; i++) {
if ((bitflag & (0x01 << i)) !== 0) {
endings.push(i);
}
}
return endings;
}
public static deltaFretToHarmonicValue(deltaFret: number): number {
switch (deltaFret) {
case 2:
return 2.4;
case 3:
return 3.2;
case 4:
case 5:
case 7:
case 9:
case 12:
case 16:
case 17:
case 19:
case 24:
return deltaFret;
case 8:
return 8.2;
case 10:
return 9.6;
case 14:
case 15:
return 14.7;
case 21:
case 22:
return 21.7;
default:
return 12;
}
}
public static clamp(value: number, min: number, max: number): number {
if (value <= min) {
return min;
}
if (value >= max) {
return max;
}
return value;
}
public static buildMultiBarRestInfo(
tracks: Track[] | null,
startIndex: number,
endIndexInclusive: number
): Map<number, number[]> | null {
if (!tracks) {
return null;
}
const stylesheet = tracks[0].score.stylesheet;
const shouldDrawMultiBarRests: boolean =
tracks.length > 1
? stylesheet.multiTrackMultiBarRest
: stylesheet.perTrackMultiBarRest?.has(tracks[0].index) === true;
if (!shouldDrawMultiBarRests) {
return null;
}
const lookup = new Map<number, number[]>();
const score = tracks[0].score;
let currentIndex = startIndex;
let tempo = score.tempo;
while (currentIndex <= endIndexInclusive) {
const currentGroupStartIndex = currentIndex;
let currentGroup: number[] | null = null;
while (currentIndex <= endIndexInclusive) {
const masterBar = score.masterBars[currentIndex];
let hasTempoChange = false;
for (const a of masterBar.tempoAutomations) {
if (a.value !== tempo) {
hasTempoChange = true;
}
tempo = a.value;
}
// check if masterbar breaks multibar rests, it must be fully empty with no annotations
if (
masterBar.alternateEndings ||
(masterBar.isRepeatStart && masterBar.index !== currentGroupStartIndex) ||
masterBar.isFreeTime ||
masterBar.isAnacrusis ||
masterBar.section !== null ||
(masterBar.index !== currentGroupStartIndex && hasTempoChange) ||
(masterBar.fermata !== null && masterBar.fermata.size > 0) ||
(masterBar.directions !== null && masterBar.directions.size > 0)
) {
break;
}
// check if masterbar breaks multibar rests because of change to previous
if (
currentGroupStartIndex > startIndex &&
masterBar.previousMasterBar &&
(masterBar.timeSignatureCommon !== masterBar.previousMasterBar!.timeSignatureCommon ||
masterBar.timeSignatureNumerator !== masterBar.previousMasterBar!.timeSignatureNumerator ||
masterBar.timeSignatureDenominator !== masterBar.previousMasterBar!.timeSignatureDenominator ||
masterBar.tripletFeel !== masterBar.previousMasterBar!.tripletFeel)
) {
break;
}
// masterbar is good, now check bars across staves
let areAllBarsSuitable = true;
for (const t of tracks) {
for (const s of t.staves) {
const bar = s.bars[masterBar.index];
if (!bar.isRestOnly) {
areAllBarsSuitable = false;
break;
}
if (
bar.index > 0 &&
(bar.keySignature !== bar.previousBar!.keySignature ||
bar.keySignatureType !== bar.previousBar!.keySignatureType)
) {
areAllBarsSuitable = false;
break;
}
}
if (!areAllBarsSuitable) {
break;
}
}
if (!areAllBarsSuitable) {
break;
}
// skip initial bar as it is not "additional" but we are checking it
currentIndex++;
if (masterBar.index > currentGroupStartIndex) {
if (currentGroup === null) {
currentGroup = [masterBar.index];
} else {
currentGroup.push(masterBar.index);
}
}
// special scenario -> repeat ends are included but then we stop
if (masterBar.isRepeatEnd) {
break;
}
}
if (currentGroup) {
lookup.set(currentGroupStartIndex, currentGroup);
} else {
currentIndex++;
}
}
return lookup;
}
public static computeFirstDisplayedBarIndex(score: Score, settings: Settings) {
let startIndex: number = settings.display.startBar;
startIndex--; // map to array index
startIndex = Math.min(score.masterBars.length - 1, Math.max(0, startIndex));
return startIndex;
}
public static computeLastDisplayedBarIndex(score: Score, settings: Settings, startIndex: number) {
let endBarIndex: number = settings.display.barCount;
if (endBarIndex < 0) {
endBarIndex = score.masterBars.length;
}
endBarIndex = startIndex + endBarIndex - 1; // map count to array index
endBarIndex = Math.min(score.masterBars.length - 1, Math.max(0, endBarIndex));
return endBarIndex;
}
public static getOrCreateHeaderFooterStyle(score: Score, element: ScoreSubElement) {
let style = score.style;
if (!score.style) {
style = new ScoreStyle();
score.style = style;
}
let headerFooterStyle: HeaderFooterStyle;
if (style!.headerAndFooter.has(element)) {
headerFooterStyle = style!.headerAndFooter.get(element)!;
} else {
headerFooterStyle = new HeaderFooterStyle();
if (ScoreStyle.defaultHeaderAndFooter.has(element)) {
const defaults = ScoreStyle.defaultHeaderAndFooter.get(element)!;
headerFooterStyle.template = defaults.template;
headerFooterStyle.textAlign = defaults.textAlign;
}
style!.headerAndFooter.set(element, headerFooterStyle);
}
return headerFooterStyle;
}
/**
* Performs some general consolidations of inconsistencies on the given score like
* missing bars, beats, duplicated midi channels etc
*/
public static consolidate(score: Score) {
// empty score?
if (score.masterBars.length === 0) {
const master: MasterBar = new MasterBar();
score.addMasterBar(master);
const tempoAutomation = new Automation();
tempoAutomation.isLinear = false;
tempoAutomation.type = AutomationType.Tempo;
tempoAutomation.value = score.tempo;
master.tempoAutomations.push(tempoAutomation);
const bar: Bar = new Bar();
score.tracks[0].staves[0].addBar(bar);
const v = new Voice();
bar.addVoice(v);
const emptyBeat: Beat = new Beat();
emptyBeat.isEmpty = true;
v.addBeat(emptyBeat);
return;
}
const usedChannels = new Set<number>([SynthConstants.PercussionChannel]);
for (const track of score.tracks) {
// ensure percussion channel
if (track.staves.length === 1 && track.staves[0].isPercussion) {
track.playbackInfo.primaryChannel = SynthConstants.PercussionChannel;
track.playbackInfo.secondaryChannel = SynthConstants.PercussionChannel;
} else {
// unique midi channels and generate secondary channels
if (track.playbackInfo.primaryChannel !== SynthConstants.PercussionChannel) {
while (usedChannels.has(track.playbackInfo.primaryChannel)) {
track.playbackInfo.primaryChannel++;
}
}
usedChannels.add(track.playbackInfo.primaryChannel);
if (track.playbackInfo.secondaryChannel !== SynthConstants.PercussionChannel) {
while (usedChannels.has(track.playbackInfo.secondaryChannel)) {
track.playbackInfo.secondaryChannel++;
}
}
usedChannels.add(track.playbackInfo.secondaryChannel);
}
for (const staff of track.staves) {
// fill empty beats
for (const b of staff.bars) {
for (const v of b.voices) {
if (v.isEmpty && v.beats.length === 0) {
const emptyBeat: Beat = new Beat();
emptyBeat.isEmpty = true;
v.addBeat(emptyBeat);
}
}
}
// fill missing bars
const voiceCount = staff.bars.length === 0 ? 1 : staff.bars[0].voices.length;
while (staff.bars.length < score.masterBars.length) {
const bar: Bar = new Bar();
staff.addBar(bar);
const previousBar = bar.previousBar;
if (previousBar) {
bar.clef = previousBar.clef;
bar.clefOttava = previousBar.clefOttava;
bar.keySignature = bar.previousBar!.keySignature;
bar.keySignatureType = bar.previousBar!.keySignatureType;
}
for (let i = 0; i < voiceCount; i++) {
const v = new Voice();
bar.addVoice(v);
const emptyBeat: Beat = new Beat();
emptyBeat.isEmpty = true;
v.addBeat(emptyBeat);
}
}
}
}
// ensure first masterbar has a tempo automation for score tempo
if (score.masterBars.length > 0) {
const firstTempoAutomation = score.masterBars[0].tempoAutomations.find(
a => a.type === AutomationType.Tempo && a.ratioPosition === 0
);
if (!firstTempoAutomation) {
const tempoAutomation = new Automation();
tempoAutomation.isLinear = false;
tempoAutomation.type = AutomationType.Tempo;
tempoAutomation.value = score.tempo;
tempoAutomation.text = score.tempoLabel;
tempoAutomation.isVisible = false;
score.masterBars[0].tempoAutomations.push(tempoAutomation);
}
}
}
/**
* Trims any empty bars at the end of the song.
* @param score
*/
public static trimEmptyBarsAtEnd(score: Score) {
while (score.masterBars.length > 1) {
const barIndex = score.masterBars.length - 1;
const masterBar = score.masterBars[barIndex];
if (masterBar.hasChanges) {
return;
}
for (const track of score.tracks) {
for (const staff of track.staves) {
if (barIndex < staff.bars.length) {
const bar = staff.bars[barIndex];
if (!bar.isEmpty || bar.hasChanges) {
// found a non-empty bar, stop whole cleanup
return;
}
}
}
}
// if we reach here, all found bars are empty, remove the bar
for (const track of score.tracks) {
for (const staff of track.staves) {
if (barIndex < staff.bars.length) {
const bar = staff.bars[barIndex];
staff.bars.pop();
// unlink
bar.previousBar!.nextBar = null;
}
}
}
score.masterBars.pop();
masterBar.previousMasterBar!.nextMasterBar = null;
}
}
/**
* Lists the display transpositions for some known midi instruments.
* It is a common practice to transpose the standard notation for instruments like guitars.
*/
public static readonly displayTranspositionPitches = new Map<number, number>([
// guitar
[24, -12],
[25, -12],
[26, -12],
[27, -12],
[28, -12],
[29, -12],
[30, -12],
[31, -12],
// bass
[32, -12],
[33, -12],
[34, -12],
[35, -12],
[36, -12],
[37, -12],
[38, -12],
[39, -12],
// Contrabass
[43, -12]
]);
/**
* @internal
*/
public static flooredDivision(a: number, b: number): number {
return a - b * Math.floor(a / b);
}
// NOTE: haven't figured out yet what exact formula is applied when transposing key signatures
// this table is simply created by checking the Guitar Pro behavior,
// The table is organized as [<transpose>][<key signature>] to match the table above
// it's also easier to read as we list every key signature per row, transposed by the same value
// this gives typically just a shifted list according to the transpose (with some special treatments)
/**
* Converts the key transpose table to actual key signatures.
* @param texts An array where every item indicates the number of accidentals and which accidental
* placed for the key signature.
*
* e.g. 3# is 3-sharps -> KeySignature.A
*/
private static _translateKeyTransposeTable(texts: string[][]): KeySignature[][] {
const keySignatures: KeySignature[][] = [];
for (const transpose of texts) {
const transposeValues: KeySignature[] = [];
keySignatures.push(transposeValues);
for (const keySignatureText of transpose) {
const keySignature =
// digit
(Number.parseInt(keySignatureText.charAt(0), 10) *
// b -> negative, # positive
(keySignatureText.charAt(1) === 'b' ? -1 : 1)) as KeySignature;
transposeValues.push(keySignature);
}
}
return keySignatures;
}
/**
* @internal
*/
private static readonly _keyTransposeTable: KeySignature[][] = ModelUtils._translateKeyTransposeTable([
/* Cb Gb Db Ab Eb Bb F C G D A E B F C# */
/* C 0 */ ['7b', '6b', '5b', '4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#', '6#', '7#'],
/* Db 1 */ ['2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#', '6#', '7#', '4b', '3b', '2b', '1b', '0#'],
/* D 2 */ ['3#', '4#', '7b', '6b', '5b', '4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#'],
/* Eb 3 */ ['4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#', '6#', '7#', '4b', '3b', '2b'],
/* E 4 */ ['1#', '2#', '3#', '4#', '7b', '6b', '5b', '4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#'],
/* F 5 */ ['6b', '5b', '4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#', '6#', '7#', '4b'],
/* Gb 6 */ ['1b', '0#', '1#', '2#', '3#', '4#', '7b', '6#', '7#', '4b', '3b', '2b', '1b', '0#', '1#'],
/* G 7 */ ['4#', '7b', '6b', '5b', '4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#', '6#'],
/* Ab 8 */ ['3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#', '6#', '7#', '4b', '3b', '2b', '1b'],
/* A 9 */ ['2#', '3#', '4#', '7b', '6b', '5b', '4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#'],
/* Bb 10 */ ['5b', '4b', '3b', '2b', '1b', '0#', '1#', '2#', '3#', '4#', '5#', '6#', '7#', '4b', '3b'],
/* B 11 */ ['0#', '1#', '2#', '3#', '4#', '7b', '6b', '6#', '4b', '3b', '2b', '1b', '0#', '1#', '2#']
]);
/**
* Transposes the given key signature.
* @internal
* @param keySignature The key signature to transpose
* @param transpose The number of semitones to transpose (+/- 0-11)
* @returns
*/
public static transposeKey(keySignature: KeySignature, transpose: number): KeySignature {
if (transpose === 0) {
return keySignature;
}
if (transpose < 0) {
const lookup = ModelUtils._keyTransposeTable[-transpose];
const keySignatureIndex = lookup.indexOf(keySignature);
if (keySignatureIndex === -1) {
return keySignature;
}
return (keySignatureIndex - 7) as KeySignature;
} else {
return ModelUtils._keyTransposeTable[transpose][keySignature + 7];
}
}
/**
* @internal
*/
public static toArticulationId(plain: string): string {
return plain.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
}
public static minBoundingBox(a: number, b: number) {
if (Number.isNaN(a)) {
return b;
} else if (Number.isNaN(b)) {
return a;
}
return a < b ? a : b;
}
public static maxBoundingBox(a: number, b: number) {
if (Number.isNaN(a)) {
return b;
} else if (Number.isNaN(b)) {
return a;
}
return a > b ? a : b;
}
public static getSystemLayout(score: Score, systemIndex: number, displayedTracks: Track[]) {
let defaultSystemsLayout: number;
let systemsLayout: number[];
if (displayedTracks.length === 1) {
defaultSystemsLayout = displayedTracks[0].defaultSystemsLayout;
systemsLayout = displayedTracks[0].systemsLayout;
} else {
// multi track applies
defaultSystemsLayout = score.defaultSystemsLayout;
systemsLayout = score.systemsLayout;
}
return systemIndex < systemsLayout.length ? systemsLayout[systemIndex] : defaultSystemsLayout;
}
// diatonic accidentals
private static readonly _degreeSemitones: number[] = [0, 2, 4, 5, 7, 9, 11];
private static readonly _sharpPreferredSpellings: SpellingBase[] = [
{ degree: 0, accidentalOffset: 0 }, // C
{ degree: 0, accidentalOffset: 1 }, // C#
{ degree: 1, accidentalOffset: 0 }, // D
{ degree: 1, accidentalOffset: 1 }, // D#
{ degree: 2, accidentalOffset: 0 }, // E
{ degree: 3, accidentalOffset: 0 }, // F
{ degree: 3, accidentalOffset: 1 }, // F#
{ degree: 4, accidentalOffset: 0 }, // G
{ degree: 4, accidentalOffset: 1 }, // G#
{ degree: 5, accidentalOffset: 0 }, // A
{ degree: 5, accidentalOffset: 1 }, // A#
{ degree: 6, accidentalOffset: 0 } // B
];
private static readonly _flatPreferredSpellings: SpellingBase[] = [
{ degree: 0, accidentalOffset: 0 }, // C
{ degree: 1, accidentalOffset: -1 }, // Db
{ degree: 1, accidentalOffset: 0 }, // D
{ degree: 2, accidentalOffset: -1 }, // Eb
{ degree: 2, accidentalOffset: 0 }, // E
{ degree: 3, accidentalOffset: 0 }, // F
{ degree: 4, accidentalOffset: -1 }, // Gb
{ degree: 4, accidentalOffset: 0 }, // G
{ degree: 5, accidentalOffset: -1 }, // Ab
{ degree: 5, accidentalOffset: 0 }, // A
{ degree: 6, accidentalOffset: -1 }, // Bb
{ degree: 6, accidentalOffset: 0 } // B
];
// 12 chromatic pitch classes with always 3 possible spellings in the
// accidental range of bb..##
private static readonly _spellingCandidates: SpellingBase[][] = [
// 0: C
[
{ degree: 0, accidentalOffset: 0 }, // C
{ degree: 1, accidentalOffset: -2 }, // Dbb
{ degree: 6, accidentalOffset: 1 } // B#
],
// 1: C#/Db
[
{ degree: 0, accidentalOffset: 1 }, // C#
{ degree: 1, accidentalOffset: -1 }, // Db
{ degree: 6, accidentalOffset: 2 } // B##
],
// 2: D
[
{ degree: 1, accidentalOffset: 0 }, // D
{ degree: 0, accidentalOffset: 2 }, // C##
{ degree: 2, accidentalOffset: -2 } // Ebb
],
// 3: D#/Eb
[
{ degree: 1, accidentalOffset: 1 }, // D#
{ degree: 2, accidentalOffset: -1 }, // Eb
{ degree: 3, accidentalOffset: -2 } // Fbb
],
// 4: E
[
{ degree: 2, accidentalOffset: 0 }, // E
{ degree: 1, accidentalOffset: 2 }, // D##
{ degree: 3, accidentalOffset: -1 } // Fb
],
// 5: F
[
{ degree: 3, accidentalOffset: 0 }, // F
{ degree: 2, accidentalOffset: 1 }, // E#
{ degree: 4, accidentalOffset: -2 } // Gbb
],
// 6: F#/Gb
[
{ degree: 3, accidentalOffset: 1 }, // F#
{ degree: 4, accidentalOffset: -1 }, // Gb
{ degree: 2, accidentalOffset: 2 } // E##
],
// 7: G
[
{ degree: 4, accidentalOffset: 0 }, // G
{ degree: 3, accidentalOffset: 2 }, // F##
{ degree: 5, accidentalOffset: -2 } // Abb
],
// 8: G#/Ab
[
{ degree: 4, accidentalOffset: 1 }, // G#
{ degree: 5, accidentalOffset: -1 } // Ab
],
// 9: A
[
{ degree: 5, accidentalOffset: 0 }, // A
{ degree: 4, accidentalOffset: 2 }, // G##
{ degree: 6, accidentalOffset: -2 } // Bbb
],
// 10: A#/Bb
[
{ degree: 5, accidentalOffset: 1 }, // A#
{ degree: 6, accidentalOffset: -1 }, // Bb
{ degree: 0, accidentalOffset: -2 } // Cbb
],
// 11: B
[
{ degree: 6, accidentalOffset: 0 }, // B
{ degree: 5, accidentalOffset: 2 }, // A##
{ degree: 0, accidentalOffset: -1 } // Cb
]
];
private static readonly _sharpKeySignatureOrder: number[] = [3, 0, 4, 1, 5, 2, 6]; // F C G D A E B
private static readonly _flatKeySignatureOrder: number[] = [6, 2, 5, 1, 4, 0, 3]; // B E A D G C F
private static readonly _keySignatureAccidentalByDegree: number[][] =
ModelUtils._buildKeySignatureAccidentalByDegree();
private static readonly _accidentalOffsetToType = new Map<number, AccidentalType>([
[-2, AccidentalType.DoubleFlat],
[-1, AccidentalType.Flat],
[0, AccidentalType.Natural],
[1, AccidentalType.Sharp],
[2, AccidentalType.DoubleSharp]
]);
private static readonly _forcedAccidentalOffsetByMode = new Map<NoteAccidentalMode, number>([
[NoteAccidentalMode.ForceSharp, 1],
[NoteAccidentalMode.ForceDoubleSharp, 2],
[NoteAccidentalMode.ForceFlat, -1],
[NoteAccidentalMode.ForceDoubleFlat, -2],
[NoteAccidentalMode.ForceNatural, 0],
[NoteAccidentalMode.ForceNone, 0],
[NoteAccidentalMode.Default, Number.NaN]
]);
private static _buildKeySignatureAccidentalByDegree(): number[][] {
const lookup: number[][] = [];
for (let ks = -7; ks <= 7; ks++) {
const row = [0, 0, 0, 0, 0, 0, 0];
if (ks > 0) {
for (let i = 0; i < ks; i++) {
row[ModelUtils._sharpKeySignatureOrder[i]] = 1;
}
} else if (ks < 0) {
for (let i = 0; i < -ks; i++) {
row[ModelUtils._flatKeySignatureOrder[i]] = -1;
}
}
lookup.push(row);
}
return lookup;
}
public static getKeySignatureAccidentalOffset(keySignature: KeySignature, degree: number): number {
return ModelUtils._keySignatureAccidentalByDegree[(keySignature as number) + 7][degree];