vexflow-native 1.1.0 → 1.3.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 +33 -1
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +50 -10
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
- package/lib/module/renderer/constants.js +2 -1
- package/lib/module/renderer/constants.js.map +1 -1
- package/lib/module/renderer/measure.js +85 -4
- package/lib/module/renderer/measure.js.map +1 -1
- package/lib/module/renderer/render.js +147 -8
- package/lib/module/renderer/render.js.map +1 -1
- package/lib/module/renderer/scale.js +78 -0
- package/lib/module/renderer/scale.js.map +1 -0
- package/lib/module/renderer/scoreParsing.js +121 -24
- package/lib/module/renderer/scoreParsing.js.map +1 -1
- package/lib/module/renderer/useScoreRecording.js +22 -4
- package/lib/module/renderer/useScoreRecording.js.map +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts +7 -1
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
- package/lib/typescript/src/renderer/constants.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
- package/lib/typescript/src/renderer/render.d.ts +25 -3
- package/lib/typescript/src/renderer/render.d.ts.map +1 -1
- package/lib/typescript/src/renderer/scale.d.ts +32 -0
- package/lib/typescript/src/renderer/scale.d.ts.map +1 -0
- package/lib/typescript/src/renderer/scoreParsing.d.ts +32 -2
- package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
- package/lib/typescript/src/renderer/types.d.ts +80 -1
- package/lib/typescript/src/renderer/types.d.ts.map +1 -1
- package/lib/typescript/src/renderer/useScoreRecording.d.ts +8 -1
- package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
- package/lib/typescript/src/state/types.d.ts +4 -0
- package/lib/typescript/src/state/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +59 -12
- package/src/renderer/constants.ts +1 -0
- package/src/renderer/measure.tsx +117 -5
- package/src/renderer/render.tsx +223 -12
- package/src/renderer/scale.ts +93 -0
- package/src/renderer/scoreParsing.tsx +173 -34
- package/src/renderer/types.ts +84 -1
- package/src/renderer/useScoreRecording.ts +41 -3
- package/src/state/types.ts +13 -0
package/src/renderer/render.tsx
CHANGED
|
@@ -2,29 +2,45 @@ import { Formatter, Stave, StaveConnector, Voice as VFVoice } from 'vexflow';
|
|
|
2
2
|
import type { StaveConnectorType } from 'vexflow';
|
|
3
3
|
|
|
4
4
|
import type { VexflowRecordingContext } from '../base';
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
NoteAttachment,
|
|
7
|
+
Score,
|
|
8
|
+
StaffGroupSymbol,
|
|
9
|
+
VoiceItem,
|
|
10
|
+
} from '../state';
|
|
6
11
|
import type {
|
|
7
12
|
GroupLayoutContext,
|
|
8
13
|
MeasureLayoutPlan,
|
|
9
14
|
ScoreLayoutPlan,
|
|
10
15
|
} from './layout';
|
|
11
|
-
import {
|
|
12
|
-
|
|
16
|
+
import {
|
|
17
|
+
indexAttachmentsByOwner,
|
|
18
|
+
makeVFVoice,
|
|
19
|
+
noteheadWidth,
|
|
20
|
+
} from './scoreParsing';
|
|
21
|
+
import type { ScoreItemsLayout, ScoreOptions } from './types';
|
|
13
22
|
import type { VFVoiceNote } from './scoreParsing';
|
|
14
23
|
|
|
15
24
|
/**
|
|
16
|
-
* Renders the score from a precomputed layout plan
|
|
25
|
+
* Renders the score from a precomputed layout plan and returns the formatted
|
|
26
|
+
* geometry of every rendered item and measure.
|
|
17
27
|
*/
|
|
18
28
|
export function renderScore(
|
|
19
29
|
ctx: VexflowRecordingContext,
|
|
20
30
|
score: Score,
|
|
21
31
|
layoutPlan: ScoreLayoutPlan,
|
|
22
|
-
|
|
23
|
-
) {
|
|
32
|
+
options: ScoreOptions
|
|
33
|
+
): ScoreItemsLayout {
|
|
24
34
|
const groupsById = new Map(
|
|
25
35
|
layoutPlan.groups.map((group) => [group.groupId, group])
|
|
26
36
|
);
|
|
27
37
|
const measuresByGroup = groupMeasuresByIndex(layoutPlan.measures);
|
|
38
|
+
const attachmentsByOwner = indexAttachmentsByOwner(score);
|
|
39
|
+
const itemsLayout: ScoreItemsLayout = {
|
|
40
|
+
items: {},
|
|
41
|
+
measures: [],
|
|
42
|
+
contentSize: layoutPlan.contentSize,
|
|
43
|
+
};
|
|
28
44
|
|
|
29
45
|
for (const system of layoutPlan.systems) {
|
|
30
46
|
const group = groupsById.get(system.groupId);
|
|
@@ -40,12 +56,23 @@ export function renderScore(
|
|
|
40
56
|
.filter((measure): measure is MeasureLayoutPlan => Boolean(measure));
|
|
41
57
|
|
|
42
58
|
for (const [measureIndex, measurePlan] of measurePlans.entries()) {
|
|
43
|
-
renderMeasure(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
59
|
+
renderMeasure(
|
|
60
|
+
ctx,
|
|
61
|
+
score,
|
|
62
|
+
group,
|
|
63
|
+
measurePlan,
|
|
64
|
+
attachmentsByOwner,
|
|
65
|
+
{
|
|
66
|
+
isFirstMeasureInSystem: measureIndex === 0,
|
|
67
|
+
isLastMeasureInSystem: measureIndex === measurePlans.length - 1,
|
|
68
|
+
fixedNoteSpacing: options.render.fixedNoteSpacing,
|
|
69
|
+
},
|
|
70
|
+
itemsLayout
|
|
71
|
+
);
|
|
47
72
|
}
|
|
48
73
|
}
|
|
74
|
+
|
|
75
|
+
return itemsLayout;
|
|
49
76
|
}
|
|
50
77
|
|
|
51
78
|
type StaffRenderArtifacts = {
|
|
@@ -66,6 +93,65 @@ type StaffRenderArtifacts = {
|
|
|
66
93
|
interface RenderMeasureOptions {
|
|
67
94
|
isFirstMeasureInSystem: boolean;
|
|
68
95
|
isLastMeasureInSystem: boolean;
|
|
96
|
+
fixedNoteSpacing: boolean;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Re-positions every tick context at its time-proportional x inside the note
|
|
101
|
+
* area, overriding the formatter's duration-weighted spacing. Runs after
|
|
102
|
+
* `formatToStave` (the same post-format `setX` mechanism the formatter's own
|
|
103
|
+
* tuning uses), so beams, tuplets and modifiers pick the final positions up
|
|
104
|
+
* at draw time. The proportional span is clamped only by the LAST context's
|
|
105
|
+
* width — with a spacer voice on the lattice that width is constant, so the
|
|
106
|
+
* mapping stays a pure function of time.
|
|
107
|
+
*/
|
|
108
|
+
export function applyFixedNoteSpacing(
|
|
109
|
+
formatter: Formatter,
|
|
110
|
+
voices: VFVoice[],
|
|
111
|
+
stave: Stave
|
|
112
|
+
) {
|
|
113
|
+
const contexts = formatter.getTickContexts();
|
|
114
|
+
const lastTick = contexts?.list[contexts.list.length - 1];
|
|
115
|
+
|
|
116
|
+
if (
|
|
117
|
+
!contexts ||
|
|
118
|
+
voices.length === 0 ||
|
|
119
|
+
lastTick === undefined ||
|
|
120
|
+
lastTick <= 0
|
|
121
|
+
) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const totalTicks = voices[0]?.getTotalTicks().value() ?? 0;
|
|
126
|
+
|
|
127
|
+
if (!(totalTicks > 0)) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Tick context x is relative to noteStartX + the stave's left note padding
|
|
132
|
+
// (see Tickable.getAbsoluteX), so the usable span ends at noteEndX. The
|
|
133
|
+
// padding is derived from Stave statics because the `Metrics` module is not
|
|
134
|
+
// re-exported by every vexflow build.
|
|
135
|
+
const stavePadding = Stave.defaultPadding - Stave.rightPadding;
|
|
136
|
+
const noteAreaWidth =
|
|
137
|
+
stave.getNoteEndX() - stave.getNoteStartX() - stavePadding;
|
|
138
|
+
let span = noteAreaWidth;
|
|
139
|
+
|
|
140
|
+
const lastContext = contexts.map[lastTick]!;
|
|
141
|
+
const lastOverflow =
|
|
142
|
+
(lastTick / totalTicks) * span + lastContext.getWidth() - noteAreaWidth;
|
|
143
|
+
|
|
144
|
+
if (lastOverflow > 0) {
|
|
145
|
+
span -= (lastOverflow * totalTicks) / lastTick;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!(span > 0)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
for (const tick of contexts.list) {
|
|
153
|
+
contexts.map[tick]!.setX((tick / totalTicks) * span);
|
|
154
|
+
}
|
|
69
155
|
}
|
|
70
156
|
|
|
71
157
|
function renderMeasure(
|
|
@@ -73,7 +159,9 @@ function renderMeasure(
|
|
|
73
159
|
score: Score,
|
|
74
160
|
group: GroupLayoutContext,
|
|
75
161
|
measurePlan: MeasureLayoutPlan,
|
|
76
|
-
|
|
162
|
+
attachmentsByOwner: Map<string, NoteAttachment[]>,
|
|
163
|
+
options: RenderMeasureOptions,
|
|
164
|
+
itemsLayout: ScoreItemsLayout
|
|
77
165
|
) {
|
|
78
166
|
const formatter = new Formatter();
|
|
79
167
|
const resolvedStateByStaffId = new Map(
|
|
@@ -95,6 +183,7 @@ function renderMeasure(
|
|
|
95
183
|
|
|
96
184
|
const voiceArtifacts = measure.voices.map((voice) => ({
|
|
97
185
|
...makeVFVoice(score, resolvedState.meter, resolvedState.clef, voice, {
|
|
186
|
+
attachmentsByOwner,
|
|
98
187
|
resolveClef: (item) =>
|
|
99
188
|
item.targetStaffId
|
|
100
189
|
? resolvedStateByStaffId.get(item.targetStaffId)?.clef ??
|
|
@@ -141,6 +230,12 @@ function renderMeasure(
|
|
|
141
230
|
stave.addClef(resolvedState.clef);
|
|
142
231
|
}
|
|
143
232
|
|
|
233
|
+
if (measure.leftModifiers?.showMeter === true) {
|
|
234
|
+
stave.addTimeSignature(
|
|
235
|
+
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
144
239
|
stave.setContext(ctx).draw();
|
|
145
240
|
return stave;
|
|
146
241
|
});
|
|
@@ -168,9 +263,25 @@ function renderMeasure(
|
|
|
168
263
|
});
|
|
169
264
|
|
|
170
265
|
if (allVoices.length > 0) {
|
|
171
|
-
|
|
266
|
+
// Format against the stave with the narrowest note area, so notes never
|
|
267
|
+
// overrun a stave whose clef or time signature pushes its note start
|
|
268
|
+
// further right.
|
|
269
|
+
const referenceStave = getFormatReferenceStave(renderedStaves);
|
|
270
|
+
formatter.formatToStave(allVoices, referenceStave);
|
|
271
|
+
|
|
272
|
+
if (options.fixedNoteSpacing && referenceStave) {
|
|
273
|
+
applyFixedNoteSpacing(formatter, allVoices, referenceStave);
|
|
274
|
+
}
|
|
172
275
|
}
|
|
173
276
|
|
|
277
|
+
collectMeasureItemsLayout(
|
|
278
|
+
itemsLayout,
|
|
279
|
+
measurePlan,
|
|
280
|
+
group.staves,
|
|
281
|
+
renderedStaves,
|
|
282
|
+
staffRenderArtifacts
|
|
283
|
+
);
|
|
284
|
+
|
|
174
285
|
staffRenderArtifacts.forEach(
|
|
175
286
|
({ vfVoices, voiceArtifacts, beams, tuplets }) => {
|
|
176
287
|
vfVoices.forEach((voice) => voice.setRendered());
|
|
@@ -183,6 +294,106 @@ function renderMeasure(
|
|
|
183
294
|
);
|
|
184
295
|
}
|
|
185
296
|
|
|
297
|
+
/**
|
|
298
|
+
* The stave with the narrowest note area — formatting to it keeps notes
|
|
299
|
+
* inside every stave of the group.
|
|
300
|
+
*/
|
|
301
|
+
function getFormatReferenceStave(renderedStaves: Stave[]): Stave {
|
|
302
|
+
return renderedStaves.reduce((reference, stave) =>
|
|
303
|
+
stave.getNoteStartX() > reference.getNoteStartX() ? stave : reference
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Records the formatted geometry of one measure; must run after
|
|
309
|
+
* `formatToStave` because note positions are only final then. Emits one
|
|
310
|
+
* measure entry per rendered stave, since note bounds differ per stave.
|
|
311
|
+
*/
|
|
312
|
+
function collectMeasureItemsLayout(
|
|
313
|
+
itemsLayout: ScoreItemsLayout,
|
|
314
|
+
measurePlan: MeasureLayoutPlan,
|
|
315
|
+
staves: GroupLayoutContext['staves'],
|
|
316
|
+
renderedStaves: Stave[],
|
|
317
|
+
staffRenderArtifacts: StaffRenderArtifacts[]
|
|
318
|
+
) {
|
|
319
|
+
staffRenderArtifacts.forEach(({ voiceArtifacts }) => {
|
|
320
|
+
voiceArtifacts.forEach(({ items, notes }) => {
|
|
321
|
+
items.forEach((item, index) => {
|
|
322
|
+
const note = notes[index];
|
|
323
|
+
|
|
324
|
+
if (!note) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const x = note.getAbsoluteX();
|
|
329
|
+
const width = note.getWidth();
|
|
330
|
+
|
|
331
|
+
itemsLayout.items[item.id] = {
|
|
332
|
+
x,
|
|
333
|
+
width,
|
|
334
|
+
headCenterX: resolveItemHeadCenterX(note, x, width),
|
|
335
|
+
measureIndex: measurePlan.measureIndex,
|
|
336
|
+
};
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
staves.forEach((staff, staffIndex) => {
|
|
342
|
+
const stave = renderedStaves[staffIndex];
|
|
343
|
+
|
|
344
|
+
if (!stave) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
itemsLayout.measures.push({
|
|
349
|
+
groupId: measurePlan.groupId,
|
|
350
|
+
staffId: staff.id,
|
|
351
|
+
measureIndex: measurePlan.measureIndex,
|
|
352
|
+
systemIndex: measurePlan.systemIndex,
|
|
353
|
+
x: measurePlan.x,
|
|
354
|
+
width: measurePlan.width,
|
|
355
|
+
staveNoteStartX: stave.getNoteStartX(),
|
|
356
|
+
staveNoteEndX: stave.getNoteEndX(),
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Detected structurally because `GhostNote`s lack these getters. */
|
|
362
|
+
type NoteHeadSpan = {
|
|
363
|
+
getNoteHeadBeginX?: () => number;
|
|
364
|
+
getNoteHeadEndX?: () => number;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Center of a formatted note's visual notehead span. When the getters are
|
|
369
|
+
* missing (GhostNotes) the fallback is the center of a NOTIONAL notehead at
|
|
370
|
+
* the block's left edge, capped by the block width — so a `spacer` rest
|
|
371
|
+
* anchors where a real note's head would sit on the same tick, and external
|
|
372
|
+
* UI aligned to it does not hop when the note appears. Zero-width `hidden`
|
|
373
|
+
* rests keep anchoring at their tick x.
|
|
374
|
+
*/
|
|
375
|
+
export function resolveItemHeadCenterX(
|
|
376
|
+
note: VFVoiceNote,
|
|
377
|
+
x: number,
|
|
378
|
+
width: number
|
|
379
|
+
): number {
|
|
380
|
+
const { getNoteHeadBeginX, getNoteHeadEndX } = note as NoteHeadSpan;
|
|
381
|
+
|
|
382
|
+
if (
|
|
383
|
+
typeof getNoteHeadBeginX === 'function' &&
|
|
384
|
+
typeof getNoteHeadEndX === 'function'
|
|
385
|
+
) {
|
|
386
|
+
const center =
|
|
387
|
+
(getNoteHeadBeginX.call(note) + getNoteHeadEndX.call(note)) / 2;
|
|
388
|
+
|
|
389
|
+
if (Number.isFinite(center) && center > x && center <= x + width) {
|
|
390
|
+
return center;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return x + Math.min(width, noteheadWidth()) / 2;
|
|
395
|
+
}
|
|
396
|
+
|
|
186
397
|
function drawVoiceItems(
|
|
187
398
|
ctx: VexflowRecordingContext,
|
|
188
399
|
items: VoiceItem[],
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { RendererRect, RendererSize, ScoreItemsLayout } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Scale rule for `options.render.scale`: layout runs in content space against
|
|
5
|
+
* a virtual viewport (the view viewport divided by the scale), the recorded
|
|
6
|
+
* picture is scaled back at draw time, and everything exposed outside the
|
|
7
|
+
* render path (scroll content size, `onItemsLayout` geometry) is converted
|
|
8
|
+
* back to view space. At scale 1 every helper returns its input untouched.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Reads the render scale from options; invalid values fall back to 1.
|
|
13
|
+
*/
|
|
14
|
+
export function getRenderScale(options?: {
|
|
15
|
+
render?: { scale?: number };
|
|
16
|
+
}): number {
|
|
17
|
+
const scale = options?.render?.scale;
|
|
18
|
+
|
|
19
|
+
return typeof scale === 'number' && Number.isFinite(scale) && scale > 0
|
|
20
|
+
? scale
|
|
21
|
+
: 1;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The virtual content-space viewport layout runs against: the view viewport
|
|
26
|
+
* divided by the scale.
|
|
27
|
+
*/
|
|
28
|
+
export function createContentViewport(
|
|
29
|
+
viewport: RendererRect,
|
|
30
|
+
scale: number
|
|
31
|
+
): RendererRect {
|
|
32
|
+
if (scale === 1) {
|
|
33
|
+
return viewport;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
x: viewport.x / scale,
|
|
38
|
+
y: viewport.y / scale,
|
|
39
|
+
width: viewport.width / scale,
|
|
40
|
+
height: viewport.height / scale,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Converts a content-space size to view space.
|
|
46
|
+
*/
|
|
47
|
+
export function toViewSize(size: RendererSize, scale: number): RendererSize {
|
|
48
|
+
if (scale === 1) {
|
|
49
|
+
return size;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
width: size.width * scale,
|
|
54
|
+
height: size.height * scale,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Converts the geometry captured by `renderScore` to view space. The field
|
|
60
|
+
* list is hand-maintained: new coordinate fields must be multiplied here too
|
|
61
|
+
* (guarded by a structural test in scale.test.ts).
|
|
62
|
+
*/
|
|
63
|
+
export function scaleItemsLayoutToViewSpace(
|
|
64
|
+
itemsLayout: ScoreItemsLayout,
|
|
65
|
+
scale: number
|
|
66
|
+
): ScoreItemsLayout {
|
|
67
|
+
if (scale === 1) {
|
|
68
|
+
return itemsLayout;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const items: ScoreItemsLayout['items'] = {};
|
|
72
|
+
|
|
73
|
+
for (const [itemId, item] of Object.entries(itemsLayout.items)) {
|
|
74
|
+
items[itemId] = {
|
|
75
|
+
...item,
|
|
76
|
+
x: item.x * scale,
|
|
77
|
+
width: item.width * scale,
|
|
78
|
+
headCenterX: item.headCenterX * scale,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
items,
|
|
84
|
+
measures: itemsLayout.measures.map((measure) => ({
|
|
85
|
+
...measure,
|
|
86
|
+
x: measure.x * scale,
|
|
87
|
+
width: measure.width * scale,
|
|
88
|
+
staveNoteStartX: measure.staveNoteStartX * scale,
|
|
89
|
+
staveNoteEndX: measure.staveNoteEndX * scale,
|
|
90
|
+
})),
|
|
91
|
+
contentSize: toViewSize(itemsLayout.contentSize, scale),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Accidental as VFAccidental,
|
|
3
|
+
Articulation as VFArticulation,
|
|
3
4
|
Beam,
|
|
4
5
|
Dot,
|
|
6
|
+
Element,
|
|
5
7
|
Fraction as VFFraction,
|
|
6
8
|
GhostNote,
|
|
9
|
+
ModifierPosition,
|
|
10
|
+
Parenthesis,
|
|
7
11
|
StaveNote,
|
|
8
12
|
Stem,
|
|
9
13
|
Tuplet,
|
|
@@ -12,10 +16,13 @@ import {
|
|
|
12
16
|
import type { StemmableNote } from 'vexflow';
|
|
13
17
|
|
|
14
18
|
import type {
|
|
19
|
+
Articulation,
|
|
15
20
|
Clef,
|
|
16
21
|
KeySignature,
|
|
17
22
|
DurationValue,
|
|
18
23
|
Meter,
|
|
24
|
+
NoteAttachment,
|
|
25
|
+
Notehead,
|
|
19
26
|
Pitch,
|
|
20
27
|
Score,
|
|
21
28
|
Staff,
|
|
@@ -45,8 +52,39 @@ export type VFVoiceNote = StemmableNote;
|
|
|
45
52
|
|
|
46
53
|
export interface MakeVFVoiceOptions {
|
|
47
54
|
resolveClef?: (item: VoiceItem) => Clef;
|
|
55
|
+
/**
|
|
56
|
+
* Precomputed attachment index; pass it when calling in a loop to avoid
|
|
57
|
+
* rebuilding it on every call.
|
|
58
|
+
*/
|
|
59
|
+
attachmentsByOwner?: Map<string, NoteAttachment[]>;
|
|
48
60
|
}
|
|
49
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Maps library notehead names to VexFlow key glyph codes.
|
|
64
|
+
*/
|
|
65
|
+
export const NOTEHEAD_TO_VF_CODE: Record<Notehead, string> = {
|
|
66
|
+
'x': 'x',
|
|
67
|
+
'circle-x': 'cx',
|
|
68
|
+
'diamond': 'h',
|
|
69
|
+
'circle': 'ci',
|
|
70
|
+
'square': 'sq',
|
|
71
|
+
'triangle': 'tu',
|
|
72
|
+
'triangle-down': 'td',
|
|
73
|
+
'slash': 'sf',
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Maps library articulation names to VexFlow articulation codes.
|
|
78
|
+
*/
|
|
79
|
+
export const ARTICULATION_TO_VF_CODE: Record<Articulation, string> = {
|
|
80
|
+
staccato: 'a.',
|
|
81
|
+
staccatissimo: 'av',
|
|
82
|
+
tenuto: 'a-',
|
|
83
|
+
accent: 'a>',
|
|
84
|
+
marcato: 'a^',
|
|
85
|
+
fermata: 'a@',
|
|
86
|
+
};
|
|
87
|
+
|
|
50
88
|
/**
|
|
51
89
|
* Maps the library stem direction to the numeric VexFlow value.
|
|
52
90
|
*/
|
|
@@ -73,7 +111,13 @@ export function pitchToVFKey(pitch: Pitch): string {
|
|
|
73
111
|
? 'd#'
|
|
74
112
|
: pitch.accidental ?? '';
|
|
75
113
|
|
|
76
|
-
|
|
114
|
+
const key = `${pitch.step.toLowerCase()}${accidental}/${pitch.octave}`;
|
|
115
|
+
|
|
116
|
+
if (pitch.notehead) {
|
|
117
|
+
return `${key}/${NOTEHEAD_TO_VF_CODE[pitch.notehead]}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return key;
|
|
77
121
|
}
|
|
78
122
|
|
|
79
123
|
/**
|
|
@@ -85,15 +129,11 @@ export function durationToVF(duration: DurationValue, isRest = false): string {
|
|
|
85
129
|
? '1/2'
|
|
86
130
|
: duration.length;
|
|
87
131
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (duration.dots) {
|
|
93
|
-
return `${length}${'d'.repeat(duration.dots)}`;
|
|
94
|
-
}
|
|
132
|
+
// Dots drive VexFlow's tick math, so rests need them in the token too or
|
|
133
|
+
// strict voices come up short.
|
|
134
|
+
const dots = 'd'.repeat(duration.dots ?? 0);
|
|
95
135
|
|
|
96
|
-
return length
|
|
136
|
+
return `${length}${dots}${isRest ? 'r' : ''}`;
|
|
97
137
|
}
|
|
98
138
|
|
|
99
139
|
/**
|
|
@@ -125,13 +165,102 @@ export function addPitchAccidentals(note: StaveNote, pitches: Pitch[]) {
|
|
|
125
165
|
});
|
|
126
166
|
}
|
|
127
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Wraps each ghost pitch of a note in parentheses.
|
|
170
|
+
*/
|
|
171
|
+
export function applyGhostParentheses(
|
|
172
|
+
note: StaveNote,
|
|
173
|
+
pitches: readonly Pitch[]
|
|
174
|
+
) {
|
|
175
|
+
pitches.forEach((pitch, index) => {
|
|
176
|
+
if (!pitch.ghost) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
note.addModifier(new Parenthesis(ModifierPosition.LEFT), index);
|
|
181
|
+
note.addModifier(new Parenthesis(ModifierPosition.RIGHT), index);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function applyArticulations(
|
|
186
|
+
note: StaveNote,
|
|
187
|
+
attachments: NoteAttachment[] | undefined
|
|
188
|
+
) {
|
|
189
|
+
if (!attachments) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
for (const attachment of attachments) {
|
|
194
|
+
if (attachment.type !== 'articulation') {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const articulation = new VFArticulation(
|
|
199
|
+
ARTICULATION_TO_VF_CODE[attachment.articulation]
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
if (attachment.placement === 'below') {
|
|
203
|
+
articulation.setPosition(ModifierPosition.BELOW);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
note.addModifier(articulation, 0);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function indexAttachmentsByOwner(
|
|
211
|
+
score: Score
|
|
212
|
+
): Map<string, NoteAttachment[]> {
|
|
213
|
+
const attachmentsByOwner = new Map<string, NoteAttachment[]>();
|
|
214
|
+
|
|
215
|
+
for (const attachment of score.attachments ?? []) {
|
|
216
|
+
const ownedAttachments = attachmentsByOwner.get(attachment.ownerId);
|
|
217
|
+
|
|
218
|
+
if (ownedAttachments) {
|
|
219
|
+
ownedAttachments.push(attachment);
|
|
220
|
+
} else {
|
|
221
|
+
attachmentsByOwner.set(attachment.ownerId, [attachment]);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return attachmentsByOwner;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// SMuFL noteheadBlack — vexflow 5's entry point does not re-export `Glyphs`.
|
|
229
|
+
const NOTEHEAD_BLACK = '\uE0A4';
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Width a `spacer` rest reserves on its tick: an up-stem flagged note
|
|
233
|
+
* (2 × notehead + padding), the widest ordinary tickable. A TickContext takes
|
|
234
|
+
* the MAX of its members' widths, so reserving the worst case keeps a tick's
|
|
235
|
+
* width constant whether or not a real note shares it — a voice of spacers
|
|
236
|
+
* therefore holds engraved spacing still while notes toggle on and off its
|
|
237
|
+
* lattice. `hidden` rests stay zero-width (pure timing placeholders).
|
|
238
|
+
* Measured live because the value depends on the active music font.
|
|
239
|
+
*/
|
|
240
|
+
/** Measured width of the black notehead under the active music font. */
|
|
241
|
+
export function noteheadWidth(): number {
|
|
242
|
+
return Element.measureWidth(NOTEHEAD_BLACK, 'NoteHead');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function spacerTickWidth(): number {
|
|
246
|
+
return 2 * noteheadWidth() + StaveNote.minNoteheadPadding;
|
|
247
|
+
}
|
|
248
|
+
|
|
128
249
|
/**
|
|
129
250
|
* Builds a VexFlow stave note from a score voice item and clef.
|
|
130
251
|
*/
|
|
131
|
-
export function voiceItemToStaveNote(
|
|
252
|
+
export function voiceItemToStaveNote(
|
|
253
|
+
item: VoiceItem,
|
|
254
|
+
clef: Clef,
|
|
255
|
+
attachments?: NoteAttachment[]
|
|
256
|
+
): VFVoiceNote {
|
|
132
257
|
if (item.type === 'rest') {
|
|
133
258
|
if (item.kind === 'hidden' || item.kind === 'spacer') {
|
|
134
|
-
|
|
259
|
+
const note = new GhostNote(durationToVF(item.duration));
|
|
260
|
+
if (item.kind === 'spacer') {
|
|
261
|
+
note.setWidth(spacerTickWidth());
|
|
262
|
+
}
|
|
263
|
+
return note;
|
|
135
264
|
}
|
|
136
265
|
|
|
137
266
|
const note = new StaveNote({
|
|
@@ -151,6 +280,8 @@ export function voiceItemToStaveNote(item: VoiceItem, clef: Clef): VFVoiceNote {
|
|
|
151
280
|
stemDirection: toVFStemDirection(item.stemDirection),
|
|
152
281
|
});
|
|
153
282
|
addPitchAccidentals(note, [item.pitch]);
|
|
283
|
+
applyGhostParentheses(note, [item.pitch]);
|
|
284
|
+
applyArticulations(note, attachments);
|
|
154
285
|
applyDots(note, item.duration.dots);
|
|
155
286
|
return note;
|
|
156
287
|
}
|
|
@@ -162,6 +293,8 @@ export function voiceItemToStaveNote(item: VoiceItem, clef: Clef): VFVoiceNote {
|
|
|
162
293
|
stemDirection: toVFStemDirection(item.stemDirection),
|
|
163
294
|
});
|
|
164
295
|
addPitchAccidentals(note, item.pitches);
|
|
296
|
+
applyGhostParentheses(note, item.pitches);
|
|
297
|
+
applyArticulations(note, attachments);
|
|
165
298
|
applyDots(note, item.duration.dots);
|
|
166
299
|
return note;
|
|
167
300
|
}
|
|
@@ -248,8 +381,14 @@ export function makeVFVoice(
|
|
|
248
381
|
beams: Beam[];
|
|
249
382
|
tuplets: Tuplet[];
|
|
250
383
|
} {
|
|
384
|
+
const attachmentsByOwner =
|
|
385
|
+
options.attachmentsByOwner ?? indexAttachmentsByOwner(score);
|
|
251
386
|
const notes = voice.items.map((item) =>
|
|
252
|
-
voiceItemToStaveNote(
|
|
387
|
+
voiceItemToStaveNote(
|
|
388
|
+
item,
|
|
389
|
+
options.resolveClef?.(item) ?? clef,
|
|
390
|
+
attachmentsByOwner.get(item.id)
|
|
391
|
+
)
|
|
253
392
|
);
|
|
254
393
|
|
|
255
394
|
const noteByItemId = new Map<string, VFVoiceNote>();
|
|
@@ -257,6 +396,28 @@ export function makeVFVoice(
|
|
|
257
396
|
noteByItemId.set(item.id, notes[index]!)
|
|
258
397
|
);
|
|
259
398
|
|
|
399
|
+
const tuplets = findTupletsForVoice(score, voice)
|
|
400
|
+
.map((group) => {
|
|
401
|
+
const tupletNotes = group.itemIds
|
|
402
|
+
.map((id) => noteByItemId.get(id))
|
|
403
|
+
.filter((note): note is StaveNote => Boolean(note));
|
|
404
|
+
|
|
405
|
+
if (tupletNotes.length < 2) {
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return new Tuplet(tupletNotes, {
|
|
410
|
+
numNotes: group.ratio.num,
|
|
411
|
+
notesOccupied: group.ratio.den,
|
|
412
|
+
bracketed: group.bracketed,
|
|
413
|
+
location:
|
|
414
|
+
group.placement === 'below'
|
|
415
|
+
? Tuplet.LOCATION_BOTTOM
|
|
416
|
+
: Tuplet.LOCATION_TOP,
|
|
417
|
+
});
|
|
418
|
+
})
|
|
419
|
+
.filter((tuplet): tuplet is Tuplet => Boolean(tuplet));
|
|
420
|
+
|
|
260
421
|
const vfVoice = new VFVoice({
|
|
261
422
|
numBeats: meter.beats,
|
|
262
423
|
beatValue: meter.beatUnit,
|
|
@@ -285,28 +446,6 @@ export function makeVFVoice(
|
|
|
285
446
|
: undefined;
|
|
286
447
|
const beams = Beam.generateBeams(notes, beamOptions);
|
|
287
448
|
|
|
288
|
-
const tuplets = findTupletsForVoice(score, voice)
|
|
289
|
-
.map((group) => {
|
|
290
|
-
const tupletNotes = group.itemIds
|
|
291
|
-
.map((id) => noteByItemId.get(id))
|
|
292
|
-
.filter((note): note is StaveNote => Boolean(note));
|
|
293
|
-
|
|
294
|
-
if (tupletNotes.length < 2) {
|
|
295
|
-
return null;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
return new Tuplet(tupletNotes, {
|
|
299
|
-
numNotes: group.ratio.num,
|
|
300
|
-
notesOccupied: group.ratio.den,
|
|
301
|
-
bracketed: group.bracketed,
|
|
302
|
-
location:
|
|
303
|
-
group.placement === 'below'
|
|
304
|
-
? Tuplet.LOCATION_BOTTOM
|
|
305
|
-
: Tuplet.LOCATION_TOP,
|
|
306
|
-
});
|
|
307
|
-
})
|
|
308
|
-
.filter((tuplet): tuplet is Tuplet => Boolean(tuplet));
|
|
309
|
-
|
|
310
449
|
return { vfVoice, notes, beams, tuplets };
|
|
311
450
|
}
|
|
312
451
|
|