vexflow-native 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -1
- package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +61 -12
- package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
- package/lib/module/musicxml/MusicXmlParser/PartParser.js +1 -0
- package/lib/module/musicxml/MusicXmlParser/PartParser.js.map +1 -1
- package/lib/module/musicxml/MusicXmlParser/index.js +1 -0
- package/lib/module/musicxml/MusicXmlParser/index.js.map +1 -1
- package/lib/module/musicxml/MusicXmlParser/mappers.js +11 -1
- package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
- package/lib/module/renderer/measure.js +19 -14
- package/lib/module/renderer/measure.js.map +1 -1
- package/lib/module/renderer/render.js +24 -13
- package/lib/module/renderer/render.js.map +1 -1
- package/lib/module/renderer/scoreParsing.js +82 -21
- package/lib/module/renderer/scoreParsing.js.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +7 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/index.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
- package/lib/typescript/src/renderer/render.d.ts +7 -3
- package/lib/typescript/src/renderer/render.d.ts.map +1 -1
- package/lib/typescript/src/renderer/scoreParsing.d.ts +14 -3
- package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
- package/lib/typescript/src/state/types.d.ts +15 -2
- package/lib/typescript/src/state/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/musicxml/MusicXmlParser/InternalTypes.ts +8 -0
- package/src/musicxml/MusicXmlParser/MeasureParser.ts +83 -20
- package/src/musicxml/MusicXmlParser/PartParser.ts +2 -0
- package/src/musicxml/MusicXmlParser/index.ts +1 -0
- package/src/musicxml/MusicXmlParser/mappers.ts +15 -1
- package/src/renderer/measure.tsx +21 -14
- package/src/renderer/render.tsx +30 -14
- package/src/renderer/scoreParsing.tsx +117 -22
- package/src/state/types.ts +20 -2
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
Chord,
|
|
3
3
|
Direction,
|
|
4
|
+
DurationValue,
|
|
4
5
|
Measure,
|
|
5
6
|
MeasureState,
|
|
7
|
+
Pitch,
|
|
6
8
|
Voice,
|
|
7
9
|
VoiceItem,
|
|
8
10
|
} from '../../state';
|
|
@@ -205,6 +207,12 @@ function parseNote(
|
|
|
205
207
|
}
|
|
206
208
|
|
|
207
209
|
if (hasChild(note, 'grace')) {
|
|
210
|
+
const graceVoiceName = childText(note, 'voice');
|
|
211
|
+
|
|
212
|
+
if (graceVoiceName) {
|
|
213
|
+
collectGraceNote(note, graceVoiceName, state);
|
|
214
|
+
}
|
|
215
|
+
|
|
208
216
|
return currentPosition;
|
|
209
217
|
}
|
|
210
218
|
|
|
@@ -239,6 +247,7 @@ function parseNote(
|
|
|
239
247
|
? mergeChordTone(voice, item, note)
|
|
240
248
|
: pushItem(voice, item);
|
|
241
249
|
|
|
250
|
+
attachPendingGraceNotes(itemId, voiceName, state);
|
|
242
251
|
applyNoteAttachments(note, itemId, staffNumber, voice.id, state);
|
|
243
252
|
|
|
244
253
|
if (isChordTone) {
|
|
@@ -330,20 +339,7 @@ function createVoiceItem(
|
|
|
330
339
|
isChordTone: boolean,
|
|
331
340
|
state: ParserState
|
|
332
341
|
): VoiceItem {
|
|
333
|
-
const
|
|
334
|
-
const type = typeText
|
|
335
|
-
? mapDurationType(typeText)
|
|
336
|
-
: inferDurationType(requiredNumberText(note, 'duration'), state.divisions);
|
|
337
|
-
const dotCount = childrenNamed(note, 'dot').length;
|
|
338
|
-
|
|
339
|
-
if (dotCount > 3) {
|
|
340
|
-
throw new MusicXmlParseError('More than three dots are unsupported');
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
const duration = {
|
|
344
|
-
length: type,
|
|
345
|
-
dots: dotCount ? (dotCount as 1 | 2 | 3) : undefined,
|
|
346
|
-
};
|
|
342
|
+
const duration = parseDuration(note, state);
|
|
347
343
|
|
|
348
344
|
if (hasChild(note, 'rest')) {
|
|
349
345
|
if (isChordTone) {
|
|
@@ -359,21 +355,88 @@ function createVoiceItem(
|
|
|
359
355
|
};
|
|
360
356
|
}
|
|
361
357
|
|
|
362
|
-
const pitch = mapPitch(
|
|
363
|
-
firstChild(note, 'pitch'),
|
|
364
|
-
childText(note, 'accidental')
|
|
365
|
-
);
|
|
366
|
-
|
|
367
358
|
return {
|
|
368
359
|
id: '',
|
|
369
360
|
type: 'note',
|
|
370
|
-
pitch,
|
|
361
|
+
pitch: parsePitch(note),
|
|
371
362
|
duration,
|
|
372
363
|
voiceId: voice.id,
|
|
373
364
|
stemDirection: mapStemDirection(childText(note, 'stem')),
|
|
374
365
|
};
|
|
375
366
|
}
|
|
376
367
|
|
|
368
|
+
function parseDuration(note: XmlElement, state: ParserState): DurationValue {
|
|
369
|
+
const typeText = childText(note, 'type');
|
|
370
|
+
const type = typeText
|
|
371
|
+
? mapDurationType(typeText)
|
|
372
|
+
: inferDurationType(requiredNumberText(note, 'duration'), state.divisions);
|
|
373
|
+
const dotCount = childrenNamed(note, 'dot').length;
|
|
374
|
+
|
|
375
|
+
if (dotCount > 3) {
|
|
376
|
+
throw new MusicXmlParseError('More than three dots are unsupported');
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
length: type,
|
|
381
|
+
dots: dotCount ? (dotCount as 1 | 2 | 3) : undefined,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function parsePitch(note: XmlElement): Pitch {
|
|
386
|
+
return mapPitch(firstChild(note, 'pitch'), childText(note, 'accidental'));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Grace chord tones keep only their first pitch; the group's slash comes
|
|
391
|
+
* from its first grace note.
|
|
392
|
+
*/
|
|
393
|
+
function collectGraceNote(
|
|
394
|
+
note: XmlElement,
|
|
395
|
+
voiceName: string,
|
|
396
|
+
state: ParserState
|
|
397
|
+
) {
|
|
398
|
+
if (hasChild(note, 'chord')) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const graceNote = {
|
|
403
|
+
pitch: parsePitch(note),
|
|
404
|
+
duration: parseDuration(note, state),
|
|
405
|
+
};
|
|
406
|
+
const pending = state.pendingGraceNotes.get(voiceName);
|
|
407
|
+
|
|
408
|
+
if (pending) {
|
|
409
|
+
pending.notes.push(graceNote);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
state.pendingGraceNotes.set(voiceName, {
|
|
414
|
+
notes: [graceNote],
|
|
415
|
+
slash: attr(firstChild(note, 'grace'), 'slash') === 'yes',
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function attachPendingGraceNotes(
|
|
420
|
+
itemId: string,
|
|
421
|
+
voiceName: string,
|
|
422
|
+
state: ParserState
|
|
423
|
+
) {
|
|
424
|
+
const pending = state.pendingGraceNotes.get(voiceName);
|
|
425
|
+
|
|
426
|
+
if (!pending) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
state.pendingGraceNotes.delete(voiceName);
|
|
431
|
+
state.attachments.push({
|
|
432
|
+
id: `${itemId}-grace`,
|
|
433
|
+
ownerId: itemId,
|
|
434
|
+
type: 'grace',
|
|
435
|
+
notes: pending.notes,
|
|
436
|
+
...(pending.slash ? { slash: true } : {}),
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
377
440
|
function pushItem(voice: Voice, item: VoiceItem): string {
|
|
378
441
|
item.id = `${voice.id}-item-${(voice.items.length + 1).toString()}`;
|
|
379
442
|
voice.items.push(item);
|
|
@@ -226,11 +226,25 @@ export function articulationsFromNotations(
|
|
|
226
226
|
articulations.push('fermata');
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
-
|
|
229
|
+
const technical = childrenNamed(notations, 'technical').flatMap((group) =>
|
|
230
|
+
group.children.map((child) => mapTechnical(child.name))
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
return [...articulations, ...technical].filter(
|
|
230
234
|
(item): item is Articulation => item !== undefined
|
|
231
235
|
);
|
|
232
236
|
}
|
|
233
237
|
|
|
238
|
+
function mapTechnical(name: string): Articulation | undefined {
|
|
239
|
+
switch (name) {
|
|
240
|
+
case 'open':
|
|
241
|
+
case 'stopped':
|
|
242
|
+
return name;
|
|
243
|
+
default:
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
234
248
|
function mapArticulation(name: string): Articulation | undefined {
|
|
235
249
|
switch (name) {
|
|
236
250
|
case 'staccato':
|
package/src/renderer/measure.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Articulation as VFArticulation,
|
|
3
3
|
Formatter,
|
|
4
|
+
GraceNoteGroup,
|
|
4
5
|
Stave,
|
|
5
6
|
Voice as VFVoice,
|
|
6
7
|
} from 'vexflow';
|
|
@@ -105,10 +106,7 @@ export function measureScore(
|
|
|
105
106
|
);
|
|
106
107
|
const vfVoices = voiceArtifacts.map(({ vfVoice }) => vfVoice);
|
|
107
108
|
|
|
108
|
-
|
|
109
|
-
formatter.joinVoices(vfVoices);
|
|
110
|
-
}
|
|
111
|
-
|
|
109
|
+
formatter.joinVoices(vfVoices);
|
|
112
110
|
allVoices.push(...vfVoices);
|
|
113
111
|
|
|
114
112
|
return {
|
|
@@ -261,9 +259,7 @@ function measureStaffVerticalBounds({
|
|
|
261
259
|
({ measure, ownerStaffId, staffIndex, voiceArtifacts }) => {
|
|
262
260
|
const vfVoices = voiceArtifacts.map(({ vfVoice }) => vfVoice);
|
|
263
261
|
|
|
264
|
-
|
|
265
|
-
formatter.joinVoices(vfVoices);
|
|
266
|
-
}
|
|
262
|
+
formatter.joinVoices(vfVoices);
|
|
267
263
|
|
|
268
264
|
voiceArtifacts.forEach(({ notes }, voiceIndex) => {
|
|
269
265
|
const items = measure.voices[voiceIndex]?.items ?? [];
|
|
@@ -306,7 +302,7 @@ function measureStaffVerticalBounds({
|
|
|
306
302
|
staffIndexById.get(
|
|
307
303
|
items[noteIndex]?.targetStaffId ?? ownerStaffId
|
|
308
304
|
) ?? staffIndex;
|
|
309
|
-
|
|
305
|
+
mergeModifierBounds(bounds[ownerStaffIndex], note);
|
|
310
306
|
mergeNoteBounds(bounds[ownerStaffIndex], note);
|
|
311
307
|
});
|
|
312
308
|
|
|
@@ -358,11 +354,12 @@ function createNoopRenderContext(): RenderContext {
|
|
|
358
354
|
const NOOP_RENDER_CONTEXT = createNoopRenderContext();
|
|
359
355
|
|
|
360
356
|
/**
|
|
361
|
-
* Merges the drawn extents of a note's articulations
|
|
362
|
-
* VexFlow only positions
|
|
363
|
-
* drawn against a no-op context first to make its
|
|
357
|
+
* Merges the drawn extents of a note's articulations and grace-note groups
|
|
358
|
+
* into the staff bounds. VexFlow only positions these modifiers inside
|
|
359
|
+
* `draw()`, so each one is drawn against a no-op context first to make its
|
|
360
|
+
* bounding boxes real.
|
|
364
361
|
*/
|
|
365
|
-
function
|
|
362
|
+
function mergeModifierBounds(
|
|
366
363
|
bounds: StaffVerticalBounds | undefined,
|
|
367
364
|
note: VFVoiceNote
|
|
368
365
|
) {
|
|
@@ -371,14 +368,24 @@ function mergeArticulationBounds(
|
|
|
371
368
|
}
|
|
372
369
|
|
|
373
370
|
for (const modifier of note.getModifiers()) {
|
|
374
|
-
|
|
371
|
+
const isArticulation = modifier instanceof VFArticulation;
|
|
372
|
+
const isGraceNoteGroup = modifier instanceof GraceNoteGroup;
|
|
373
|
+
|
|
374
|
+
if (!isArticulation && !isGraceNoteGroup) {
|
|
375
375
|
continue;
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
try {
|
|
379
379
|
modifier.setContext(NOOP_RENDER_CONTEXT);
|
|
380
380
|
modifier.draw();
|
|
381
|
-
|
|
381
|
+
|
|
382
|
+
if (isGraceNoteGroup) {
|
|
383
|
+
modifier.getGraceNotes().forEach((graceNote) => {
|
|
384
|
+
mergeBoundingBox(bounds, graceNote.getBoundingBox());
|
|
385
|
+
});
|
|
386
|
+
} else {
|
|
387
|
+
mergeBoundingBox(bounds, modifier.getBoundingBox());
|
|
388
|
+
}
|
|
382
389
|
} catch {
|
|
383
390
|
// Ghost and spacer notes cannot place modifiers; note bounds remain
|
|
384
391
|
// the fallback.
|
package/src/renderer/render.tsx
CHANGED
|
@@ -101,9 +101,13 @@ interface RenderMeasureOptions {
|
|
|
101
101
|
* area, overriding the formatter's duration-weighted spacing. Runs after
|
|
102
102
|
* `formatToStave` (the same post-format `setX` mechanism the formatter's own
|
|
103
103
|
* tuning uses), so beams, tuplets and modifiers pick the final positions up
|
|
104
|
-
* at draw time. The
|
|
105
|
-
*
|
|
106
|
-
*
|
|
104
|
+
* at draw time. The lattice follows VexFlow's own convention for its first
|
|
105
|
+
* tick: it starts at that context's left-modifier width (`totalLeftPx`), so a
|
|
106
|
+
* grace group or accidental on beat 1 hangs inside the note area instead of
|
|
107
|
+
* over the clef. The proportional span is clamped only by that inset and the
|
|
108
|
+
* LAST context's width — with a spacer voice on the lattice the latter is
|
|
109
|
+
* constant, so the mapping stays a pure function of time and of what hangs
|
|
110
|
+
* left of beat 1.
|
|
107
111
|
*/
|
|
108
112
|
export function applyFixedNoteSpacing(
|
|
109
113
|
formatter: Formatter,
|
|
@@ -111,13 +115,13 @@ export function applyFixedNoteSpacing(
|
|
|
111
115
|
stave: Stave
|
|
112
116
|
) {
|
|
113
117
|
const contexts = formatter.getTickContexts();
|
|
114
|
-
const
|
|
118
|
+
const lastTickKey = contexts?.list[contexts.list.length - 1];
|
|
115
119
|
|
|
116
120
|
if (
|
|
117
121
|
!contexts ||
|
|
118
122
|
voices.length === 0 ||
|
|
119
|
-
|
|
120
|
-
|
|
123
|
+
lastTickKey === undefined ||
|
|
124
|
+
lastTickKey <= 0
|
|
121
125
|
) {
|
|
122
126
|
return;
|
|
123
127
|
}
|
|
@@ -128,6 +132,14 @@ export function applyFixedNoteSpacing(
|
|
|
128
132
|
return;
|
|
129
133
|
}
|
|
130
134
|
|
|
135
|
+
// Tick context keys are ticks × the formatter's resolution multiplier (the
|
|
136
|
+
// lcm that keeps tuplet ticks integral: 3 with triplets, 5 with
|
|
137
|
+
// quintuplets…), while getTotalTicks() is in plain ticks. Compare in plain
|
|
138
|
+
// ticks, or every position scales by the multiplier and the overflow
|
|
139
|
+
// correction below crushes the last tick flush against the note end.
|
|
140
|
+
const multiplier = contexts.resolutionMultiplier || 1;
|
|
141
|
+
const lastTick = lastTickKey / multiplier;
|
|
142
|
+
|
|
131
143
|
// Tick context x is relative to noteStartX + the stave's left note padding
|
|
132
144
|
// (see Tickable.getAbsoluteX), so the usable span ends at noteEndX. The
|
|
133
145
|
// padding is derived from Stave statics because the `Metrics` module is not
|
|
@@ -135,11 +147,15 @@ export function applyFixedNoteSpacing(
|
|
|
135
147
|
const stavePadding = Stave.defaultPadding - Stave.rightPadding;
|
|
136
148
|
const noteAreaWidth =
|
|
137
149
|
stave.getNoteEndX() - stave.getNoteStartX() - stavePadding;
|
|
138
|
-
|
|
150
|
+
const firstLeft = contexts.map[contexts.list[0]!]!.getMetrics().totalLeftPx;
|
|
151
|
+
let span = noteAreaWidth - firstLeft;
|
|
139
152
|
|
|
140
|
-
const lastContext = contexts.map[
|
|
153
|
+
const lastContext = contexts.map[lastTickKey]!;
|
|
141
154
|
const lastOverflow =
|
|
142
|
-
|
|
155
|
+
firstLeft +
|
|
156
|
+
(lastTick / totalTicks) * span +
|
|
157
|
+
lastContext.getWidth() -
|
|
158
|
+
noteAreaWidth;
|
|
143
159
|
|
|
144
160
|
if (lastOverflow > 0) {
|
|
145
161
|
span -= (lastOverflow * totalTicks) / lastTick;
|
|
@@ -149,8 +165,10 @@ export function applyFixedNoteSpacing(
|
|
|
149
165
|
return;
|
|
150
166
|
}
|
|
151
167
|
|
|
152
|
-
for (const
|
|
153
|
-
contexts.map[
|
|
168
|
+
for (const tickKey of contexts.list) {
|
|
169
|
+
contexts.map[tickKey]!.setX(
|
|
170
|
+
firstLeft + (tickKey / multiplier / totalTicks) * span
|
|
171
|
+
);
|
|
154
172
|
}
|
|
155
173
|
}
|
|
156
174
|
|
|
@@ -195,9 +213,7 @@ function renderMeasure(
|
|
|
195
213
|
}));
|
|
196
214
|
const vfVoices = voiceArtifacts.map(({ vfVoice }) => vfVoice);
|
|
197
215
|
|
|
198
|
-
|
|
199
|
-
formatter.joinVoices(vfVoices);
|
|
200
|
-
}
|
|
216
|
+
formatter.joinVoices(vfVoices);
|
|
201
217
|
|
|
202
218
|
return {
|
|
203
219
|
vfVoices,
|
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
Element,
|
|
7
7
|
Fraction as VFFraction,
|
|
8
8
|
GhostNote,
|
|
9
|
+
GraceNote as VFGraceNote,
|
|
10
|
+
GraceNoteGroup,
|
|
9
11
|
ModifierPosition,
|
|
10
12
|
Parenthesis,
|
|
11
13
|
StaveNote,
|
|
@@ -20,6 +22,7 @@ import type {
|
|
|
20
22
|
Clef,
|
|
21
23
|
KeySignature,
|
|
22
24
|
DurationValue,
|
|
25
|
+
GraceNoteAttachment,
|
|
23
26
|
Meter,
|
|
24
27
|
NoteAttachment,
|
|
25
28
|
Notehead,
|
|
@@ -83,6 +86,8 @@ export const ARTICULATION_TO_VF_CODE: Record<Articulation, string> = {
|
|
|
83
86
|
accent: 'a>',
|
|
84
87
|
marcato: 'a^',
|
|
85
88
|
fermata: 'a@',
|
|
89
|
+
open: 'ah',
|
|
90
|
+
stopped: 'a+',
|
|
86
91
|
};
|
|
87
92
|
|
|
88
93
|
/**
|
|
@@ -148,7 +153,10 @@ export function applyDots(note: StaveNote, dots?: 0 | 1 | 2 | 3) {
|
|
|
148
153
|
/**
|
|
149
154
|
* Adds pitch accidentals to the matching keys in a VexFlow note.
|
|
150
155
|
*/
|
|
151
|
-
export function addPitchAccidentals(
|
|
156
|
+
export function addPitchAccidentals(
|
|
157
|
+
note: StaveNote,
|
|
158
|
+
pitches: readonly Pitch[]
|
|
159
|
+
) {
|
|
152
160
|
pitches.forEach((pitch, index) => {
|
|
153
161
|
if (!pitch.accidental) {
|
|
154
162
|
return;
|
|
@@ -182,8 +190,43 @@ export function applyGhostParentheses(
|
|
|
182
190
|
});
|
|
183
191
|
}
|
|
184
192
|
|
|
185
|
-
|
|
193
|
+
function hasAccentAttachment(attachments: NoteAttachment[] | undefined) {
|
|
194
|
+
return (
|
|
195
|
+
attachments?.some(
|
|
196
|
+
(attachment) =>
|
|
197
|
+
attachment.type === 'articulation' &&
|
|
198
|
+
attachment.articulation === 'accent'
|
|
199
|
+
) ?? false
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Draws one accent above a note whose pitches carry the `accent` flag, unless
|
|
205
|
+
* the owner already has an accent attachment.
|
|
206
|
+
*/
|
|
207
|
+
export function applyPitchAccent(
|
|
186
208
|
note: StaveNote,
|
|
209
|
+
pitches: readonly Pitch[],
|
|
210
|
+
attachments?: NoteAttachment[]
|
|
211
|
+
) {
|
|
212
|
+
if (!pitches.some((pitch) => pitch.accent)) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (hasAccentAttachment(attachments)) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
note.addModifier(new VFArticulation(ARTICULATION_TO_VF_CODE.accent), 0);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Attaches the owner's articulation and grace-note modifiers to a VexFlow
|
|
225
|
+
* note; dynamics and lyrics are drawn elsewhere.
|
|
226
|
+
*/
|
|
227
|
+
export function applyNoteModifiers(
|
|
228
|
+
note: StaveNote,
|
|
229
|
+
clef: Clef,
|
|
187
230
|
attachments: NoteAttachment[] | undefined
|
|
188
231
|
) {
|
|
189
232
|
if (!attachments) {
|
|
@@ -191,20 +234,66 @@ export function applyArticulations(
|
|
|
191
234
|
}
|
|
192
235
|
|
|
193
236
|
for (const attachment of attachments) {
|
|
194
|
-
if (attachment.type
|
|
195
|
-
|
|
196
|
-
|
|
237
|
+
if (attachment.type === 'articulation') {
|
|
238
|
+
const articulation = new VFArticulation(
|
|
239
|
+
ARTICULATION_TO_VF_CODE[attachment.articulation]
|
|
240
|
+
);
|
|
197
241
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
242
|
+
if (attachment.placement === 'below') {
|
|
243
|
+
articulation.setPosition(ModifierPosition.BELOW);
|
|
244
|
+
}
|
|
201
245
|
|
|
202
|
-
|
|
203
|
-
|
|
246
|
+
note.addModifier(articulation, 0);
|
|
247
|
+
} else if (attachment.type === 'grace') {
|
|
248
|
+
applyGraceNoteGroup(note, clef, attachment);
|
|
204
249
|
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Grace notes stem up unless the owner was built stem-down (auto stems that
|
|
255
|
+
* flip later in `Beam.generateBeams` are not followed); two or more are
|
|
256
|
+
* beamed together, with the acciaccatura slash on the first stem only.
|
|
257
|
+
*/
|
|
258
|
+
function applyGraceNoteGroup(
|
|
259
|
+
note: StaveNote,
|
|
260
|
+
clef: Clef,
|
|
261
|
+
attachment: GraceNoteAttachment
|
|
262
|
+
) {
|
|
263
|
+
if (attachment.notes.length === 0) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const stemDirection =
|
|
268
|
+
note.getStemDirection() === Stem.DOWN ? Stem.DOWN : Stem.UP;
|
|
269
|
+
const graceNotes = attachment.notes.map((graceNote, index) => {
|
|
270
|
+
const vfGraceNote = new VFGraceNote({
|
|
271
|
+
clef,
|
|
272
|
+
keys: [pitchToVFKey(graceNote.pitch)],
|
|
273
|
+
duration: durationToVF(graceNote.duration),
|
|
274
|
+
slash: index === 0 && attachment.slash === true,
|
|
275
|
+
stemDirection,
|
|
276
|
+
});
|
|
277
|
+
decorateStaveNote(vfGraceNote, [graceNote.pitch], graceNote.duration);
|
|
278
|
+
return vfGraceNote;
|
|
279
|
+
});
|
|
280
|
+
const group = new GraceNoteGroup(graceNotes);
|
|
205
281
|
|
|
206
|
-
|
|
282
|
+
if (graceNotes.length > 1) {
|
|
283
|
+
group.beamNotes();
|
|
207
284
|
}
|
|
285
|
+
|
|
286
|
+
note.addModifier(group, 0);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function decorateStaveNote(
|
|
290
|
+
note: StaveNote,
|
|
291
|
+
pitches: readonly Pitch[],
|
|
292
|
+
duration: DurationValue
|
|
293
|
+
) {
|
|
294
|
+
addPitchAccidentals(note, pitches);
|
|
295
|
+
applyGhostParentheses(note, pitches);
|
|
296
|
+
applyDots(note, duration.dots);
|
|
208
297
|
}
|
|
209
298
|
|
|
210
299
|
export function indexAttachmentsByOwner(
|
|
@@ -279,10 +368,9 @@ export function voiceItemToStaveNote(
|
|
|
279
368
|
duration: durationToVF(item.duration),
|
|
280
369
|
stemDirection: toVFStemDirection(item.stemDirection),
|
|
281
370
|
});
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
applyDots(note, item.duration.dots);
|
|
371
|
+
decorateStaveNote(note, [item.pitch], item.duration);
|
|
372
|
+
applyPitchAccent(note, [item.pitch], attachments);
|
|
373
|
+
applyNoteModifiers(note, clef, attachments);
|
|
286
374
|
return note;
|
|
287
375
|
}
|
|
288
376
|
|
|
@@ -292,22 +380,29 @@ export function voiceItemToStaveNote(
|
|
|
292
380
|
duration: durationToVF(item.duration),
|
|
293
381
|
stemDirection: toVFStemDirection(item.stemDirection),
|
|
294
382
|
});
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
applyDots(note, item.duration.dots);
|
|
383
|
+
decorateStaveNote(note, item.pitches, item.duration);
|
|
384
|
+
applyPitchAccent(note, item.pitches, attachments);
|
|
385
|
+
applyNoteModifiers(note, clef, attachments);
|
|
299
386
|
return note;
|
|
300
387
|
}
|
|
301
388
|
|
|
302
389
|
/**
|
|
303
|
-
*
|
|
390
|
+
* Beam groups for a meter: its explicit `beamGroups`, otherwise VexFlow's
|
|
391
|
+
* conventional grouping for the time signature (quarters in x/4, dotted
|
|
392
|
+
* quarters in 6/8, 9/8, 12/8, halves in x/2).
|
|
304
393
|
*/
|
|
305
394
|
export function beamGroupsToVF(meter?: Meter): VFFraction[] | undefined {
|
|
306
|
-
if (!meter
|
|
395
|
+
if (!meter) {
|
|
307
396
|
return undefined;
|
|
308
397
|
}
|
|
309
398
|
|
|
310
|
-
|
|
399
|
+
if (meter.beamGroups?.length) {
|
|
400
|
+
return meter.beamGroups.map(
|
|
401
|
+
(group) => new VFFraction(group.num, group.den)
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return Beam.getDefaultBeamGroups(`${meter.beats}/${meter.beatUnit}`);
|
|
311
406
|
}
|
|
312
407
|
|
|
313
408
|
function hasExplicitStemDirection(item: VoiceItem): boolean {
|
package/src/state/types.ts
CHANGED
|
@@ -64,7 +64,9 @@ export type Articulation =
|
|
|
64
64
|
| 'accent'
|
|
65
65
|
| 'marcato'
|
|
66
66
|
| 'staccatissimo'
|
|
67
|
-
| 'fermata'
|
|
67
|
+
| 'fermata'
|
|
68
|
+
| 'open'
|
|
69
|
+
| 'stopped';
|
|
68
70
|
|
|
69
71
|
export type Dynamic =
|
|
70
72
|
| 'ppp'
|
|
@@ -102,6 +104,8 @@ export interface Pitch {
|
|
|
102
104
|
accidental?: Accidental;
|
|
103
105
|
notehead?: Notehead;
|
|
104
106
|
ghost?: boolean;
|
|
107
|
+
/** A note or chord with any accented pitch draws one accent (`>`) above it. */
|
|
108
|
+
accent?: boolean;
|
|
105
109
|
}
|
|
106
110
|
|
|
107
111
|
export interface DurationValue {
|
|
@@ -269,10 +273,24 @@ export interface LyricAttachment extends AttachmentBase {
|
|
|
269
273
|
verse?: number;
|
|
270
274
|
}
|
|
271
275
|
|
|
276
|
+
export interface GraceNote {
|
|
277
|
+
pitch: Pitch;
|
|
278
|
+
duration: DurationValue;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export interface GraceNoteAttachment extends AttachmentBase {
|
|
282
|
+
type: 'grace';
|
|
283
|
+
/** Played before the owner, in order. */
|
|
284
|
+
notes: GraceNote[];
|
|
285
|
+
/** Acciaccatura slash through the (first) stem. */
|
|
286
|
+
slash?: boolean;
|
|
287
|
+
}
|
|
288
|
+
|
|
272
289
|
export type NoteAttachment =
|
|
273
290
|
| ArticulationAttachment
|
|
274
291
|
| DynamicAttachment
|
|
275
|
-
| LyricAttachment
|
|
292
|
+
| LyricAttachment
|
|
293
|
+
| GraceNoteAttachment;
|
|
276
294
|
|
|
277
295
|
export interface Tie {
|
|
278
296
|
id: Id;
|