/
GraphicalStaffEntry.ts
347 lines (318 loc) · 13.6 KB
/
GraphicalStaffEntry.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
import {SourceStaffEntry} from "../VoiceData/SourceStaffEntry";
import {BoundingBox} from "./BoundingBox";
import {Fraction} from "../../Common/DataObjects/Fraction";
import {VerticalGraphicalStaffEntryContainer} from "./VerticalGraphicalStaffEntryContainer";
import {Note} from "../VoiceData/Note";
import {Slur} from "../VoiceData/Expressions/ContinuousExpressions/Slur";
import {Voice} from "../VoiceData/Voice";
import {VoiceEntry} from "../VoiceData/VoiceEntry";
import {GraphicalTie} from "./GraphicalTie";
import {GraphicalObject} from "./GraphicalObject";
import {GraphicalMeasure} from "./GraphicalMeasure";
import {GraphicalNote} from "./GraphicalNote";
import {GraphicalChordSymbolContainer} from "./GraphicalChordSymbolContainer";
import {GraphicalLyricEntry} from "./GraphicalLyricEntry";
import {AbstractGraphicalInstruction} from "./AbstractGraphicalInstruction";
import {GraphicalStaffEntryLink} from "./GraphicalStaffEntryLink";
import {CollectionUtil} from "../../Util/CollectionUtil";
import { GraphicalVoiceEntry } from "./GraphicalVoiceEntry";
import { MusicSheetCalculator } from "./MusicSheetCalculator";
import { Tie } from "../VoiceData/Tie";
import { GraphicalLabel } from "./GraphicalLabel";
import { SkyBottomLineCalculator } from "./SkyBottomLineCalculator";
/**
* The graphical counterpart of a [[SourceStaffEntry]].
*/
export abstract class GraphicalStaffEntry extends GraphicalObject {
constructor(parentMeasure: GraphicalMeasure, sourceStaffEntry: SourceStaffEntry = undefined, staffEntryParent: GraphicalStaffEntry = undefined) {
super();
this.parentMeasure = parentMeasure;
this.graphicalVoiceEntries = [];
this.sourceStaffEntry = sourceStaffEntry;
if (staffEntryParent) {
this.staffEntryParent = staffEntryParent;
this.parentVerticalContainer = staffEntryParent.parentVerticalContainer;
this.PositionAndShape = new BoundingBox(this, staffEntryParent.PositionAndShape);
} else {
this.PositionAndShape = new BoundingBox(this, parentMeasure.PositionAndShape);
}
if (sourceStaffEntry) {
this.relInMeasureTimestamp = sourceStaffEntry.Timestamp;
}
this.FingeringEntries = [];
}
public graphicalChordContainers: GraphicalChordSymbolContainer[] = [];
public graphicalLink: GraphicalStaffEntryLink;
// Extra member needed, as tie notes have no direct source entry with the right time stamp.
public relInMeasureTimestamp: Fraction;
public sourceStaffEntry: SourceStaffEntry;
public parentMeasure: GraphicalMeasure;
public graphicalVoiceEntries: GraphicalVoiceEntry[];
public staffEntryParent: GraphicalStaffEntry;
public parentVerticalContainer: VerticalGraphicalStaffEntryContainer;
public tabStaffEntry: GraphicalStaffEntry = undefined;
public MaxAccidentals: number = 0;
private graphicalInstructions: AbstractGraphicalInstruction[] = [];
public ties: Tie[] = [];
private graphicalTies: GraphicalTie[] = [];
private lyricsEntries: GraphicalLyricEntry[] = [];
public FingeringEntries: GraphicalLabel[];
public get GraphicalInstructions(): AbstractGraphicalInstruction[] {
return this.graphicalInstructions;
}
public get GraphicalTies(): GraphicalTie[] {
return this.graphicalTies;
}
public get LyricsEntries(): GraphicalLyricEntry[] {
return this.lyricsEntries;
}
public set LyricsEntries(value: GraphicalLyricEntry[]) {
this.lyricsEntries = value;
}
/**
* Calculate the absolute Timestamp.
* @returns {Fraction}
*/
public getAbsoluteTimestamp(): Fraction {
const result: Fraction = this.parentMeasure.parentSourceMeasure.AbsoluteTimestamp.clone();
if (this.relInMeasureTimestamp) {
result.Add(this.relInMeasureTimestamp);
}
return result;
}
/**
* Search through all the GraphicalNotes to find the suitable one for a TieEndNote.
* @param tieNote
* @returns {any}
*/
public findTieGraphicalNoteFromNote(tieNote: Note): GraphicalNote {
for (const gve of this.graphicalVoiceEntries) {
for (const graphicalNote of gve.notes) {
const note: Note = graphicalNote.sourceNote;
if (!note.isRest()
&& note.Pitch.FundamentalNote === tieNote.Pitch.FundamentalNote
&& note.Pitch.Octave === tieNote.Pitch.Octave
&& note.getAbsoluteTimestamp().Equals(tieNote.getAbsoluteTimestamp())) {
return graphicalNote;
}
}
}
return undefined;
}
/**
* Search through all [[GraphicalNote]]s to find the suitable one for an StartSlurNote (that 's also an EndTieNote).
* @param tieNote
* @param slur
* @returns {any}
*/
public findEndTieGraphicalNoteFromNoteWithStartingSlur(tieNote: Note, slur: Slur): GraphicalNote {
if (!tieNote) {
return undefined;
}
for (const gve of this.graphicalVoiceEntries) {
if (gve.parentVoiceEntry !== tieNote.ParentVoiceEntry) {
continue;
}
for (const graphicalNote of gve.notes) {
const note: Note = graphicalNote.sourceNote;
if (note.NoteTie && note.NoteSlurs.indexOf(slur) !== -1) {
return graphicalNote;
}
}
}
return undefined;
}
public findGraphicalNoteFromGraceNote(graceNote: Note): GraphicalNote {
if (!graceNote) {
return undefined;
}
for (const gve of this.graphicalVoiceEntries) {
if (gve.parentVoiceEntry !== graceNote.ParentVoiceEntry) {
continue;
}
for (const graphicalNote of gve.notes) {
if (graphicalNote.sourceNote === graceNote) {
return graphicalNote;
}
}
}
return undefined;
}
public findGraphicalNoteFromNote(note: Note): GraphicalNote {
if (!note) {
return undefined;
}
for (const gve of this.graphicalVoiceEntries) {
if (gve.parentVoiceEntry !== note.ParentVoiceEntry) {
continue;
}
for (const graphicalNote of gve.notes) {
if (graphicalNote.sourceNote === note && this.getAbsoluteTimestamp().Equals(note.getAbsoluteTimestamp())) {
return graphicalNote;
}
}
}
return undefined;
}
public getGraphicalNoteDurationFromVoice(voice: Voice): Fraction {
for (const gve of this.graphicalVoiceEntries) {
if (gve.parentVoiceEntry.ParentVoice !== voice) {
continue;
}
return gve.notes[0].graphicalNoteLength;
}
return new Fraction(0, 1);
}
/**
* Find the [[StaffEntry]]'s [[GraphicalNote]]s that correspond to the given [[VoiceEntry]]'s [[Note]]s.
* @param voiceEntry
* @returns {any}
*/
public findVoiceEntryGraphicalNotes(voiceEntry: VoiceEntry): GraphicalNote[] {
for (const gve of this.graphicalVoiceEntries) {
if (gve.parentVoiceEntry === voiceEntry) {
return gve.notes;
}
}
return undefined;
}
/**
* Check if the given [[VoiceEntry]] is part of the [[StaffEntry]]'s Linked [[VoiceEntry]].
* @param voiceEntry
* @returns {boolean}
*/
public isVoiceEntryPartOfLinkedVoiceEntry(voiceEntry: VoiceEntry): boolean {
if (this.sourceStaffEntry.Link) {
for (let idx: number = 0, len: number = this.sourceStaffEntry.Link.LinkStaffEntries.length; idx < len; ++idx) {
const sEntry: SourceStaffEntry = this.sourceStaffEntry.Link.LinkStaffEntries[idx];
if (sEntry.VoiceEntries.indexOf(voiceEntry) !== -1 && sEntry !== this.sourceStaffEntry) {
return true;
}
}
}
return false;
}
/**
* Return the [[StaffEntry]]'s Minimum NoteLength.
* @returns {Fraction}
*/
public findStaffEntryMinNoteLength(): Fraction {
let minLength: Fraction = new Fraction(Number.MAX_VALUE, 1);
for (const gve of this.graphicalVoiceEntries) {
for (const graphicalNote of gve.notes) {
const calNoteLen: Fraction = graphicalNote.graphicalNoteLength;
if (calNoteLen.lt(minLength) && calNoteLen.GetExpandedNumerator() > 0) {
minLength = calNoteLen;
}
}
}
return minLength;
}
public findStaffEntryMaxNoteLength(): Fraction {
let maxLength: Fraction = new Fraction(0, 1);
for (const gve of this.graphicalVoiceEntries) {
for (const graphicalNote of gve.notes) {
const calNoteLen: Fraction = graphicalNote.graphicalNoteLength;
if (maxLength.lt(calNoteLen) && calNoteLen.GetExpandedNumerator() > 0) {
maxLength = calNoteLen;
}
}
}
return maxLength;
}
/**
* Find or creates the list of [[GraphicalNote]]s in case of a [[VoiceEntry]] (not from TiedNote).
* @param voiceEntry
* @returns {GraphicalNote[]}
*/
public findOrCreateGraphicalVoiceEntry(voiceEntry: VoiceEntry): GraphicalVoiceEntry {
for (const gve of this.graphicalVoiceEntries) {
if (gve.parentVoiceEntry === voiceEntry) {
return gve;
}
}
// if not found in list, create new one and add to list:
const graphicalVoiceEntry: GraphicalVoiceEntry = MusicSheetCalculator.symbolFactory.createVoiceEntry(voiceEntry, this);
this.graphicalVoiceEntries.push(graphicalVoiceEntry);
return graphicalVoiceEntry;
}
/**
* Find or creates the list of [[GraphicalNote]]s in case of a TiedNote.
* @param graphicalNote
* @returns {GraphicalNote[]}
*/
public findOrCreateGraphicalVoiceEntryFromGraphicalNote(graphicalNote: GraphicalNote): GraphicalVoiceEntry {
for (const gve of this.graphicalVoiceEntries) {
if (gve === graphicalNote.parentVoiceEntry) {
return gve;
}
}
// if not found in list, create new one and add to list:
const graphicalVoiceEntry: GraphicalVoiceEntry = MusicSheetCalculator.symbolFactory.createVoiceEntry(graphicalNote.sourceNote.ParentVoiceEntry, this);
this.graphicalVoiceEntries.push(graphicalVoiceEntry);
return graphicalVoiceEntry;
}
/**
* Insert the [[GraphicalNote]] to the correct index of the [[GraphicalNote]]s list,
* so that the order of the [[GraphicalNote]]'s in the list corresponds to the [[VoiceEntry]]'s [[Note]]s order.
* (needed when adding Tie-EndNotes).
* @param graphicalNotes
* @param graphicalNote
*/
public addGraphicalNoteToListAtCorrectYPosition(gve: GraphicalVoiceEntry, graphicalNote: GraphicalNote): void {
const graphicalNotes: GraphicalNote[] = gve.notes;
if (graphicalNotes.length === 0 ||
graphicalNote.PositionAndShape.RelativePosition.y < CollectionUtil.last(graphicalNotes).PositionAndShape.RelativePosition.y) {
graphicalNotes.push(graphicalNote);
} else {
for (let i: number = graphicalNotes.length - 1; i >= 0; i--) {
if (graphicalNotes[i].PositionAndShape.RelativePosition.y > graphicalNote.PositionAndShape.RelativePosition.y) {
graphicalNotes.splice(i + 1, 0, graphicalNote);
break;
}
if (i === 0) {
graphicalNotes.splice(0, 0, graphicalNote);
break;
}
}
}
}
/**
* Returns true if this staff entry has only rests
*/
public hasOnlyRests(): boolean {
return this.sourceStaffEntry.hasOnlyRests;
}
public getSkylineMin(): number {
const skybottomcalculator: SkyBottomLineCalculator = this.parentMeasure?.ParentStaffLine.SkyBottomLineCalculator;
if (!skybottomcalculator) {
return undefined;
}
const [start, end] = this.getAbsoluteStartAndEnd();
return skybottomcalculator.getSkyLineMinInRange(start, end);
}
/** Highest Y around the staff entry and notes in OSMD units (pixels / 10). Note that negative y is up. */
public getHighestYAtEntry(): number {
const baseY: number = this.parentMeasure.ParentStaffLine.PositionAndShape.AbsolutePosition.y;
return baseY + this.getSkylineMin();
}
/** Lowest Y around the staff entry and notes in OSMD units (pixels / 10). Note that positive y is down. */
public getLowestYAtEntry(): number {
const baseY: number = this.parentMeasure.ParentStaffLine.PositionAndShape.AbsolutePosition.y;
return baseY + this.getBottomlineMax();
}
public getBottomlineMax(): number {
const skybottomcalculator: SkyBottomLineCalculator = this.parentMeasure?.ParentStaffLine.SkyBottomLineCalculator;
if (!skybottomcalculator) {
return undefined;
}
const [start, end] = this.getAbsoluteStartAndEnd();
return skybottomcalculator.getBottomLineMaxInRange(start, end);
}
public getAbsoluteStartAndEnd(): [number, number] {
let start: number = this.PositionAndShape.AbsolutePosition.x;
start -= this.parentMeasure.ParentStaffLine.PositionAndShape.AbsolutePosition.x;
const end: number = start + this.PositionAndShape.Size.width;
return [start, end];
}
}