/
VoiceEntry.ts
439 lines (430 loc) · 19.8 KB
/
VoiceEntry.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
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
import {Fraction} from "../../Common/DataObjects/Fraction";
import {Voice} from "./Voice";
import {SourceStaffEntry} from "./SourceStaffEntry";
import {Note} from "./Note";
import {Pitch} from "../../Common/DataObjects/Pitch";
import {LyricsEntry} from "./Lyrics/LyricsEntry";
import {TechnicalInstruction} from "./Instructions/TechnicalInstruction";
import {OrnamentContainer} from "./OrnamentContainer";
import {KeyInstruction} from "./Instructions/KeyInstruction";
import {OrnamentEnum} from "./OrnamentContainer";
import {AccidentalEnum} from "../../Common/DataObjects/Pitch";
import { Dictionary } from "typescript-collections";
import {Arpeggio} from "./Arpeggio";
import { SourceMeasure } from "./SourceMeasure";
import { Articulation } from "./Articulation";
/**
* A [[VoiceEntry]] contains the notes in a voice at a timestamp.
*/
export class VoiceEntry {
/**
*
* @param timestamp The relative timestamp within the source measure.
* @param parentVoice
* @param parentSourceStaffEntry
* @param isGrace States whether the VoiceEntry has (only) grace notes.
* @param graceNoteSlash States whether the grace note(s) have a slash (Acciaccatura, played before the beat)
*/
constructor(timestamp: Fraction, parentVoice: Voice, parentSourceStaffEntry: SourceStaffEntry,
isGrace: boolean = false, graceNoteSlash: boolean = false, graceSlur: boolean = false) {
this.timestamp = timestamp;
this.parentVoice = parentVoice;
this.parentSourceStaffEntry = parentSourceStaffEntry;
this.isGrace = isGrace;
this.graceAfterMainNote = false;
this.graceNoteSlash = graceNoteSlash;
this.graceSlur = graceSlur;
// add currentVoiceEntry to staff entry:
if (parentSourceStaffEntry !== undefined) {
const list: VoiceEntry[] = parentSourceStaffEntry.VoiceEntries;
if (list.indexOf(this) === -1) {
list.push(this);
}
}
}
private parentVoice: Voice;
private parentSourceStaffEntry: SourceStaffEntry;
private timestamp: Fraction;
private notes: Note[] = [];
private isGrace: boolean;
/** States whether the grace notes come after a main note (at end of measure). */
private graceAfterMainNote: boolean;
private graceNoteSlash: boolean;
private graceSlur: boolean; // TODO grace slur system could be refined to be non-binary
private articulations: Articulation[] = [];
private technicalInstructions: TechnicalInstruction[] = [];
private lyricsEntries: Dictionary<string, LyricsEntry> = new Dictionary<string, LyricsEntry>();
/** The Arpeggio consisting of this VoiceEntry's notes. Undefined if no arpeggio exists. */
private arpeggio: Arpeggio;
private ornamentContainer: OrnamentContainer;
private wantedStemDirection: StemDirectionType = StemDirectionType.Undefined;
/** Stem direction specified in the xml stem element. */
private stemDirectionXml: StemDirectionType = StemDirectionType.Undefined;
private stemDirection: StemDirectionType = StemDirectionType.Undefined;
/** Color of the stem given in XML. RGB Hexadecimal, like #00FF00. */
private stemColorXml: string;
/** Color of the stem currently set. RGB Hexadecimal, like #00FF00. */
private stemColor: string;
public get ParentSourceStaffEntry(): SourceStaffEntry {
return this.parentSourceStaffEntry;
}
public get ParentVoice(): Voice {
return this.parentVoice;
}
public get Timestamp(): Fraction {
return this.timestamp;
}
public set Timestamp(value: Fraction) {
this.timestamp = value;
}
public get Notes(): Note[] {
return this.notes;
}
public get IsGrace(): boolean {
return this.isGrace;
}
public set IsGrace(value: boolean) {
this.isGrace = value;
}
public get GraceAfterMainNote(): boolean {
return this.graceAfterMainNote;
}
public set GraceAfterMainNote(value: boolean) {
this.graceAfterMainNote = value;
}
public get GraceNoteSlash(): boolean {
return this.graceNoteSlash;
}
public set GraceNoteSlash(value: boolean) {
this.graceNoteSlash = value;
}
public get GraceSlur(): boolean {
return this.graceSlur;
}
public set GraceSlur(value: boolean) {
this.graceSlur = value;
}
public get Articulations(): Articulation[] {
return this.articulations;
}
public set Articulations(value: Articulation[]) {
this.articulations = value;
}
public get TechnicalInstructions(): TechnicalInstruction[] {
return this.technicalInstructions;
}
public get LyricsEntries(): Dictionary<string, LyricsEntry> {
return this.lyricsEntries;
}
public get Arpeggio(): Arpeggio {
return this.arpeggio;
}
public set Arpeggio(value: Arpeggio) {
this.arpeggio = value;
}
public get OrnamentContainer(): OrnamentContainer {
return this.ornamentContainer;
}
public set OrnamentContainer(value: OrnamentContainer) {
this.ornamentContainer = value;
}
// WantedStemDirection provides the stem direction to VexFlow in case of more than 1 voice
// for optimal graphical appearance
public set WantedStemDirection(value: StemDirectionType) {
this.wantedStemDirection = value;
}
public get WantedStemDirection(): StemDirectionType {
return this.wantedStemDirection;
}
public set StemDirectionXml(value: StemDirectionType) {
this.stemDirectionXml = value;
}
public get StemDirectionXml(): StemDirectionType {
return this.stemDirectionXml;
}
// StemDirection holds the actual value of the stem
public set StemDirection(value: StemDirectionType) {
this.stemDirection = value;
}
public get StemDirection(): StemDirectionType {
return this.stemDirection;
}
public get StemColorXml(): string {
return this.stemColorXml;
}
public set StemColorXml(value: string) {
this.stemColorXml = value;
}
public get StemColor(): string {
return this.stemColor;
}
public set StemColor(value: string) {
this.stemColor = value;
}
public hasArticulation(articulation: Articulation): boolean {
for (const existingArticulation of this.articulations) {
if (existingArticulation.Equals(articulation)) {
return true;
}
}
return false;
}
public static isSupportedArticulation(articulation: ArticulationEnum): boolean {
switch (articulation) {
case ArticulationEnum.accent:
case ArticulationEnum.strongaccent:
case ArticulationEnum.softaccent:
case ArticulationEnum.invertedstrongaccent:
case ArticulationEnum.staccato:
case ArticulationEnum.staccatissimo:
case ArticulationEnum.spiccato:
case ArticulationEnum.tenuto:
case ArticulationEnum.fermata:
case ArticulationEnum.invertedfermata:
case ArticulationEnum.breathmark:
case ArticulationEnum.caesura:
case ArticulationEnum.lefthandpizzicato:
case ArticulationEnum.naturalharmonic:
case ArticulationEnum.snappizzicato:
case ArticulationEnum.upbow:
case ArticulationEnum.downbow:
case ArticulationEnum.bend:
return true;
default:
return false;
}
}
public hasTie(): boolean {
for (let idx: number = 0, len: number = this.Notes.length; idx < len; ++idx) {
const note: Note = this.Notes[idx];
if (note.NoteTie) { return true; }
}
return false;
}
public hasSlur(): boolean {
for (let idx: number = 0, len: number = this.Notes.length; idx < len; ++idx) {
const note: Note = this.Notes[idx];
if (note.NoteSlurs.length > 0) { return true; }
}
return false;
}
public isStaccato(): boolean {
for (const articulation of this.Articulations) {
if (articulation.articulationEnum === ArticulationEnum.staccato) {
return true;
}
}
return false;
}
public isAccent(): boolean {
for (const articulation of this.Articulations) {
if (articulation.articulationEnum === ArticulationEnum.accent || articulation.articulationEnum === ArticulationEnum.strongaccent) {
return true;
}
}
return false;
}
public getVerseNumberForLyricEntry(lyricsEntry: LyricsEntry): string {
let verseNumber: string = "1";
this.lyricsEntries.forEach((key: string, value: LyricsEntry): void => {
if (lyricsEntry === value) {
verseNumber = key;
}
});
return verseNumber;
}
//public createVoiceEntriesForOrnament(activeKey: KeyInstruction): VoiceEntry[] {
// return this.createVoiceEntriesForOrnament(this, activeKey);
//}
public createVoiceEntriesForOrnament(voiceEntryWithOrnament: VoiceEntry, activeKey: KeyInstruction): VoiceEntry[] {
if (!voiceEntryWithOrnament) {
voiceEntryWithOrnament = this;
}
const voiceEntries: VoiceEntry[] = [];
if (!voiceEntryWithOrnament.ornamentContainer) {
return;
}
const baseNote: Note = this.notes[0];
const baselength: Fraction = baseNote.Length;
const baseVoice: Voice = voiceEntryWithOrnament.ParentVoice;
const baseTimestamp: Fraction = voiceEntryWithOrnament.Timestamp;
let currentTimestamp: Fraction = Fraction.createFromFraction(baseTimestamp);
//let length: Fraction;
switch (voiceEntryWithOrnament.ornamentContainer.GetOrnament) {
case OrnamentEnum.Trill: {
const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 8);
const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
let alteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
if (voiceEntryWithOrnament.OrnamentContainer.AccidentalAbove !== AccidentalEnum.NONE) {
alteration = voiceEntryWithOrnament.ornamentContainer.AccidentalAbove;
}
for (let i: number = 0; i < 8; i++) {
currentTimestamp = Fraction.plus(baseTimestamp, new Fraction(i * length.Numerator, length.Denominator));
if ((i % 2) === 0) {
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
} else {
this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, baseNote.SourceMeasure, higherPitch, alteration, voiceEntries);
}
}
break;
}
case OrnamentEnum.Turn: {
const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
this.createAlteratedVoiceEntry(
currentTimestamp, length, baseVoice, baseNote.SourceMeasure, higherPitch, higherAlteration, voiceEntries
);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp.Add(length);
this.createAlteratedVoiceEntry(
currentTimestamp, length, baseVoice, baseNote.SourceMeasure, lowerPitch, lowerAlteration, voiceEntries
);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
break;
}
case OrnamentEnum.InvertedTurn: {
const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
this.createAlteratedVoiceEntry(
currentTimestamp, length, baseVoice, baseNote.SourceMeasure, lowerPitch, lowerAlteration, voiceEntries
);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp.Add(length);
this.createAlteratedVoiceEntry(
currentTimestamp, length, baseVoice, baseNote.SourceMeasure, higherPitch, higherAlteration, voiceEntries
);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
break;
}
case OrnamentEnum.DelayedTurn: {
const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 2);
const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp = Fraction.plus(baseTimestamp, length);
length.Denominator = baselength.Denominator * 8;
this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, baseNote.SourceMeasure, higherPitch, higherAlteration, voiceEntries);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp.Add(length);
this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, baseNote.SourceMeasure, lowerPitch, lowerAlteration, voiceEntries);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
break;
}
case OrnamentEnum.DelayedInvertedTurn: {
const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 2);
const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp = Fraction.plus(baseTimestamp, length);
length.Denominator = baselength.Denominator * 8;
this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, baseNote.SourceMeasure, lowerPitch, lowerAlteration, voiceEntries);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp.Add(length);
this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, baseNote.SourceMeasure, higherPitch, higherAlteration, voiceEntries);
currentTimestamp.Add(length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
break;
}
case OrnamentEnum.Mordent: {
const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
const alteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp.Add(length);
this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, baseNote.SourceMeasure, higherPitch, alteration, voiceEntries);
length.Denominator = baselength.Denominator * 2;
currentTimestamp = Fraction.plus(baseTimestamp, length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
break;
}
case OrnamentEnum.InvertedMordent: {
const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
const alteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
currentTimestamp.Add(length);
this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, baseNote.SourceMeasure, lowerPitch, alteration, voiceEntries);
length.Denominator = baselength.Denominator * 2;
currentTimestamp = Fraction.plus(baseTimestamp, length);
this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
break;
}
default:
throw new RangeError();
}
return voiceEntries;
}
private createBaseVoiceEntry(
currentTimestamp: Fraction, length: Fraction, baseVoice: Voice, baseNote: Note, voiceEntries: VoiceEntry[]
): void {
const voiceEntry: VoiceEntry = new VoiceEntry(currentTimestamp, baseVoice, baseNote.ParentStaffEntry);
const pitch: Pitch = new Pitch(baseNote.Pitch.FundamentalNote, baseNote.Pitch.Octave, baseNote.Pitch.Accidental);
const note: Note = new Note(voiceEntry, undefined, length, pitch, baseNote.SourceMeasure);
voiceEntry.Notes.push(note);
voiceEntries.push(voiceEntry);
}
private createAlteratedVoiceEntry(
currentTimestamp: Fraction, length: Fraction, baseVoice: Voice, sourceMeasure: SourceMeasure, higherPitch: Pitch,
alteration: AccidentalEnum, voiceEntries: VoiceEntry[]
): void {
const voiceEntry: VoiceEntry = new VoiceEntry(currentTimestamp, baseVoice, undefined);
const pitch: Pitch = new Pitch(higherPitch.FundamentalNote, higherPitch.Octave, alteration);
const note: Note = new Note(voiceEntry, undefined, length, pitch, sourceMeasure);
voiceEntry.Notes.push(note);
voiceEntries.push(voiceEntry);
}
}
export enum ArticulationEnum {
accent,
strongaccent,
softaccent,
marcatoup,
marcatodown,
invertedstrongaccent,
staccato,
staccatissimo,
spiccato,
tenuto,
fermata,
invertedfermata,
breathmark,
caesura,
lefthandpizzicato,
naturalharmonic,
snappizzicato,
upbow,
downbow,
scoop,
plop,
doit,
falloff,
stress,
unstress,
detachedlegato,
otherarticulation,
bend
}
export enum StemDirectionType {
Undefined = -1,
Up = 0,
Down = 1,
None = 2,
Double = 3
}