vexflow 5.2.0 → 5.4.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.
Files changed (189) hide show
  1. package/README.md +2 -0
  2. package/build/cjs/vexflow-bravura.js +2 -2
  3. package/build/cjs/vexflow-bravura.js.map +1 -1
  4. package/build/cjs/vexflow-core.js +2 -2
  5. package/build/cjs/vexflow-core.js.map +1 -1
  6. package/build/cjs/vexflow-debug-with-tests.js +6502 -1708
  7. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  8. package/build/cjs/vexflow-debug.js +5622 -1618
  9. package/build/cjs/vexflow-debug.js.map +1 -1
  10. package/build/cjs/vexflow.js +2 -2
  11. package/build/cjs/vexflow.js.map +1 -1
  12. package/build/esm/src/accidental.js +10 -3
  13. package/build/esm/src/alternatingtremolo.js +68 -0
  14. package/build/esm/src/annotation.js +19 -3
  15. package/build/esm/src/articulation.js +28 -6
  16. package/build/esm/src/beam.js +171 -28
  17. package/build/esm/src/bend.js +79 -23
  18. package/build/esm/src/clef.js +3 -0
  19. package/build/esm/src/continuouslayout.js +681 -0
  20. package/build/esm/src/continuousspan.js +159 -0
  21. package/build/esm/src/curve.js +48 -55
  22. package/build/esm/src/dot.js +13 -4
  23. package/build/esm/src/easyscore/composition.js +52 -2
  24. package/build/esm/src/easyscore/policies.js +39 -8
  25. package/build/esm/src/element.js +4 -0
  26. package/build/esm/src/engravinggeometry.js +157 -0
  27. package/build/esm/src/engravingprofile.js +44 -0
  28. package/build/esm/src/factory.js +31 -11
  29. package/build/esm/src/font.js +9 -1
  30. package/build/esm/src/formatter.js +9 -1
  31. package/build/esm/src/fretdiagram.js +77 -0
  32. package/build/esm/src/gracenote.js +43 -38
  33. package/build/esm/src/gracenotegroup.js +53 -56
  34. package/build/esm/src/guitargesture.js +33 -0
  35. package/build/esm/src/guitarmodifiers.js +26 -0
  36. package/build/esm/src/index.js +10 -0
  37. package/build/esm/src/instrumentprofile.js +123 -0
  38. package/build/esm/src/keysignature.js +24 -2
  39. package/build/esm/src/modifiercontext.js +11 -0
  40. package/build/esm/src/notationspelling.js +73 -0
  41. package/build/esm/src/note.js +3 -0
  42. package/build/esm/src/notehead.js +11 -6
  43. package/build/esm/src/ornament.js +43 -18
  44. package/build/esm/src/pagebreaks.js +58 -0
  45. package/build/esm/src/pedalmarking.js +39 -17
  46. package/build/esm/src/phraselayout.js +503 -0
  47. package/build/esm/src/phraseribbon.js +28 -0
  48. package/build/esm/src/proportionalformatter.js +54 -6
  49. package/build/esm/src/publication.js +402 -0
  50. package/build/esm/src/publicationview.js +114 -0
  51. package/build/esm/src/scorenavigation.js +172 -0
  52. package/build/esm/src/sheet.js +14 -2
  53. package/build/esm/src/stave.js +33 -16
  54. package/build/esm/src/stavebarline.js +5 -7
  55. package/build/esm/src/staveconnector.js +1 -0
  56. package/build/esm/src/stavehairpin.js +53 -27
  57. package/build/esm/src/stavenote.js +134 -116
  58. package/build/esm/src/stavetie.js +66 -9
  59. package/build/esm/src/stem.js +26 -11
  60. package/build/esm/src/strokes.js +4 -0
  61. package/build/esm/src/system.js +19 -4
  62. package/build/esm/src/systembreaks.js +42 -0
  63. package/build/esm/src/tabnote.js +6 -1
  64. package/build/esm/src/textbracket.js +24 -5
  65. package/build/esm/src/tickcontext.js +19 -0
  66. package/build/esm/src/timesignature.js +20 -6
  67. package/build/esm/src/tuplet.js +58 -17
  68. package/build/esm/src/version.js +3 -3
  69. package/build/esm/src/vexflow.js +20 -0
  70. package/build/esm/src/voice.js +7 -3
  71. package/build/esm/tests/bend_tests.js +14 -1
  72. package/build/esm/tests/curve_tests.js +14 -4
  73. package/build/esm/tests/demo_repertoire_tests.js +27 -0
  74. package/build/esm/tests/demos/drums.js +54 -0
  75. package/build/esm/tests/demos/electric.js +63 -0
  76. package/build/esm/tests/demos/greensleeves.js +90 -0
  77. package/build/esm/tests/demos/joy.js +61 -0
  78. package/build/esm/tests/demos/kesh.js +80 -0
  79. package/build/esm/tests/demos/prelude.js +59 -0
  80. package/build/esm/tests/demos/quartertones.js +50 -0
  81. package/build/esm/tests/demos/saints.js +65 -0
  82. package/build/esm/tests/engraving/continuous-studies.mjs +140 -0
  83. package/build/esm/tests/engraving/instrument-studies.mjs +273 -0
  84. package/build/esm/tests/engraving/phrase-studies.mjs +63 -0
  85. package/build/esm/tests/engraving/publication-studies.mjs +87 -0
  86. package/build/esm/tests/engraving/score-layout.mjs +75 -0
  87. package/build/esm/tests/engraving/score-persistence.mjs +262 -0
  88. package/build/esm/tests/engraving/specialist-studies.mjs +347 -0
  89. package/build/esm/tests/engraving/studies.mjs +367 -0
  90. package/build/esm/tests/formatter_tests.js +10 -1
  91. package/build/esm/tests/index.js +1 -0
  92. package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
  93. package/build/esm/tests/node/continuous-score-tests.mjs +282 -0
  94. package/build/esm/tests/node/easyscore-tests.mjs +71 -0
  95. package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
  96. package/build/esm/tests/node/instrument-profile-tests.mjs +224 -0
  97. package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
  98. package/build/esm/tests/node/phrase-layout-tests.mjs +281 -0
  99. package/build/esm/tests/node/proportional-spacing-tests.mjs +64 -0
  100. package/build/esm/tests/node/publication-tests.mjs +219 -0
  101. package/build/esm/tests/node/rendering-tests.mjs +6 -2
  102. package/build/esm/tests/threevoice_tests.js +11 -1
  103. package/build/esm/tests/vexflow_test_helpers.js +19 -10
  104. package/build/esm/tools/audit-dependencies.mjs +61 -0
  105. package/build/esm/tools/easyscore-structure.mjs +1 -1
  106. package/build/esm/tools/explorer/catalog/entries.mjs +2 -1
  107. package/build/esm/tools/explorer/catalog/expression.mjs +14 -9
  108. package/build/esm/tools/explorer/catalog/guitar.mjs +238 -0
  109. package/build/esm/tools/explorer/catalog/layout.mjs +8 -6
  110. package/build/esm/tools/explorer/catalog/notes.mjs +16 -14
  111. package/build/esm/tools/explorer/catalog/staves.mjs +12 -6
  112. package/build/esm/tools/explorer/catalog/strings.mjs +10 -7
  113. package/build/esm/tools/score-fixtures.mjs +23 -6
  114. package/build/types/src/accidental.d.ts +4 -0
  115. package/build/types/src/alternatingtremolo.d.ts +13 -0
  116. package/build/types/src/annotation.d.ts +9 -0
  117. package/build/types/src/articulation.d.ts +9 -1
  118. package/build/types/src/beam.d.ts +16 -0
  119. package/build/types/src/bend.d.ts +8 -0
  120. package/build/types/src/clef.d.ts +3 -0
  121. package/build/types/src/continuouslayout.d.ts +103 -0
  122. package/build/types/src/continuousspan.d.ts +60 -0
  123. package/build/types/src/curve.d.ts +20 -0
  124. package/build/types/src/dot.d.ts +5 -0
  125. package/build/types/src/easyscore/composition.d.ts +27 -0
  126. package/build/types/src/easyscore/policies.d.ts +1 -1
  127. package/build/types/src/element.d.ts +2 -0
  128. package/build/types/src/engravinggeometry.d.ts +31 -0
  129. package/build/types/src/engravingprofile.d.ts +20 -0
  130. package/build/types/src/factory.d.ts +6 -1
  131. package/build/types/src/formatter.d.ts +3 -3
  132. package/build/types/src/fretdiagram.d.ts +23 -0
  133. package/build/types/src/gracenote.d.ts +7 -0
  134. package/build/types/src/gracenotegroup.d.ts +7 -2
  135. package/build/types/src/guitargesture.d.ts +26 -0
  136. package/build/types/src/guitarmodifiers.d.ts +3 -0
  137. package/build/types/src/index.d.ts +12 -0
  138. package/build/types/src/instrumentprofile.d.ts +124 -0
  139. package/build/types/src/keysignature.d.ts +9 -0
  140. package/build/types/src/modifiercontext.d.ts +4 -0
  141. package/build/types/src/notationspelling.d.ts +25 -0
  142. package/build/types/src/note.d.ts +15 -0
  143. package/build/types/src/notehead.d.ts +5 -0
  144. package/build/types/src/ornament.d.ts +10 -1
  145. package/build/types/src/pagebreaks.d.ts +26 -0
  146. package/build/types/src/pedalmarking.d.ts +9 -0
  147. package/build/types/src/phraselayout.d.ts +87 -0
  148. package/build/types/src/phraseribbon.d.ts +7 -0
  149. package/build/types/src/proportionalformatter.d.ts +11 -2
  150. package/build/types/src/publication.d.ts +61 -0
  151. package/build/types/src/publicationview.d.ts +27 -0
  152. package/build/types/src/scorenavigation.d.ts +33 -0
  153. package/build/types/src/sheet.d.ts +5 -0
  154. package/build/types/src/stave.d.ts +13 -0
  155. package/build/types/src/stavehairpin.d.ts +18 -0
  156. package/build/types/src/stavenote.d.ts +20 -2
  157. package/build/types/src/stavetie.d.ts +9 -0
  158. package/build/types/src/stem.d.ts +10 -1
  159. package/build/types/src/system.d.ts +8 -1
  160. package/build/types/src/systembreaks.d.ts +9 -0
  161. package/build/types/src/textbracket.d.ts +7 -0
  162. package/build/types/src/tickcontext.d.ts +5 -0
  163. package/build/types/src/timesignature.d.ts +6 -0
  164. package/build/types/src/tuplet.d.ts +5 -48
  165. package/build/types/src/vexflow.d.ts +20 -0
  166. package/build/types/src/voice.d.ts +2 -0
  167. package/changelog/CHANGELOG.md +25 -0
  168. package/package.json +15 -3
  169. package/tests/all.html +277 -0
  170. package/tests/demo_petzold_tests.ts +435 -0
  171. package/tests/demos/drums.ts +55 -0
  172. package/tests/demos/drums.webp +0 -0
  173. package/tests/demos/electric.ts +70 -0
  174. package/tests/demos/electric.webp +0 -0
  175. package/tests/demos/greensleeves.ts +97 -0
  176. package/tests/demos/greensleeves.webp +0 -0
  177. package/tests/demos/joy.ts +65 -0
  178. package/tests/demos/joy.webp +0 -0
  179. package/tests/demos/kesh.ts +86 -0
  180. package/tests/demos/kesh.webp +0 -0
  181. package/tests/demos/prelude.ts +65 -0
  182. package/tests/demos/prelude.webp +0 -0
  183. package/tests/demos/quartertones.ts +51 -0
  184. package/tests/demos/quartertones.webp +0 -0
  185. package/tests/demos/saints.ts +70 -0
  186. package/tests/demos/saints.webp +0 -0
  187. package/tests/flow.html +550 -239
  188. package/tests/fonts.css +493 -0
  189. package/tests/minuet.webp +0 -0
@@ -0,0 +1,262 @@
1
+ const durations = new Map([
2
+ ['1/1', 'w'],
3
+ ['1/2', 'h'],
4
+ ['1/4', 'q'],
5
+ ['1/8', '8'],
6
+ ['1/16', '16'],
7
+ ]);
8
+ const fractions = new Map([...durations].map(([a, b]) => [b, a]));
9
+ function fail(code, sourceId, phase) {
10
+ const diagnostic = { code, sourceId, severity: 'error', phase };
11
+ throw Object.assign(new Error(`${code} at ${sourceId}`), { diagnostic });
12
+ }
13
+ function ticks(at, owner, phase) {
14
+ const match = /^(0|[1-9]\d*)\/(1|2|4|8|16)$/.exec(at);
15
+ if (!match || +match[1] > +match[2])
16
+ fail('UnsupportedTime', owner, phase);
17
+ const value = (+match[1] * 16) / +match[2];
18
+ if (at !== rational(value))
19
+ fail('UnsupportedTime', owner, phase);
20
+ return value;
21
+ }
22
+ function rational(t) {
23
+ if (!t)
24
+ return '0/1';
25
+ for (const denominator of [1, 2, 4, 8, 16]) {
26
+ if (Number.isInteger((t * denominator) / 16))
27
+ return `${(t * denominator) / 16}/${denominator}`;
28
+ }
29
+ }
30
+ function fields(object, names, id, phase) {
31
+ var _a, _b, _c;
32
+ if (!object ||
33
+ Object.getPrototypeOf(object) !== Object.prototype ||
34
+ Object.keys(object).some((k) => !names.includes(k)))
35
+ fail('UnsupportedInput', (_c = (_b = (_a = object === null || object === void 0 ? void 0 : object.getAttribute) === null || _a === void 0 ? void 0 : _a.call(object, 'id')) !== null && _b !== void 0 ? _b : object === null || object === void 0 ? void 0 : object.id) !== null && _c !== void 0 ? _c : id, phase);
36
+ }
37
+ function validate(score, phase) {
38
+ var _a, _b;
39
+ const ids = new Set();
40
+ const events = new Set();
41
+ const claim = (id) => {
42
+ if (typeof id !== 'string' || !id || ids.has(id))
43
+ fail('InvalidId', id, phase);
44
+ ids.add(id);
45
+ };
46
+ fields(score, ['staves', 'measures', 'marks'], 'score', phase);
47
+ if (![score.staves, score.measures, score.marks].every(Array.isArray))
48
+ fail('UnsupportedInput', 'score', phase);
49
+ if (!score.staves.length || !score.measures.length)
50
+ fail('EmptyScore', 'score', phase);
51
+ for (const staff of score.staves) {
52
+ fields(staff, ['id', 'voice', 'clef', 'key', 'time'], (_a = staff === null || staff === void 0 ? void 0 : staff.id) !== null && _a !== void 0 ? _a : 'staff', phase);
53
+ claim(staff.id);
54
+ claim(staff.voice);
55
+ if (!['treble', 'bass'].includes(staff.clef) || !['C', 'G'].includes(staff.key) || staff.time !== '4/4')
56
+ fail('UnsupportedContext', staff.id, phase);
57
+ }
58
+ for (const bar of score.measures) {
59
+ fields(bar, ['id', 'key', 'voices'], (_b = bar === null || bar === void 0 ? void 0 : bar.id) !== null && _b !== void 0 ? _b : 'measure', phase);
60
+ claim(bar.id);
61
+ if (bar.key !== undefined && !['C', 'G'].includes(bar.key))
62
+ fail('UnsupportedContext', bar.id, phase);
63
+ fields(bar.voices, score.staves.map((s) => s.voice), bar.id, phase);
64
+ for (const staff of score.staves) {
65
+ let t = 0;
66
+ if (!Array.isArray(bar.voices[staff.voice]))
67
+ fail('MissingVoice', bar.id, phase);
68
+ for (const event of bar.voices[staff.voice]) {
69
+ fields(event, ['id', 'pitch', 'at', 'duration'], bar.id, phase);
70
+ claim(event.id);
71
+ events.add(event.id);
72
+ if (!durations.has(event.duration) ||
73
+ ticks(event.at, event.id, phase) !== t ||
74
+ !/^[a-g](#|b)?\/[0-9]$/.test(event.pitch))
75
+ fail('UnsupportedEvent', event.id, phase);
76
+ t += ticks(event.duration, event.id, phase);
77
+ }
78
+ if (t !== 16)
79
+ fail('IncompleteMeasure', bar.id, phase);
80
+ }
81
+ }
82
+ const barIndex = (id, owner) => {
83
+ const index = score.measures.findIndex((b) => b.id === id);
84
+ if (index < 0)
85
+ fail('DanglingAnchor', owner, phase);
86
+ return index;
87
+ };
88
+ for (const mark of score.marks) {
89
+ claim(mark === null || mark === void 0 ? void 0 : mark.id);
90
+ if (mark.kind === 'dynamic') {
91
+ fields(mark, ['id', 'kind', 'event', 'text', 'offset'], mark.id, phase);
92
+ if (!events.has(mark.event))
93
+ fail('DanglingAnchor', mark.id, phase);
94
+ if (!Number.isFinite(mark.offset) || mark.text !== 'p')
95
+ fail('UnsupportedMark', mark.id, phase);
96
+ continue;
97
+ }
98
+ if (!score.staves.some((s) => s.id === mark.staff))
99
+ fail('DanglingAnchor', mark.id, phase);
100
+ if (mark.kind === 'rehearsal') {
101
+ fields(mark, ['id', 'kind', 'measure', 'staff', 'text'], mark.id, phase);
102
+ barIndex(mark.measure, mark.id);
103
+ if (typeof mark.text !== 'string')
104
+ fail('UnsupportedMark', mark.id, phase);
105
+ }
106
+ else if (mark.kind === 'pedal') {
107
+ fields(mark, ['id', 'kind', 'staff', 'start', 'end'], mark.id, phase);
108
+ const positions = [mark.start, mark.end].map((anchor, i) => {
109
+ fields(anchor, ['measure', 'at', 'continuation'], mark.id, phase);
110
+ const bar = barIndex(anchor.measure, mark.id);
111
+ const at = ticks(anchor.at, mark.id, phase);
112
+ if (anchor.continuation !== undefined &&
113
+ (anchor.continuation !== true || (i ? bar !== score.measures.length - 1 || at !== 16 : bar !== 0 || at !== 0)))
114
+ fail('InvalidContinuation', mark.id, phase);
115
+ return bar * 16 + at;
116
+ });
117
+ if (positions[0] >= positions[1])
118
+ fail('InvalidSpan', mark.id, phase);
119
+ }
120
+ else
121
+ fail('UnsupportedMark', mark.id, phase);
122
+ }
123
+ return score;
124
+ }
125
+ export function compositionInput(score) {
126
+ return {
127
+ staves: score.staves.map((staff) => ({
128
+ id: staff.id,
129
+ clef: staff.clef,
130
+ key: staff.key,
131
+ time: staff.time,
132
+ measures: score.measures.map((bar) => (Object.assign(Object.assign({}, (bar.key && { key: bar.key })), { voices: [
133
+ {
134
+ notes: bar.voices[staff.voice].map((e) => ({
135
+ id: e.id,
136
+ note: { clef: staff.clef, keys: [e.pitch], duration: durations.get(e.duration) },
137
+ })),
138
+ },
139
+ ] }))),
140
+ })),
141
+ };
142
+ }
143
+ export function saveScore(score, format) {
144
+ validate(score, 'encode');
145
+ if (format === 'logical')
146
+ return JSON.stringify({ format, version: 1, score });
147
+ if (format !== 'easyscore')
148
+ fail('UnsupportedFormat', 'score', 'encode');
149
+ return JSON.stringify({
150
+ format,
151
+ version: 1,
152
+ composition: compositionInput(score),
153
+ measureIds: score.measures.map((b) => b.id),
154
+ voiceIds: score.staves.map((s) => s.voice),
155
+ marks: score.marks,
156
+ });
157
+ }
158
+ export function loadScore(text) {
159
+ var _a, _b;
160
+ let file;
161
+ try {
162
+ file = JSON.parse(text);
163
+ }
164
+ catch (_c) {
165
+ fail('InvalidDocument', 'score', 'decode');
166
+ }
167
+ if ((file === null || file === void 0 ? void 0 : file.version) !== 1)
168
+ fail('UnsupportedVersion', 'score', 'decode');
169
+ if (file.format === 'logical') {
170
+ fields(file, ['format', 'version', 'score'], 'score', 'decode');
171
+ return validate(file.score, 'decode');
172
+ }
173
+ if (file.format !== 'easyscore')
174
+ fail('UnsupportedFormat', 'score', 'decode');
175
+ fields(file, ['format', 'version', 'composition', 'measureIds', 'voiceIds', 'marks'], 'score', 'decode');
176
+ if (![(_a = file.composition) === null || _a === void 0 ? void 0 : _a.staves, file.measureIds, file.voiceIds, file.marks].every(Array.isArray) ||
177
+ !file.composition.staves.length ||
178
+ file.composition.staves.length !== file.voiceIds.length)
179
+ fail('InvalidDocument', 'score', 'decode');
180
+ for (const staff of file.composition.staves) {
181
+ if (!Array.isArray(staff === null || staff === void 0 ? void 0 : staff.measures) || staff.measures.length !== file.measureIds.length)
182
+ fail('InvalidDocument', (_b = staff === null || staff === void 0 ? void 0 : staff.id) !== null && _b !== void 0 ? _b : 'staff', 'decode');
183
+ }
184
+ const staves = file.composition.staves.map(({ id, clef, key, time }, i) => ({
185
+ id,
186
+ clef,
187
+ key,
188
+ time,
189
+ voice: file.voiceIds[i],
190
+ }));
191
+ const measures = file.measureIds.map((id, i) => {
192
+ var _a, _b;
193
+ const voices = Object.create(null);
194
+ const key = (_a = file.composition.staves[0].measures[i]) === null || _a === void 0 ? void 0 : _a.key;
195
+ for (const [j, staff] of file.composition.staves.entries()) {
196
+ const bar = staff.measures[i];
197
+ if (!bar ||
198
+ bar.key !== key ||
199
+ !Array.isArray(bar.voices) ||
200
+ bar.voices.length !== 1 ||
201
+ !Array.isArray((_b = bar.voices[0]) === null || _b === void 0 ? void 0 : _b.notes))
202
+ fail('UnsupportedContext', id, 'decode');
203
+ let t = 0;
204
+ voices[staves[j].voice] = bar.voices[0].notes.map((item) => {
205
+ const { id, note } = item !== null && item !== void 0 ? item : {};
206
+ if (!Array.isArray(note === null || note === void 0 ? void 0 : note.keys) || note.keys.length !== 1 || !fractions.has(note.duration))
207
+ fail('UnsupportedEvent', id, 'decode');
208
+ const event = { id, pitch: note.keys[0], duration: fractions.get(note.duration), at: rational(t) };
209
+ t += ticks(event.duration, id, 'decode');
210
+ return event;
211
+ });
212
+ }
213
+ return Object.assign(Object.assign({ id }, (key && { key })), { voices: Object.assign({}, voices) });
214
+ });
215
+ const score = validate({ staves, measures, marks: file.marks }, 'decode');
216
+ const reconstructed = compositionInput(score);
217
+ const canonical = (value) => Array.isArray(value)
218
+ ? value.map(canonical)
219
+ : value && typeof value === 'object'
220
+ ? Object.fromEntries(Object.keys(value)
221
+ .sort()
222
+ .map((k) => [k, canonical(value[k])]))
223
+ : value;
224
+ if (JSON.stringify(canonical(reconstructed)) !== JSON.stringify(canonical(file.composition)))
225
+ fail('UnsupportedInput', 'composition', 'decode');
226
+ return score;
227
+ }
228
+ export function excerpt(score, firstId, lastId = firstId) {
229
+ validate(score, 'excerpt');
230
+ const index = (id) => score.measures.findIndex((b) => b.id === id);
231
+ const first = index(firstId), last = index(lastId);
232
+ if (first < 0 || last < first)
233
+ fail('InvalidRange', firstId, 'excerpt');
234
+ const measures = structuredClone(score.measures.slice(first, last + 1));
235
+ const events = new Set(measures.flatMap((b) => Object.values(b.voices)
236
+ .flat()
237
+ .map((e) => e.id)));
238
+ const staves = score.staves.map((staff) => {
239
+ var _a;
240
+ let key = staff.key;
241
+ for (const bar of score.measures.slice(0, first + 1))
242
+ key = (_a = bar.key) !== null && _a !== void 0 ? _a : key;
243
+ return Object.assign(Object.assign({}, staff), { key });
244
+ });
245
+ const marks = [];
246
+ for (const mark of structuredClone(score.marks)) {
247
+ if (mark.kind === 'dynamic' && events.has(mark.event))
248
+ marks.push(mark);
249
+ if (mark.kind === 'rehearsal' && index(mark.measure) >= first && index(mark.measure) <= last)
250
+ marks.push(mark);
251
+ if (mark.kind !== 'pedal' || index(mark.end.measure) < first || index(mark.start.measure) > last)
252
+ continue;
253
+ if (index(mark.start.measure) < first)
254
+ mark.start = { measure: firstId, at: '0/1', continuation: true };
255
+ if (index(mark.end.measure) > last)
256
+ mark.end = { measure: lastId, at: '1/1', continuation: true };
257
+ if (mark.start.measure !== mark.end.measure ||
258
+ ticks(mark.start.at, mark.id, 'excerpt') < ticks(mark.end.at, mark.id, 'excerpt'))
259
+ marks.push(mark);
260
+ }
261
+ return validate({ staves, measures, marks }, 'excerpt');
262
+ }
@@ -0,0 +1,347 @@
1
+ export function registerSpecialistStudies() {
2
+ const VF = globalThis.VexFlow;
3
+ const width = 800;
4
+ const voice = (f, notes) => f.EasyScore().voice(notes, { mode: 'soft' });
5
+ const note = (f, keys, duration, options = {}) => f.StaveNote(Object.assign({ keys, duration }, options));
6
+ const label = (f, n, text, vJustify = 'top') => n.addModifier(f.Annotation({ text, vJustify }), 0);
7
+ const system = (f, x, y, w) => f.System({ x, y, width: w, spaceBetweenStaves: 12 });
8
+ function timeline(notes) {
9
+ const times = [0];
10
+ for (const n of notes)
11
+ times.push(times.at(-1) + n.getTicks().value() / (VF.RESOLUTION / 16));
12
+ return times;
13
+ }
14
+ function pair(f, tuning, strings, duration, stem = 1) {
15
+ const positions = strings.map(([str, fret]) => ({ str, fret }));
16
+ const keys = positions.map(({ str, fret }) => fret === 'X' ? tuning.getNoteForFret(0, str) + '/x' : tuning.getNoteForFret(fret, str));
17
+ return {
18
+ staff: note(f, keys, duration, { stemDirection: stem }),
19
+ tab: f.TabNote({ positions, duration, stemDirection: stem }, true),
20
+ };
21
+ }
22
+ function rest(f, duration) {
23
+ return { staff: note(f, ['b/4'], duration + 'r'), tab: f.GhostNote({ duration }) };
24
+ }
25
+ function guitarBar(f, x, y, w, parts, meter, clef = true, tabSpaceAbove = 0) {
26
+ const sys = system(f, x, y, w);
27
+ const staffVoices = parts.map((p) => voice(f, p.map((e) => e.staff)));
28
+ VF.Accidental.applyAccidentals(staffVoices, 'C');
29
+ const staff = sys.addStave({ voices: staffVoices });
30
+ const tab = sys.addStave({
31
+ stave: f.TabStave({ x, y, width: w }),
32
+ spaceAbove: tabSpaceAbove,
33
+ voices: parts.map((p) => voice(f, p.map((e) => e.tab))),
34
+ });
35
+ if (clef) {
36
+ staff.addClef('treble');
37
+ tab.addTabGlyph();
38
+ }
39
+ if (meter)
40
+ staff.addTimeSignature(meter);
41
+ }
42
+ function beamPair(f, events) {
43
+ for (const view of ['staff', 'tab'])
44
+ f.Beam({ notes: events.map((e) => e[view]) });
45
+ }
46
+ function checkPair(test, events, expected) {
47
+ for (const view of ['staff', 'tab']) {
48
+ test.assert.deepEqual(timeline(events.map((e) => e[view])), expected, view + ' onset and end times');
49
+ }
50
+ for (const event of events) {
51
+ test.assert.equal(event.staff.checkTickContext().getX(), event.tab.checkTickContext().getX(), 'Shared rhythmic column');
52
+ }
53
+ }
54
+ function flamenco(f, test) {
55
+ const tuning = new VF.Tuning('standard');
56
+ const grip = [
57
+ [4, 2],
58
+ [3, 1],
59
+ [2, 0],
60
+ [1, 0],
61
+ ];
62
+ const events = ['16', '16', '16', '16', 'q', 'q', 'q'].map((d) => pair(f, tuning, grip, d));
63
+ const marks = ['a', 'm', 'i', 'i', 'i + golpe', 'p', ''];
64
+ events.forEach((e, i) => {
65
+ if (marks[i])
66
+ label(f, e.staff, marks[i]);
67
+ const type = i === 6 ? VF.Stroke.Type.RASGUEADO_DOWN : i === 3 ? VF.Stroke.Type.BRUSH_UP : VF.Stroke.Type.BRUSH_DOWN;
68
+ for (const n of [e.staff, e.tab])
69
+ n.addModifier(new VF.Stroke(type), 0);
70
+ });
71
+ events[4].staff.addModifier(f.Articulation({ type: 'a>', position: 'above' }));
72
+ beamPair(f, events.slice(0, 4));
73
+ guitarBar(f, 18, 43, 764, [events], '4/4');
74
+ f.draw();
75
+ checkPair(test, events, [0, 1, 2, 3, 4, 8, 12, 16]);
76
+ test.assert.deepEqual(events[4].tab.getPositions().map((p) => p.str), [4, 3, 2, 1], 'Four struck strings on the golpe beat');
77
+ }
78
+ function bossa(f, test) {
79
+ const tuning = new VF.Tuning('standard');
80
+ const chord = [
81
+ [3, 0],
82
+ [2, 1],
83
+ [1, 0],
84
+ ];
85
+ const upper = [
86
+ [rest(f, '8'), pair(f, tuning, chord, '8'), rest(f, '8'), pair(f, tuning, chord, '8')],
87
+ [
88
+ pair(f, tuning, chord, '8'),
89
+ pair(f, tuning, [
90
+ [3, 'X'],
91
+ [2, 'X'],
92
+ [1, 'X'],
93
+ ], '8'),
94
+ rest(f, '8'),
95
+ pair(f, tuning, chord, '8'),
96
+ ],
97
+ ];
98
+ const bass = [
99
+ [pair(f, tuning, [[5, 0]], 'h', -1)],
100
+ [pair(f, tuning, [[6, 0]], 'q', -1), pair(f, tuning, [[5, 0]], 'q', -1)],
101
+ ];
102
+ guitarBar(f, 18, 25, 400, [upper[0], bass[0]], '2/4', true, 3);
103
+ guitarBar(f, 418, 25, 364, [upper[1], bass[1]], null, false, 3);
104
+ const first = upper[0][3], last = upper[1][0], indices = [0, 1, 2];
105
+ f.StaveTie({
106
+ from: first.staff,
107
+ to: last.staff,
108
+ firstIndexes: indices,
109
+ lastIndexes: indices,
110
+ options: { direction: -1 },
111
+ });
112
+ f.TabTie({ firstNote: first.tab, lastNote: last.tab, firstIndexes: indices, lastIndexes: indices });
113
+ f.draw();
114
+ for (const line of upper)
115
+ checkPair(test, line, [0, 2, 4, 6, 8]);
116
+ checkPair(test, bass[0], [0, 8]);
117
+ checkPair(test, bass[1], [0, 4, 8]);
118
+ test.assert.deepEqual(first.tab.getPositions(), last.tab.getPositions(), 'Barline tie retains the same strings and frets');
119
+ test.assert.ok(upper[1][1].tab.getPositions().every((p) => p.fret === 'X'), 'Three muted strings share the second attack');
120
+ }
121
+ function metal(f, test) {
122
+ const tuning = new VF.Tuning('dropd');
123
+ const bass = Array.from({ length: 4 }, () => pair(f, tuning, [[6, 0]], '8', -1));
124
+ const ring = pair(f, tuning, [
125
+ [3, 2],
126
+ [2, 3],
127
+ ], 'h');
128
+ label(f, ring.staff, 'let ring');
129
+ label(f, bass[0].tab, 'P.M. ⑥', 'bottom');
130
+ beamPair(f, bass.slice(0, 2));
131
+ beamPair(f, bass.slice(2));
132
+ guitarBar(f, 18, 30, 764, [[ring], bass], '2/4', true, 6);
133
+ const lead = [7, 12, 7, 10].map((fret) => pair(f, tuning, [[1, fret]], '16'));
134
+ label(f, lead[1].staff, 'T');
135
+ beamPair(f, lead);
136
+ for (const [a, b, letter] of [
137
+ [1, 2, 'P'],
138
+ [2, 3, 'H'],
139
+ ]) {
140
+ f.TabTie({ firstNote: lead[a].tab, lastNote: lead[b].tab, firstIndexes: [0], lastIndexes: [0] }, letter);
141
+ }
142
+ const bend = pair(f, tuning, [
143
+ [3, 7],
144
+ [2, 5],
145
+ ], 'q');
146
+ bend.tab.addModifier(new VF.Bend([
147
+ { type: VF.Bend.UP, text: 'full' },
148
+ { type: VF.Bend.DOWN, text: '' },
149
+ ]), 0);
150
+ const tail = [pair(f, tuning, [[6, 0]], '8', -1), rest(f, '8'), rest(f, 'q')];
151
+ label(f, tail[0].tab, 'P.M. ⑥ ends', 'bottom');
152
+ guitarBar(f, 18, 466, 764, [[...lead, bend], tail], '2/4', true, 7);
153
+ f.draw();
154
+ checkPair(test, bass, [0, 2, 4, 6, 8]);
155
+ checkPair(test, [ring], [0, 8]);
156
+ checkPair(test, [...lead, bend], [0, 1, 2, 3, 4, 8]);
157
+ checkPair(test, tail, [0, 2, 4, 8]);
158
+ const mark = bend.tab.getModifiers().find((m) => m instanceof VF.Bend);
159
+ test.assert.equal(bend.tab.getPositions()[mark.getIndex()].str, 3, 'Only string three changes pitch');
160
+ }
161
+ function contemporary(f, test) {
162
+ const groups = [];
163
+ for (const [i, pitches] of [
164
+ ['c/5', 'd/5', 'e/5', 'f/5'],
165
+ ['f/5', 'e/5', 'd/5', 'c/5'],
166
+ ].entries()) {
167
+ const x = i ? 418 : 18;
168
+ const sys = system(f, x, 38, i ? 364 : 400);
169
+ const pulse = pitches.map((k) => note(f, [k], '16'));
170
+ const cluster = note(f, ['c/5', 'd/5', 'e/5', 'f/5'], 'q');
171
+ const reply = [note(f, ['b/4'], 'qr'), note(f, ['g/4'], 'q')];
172
+ f.Beam({ notes: pulse });
173
+ label(f, pulse[0], i ? 'rit.' : 'accel.');
174
+ const top = sys.addStave({ voices: [voice(f, [...pulse, cluster])] });
175
+ const lower = sys.addStave({ voices: [voice(f, reply)] });
176
+ if (!i)
177
+ for (const staff of [top, lower])
178
+ staff.addClef('treble').addTimeSignature('2/4');
179
+ groups.push({ pulse, cluster, reply });
180
+ }
181
+ f.draw();
182
+ for (const { pulse, cluster, reply } of groups) {
183
+ test.assert.deepEqual(timeline([...pulse, cluster]), [0, 1, 2, 3, 4, 8], 'Beam occupies one quarter');
184
+ test.assert.equal(cluster.checkTickContext().getX(), reply[1].checkTickContext().getX(), 'Both players meet on beat two');
185
+ test.assert.deepEqual(cluster.getKeys(), ['c/5', 'd/5', 'e/5', 'f/5'], 'White-key cluster bounds and contents');
186
+ }
187
+ }
188
+ function ensemble(f, test) {
189
+ const flute = [
190
+ ['c/5', 'q'],
191
+ ['d/5', 'q'],
192
+ ['e/5', 'q'],
193
+ ['g/5', 'h'],
194
+ ['f/5', 'q'],
195
+ ];
196
+ const cello = [
197
+ ['c/3', 'h'],
198
+ ['g/3', 'q'],
199
+ ['f/3', 'q'],
200
+ ['e/3', 'h'],
201
+ ];
202
+ const after = [
203
+ [
204
+ ['e/5', 'q'],
205
+ ['d/5', 'q'],
206
+ ],
207
+ [['c/3', 'h']],
208
+ ];
209
+ const clefs = ['treble', 'bass'];
210
+ const make = (events, i) => events.map(([k, d]) => note(f, [k], d, { clef: clefs[i] }));
211
+ const full = [make(flute, 0), make(cello, 1)];
212
+ const cue = after.map(make);
213
+ const first = system(f, 83, 12, 699), second = system(f, 83, 242, 699);
214
+ const staves = full.map((ns, i) => first
215
+ .addStave({ voices: [voice(f, ns)] })
216
+ .addClef(clefs[i])
217
+ .addTimeSignature(i ? '2/4' : '3/4'));
218
+ const names = ['Flute', 'Cello'];
219
+ staves.forEach((s, i) => s.setStaveText(names[i], VF.Modifier.Position.LEFT));
220
+ first.addConnector('singleLeft');
221
+ cue.forEach((ns, i) => {
222
+ const s = second
223
+ .addStave({ voices: [voice(f, ns)] })
224
+ .addClef(clefs[i])
225
+ .addTimeSignature('2/4');
226
+ s.setStaveText(names[i], VF.Modifier.Position.LEFT);
227
+ if (!i)
228
+ s.setSection('B', 0);
229
+ });
230
+ second.addConnector('singleLeft');
231
+ const parts = [];
232
+ for (const [i, bars] of [
233
+ [flute.slice(0, 3), flute.slice(3), after[0]],
234
+ [cello.slice(0, 1), cello.slice(1, 3), cello.slice(3), after[1]],
235
+ ].entries()) {
236
+ const length = 699 / bars.length, parsed = [];
237
+ bars.forEach((bar, j) => {
238
+ const ns = make(bar, i);
239
+ const s = system(f, 83 + j * length, 480 + i * 118, length).addStave({ voices: [voice(f, ns)] });
240
+ if (!j)
241
+ s.addClef(clefs[i])
242
+ .addTimeSignature(i ? '2/4' : '3/4')
243
+ .setStaveText(names[i] + ' part', VF.Modifier.Position.LEFT);
244
+ if (j === bars.length - 1) {
245
+ if (!i)
246
+ s.addTimeSignature('2/4');
247
+ s.setSection('B', 0);
248
+ }
249
+ parsed.push(...ns);
250
+ });
251
+ parts.push(parsed);
252
+ }
253
+ f.draw();
254
+ for (const [staff, indices] of [
255
+ [0, [3]],
256
+ [1, [1, 3]],
257
+ ]) {
258
+ for (const j of indices)
259
+ new VF.Barline(VF.Barline.type.SINGLE)
260
+ .setStave(staves[staff])
261
+ .setX(full[staff][j].getAbsoluteX() - 12)
262
+ .draw();
263
+ }
264
+ test.assert.deepEqual(timeline(full[0]), [0, 4, 8, 12, 20, 24], 'Flute local bars occupy six quarters');
265
+ test.assert.deepEqual(timeline(full[1]), [0, 8, 12, 16, 24], 'Cello local bars occupy the same six quarters');
266
+ for (const [a, b] of [
267
+ [0, 0],
268
+ [2, 1],
269
+ [3, 2],
270
+ ])
271
+ test.assert.equal(full[0][a].checkTickContext().getX(), full[1][b].checkTickContext().getX(), 'Coincident score attacks align');
272
+ for (const [i, ns] of parts.entries()) {
273
+ const signature = (n) => [n.getKeys(), n.getTicks().value()];
274
+ test.assert.deepEqual(ns.map(signature), [...full[i], ...cue[i]].map(signature), 'Extracted part preserves pitches and durations');
275
+ }
276
+ }
277
+ const studies = [
278
+ [
279
+ 'Four strokes and a golpe',
280
+ flamenco,
281
+ 312,
282
+ 'Flamenco guitar · tangos pulse',
283
+ 'On beat 1, play a–m–i bass to treble, then return with i. Accent beat 2 with a simultaneous golpe. Beat 4 is a free roll.',
284
+ 'The arrow points along the strings on the page. a = ring, m = middle, i = index, p = thumb.',
285
+ ],
286
+ [
287
+ 'Bass under an anticipation',
288
+ bossa,
289
+ 342,
290
+ 'Brazilian violão · bossa nova in 2/4',
291
+ 'Hold A through both offbeat chords. Carry the last chord over the barline. Mute the three upper strings on the next offbeat.',
292
+ 'Read the bass stems down and chords up. X is a struck, muted string. The tie adds no new attack.',
293
+ ],
294
+ [
295
+ 'Pedal string and unison bend',
296
+ metal,
297
+ 847,
298
+ 'Electric guitar · Drop D',
299
+ 'Mute only string 6 through the first attack of line two. Tap fret 12, pull to 7, hammer to 10.',
300
+ 'At the final chord, bend string 3 up a whole tone to the stationary E on string 2, then release.',
301
+ ],
302
+ [
303
+ 'Converging on beat two',
304
+ contemporary,
305
+ 279,
306
+ 'Piano and flute · metered contemporary notation',
307
+ 'The piano fans four attacks faster in bar 1 and slower in bar 2. Each group lasts one beat. Strike the white keys C5–F5 together on beat 2.',
308
+ 'Flute enters with the cluster both times. The control uses ordinary beams, tempo words and a written-out chord.',
309
+ ],
310
+ [
311
+ 'Local bars and a common cue',
312
+ ensemble,
313
+ 718,
314
+ 'Flute and cello · conductor score and separate parts',
315
+ 'In the score, count two flute bars of 3/4 against three cello bars of 2/4. Hold flute G across the last cello barline.',
316
+ 'At B both enter in 2/4. Below the two score systems, follow the same cue in the individual flute and cello parts.',
317
+ ],
318
+ ];
319
+ QUnit.module('M8 reference scores');
320
+ for (const [title, draw, height, subtitle, target, legend] of studies) {
321
+ VF.Test.runTests(title, (test) => {
322
+ const f = VF.Test.makeFactory(test, width, height);
323
+ draw(f, test);
324
+ const root = document.getElementById(test.elementId);
325
+ const svg = root.querySelector('svg');
326
+ const ink = svg.getBBox();
327
+ const left = Math.min(0, Math.floor(ink.x) - 4);
328
+ const top = Math.min(0, Math.floor(ink.y) - 4);
329
+ const w = Math.max(width, Math.ceil(ink.x + ink.width) + 4) - left;
330
+ const h = Math.max(height, Math.ceil(ink.y + ink.height) + 4) - top;
331
+ svg.setAttribute('viewBox', [left, top, w, h].join(' '));
332
+ svg.style.height = Math.ceil((h * width) / w) + 'px';
333
+ const heading = document.createElement('header');
334
+ heading.style.cssText = 'font:16px Arial,sans-serif;padding:16px 18px 0;color:#222';
335
+ const name = document.createElement('strong');
336
+ name.style.cssText = 'display:block;font-size:22px;margin-bottom:4px';
337
+ name.textContent = title;
338
+ heading.append(name, subtitle);
339
+ root.prepend(heading);
340
+ const reading = document.createElement('footer');
341
+ reading.style.cssText =
342
+ 'box-sizing:border-box;width:800px;padding:7px 18px 16px;font:15px/1.45 Arial,sans-serif;color:#222';
343
+ reading.append(target, document.createElement('br'), legend);
344
+ root.append(reading);
345
+ });
346
+ }
347
+ }