/
RepetitionCalculator.ts
102 lines (98 loc) · 4.88 KB
/
RepetitionCalculator.ts
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
import {SourceMeasure} from "../../VoiceData/SourceMeasure";
import {RepetitionInstruction, RepetitionInstructionEnum, AlignmentType} from "../../VoiceData/Instructions/RepetitionInstruction";
import {RepetitionInstructionComparer} from "../../VoiceData/Instructions/RepetitionInstruction";
import {ArgumentOutOfRangeException} from "../../Exceptions";
import {MusicSheet} from "../../MusicSheet";
export class RepetitionCalculator {
private musicSheet: MusicSheet;
private repetitionInstructions: RepetitionInstruction[] = [];
private currentMeasure: SourceMeasure;
private currentMeasureIndex: number;
/**
* Is called when all repetition symbols have been read from xml.
* Creates the repetition instructions and adds them to the corresponding measure.
* Creates the logical repetition objects for iteration and playback.
* @param musicSheet
* @param repetitionInstructions
*/
public calculateRepetitions(musicSheet: MusicSheet, repetitionInstructions: RepetitionInstruction[]): void {
this.musicSheet = <MusicSheet>musicSheet;
this.repetitionInstructions = repetitionInstructions;
const sourceMeasures: SourceMeasure[] = this.musicSheet.SourceMeasures;
for (let idx: number = 0, len: number = this.repetitionInstructions.length; idx < len; ++idx) {
const instruction: RepetitionInstruction = this.repetitionInstructions[idx];
this.currentMeasureIndex = instruction.measureIndex;
this.currentMeasure = sourceMeasures[this.currentMeasureIndex];
this.handleRepetitionInstructions(instruction);
}
// if there are more than one instruction at measure begin or end,
// sort them according to the nesting of the repetitions:
for (let idx: number = 0, len: number = this.musicSheet.SourceMeasures.length; idx < len; ++idx) {
const measure: SourceMeasure = this.musicSheet.SourceMeasures[idx];
if (measure.FirstRepetitionInstructions.length > 1) {
measure.FirstRepetitionInstructions.sort(RepetitionInstructionComparer.Compare);
}
if (measure.LastRepetitionInstructions.length > 1) {
measure.LastRepetitionInstructions.sort(RepetitionInstructionComparer.Compare);
}
}
}
private handleRepetitionInstructions(currentRepetitionInstruction: RepetitionInstruction): boolean {
if (!this.currentMeasure) {
return false;
}
switch (currentRepetitionInstruction.type) {
case RepetitionInstructionEnum.StartLine:
this.currentMeasure.FirstRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.BackJumpLine:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.Ending:
// set ending start or end
if (currentRepetitionInstruction.alignment === AlignmentType.Begin) { // ending start
this.currentMeasure.FirstRepetitionInstructions.push(currentRepetitionInstruction);
} else { // ending end
for (let idx: number = 0, len: number = currentRepetitionInstruction.endingIndices.length; idx < len; ++idx) {
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
}
}
break;
case RepetitionInstructionEnum.Segno:
this.currentMeasure.FirstRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.Fine:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.ToCoda:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.Coda:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.DaCapo:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.DalSegno:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.DalSegnoAlFine:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.DaCapoAlFine:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.DalSegnoAlCoda:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.DaCapoAlCoda:
this.currentMeasure.LastRepetitionInstructions.push(currentRepetitionInstruction);
break;
case RepetitionInstructionEnum.None:
break;
default:
throw new ArgumentOutOfRangeException("currentRepetitionInstruction");
}
return true;
}
}