musicxml-io 0.3.3 → 0.3.4

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.
@@ -0,0 +1,1809 @@
1
+ // src/utils/index.ts
2
+ var STEPS = ["C", "D", "E", "F", "G", "A", "B"];
3
+ var STEP_SEMITONES = {
4
+ "C": 0,
5
+ "D": 2,
6
+ "E": 4,
7
+ "F": 5,
8
+ "G": 7,
9
+ "A": 9,
10
+ "B": 11
11
+ };
12
+ var SHARP_ORDER = ["F", "C", "G", "D", "A", "E", "B"];
13
+ var FLAT_ORDER = ["B", "E", "A", "D", "G", "C", "F"];
14
+ function pitchToSemitone(pitch) {
15
+ return pitch.octave * 12 + STEP_SEMITONES[pitch.step] + (pitch.alter ?? 0);
16
+ }
17
+ function getAlterForStepInKey(step, key) {
18
+ const fifths = key.fifths;
19
+ if (fifths > 0) {
20
+ const sharps = SHARP_ORDER.slice(0, fifths);
21
+ return sharps.includes(step) ? 1 : 0;
22
+ } else if (fifths < 0) {
23
+ const flats = FLAT_ORDER.slice(0, -fifths);
24
+ return flats.includes(step) ? -1 : 0;
25
+ }
26
+ return 0;
27
+ }
28
+ function getAlteredStepsInKey(key) {
29
+ const alterations = /* @__PURE__ */ new Map();
30
+ const fifths = key.fifths;
31
+ if (fifths > 0) {
32
+ SHARP_ORDER.slice(0, fifths).forEach((step) => alterations.set(step, 1));
33
+ } else if (fifths < 0) {
34
+ FLAT_ORDER.slice(0, -fifths).forEach((step) => alterations.set(step, -1));
35
+ }
36
+ return alterations;
37
+ }
38
+ function getAccidentalsInMeasure(measure, upToPosition, voice) {
39
+ const accidentals = /* @__PURE__ */ new Map();
40
+ let position = 0;
41
+ for (const entry of measure.entries) {
42
+ if (position >= upToPosition) break;
43
+ if (entry.type === "note") {
44
+ if (voice === void 0 || entry.voice === voice) {
45
+ if (entry.pitch && entry.accidental) {
46
+ const key = `${entry.pitch.step}${entry.pitch.octave}`;
47
+ accidentals.set(key, entry.pitch.alter ?? 0);
48
+ }
49
+ }
50
+ if (!entry.chord) {
51
+ position += entry.duration;
52
+ }
53
+ } else if (entry.type === "backup") {
54
+ position -= entry.duration;
55
+ } else if (entry.type === "forward") {
56
+ position += entry.duration;
57
+ }
58
+ }
59
+ return accidentals;
60
+ }
61
+ function semitoneToKeyAwarePitch(semitone, key, options) {
62
+ const octave = Math.floor(semitone / 12);
63
+ const pitchClass = (semitone % 12 + 12) % 12;
64
+ const keyPreferSharp = key.fifths >= 0;
65
+ const preferSharp = options?.preferSharp ?? keyPreferSharp;
66
+ for (const step of STEPS) {
67
+ const stepSemitone = STEP_SEMITONES[step];
68
+ if (stepSemitone === pitchClass) {
69
+ return { step, octave };
70
+ }
71
+ }
72
+ const keyAlterations = getAlteredStepsInKey(key);
73
+ for (const step of STEPS) {
74
+ const stepSemitone = STEP_SEMITONES[step];
75
+ const keyAlter = keyAlterations.get(step) ?? 0;
76
+ if ((stepSemitone + keyAlter) % 12 === pitchClass) {
77
+ return { step, octave, alter: keyAlter };
78
+ }
79
+ }
80
+ if (preferSharp) {
81
+ for (const step of STEPS) {
82
+ const stepSemitone = STEP_SEMITONES[step];
83
+ const diff = (pitchClass - stepSemitone + 12) % 12;
84
+ if (diff === 1) {
85
+ return { step, octave, alter: 1 };
86
+ }
87
+ }
88
+ for (const step of STEPS) {
89
+ const stepSemitone = STEP_SEMITONES[step];
90
+ const diff = (pitchClass - stepSemitone + 12) % 12;
91
+ if (diff === 2) {
92
+ return { step, octave, alter: 2 };
93
+ }
94
+ }
95
+ } else {
96
+ for (const step of STEPS) {
97
+ const stepSemitone = STEP_SEMITONES[step];
98
+ const diff = (stepSemitone - pitchClass + 12) % 12;
99
+ if (diff === 1) {
100
+ return { step, octave, alter: -1 };
101
+ }
102
+ }
103
+ for (const step of STEPS) {
104
+ const stepSemitone = STEP_SEMITONES[step];
105
+ const diff = (stepSemitone - pitchClass + 12) % 12;
106
+ if (diff === 2) {
107
+ return { step, octave, alter: -2 };
108
+ }
109
+ }
110
+ }
111
+ return { step: "C", octave, alter: pitchClass };
112
+ }
113
+ function determineAccidental(pitch, key, accidentalsInMeasure) {
114
+ const noteKey = `${pitch.step}${pitch.octave}`;
115
+ const alter = pitch.alter ?? 0;
116
+ const keyAlter = getAlterForStepInKey(pitch.step, key);
117
+ const previousAlter = accidentalsInMeasure.get(noteKey);
118
+ if (previousAlter !== void 0) {
119
+ if (alter === previousAlter) {
120
+ return void 0;
121
+ }
122
+ } else {
123
+ if (alter === keyAlter) {
124
+ return void 0;
125
+ }
126
+ }
127
+ if (alter === 0) return "natural";
128
+ if (alter === 1) return "sharp";
129
+ if (alter === -1) return "flat";
130
+ if (alter === 2) return "double-sharp";
131
+ if (alter === -2) return "double-flat";
132
+ return void 0;
133
+ }
134
+ function createPositionState() {
135
+ return { position: 0, lastNonChordPosition: 0 };
136
+ }
137
+ function updatePositionForEntry(state, entry) {
138
+ const pos = state.position;
139
+ switch (entry.type) {
140
+ case "note": {
141
+ const note = entry;
142
+ if (!note.chord) {
143
+ state.lastNonChordPosition = state.position;
144
+ state.position += note.duration;
145
+ }
146
+ return note.chord ? state.lastNonChordPosition : pos;
147
+ }
148
+ case "backup":
149
+ state.position -= entry.duration;
150
+ state.lastNonChordPosition = state.position;
151
+ return pos;
152
+ case "forward":
153
+ state.position += entry.duration;
154
+ state.lastNonChordPosition = state.position;
155
+ return pos;
156
+ default:
157
+ return pos;
158
+ }
159
+ }
160
+ function getAbsolutePositionForNote(note, measure) {
161
+ const state = createPositionState();
162
+ for (const entry of measure.entries) {
163
+ if (entry === note) return entry.chord ? state.lastNonChordPosition : state.position;
164
+ updatePositionForEntry(state, entry);
165
+ }
166
+ return state.position;
167
+ }
168
+ function getMeasureEndPosition(measure) {
169
+ const state = createPositionState();
170
+ for (const entry of measure.entries) updatePositionForEntry(state, entry);
171
+ return state.position;
172
+ }
173
+
174
+ // src/query/index.ts
175
+ function getNotesForVoice(measure, filter) {
176
+ return measure.entries.filter((entry) => {
177
+ if (entry.type !== "note") return false;
178
+ if (filter.voice !== void 0 && entry.voice !== filter.voice) return false;
179
+ if (filter.staff !== void 0 && (entry.staff ?? 1) !== filter.staff) return false;
180
+ return true;
181
+ });
182
+ }
183
+ function getNotesForStaff(measure, filter) {
184
+ return measure.entries.filter((entry) => {
185
+ if (entry.type !== "note") return false;
186
+ return (entry.staff ?? 1) === filter.staff;
187
+ });
188
+ }
189
+ function groupByVoice(measure) {
190
+ const groups = /* @__PURE__ */ new Map();
191
+ for (const entry of measure.entries) {
192
+ if (entry.type !== "note") continue;
193
+ const staff = entry.staff ?? 1;
194
+ const voice = entry.voice ?? 1;
195
+ const key = `${staff}-${voice}`;
196
+ if (!groups.has(key)) {
197
+ groups.set(key, { staff, voice, notes: [] });
198
+ }
199
+ groups.get(key).notes.push(entry);
200
+ }
201
+ return Array.from(groups.values()).sort((a, b) => {
202
+ if (a.staff !== b.staff) return a.staff - b.staff;
203
+ return a.voice - b.voice;
204
+ });
205
+ }
206
+ function groupByStaff(measure) {
207
+ const groups = /* @__PURE__ */ new Map();
208
+ for (const entry of measure.entries) {
209
+ if (entry.type !== "note") continue;
210
+ const staff = entry.staff ?? 1;
211
+ if (!groups.has(staff)) {
212
+ groups.set(staff, { staff, notes: [] });
213
+ }
214
+ groups.get(staff).notes.push(entry);
215
+ }
216
+ return Array.from(groups.values()).sort((a, b) => a.staff - b.staff);
217
+ }
218
+ function getAbsolutePosition(note, measure) {
219
+ return getAbsolutePositionForNote(note, measure);
220
+ }
221
+ function withAbsolutePositions(measure) {
222
+ const result = [];
223
+ const state = createPositionState();
224
+ for (const entry of measure.entries) {
225
+ if (entry.type === "note") {
226
+ const notePosition = entry.chord ? state.lastNonChordPosition : state.position;
227
+ result.push({
228
+ ...entry,
229
+ absolutePosition: notePosition
230
+ });
231
+ }
232
+ updatePositionForEntry(state, entry);
233
+ }
234
+ return result;
235
+ }
236
+ function getChords(measure, filter) {
237
+ const notesWithPos = withAbsolutePositions(measure);
238
+ const filteredNotes = filter ? notesWithPos.filter((note) => {
239
+ if (filter.voice !== void 0 && note.voice !== filter.voice) return false;
240
+ if (filter.staff !== void 0 && (note.staff ?? 1) !== filter.staff) return false;
241
+ return true;
242
+ }) : notesWithPos;
243
+ const chordMap = /* @__PURE__ */ new Map();
244
+ for (const note of filteredNotes) {
245
+ const pos = note.absolutePosition;
246
+ if (!chordMap.has(pos)) {
247
+ chordMap.set(pos, []);
248
+ }
249
+ chordMap.get(pos).push(note);
250
+ }
251
+ const chords = [];
252
+ for (const [position, notes] of chordMap.entries()) {
253
+ const duration = notes[0].duration;
254
+ chords.push({
255
+ position,
256
+ duration,
257
+ notes: notes.map(({ absolutePosition, ...note }) => note)
258
+ });
259
+ }
260
+ return chords.sort((a, b) => a.position - b.position);
261
+ }
262
+ function* iterateNotes(score) {
263
+ for (const part of score.parts) {
264
+ for (const measure of part.measures) {
265
+ const state = createPositionState();
266
+ for (const entry of measure.entries) {
267
+ if (entry.type === "note") {
268
+ const notePosition = entry.chord ? state.lastNonChordPosition : state.position;
269
+ yield {
270
+ part,
271
+ measure,
272
+ note: entry,
273
+ position: notePosition
274
+ };
275
+ }
276
+ updatePositionForEntry(state, entry);
277
+ }
278
+ }
279
+ }
280
+ }
281
+ function getAllNotes(score) {
282
+ return Array.from(iterateNotes(score));
283
+ }
284
+ function getVoices(measure) {
285
+ const voices = /* @__PURE__ */ new Set();
286
+ for (const entry of measure.entries) {
287
+ if (entry.type === "note") {
288
+ voices.add(entry.voice ?? 1);
289
+ }
290
+ }
291
+ return Array.from(voices).sort((a, b) => a - b);
292
+ }
293
+ function getStaves(measure) {
294
+ const staves = /* @__PURE__ */ new Set();
295
+ for (const entry of measure.entries) {
296
+ if (entry.type === "note") {
297
+ staves.add(entry.staff ?? 1);
298
+ }
299
+ }
300
+ return Array.from(staves).sort((a, b) => a - b);
301
+ }
302
+ function hasNotes(measure) {
303
+ return measure.entries.some((entry) => entry.type === "note");
304
+ }
305
+ function isRestMeasure(measure) {
306
+ const notes = measure.entries.filter((entry) => entry.type === "note");
307
+ return notes.length === 0 || notes.every((note) => !note.pitch);
308
+ }
309
+ function getNormalizedPosition(note, measure, options) {
310
+ const absolutePosition = getAbsolutePosition(note, measure);
311
+ const currentDivisions = options.currentDivisions ?? measure.attributes?.divisions ?? 1;
312
+ return absolutePosition * options.baseDivisions / currentDivisions;
313
+ }
314
+ function getNormalizedDuration(note, options) {
315
+ const currentDivisions = options.currentDivisions ?? 1;
316
+ return note.duration * options.baseDivisions / currentDivisions;
317
+ }
318
+ function getEntriesForStaff(measure, staff) {
319
+ return measure.entries.filter((entry) => {
320
+ if (entry.type === "note") {
321
+ return (entry.staff ?? 1) === staff;
322
+ }
323
+ if (entry.type === "forward") {
324
+ return (entry.staff ?? 1) === staff;
325
+ }
326
+ if (entry.type === "direction") {
327
+ return (entry.staff ?? 1) === staff;
328
+ }
329
+ if (entry.type === "backup") {
330
+ return false;
331
+ }
332
+ return false;
333
+ });
334
+ }
335
+ function buildVoiceToStaffMap(measure) {
336
+ const map = /* @__PURE__ */ new Map();
337
+ for (const entry of measure.entries) {
338
+ if (entry.type === "note" && entry.staff !== void 0) {
339
+ const voice = entry.voice ?? 1;
340
+ const staff = entry.staff;
341
+ if (!map.has(voice)) {
342
+ map.set(voice, staff);
343
+ }
344
+ }
345
+ }
346
+ return {
347
+ get: (voice) => map.get(voice),
348
+ has: (voice) => map.has(voice),
349
+ entries: () => map.entries(),
350
+ size: map.size
351
+ };
352
+ }
353
+ function buildVoiceToStaffMapForPart(part) {
354
+ const map = /* @__PURE__ */ new Map();
355
+ for (const measure of part.measures) {
356
+ for (const entry of measure.entries) {
357
+ if (entry.type === "note" && entry.staff !== void 0) {
358
+ const voice = entry.voice ?? 1;
359
+ const staff = entry.staff;
360
+ if (!map.has(voice)) {
361
+ map.set(voice, staff);
362
+ }
363
+ }
364
+ }
365
+ }
366
+ return {
367
+ get: (voice) => map.get(voice),
368
+ has: (voice) => map.has(voice),
369
+ entries: () => map.entries(),
370
+ size: map.size
371
+ };
372
+ }
373
+ function inferStaff(entry, voiceToStaffMap) {
374
+ if (entry.staff !== void 0) {
375
+ return entry.staff;
376
+ }
377
+ const inferredStaff = voiceToStaffMap.get(entry.voice ?? 1);
378
+ if (inferredStaff !== void 0) {
379
+ return inferredStaff;
380
+ }
381
+ return 1;
382
+ }
383
+ function getEffectiveStaff(entry, measure) {
384
+ if (entry.staff !== void 0) {
385
+ return entry.staff;
386
+ }
387
+ const map = buildVoiceToStaffMap(measure);
388
+ return inferStaff(entry, map);
389
+ }
390
+ function getClefForStaff(score, options) {
391
+ const part = score.parts[options.partIndex];
392
+ if (!part) return void 0;
393
+ for (let i = options.measureIndex; i >= 0; i--) {
394
+ const measure = part.measures[i];
395
+ for (const entry of measure.entries) {
396
+ if (entry.type === "attributes" && entry.attributes.clef) {
397
+ for (const clef of entry.attributes.clef) {
398
+ if ((clef.staff ?? 1) === options.staff) {
399
+ return clef;
400
+ }
401
+ }
402
+ }
403
+ }
404
+ if (measure.attributes?.clef) {
405
+ for (const clef of measure.attributes.clef) {
406
+ if ((clef.staff ?? 1) === options.staff) {
407
+ return clef;
408
+ }
409
+ }
410
+ }
411
+ }
412
+ return void 0;
413
+ }
414
+ function getVoicesForStaff(measure, staff) {
415
+ const voices = /* @__PURE__ */ new Set();
416
+ for (const entry of measure.entries) {
417
+ if (entry.type === "note") {
418
+ const entryStaff = entry.staff ?? 1;
419
+ if (entryStaff === staff) {
420
+ voices.add(entry.voice ?? 1);
421
+ }
422
+ }
423
+ }
424
+ return Array.from(voices).sort((a, b) => a - b);
425
+ }
426
+ function getStaffRange(score, partIndex) {
427
+ const part = score.parts[partIndex];
428
+ if (!part) return { min: 1, max: 1 };
429
+ let min = 1;
430
+ let max = 1;
431
+ for (const measure of part.measures) {
432
+ if (measure.attributes?.staves !== void 0) {
433
+ max = Math.max(max, measure.attributes.staves);
434
+ }
435
+ for (const entry of measure.entries) {
436
+ if (entry.type === "note" && entry.staff !== void 0) {
437
+ max = Math.max(max, entry.staff);
438
+ }
439
+ }
440
+ }
441
+ return { min, max };
442
+ }
443
+ function getEntriesAtPosition(measure, position, options) {
444
+ const result = [];
445
+ const state = createPositionState();
446
+ for (const entry of measure.entries) {
447
+ const currentPosition = entry.type === "note" && entry.chord ? state.lastNonChordPosition : state.position;
448
+ if (currentPosition === position) {
449
+ if (entry.type === "note") {
450
+ if (options?.staff !== void 0 && (entry.staff ?? 1) !== options.staff) {
451
+ updatePositionForEntry(state, entry);
452
+ continue;
453
+ }
454
+ if (options?.voice !== void 0 && entry.voice !== options.voice) {
455
+ updatePositionForEntry(state, entry);
456
+ continue;
457
+ }
458
+ if (options?.includeChordNotes === false && entry.chord) {
459
+ updatePositionForEntry(state, entry);
460
+ continue;
461
+ }
462
+ }
463
+ result.push(entry);
464
+ }
465
+ updatePositionForEntry(state, entry);
466
+ }
467
+ return result;
468
+ }
469
+ function getNotesAtPosition(measure, position, options) {
470
+ return getEntriesAtPosition(measure, position, options).filter(
471
+ (entry) => entry.type === "note"
472
+ );
473
+ }
474
+ function getEntriesInRange(measure, range, options) {
475
+ const result = [];
476
+ const state = createPositionState();
477
+ for (const entry of measure.entries) {
478
+ const currentPosition = entry.type === "note" && entry.chord ? state.lastNonChordPosition : state.position;
479
+ if (currentPosition >= range.start && currentPosition < range.end) {
480
+ if (entry.type === "note") {
481
+ if (options?.staff !== void 0 && (entry.staff ?? 1) !== options.staff) {
482
+ updatePositionForEntry(state, entry);
483
+ continue;
484
+ }
485
+ if (options?.voice !== void 0 && entry.voice !== options.voice) {
486
+ updatePositionForEntry(state, entry);
487
+ continue;
488
+ }
489
+ if (options?.includeChordNotes === false && entry.chord) {
490
+ updatePositionForEntry(state, entry);
491
+ continue;
492
+ }
493
+ }
494
+ result.push(entry);
495
+ }
496
+ updatePositionForEntry(state, entry);
497
+ }
498
+ return result;
499
+ }
500
+ function getNotesInRange(measure, range, options) {
501
+ return getEntriesInRange(measure, range, options).filter(
502
+ (entry) => entry.type === "note"
503
+ );
504
+ }
505
+ function getVerticalSlice(score, options) {
506
+ const parts = /* @__PURE__ */ new Map();
507
+ for (let partIndex = 0; partIndex < score.parts.length; partIndex++) {
508
+ const part = score.parts[partIndex];
509
+ const measure = part.measures[options.measureIndex];
510
+ if (!measure) continue;
511
+ const notes = getNotesAtPosition(measure, options.position);
512
+ if (notes.length > 0) {
513
+ parts.set(partIndex, notes);
514
+ }
515
+ }
516
+ return {
517
+ measureIndex: options.measureIndex,
518
+ position: options.position,
519
+ parts
520
+ };
521
+ }
522
+ function getVoiceLine(score, options) {
523
+ const part = score.parts[options.partIndex];
524
+ if (!part) {
525
+ return { partIndex: options.partIndex, voice: options.voice, staff: options.staff, notes: [] };
526
+ }
527
+ const notes = [];
528
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
529
+ const measure = part.measures[measureIndex];
530
+ const state = createPositionState();
531
+ for (const entry of measure.entries) {
532
+ if (entry.type === "note") {
533
+ const entryStaff = entry.staff ?? 1;
534
+ const matchesVoice = entry.voice === options.voice;
535
+ const matchesStaff = options.staff === void 0 || entryStaff === options.staff;
536
+ if (matchesVoice && matchesStaff) {
537
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
538
+ notes.push({
539
+ note: entry,
540
+ part,
541
+ partIndex: options.partIndex,
542
+ measure,
543
+ measureIndex,
544
+ position
545
+ });
546
+ }
547
+ }
548
+ updatePositionForEntry(state, entry);
549
+ }
550
+ }
551
+ return {
552
+ partIndex: options.partIndex,
553
+ voice: options.voice,
554
+ staff: options.staff,
555
+ notes
556
+ };
557
+ }
558
+ function getVoiceLineInRange(score, options) {
559
+ const part = score.parts[options.partIndex];
560
+ if (!part) {
561
+ return { partIndex: options.partIndex, voice: options.voice, staff: options.staff, notes: [] };
562
+ }
563
+ const notes = [];
564
+ for (let measureIndex = options.startMeasure; measureIndex <= options.endMeasure && measureIndex < part.measures.length; measureIndex++) {
565
+ const measure = part.measures[measureIndex];
566
+ const state = createPositionState();
567
+ for (const entry of measure.entries) {
568
+ if (entry.type === "note") {
569
+ const entryStaff = entry.staff ?? 1;
570
+ const matchesVoice = entry.voice === options.voice;
571
+ const matchesStaff = options.staff === void 0 || entryStaff === options.staff;
572
+ if (matchesVoice && matchesStaff) {
573
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
574
+ notes.push({
575
+ note: entry,
576
+ part,
577
+ partIndex: options.partIndex,
578
+ measure,
579
+ measureIndex,
580
+ position
581
+ });
582
+ }
583
+ }
584
+ updatePositionForEntry(state, entry);
585
+ }
586
+ }
587
+ return {
588
+ partIndex: options.partIndex,
589
+ voice: options.voice,
590
+ staff: options.staff,
591
+ notes
592
+ };
593
+ }
594
+ function* iterateEntries(score) {
595
+ for (let partIndex = 0; partIndex < score.parts.length; partIndex++) {
596
+ const part = score.parts[partIndex];
597
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
598
+ const measure = part.measures[measureIndex];
599
+ const state = createPositionState();
600
+ for (const entry of measure.entries) {
601
+ const position = entry.type === "note" && entry.chord ? state.lastNonChordPosition : state.position;
602
+ yield {
603
+ entry,
604
+ part,
605
+ partIndex,
606
+ measure,
607
+ measureIndex,
608
+ position
609
+ };
610
+ updatePositionForEntry(state, entry);
611
+ }
612
+ }
613
+ }
614
+ }
615
+ function getNextNote(score, context) {
616
+ const part = score.parts[context.partIndex];
617
+ if (!part) return null;
618
+ let foundCurrent = false;
619
+ for (let measureIndex = context.measureIndex; measureIndex < part.measures.length; measureIndex++) {
620
+ const measure = part.measures[measureIndex];
621
+ const state = createPositionState();
622
+ for (const entry of measure.entries) {
623
+ if (entry.type === "note") {
624
+ const entryPosition = entry.chord ? state.lastNonChordPosition : state.position;
625
+ if (measureIndex === context.measureIndex && entry === context.note) {
626
+ foundCurrent = true;
627
+ updatePositionForEntry(state, entry);
628
+ continue;
629
+ }
630
+ if (foundCurrent && entry.voice === context.note.voice && !entry.chord) {
631
+ if (context.note.staff !== void 0) {
632
+ if ((entry.staff ?? 1) !== context.note.staff) {
633
+ updatePositionForEntry(state, entry);
634
+ continue;
635
+ }
636
+ }
637
+ return {
638
+ note: entry,
639
+ part,
640
+ partIndex: context.partIndex,
641
+ measure,
642
+ measureIndex,
643
+ position: entryPosition
644
+ };
645
+ }
646
+ }
647
+ updatePositionForEntry(state, entry);
648
+ }
649
+ }
650
+ return null;
651
+ }
652
+ function getPrevNote(score, context) {
653
+ const part = score.parts[context.partIndex];
654
+ if (!part) return null;
655
+ let lastCandidate = null;
656
+ for (let measureIndex = 0; measureIndex <= context.measureIndex; measureIndex++) {
657
+ const measure = part.measures[measureIndex];
658
+ const state = createPositionState();
659
+ for (const entry of measure.entries) {
660
+ if (entry.type === "note") {
661
+ const entryPosition = entry.chord ? state.lastNonChordPosition : state.position;
662
+ if (measureIndex === context.measureIndex && entry === context.note) {
663
+ return lastCandidate;
664
+ }
665
+ if (entry.voice === context.note.voice && !entry.chord) {
666
+ if (context.note.staff !== void 0) {
667
+ if ((entry.staff ?? 1) !== context.note.staff) {
668
+ updatePositionForEntry(state, entry);
669
+ continue;
670
+ }
671
+ }
672
+ lastCandidate = {
673
+ note: entry,
674
+ part,
675
+ partIndex: context.partIndex,
676
+ measure,
677
+ measureIndex,
678
+ position: entryPosition
679
+ };
680
+ }
681
+ }
682
+ updatePositionForEntry(state, entry);
683
+ }
684
+ }
685
+ return null;
686
+ }
687
+ function getAdjacentNotes(score, context) {
688
+ return {
689
+ prev: getPrevNote(score, context),
690
+ next: getNextNote(score, context)
691
+ };
692
+ }
693
+ function getDirections(score, options) {
694
+ const results = [];
695
+ const startPart = options?.partIndex ?? 0;
696
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
697
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
698
+ const part = score.parts[partIndex];
699
+ if (!part) continue;
700
+ const startMeasure = options?.measureIndex ?? 0;
701
+ const endMeasure = options?.measureIndex !== void 0 ? options.measureIndex + 1 : part.measures.length;
702
+ for (let measureIndex = startMeasure; measureIndex < endMeasure; measureIndex++) {
703
+ const measure = part.measures[measureIndex];
704
+ if (!measure) continue;
705
+ const state = createPositionState();
706
+ for (const entry of measure.entries) {
707
+ if (entry.type === "direction") {
708
+ results.push({
709
+ direction: entry,
710
+ part,
711
+ partIndex,
712
+ measure,
713
+ measureIndex,
714
+ position: state.position
715
+ });
716
+ }
717
+ updatePositionForEntry(state, entry);
718
+ }
719
+ }
720
+ }
721
+ return results;
722
+ }
723
+ function getDirectionsAtPosition(measure, position) {
724
+ const results = [];
725
+ const state = createPositionState();
726
+ for (const entry of measure.entries) {
727
+ if (entry.type === "direction" && state.position === position) {
728
+ results.push(entry);
729
+ }
730
+ updatePositionForEntry(state, entry);
731
+ }
732
+ return results;
733
+ }
734
+ function findDirectionsByType(score, kind) {
735
+ const allDirections = getDirections(score);
736
+ return allDirections.filter(
737
+ (d) => d.direction.directionTypes.some((dt) => dt.kind === kind)
738
+ );
739
+ }
740
+ function getDynamics(score, options) {
741
+ const results = [];
742
+ const startPart = options?.partIndex ?? 0;
743
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
744
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
745
+ const part = score.parts[partIndex];
746
+ if (!part) continue;
747
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
748
+ const measure = part.measures[measureIndex];
749
+ const state = createPositionState();
750
+ for (const entry of measure.entries) {
751
+ if (entry.type === "direction") {
752
+ for (const dirType of entry.directionTypes) {
753
+ if (dirType.kind === "dynamics") {
754
+ results.push({
755
+ dynamic: dirType.value,
756
+ otherDynamics: dirType.otherDynamics,
757
+ direction: entry,
758
+ part,
759
+ partIndex,
760
+ measure,
761
+ measureIndex,
762
+ position: state.position
763
+ });
764
+ }
765
+ }
766
+ }
767
+ updatePositionForEntry(state, entry);
768
+ }
769
+ }
770
+ }
771
+ return results;
772
+ }
773
+ function getTempoMarkings(score) {
774
+ const results = [];
775
+ for (let partIndex = 0; partIndex < score.parts.length; partIndex++) {
776
+ const part = score.parts[partIndex];
777
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
778
+ const measure = part.measures[measureIndex];
779
+ const state = createPositionState();
780
+ for (const entry of measure.entries) {
781
+ if (entry.type === "direction") {
782
+ for (const dirType of entry.directionTypes) {
783
+ if (dirType.kind === "metronome") {
784
+ results.push({
785
+ beatUnit: dirType.beatUnit,
786
+ perMinute: dirType.perMinute,
787
+ beatUnitDot: dirType.beatUnitDot,
788
+ direction: entry,
789
+ partIndex,
790
+ measureIndex,
791
+ position: state.position
792
+ });
793
+ }
794
+ }
795
+ }
796
+ updatePositionForEntry(state, entry);
797
+ }
798
+ }
799
+ }
800
+ return results;
801
+ }
802
+ function getPedalMarkings(score, options) {
803
+ const results = [];
804
+ const startPart = options?.partIndex ?? 0;
805
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
806
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
807
+ const part = score.parts[partIndex];
808
+ if (!part) continue;
809
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
810
+ const measure = part.measures[measureIndex];
811
+ const state = createPositionState();
812
+ for (const entry of measure.entries) {
813
+ if (entry.type === "direction") {
814
+ for (const dirType of entry.directionTypes) {
815
+ if (dirType.kind === "pedal") {
816
+ results.push({
817
+ pedalType: dirType.type,
818
+ direction: entry,
819
+ partIndex,
820
+ measureIndex,
821
+ position: state.position
822
+ });
823
+ }
824
+ }
825
+ }
826
+ updatePositionForEntry(state, entry);
827
+ }
828
+ }
829
+ }
830
+ return results;
831
+ }
832
+ function getWedges(score, options) {
833
+ const results = [];
834
+ const startPart = options?.partIndex ?? 0;
835
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
836
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
837
+ const part = score.parts[partIndex];
838
+ if (!part) continue;
839
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
840
+ const measure = part.measures[measureIndex];
841
+ const state = createPositionState();
842
+ for (const entry of measure.entries) {
843
+ if (entry.type === "direction") {
844
+ for (const dirType of entry.directionTypes) {
845
+ if (dirType.kind === "wedge") {
846
+ results.push({
847
+ wedgeType: dirType.type,
848
+ direction: entry,
849
+ partIndex,
850
+ measureIndex,
851
+ position: state.position
852
+ });
853
+ }
854
+ }
855
+ }
856
+ updatePositionForEntry(state, entry);
857
+ }
858
+ }
859
+ }
860
+ return results;
861
+ }
862
+ function getOctaveShifts(score, options) {
863
+ const results = [];
864
+ const startPart = options?.partIndex ?? 0;
865
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
866
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
867
+ const part = score.parts[partIndex];
868
+ if (!part) continue;
869
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
870
+ const measure = part.measures[measureIndex];
871
+ const state = createPositionState();
872
+ for (const entry of measure.entries) {
873
+ if (entry.type === "direction") {
874
+ for (const dirType of entry.directionTypes) {
875
+ if (dirType.kind === "octave-shift") {
876
+ results.push({
877
+ shiftType: dirType.type,
878
+ size: dirType.size,
879
+ direction: entry,
880
+ partIndex,
881
+ measureIndex,
882
+ position: state.position
883
+ });
884
+ }
885
+ }
886
+ }
887
+ updatePositionForEntry(state, entry);
888
+ }
889
+ }
890
+ }
891
+ return results;
892
+ }
893
+ function getTiedNoteGroups(score, options) {
894
+ const results = [];
895
+ const startPart = options?.partIndex ?? 0;
896
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
897
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
898
+ const part = score.parts[partIndex];
899
+ if (!part) continue;
900
+ const pendingTies = /* @__PURE__ */ new Map();
901
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
902
+ const measure = part.measures[measureIndex];
903
+ const state = createPositionState();
904
+ for (const entry of measure.entries) {
905
+ if (entry.type === "note" && entry.pitch) {
906
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
907
+ const pitchKey = `${entry.pitch.step}${entry.pitch.octave}-${entry.voice}`;
908
+ const context = {
909
+ note: entry,
910
+ part,
911
+ partIndex,
912
+ measure,
913
+ measureIndex,
914
+ position
915
+ };
916
+ const hasTieStart = entry.tie?.type === "start" || entry.ties?.some((t) => t.type === "start") || entry.notations?.some((n) => n.type === "tied" && n.tiedType === "start");
917
+ const hasTieStop = entry.tie?.type === "stop" || entry.ties?.some((t) => t.type === "stop") || entry.notations?.some((n) => n.type === "tied" && n.tiedType === "stop");
918
+ if (hasTieStop && pendingTies.has(pitchKey)) {
919
+ const group = pendingTies.get(pitchKey);
920
+ group.push(context);
921
+ if (!hasTieStart) {
922
+ const totalDuration = group.reduce((sum, nc) => sum + nc.note.duration, 0);
923
+ results.push({ notes: group, totalDuration });
924
+ pendingTies.delete(pitchKey);
925
+ }
926
+ } else if (hasTieStart) {
927
+ pendingTies.set(pitchKey, [context]);
928
+ }
929
+ }
930
+ updatePositionForEntry(state, entry);
931
+ }
932
+ }
933
+ }
934
+ return results;
935
+ }
936
+ function getSlurSpans(score, options) {
937
+ const results = [];
938
+ const startPart = options?.partIndex ?? 0;
939
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
940
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
941
+ const part = score.parts[partIndex];
942
+ if (!part) continue;
943
+ const pendingSlurs = /* @__PURE__ */ new Map();
944
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
945
+ const measure = part.measures[measureIndex];
946
+ const state = createPositionState();
947
+ for (const entry of measure.entries) {
948
+ if (entry.type === "note") {
949
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
950
+ const context = {
951
+ note: entry,
952
+ part,
953
+ partIndex,
954
+ measure,
955
+ measureIndex,
956
+ position
957
+ };
958
+ if (entry.notations) {
959
+ for (const notation of entry.notations) {
960
+ if (notation.type === "slur") {
961
+ const slurNumber = notation.number ?? 1;
962
+ if (notation.slurType === "start") {
963
+ pendingSlurs.set(slurNumber, { startNote: context, notes: [context] });
964
+ } else if (notation.slurType === "stop" && pendingSlurs.has(slurNumber)) {
965
+ const pending = pendingSlurs.get(slurNumber);
966
+ pending.notes.push(context);
967
+ results.push({
968
+ number: slurNumber,
969
+ startNote: pending.startNote,
970
+ endNote: context,
971
+ notes: pending.notes
972
+ });
973
+ pendingSlurs.delete(slurNumber);
974
+ } else if (notation.slurType === "continue" && pendingSlurs.has(slurNumber)) {
975
+ pendingSlurs.get(slurNumber).notes.push(context);
976
+ }
977
+ }
978
+ }
979
+ }
980
+ for (const [, pending] of pendingSlurs) {
981
+ const lastNote = pending.notes[pending.notes.length - 1];
982
+ if (lastNote !== context) {
983
+ pending.notes.push(context);
984
+ }
985
+ }
986
+ }
987
+ updatePositionForEntry(state, entry);
988
+ }
989
+ }
990
+ }
991
+ return results;
992
+ }
993
+ function getTupletGroups(score, options) {
994
+ const results = [];
995
+ const startPart = options?.partIndex ?? 0;
996
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
997
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
998
+ const part = score.parts[partIndex];
999
+ if (!part) continue;
1000
+ const pendingTuplets = /* @__PURE__ */ new Map();
1001
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1002
+ const measure = part.measures[measureIndex];
1003
+ const state = createPositionState();
1004
+ for (const entry of measure.entries) {
1005
+ if (entry.type === "note") {
1006
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
1007
+ const context = {
1008
+ note: entry,
1009
+ part,
1010
+ partIndex,
1011
+ measure,
1012
+ measureIndex,
1013
+ position
1014
+ };
1015
+ if (entry.notations) {
1016
+ for (const notation of entry.notations) {
1017
+ if (notation.type === "tuplet") {
1018
+ const tupletNumber = notation.number ?? 1;
1019
+ if (notation.tupletType === "start") {
1020
+ const actualNotes = entry.timeModification?.actualNotes ?? 3;
1021
+ const normalNotes = entry.timeModification?.normalNotes ?? 2;
1022
+ pendingTuplets.set(tupletNumber, {
1023
+ notes: [context],
1024
+ actualNotes,
1025
+ normalNotes
1026
+ });
1027
+ } else if (notation.tupletType === "stop" && pendingTuplets.has(tupletNumber)) {
1028
+ const pending = pendingTuplets.get(tupletNumber);
1029
+ pending.notes.push(context);
1030
+ results.push({
1031
+ number: tupletNumber,
1032
+ notes: pending.notes,
1033
+ actualNotes: pending.actualNotes,
1034
+ normalNotes: pending.normalNotes
1035
+ });
1036
+ pendingTuplets.delete(tupletNumber);
1037
+ }
1038
+ }
1039
+ }
1040
+ }
1041
+ if (entry.timeModification && !entry.notations?.some((n) => n.type === "tuplet")) {
1042
+ for (const [, pending] of pendingTuplets) {
1043
+ if (entry.timeModification.actualNotes === pending.actualNotes && entry.timeModification.normalNotes === pending.normalNotes) {
1044
+ pending.notes.push(context);
1045
+ }
1046
+ }
1047
+ }
1048
+ }
1049
+ updatePositionForEntry(state, entry);
1050
+ }
1051
+ }
1052
+ }
1053
+ return results;
1054
+ }
1055
+ function getBeamGroups(measure) {
1056
+ const results = [];
1057
+ const state = createPositionState();
1058
+ const pendingBeams = /* @__PURE__ */ new Map();
1059
+ for (const entry of measure.entries) {
1060
+ if (entry.type === "note" && entry.beam) {
1061
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
1062
+ const context = {
1063
+ note: entry,
1064
+ part: {},
1065
+ // Will be set by caller if needed
1066
+ partIndex: 0,
1067
+ measure,
1068
+ measureIndex: 0,
1069
+ position
1070
+ };
1071
+ for (const beam of entry.beam) {
1072
+ const beamNumber = beam.number;
1073
+ if (beam.type === "begin") {
1074
+ pendingBeams.set(beamNumber, [context]);
1075
+ } else if (beam.type === "continue" && pendingBeams.has(beamNumber)) {
1076
+ pendingBeams.get(beamNumber).push(context);
1077
+ } else if (beam.type === "end" && pendingBeams.has(beamNumber)) {
1078
+ const group = pendingBeams.get(beamNumber);
1079
+ group.push(context);
1080
+ results.push({
1081
+ notes: group,
1082
+ beamLevel: beamNumber
1083
+ });
1084
+ pendingBeams.delete(beamNumber);
1085
+ }
1086
+ }
1087
+ }
1088
+ updatePositionForEntry(state, entry);
1089
+ }
1090
+ return results;
1091
+ }
1092
+ function findNotesWithNotation(score, notationType, options) {
1093
+ const results = [];
1094
+ const startPart = options?.partIndex ?? 0;
1095
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1096
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1097
+ const part = score.parts[partIndex];
1098
+ if (!part) continue;
1099
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1100
+ const measure = part.measures[measureIndex];
1101
+ const state = createPositionState();
1102
+ for (const entry of measure.entries) {
1103
+ if (entry.type === "note") {
1104
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
1105
+ if (entry.notations?.some((n) => n.type === notationType)) {
1106
+ results.push({
1107
+ note: entry,
1108
+ part,
1109
+ partIndex,
1110
+ measure,
1111
+ measureIndex,
1112
+ position
1113
+ });
1114
+ }
1115
+ }
1116
+ updatePositionForEntry(state, entry);
1117
+ }
1118
+ }
1119
+ }
1120
+ return results;
1121
+ }
1122
+ function getHarmonies(score, options) {
1123
+ const results = [];
1124
+ const startPart = options?.partIndex ?? 0;
1125
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1126
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1127
+ const part = score.parts[partIndex];
1128
+ if (!part) continue;
1129
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1130
+ const measure = part.measures[measureIndex];
1131
+ const state = createPositionState();
1132
+ for (const entry of measure.entries) {
1133
+ if (entry.type === "harmony") {
1134
+ results.push({
1135
+ harmony: entry,
1136
+ part,
1137
+ partIndex,
1138
+ measure,
1139
+ measureIndex,
1140
+ position: state.position
1141
+ });
1142
+ }
1143
+ updatePositionForEntry(state, entry);
1144
+ }
1145
+ }
1146
+ }
1147
+ return results;
1148
+ }
1149
+ function getHarmonyAtPosition(measure, position) {
1150
+ const state = createPositionState();
1151
+ let lastHarmony;
1152
+ for (const entry of measure.entries) {
1153
+ if (entry.type === "harmony") {
1154
+ if (state.position <= position) {
1155
+ lastHarmony = entry;
1156
+ }
1157
+ }
1158
+ updatePositionForEntry(state, entry);
1159
+ if (state.position > position && lastHarmony) {
1160
+ break;
1161
+ }
1162
+ }
1163
+ return lastHarmony;
1164
+ }
1165
+ function getChordProgression(score, options) {
1166
+ const harmonies = getHarmonies(score, options);
1167
+ return harmonies.map((h) => {
1168
+ const rootAlter = h.harmony.root.rootAlter ?? 0;
1169
+ const rootAccidental = rootAlter > 0 ? "#".repeat(rootAlter) : "b".repeat(-rootAlter);
1170
+ const root = h.harmony.root.rootStep + rootAccidental;
1171
+ let bass;
1172
+ if (h.harmony.bass) {
1173
+ const bassAlter = h.harmony.bass.bassAlter ?? 0;
1174
+ const bassAccidental = bassAlter > 0 ? "#".repeat(bassAlter) : "b".repeat(-bassAlter);
1175
+ bass = h.harmony.bass.bassStep + bassAccidental;
1176
+ }
1177
+ return {
1178
+ root,
1179
+ kind: h.harmony.kind,
1180
+ bass,
1181
+ measureIndex: h.measureIndex,
1182
+ position: h.position
1183
+ };
1184
+ });
1185
+ }
1186
+ function getLyrics(score, options) {
1187
+ const results = [];
1188
+ const startPart = options?.partIndex ?? 0;
1189
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1190
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1191
+ const part = score.parts[partIndex];
1192
+ if (!part) continue;
1193
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1194
+ const measure = part.measures[measureIndex];
1195
+ const state = createPositionState();
1196
+ for (const entry of measure.entries) {
1197
+ if (entry.type === "note" && entry.lyrics) {
1198
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
1199
+ for (const lyric of entry.lyrics) {
1200
+ const verse = lyric.number ?? 1;
1201
+ if (options?.verse !== void 0 && verse !== options.verse) {
1202
+ continue;
1203
+ }
1204
+ results.push({
1205
+ lyric,
1206
+ note: entry,
1207
+ part,
1208
+ partIndex,
1209
+ measure,
1210
+ measureIndex,
1211
+ position,
1212
+ verse
1213
+ });
1214
+ }
1215
+ }
1216
+ updatePositionForEntry(state, entry);
1217
+ }
1218
+ }
1219
+ }
1220
+ return results;
1221
+ }
1222
+ function getLyricText(score, options) {
1223
+ const lyrics = getLyrics(score, options);
1224
+ const verseMap = /* @__PURE__ */ new Map();
1225
+ for (const lyric of lyrics) {
1226
+ if (!verseMap.has(lyric.verse)) {
1227
+ verseMap.set(lyric.verse, []);
1228
+ }
1229
+ verseMap.get(lyric.verse).push(lyric);
1230
+ }
1231
+ const results = [];
1232
+ for (const [verse, verseLyrics] of verseMap) {
1233
+ const syllables = [];
1234
+ const textParts = [];
1235
+ for (const lyric of verseLyrics) {
1236
+ const text = lyric.lyric.text;
1237
+ syllables.push({
1238
+ text,
1239
+ position: lyric.position,
1240
+ measureIndex: lyric.measureIndex
1241
+ });
1242
+ const syllabic = lyric.lyric.syllabic;
1243
+ if (syllabic === "begin" || syllabic === "middle") {
1244
+ textParts.push(text + "-");
1245
+ } else if (syllabic === "end") {
1246
+ textParts.push(text + " ");
1247
+ } else {
1248
+ textParts.push(text + " ");
1249
+ }
1250
+ }
1251
+ results.push({
1252
+ verse,
1253
+ text: textParts.join("").trim(),
1254
+ syllables
1255
+ });
1256
+ }
1257
+ return results.sort((a, b) => a.verse - b.verse);
1258
+ }
1259
+ function getVerseCount(score, options) {
1260
+ const verses = /* @__PURE__ */ new Set();
1261
+ const startPart = options?.partIndex ?? 0;
1262
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1263
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1264
+ const part = score.parts[partIndex];
1265
+ if (!part) continue;
1266
+ for (const measure of part.measures) {
1267
+ for (const entry of measure.entries) {
1268
+ if (entry.type === "note" && entry.lyrics) {
1269
+ for (const lyric of entry.lyrics) {
1270
+ verses.add(lyric.number ?? 1);
1271
+ }
1272
+ }
1273
+ }
1274
+ }
1275
+ }
1276
+ return verses.size;
1277
+ }
1278
+ function getRepeatStructure(score, options) {
1279
+ const results = [];
1280
+ const partIndex = options?.partIndex ?? 0;
1281
+ const part = score.parts[partIndex];
1282
+ if (!part) return results;
1283
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1284
+ const measure = part.measures[measureIndex];
1285
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1286
+ if (measure.barlines) {
1287
+ for (const barline of measure.barlines) {
1288
+ if (barline.repeat) {
1289
+ results.push({
1290
+ type: barline.repeat.direction,
1291
+ times: barline.repeat.times,
1292
+ measureIndex,
1293
+ measureNumber
1294
+ });
1295
+ }
1296
+ }
1297
+ }
1298
+ }
1299
+ return results;
1300
+ }
1301
+ function findBarlines(score, options) {
1302
+ const results = [];
1303
+ const startPart = options?.partIndex ?? 0;
1304
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1305
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1306
+ const part = score.parts[partIndex];
1307
+ if (!part) continue;
1308
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1309
+ const measure = part.measures[measureIndex];
1310
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1311
+ if (measure.barlines) {
1312
+ for (const barline of measure.barlines) {
1313
+ if (options?.style !== void 0 && barline.barStyle !== options.style) {
1314
+ continue;
1315
+ }
1316
+ if (options?.repeat !== void 0) {
1317
+ const hasRepeat = barline.repeat !== void 0;
1318
+ if (hasRepeat !== options.repeat) {
1319
+ continue;
1320
+ }
1321
+ }
1322
+ results.push({
1323
+ barline,
1324
+ partIndex,
1325
+ measureIndex,
1326
+ measureNumber
1327
+ });
1328
+ }
1329
+ }
1330
+ }
1331
+ }
1332
+ return results;
1333
+ }
1334
+ function getEndings(score, options) {
1335
+ const results = [];
1336
+ const startPart = options?.partIndex ?? 0;
1337
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1338
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1339
+ const part = score.parts[partIndex];
1340
+ if (!part) continue;
1341
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1342
+ const measure = part.measures[measureIndex];
1343
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1344
+ if (measure.barlines) {
1345
+ for (const barline of measure.barlines) {
1346
+ if (barline.ending) {
1347
+ results.push({
1348
+ number: barline.ending.number,
1349
+ type: barline.ending.type,
1350
+ partIndex,
1351
+ measureIndex,
1352
+ measureNumber
1353
+ });
1354
+ }
1355
+ }
1356
+ }
1357
+ }
1358
+ }
1359
+ return results;
1360
+ }
1361
+ function getKeyChanges(score, options) {
1362
+ const results = [];
1363
+ const startPart = options?.partIndex ?? 0;
1364
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1365
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1366
+ const part = score.parts[partIndex];
1367
+ if (!part) continue;
1368
+ let lastKey;
1369
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1370
+ const measure = part.measures[measureIndex];
1371
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1372
+ const state = createPositionState();
1373
+ if (measure.attributes?.key) {
1374
+ const key = measure.attributes.key;
1375
+ if (!lastKey || lastKey.fifths !== key.fifths || lastKey.mode !== key.mode) {
1376
+ results.push({
1377
+ key,
1378
+ partIndex,
1379
+ measureIndex,
1380
+ measureNumber,
1381
+ position: 0
1382
+ });
1383
+ lastKey = key;
1384
+ }
1385
+ }
1386
+ for (const entry of measure.entries) {
1387
+ if (entry.type === "attributes" && entry.attributes.key) {
1388
+ const key = entry.attributes.key;
1389
+ if (!lastKey || lastKey.fifths !== key.fifths || lastKey.mode !== key.mode) {
1390
+ results.push({
1391
+ key,
1392
+ partIndex,
1393
+ measureIndex,
1394
+ measureNumber,
1395
+ position: state.position
1396
+ });
1397
+ lastKey = key;
1398
+ }
1399
+ }
1400
+ updatePositionForEntry(state, entry);
1401
+ }
1402
+ }
1403
+ }
1404
+ return results;
1405
+ }
1406
+ function getTimeChanges(score, options) {
1407
+ const results = [];
1408
+ const startPart = options?.partIndex ?? 0;
1409
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1410
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1411
+ const part = score.parts[partIndex];
1412
+ if (!part) continue;
1413
+ let lastTime;
1414
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1415
+ const measure = part.measures[measureIndex];
1416
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1417
+ if (measure.attributes?.time) {
1418
+ const time = measure.attributes.time;
1419
+ if (!lastTime || lastTime.beats !== time.beats || lastTime.beatType !== time.beatType) {
1420
+ results.push({
1421
+ time,
1422
+ partIndex,
1423
+ measureIndex,
1424
+ measureNumber
1425
+ });
1426
+ lastTime = time;
1427
+ }
1428
+ }
1429
+ for (const entry of measure.entries) {
1430
+ if (entry.type === "attributes" && entry.attributes.time) {
1431
+ const time = entry.attributes.time;
1432
+ if (!lastTime || lastTime.beats !== time.beats || lastTime.beatType !== time.beatType) {
1433
+ results.push({
1434
+ time,
1435
+ partIndex,
1436
+ measureIndex,
1437
+ measureNumber
1438
+ });
1439
+ lastTime = time;
1440
+ }
1441
+ }
1442
+ }
1443
+ }
1444
+ }
1445
+ return results;
1446
+ }
1447
+ function getClefChanges(score, options) {
1448
+ const results = [];
1449
+ const startPart = options?.partIndex ?? 0;
1450
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1451
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1452
+ const part = score.parts[partIndex];
1453
+ if (!part) continue;
1454
+ const lastClefs = /* @__PURE__ */ new Map();
1455
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1456
+ const measure = part.measures[measureIndex];
1457
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1458
+ const state = createPositionState();
1459
+ if (measure.attributes?.clef) {
1460
+ for (const clef of measure.attributes.clef) {
1461
+ const staff = clef.staff ?? 1;
1462
+ if (options?.staff !== void 0 && staff !== options.staff) {
1463
+ continue;
1464
+ }
1465
+ const lastClef = lastClefs.get(staff);
1466
+ if (!lastClef || lastClef.sign !== clef.sign || lastClef.line !== clef.line || lastClef.clefOctaveChange !== clef.clefOctaveChange) {
1467
+ results.push({
1468
+ clef,
1469
+ staff,
1470
+ partIndex,
1471
+ measureIndex,
1472
+ measureNumber,
1473
+ position: 0
1474
+ });
1475
+ lastClefs.set(staff, clef);
1476
+ }
1477
+ }
1478
+ }
1479
+ for (const entry of measure.entries) {
1480
+ if (entry.type === "attributes" && entry.attributes.clef) {
1481
+ for (const clef of entry.attributes.clef) {
1482
+ const staff = clef.staff ?? 1;
1483
+ if (options?.staff !== void 0 && staff !== options.staff) {
1484
+ continue;
1485
+ }
1486
+ const lastClef = lastClefs.get(staff);
1487
+ if (!lastClef || lastClef.sign !== clef.sign || lastClef.line !== clef.line || lastClef.clefOctaveChange !== clef.clefOctaveChange) {
1488
+ results.push({
1489
+ clef,
1490
+ staff,
1491
+ partIndex,
1492
+ measureIndex,
1493
+ measureNumber,
1494
+ position: state.position
1495
+ });
1496
+ lastClefs.set(staff, clef);
1497
+ }
1498
+ }
1499
+ }
1500
+ updatePositionForEntry(state, entry);
1501
+ }
1502
+ }
1503
+ }
1504
+ return results;
1505
+ }
1506
+ function getStructuralChanges(score, options) {
1507
+ return {
1508
+ keyChanges: getKeyChanges(score, options),
1509
+ timeChanges: getTimeChanges(score, options),
1510
+ clefChanges: getClefChanges(score, options)
1511
+ };
1512
+ }
1513
+ function getPartByIndex(score, index) {
1514
+ return score.parts[index];
1515
+ }
1516
+ function getPartCount(score) {
1517
+ return score.parts.length;
1518
+ }
1519
+ function getPartIds(score) {
1520
+ return score.parts.map((part) => part.id);
1521
+ }
1522
+ function getMeasure(score, options) {
1523
+ const part = score.parts[options.part];
1524
+ if (!part) return void 0;
1525
+ const targetMeasure = String(options.measure);
1526
+ return part.measures.find((m) => m.number === targetMeasure);
1527
+ }
1528
+ function getMeasureByIndex(score, options) {
1529
+ const part = score.parts[options.part];
1530
+ if (!part) return void 0;
1531
+ return part.measures[options.measureIndex];
1532
+ }
1533
+ function getMeasureCount(score) {
1534
+ if (score.parts.length === 0) return 0;
1535
+ return score.parts[0].measures.length;
1536
+ }
1537
+ function getDivisions(score, options) {
1538
+ const part = score.parts[options.part];
1539
+ if (!part) return 1;
1540
+ const targetMeasure = parseInt(String(options.measure), 10);
1541
+ if (isNaN(targetMeasure)) return 1;
1542
+ for (let i = 0; i < part.measures.length; i++) {
1543
+ const m = part.measures[i];
1544
+ const mNum = parseInt(m.number, 10);
1545
+ if (!isNaN(mNum) && mNum > targetMeasure) break;
1546
+ if (m.attributes?.divisions !== void 0) {
1547
+ }
1548
+ }
1549
+ let divisions = 1;
1550
+ for (const m of part.measures) {
1551
+ const mNum = parseInt(m.number, 10);
1552
+ if (!isNaN(mNum) && mNum > targetMeasure) break;
1553
+ if (m.attributes?.divisions !== void 0) {
1554
+ divisions = m.attributes.divisions;
1555
+ }
1556
+ }
1557
+ return divisions;
1558
+ }
1559
+ function getAttributesAtMeasure(score, options) {
1560
+ const part = score.parts[options.part];
1561
+ if (!part) return {};
1562
+ const targetMeasure = parseInt(String(options.measure), 10);
1563
+ const result = {};
1564
+ for (const m of part.measures) {
1565
+ const mNum = parseInt(m.number, 10);
1566
+ if (!isNaN(targetMeasure) && !isNaN(mNum) && mNum > targetMeasure) break;
1567
+ if (m.attributes) {
1568
+ if (m.attributes.divisions !== void 0) result.divisions = m.attributes.divisions;
1569
+ if (m.attributes.time !== void 0) result.time = m.attributes.time;
1570
+ if (m.attributes.key !== void 0) result.key = m.attributes.key;
1571
+ if (m.attributes.clef !== void 0) result.clef = m.attributes.clef;
1572
+ if (m.attributes.staves !== void 0) result.staves = m.attributes.staves;
1573
+ if (m.attributes.transpose !== void 0) result.transpose = m.attributes.transpose;
1574
+ }
1575
+ }
1576
+ return result;
1577
+ }
1578
+ function findNotes(score, filter) {
1579
+ const results = [];
1580
+ for (const part of score.parts) {
1581
+ for (const measure of part.measures) {
1582
+ for (const entry of measure.entries) {
1583
+ if (entry.type !== "note") continue;
1584
+ if (filter.pitchRange && entry.pitch) {
1585
+ const noteValue = pitchToSemitone(entry.pitch);
1586
+ if (filter.pitchRange.min) {
1587
+ const minValue = pitchToSemitone(filter.pitchRange.min);
1588
+ if (noteValue < minValue) continue;
1589
+ }
1590
+ if (filter.pitchRange.max) {
1591
+ const maxValue = pitchToSemitone(filter.pitchRange.max);
1592
+ if (noteValue > maxValue) continue;
1593
+ }
1594
+ }
1595
+ if (filter.voice !== void 0 && entry.voice !== filter.voice) continue;
1596
+ if (filter.staff !== void 0 && (entry.staff ?? 1) !== filter.staff) continue;
1597
+ if (filter.noteType !== void 0 && entry.noteType !== filter.noteType) continue;
1598
+ if (filter.hasTie !== void 0) {
1599
+ const hasTie = entry.tie !== void 0;
1600
+ if (filter.hasTie !== hasTie) continue;
1601
+ }
1602
+ results.push(entry);
1603
+ }
1604
+ }
1605
+ }
1606
+ return results;
1607
+ }
1608
+ function getDuration(score) {
1609
+ if (score.parts.length === 0) return 0;
1610
+ const part = score.parts[0];
1611
+ let totalDuration = 0;
1612
+ let divisions = 1;
1613
+ for (const measure of part.measures) {
1614
+ if (measure.attributes?.divisions !== void 0) {
1615
+ divisions = measure.attributes.divisions;
1616
+ }
1617
+ let measureDuration = 0;
1618
+ for (const entry of measure.entries) {
1619
+ if (entry.type === "note" && !entry.chord) {
1620
+ measureDuration = Math.max(measureDuration, entry.duration);
1621
+ }
1622
+ }
1623
+ const attrs = getAttributesAtMeasure(score, { part: 0, measure: measure.number });
1624
+ if (attrs.time) {
1625
+ const beats = parseInt(attrs.time.beats, 10) || 4;
1626
+ const expectedDuration = beats / attrs.time.beatType * 4 * divisions;
1627
+ measureDuration = Math.max(measureDuration, expectedDuration);
1628
+ }
1629
+ totalDuration += measureDuration;
1630
+ }
1631
+ return totalDuration;
1632
+ }
1633
+ function getPartById(score, id) {
1634
+ return score.parts.find((p) => p.id === id);
1635
+ }
1636
+ function getPartIndex(score, id) {
1637
+ return score.parts.findIndex((p) => p.id === id);
1638
+ }
1639
+ function hasMultipleStaves(score, partIndex = 0) {
1640
+ const part = score.parts[partIndex];
1641
+ if (!part) return false;
1642
+ for (const measure of part.measures) {
1643
+ if (measure.attributes?.staves !== void 0 && measure.attributes.staves > 1) {
1644
+ return true;
1645
+ }
1646
+ }
1647
+ return false;
1648
+ }
1649
+ function getStaveCount(score, partIndex = 0) {
1650
+ const part = score.parts[partIndex];
1651
+ if (!part) return 1;
1652
+ for (const measure of part.measures) {
1653
+ if (measure.attributes?.staves !== void 0) {
1654
+ return measure.attributes.staves;
1655
+ }
1656
+ }
1657
+ return 1;
1658
+ }
1659
+ function measureRoundtrip(original, exported) {
1660
+ const notesOriginal = countNotes(original);
1661
+ const notesExported = countNotes(exported);
1662
+ const notesPreserved = Math.min(notesOriginal, notesExported);
1663
+ const measuresOriginal = getMeasureCount(original);
1664
+ const measuresExported = getMeasureCount(exported);
1665
+ const measuresPreserved = Math.min(measuresOriginal, measuresExported);
1666
+ const partsOriginal = original.parts.length;
1667
+ const partsExported = exported.parts.length;
1668
+ const partsPreserved = Math.min(partsOriginal, partsExported);
1669
+ const preservationRate = notesOriginal > 0 ? notesPreserved / notesOriginal : 1;
1670
+ return {
1671
+ notesOriginal,
1672
+ notesPreserved,
1673
+ measuresOriginal,
1674
+ measuresPreserved,
1675
+ partsOriginal,
1676
+ partsPreserved,
1677
+ preservationRate
1678
+ };
1679
+ }
1680
+ function countNotes(score) {
1681
+ let count = 0;
1682
+ for (const part of score.parts) {
1683
+ for (const measure of part.measures) {
1684
+ for (const entry of measure.entries) {
1685
+ if (entry.type === "note") {
1686
+ count++;
1687
+ }
1688
+ }
1689
+ }
1690
+ }
1691
+ return count;
1692
+ }
1693
+ function scoresEqual(a, b) {
1694
+ if (a.parts.length !== b.parts.length) return false;
1695
+ for (let i = 0; i < a.parts.length; i++) {
1696
+ const partA = a.parts[i];
1697
+ const partB = b.parts[i];
1698
+ if (partA.measures.length !== partB.measures.length) return false;
1699
+ for (let j = 0; j < partA.measures.length; j++) {
1700
+ const measureA = partA.measures[j];
1701
+ const measureB = partB.measures[j];
1702
+ if (measureA.number !== measureB.number) return false;
1703
+ const notesA = measureA.entries.filter((e) => e.type === "note");
1704
+ const notesB = measureB.entries.filter((e) => e.type === "note");
1705
+ if (notesA.length !== notesB.length) return false;
1706
+ for (let k = 0; k < notesA.length; k++) {
1707
+ const noteA = notesA[k];
1708
+ const noteB = notesB[k];
1709
+ if (!pitchesEqual(noteA.pitch, noteB.pitch)) return false;
1710
+ if (noteA.duration !== noteB.duration) return false;
1711
+ if (noteA.voice !== noteB.voice) return false;
1712
+ }
1713
+ }
1714
+ }
1715
+ return true;
1716
+ }
1717
+ function pitchesEqual(a, b) {
1718
+ if (a === void 0 && b === void 0) return true;
1719
+ if (a === void 0 || b === void 0) return false;
1720
+ return a.step === b.step && a.octave === b.octave && (a.alter ?? 0) === (b.alter ?? 0);
1721
+ }
1722
+
1723
+ export {
1724
+ STEPS,
1725
+ STEP_SEMITONES,
1726
+ pitchToSemitone,
1727
+ getAccidentalsInMeasure,
1728
+ semitoneToKeyAwarePitch,
1729
+ determineAccidental,
1730
+ getAbsolutePositionForNote,
1731
+ getMeasureEndPosition,
1732
+ getNotesForVoice,
1733
+ getNotesForStaff,
1734
+ groupByVoice,
1735
+ groupByStaff,
1736
+ getAbsolutePosition,
1737
+ withAbsolutePositions,
1738
+ getChords,
1739
+ iterateNotes,
1740
+ getAllNotes,
1741
+ getVoices,
1742
+ getStaves,
1743
+ hasNotes,
1744
+ isRestMeasure,
1745
+ getNormalizedPosition,
1746
+ getNormalizedDuration,
1747
+ getEntriesForStaff,
1748
+ buildVoiceToStaffMap,
1749
+ buildVoiceToStaffMapForPart,
1750
+ inferStaff,
1751
+ getEffectiveStaff,
1752
+ getClefForStaff,
1753
+ getVoicesForStaff,
1754
+ getStaffRange,
1755
+ getEntriesAtPosition,
1756
+ getNotesAtPosition,
1757
+ getEntriesInRange,
1758
+ getNotesInRange,
1759
+ getVerticalSlice,
1760
+ getVoiceLine,
1761
+ getVoiceLineInRange,
1762
+ iterateEntries,
1763
+ getNextNote,
1764
+ getPrevNote,
1765
+ getAdjacentNotes,
1766
+ getDirections,
1767
+ getDirectionsAtPosition,
1768
+ findDirectionsByType,
1769
+ getDynamics,
1770
+ getTempoMarkings,
1771
+ getPedalMarkings,
1772
+ getWedges,
1773
+ getOctaveShifts,
1774
+ getTiedNoteGroups,
1775
+ getSlurSpans,
1776
+ getTupletGroups,
1777
+ getBeamGroups,
1778
+ findNotesWithNotation,
1779
+ getHarmonies,
1780
+ getHarmonyAtPosition,
1781
+ getChordProgression,
1782
+ getLyrics,
1783
+ getLyricText,
1784
+ getVerseCount,
1785
+ getRepeatStructure,
1786
+ findBarlines,
1787
+ getEndings,
1788
+ getKeyChanges,
1789
+ getTimeChanges,
1790
+ getClefChanges,
1791
+ getStructuralChanges,
1792
+ getPartByIndex,
1793
+ getPartCount,
1794
+ getPartIds,
1795
+ getMeasure,
1796
+ getMeasureByIndex,
1797
+ getMeasureCount,
1798
+ getDivisions,
1799
+ getAttributesAtMeasure,
1800
+ findNotes,
1801
+ getDuration,
1802
+ getPartById,
1803
+ getPartIndex,
1804
+ hasMultipleStaves,
1805
+ getStaveCount,
1806
+ measureRoundtrip,
1807
+ countNotes,
1808
+ scoresEqual
1809
+ };