vexflow 5.1.2 → 5.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 +7 -6
- package/build/cjs/vexflow-bravura.js +2 -2
- package/build/cjs/vexflow-bravura.js.map +1 -1
- package/build/cjs/vexflow-core.js +2 -2
- package/build/cjs/vexflow-core.js.map +1 -1
- package/build/cjs/vexflow-debug-with-tests.js +5325 -1730
- package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
- package/build/cjs/vexflow-debug.js +4228 -1545
- package/build/cjs/vexflow-debug.js.map +1 -1
- package/build/cjs/vexflow.js +2 -2
- package/build/cjs/vexflow.js.map +1 -1
- package/build/esm/src/accidental.js +13 -3
- package/build/esm/src/articulation.js +51 -20
- package/build/esm/src/beam.js +170 -30
- package/build/esm/src/bend.js +24 -19
- package/build/esm/src/clef.js +3 -0
- package/build/esm/src/clefnote.js +5 -1
- package/build/esm/src/curve.js +27 -53
- package/build/esm/src/dot.js +14 -5
- package/build/esm/src/easyscore/composition.js +336 -0
- package/build/esm/src/easyscore/elementindex.js +81 -0
- package/build/esm/src/easyscore/elements.js +324 -0
- package/build/esm/src/easyscore/policies.js +97 -0
- package/build/esm/src/easyscore/rhythm.js +42 -0
- package/build/esm/src/easyscore.js +448 -26
- package/build/esm/src/element.js +17 -2
- package/build/esm/src/engravinggeometry.js +150 -0
- package/build/esm/src/engravingprofile.js +44 -0
- package/build/esm/src/factory.js +268 -64
- package/build/esm/src/formatter.js +40 -4
- package/build/esm/src/gracenote.js +43 -38
- package/build/esm/src/gracenotegroup.js +53 -56
- package/build/esm/src/index.js +2 -0
- package/build/esm/src/keysignature.js +10 -2
- package/build/esm/src/keysignote.js +2 -2
- package/build/esm/src/modifiercontext.js +11 -0
- package/build/esm/src/note.js +5 -0
- package/build/esm/src/notehead.js +11 -6
- package/build/esm/src/ornament.js +38 -18
- package/build/esm/src/parenthesis.js +1 -1
- package/build/esm/src/parser.js +6 -1
- package/build/esm/src/proportionalformatter.js +234 -0
- package/build/esm/src/registry.js +27 -0
- package/build/esm/src/sheet.js +20 -3
- package/build/esm/src/stave.js +9 -2
- package/build/esm/src/stavenote.js +140 -127
- package/build/esm/src/stavetie.js +39 -7
- package/build/esm/src/stem.js +26 -11
- package/build/esm/src/stemmablenote.js +6 -1
- package/build/esm/src/tickable.js +5 -0
- package/build/esm/src/tickcontext.js +19 -0
- package/build/esm/src/timesignature.js +5 -0
- package/build/esm/src/tuplet.js +60 -19
- package/build/esm/src/version.js +3 -3
- package/build/esm/src/vexflow.js +6 -1
- package/build/esm/tests/beam_tests.js +35 -0
- package/build/esm/tests/bend_tests.js +14 -1
- package/build/esm/tests/curve_tests.js +14 -4
- package/build/esm/tests/demo_repertoire_tests.js +27 -0
- package/build/esm/tests/demos/drums.js +54 -0
- package/build/esm/tests/demos/electric.js +63 -0
- package/build/esm/tests/demos/greensleeves.js +90 -0
- package/build/esm/tests/demos/joy.js +61 -0
- package/build/esm/tests/demos/kesh.js +80 -0
- package/build/esm/tests/demos/prelude.js +59 -0
- package/build/esm/tests/demos/quartertones.js +50 -0
- package/build/esm/tests/demos/saints.js +65 -0
- package/build/esm/tests/easyscore_tests.js +40 -0
- package/build/esm/tests/easyscore_types.js +65 -0
- package/build/esm/tests/engraving/score-layout.mjs +75 -0
- package/build/esm/tests/engraving/score-persistence.mjs +262 -0
- package/build/esm/tests/engraving/studies.mjs +279 -0
- package/build/esm/tests/formatter_tests.js +51 -1
- package/build/esm/tests/index.js +1 -0
- package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
- package/build/esm/tests/node/easyscore-rendering-tests.mjs +332 -0
- package/build/esm/tests/node/easyscore-tests.mjs +1024 -0
- package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
- package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
- package/build/esm/tests/node/proportional-spacing-tests.mjs +278 -0
- package/build/esm/tests/node/rendering-tests.mjs +6 -2
- package/build/esm/tests/threevoice_tests.js +11 -1
- package/build/esm/tests/vexflow_test_helpers.js +19 -10
- package/build/esm/tools/audit-dependencies.mjs +61 -0
- package/build/esm/tools/easyscore-api-audit.mjs +58 -0
- package/build/esm/tools/easyscore-composition.mjs +62 -0
- package/build/esm/tools/easyscore-example.mjs +16 -0
- package/build/esm/tools/easyscore-families.mjs +61 -0
- package/build/esm/tools/easyscore-references-example.mjs +26 -0
- package/build/esm/tools/easyscore-rhythm.mjs +58 -0
- package/build/esm/tools/easyscore-structure.mjs +88 -0
- package/build/esm/tools/explorer/catalog/entries.mjs +6 -0
- package/build/esm/tools/explorer/catalog/expression.mjs +68 -0
- package/build/esm/tools/explorer/catalog/helpers.mjs +29 -0
- package/build/esm/tools/explorer/catalog/layout.mjs +109 -0
- package/build/esm/tools/explorer/catalog/notes.mjs +61 -0
- package/build/esm/tools/explorer/catalog/staves.mjs +85 -0
- package/build/esm/tools/explorer/catalog/strings.mjs +25 -0
- package/build/esm/tools/explorer/catalog.mjs +14 -0
- package/build/esm/tools/score-fixtures.mjs +23 -6
- package/build/types/src/accidental.d.ts +6 -0
- package/build/types/src/articulation.d.ts +12 -1
- package/build/types/src/beam.d.ts +16 -0
- package/build/types/src/clef.d.ts +2 -0
- package/build/types/src/curve.d.ts +9 -0
- package/build/types/src/dot.d.ts +5 -0
- package/build/types/src/easyscore/composition.d.ts +126 -0
- package/build/types/src/easyscore/elementindex.d.ts +30 -0
- package/build/types/src/easyscore/elements.d.ts +156 -0
- package/build/types/src/easyscore/policies.d.ts +12 -0
- package/build/types/src/easyscore/rhythm.d.ts +9 -0
- package/build/types/src/easyscore.d.ts +153 -11
- package/build/types/src/element.d.ts +7 -1
- package/build/types/src/engravinggeometry.d.ts +31 -0
- package/build/types/src/engravingprofile.d.ts +20 -0
- package/build/types/src/factory.d.ts +97 -9
- package/build/types/src/formatter.d.ts +7 -1
- package/build/types/src/gracenote.d.ts +7 -0
- package/build/types/src/gracenotegroup.d.ts +7 -2
- package/build/types/src/index.d.ts +2 -0
- package/build/types/src/keysignature.d.ts +3 -0
- package/build/types/src/keysignote.d.ts +1 -1
- package/build/types/src/modifiercontext.d.ts +4 -0
- package/build/types/src/note.d.ts +2 -0
- package/build/types/src/notehead.d.ts +5 -0
- package/build/types/src/ornament.d.ts +7 -0
- package/build/types/src/parser.d.ts +5 -0
- package/build/types/src/proportionalformatter.d.ts +39 -0
- package/build/types/src/registry.d.ts +4 -0
- package/build/types/src/stave.d.ts +2 -0
- package/build/types/src/stavenote.d.ts +21 -3
- package/build/types/src/stavetie.d.ts +4 -0
- package/build/types/src/stem.d.ts +10 -1
- package/build/types/src/stemmablenote.d.ts +6 -1
- package/build/types/src/tickable.d.ts +2 -0
- package/build/types/src/tickcontext.d.ts +5 -0
- package/build/types/src/timesignature.d.ts +2 -0
- package/build/types/src/tuplet.d.ts +5 -48
- package/build/types/src/vexflow.d.ts +6 -1
- package/changelog/CHANGELOG.md +52 -0
- package/package.json +13 -3
- package/tests/all.html +277 -0
- package/tests/demo_petzold_tests.ts +435 -0
- package/tests/demos/drums.ts +55 -0
- package/tests/demos/drums.webp +0 -0
- package/tests/demos/electric.ts +70 -0
- package/tests/demos/electric.webp +0 -0
- package/tests/demos/greensleeves.ts +97 -0
- package/tests/demos/greensleeves.webp +0 -0
- package/tests/demos/joy.ts +65 -0
- package/tests/demos/joy.webp +0 -0
- package/tests/demos/kesh.ts +86 -0
- package/tests/demos/kesh.webp +0 -0
- package/tests/demos/prelude.ts +65 -0
- package/tests/demos/prelude.webp +0 -0
- package/tests/demos/quartertones.ts +51 -0
- package/tests/demos/quartertones.webp +0 -0
- package/tests/demos/saints.ts +70 -0
- package/tests/demos/saints.webp +0 -0
- package/tests/flow.html +550 -239
- package/tests/fonts.css +493 -0
- package/tests/minuet.webp +0 -0
package/build/esm/src/dot.js
CHANGED
|
@@ -82,7 +82,7 @@ export class Dot extends Modifier {
|
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
if (note.isRest()) {
|
|
85
|
-
dot.dotShiftY
|
|
85
|
+
dot.dotShiftY = dot.restShiftY - halfShiftY;
|
|
86
86
|
}
|
|
87
87
|
else {
|
|
88
88
|
dot.dotShiftY = -halfShiftY;
|
|
@@ -99,23 +99,23 @@ export class Dot extends Modifier {
|
|
|
99
99
|
}
|
|
100
100
|
constructor() {
|
|
101
101
|
super();
|
|
102
|
+
this.restShiftY = 0;
|
|
102
103
|
this.position = Modifier.Position.RIGHT;
|
|
103
104
|
this.setText(Glyphs.augmentationDot);
|
|
104
105
|
this.dotShiftY = 0;
|
|
105
106
|
}
|
|
106
107
|
setNote(note) {
|
|
107
108
|
this.note = note;
|
|
108
|
-
this.
|
|
109
|
+
this.setFont(note.fontInfo);
|
|
109
110
|
return this;
|
|
110
111
|
}
|
|
111
112
|
setDotShiftY(y) {
|
|
113
|
+
this.restShiftY = y;
|
|
112
114
|
this.dotShiftY = y;
|
|
113
115
|
return this;
|
|
114
116
|
}
|
|
115
|
-
|
|
116
|
-
const ctx = this.checkContext();
|
|
117
|
+
positionGlyph() {
|
|
117
118
|
const note = this.checkAttachedNote();
|
|
118
|
-
this.setRendered();
|
|
119
119
|
const stave = note.checkStave();
|
|
120
120
|
const lineSpace = stave.getSpacingBetweenLines();
|
|
121
121
|
const start = note.getModifierStartXY(this.position, this.index, { forceFlagRight: true });
|
|
@@ -124,6 +124,15 @@ export class Dot extends Modifier {
|
|
|
124
124
|
}
|
|
125
125
|
this.x = start.x;
|
|
126
126
|
this.y = start.y + this.dotShiftY * lineSpace;
|
|
127
|
+
}
|
|
128
|
+
getInkBounds() {
|
|
129
|
+
this.positionGlyph();
|
|
130
|
+
return this.getTextInk(this.x, this.y);
|
|
131
|
+
}
|
|
132
|
+
draw() {
|
|
133
|
+
const ctx = this.checkContext();
|
|
134
|
+
this.positionGlyph();
|
|
135
|
+
this.setRendered();
|
|
127
136
|
this.renderText(ctx, 0, 0);
|
|
128
137
|
}
|
|
129
138
|
}
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { Factory } from '../factory.js';
|
|
13
|
+
import { Fraction } from '../fraction.js';
|
|
14
|
+
import { Modifier } from '../modifier.js';
|
|
15
|
+
import { Note } from '../note.js';
|
|
16
|
+
import { StemmableNote } from '../stemmablenote.js';
|
|
17
|
+
import { Tickable } from '../tickable.js';
|
|
18
|
+
import { RuntimeError } from '../util.js';
|
|
19
|
+
import { Voice } from '../voice.js';
|
|
20
|
+
import { applyCompositionAccidentals } from './policies.js';
|
|
21
|
+
function buildGraceGroup(score, { side, notes, slur, beam }) {
|
|
22
|
+
if (!['before', 'after'].includes(side) || !notes.length)
|
|
23
|
+
invalid('Expected a nonempty before or after grace group.');
|
|
24
|
+
const children = notes.map((_a) => {
|
|
25
|
+
var { accidentals } = _a, spec = __rest(_a, ["accidentals"]);
|
|
26
|
+
const child = score.make.graceNote(spec);
|
|
27
|
+
for (const { index, type, cautionary } of accidentals !== null && accidentals !== void 0 ? accidentals : []) {
|
|
28
|
+
const mark = score.make.accidental({ type });
|
|
29
|
+
if (cautionary)
|
|
30
|
+
mark.setAsCautionary();
|
|
31
|
+
score.attach(child, mark, index);
|
|
32
|
+
}
|
|
33
|
+
return child;
|
|
34
|
+
});
|
|
35
|
+
const group = score.make.graceNoteGroup({ notes: children, slur });
|
|
36
|
+
group.setPosition(side === 'after' ? Modifier.Position.RIGHT : Modifier.Position.LEFT);
|
|
37
|
+
if (beam)
|
|
38
|
+
group.beamNotes();
|
|
39
|
+
return group;
|
|
40
|
+
}
|
|
41
|
+
function invalid(message) {
|
|
42
|
+
throw new RuntimeError('BadComposition', message);
|
|
43
|
+
}
|
|
44
|
+
function ticksFor(time) {
|
|
45
|
+
return new Voice(time).getTotalTicks().clone();
|
|
46
|
+
}
|
|
47
|
+
function buildVoice(score, spec, initial, display) {
|
|
48
|
+
var _a;
|
|
49
|
+
return score.withDefaults({ clef: initial.clef, time: initial.time, stem: (_a = spec.stem) !== null && _a !== void 0 ? _a : 'auto' }, () => {
|
|
50
|
+
var _a;
|
|
51
|
+
let state = Object.assign({}, initial);
|
|
52
|
+
const notes = [];
|
|
53
|
+
const changes = [];
|
|
54
|
+
const assignments = new Map();
|
|
55
|
+
const generated = new Set();
|
|
56
|
+
for (const item of typeof spec.notes === 'string' ? [spec.notes] : spec.notes) {
|
|
57
|
+
if (typeof item === 'string') {
|
|
58
|
+
notes.push(...score.notes(item));
|
|
59
|
+
score.builder
|
|
60
|
+
.getElements()
|
|
61
|
+
.accidentals.flat()
|
|
62
|
+
.forEach((mark) => {
|
|
63
|
+
if (mark)
|
|
64
|
+
generated.add(mark);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
else if (item instanceof Tickable) {
|
|
68
|
+
if (Factory.getOwner(item) !== score.factory)
|
|
69
|
+
invalid('Adopt native notes before composing them.');
|
|
70
|
+
try {
|
|
71
|
+
item.getVoice();
|
|
72
|
+
invalid('Composition requires notes that are not already in a voice.');
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (!(error instanceof RuntimeError) || error.code !== 'NoVoice')
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
notes.push(item);
|
|
79
|
+
}
|
|
80
|
+
else if ('change' in item) {
|
|
81
|
+
const previous = state;
|
|
82
|
+
state = Object.assign(Object.assign({}, state), item.change);
|
|
83
|
+
changes.push({ index: notes.length, state: Object.assign({}, state) });
|
|
84
|
+
if (display) {
|
|
85
|
+
if (item.change.clef)
|
|
86
|
+
notes.push(score.make.clefNote({ type: state.clef, options: { size: 'small' } }));
|
|
87
|
+
if (item.change.key)
|
|
88
|
+
notes.push(score.make.keySigNote({ key: state.key, cancelKey: previous.key, clef: state.clef }));
|
|
89
|
+
if (item.change.time)
|
|
90
|
+
notes.push(score.make.timeSigNote({ time: state.time }));
|
|
91
|
+
}
|
|
92
|
+
score.set({ clef: state.clef, time: state.time });
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
const note = 'tab' in item
|
|
96
|
+
? score.make.tabNote(item.tab, item.drawStem)
|
|
97
|
+
: score.make.note('note' in item ? item.note : item);
|
|
98
|
+
if ('grace' in item && item.grace) {
|
|
99
|
+
score.attach(note, buildGraceGroup(score, item.grace), 0);
|
|
100
|
+
}
|
|
101
|
+
if ('id' in item && item.id !== undefined)
|
|
102
|
+
note.setAttribute('id', item.id);
|
|
103
|
+
if ('stave' in item && item.stave)
|
|
104
|
+
assignments.set(note, item.stave);
|
|
105
|
+
notes.push(note);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const timed = notes.filter((note) => !note.shouldIgnoreTicks());
|
|
109
|
+
for (const group of (_a = spec.tuplets) !== null && _a !== void 0 ? _a : []) {
|
|
110
|
+
if (!Number.isInteger(group.start) ||
|
|
111
|
+
!Number.isInteger(group.count) ||
|
|
112
|
+
group.start < 0 ||
|
|
113
|
+
group.count < 1 ||
|
|
114
|
+
group.start + group.count > timed.length)
|
|
115
|
+
invalid('Tuplet range is outside its voice.');
|
|
116
|
+
const members = timed.slice(group.start, group.start + group.count);
|
|
117
|
+
if (!members.every((note) => note instanceof Note))
|
|
118
|
+
invalid('Tuplets require native notes.');
|
|
119
|
+
score.make.tuplet({ notes: members, options: group.options });
|
|
120
|
+
}
|
|
121
|
+
if (spec.autoBeam) {
|
|
122
|
+
if (!timed.every((note) => note instanceof StemmableNote))
|
|
123
|
+
invalid('Automatic beams require stemmable notes.');
|
|
124
|
+
score.set({ time: initial.time });
|
|
125
|
+
score.autoBeam(timed, typeof spec.autoBeam === 'boolean' ? {} : spec.autoBeam);
|
|
126
|
+
}
|
|
127
|
+
return { notes, state, changes, assignments, generated };
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function stateChanges(voice) {
|
|
131
|
+
return voice.changes.map(({ index, state }) => ({
|
|
132
|
+
at: voice.notes
|
|
133
|
+
.slice(0, index)
|
|
134
|
+
.reduce((sum, note) => (note.shouldIgnoreTicks() ? sum : sum.add(note.getTicks())), new Fraction(0, 1)),
|
|
135
|
+
state,
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
function addSignatures(score, stave, state, previous) {
|
|
139
|
+
if (!previous || previous.clef !== state.clef)
|
|
140
|
+
score.attachToStave(stave, score.make.clef(state.clef));
|
|
141
|
+
if (state.clef !== 'tab' && (!previous || previous.key !== state.key))
|
|
142
|
+
score.attachToStave(stave, score.make.keySignature(state.key, previous === null || previous === void 0 ? void 0 : previous.key));
|
|
143
|
+
if (!previous || previous.time !== state.time)
|
|
144
|
+
score.attachToStave(stave, score.make.timeSignature(state.time));
|
|
145
|
+
}
|
|
146
|
+
function buildSpans(score, spec, measures, diagnostics) {
|
|
147
|
+
var _a;
|
|
148
|
+
return ((_a = spec.spans) !== null && _a !== void 0 ? _a : []).map((span) => {
|
|
149
|
+
const from = span.from === undefined ? undefined : score.getNote(span.from);
|
|
150
|
+
const to = span.to === undefined ? undefined : score.getNote(span.to);
|
|
151
|
+
for (const note of [from, to])
|
|
152
|
+
if (note && !measures.has(note))
|
|
153
|
+
invalid('Span endpoints must belong to this composition.');
|
|
154
|
+
if (from && to && measures.get(from) !== measures.get(to)) {
|
|
155
|
+
if (spec.crossMeasureSpans !== 'manual')
|
|
156
|
+
invalid('Cross-measure spans need explicit manual segmentation.');
|
|
157
|
+
diagnostics.push({
|
|
158
|
+
code: 'ManualSpan',
|
|
159
|
+
message: 'A cross-measure span will not split when Sheet wraps. Replace it with native partial segments before drawing across rows.',
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (span.kind === 'tie')
|
|
163
|
+
return score.make.tie(Object.assign(Object.assign({}, span), { from, to }));
|
|
164
|
+
if (span.kind === 'curve')
|
|
165
|
+
return score.make.curve({ from, to, options: span.options });
|
|
166
|
+
if (!from || !to)
|
|
167
|
+
invalid('Hairpins require both endpoints.');
|
|
168
|
+
return score.make.hairpin({ from, to, type: span.type, options: span.options });
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
function buildStaffMeasure(score, spec, staff, index, previous, tracking) {
|
|
172
|
+
var _a, _b, _c, _d;
|
|
173
|
+
const assignments = new Map();
|
|
174
|
+
const measure = staff.measures[index];
|
|
175
|
+
if (!measure.voices.length)
|
|
176
|
+
invalid('Every stave measure needs at least one voice.');
|
|
177
|
+
const state = {
|
|
178
|
+
clef: (_a = measure.clef) !== null && _a !== void 0 ? _a : previous.clef,
|
|
179
|
+
key: (_b = measure.key) !== null && _b !== void 0 ? _b : previous.key,
|
|
180
|
+
time: (_c = measure.time) !== null && _c !== void 0 ? _c : previous.time,
|
|
181
|
+
};
|
|
182
|
+
const span = (_d = measure.span) !== null && _d !== void 0 ? _d : state.time;
|
|
183
|
+
const ticks = ticksFor(span);
|
|
184
|
+
const built = measure.voices.map((voice, voiceIndex) => buildVoice(score, voice, state, voiceIndex === 0));
|
|
185
|
+
const changes = stateChanges(built[0]);
|
|
186
|
+
const signature = (items) => JSON.stringify(items.map(({ at, state }) => [at.clone().simplify().toString(), state]));
|
|
187
|
+
for (const voice of built) {
|
|
188
|
+
if (signature(stateChanges(voice)) !== signature(changes))
|
|
189
|
+
invalid('Inline staff changes must agree in all voices.');
|
|
190
|
+
const actual = voice.notes.reduce((sum, note) => (note.shouldIgnoreTicks() ? sum : sum.add(note.getTicks())), new Fraction(0, 1));
|
|
191
|
+
if (!actual.equals(ticks))
|
|
192
|
+
invalid(`Measure ${index + 1} on ${staff.id} does not fill its declared span.`);
|
|
193
|
+
for (const note of voice.notes) {
|
|
194
|
+
if (tracking.seen.has(note))
|
|
195
|
+
invalid('A note cannot belong to two composition voices.');
|
|
196
|
+
tracking.seen.add(note);
|
|
197
|
+
if (note instanceof Note)
|
|
198
|
+
tracking.noteMeasures.set(note, index);
|
|
199
|
+
}
|
|
200
|
+
voice.assignments.forEach((id, note) => assignments.set(note, id));
|
|
201
|
+
}
|
|
202
|
+
const nativeVoices = built.map((voice, voiceIndex) => score.voice(voice.notes, Object.assign(Object.assign({}, measure.voices[voiceIndex].options), { time: span, mode: 'strict' })));
|
|
203
|
+
let currentKey = state.key;
|
|
204
|
+
const keys = [];
|
|
205
|
+
for (const change of changes)
|
|
206
|
+
if (change.state.key !== currentKey) {
|
|
207
|
+
currentKey = change.state.key;
|
|
208
|
+
keys.push({ at: change.at, key: currentKey });
|
|
209
|
+
}
|
|
210
|
+
const policy = {
|
|
211
|
+
key: state.key,
|
|
212
|
+
changes: keys,
|
|
213
|
+
generated: new Set(built.flatMap((voice) => [...voice.generated])),
|
|
214
|
+
};
|
|
215
|
+
const stave = staff.kind === 'tab'
|
|
216
|
+
? score.make.tabStave({ options: staff.options })
|
|
217
|
+
: score.make.stave({ options: Object.assign(Object.assign({}, staff.options), { clef: state.clef }) });
|
|
218
|
+
addSignatures(score, stave, state, index && spec.signatures !== 'every-measure' ? previous : undefined);
|
|
219
|
+
return {
|
|
220
|
+
stave,
|
|
221
|
+
voices: nativeVoices,
|
|
222
|
+
notes: built.map((voice) => voice.notes),
|
|
223
|
+
assignments,
|
|
224
|
+
policy,
|
|
225
|
+
ticks,
|
|
226
|
+
state: built[0].state,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
export function compose(score, spec) {
|
|
230
|
+
var _a, _b;
|
|
231
|
+
const count = (_b = (_a = spec.staves[0]) === null || _a === void 0 ? void 0 : _a.measures.length) !== null && _b !== void 0 ? _b : 0;
|
|
232
|
+
if (!count || !spec.staves.length)
|
|
233
|
+
invalid('A composition needs staves and measures.');
|
|
234
|
+
if (new Set(spec.staves.map((stave) => stave.id)).size !== spec.staves.length ||
|
|
235
|
+
spec.staves.some((stave) => !stave.id))
|
|
236
|
+
invalid('Staff IDs must be unique and nonempty.');
|
|
237
|
+
if (spec.staves.some((stave) => stave.measures.length !== count))
|
|
238
|
+
invalid('All staves need the same number of measures.');
|
|
239
|
+
if (spec.accidentals && !['literal', 'key-aware'].includes(spec.accidentals))
|
|
240
|
+
invalid('Unknown accidental policy.');
|
|
241
|
+
if (spec.graceAccidentals && !['explicit', 'ordered'].includes(spec.graceAccidentals))
|
|
242
|
+
invalid('Unknown grace accidental policy.');
|
|
243
|
+
if (spec.graceAccidentals === 'ordered' && spec.accidentals !== 'key-aware')
|
|
244
|
+
invalid('Ordered grace accidentals require key-aware accidentals.');
|
|
245
|
+
if (spec.signatures && !['changes', 'every-measure'].includes(spec.signatures))
|
|
246
|
+
invalid('Unknown signature policy.');
|
|
247
|
+
if (spec.crossMeasureSpans && !['reject', 'manual'].includes(spec.crossMeasureSpans))
|
|
248
|
+
invalid('Unknown span policy.');
|
|
249
|
+
const nativeInputs = [];
|
|
250
|
+
for (const stave of spec.staves)
|
|
251
|
+
for (const measure of stave.measures)
|
|
252
|
+
for (const voice of measure.voices)
|
|
253
|
+
if (typeof voice.notes !== 'string')
|
|
254
|
+
for (const item of voice.notes)
|
|
255
|
+
if (item instanceof Note)
|
|
256
|
+
nativeInputs.push(item);
|
|
257
|
+
if (nativeInputs.length &&
|
|
258
|
+
(spec.accidentals === 'key-aware' ||
|
|
259
|
+
spec.staves.some((stave) => stave.measures.some((measure) => measure.voices.some((voice) => { var _a; return voice.autoBeam || ((_a = voice.tuplets) === null || _a === void 0 ? void 0 : _a.length); })))))
|
|
260
|
+
invalid('Apply rhythm and accidental policies before adopting native inputs, or use typed note descriptions.');
|
|
261
|
+
const priorStaves = new Map(nativeInputs.map((note) => [note, note.getStave()]));
|
|
262
|
+
const seen = new Set();
|
|
263
|
+
try {
|
|
264
|
+
return score.build(() => {
|
|
265
|
+
const result = {
|
|
266
|
+
sheet: score.sheet(spec.sheet),
|
|
267
|
+
systems: [],
|
|
268
|
+
staves: [],
|
|
269
|
+
voices: [],
|
|
270
|
+
notes: [],
|
|
271
|
+
spans: [],
|
|
272
|
+
diagnostics: [],
|
|
273
|
+
getElement: (id) => score.getElement(id),
|
|
274
|
+
getNote: (id) => score.getNote(id),
|
|
275
|
+
};
|
|
276
|
+
if (spec.signatures !== 'every-measure')
|
|
277
|
+
result.diagnostics.push({
|
|
278
|
+
code: 'SignaturePolicy',
|
|
279
|
+
message: 'Signatures appear at the first measure and explicit changes. Sheet does not repeat them at wrapped row starts.',
|
|
280
|
+
});
|
|
281
|
+
const states = spec.staves.map((stave) => {
|
|
282
|
+
var _a, _b, _c;
|
|
283
|
+
return ({
|
|
284
|
+
clef: (_a = stave.clef) !== null && _a !== void 0 ? _a : (stave.kind === 'tab' ? 'tab' : 'treble'),
|
|
285
|
+
key: (_b = stave.key) !== null && _b !== void 0 ? _b : 'C',
|
|
286
|
+
time: (_c = stave.time) !== null && _c !== void 0 ? _c : '4/4',
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
const noteMeasures = new Map();
|
|
290
|
+
for (let index = 0; index < count; index++) {
|
|
291
|
+
const system = result.sheet.addSystem(spec.system);
|
|
292
|
+
result.systems.push(system);
|
|
293
|
+
const parts = spec.staves.map((staff, staffIndex) => {
|
|
294
|
+
const part = buildStaffMeasure(score, spec, staff, index, states[staffIndex], { seen, noteMeasures });
|
|
295
|
+
states[staffIndex] = part.state;
|
|
296
|
+
system.addStave(Object.assign(Object.assign({ stave: part.stave, voices: part.voices }, (staff.spaceAbove === undefined ? {} : { spaceAbove: staff.spaceAbove })), (staff.spaceBelow === undefined ? {} : { spaceBelow: staff.spaceBelow })));
|
|
297
|
+
return part;
|
|
298
|
+
});
|
|
299
|
+
if (parts.some((part) => !part.ticks.equals(parts[0].ticks)))
|
|
300
|
+
invalid('Staves must share the same actual measure span.');
|
|
301
|
+
const staves = parts.map((part) => part.stave);
|
|
302
|
+
const voices = parts.map((part) => part.voices);
|
|
303
|
+
const notes = parts.map((part) => part.notes);
|
|
304
|
+
const policies = parts.map((part) => part.policy);
|
|
305
|
+
const assignments = new Map(parts.flatMap((part) => [...part.assignments]));
|
|
306
|
+
assignments.forEach((id, note) => {
|
|
307
|
+
const target = spec.staves.findIndex((staff) => staff.id === id);
|
|
308
|
+
if (target < 0)
|
|
309
|
+
invalid(`Unknown cross-staff target: ${id}`);
|
|
310
|
+
note.setStave(staves[target]);
|
|
311
|
+
});
|
|
312
|
+
if (spec.accidentals === 'key-aware') {
|
|
313
|
+
const generated = new Set(policies.flatMap((policy) => [...policy.generated]));
|
|
314
|
+
policies.forEach((policy, staffIndex) => applyCompositionAccidentals(score, voices.flat(), policy.key, policy.changes, generated, staves[staffIndex], spec.graceAccidentals === 'ordered'));
|
|
315
|
+
}
|
|
316
|
+
result.staves.push(staves);
|
|
317
|
+
result.voices.push(voices);
|
|
318
|
+
result.notes.push(notes);
|
|
319
|
+
}
|
|
320
|
+
result.spans = buildSpans(score, spec, noteMeasures, result.diagnostics);
|
|
321
|
+
return result;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
for (const note of nativeInputs)
|
|
326
|
+
if (seen.has(note)) {
|
|
327
|
+
note.clearVoice();
|
|
328
|
+
const stave = priorStaves.get(note);
|
|
329
|
+
if (stave)
|
|
330
|
+
note.setStave(stave);
|
|
331
|
+
else
|
|
332
|
+
note.clearStave();
|
|
333
|
+
}
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { RuntimeError } from '../util.js';
|
|
2
|
+
export class EasyScoreReferences {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.ids = new Map();
|
|
5
|
+
this.entries = new Map();
|
|
6
|
+
this.scopes = [];
|
|
7
|
+
}
|
|
8
|
+
static sourceOf(element) {
|
|
9
|
+
const source = this.sources.get(element);
|
|
10
|
+
return source && Object.assign({}, source);
|
|
11
|
+
}
|
|
12
|
+
get(id) {
|
|
13
|
+
return this.ids.get(id);
|
|
14
|
+
}
|
|
15
|
+
getSource(element) {
|
|
16
|
+
var _a;
|
|
17
|
+
const source = (_a = this.entries.get(element)) === null || _a === void 0 ? void 0 : _a.source;
|
|
18
|
+
return source && Object.assign({}, source);
|
|
19
|
+
}
|
|
20
|
+
validate(element, id) {
|
|
21
|
+
if (!id)
|
|
22
|
+
throw new RuntimeError('BadArguments', 'Score elements need a nonempty ID.');
|
|
23
|
+
const existing = this.ids.get(id);
|
|
24
|
+
if (existing && existing !== element)
|
|
25
|
+
throw new RuntimeError('BadArguments', `Duplicate score element ID: ${id}`);
|
|
26
|
+
}
|
|
27
|
+
add(element, source) {
|
|
28
|
+
var _a;
|
|
29
|
+
const previous = this.entries.get(element);
|
|
30
|
+
if (previous)
|
|
31
|
+
return element;
|
|
32
|
+
const id = element.getAttribute('id');
|
|
33
|
+
this.validate(element, id);
|
|
34
|
+
const entry = {
|
|
35
|
+
id,
|
|
36
|
+
source: source && Object.assign({}, source),
|
|
37
|
+
unsubscribe: element.onIdChange((next) => this.validate(element, next), () => {
|
|
38
|
+
this.ids.delete(entry.id);
|
|
39
|
+
entry.id = element.getAttribute('id');
|
|
40
|
+
this.ids.set(entry.id, element);
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
this.entries.set(element, entry);
|
|
44
|
+
this.ids.set(id, element);
|
|
45
|
+
if (entry.source)
|
|
46
|
+
EasyScoreReferences.sources.set(element, entry.source);
|
|
47
|
+
(_a = this.scopes[this.scopes.length - 1]) === null || _a === void 0 ? void 0 : _a.push(() => this.remove(element));
|
|
48
|
+
return element;
|
|
49
|
+
}
|
|
50
|
+
remove(element) {
|
|
51
|
+
const entry = this.entries.get(element);
|
|
52
|
+
if (!entry)
|
|
53
|
+
return;
|
|
54
|
+
entry.unsubscribe();
|
|
55
|
+
this.ids.delete(entry.id);
|
|
56
|
+
this.entries.delete(element);
|
|
57
|
+
if (entry.source === EasyScoreReferences.sources.get(element))
|
|
58
|
+
EasyScoreReferences.sources.delete(element);
|
|
59
|
+
}
|
|
60
|
+
clear() {
|
|
61
|
+
for (const element of this.entries.keys())
|
|
62
|
+
this.remove(element);
|
|
63
|
+
}
|
|
64
|
+
transaction(build) {
|
|
65
|
+
var _a;
|
|
66
|
+
const undo = [];
|
|
67
|
+
this.scopes.push(undo);
|
|
68
|
+
try {
|
|
69
|
+
const result = build();
|
|
70
|
+
this.scopes.pop();
|
|
71
|
+
(_a = this.scopes[this.scopes.length - 1]) === null || _a === void 0 ? void 0 : _a.push(...undo);
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
this.scopes.pop();
|
|
76
|
+
undo.reverse().forEach((revert) => revert());
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
EasyScoreReferences.sources = new WeakMap();
|