musicxml-io 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -178,21 +178,1271 @@ function getNormalizedDuration(note, options) {
178
178
  const currentDivisions = options.currentDivisions ?? 1;
179
179
  return note.duration * options.baseDivisions / currentDivisions;
180
180
  }
181
+ function getEntriesForStaff(measure, staff) {
182
+ return measure.entries.filter((entry) => {
183
+ if (entry.type === "note") {
184
+ return (entry.staff ?? 1) === staff;
185
+ }
186
+ if (entry.type === "forward") {
187
+ return (entry.staff ?? 1) === staff;
188
+ }
189
+ if (entry.type === "direction") {
190
+ return (entry.staff ?? 1) === staff;
191
+ }
192
+ if (entry.type === "backup") {
193
+ return false;
194
+ }
195
+ return false;
196
+ });
197
+ }
198
+ function buildVoiceToStaffMap(measure) {
199
+ const map = /* @__PURE__ */ new Map();
200
+ for (const entry of measure.entries) {
201
+ if (entry.type === "note" && entry.staff !== void 0) {
202
+ const voice = entry.voice;
203
+ const staff = entry.staff;
204
+ if (!map.has(voice)) {
205
+ map.set(voice, staff);
206
+ }
207
+ }
208
+ }
209
+ return {
210
+ get: (voice) => map.get(voice),
211
+ has: (voice) => map.has(voice),
212
+ entries: () => map.entries(),
213
+ size: map.size
214
+ };
215
+ }
216
+ function buildVoiceToStaffMapForPart(part) {
217
+ const map = /* @__PURE__ */ new Map();
218
+ for (const measure of part.measures) {
219
+ for (const entry of measure.entries) {
220
+ if (entry.type === "note" && entry.staff !== void 0) {
221
+ const voice = entry.voice;
222
+ const staff = entry.staff;
223
+ if (!map.has(voice)) {
224
+ map.set(voice, staff);
225
+ }
226
+ }
227
+ }
228
+ }
229
+ return {
230
+ get: (voice) => map.get(voice),
231
+ has: (voice) => map.has(voice),
232
+ entries: () => map.entries(),
233
+ size: map.size
234
+ };
235
+ }
236
+ function inferStaff(entry, voiceToStaffMap) {
237
+ if (entry.staff !== void 0) {
238
+ return entry.staff;
239
+ }
240
+ const inferredStaff = voiceToStaffMap.get(entry.voice);
241
+ if (inferredStaff !== void 0) {
242
+ return inferredStaff;
243
+ }
244
+ return 1;
245
+ }
246
+ function getEffectiveStaff(entry, measure) {
247
+ if (entry.staff !== void 0) {
248
+ return entry.staff;
249
+ }
250
+ const map = buildVoiceToStaffMap(measure);
251
+ return inferStaff(entry, map);
252
+ }
253
+ function getClefForStaff(score, options) {
254
+ const part = score.parts[options.partIndex];
255
+ if (!part) return void 0;
256
+ for (let i = options.measureIndex; i >= 0; i--) {
257
+ const measure = part.measures[i];
258
+ for (const entry of measure.entries) {
259
+ if (entry.type === "attributes" && entry.attributes.clef) {
260
+ for (const clef of entry.attributes.clef) {
261
+ if ((clef.staff ?? 1) === options.staff) {
262
+ return clef;
263
+ }
264
+ }
265
+ }
266
+ }
267
+ if (measure.attributes?.clef) {
268
+ for (const clef of measure.attributes.clef) {
269
+ if ((clef.staff ?? 1) === options.staff) {
270
+ return clef;
271
+ }
272
+ }
273
+ }
274
+ }
275
+ return void 0;
276
+ }
277
+ function getVoicesForStaff(measure, staff) {
278
+ const voices = /* @__PURE__ */ new Set();
279
+ for (const entry of measure.entries) {
280
+ if (entry.type === "note") {
281
+ const entryStaff = entry.staff ?? 1;
282
+ if (entryStaff === staff) {
283
+ voices.add(entry.voice);
284
+ }
285
+ }
286
+ }
287
+ return Array.from(voices).sort((a, b) => a - b);
288
+ }
289
+ function getStaffRange(score, partIndex) {
290
+ const part = score.parts[partIndex];
291
+ if (!part) return { min: 1, max: 1 };
292
+ let min = 1;
293
+ let max = 1;
294
+ for (const measure of part.measures) {
295
+ if (measure.attributes?.staves !== void 0) {
296
+ max = Math.max(max, measure.attributes.staves);
297
+ }
298
+ for (const entry of measure.entries) {
299
+ if (entry.type === "note" && entry.staff !== void 0) {
300
+ max = Math.max(max, entry.staff);
301
+ }
302
+ }
303
+ }
304
+ return { min, max };
305
+ }
306
+ function getEntriesAtPosition(measure, position, options) {
307
+ const result = [];
308
+ const state = createPositionState();
309
+ for (const entry of measure.entries) {
310
+ const currentPosition = entry.type === "note" && entry.chord ? state.lastNonChordPosition : state.position;
311
+ if (currentPosition === position) {
312
+ if (entry.type === "note") {
313
+ if (options?.staff !== void 0 && (entry.staff ?? 1) !== options.staff) {
314
+ updatePositionForEntry(state, entry);
315
+ continue;
316
+ }
317
+ if (options?.voice !== void 0 && entry.voice !== options.voice) {
318
+ updatePositionForEntry(state, entry);
319
+ continue;
320
+ }
321
+ if (options?.includeChordNotes === false && entry.chord) {
322
+ updatePositionForEntry(state, entry);
323
+ continue;
324
+ }
325
+ }
326
+ result.push(entry);
327
+ }
328
+ updatePositionForEntry(state, entry);
329
+ }
330
+ return result;
331
+ }
332
+ function getNotesAtPosition(measure, position, options) {
333
+ return getEntriesAtPosition(measure, position, options).filter(
334
+ (entry) => entry.type === "note"
335
+ );
336
+ }
337
+ function getEntriesInRange(measure, range, options) {
338
+ const result = [];
339
+ const state = createPositionState();
340
+ for (const entry of measure.entries) {
341
+ const currentPosition = entry.type === "note" && entry.chord ? state.lastNonChordPosition : state.position;
342
+ if (currentPosition >= range.start && currentPosition < range.end) {
343
+ if (entry.type === "note") {
344
+ if (options?.staff !== void 0 && (entry.staff ?? 1) !== options.staff) {
345
+ updatePositionForEntry(state, entry);
346
+ continue;
347
+ }
348
+ if (options?.voice !== void 0 && entry.voice !== options.voice) {
349
+ updatePositionForEntry(state, entry);
350
+ continue;
351
+ }
352
+ if (options?.includeChordNotes === false && entry.chord) {
353
+ updatePositionForEntry(state, entry);
354
+ continue;
355
+ }
356
+ }
357
+ result.push(entry);
358
+ }
359
+ updatePositionForEntry(state, entry);
360
+ }
361
+ return result;
362
+ }
363
+ function getNotesInRange(measure, range, options) {
364
+ return getEntriesInRange(measure, range, options).filter(
365
+ (entry) => entry.type === "note"
366
+ );
367
+ }
368
+ function getVerticalSlice(score, options) {
369
+ const parts = /* @__PURE__ */ new Map();
370
+ for (let partIndex = 0; partIndex < score.parts.length; partIndex++) {
371
+ const part = score.parts[partIndex];
372
+ const measure = part.measures[options.measureIndex];
373
+ if (!measure) continue;
374
+ const notes = getNotesAtPosition(measure, options.position);
375
+ if (notes.length > 0) {
376
+ parts.set(partIndex, notes);
377
+ }
378
+ }
379
+ return {
380
+ measureIndex: options.measureIndex,
381
+ position: options.position,
382
+ parts
383
+ };
384
+ }
385
+ function getVoiceLine(score, options) {
386
+ const part = score.parts[options.partIndex];
387
+ if (!part) {
388
+ return { partIndex: options.partIndex, voice: options.voice, staff: options.staff, notes: [] };
389
+ }
390
+ const notes = [];
391
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
392
+ const measure = part.measures[measureIndex];
393
+ const state = createPositionState();
394
+ for (const entry of measure.entries) {
395
+ if (entry.type === "note") {
396
+ const entryStaff = entry.staff ?? 1;
397
+ const matchesVoice = entry.voice === options.voice;
398
+ const matchesStaff = options.staff === void 0 || entryStaff === options.staff;
399
+ if (matchesVoice && matchesStaff) {
400
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
401
+ notes.push({
402
+ note: entry,
403
+ part,
404
+ partIndex: options.partIndex,
405
+ measure,
406
+ measureIndex,
407
+ position
408
+ });
409
+ }
410
+ }
411
+ updatePositionForEntry(state, entry);
412
+ }
413
+ }
414
+ return {
415
+ partIndex: options.partIndex,
416
+ voice: options.voice,
417
+ staff: options.staff,
418
+ notes
419
+ };
420
+ }
421
+ function getVoiceLineInRange(score, options) {
422
+ const part = score.parts[options.partIndex];
423
+ if (!part) {
424
+ return { partIndex: options.partIndex, voice: options.voice, staff: options.staff, notes: [] };
425
+ }
426
+ const notes = [];
427
+ for (let measureIndex = options.startMeasure; measureIndex <= options.endMeasure && measureIndex < part.measures.length; measureIndex++) {
428
+ const measure = part.measures[measureIndex];
429
+ const state = createPositionState();
430
+ for (const entry of measure.entries) {
431
+ if (entry.type === "note") {
432
+ const entryStaff = entry.staff ?? 1;
433
+ const matchesVoice = entry.voice === options.voice;
434
+ const matchesStaff = options.staff === void 0 || entryStaff === options.staff;
435
+ if (matchesVoice && matchesStaff) {
436
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
437
+ notes.push({
438
+ note: entry,
439
+ part,
440
+ partIndex: options.partIndex,
441
+ measure,
442
+ measureIndex,
443
+ position
444
+ });
445
+ }
446
+ }
447
+ updatePositionForEntry(state, entry);
448
+ }
449
+ }
450
+ return {
451
+ partIndex: options.partIndex,
452
+ voice: options.voice,
453
+ staff: options.staff,
454
+ notes
455
+ };
456
+ }
457
+ function* iterateEntries(score) {
458
+ for (let partIndex = 0; partIndex < score.parts.length; partIndex++) {
459
+ const part = score.parts[partIndex];
460
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
461
+ const measure = part.measures[measureIndex];
462
+ const state = createPositionState();
463
+ for (const entry of measure.entries) {
464
+ const position = entry.type === "note" && entry.chord ? state.lastNonChordPosition : state.position;
465
+ yield {
466
+ entry,
467
+ part,
468
+ partIndex,
469
+ measure,
470
+ measureIndex,
471
+ position
472
+ };
473
+ updatePositionForEntry(state, entry);
474
+ }
475
+ }
476
+ }
477
+ }
478
+ function getNextNote(score, context) {
479
+ const part = score.parts[context.partIndex];
480
+ if (!part) return null;
481
+ let foundCurrent = false;
482
+ for (let measureIndex = context.measureIndex; measureIndex < part.measures.length; measureIndex++) {
483
+ const measure = part.measures[measureIndex];
484
+ const state = createPositionState();
485
+ for (const entry of measure.entries) {
486
+ if (entry.type === "note") {
487
+ const entryPosition = entry.chord ? state.lastNonChordPosition : state.position;
488
+ if (measureIndex === context.measureIndex && entry === context.note) {
489
+ foundCurrent = true;
490
+ updatePositionForEntry(state, entry);
491
+ continue;
492
+ }
493
+ if (foundCurrent && entry.voice === context.note.voice && !entry.chord) {
494
+ if (context.note.staff !== void 0) {
495
+ if ((entry.staff ?? 1) !== context.note.staff) {
496
+ updatePositionForEntry(state, entry);
497
+ continue;
498
+ }
499
+ }
500
+ return {
501
+ note: entry,
502
+ part,
503
+ partIndex: context.partIndex,
504
+ measure,
505
+ measureIndex,
506
+ position: entryPosition
507
+ };
508
+ }
509
+ }
510
+ updatePositionForEntry(state, entry);
511
+ }
512
+ }
513
+ return null;
514
+ }
515
+ function getPrevNote(score, context) {
516
+ const part = score.parts[context.partIndex];
517
+ if (!part) return null;
518
+ let lastCandidate = null;
519
+ for (let measureIndex = 0; measureIndex <= context.measureIndex; measureIndex++) {
520
+ const measure = part.measures[measureIndex];
521
+ const state = createPositionState();
522
+ for (const entry of measure.entries) {
523
+ if (entry.type === "note") {
524
+ const entryPosition = entry.chord ? state.lastNonChordPosition : state.position;
525
+ if (measureIndex === context.measureIndex && entry === context.note) {
526
+ return lastCandidate;
527
+ }
528
+ if (entry.voice === context.note.voice && !entry.chord) {
529
+ if (context.note.staff !== void 0) {
530
+ if ((entry.staff ?? 1) !== context.note.staff) {
531
+ updatePositionForEntry(state, entry);
532
+ continue;
533
+ }
534
+ }
535
+ lastCandidate = {
536
+ note: entry,
537
+ part,
538
+ partIndex: context.partIndex,
539
+ measure,
540
+ measureIndex,
541
+ position: entryPosition
542
+ };
543
+ }
544
+ }
545
+ updatePositionForEntry(state, entry);
546
+ }
547
+ }
548
+ return null;
549
+ }
550
+ function getAdjacentNotes(score, context) {
551
+ return {
552
+ prev: getPrevNote(score, context),
553
+ next: getNextNote(score, context)
554
+ };
555
+ }
556
+ function getDirections(score, options) {
557
+ const results = [];
558
+ const startPart = options?.partIndex ?? 0;
559
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
560
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
561
+ const part = score.parts[partIndex];
562
+ if (!part) continue;
563
+ const startMeasure = options?.measureIndex ?? 0;
564
+ const endMeasure = options?.measureIndex !== void 0 ? options.measureIndex + 1 : part.measures.length;
565
+ for (let measureIndex = startMeasure; measureIndex < endMeasure; measureIndex++) {
566
+ const measure = part.measures[measureIndex];
567
+ if (!measure) continue;
568
+ const state = createPositionState();
569
+ for (const entry of measure.entries) {
570
+ if (entry.type === "direction") {
571
+ results.push({
572
+ direction: entry,
573
+ part,
574
+ partIndex,
575
+ measure,
576
+ measureIndex,
577
+ position: state.position
578
+ });
579
+ }
580
+ updatePositionForEntry(state, entry);
581
+ }
582
+ }
583
+ }
584
+ return results;
585
+ }
586
+ function getDirectionsAtPosition(measure, position) {
587
+ const results = [];
588
+ const state = createPositionState();
589
+ for (const entry of measure.entries) {
590
+ if (entry.type === "direction" && state.position === position) {
591
+ results.push(entry);
592
+ }
593
+ updatePositionForEntry(state, entry);
594
+ }
595
+ return results;
596
+ }
597
+ function findDirectionsByType(score, kind) {
598
+ const allDirections = getDirections(score);
599
+ return allDirections.filter(
600
+ (d) => d.direction.directionTypes.some((dt) => dt.kind === kind)
601
+ );
602
+ }
603
+ function getDynamics(score, options) {
604
+ const results = [];
605
+ const startPart = options?.partIndex ?? 0;
606
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
607
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
608
+ const part = score.parts[partIndex];
609
+ if (!part) continue;
610
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
611
+ const measure = part.measures[measureIndex];
612
+ const state = createPositionState();
613
+ for (const entry of measure.entries) {
614
+ if (entry.type === "direction") {
615
+ for (const dirType of entry.directionTypes) {
616
+ if (dirType.kind === "dynamics") {
617
+ results.push({
618
+ dynamic: dirType.value,
619
+ direction: entry,
620
+ part,
621
+ partIndex,
622
+ measure,
623
+ measureIndex,
624
+ position: state.position
625
+ });
626
+ }
627
+ }
628
+ }
629
+ updatePositionForEntry(state, entry);
630
+ }
631
+ }
632
+ }
633
+ return results;
634
+ }
635
+ function getTempoMarkings(score) {
636
+ const results = [];
637
+ for (let partIndex = 0; partIndex < score.parts.length; partIndex++) {
638
+ const part = score.parts[partIndex];
639
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
640
+ const measure = part.measures[measureIndex];
641
+ const state = createPositionState();
642
+ for (const entry of measure.entries) {
643
+ if (entry.type === "direction") {
644
+ for (const dirType of entry.directionTypes) {
645
+ if (dirType.kind === "metronome") {
646
+ results.push({
647
+ beatUnit: dirType.beatUnit,
648
+ perMinute: dirType.perMinute,
649
+ beatUnitDot: dirType.beatUnitDot,
650
+ direction: entry,
651
+ partIndex,
652
+ measureIndex,
653
+ position: state.position
654
+ });
655
+ }
656
+ }
657
+ }
658
+ updatePositionForEntry(state, entry);
659
+ }
660
+ }
661
+ }
662
+ return results;
663
+ }
664
+ function getPedalMarkings(score, options) {
665
+ const results = [];
666
+ const startPart = options?.partIndex ?? 0;
667
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
668
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
669
+ const part = score.parts[partIndex];
670
+ if (!part) continue;
671
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
672
+ const measure = part.measures[measureIndex];
673
+ const state = createPositionState();
674
+ for (const entry of measure.entries) {
675
+ if (entry.type === "direction") {
676
+ for (const dirType of entry.directionTypes) {
677
+ if (dirType.kind === "pedal") {
678
+ results.push({
679
+ pedalType: dirType.type,
680
+ direction: entry,
681
+ partIndex,
682
+ measureIndex,
683
+ position: state.position
684
+ });
685
+ }
686
+ }
687
+ }
688
+ updatePositionForEntry(state, entry);
689
+ }
690
+ }
691
+ }
692
+ return results;
693
+ }
694
+ function getWedges(score, options) {
695
+ const results = [];
696
+ const startPart = options?.partIndex ?? 0;
697
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
698
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
699
+ const part = score.parts[partIndex];
700
+ if (!part) continue;
701
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
702
+ const measure = part.measures[measureIndex];
703
+ const state = createPositionState();
704
+ for (const entry of measure.entries) {
705
+ if (entry.type === "direction") {
706
+ for (const dirType of entry.directionTypes) {
707
+ if (dirType.kind === "wedge") {
708
+ results.push({
709
+ wedgeType: dirType.type,
710
+ direction: entry,
711
+ partIndex,
712
+ measureIndex,
713
+ position: state.position
714
+ });
715
+ }
716
+ }
717
+ }
718
+ updatePositionForEntry(state, entry);
719
+ }
720
+ }
721
+ }
722
+ return results;
723
+ }
724
+ function getOctaveShifts(score, options) {
725
+ const results = [];
726
+ const startPart = options?.partIndex ?? 0;
727
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
728
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
729
+ const part = score.parts[partIndex];
730
+ if (!part) continue;
731
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
732
+ const measure = part.measures[measureIndex];
733
+ const state = createPositionState();
734
+ for (const entry of measure.entries) {
735
+ if (entry.type === "direction") {
736
+ for (const dirType of entry.directionTypes) {
737
+ if (dirType.kind === "octave-shift") {
738
+ results.push({
739
+ shiftType: dirType.type,
740
+ size: dirType.size,
741
+ direction: entry,
742
+ partIndex,
743
+ measureIndex,
744
+ position: state.position
745
+ });
746
+ }
747
+ }
748
+ }
749
+ updatePositionForEntry(state, entry);
750
+ }
751
+ }
752
+ }
753
+ return results;
754
+ }
755
+ function getTiedNoteGroups(score, options) {
756
+ const results = [];
757
+ const startPart = options?.partIndex ?? 0;
758
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
759
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
760
+ const part = score.parts[partIndex];
761
+ if (!part) continue;
762
+ const pendingTies = /* @__PURE__ */ new Map();
763
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
764
+ const measure = part.measures[measureIndex];
765
+ const state = createPositionState();
766
+ for (const entry of measure.entries) {
767
+ if (entry.type === "note" && entry.pitch) {
768
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
769
+ const pitchKey = `${entry.pitch.step}${entry.pitch.octave}-${entry.voice}`;
770
+ const context = {
771
+ note: entry,
772
+ part,
773
+ partIndex,
774
+ measure,
775
+ measureIndex,
776
+ position
777
+ };
778
+ const hasTieStart = entry.tie?.type === "start" || entry.ties?.some((t) => t.type === "start") || entry.notations?.some((n) => n.type === "tied" && n.tiedType === "start");
779
+ const hasTieStop = entry.tie?.type === "stop" || entry.ties?.some((t) => t.type === "stop") || entry.notations?.some((n) => n.type === "tied" && n.tiedType === "stop");
780
+ if (hasTieStop && pendingTies.has(pitchKey)) {
781
+ const group = pendingTies.get(pitchKey);
782
+ group.push(context);
783
+ if (!hasTieStart) {
784
+ const totalDuration = group.reduce((sum, nc) => sum + nc.note.duration, 0);
785
+ results.push({ notes: group, totalDuration });
786
+ pendingTies.delete(pitchKey);
787
+ }
788
+ } else if (hasTieStart) {
789
+ pendingTies.set(pitchKey, [context]);
790
+ }
791
+ }
792
+ updatePositionForEntry(state, entry);
793
+ }
794
+ }
795
+ }
796
+ return results;
797
+ }
798
+ function getSlurSpans(score, options) {
799
+ const results = [];
800
+ const startPart = options?.partIndex ?? 0;
801
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
802
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
803
+ const part = score.parts[partIndex];
804
+ if (!part) continue;
805
+ const pendingSlurs = /* @__PURE__ */ new Map();
806
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
807
+ const measure = part.measures[measureIndex];
808
+ const state = createPositionState();
809
+ for (const entry of measure.entries) {
810
+ if (entry.type === "note") {
811
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
812
+ const context = {
813
+ note: entry,
814
+ part,
815
+ partIndex,
816
+ measure,
817
+ measureIndex,
818
+ position
819
+ };
820
+ if (entry.notations) {
821
+ for (const notation of entry.notations) {
822
+ if (notation.type === "slur") {
823
+ const slurNumber = notation.number ?? 1;
824
+ if (notation.slurType === "start") {
825
+ pendingSlurs.set(slurNumber, { startNote: context, notes: [context] });
826
+ } else if (notation.slurType === "stop" && pendingSlurs.has(slurNumber)) {
827
+ const pending = pendingSlurs.get(slurNumber);
828
+ pending.notes.push(context);
829
+ results.push({
830
+ number: slurNumber,
831
+ startNote: pending.startNote,
832
+ endNote: context,
833
+ notes: pending.notes
834
+ });
835
+ pendingSlurs.delete(slurNumber);
836
+ } else if (notation.slurType === "continue" && pendingSlurs.has(slurNumber)) {
837
+ pendingSlurs.get(slurNumber).notes.push(context);
838
+ }
839
+ }
840
+ }
841
+ }
842
+ for (const [, pending] of pendingSlurs) {
843
+ const lastNote = pending.notes[pending.notes.length - 1];
844
+ if (lastNote !== context) {
845
+ pending.notes.push(context);
846
+ }
847
+ }
848
+ }
849
+ updatePositionForEntry(state, entry);
850
+ }
851
+ }
852
+ }
853
+ return results;
854
+ }
855
+ function getTupletGroups(score, options) {
856
+ const results = [];
857
+ const startPart = options?.partIndex ?? 0;
858
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
859
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
860
+ const part = score.parts[partIndex];
861
+ if (!part) continue;
862
+ const pendingTuplets = /* @__PURE__ */ new Map();
863
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
864
+ const measure = part.measures[measureIndex];
865
+ const state = createPositionState();
866
+ for (const entry of measure.entries) {
867
+ if (entry.type === "note") {
868
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
869
+ const context = {
870
+ note: entry,
871
+ part,
872
+ partIndex,
873
+ measure,
874
+ measureIndex,
875
+ position
876
+ };
877
+ if (entry.notations) {
878
+ for (const notation of entry.notations) {
879
+ if (notation.type === "tuplet") {
880
+ const tupletNumber = notation.number ?? 1;
881
+ if (notation.tupletType === "start") {
882
+ const actualNotes = entry.timeModification?.actualNotes ?? 3;
883
+ const normalNotes = entry.timeModification?.normalNotes ?? 2;
884
+ pendingTuplets.set(tupletNumber, {
885
+ notes: [context],
886
+ actualNotes,
887
+ normalNotes
888
+ });
889
+ } else if (notation.tupletType === "stop" && pendingTuplets.has(tupletNumber)) {
890
+ const pending = pendingTuplets.get(tupletNumber);
891
+ pending.notes.push(context);
892
+ results.push({
893
+ number: tupletNumber,
894
+ notes: pending.notes,
895
+ actualNotes: pending.actualNotes,
896
+ normalNotes: pending.normalNotes
897
+ });
898
+ pendingTuplets.delete(tupletNumber);
899
+ }
900
+ }
901
+ }
902
+ }
903
+ if (entry.timeModification && !entry.notations?.some((n) => n.type === "tuplet")) {
904
+ for (const [, pending] of pendingTuplets) {
905
+ if (entry.timeModification.actualNotes === pending.actualNotes && entry.timeModification.normalNotes === pending.normalNotes) {
906
+ pending.notes.push(context);
907
+ }
908
+ }
909
+ }
910
+ }
911
+ updatePositionForEntry(state, entry);
912
+ }
913
+ }
914
+ }
915
+ return results;
916
+ }
917
+ function getBeamGroups(measure) {
918
+ const results = [];
919
+ const state = createPositionState();
920
+ const pendingBeams = /* @__PURE__ */ new Map();
921
+ for (const entry of measure.entries) {
922
+ if (entry.type === "note" && entry.beam) {
923
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
924
+ const context = {
925
+ note: entry,
926
+ part: {},
927
+ // Will be set by caller if needed
928
+ partIndex: 0,
929
+ measure,
930
+ measureIndex: 0,
931
+ position
932
+ };
933
+ for (const beam of entry.beam) {
934
+ const beamNumber = beam.number;
935
+ if (beam.type === "begin") {
936
+ pendingBeams.set(beamNumber, [context]);
937
+ } else if (beam.type === "continue" && pendingBeams.has(beamNumber)) {
938
+ pendingBeams.get(beamNumber).push(context);
939
+ } else if (beam.type === "end" && pendingBeams.has(beamNumber)) {
940
+ const group = pendingBeams.get(beamNumber);
941
+ group.push(context);
942
+ results.push({
943
+ notes: group,
944
+ beamLevel: beamNumber
945
+ });
946
+ pendingBeams.delete(beamNumber);
947
+ }
948
+ }
949
+ }
950
+ updatePositionForEntry(state, entry);
951
+ }
952
+ return results;
953
+ }
954
+ function findNotesWithNotation(score, notationType, options) {
955
+ const results = [];
956
+ const startPart = options?.partIndex ?? 0;
957
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
958
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
959
+ const part = score.parts[partIndex];
960
+ if (!part) continue;
961
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
962
+ const measure = part.measures[measureIndex];
963
+ const state = createPositionState();
964
+ for (const entry of measure.entries) {
965
+ if (entry.type === "note") {
966
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
967
+ if (entry.notations?.some((n) => n.type === notationType)) {
968
+ results.push({
969
+ note: entry,
970
+ part,
971
+ partIndex,
972
+ measure,
973
+ measureIndex,
974
+ position
975
+ });
976
+ }
977
+ }
978
+ updatePositionForEntry(state, entry);
979
+ }
980
+ }
981
+ }
982
+ return results;
983
+ }
984
+ function getHarmonies(score, options) {
985
+ const results = [];
986
+ const startPart = options?.partIndex ?? 0;
987
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
988
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
989
+ const part = score.parts[partIndex];
990
+ if (!part) continue;
991
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
992
+ const measure = part.measures[measureIndex];
993
+ const state = createPositionState();
994
+ for (const entry of measure.entries) {
995
+ if (entry.type === "harmony") {
996
+ results.push({
997
+ harmony: entry,
998
+ part,
999
+ partIndex,
1000
+ measure,
1001
+ measureIndex,
1002
+ position: state.position
1003
+ });
1004
+ }
1005
+ updatePositionForEntry(state, entry);
1006
+ }
1007
+ }
1008
+ }
1009
+ return results;
1010
+ }
1011
+ function getHarmonyAtPosition(measure, position) {
1012
+ const state = createPositionState();
1013
+ let lastHarmony;
1014
+ for (const entry of measure.entries) {
1015
+ if (entry.type === "harmony") {
1016
+ if (state.position <= position) {
1017
+ lastHarmony = entry;
1018
+ }
1019
+ }
1020
+ updatePositionForEntry(state, entry);
1021
+ if (state.position > position && lastHarmony) {
1022
+ break;
1023
+ }
1024
+ }
1025
+ return lastHarmony;
1026
+ }
1027
+ function getChordProgression(score, options) {
1028
+ const harmonies = getHarmonies(score, options);
1029
+ return harmonies.map((h) => {
1030
+ const rootAlter = h.harmony.root.rootAlter ?? 0;
1031
+ const rootAccidental = rootAlter > 0 ? "#".repeat(rootAlter) : "b".repeat(-rootAlter);
1032
+ const root = h.harmony.root.rootStep + rootAccidental;
1033
+ let bass;
1034
+ if (h.harmony.bass) {
1035
+ const bassAlter = h.harmony.bass.bassAlter ?? 0;
1036
+ const bassAccidental = bassAlter > 0 ? "#".repeat(bassAlter) : "b".repeat(-bassAlter);
1037
+ bass = h.harmony.bass.bassStep + bassAccidental;
1038
+ }
1039
+ return {
1040
+ root,
1041
+ kind: h.harmony.kind,
1042
+ bass,
1043
+ measureIndex: h.measureIndex,
1044
+ position: h.position
1045
+ };
1046
+ });
1047
+ }
1048
+ function getLyrics(score, options) {
1049
+ const results = [];
1050
+ const startPart = options?.partIndex ?? 0;
1051
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1052
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1053
+ const part = score.parts[partIndex];
1054
+ if (!part) continue;
1055
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1056
+ const measure = part.measures[measureIndex];
1057
+ const state = createPositionState();
1058
+ for (const entry of measure.entries) {
1059
+ if (entry.type === "note" && entry.lyrics) {
1060
+ const position = entry.chord ? state.lastNonChordPosition : state.position;
1061
+ for (const lyric of entry.lyrics) {
1062
+ const verse = lyric.number ?? 1;
1063
+ if (options?.verse !== void 0 && verse !== options.verse) {
1064
+ continue;
1065
+ }
1066
+ results.push({
1067
+ lyric,
1068
+ note: entry,
1069
+ part,
1070
+ partIndex,
1071
+ measure,
1072
+ measureIndex,
1073
+ position,
1074
+ verse
1075
+ });
1076
+ }
1077
+ }
1078
+ updatePositionForEntry(state, entry);
1079
+ }
1080
+ }
1081
+ }
1082
+ return results;
1083
+ }
1084
+ function getLyricText(score, options) {
1085
+ const lyrics = getLyrics(score, options);
1086
+ const verseMap = /* @__PURE__ */ new Map();
1087
+ for (const lyric of lyrics) {
1088
+ if (!verseMap.has(lyric.verse)) {
1089
+ verseMap.set(lyric.verse, []);
1090
+ }
1091
+ verseMap.get(lyric.verse).push(lyric);
1092
+ }
1093
+ const results = [];
1094
+ for (const [verse, verseLyrics] of verseMap) {
1095
+ const syllables = [];
1096
+ const textParts = [];
1097
+ for (const lyric of verseLyrics) {
1098
+ const text = lyric.lyric.text;
1099
+ syllables.push({
1100
+ text,
1101
+ position: lyric.position,
1102
+ measureIndex: lyric.measureIndex
1103
+ });
1104
+ const syllabic = lyric.lyric.syllabic;
1105
+ if (syllabic === "begin" || syllabic === "middle") {
1106
+ textParts.push(text + "-");
1107
+ } else if (syllabic === "end") {
1108
+ textParts.push(text + " ");
1109
+ } else {
1110
+ textParts.push(text + " ");
1111
+ }
1112
+ }
1113
+ results.push({
1114
+ verse,
1115
+ text: textParts.join("").trim(),
1116
+ syllables
1117
+ });
1118
+ }
1119
+ return results.sort((a, b) => a.verse - b.verse);
1120
+ }
1121
+ function getVerseCount(score, options) {
1122
+ const verses = /* @__PURE__ */ new Set();
1123
+ const startPart = options?.partIndex ?? 0;
1124
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1125
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1126
+ const part = score.parts[partIndex];
1127
+ if (!part) continue;
1128
+ for (const measure of part.measures) {
1129
+ for (const entry of measure.entries) {
1130
+ if (entry.type === "note" && entry.lyrics) {
1131
+ for (const lyric of entry.lyrics) {
1132
+ verses.add(lyric.number ?? 1);
1133
+ }
1134
+ }
1135
+ }
1136
+ }
1137
+ }
1138
+ return verses.size;
1139
+ }
1140
+ function getRepeatStructure(score, options) {
1141
+ const results = [];
1142
+ const partIndex = options?.partIndex ?? 0;
1143
+ const part = score.parts[partIndex];
1144
+ if (!part) return results;
1145
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1146
+ const measure = part.measures[measureIndex];
1147
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1148
+ if (measure.barlines) {
1149
+ for (const barline of measure.barlines) {
1150
+ if (barline.repeat) {
1151
+ results.push({
1152
+ type: barline.repeat.direction,
1153
+ times: barline.repeat.times,
1154
+ measureIndex,
1155
+ measureNumber
1156
+ });
1157
+ }
1158
+ }
1159
+ }
1160
+ }
1161
+ return results;
1162
+ }
1163
+ function findBarlines(score, options) {
1164
+ const results = [];
1165
+ const startPart = options?.partIndex ?? 0;
1166
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1167
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1168
+ const part = score.parts[partIndex];
1169
+ if (!part) continue;
1170
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1171
+ const measure = part.measures[measureIndex];
1172
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1173
+ if (measure.barlines) {
1174
+ for (const barline of measure.barlines) {
1175
+ if (options?.style !== void 0 && barline.barStyle !== options.style) {
1176
+ continue;
1177
+ }
1178
+ if (options?.repeat !== void 0) {
1179
+ const hasRepeat = barline.repeat !== void 0;
1180
+ if (hasRepeat !== options.repeat) {
1181
+ continue;
1182
+ }
1183
+ }
1184
+ results.push({
1185
+ barline,
1186
+ partIndex,
1187
+ measureIndex,
1188
+ measureNumber
1189
+ });
1190
+ }
1191
+ }
1192
+ }
1193
+ }
1194
+ return results;
1195
+ }
1196
+ function getEndings(score, options) {
1197
+ const results = [];
1198
+ const startPart = options?.partIndex ?? 0;
1199
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1200
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1201
+ const part = score.parts[partIndex];
1202
+ if (!part) continue;
1203
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1204
+ const measure = part.measures[measureIndex];
1205
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1206
+ if (measure.barlines) {
1207
+ for (const barline of measure.barlines) {
1208
+ if (barline.ending) {
1209
+ results.push({
1210
+ number: barline.ending.number,
1211
+ type: barline.ending.type,
1212
+ partIndex,
1213
+ measureIndex,
1214
+ measureNumber
1215
+ });
1216
+ }
1217
+ }
1218
+ }
1219
+ }
1220
+ }
1221
+ return results;
1222
+ }
1223
+ function getKeyChanges(score, options) {
1224
+ const results = [];
1225
+ const startPart = options?.partIndex ?? 0;
1226
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1227
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1228
+ const part = score.parts[partIndex];
1229
+ if (!part) continue;
1230
+ let lastKey;
1231
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1232
+ const measure = part.measures[measureIndex];
1233
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1234
+ const state = createPositionState();
1235
+ if (measure.attributes?.key) {
1236
+ const key = measure.attributes.key;
1237
+ if (!lastKey || lastKey.fifths !== key.fifths || lastKey.mode !== key.mode) {
1238
+ results.push({
1239
+ key,
1240
+ partIndex,
1241
+ measureIndex,
1242
+ measureNumber,
1243
+ position: 0
1244
+ });
1245
+ lastKey = key;
1246
+ }
1247
+ }
1248
+ for (const entry of measure.entries) {
1249
+ if (entry.type === "attributes" && entry.attributes.key) {
1250
+ const key = entry.attributes.key;
1251
+ if (!lastKey || lastKey.fifths !== key.fifths || lastKey.mode !== key.mode) {
1252
+ results.push({
1253
+ key,
1254
+ partIndex,
1255
+ measureIndex,
1256
+ measureNumber,
1257
+ position: state.position
1258
+ });
1259
+ lastKey = key;
1260
+ }
1261
+ }
1262
+ updatePositionForEntry(state, entry);
1263
+ }
1264
+ }
1265
+ }
1266
+ return results;
1267
+ }
1268
+ function getTimeChanges(score, options) {
1269
+ const results = [];
1270
+ const startPart = options?.partIndex ?? 0;
1271
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1272
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1273
+ const part = score.parts[partIndex];
1274
+ if (!part) continue;
1275
+ let lastTime;
1276
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1277
+ const measure = part.measures[measureIndex];
1278
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1279
+ if (measure.attributes?.time) {
1280
+ const time = measure.attributes.time;
1281
+ if (!lastTime || lastTime.beats !== time.beats || lastTime.beatType !== time.beatType) {
1282
+ results.push({
1283
+ time,
1284
+ partIndex,
1285
+ measureIndex,
1286
+ measureNumber
1287
+ });
1288
+ lastTime = time;
1289
+ }
1290
+ }
1291
+ for (const entry of measure.entries) {
1292
+ if (entry.type === "attributes" && entry.attributes.time) {
1293
+ const time = entry.attributes.time;
1294
+ if (!lastTime || lastTime.beats !== time.beats || lastTime.beatType !== time.beatType) {
1295
+ results.push({
1296
+ time,
1297
+ partIndex,
1298
+ measureIndex,
1299
+ measureNumber
1300
+ });
1301
+ lastTime = time;
1302
+ }
1303
+ }
1304
+ }
1305
+ }
1306
+ }
1307
+ return results;
1308
+ }
1309
+ function getClefChanges(score, options) {
1310
+ const results = [];
1311
+ const startPart = options?.partIndex ?? 0;
1312
+ const endPart = options?.partIndex !== void 0 ? options.partIndex + 1 : score.parts.length;
1313
+ for (let partIndex = startPart; partIndex < endPart; partIndex++) {
1314
+ const part = score.parts[partIndex];
1315
+ if (!part) continue;
1316
+ const lastClefs = /* @__PURE__ */ new Map();
1317
+ for (let measureIndex = 0; measureIndex < part.measures.length; measureIndex++) {
1318
+ const measure = part.measures[measureIndex];
1319
+ const measureNumber = measure.number ?? String(measureIndex + 1);
1320
+ const state = createPositionState();
1321
+ if (measure.attributes?.clef) {
1322
+ for (const clef of measure.attributes.clef) {
1323
+ const staff = clef.staff ?? 1;
1324
+ if (options?.staff !== void 0 && staff !== options.staff) {
1325
+ continue;
1326
+ }
1327
+ const lastClef = lastClefs.get(staff);
1328
+ if (!lastClef || lastClef.sign !== clef.sign || lastClef.line !== clef.line || lastClef.clefOctaveChange !== clef.clefOctaveChange) {
1329
+ results.push({
1330
+ clef,
1331
+ staff,
1332
+ partIndex,
1333
+ measureIndex,
1334
+ measureNumber,
1335
+ position: 0
1336
+ });
1337
+ lastClefs.set(staff, clef);
1338
+ }
1339
+ }
1340
+ }
1341
+ for (const entry of measure.entries) {
1342
+ if (entry.type === "attributes" && entry.attributes.clef) {
1343
+ for (const clef of entry.attributes.clef) {
1344
+ const staff = clef.staff ?? 1;
1345
+ if (options?.staff !== void 0 && staff !== options.staff) {
1346
+ continue;
1347
+ }
1348
+ const lastClef = lastClefs.get(staff);
1349
+ if (!lastClef || lastClef.sign !== clef.sign || lastClef.line !== clef.line || lastClef.clefOctaveChange !== clef.clefOctaveChange) {
1350
+ results.push({
1351
+ clef,
1352
+ staff,
1353
+ partIndex,
1354
+ measureIndex,
1355
+ measureNumber,
1356
+ position: state.position
1357
+ });
1358
+ lastClefs.set(staff, clef);
1359
+ }
1360
+ }
1361
+ }
1362
+ updatePositionForEntry(state, entry);
1363
+ }
1364
+ }
1365
+ }
1366
+ return results;
1367
+ }
1368
+ function getStructuralChanges(score, options) {
1369
+ return {
1370
+ keyChanges: getKeyChanges(score, options),
1371
+ timeChanges: getTimeChanges(score, options),
1372
+ clefChanges: getClefChanges(score, options)
1373
+ };
1374
+ }
1375
+ function getPartByIndex(score, index) {
1376
+ return score.parts[index];
1377
+ }
1378
+ function getPartCount(score) {
1379
+ return score.parts.length;
1380
+ }
1381
+ function getPartIds(score) {
1382
+ return score.parts.map((part) => part.id);
1383
+ }
181
1384
  export {
1385
+ buildVoiceToStaffMap,
1386
+ buildVoiceToStaffMapForPart,
1387
+ findBarlines,
1388
+ findDirectionsByType,
1389
+ findNotesWithNotation,
182
1390
  getAbsolutePosition,
1391
+ getAdjacentNotes,
183
1392
  getAllNotes,
1393
+ getBeamGroups,
1394
+ getChordProgression,
184
1395
  getChords,
1396
+ getClefChanges,
1397
+ getClefForStaff,
1398
+ getDirections,
1399
+ getDirectionsAtPosition,
1400
+ getDynamics,
1401
+ getEffectiveStaff,
1402
+ getEndings,
1403
+ getEntriesAtPosition,
1404
+ getEntriesForStaff,
1405
+ getEntriesInRange,
1406
+ getHarmonies,
1407
+ getHarmonyAtPosition,
1408
+ getKeyChanges,
1409
+ getLyricText,
1410
+ getLyrics,
1411
+ getNextNote,
185
1412
  getNormalizedDuration,
186
1413
  getNormalizedPosition,
1414
+ getNotesAtPosition,
187
1415
  getNotesForStaff,
188
1416
  getNotesForVoice,
1417
+ getNotesInRange,
1418
+ getOctaveShifts,
1419
+ getPartByIndex,
1420
+ getPartCount,
1421
+ getPartIds,
1422
+ getPedalMarkings,
1423
+ getPrevNote,
1424
+ getRepeatStructure,
1425
+ getSlurSpans,
1426
+ getStaffRange,
189
1427
  getStaves,
1428
+ getStructuralChanges,
1429
+ getTempoMarkings,
1430
+ getTiedNoteGroups,
1431
+ getTimeChanges,
1432
+ getTupletGroups,
1433
+ getVerseCount,
1434
+ getVerticalSlice,
1435
+ getVoiceLine,
1436
+ getVoiceLineInRange,
190
1437
  getVoices,
1438
+ getVoicesForStaff,
1439
+ getWedges,
191
1440
  groupByStaff,
192
1441
  groupByVoice,
193
1442
  hasNotes,
1443
+ inferStaff,
194
1444
  isRestMeasure,
1445
+ iterateEntries,
195
1446
  iterateNotes,
196
1447
  withAbsolutePositions
197
1448
  };
198
- //# sourceMappingURL=index.mjs.map