-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathAlphaTex1LanguageHandler.ts
More file actions
3423 lines (3166 loc) · 142 KB
/
Copy pathAlphaTex1LanguageHandler.ts
File metadata and controls
3423 lines (3166 loc) · 142 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
// unfortunately the "old" alphaTex syntax had no strict delimiters
// for arguments and properties. That's why we need to parse the properties exactly
// as needed for the identifiers. In an alphaTex2 we should make this parsing simpler.
// the parser should not need to do that semantic checks, that's the importers job
// but we emit "Hint" diagnostics for now.
import { AlphaTex1EnumMappings } from '@coderline/alphatab/importer/alphaTex/AlphaTex1EnumMappings';
import {
AlphaTex1LanguageDefinitions,
type AlphaTexSignatureDefinition
} from '@coderline/alphatab/importer/alphaTex/AlphaTex1LanguageDefinitions';
import {
AlphaTex1MetaDataReader,
type SignatureResolutionInfo
} from '@coderline/alphatab/importer/alphaTex/AlphaTex1MetaDataReader';
import {
type AlphaTexArgumentList,
type AlphaTexAstNode,
type AlphaTexIdentifier,
type AlphaTexMetaDataNode,
AlphaTexNodeType,
type AlphaTexNumberLiteral,
type AlphaTexPropertyNode,
type AlphaTexStringLiteral,
type AlphaTexTextNode,
type IAlphaTexArgumentValue,
type IAlphaTexAstNode
} from '@coderline/alphatab/importer/alphaTex/AlphaTexAst';
import { AlphaTexParseMode } from '@coderline/alphatab/importer/alphaTex/AlphaTexParser';
import {
AlphaTexDiagnosticCode,
AlphaTexDiagnosticsSeverity,
AlphaTexStaffNoteKind,
ArgumentListParseTypesMode,
type IAlphaTexImporter
} from '@coderline/alphatab/importer/alphaTex/AlphaTexShared';
import { Atnf } from '@coderline/alphatab/importer/alphaTex/ATNF';
import {
ApplyNodeResult,
ApplyStructuralMetaDataResult,
type IAlphaTexLanguageImportHandler
} from '@coderline/alphatab/importer/alphaTex/IAlphaTexLanguageImportHandler';
import { GeneralMidi } from '@coderline/alphatab/midi/GeneralMidi';
import { AccentuationType } from '@coderline/alphatab/model/AccentuationType';
import { Automation, AutomationType, type FlatSyncPoint } from '@coderline/alphatab/model/Automation';
import { type Bar, BarLineStyle, SustainPedalMarker, SustainPedalMarkerType } from '@coderline/alphatab/model/Bar';
import { BarreShape } from '@coderline/alphatab/model/BarreShape';
import { type Beat, BeatBeamingMode } from '@coderline/alphatab/model/Beat';
import { BendPoint } from '@coderline/alphatab/model/BendPoint';
import { BendStyle } from '@coderline/alphatab/model/BendStyle';
import { BrushType } from '@coderline/alphatab/model/BrushType';
import { Chord } from '@coderline/alphatab/model/Chord';
import { Clef } from '@coderline/alphatab/model/Clef';
import { Color } from '@coderline/alphatab/model/Color';
import { CrescendoType } from '@coderline/alphatab/model/CrescendoType';
import { Duration } from '@coderline/alphatab/model/Duration';
import { FadeType } from '@coderline/alphatab/model/FadeType';
import { Fermata } from '@coderline/alphatab/model/Fermata';
import { Fingers } from '@coderline/alphatab/model/Fingers';
import { GolpeType } from '@coderline/alphatab/model/GolpeType';
import { GraceType } from '@coderline/alphatab/model/GraceType';
import { HarmonicType } from '@coderline/alphatab/model/HarmonicType';
import { KeySignatureType } from '@coderline/alphatab/model/KeySignatureType';
import { Lyrics } from '@coderline/alphatab/model/Lyrics';
import type { MasterBar } from '@coderline/alphatab/model/MasterBar';
import { ModelUtils } from '@coderline/alphatab/model/ModelUtils';
import type { Note } from '@coderline/alphatab/model/Note';
import { NoteAccidentalMode } from '@coderline/alphatab/model/NoteAccidentalMode';
import { NoteOrnament } from '@coderline/alphatab/model/NoteOrnament';
import { Ottavia } from '@coderline/alphatab/model/Ottavia';
import { PercussionMapper } from '@coderline/alphatab/model/PercussionMapper';
import { PickStroke } from '@coderline/alphatab/model/PickStroke';
import type { RenderStylesheet } from '@coderline/alphatab/model/RenderStylesheet';
import { HeaderFooterStyle, Score, ScoreStyle, ScoreSubElement } from '@coderline/alphatab/model/Score';
import { Section } from '@coderline/alphatab/model/Section';
import { SimileMark } from '@coderline/alphatab/model/SimileMark';
import { SlideInType } from '@coderline/alphatab/model/SlideInType';
import { SlideOutType } from '@coderline/alphatab/model/SlideOutType';
import { Staff } from '@coderline/alphatab/model/Staff';
import { Track } from '@coderline/alphatab/model/Track';
import { TripletFeel } from '@coderline/alphatab/model/TripletFeel';
import { Tuning } from '@coderline/alphatab/model/Tuning';
import { VibratoType } from '@coderline/alphatab/model/VibratoType';
import { WahPedal } from '@coderline/alphatab/model/WahPedal';
import { BeamDirection } from '@coderline/alphatab/rendering/_barrel';
import { SynthConstants } from '@coderline/alphatab/synth/SynthConstants';
/**
* @internal
*/
export class AlphaTex1LanguageHandler implements IAlphaTexLanguageImportHandler {
public static readonly instance = new AlphaTex1LanguageHandler();
public applyScoreMetaData(
importer: IAlphaTexImporter,
score: Score,
metaData: AlphaTexMetaDataNode
): ApplyNodeResult {
const result = this._checkArgumentTypes(
importer,
[AlphaTex1LanguageDefinitions.scoreMetaDataSignatures],
metaData,
metaData.tag.tag.text.toLowerCase(),
metaData.arguments
);
if (result !== undefined) {
return result!;
}
switch (metaData.tag.tag.text.toLowerCase()) {
case 'title':
score.title = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.Title, metaData);
return ApplyNodeResult.Applied;
case 'subtitle':
score.subTitle = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.SubTitle, metaData);
return ApplyNodeResult.Applied;
case 'artist':
score.artist = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.Artist, metaData);
return ApplyNodeResult.Applied;
case 'album':
score.album = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.Album, metaData);
return ApplyNodeResult.Applied;
case 'words':
score.words = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.Words, metaData);
return ApplyNodeResult.Applied;
case 'music':
score.music = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.Music, metaData);
return ApplyNodeResult.Applied;
case 'copyright':
score.copyright = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.Copyright, metaData);
return ApplyNodeResult.Applied;
case 'instructions':
score.instructions = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
return ApplyNodeResult.Applied;
case 'notices':
score.notices = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
return ApplyNodeResult.Applied;
case 'tab':
score.tab = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
this._headerFooterStyle(importer, score, ScoreSubElement.Transcriber, metaData);
return ApplyNodeResult.Applied;
case 'copyright2':
this._headerFooterStyle(importer, score, ScoreSubElement.CopyrightSecondLine, metaData, 0);
return ApplyNodeResult.Applied;
case 'wordsandmusic':
this._headerFooterStyle(importer, score, ScoreSubElement.WordsAndMusic, metaData, 0);
return ApplyNodeResult.Applied;
case 'defaultsystemslayout':
score.defaultSystemsLayout = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
return ApplyNodeResult.Applied;
case 'systemslayout':
for (const v of metaData.arguments!.arguments) {
score.systemsLayout.push((v as AlphaTexNumberLiteral).value);
}
return ApplyNodeResult.Applied;
case 'hidedynamics':
score.stylesheet.hideDynamics = true;
return ApplyNodeResult.Applied;
case 'showdynamics':
score.stylesheet.hideDynamics = false;
return ApplyNodeResult.Applied;
case 'extendbarlines':
score.stylesheet.extendBarLines = true;
return ApplyNodeResult.Applied;
case 'bracketextendmode':
const bracketExtendMode = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'bracket extend mode',
AlphaTex1EnumMappings.bracketExtendMode
);
if (bracketExtendMode === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
score.stylesheet.bracketExtendMode = bracketExtendMode!;
return ApplyNodeResult.Applied;
case 'usesystemsignseparator':
score.stylesheet.useSystemSignSeparator = true;
return ApplyNodeResult.Applied;
case 'multibarrest':
score.stylesheet.multiTrackMultiBarRest = true;
return ApplyNodeResult.Applied;
case 'singletracktracknamepolicy':
const singleTrackTrackNamePolicy = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'track name policy',
AlphaTex1EnumMappings.trackNamePolicy
);
if (singleTrackTrackNamePolicy === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
score.stylesheet.singleTrackTrackNamePolicy = singleTrackTrackNamePolicy!;
return ApplyNodeResult.Applied;
case 'multitracktracknamepolicy':
const multiTrackTrackNamePolicy = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'track name policy',
AlphaTex1EnumMappings.trackNamePolicy
);
if (multiTrackTrackNamePolicy === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
score.stylesheet.multiTrackTrackNamePolicy = multiTrackTrackNamePolicy!;
return ApplyNodeResult.Applied;
case 'firstsystemtracknamemode':
const firstSystemTrackNameMode = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'track name mode',
AlphaTex1EnumMappings.trackNameMode
);
if (firstSystemTrackNameMode === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
score.stylesheet.firstSystemTrackNameMode = firstSystemTrackNameMode!;
return ApplyNodeResult.Applied;
case 'othersystemstracknamemode':
const otherSystemsTrackNameMode = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'track name mode',
AlphaTex1EnumMappings.trackNameMode
);
if (otherSystemsTrackNameMode === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
score.stylesheet.otherSystemsTrackNameMode = otherSystemsTrackNameMode!;
return ApplyNodeResult.Applied;
case 'firstsystemtracknameorientation':
const firstSystemTrackNameOrientation = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'track name orientation',
AlphaTex1EnumMappings.trackNameOrientation
);
if (firstSystemTrackNameOrientation === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
score.stylesheet.firstSystemTrackNameOrientation = firstSystemTrackNameOrientation!;
return ApplyNodeResult.Applied;
case 'othersystemstracknameorientation':
const otherSystemsTrackNameOrientation = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'track name orientation',
AlphaTex1EnumMappings.trackNameOrientation
);
if (otherSystemsTrackNameOrientation === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
score.stylesheet.otherSystemsTrackNameOrientation = otherSystemsTrackNameOrientation!;
return ApplyNodeResult.Applied;
default:
return ApplyNodeResult.NotAppliedUnrecognizedMarker;
}
}
private _checkArgumentTypes(
importer: IAlphaTexImporter,
lookupList: Map<string, AlphaTexSignatureDefinition[] | null>[],
parent: AlphaTexAstNode,
tag: string,
args: AlphaTexArgumentList | undefined
): ApplyNodeResult | undefined {
const lookup = lookupList.find(l => l.has(tag));
if (!lookup) {
return ApplyNodeResult.NotAppliedUnrecognizedMarker;
}
const types = lookup.get(tag);
if (!types) {
if (args && args.arguments.length > 0) {
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT300,
message: `Expected no arguments, but found some.`,
start: args.start,
end: args.end,
severity: AlphaTexDiagnosticsSeverity.Warning
});
}
return undefined;
}
if (!this._validateArgumentTypes(importer, types, parent, args)) {
return ApplyNodeResult.NotAppliedSemanticError;
}
return undefined;
}
public applyStaffMetaData(
importer: IAlphaTexImporter,
staff: Staff,
metaData: AlphaTexMetaDataNode
): ApplyNodeResult {
const result = this._checkArgumentTypes(
importer,
[AlphaTex1LanguageDefinitions.staffMetaDataSignatures],
metaData,
metaData.tag.tag.text.toLowerCase(),
metaData.arguments
);
if (result !== undefined) {
return result!;
}
switch (metaData.tag.tag.text.toLowerCase()) {
case 'capo':
staff.capo = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
return ApplyNodeResult.Applied;
case 'tuning':
const tuning: number[] = [];
let hideTuning = false;
let tuningName = '';
for (let i = 0; i < metaData.arguments!.arguments.length; i++) {
const v = metaData.arguments!.arguments[i];
const text = (v as AlphaTexTextNode).text;
switch (text) {
case 'piano':
case 'none':
case 'voice':
importer.applyStaffNoteKind(staff, AlphaTexStaffNoteKind.Pitched);
i = metaData.arguments!.arguments.length;
break;
// backwards compatibility only
case 'hide':
hideTuning = true;
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT305,
message: `This value should be rather specified via the properties.`,
start: v.start,
end: v.end,
severity: AlphaTexDiagnosticsSeverity.Warning
});
break;
default:
const t = ModelUtils.parseTuning(text);
if (t) {
tuning.push(t.realValue);
} else if (i === metaData.arguments!.arguments.length - 1 && tuning.length > 0) {
tuningName = text;
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT305,
message: `This value should be rather specified via the properties.`,
start: v.start,
end: v.end,
severity: AlphaTexDiagnosticsSeverity.Warning
});
} else {
const tuningLetters = Array.from(ModelUtils.tuningLetters).join(',');
const accidentalModes = Array.from(ModelUtils.accidentalModeMapping.keys()).join(',');
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT209,
message: `Unexpected tuning value '${text}', expected: <note><accidental><octave> where <note>=oneOf(${tuningLetters}) <accidental>=oneOf(${accidentalModes}), <octave>=number`,
start: v.start,
end: v.end,
severity: AlphaTexDiagnosticsSeverity.Error
});
}
break;
}
}
importer.state.staffHasExplicitTuning.add(staff);
importer.state.staffTuningApplied.delete(staff);
staff.stringTuning = new Tuning();
staff.stringTuning.tunings = tuning;
staff.stringTuning.name = tuningName;
this._tuningProperties(importer, staff, staff.stringTuning, metaData);
if (hideTuning) {
if (!staff.track.score.stylesheet.perTrackDisplayTuning) {
staff.track.score.stylesheet.perTrackDisplayTuning = new Map<number, boolean>();
}
staff.track.score.stylesheet.perTrackDisplayTuning!.set(staff.track.index, false);
}
return ApplyNodeResult.Applied;
case 'instrument':
importer.state.staffTuningApplied.delete(staff);
this._readTrackInstrument(importer, staff.track, metaData.arguments!);
return ApplyNodeResult.Applied;
case 'bank':
staff.track.playbackInfo.bank = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
return ApplyNodeResult.Applied;
case 'lyrics':
const lyrics: Lyrics = new Lyrics();
lyrics.startBar = 0;
lyrics.text = '';
if (metaData.arguments!.arguments.length === 2) {
lyrics.startBar = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
lyrics.text = (metaData.arguments!.arguments[1] as AlphaTexTextNode).text;
} else {
lyrics.text = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
}
importer.state.lyrics.get(staff.track.index)!.push(lyrics);
return ApplyNodeResult.Applied;
case 'chord':
const chord = new Chord();
this._chordProperties(importer, chord, metaData);
chord.name = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
for (let i = 1; i < metaData.arguments!.arguments.length; i++) {
const v = metaData.arguments!.arguments[i];
if (v.nodeType === AlphaTexNodeType.Number) {
chord.strings.push((v as AlphaTexNumberLiteral).value);
} else if (v.nodeType === AlphaTexNodeType.Ident) {
const txt = (v as AlphaTexIdentifier).text;
if (txt === 'x') {
chord.strings.push(-1);
} else {
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT209,
message: `Unexpected chord value '${txt}', expected: 'x'`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: v.start,
end: v.end
});
}
}
}
staff.addChord(AlphaTex1LanguageHandler._getChordId(staff, chord.name), chord);
return ApplyNodeResult.Applied;
case 'articulation':
const percussionArticulationNames = importer.state.percussionArticulationNames;
const articulationName = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
if (articulationName === 'defaults') {
for (const [defaultName, defaultValue] of PercussionMapper.instrumentArticulationNames) {
percussionArticulationNames.set(defaultName.toLowerCase(), defaultValue);
percussionArticulationNames.set(ModelUtils.toArticulationId(defaultName), defaultValue);
}
return ApplyNodeResult.Applied;
}
if (metaData.arguments!.arguments.length === 2) {
const number = (metaData.arguments!.arguments[1] as AlphaTexNumberLiteral).value;
if (PercussionMapper.instrumentArticulations.has(number)) {
percussionArticulationNames.set(articulationName.toLowerCase(), number);
return ApplyNodeResult.Applied;
} else {
const articulations = Array.from(PercussionMapper.instrumentArticulations.keys())
.map(n => `${n}`)
.join(',');
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT209,
message: `Unexpected articulation value '${number}', expected: ${articulations}`,
start: metaData.arguments!.arguments[1].start,
end: metaData.arguments!.arguments[1].end,
severity: AlphaTexDiagnosticsSeverity.Error
});
return ApplyNodeResult.NotAppliedSemanticError;
}
}
return ApplyNodeResult.Applied;
case 'accidentals':
return AlphaTex1LanguageHandler._handleAccidentalMode(importer, metaData.arguments!);
case 'displaytranspose':
staff.displayTranspositionPitch =
(metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value * -1;
importer.state.staffHasExplicitDisplayTransposition.add(staff);
return ApplyNodeResult.Applied;
case 'transpose':
staff.transpositionPitch = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value * -1;
return ApplyNodeResult.Applied;
default:
return ApplyNodeResult.NotAppliedUnrecognizedMarker;
}
}
public applyBarMetaData(importer: IAlphaTexImporter, bar: Bar, metaData: AlphaTexMetaDataNode): ApplyNodeResult {
const result = this._checkArgumentTypes(
importer,
[AlphaTex1LanguageDefinitions.barMetaDataSignatures],
metaData,
metaData.tag.tag.text.toLowerCase(),
metaData.arguments
);
if (result !== undefined) {
return result!;
}
switch (metaData.tag.tag.text.toLowerCase()) {
case 'sync':
const syncPoint = this._buildSyncPoint(metaData);
importer.state.syncPoints.push(syncPoint);
return ApplyNodeResult.Applied;
case 'tempo':
let ti = 0;
const tempo = (metaData.arguments!.arguments[ti++] as AlphaTexNumberLiteral).value;
let tempoLabel = '';
let isVisible = true;
let ratioPosition = 0;
while (ti < metaData.arguments!.arguments.length) {
switch (metaData.arguments!.arguments[ti].nodeType) {
case AlphaTexNodeType.Ident:
case AlphaTexNodeType.String:
const txt = (metaData.arguments!.arguments[ti] as AlphaTexTextNode).text;
if (txt === 'hide') {
isVisible = false;
} else {
tempoLabel = txt;
}
break;
case AlphaTexNodeType.Number:
ratioPosition = (metaData.arguments!.arguments[ti] as AlphaTexNumberLiteral).value;
break;
}
ti++;
}
let tempoAutomation = bar.masterBar.tempoAutomations.find(a => a.ratioPosition === ratioPosition);
if (!tempoAutomation) {
tempoAutomation = new Automation();
bar.masterBar.tempoAutomations.push(tempoAutomation);
}
tempoAutomation.isLinear = false;
tempoAutomation.type = AutomationType.Tempo;
tempoAutomation.value = tempo;
tempoAutomation.text = tempoLabel;
tempoAutomation.ratioPosition = ratioPosition;
tempoAutomation.isVisible = isVisible;
return ApplyNodeResult.Applied;
case 'rc':
bar.masterBar.repeatCount = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
return ApplyNodeResult.Applied;
case 'ae':
for (const e of metaData.arguments!.arguments) {
if (e.nodeType === AlphaTexNodeType.Number) {
const num = (e as AlphaTexNumberLiteral).value;
if (num < 1 || num > 31) {
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT211,
message: `Value is out of valid range. Allowed range: %s, Actual Value: %s`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: e.start,
end: e.end
});
return ApplyNodeResult.NotAppliedSemanticError;
} else {
// Alternate endings bitflag starts from 0
bar.masterBar.alternateEndings |= 1 << (num - 1);
}
} else {
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT202,
message: `Unexpected '${AlphaTexNodeType[e.nodeType]}' token. Expected one of following: ${AlphaTexNodeType[AlphaTexNodeType.Number]}`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: e.start,
end: e.end
});
}
}
return ApplyNodeResult.Applied;
case 'ts':
switch (metaData.arguments!.arguments[0].nodeType) {
case AlphaTexNodeType.Number:
bar.masterBar.timeSignatureNumerator = (
metaData.arguments!.arguments[0] as AlphaTexNumberLiteral
).value;
bar.masterBar.timeSignatureDenominator = (
metaData.arguments!.arguments[1] as AlphaTexNumberLiteral
).value;
break;
case AlphaTexNodeType.Ident:
case AlphaTexNodeType.String:
const tsValue = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
if (tsValue.toLowerCase() === 'common') {
bar.masterBar.timeSignatureCommon = true;
bar.masterBar.timeSignatureNumerator = 4;
bar.masterBar.timeSignatureDenominator = 4;
} else {
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT209,
message: `Unexpected time signature value '${tsValue}', expected: common or two numbers`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: metaData.arguments!.arguments[0].start,
end: metaData.arguments!.arguments[0].end
});
return ApplyNodeResult.NotAppliedSemanticError;
}
break;
}
return ApplyNodeResult.Applied;
case 'ks':
const keySignature = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'key signature',
AlphaTex1EnumMappings.keySignature
);
if (keySignature === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
const keySignatureType = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'key signature type',
AlphaTex1EnumMappings.keySignatureType
);
if (keySignatureType === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.keySignature = keySignature!;
bar.keySignatureType = keySignatureType!;
return ApplyNodeResult.Applied;
case 'clef':
switch (metaData.arguments!.arguments[0].nodeType) {
case AlphaTexNodeType.Ident:
case AlphaTexNodeType.String:
const clef = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'clef',
AlphaTex1EnumMappings.clef
);
if (clef === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.clef = clef!;
break;
case AlphaTexNodeType.Number:
const clefValue = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
switch (clefValue) {
case 0:
bar.clef = Clef.Neutral;
break;
case 43:
bar.clef = Clef.G2;
break;
case 65:
bar.clef = Clef.F4;
break;
case 48:
bar.clef = Clef.C3;
break;
case 60:
bar.clef = Clef.C4;
break;
default:
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT209,
message: `Unexpected clef value '${clefValue}', expected: ${Array.from(AlphaTex1EnumMappings.clef.keys()).join(',')}`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: metaData.arguments!.arguments[0].start,
end: metaData.arguments!.arguments[0].end
});
return ApplyNodeResult.NotAppliedSemanticError;
}
break;
}
return ApplyNodeResult.Applied;
case 'section':
const section = new Section();
if (metaData.arguments!.arguments.length === 1) {
section.text = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
} else {
section.marker = (metaData.arguments!.arguments[0] as AlphaTexTextNode).text;
section.text = (metaData.arguments!.arguments[1] as AlphaTexTextNode).text;
}
bar.masterBar.section = section;
return ApplyNodeResult.Applied;
case 'tf':
switch (metaData.arguments!.arguments[0].nodeType) {
case AlphaTexNodeType.Ident:
case AlphaTexNodeType.String:
const tripletFeel = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'triplet feel',
AlphaTex1EnumMappings.tripletFeel
);
if (tripletFeel === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.masterBar.tripletFeel = tripletFeel!;
break;
case AlphaTexNodeType.Number:
const tripletFeelValue = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
switch (tripletFeelValue) {
case 0:
bar.masterBar.tripletFeel = TripletFeel.NoTripletFeel;
break;
case 1:
bar.masterBar.tripletFeel = TripletFeel.Triplet16th;
break;
case 2:
bar.masterBar.tripletFeel = TripletFeel.Triplet8th;
break;
case 3:
bar.masterBar.tripletFeel = TripletFeel.Dotted16th;
break;
case 4:
bar.masterBar.tripletFeel = TripletFeel.Dotted8th;
break;
case 5:
bar.masterBar.tripletFeel = TripletFeel.Scottish16th;
break;
case 6:
bar.masterBar.tripletFeel = TripletFeel.Scottish8th;
break;
default:
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT209,
message: `Unexpected triplet feel value '${tripletFeelValue}', expected: ${Array.from(AlphaTex1EnumMappings.tripletFeel.keys()).join(',')}`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: metaData.arguments!.arguments[0].start,
end: metaData.arguments!.arguments[0].end
});
return ApplyNodeResult.NotAppliedSemanticError;
}
break;
}
return ApplyNodeResult.Applied;
case 'barlineleft':
const barLineLeft = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'bar line',
AlphaTex1EnumMappings.barLineStyle
);
if (barLineLeft === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.barLineLeft = barLineLeft!;
return ApplyNodeResult.Applied;
case 'barlineright':
const barLineRight = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'bar line',
AlphaTex1EnumMappings.barLineStyle
);
if (barLineRight === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.barLineRight = barLineRight!;
return ApplyNodeResult.Applied;
case 'accidentals':
return AlphaTex1LanguageHandler._handleAccidentalMode(importer, metaData.arguments!);
case 'voicemode':
return AlphaTex1LanguageHandler._handleVoiceMode(importer, metaData.arguments!);
case 'jump':
const direction = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'direction',
AlphaTex1EnumMappings.direction
);
if (direction === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.masterBar.addDirection(direction!);
return ApplyNodeResult.Applied;
case 'ottava':
const ottava = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'clef ottava',
AlphaTex1EnumMappings.ottavia
);
if (ottava === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.clefOttava = ottava!;
return ApplyNodeResult.Applied;
case 'simile':
const simile = AlphaTex1LanguageHandler._parseEnumValue(
importer,
metaData.arguments!,
'simile mark',
AlphaTex1EnumMappings.simileMark
);
if (simile === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
bar.simileMark = simile!;
return ApplyNodeResult.Applied;
case 'width':
bar.masterBar.displayWidth = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
bar.displayWidth = bar.masterBar.displayWidth;
return ApplyNodeResult.Applied;
case 'scale':
bar.masterBar.displayScale = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
bar.displayScale = bar.masterBar.displayScale;
return ApplyNodeResult.Applied;
case 'spd':
const sustainPedalDown = new SustainPedalMarker();
sustainPedalDown.pedalType = SustainPedalMarkerType.Down;
sustainPedalDown.ratioPosition = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
bar.sustainPedals.push(sustainPedalDown);
return ApplyNodeResult.Applied;
case 'spu':
const sustainPedalUp = new SustainPedalMarker();
sustainPedalUp.pedalType = SustainPedalMarkerType.Up;
sustainPedalUp.ratioPosition = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
bar.sustainPedals.push(sustainPedalUp);
return ApplyNodeResult.Applied;
case 'sph':
const sustainPedalHold = new SustainPedalMarker();
sustainPedalHold.pedalType = SustainPedalMarkerType.Hold;
sustainPedalHold.ratioPosition = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
bar.sustainPedals.push(sustainPedalHold);
return ApplyNodeResult.Applied;
case 'ft':
bar.masterBar.isFreeTime = true;
return ApplyNodeResult.Applied;
case 'ro':
bar.masterBar.isRepeatStart = true;
return ApplyNodeResult.Applied;
case 'ac':
bar.masterBar.isAnacrusis = true;
return ApplyNodeResult.Applied;
case 'db':
bar.masterBar.isDoubleBar = true;
bar.barLineRight = BarLineStyle.LightLight;
return ApplyNodeResult.Applied;
default:
return ApplyNodeResult.NotAppliedUnrecognizedMarker;
}
}
private static _handleAccidentalMode(importer: IAlphaTexImporter, args: AlphaTexArgumentList): ApplyNodeResult {
const accidentalMode = AlphaTex1LanguageHandler._parseEnumValue(
importer,
args,
'accidental mode',
AlphaTex1EnumMappings.alphaTexAccidentalMode
);
if (accidentalMode === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
importer.state.accidentalMode = accidentalMode!;
return ApplyNodeResult.Applied;
}
private static _handleVoiceMode(importer: IAlphaTexImporter, args: AlphaTexArgumentList): ApplyNodeResult {
const voiceMode = AlphaTex1LanguageHandler._parseEnumValue(
importer,
args,
'voice mode',
AlphaTex1EnumMappings.alphaTexVoiceMode
);
if (voiceMode === undefined) {
return ApplyNodeResult.NotAppliedSemanticError;
}
importer.state.voiceMode = voiceMode!;
return ApplyNodeResult.Applied;
}
private static _getChordId(currentStaff: Staff, chordName: string): string {
return chordName.toLowerCase() + currentStaff.index + currentStaff.track.index;
}
private _buildSyncPoint(metaData: AlphaTexMetaDataNode): FlatSyncPoint {
const barIndex = (metaData.arguments!.arguments[0] as AlphaTexNumberLiteral).value;
const barOccurence = (metaData.arguments!.arguments[1] as AlphaTexNumberLiteral).value;
const millisecondOffset = (metaData.arguments!.arguments[2] as AlphaTexNumberLiteral).value;
let barPosition = 0;
if (metaData.arguments!.arguments.length > 3) {
barPosition = (metaData.arguments!.arguments[3] as AlphaTexNumberLiteral).value;
}
return {
barIndex,
barOccurence,
barPosition,
millisecondOffset
};
}
private _validateArgumentTypes(
importer: IAlphaTexImporter,
signatures: AlphaTexSignatureDefinition[],
parent: AlphaTexAstNode,
args: AlphaTexArgumentList | undefined
) {
if (!args) {
const hasEmptyParameterOverload = signatures.some(
c =>
c.parameters.length === 0 ||
!c.parameters.some(
v =>
v.parseMode === ArgumentListParseTypesMode.Required ||
v.parseMode === ArgumentListParseTypesMode.RequiredAsFloat ||
v.parseMode === ArgumentListParseTypesMode.RequiredAsValueList
)
);
if (hasEmptyParameterOverload) {
return true;
}
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT219,
message: `Error parsing arguments: no overload matched arguments ${AlphaTex1MetaDataReader.generateSignaturesFromArguments(undefined)}. Signatures: ${AlphaTex1MetaDataReader.generateSignatures(signatures)}`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: parent.start,
end: parent.end
});
return false;
}
if (args.validated) {
return true;
}
let error = false;
const candidates = new Map<number, SignatureResolutionInfo>(
signatures.map((v, i) => [
i,
{
signature: v,
parameterIndex: 0,
parameterValueMatches: 0,
parameterHasValues: false
} as SignatureResolutionInfo
])
);
const parseFull = importer.parseMode === AlphaTexParseMode.Full;
const trackValue = parseFull
? (value: IAlphaTexAstNode, overloadIndex: number) => {
const overload = candidates.get(overloadIndex)!;
const valueNode = value as IAlphaTexArgumentValue;
if (!valueNode.parameterIndices) {
valueNode.parameterIndices = new Map<number, number>();
}
valueNode.parameterIndices.set(overloadIndex, overload.parameterIndex);
}
: (_value: IAlphaTexAstNode, _overloadIndex: number) => {};
for (const value of args.arguments) {
AlphaTex1MetaDataReader.filterSignatureCandidates(candidates, value, false, trackValue);
if (candidates.size === 0) {
break;
}
}
const allCandidates = parseFull ? Array.from(candidates.entries()) : undefined;
AlphaTex1MetaDataReader.filterIncompleteCandidates(candidates);
if (candidates.size === 0) {
importer.addSemanticDiagnostic({
code: AlphaTexDiagnosticCode.AT219,
message: `Error parsing arguments: no overload matched arguments ${AlphaTex1MetaDataReader.generateSignaturesFromArguments(args.arguments)}. Signatures:\n${AlphaTex1MetaDataReader.generateSignatures(signatures)}`,
severity: AlphaTexDiagnosticsSeverity.Error,
start: args.start,
end: args.end
});
error = true;
}
if (allCandidates) {
// sort by how well the candidate matches
AlphaTex1MetaDataReader.sortCandidates(allCandidates);
args.signatureCandidateIndices = allCandidates.map(c => c[0]);
}
return !error;
}
private _headerFooterStyle(
importer: IAlphaTexImporter,
score: Score,
element: ScoreSubElement,
metaData: AlphaTexMetaDataNode,
startIndex: number = 1
) {
const remaining = metaData.arguments!.arguments.length - startIndex;
if (remaining < 1) {
return;
}
const style = ModelUtils.getOrCreateHeaderFooterStyle(score, element);
if (style.isVisible === undefined) {
style.isVisible = true;