vexflow-native 1.0.1 → 1.2.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 +35 -1
- package/lib/module/base/VexflowRecordingContext.js +69 -7
- package/lib/module/base/VexflowRecordingContext.js.map +1 -1
- package/lib/module/base/VexflowRecordingReplay.js +79 -5
- package/lib/module/base/VexflowRecordingReplay.js.map +1 -1
- package/lib/module/base/index.js +1 -0
- package/lib/module/base/index.js.map +1 -1
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +103 -18
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.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 +120 -7
- 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 +96 -23
- 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/base/VexflowRecordingContext.d.ts +32 -3
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +10 -2
- package/lib/typescript/src/base/VexflowRecordingReplay.d.ts.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingTypes.d.ts +53 -1
- package/lib/typescript/src/base/VexflowRecordingTypes.d.ts.map +1 -1
- package/lib/typescript/src/base/index.d.ts +1 -0
- package/lib/typescript/src/base/index.d.ts.map +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts +12 -5
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
- package/lib/typescript/src/renderer/render.d.ts +11 -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 +21 -2
- package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
- package/lib/typescript/src/renderer/types.d.ts +76 -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 +2 -3
- package/src/base/VexflowRecordingContext.ts +57 -7
- package/src/base/VexflowRecordingReplay.ts +121 -5
- package/src/base/VexflowRecordingTypes.ts +54 -2
- package/src/base/index.ts +1 -0
- package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +146 -25
- package/src/renderer/measure.tsx +117 -5
- package/src/renderer/render.tsx +181 -15
- package/src/renderer/scale.ts +93 -0
- package/src/renderer/scoreParsing.tsx +146 -33
- package/src/renderer/types.ts +80 -1
- package/src/renderer/useScoreRecording.ts +41 -3
- package/src/state/types.ts +13 -0
package/src/renderer/render.tsx
CHANGED
|
@@ -2,29 +2,41 @@ 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 { makeVFVoice } from './scoreParsing';
|
|
12
|
-
import type { ScoreOptions } from './types';
|
|
16
|
+
import { indexAttachmentsByOwner, makeVFVoice } from './scoreParsing';
|
|
17
|
+
import type { ScoreItemsLayout, ScoreOptions } from './types';
|
|
13
18
|
import type { VFVoiceNote } from './scoreParsing';
|
|
14
19
|
|
|
15
20
|
/**
|
|
16
|
-
* Renders the score from a precomputed layout plan
|
|
21
|
+
* Renders the score from a precomputed layout plan and returns the formatted
|
|
22
|
+
* geometry of every rendered item and measure.
|
|
17
23
|
*/
|
|
18
24
|
export function renderScore(
|
|
19
25
|
ctx: VexflowRecordingContext,
|
|
20
26
|
score: Score,
|
|
21
27
|
layoutPlan: ScoreLayoutPlan,
|
|
22
28
|
_options: ScoreOptions
|
|
23
|
-
) {
|
|
29
|
+
): ScoreItemsLayout {
|
|
24
30
|
const groupsById = new Map(
|
|
25
31
|
layoutPlan.groups.map((group) => [group.groupId, group])
|
|
26
32
|
);
|
|
27
33
|
const measuresByGroup = groupMeasuresByIndex(layoutPlan.measures);
|
|
34
|
+
const attachmentsByOwner = indexAttachmentsByOwner(score);
|
|
35
|
+
const itemsLayout: ScoreItemsLayout = {
|
|
36
|
+
items: {},
|
|
37
|
+
measures: [],
|
|
38
|
+
contentSize: layoutPlan.contentSize,
|
|
39
|
+
};
|
|
28
40
|
|
|
29
41
|
for (const system of layoutPlan.systems) {
|
|
30
42
|
const group = groupsById.get(system.groupId);
|
|
@@ -40,12 +52,22 @@ export function renderScore(
|
|
|
40
52
|
.filter((measure): measure is MeasureLayoutPlan => Boolean(measure));
|
|
41
53
|
|
|
42
54
|
for (const [measureIndex, measurePlan] of measurePlans.entries()) {
|
|
43
|
-
renderMeasure(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
55
|
+
renderMeasure(
|
|
56
|
+
ctx,
|
|
57
|
+
score,
|
|
58
|
+
group,
|
|
59
|
+
measurePlan,
|
|
60
|
+
attachmentsByOwner,
|
|
61
|
+
{
|
|
62
|
+
isFirstMeasureInSystem: measureIndex === 0,
|
|
63
|
+
isLastMeasureInSystem: measureIndex === measurePlans.length - 1,
|
|
64
|
+
},
|
|
65
|
+
itemsLayout
|
|
66
|
+
);
|
|
47
67
|
}
|
|
48
68
|
}
|
|
69
|
+
|
|
70
|
+
return itemsLayout;
|
|
49
71
|
}
|
|
50
72
|
|
|
51
73
|
type StaffRenderArtifacts = {
|
|
@@ -73,7 +95,9 @@ function renderMeasure(
|
|
|
73
95
|
score: Score,
|
|
74
96
|
group: GroupLayoutContext,
|
|
75
97
|
measurePlan: MeasureLayoutPlan,
|
|
76
|
-
|
|
98
|
+
attachmentsByOwner: Map<string, NoteAttachment[]>,
|
|
99
|
+
options: RenderMeasureOptions,
|
|
100
|
+
itemsLayout: ScoreItemsLayout
|
|
77
101
|
) {
|
|
78
102
|
const formatter = new Formatter();
|
|
79
103
|
const resolvedStateByStaffId = new Map(
|
|
@@ -95,6 +119,7 @@ function renderMeasure(
|
|
|
95
119
|
|
|
96
120
|
const voiceArtifacts = measure.voices.map((voice) => ({
|
|
97
121
|
...makeVFVoice(score, resolvedState.meter, resolvedState.clef, voice, {
|
|
122
|
+
attachmentsByOwner,
|
|
98
123
|
resolveClef: (item) =>
|
|
99
124
|
item.targetStaffId
|
|
100
125
|
? resolvedStateByStaffId.get(item.targetStaffId)?.clef ??
|
|
@@ -141,6 +166,12 @@ function renderMeasure(
|
|
|
141
166
|
stave.addClef(resolvedState.clef);
|
|
142
167
|
}
|
|
143
168
|
|
|
169
|
+
if (measure.leftModifiers?.showMeter === true) {
|
|
170
|
+
stave.addTimeSignature(
|
|
171
|
+
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
144
175
|
stave.setContext(ctx).draw();
|
|
145
176
|
return stave;
|
|
146
177
|
});
|
|
@@ -168,13 +199,148 @@ function renderMeasure(
|
|
|
168
199
|
});
|
|
169
200
|
|
|
170
201
|
if (allVoices.length > 0) {
|
|
171
|
-
|
|
202
|
+
// Format against the stave with the narrowest note area, so notes never
|
|
203
|
+
// overrun a stave whose clef or time signature pushes its note start
|
|
204
|
+
// further right.
|
|
205
|
+
formatter.formatToStave(allVoices, getFormatReferenceStave(renderedStaves));
|
|
172
206
|
}
|
|
173
207
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
208
|
+
collectMeasureItemsLayout(
|
|
209
|
+
itemsLayout,
|
|
210
|
+
measurePlan,
|
|
211
|
+
group.staves,
|
|
212
|
+
renderedStaves,
|
|
213
|
+
staffRenderArtifacts
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
staffRenderArtifacts.forEach(
|
|
217
|
+
({ vfVoices, voiceArtifacts, beams, tuplets }) => {
|
|
218
|
+
vfVoices.forEach((voice) => voice.setRendered());
|
|
219
|
+
voiceArtifacts.forEach(({ items, notes }) => {
|
|
220
|
+
drawVoiceItems(ctx, items, notes);
|
|
221
|
+
});
|
|
222
|
+
beams.forEach((beam) => beam.setContext(ctx).draw());
|
|
223
|
+
tuplets.forEach((tuplet) => tuplet.setContext(ctx).draw());
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The stave with the narrowest note area — formatting to it keeps notes
|
|
230
|
+
* inside every stave of the group.
|
|
231
|
+
*/
|
|
232
|
+
function getFormatReferenceStave(renderedStaves: Stave[]): Stave {
|
|
233
|
+
return renderedStaves.reduce((reference, stave) =>
|
|
234
|
+
stave.getNoteStartX() > reference.getNoteStartX() ? stave : reference
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Records the formatted geometry of one measure; must run after
|
|
240
|
+
* `formatToStave` because note positions are only final then. Emits one
|
|
241
|
+
* measure entry per rendered stave, since note bounds differ per stave.
|
|
242
|
+
*/
|
|
243
|
+
function collectMeasureItemsLayout(
|
|
244
|
+
itemsLayout: ScoreItemsLayout,
|
|
245
|
+
measurePlan: MeasureLayoutPlan,
|
|
246
|
+
staves: GroupLayoutContext['staves'],
|
|
247
|
+
renderedStaves: Stave[],
|
|
248
|
+
staffRenderArtifacts: StaffRenderArtifacts[]
|
|
249
|
+
) {
|
|
250
|
+
staffRenderArtifacts.forEach(({ voiceArtifacts }) => {
|
|
251
|
+
voiceArtifacts.forEach(({ items, notes }) => {
|
|
252
|
+
items.forEach((item, index) => {
|
|
253
|
+
const note = notes[index];
|
|
254
|
+
|
|
255
|
+
if (!note) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const x = note.getAbsoluteX();
|
|
260
|
+
const width = note.getWidth();
|
|
261
|
+
|
|
262
|
+
itemsLayout.items[item.id] = {
|
|
263
|
+
x,
|
|
264
|
+
width,
|
|
265
|
+
headCenterX: resolveItemHeadCenterX(note, x, width),
|
|
266
|
+
measureIndex: measurePlan.measureIndex,
|
|
267
|
+
};
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
staves.forEach((staff, staffIndex) => {
|
|
273
|
+
const stave = renderedStaves[staffIndex];
|
|
274
|
+
|
|
275
|
+
if (!stave) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
itemsLayout.measures.push({
|
|
280
|
+
groupId: measurePlan.groupId,
|
|
281
|
+
staffId: staff.id,
|
|
282
|
+
measureIndex: measurePlan.measureIndex,
|
|
283
|
+
systemIndex: measurePlan.systemIndex,
|
|
284
|
+
x: measurePlan.x,
|
|
285
|
+
width: measurePlan.width,
|
|
286
|
+
staveNoteStartX: stave.getNoteStartX(),
|
|
287
|
+
staveNoteEndX: stave.getNoteEndX(),
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Detected structurally because `GhostNote`s lack these getters. */
|
|
293
|
+
type NoteHeadSpan = {
|
|
294
|
+
getNoteHeadBeginX?: () => number;
|
|
295
|
+
getNoteHeadEndX?: () => number;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Center of a formatted note's visual notehead span. Falls back to the block
|
|
300
|
+
* center when the getters are missing or the reported span lands outside the
|
|
301
|
+
* note block.
|
|
302
|
+
*/
|
|
303
|
+
export function resolveItemHeadCenterX(
|
|
304
|
+
note: VFVoiceNote,
|
|
305
|
+
x: number,
|
|
306
|
+
width: number
|
|
307
|
+
): number {
|
|
308
|
+
const { getNoteHeadBeginX, getNoteHeadEndX } = note as NoteHeadSpan;
|
|
309
|
+
|
|
310
|
+
if (
|
|
311
|
+
typeof getNoteHeadBeginX === 'function' &&
|
|
312
|
+
typeof getNoteHeadEndX === 'function'
|
|
313
|
+
) {
|
|
314
|
+
const center =
|
|
315
|
+
(getNoteHeadBeginX.call(note) + getNoteHeadEndX.call(note)) / 2;
|
|
316
|
+
|
|
317
|
+
if (Number.isFinite(center) && center > x && center <= x + width) {
|
|
318
|
+
return center;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return x + width / 2;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function drawVoiceItems(
|
|
326
|
+
ctx: VexflowRecordingContext,
|
|
327
|
+
items: VoiceItem[],
|
|
328
|
+
notes: VFVoiceNote[]
|
|
329
|
+
) {
|
|
330
|
+
items.forEach((item, index) => {
|
|
331
|
+
const note = notes[index];
|
|
332
|
+
|
|
333
|
+
if (!note) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
ctx.beginColorGroup(item.id);
|
|
338
|
+
|
|
339
|
+
try {
|
|
340
|
+
note.setContext(ctx).drawWithStyle();
|
|
341
|
+
} finally {
|
|
342
|
+
ctx.endColorGroup();
|
|
343
|
+
}
|
|
178
344
|
});
|
|
179
345
|
}
|
|
180
346
|
|
|
@@ -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,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Accidental as VFAccidental,
|
|
3
|
+
Articulation as VFArticulation,
|
|
3
4
|
Beam,
|
|
4
5
|
Dot,
|
|
5
6
|
Fraction as VFFraction,
|
|
6
7
|
GhostNote,
|
|
8
|
+
ModifierPosition,
|
|
9
|
+
Parenthesis,
|
|
7
10
|
StaveNote,
|
|
8
11
|
Stem,
|
|
9
12
|
Tuplet,
|
|
@@ -12,10 +15,13 @@ import {
|
|
|
12
15
|
import type { StemmableNote } from 'vexflow';
|
|
13
16
|
|
|
14
17
|
import type {
|
|
18
|
+
Articulation,
|
|
15
19
|
Clef,
|
|
16
20
|
KeySignature,
|
|
17
21
|
DurationValue,
|
|
18
22
|
Meter,
|
|
23
|
+
NoteAttachment,
|
|
24
|
+
Notehead,
|
|
19
25
|
Pitch,
|
|
20
26
|
Score,
|
|
21
27
|
Staff,
|
|
@@ -45,8 +51,39 @@ export type VFVoiceNote = StemmableNote;
|
|
|
45
51
|
|
|
46
52
|
export interface MakeVFVoiceOptions {
|
|
47
53
|
resolveClef?: (item: VoiceItem) => Clef;
|
|
54
|
+
/**
|
|
55
|
+
* Precomputed attachment index; pass it when calling in a loop to avoid
|
|
56
|
+
* rebuilding it on every call.
|
|
57
|
+
*/
|
|
58
|
+
attachmentsByOwner?: Map<string, NoteAttachment[]>;
|
|
48
59
|
}
|
|
49
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Maps library notehead names to VexFlow key glyph codes.
|
|
63
|
+
*/
|
|
64
|
+
export const NOTEHEAD_TO_VF_CODE: Record<Notehead, string> = {
|
|
65
|
+
'x': 'x',
|
|
66
|
+
'circle-x': 'cx',
|
|
67
|
+
'diamond': 'h',
|
|
68
|
+
'circle': 'ci',
|
|
69
|
+
'square': 'sq',
|
|
70
|
+
'triangle': 'tu',
|
|
71
|
+
'triangle-down': 'td',
|
|
72
|
+
'slash': 'sf',
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Maps library articulation names to VexFlow articulation codes.
|
|
77
|
+
*/
|
|
78
|
+
export const ARTICULATION_TO_VF_CODE: Record<Articulation, string> = {
|
|
79
|
+
staccato: 'a.',
|
|
80
|
+
staccatissimo: 'av',
|
|
81
|
+
tenuto: 'a-',
|
|
82
|
+
accent: 'a>',
|
|
83
|
+
marcato: 'a^',
|
|
84
|
+
fermata: 'a@',
|
|
85
|
+
};
|
|
86
|
+
|
|
50
87
|
/**
|
|
51
88
|
* Maps the library stem direction to the numeric VexFlow value.
|
|
52
89
|
*/
|
|
@@ -73,7 +110,13 @@ export function pitchToVFKey(pitch: Pitch): string {
|
|
|
73
110
|
? 'd#'
|
|
74
111
|
: pitch.accidental ?? '';
|
|
75
112
|
|
|
76
|
-
|
|
113
|
+
const key = `${pitch.step.toLowerCase()}${accidental}/${pitch.octave}`;
|
|
114
|
+
|
|
115
|
+
if (pitch.notehead) {
|
|
116
|
+
return `${key}/${NOTEHEAD_TO_VF_CODE[pitch.notehead]}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return key;
|
|
77
120
|
}
|
|
78
121
|
|
|
79
122
|
/**
|
|
@@ -85,15 +128,11 @@ export function durationToVF(duration: DurationValue, isRest = false): string {
|
|
|
85
128
|
? '1/2'
|
|
86
129
|
: duration.length;
|
|
87
130
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (duration.dots) {
|
|
93
|
-
return `${length}${'d'.repeat(duration.dots)}`;
|
|
94
|
-
}
|
|
131
|
+
// Dots drive VexFlow's tick math, so rests need them in the token too or
|
|
132
|
+
// strict voices come up short.
|
|
133
|
+
const dots = 'd'.repeat(duration.dots ?? 0);
|
|
95
134
|
|
|
96
|
-
return length
|
|
135
|
+
return `${length}${dots}${isRest ? 'r' : ''}`;
|
|
97
136
|
}
|
|
98
137
|
|
|
99
138
|
/**
|
|
@@ -125,10 +164,74 @@ export function addPitchAccidentals(note: StaveNote, pitches: Pitch[]) {
|
|
|
125
164
|
});
|
|
126
165
|
}
|
|
127
166
|
|
|
167
|
+
/**
|
|
168
|
+
* Wraps each ghost pitch of a note in parentheses.
|
|
169
|
+
*/
|
|
170
|
+
export function applyGhostParentheses(
|
|
171
|
+
note: StaveNote,
|
|
172
|
+
pitches: readonly Pitch[]
|
|
173
|
+
) {
|
|
174
|
+
pitches.forEach((pitch, index) => {
|
|
175
|
+
if (!pitch.ghost) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
note.addModifier(new Parenthesis(ModifierPosition.LEFT), index);
|
|
180
|
+
note.addModifier(new Parenthesis(ModifierPosition.RIGHT), index);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function applyArticulations(
|
|
185
|
+
note: StaveNote,
|
|
186
|
+
attachments: NoteAttachment[] | undefined
|
|
187
|
+
) {
|
|
188
|
+
if (!attachments) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
for (const attachment of attachments) {
|
|
193
|
+
if (attachment.type !== 'articulation') {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const articulation = new VFArticulation(
|
|
198
|
+
ARTICULATION_TO_VF_CODE[attachment.articulation]
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
if (attachment.placement === 'below') {
|
|
202
|
+
articulation.setPosition(ModifierPosition.BELOW);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
note.addModifier(articulation, 0);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function indexAttachmentsByOwner(
|
|
210
|
+
score: Score
|
|
211
|
+
): Map<string, NoteAttachment[]> {
|
|
212
|
+
const attachmentsByOwner = new Map<string, NoteAttachment[]>();
|
|
213
|
+
|
|
214
|
+
for (const attachment of score.attachments ?? []) {
|
|
215
|
+
const ownedAttachments = attachmentsByOwner.get(attachment.ownerId);
|
|
216
|
+
|
|
217
|
+
if (ownedAttachments) {
|
|
218
|
+
ownedAttachments.push(attachment);
|
|
219
|
+
} else {
|
|
220
|
+
attachmentsByOwner.set(attachment.ownerId, [attachment]);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return attachmentsByOwner;
|
|
225
|
+
}
|
|
226
|
+
|
|
128
227
|
/**
|
|
129
228
|
* Builds a VexFlow stave note from a score voice item and clef.
|
|
130
229
|
*/
|
|
131
|
-
export function voiceItemToStaveNote(
|
|
230
|
+
export function voiceItemToStaveNote(
|
|
231
|
+
item: VoiceItem,
|
|
232
|
+
clef: Clef,
|
|
233
|
+
attachments?: NoteAttachment[]
|
|
234
|
+
): VFVoiceNote {
|
|
132
235
|
if (item.type === 'rest') {
|
|
133
236
|
if (item.kind === 'hidden' || item.kind === 'spacer') {
|
|
134
237
|
return new GhostNote(durationToVF(item.duration));
|
|
@@ -151,6 +254,8 @@ export function voiceItemToStaveNote(item: VoiceItem, clef: Clef): VFVoiceNote {
|
|
|
151
254
|
stemDirection: toVFStemDirection(item.stemDirection),
|
|
152
255
|
});
|
|
153
256
|
addPitchAccidentals(note, [item.pitch]);
|
|
257
|
+
applyGhostParentheses(note, [item.pitch]);
|
|
258
|
+
applyArticulations(note, attachments);
|
|
154
259
|
applyDots(note, item.duration.dots);
|
|
155
260
|
return note;
|
|
156
261
|
}
|
|
@@ -162,6 +267,8 @@ export function voiceItemToStaveNote(item: VoiceItem, clef: Clef): VFVoiceNote {
|
|
|
162
267
|
stemDirection: toVFStemDirection(item.stemDirection),
|
|
163
268
|
});
|
|
164
269
|
addPitchAccidentals(note, item.pitches);
|
|
270
|
+
applyGhostParentheses(note, item.pitches);
|
|
271
|
+
applyArticulations(note, attachments);
|
|
165
272
|
applyDots(note, item.duration.dots);
|
|
166
273
|
return note;
|
|
167
274
|
}
|
|
@@ -248,8 +355,14 @@ export function makeVFVoice(
|
|
|
248
355
|
beams: Beam[];
|
|
249
356
|
tuplets: Tuplet[];
|
|
250
357
|
} {
|
|
358
|
+
const attachmentsByOwner =
|
|
359
|
+
options.attachmentsByOwner ?? indexAttachmentsByOwner(score);
|
|
251
360
|
const notes = voice.items.map((item) =>
|
|
252
|
-
voiceItemToStaveNote(
|
|
361
|
+
voiceItemToStaveNote(
|
|
362
|
+
item,
|
|
363
|
+
options.resolveClef?.(item) ?? clef,
|
|
364
|
+
attachmentsByOwner.get(item.id)
|
|
365
|
+
)
|
|
253
366
|
);
|
|
254
367
|
|
|
255
368
|
const noteByItemId = new Map<string, VFVoiceNote>();
|
|
@@ -257,6 +370,28 @@ export function makeVFVoice(
|
|
|
257
370
|
noteByItemId.set(item.id, notes[index]!)
|
|
258
371
|
);
|
|
259
372
|
|
|
373
|
+
const tuplets = findTupletsForVoice(score, voice)
|
|
374
|
+
.map((group) => {
|
|
375
|
+
const tupletNotes = group.itemIds
|
|
376
|
+
.map((id) => noteByItemId.get(id))
|
|
377
|
+
.filter((note): note is StaveNote => Boolean(note));
|
|
378
|
+
|
|
379
|
+
if (tupletNotes.length < 2) {
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return new Tuplet(tupletNotes, {
|
|
384
|
+
numNotes: group.ratio.num,
|
|
385
|
+
notesOccupied: group.ratio.den,
|
|
386
|
+
bracketed: group.bracketed,
|
|
387
|
+
location:
|
|
388
|
+
group.placement === 'below'
|
|
389
|
+
? Tuplet.LOCATION_BOTTOM
|
|
390
|
+
: Tuplet.LOCATION_TOP,
|
|
391
|
+
});
|
|
392
|
+
})
|
|
393
|
+
.filter((tuplet): tuplet is Tuplet => Boolean(tuplet));
|
|
394
|
+
|
|
260
395
|
const vfVoice = new VFVoice({
|
|
261
396
|
numBeats: meter.beats,
|
|
262
397
|
beatValue: meter.beatUnit,
|
|
@@ -285,28 +420,6 @@ export function makeVFVoice(
|
|
|
285
420
|
: undefined;
|
|
286
421
|
const beams = Beam.generateBeams(notes, beamOptions);
|
|
287
422
|
|
|
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
423
|
return { vfVoice, notes, beams, tuplets };
|
|
311
424
|
}
|
|
312
425
|
|