musicxml-io 0.2.0 → 0.2.7

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.
@@ -1,3 +1,9 @@
1
+ // src/id.ts
2
+ import { nanoid } from "nanoid";
3
+ function generateId() {
4
+ return "i" + nanoid(10);
5
+ }
6
+
1
7
  // src/utils/index.ts
2
8
  var STEPS = ["C", "D", "E", "F", "G", "A", "B"];
3
9
  var STEP_SEMITONES = {
@@ -9,6 +15,128 @@ var STEP_SEMITONES = {
9
15
  "A": 9,
10
16
  "B": 11
11
17
  };
18
+ var SHARP_ORDER = ["F", "C", "G", "D", "A", "E", "B"];
19
+ var FLAT_ORDER = ["B", "E", "A", "D", "G", "C", "F"];
20
+ function pitchToSemitone(pitch) {
21
+ return pitch.octave * 12 + STEP_SEMITONES[pitch.step] + (pitch.alter ?? 0);
22
+ }
23
+ function getAlterForStepInKey(step, key) {
24
+ const fifths = key.fifths;
25
+ if (fifths > 0) {
26
+ const sharps = SHARP_ORDER.slice(0, fifths);
27
+ return sharps.includes(step) ? 1 : 0;
28
+ } else if (fifths < 0) {
29
+ const flats = FLAT_ORDER.slice(0, -fifths);
30
+ return flats.includes(step) ? -1 : 0;
31
+ }
32
+ return 0;
33
+ }
34
+ function getAlteredStepsInKey(key) {
35
+ const alterations = /* @__PURE__ */ new Map();
36
+ const fifths = key.fifths;
37
+ if (fifths > 0) {
38
+ SHARP_ORDER.slice(0, fifths).forEach((step) => alterations.set(step, 1));
39
+ } else if (fifths < 0) {
40
+ FLAT_ORDER.slice(0, -fifths).forEach((step) => alterations.set(step, -1));
41
+ }
42
+ return alterations;
43
+ }
44
+ function getAccidentalsInMeasure(measure, upToPosition, voice) {
45
+ const accidentals = /* @__PURE__ */ new Map();
46
+ let position = 0;
47
+ for (const entry of measure.entries) {
48
+ if (position >= upToPosition) break;
49
+ if (entry.type === "note") {
50
+ if (voice === void 0 || entry.voice === voice) {
51
+ if (entry.pitch && entry.accidental) {
52
+ const key = `${entry.pitch.step}${entry.pitch.octave}`;
53
+ accidentals.set(key, entry.pitch.alter ?? 0);
54
+ }
55
+ }
56
+ if (!entry.chord) {
57
+ position += entry.duration;
58
+ }
59
+ } else if (entry.type === "backup") {
60
+ position -= entry.duration;
61
+ } else if (entry.type === "forward") {
62
+ position += entry.duration;
63
+ }
64
+ }
65
+ return accidentals;
66
+ }
67
+ function semitoneToKeyAwarePitch(semitone, key, options) {
68
+ const octave = Math.floor(semitone / 12);
69
+ const pitchClass = (semitone % 12 + 12) % 12;
70
+ const keyPreferSharp = key.fifths >= 0;
71
+ const preferSharp = options?.preferSharp ?? keyPreferSharp;
72
+ for (const step of STEPS) {
73
+ const stepSemitone = STEP_SEMITONES[step];
74
+ if (stepSemitone === pitchClass) {
75
+ return { step, octave };
76
+ }
77
+ }
78
+ const keyAlterations = getAlteredStepsInKey(key);
79
+ for (const step of STEPS) {
80
+ const stepSemitone = STEP_SEMITONES[step];
81
+ const keyAlter = keyAlterations.get(step) ?? 0;
82
+ if ((stepSemitone + keyAlter) % 12 === pitchClass) {
83
+ return { step, octave, alter: keyAlter };
84
+ }
85
+ }
86
+ if (preferSharp) {
87
+ for (const step of STEPS) {
88
+ const stepSemitone = STEP_SEMITONES[step];
89
+ const diff = (pitchClass - stepSemitone + 12) % 12;
90
+ if (diff === 1) {
91
+ return { step, octave, alter: 1 };
92
+ }
93
+ }
94
+ for (const step of STEPS) {
95
+ const stepSemitone = STEP_SEMITONES[step];
96
+ const diff = (pitchClass - stepSemitone + 12) % 12;
97
+ if (diff === 2) {
98
+ return { step, octave, alter: 2 };
99
+ }
100
+ }
101
+ } else {
102
+ for (const step of STEPS) {
103
+ const stepSemitone = STEP_SEMITONES[step];
104
+ const diff = (stepSemitone - pitchClass + 12) % 12;
105
+ if (diff === 1) {
106
+ return { step, octave, alter: -1 };
107
+ }
108
+ }
109
+ for (const step of STEPS) {
110
+ const stepSemitone = STEP_SEMITONES[step];
111
+ const diff = (stepSemitone - pitchClass + 12) % 12;
112
+ if (diff === 2) {
113
+ return { step, octave, alter: -2 };
114
+ }
115
+ }
116
+ }
117
+ return { step: "C", octave, alter: pitchClass };
118
+ }
119
+ function determineAccidental(pitch, key, accidentalsInMeasure) {
120
+ const noteKey = `${pitch.step}${pitch.octave}`;
121
+ const alter = pitch.alter ?? 0;
122
+ const keyAlter = getAlterForStepInKey(pitch.step, key);
123
+ const previousAlter = accidentalsInMeasure.get(noteKey);
124
+ if (previousAlter !== void 0) {
125
+ if (alter === previousAlter) {
126
+ return void 0;
127
+ }
128
+ } else {
129
+ if (alter === keyAlter) {
130
+ return void 0;
131
+ }
132
+ }
133
+ if (alter === 0) return "natural";
134
+ if (alter === 1) return "sharp";
135
+ if (alter === -1) return "flat";
136
+ if (alter === 2) return "double-sharp";
137
+ if (alter === -2) return "double-flat";
138
+ return void 0;
139
+ }
12
140
  function createPositionState() {
13
141
  return { position: 0, lastNonChordPosition: 0 };
14
142
  }
@@ -35,6 +163,14 @@ function updatePositionForEntry(state, entry) {
35
163
  return pos;
36
164
  }
37
165
  }
166
+ function getAbsolutePositionForNote(note, measure) {
167
+ const state = createPositionState();
168
+ for (const entry of measure.entries) {
169
+ if (entry === note) return entry.chord ? state.lastNonChordPosition : state.position;
170
+ updatePositionForEntry(state, entry);
171
+ }
172
+ return state.position;
173
+ }
38
174
  function getMeasureEndPosition(measure) {
39
175
  const state = createPositionState();
40
176
  for (const entry of measure.entries) updatePositionForEntry(state, entry);
@@ -904,6 +1040,27 @@ function getMeasureContext(score, partIndex, measureIndex) {
904
1040
  };
905
1041
  }
906
1042
 
1043
+ // src/query/index.ts
1044
+ function getAttributesAtMeasure(score, options) {
1045
+ const part = score.parts[options.part];
1046
+ if (!part) return {};
1047
+ const targetMeasure = parseInt(String(options.measure), 10);
1048
+ const result = {};
1049
+ for (const m of part.measures) {
1050
+ const mNum = parseInt(m.number, 10);
1051
+ if (!isNaN(targetMeasure) && !isNaN(mNum) && mNum > targetMeasure) break;
1052
+ if (m.attributes) {
1053
+ if (m.attributes.divisions !== void 0) result.divisions = m.attributes.divisions;
1054
+ if (m.attributes.time !== void 0) result.time = m.attributes.time;
1055
+ if (m.attributes.key !== void 0) result.key = m.attributes.key;
1056
+ if (m.attributes.clef !== void 0) result.clef = m.attributes.clef;
1057
+ if (m.attributes.staves !== void 0) result.staves = m.attributes.staves;
1058
+ if (m.attributes.transpose !== void 0) result.transpose = m.attributes.transpose;
1059
+ }
1060
+ }
1061
+ return result;
1062
+ }
1063
+
907
1064
  // src/operations/index.ts
908
1065
  function success(data, warnings) {
909
1066
  return { success: true, data, warnings };
@@ -923,6 +1080,34 @@ function operationError(code, message, location = {}, details) {
923
1080
  function cloneScore(score) {
924
1081
  return JSON.parse(JSON.stringify(score));
925
1082
  }
1083
+ function cloneNoteWithNewId(note) {
1084
+ const cloned = JSON.parse(JSON.stringify(note));
1085
+ cloned._id = generateId();
1086
+ return cloned;
1087
+ }
1088
+ function cloneEntryWithNewId(entry) {
1089
+ const cloned = JSON.parse(JSON.stringify(entry));
1090
+ cloned._id = generateId();
1091
+ return cloned;
1092
+ }
1093
+ function cloneMeasureWithNewIds(measure) {
1094
+ const cloned = JSON.parse(JSON.stringify(measure));
1095
+ cloned._id = generateId();
1096
+ cloned.entries = cloned.entries.map((entry) => cloneEntryWithNewId(entry));
1097
+ if (cloned.barlines) {
1098
+ cloned.barlines = cloned.barlines.map((barline) => ({
1099
+ ...barline,
1100
+ _id: generateId()
1101
+ }));
1102
+ }
1103
+ return cloned;
1104
+ }
1105
+ function clonePartWithNewIds(part) {
1106
+ const cloned = JSON.parse(JSON.stringify(part));
1107
+ cloned._id = generateId();
1108
+ cloned.measures = cloned.measures.map((measure) => cloneMeasureWithNewIds(measure));
1109
+ return cloned;
1110
+ }
926
1111
  function getMeasureDuration(divisions, time) {
927
1112
  const beats = parseInt(time.beats, 10);
928
1113
  if (isNaN(beats)) return divisions * 4;
@@ -985,6 +1170,7 @@ function hasNotesInRange(voiceEntries, startPos, endPos) {
985
1170
  }
986
1171
  function createRest(duration, voice, staff) {
987
1172
  return {
1173
+ _id: generateId(),
988
1174
  type: "note",
989
1175
  rest: { displayStep: void 0, displayOctave: void 0 },
990
1176
  duration,
@@ -1046,10 +1232,11 @@ function rebuildMeasureWithVoice(measure, voice, newEntries, measureDuration, st
1046
1232
  for (const { position: targetPos, entry } of allEntries) {
1047
1233
  const diff = targetPos - currentPosition;
1048
1234
  if (diff < 0) {
1049
- result.push({ type: "backup", duration: -diff });
1235
+ result.push({ _id: generateId(), type: "backup", duration: -diff });
1050
1236
  currentPosition = targetPos;
1051
1237
  } else if (diff > 0) {
1052
1238
  result.push({
1239
+ _id: generateId(),
1053
1240
  type: "forward",
1054
1241
  duration: diff,
1055
1242
  voice: entry.type === "note" ? entry.voice : 1,
@@ -1104,6 +1291,7 @@ function insertNote(score, options) {
1104
1291
  )]);
1105
1292
  }
1106
1293
  const newNote = {
1294
+ _id: generateId(),
1107
1295
  type: "note",
1108
1296
  pitch: options.pitch,
1109
1297
  duration: options.duration,
@@ -1210,6 +1398,7 @@ function addChord(score, options) {
1210
1398
  return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1211
1399
  }
1212
1400
  const chordNote = {
1401
+ _id: generateId(),
1213
1402
  type: "note",
1214
1403
  pitch: options.pitch,
1215
1404
  duration: targetEntry.duration,
@@ -1360,6 +1549,161 @@ function setNotePitch(score, options) {
1360
1549
  }
1361
1550
  return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1362
1551
  }
1552
+ function setNotePitchBySemitone(score, options) {
1553
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
1554
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
1555
+ }
1556
+ const part = score.parts[options.partIndex];
1557
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
1558
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1559
+ }
1560
+ const result = cloneScore(score);
1561
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
1562
+ const measureNumber = measure.number ?? String(options.measureIndex + 1);
1563
+ const attrs = getAttributesAtMeasure(result, { part: options.partIndex, measure: measureNumber });
1564
+ const keySignature = attrs.key ?? { fifths: 0 };
1565
+ let noteCount = 0;
1566
+ for (const entry of measure.entries) {
1567
+ if (entry.type === "note" && !entry.rest) {
1568
+ if (noteCount === options.noteIndex) {
1569
+ const notePosition = getAbsolutePositionForNote(entry, measure);
1570
+ const accidentalsInMeasure = getAccidentalsInMeasure(measure, notePosition, entry.voice);
1571
+ const newPitch = semitoneToKeyAwarePitch(options.semitone, keySignature, {
1572
+ preferSharp: options.preferSharp
1573
+ });
1574
+ const accidental = determineAccidental(newPitch, keySignature, accidentalsInMeasure);
1575
+ entry.pitch = newPitch;
1576
+ if (accidental) {
1577
+ entry.accidental = { value: accidental };
1578
+ } else {
1579
+ delete entry.accidental;
1580
+ }
1581
+ return success(result);
1582
+ }
1583
+ noteCount++;
1584
+ }
1585
+ }
1586
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1587
+ }
1588
+ function shiftNotePitch(score, options) {
1589
+ if (options.semitones === 0) {
1590
+ return success(score);
1591
+ }
1592
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
1593
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
1594
+ }
1595
+ const part = score.parts[options.partIndex];
1596
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
1597
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1598
+ }
1599
+ const measure = part.measures[options.measureIndex];
1600
+ let noteCount = 0;
1601
+ let currentSemitone = null;
1602
+ for (const entry of measure.entries) {
1603
+ if (entry.type === "note" && !entry.rest) {
1604
+ if (noteCount === options.noteIndex) {
1605
+ if (!entry.pitch) {
1606
+ return failure([operationError("NOTE_NOT_FOUND", "Note has no pitch", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1607
+ }
1608
+ currentSemitone = pitchToSemitone(entry.pitch);
1609
+ break;
1610
+ }
1611
+ noteCount++;
1612
+ }
1613
+ }
1614
+ if (currentSemitone === null) {
1615
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1616
+ }
1617
+ return setNotePitchBySemitone(score, {
1618
+ partIndex: options.partIndex,
1619
+ measureIndex: options.measureIndex,
1620
+ noteIndex: options.noteIndex,
1621
+ semitone: currentSemitone + options.semitones,
1622
+ preferSharp: options.preferSharp
1623
+ });
1624
+ }
1625
+ function raiseAccidental(score, options) {
1626
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
1627
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
1628
+ }
1629
+ const part = score.parts[options.partIndex];
1630
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
1631
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1632
+ }
1633
+ const result = cloneScore(score);
1634
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
1635
+ const measureNumber = measure.number ?? String(options.measureIndex + 1);
1636
+ const attrs = getAttributesAtMeasure(result, { part: options.partIndex, measure: measureNumber });
1637
+ const keySignature = attrs.key ?? { fifths: 0 };
1638
+ let noteCount = 0;
1639
+ for (const entry of measure.entries) {
1640
+ if (entry.type === "note" && !entry.rest) {
1641
+ if (noteCount === options.noteIndex) {
1642
+ if (!entry.pitch) {
1643
+ return failure([operationError("NOTE_NOT_FOUND", "Note has no pitch", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1644
+ }
1645
+ const currentAlter = entry.pitch.alter ?? 0;
1646
+ const newAlter = currentAlter + 1;
1647
+ if (newAlter > 2) {
1648
+ return failure([operationError("ACCIDENTAL_OUT_OF_BOUNDS", `Cannot raise accidental beyond double-sharp (current: ${currentAlter})`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1649
+ }
1650
+ entry.pitch.alter = newAlter === 0 ? void 0 : newAlter;
1651
+ const notePosition = getAbsolutePositionForNote(entry, measure);
1652
+ const accidentalsInMeasure = getAccidentalsInMeasure(measure, notePosition, entry.voice);
1653
+ const accidental = determineAccidental(entry.pitch, keySignature, accidentalsInMeasure);
1654
+ if (accidental) {
1655
+ entry.accidental = { value: accidental };
1656
+ } else {
1657
+ delete entry.accidental;
1658
+ }
1659
+ return success(result);
1660
+ }
1661
+ noteCount++;
1662
+ }
1663
+ }
1664
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1665
+ }
1666
+ function lowerAccidental(score, options) {
1667
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
1668
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
1669
+ }
1670
+ const part = score.parts[options.partIndex];
1671
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
1672
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1673
+ }
1674
+ const result = cloneScore(score);
1675
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
1676
+ const measureNumber = measure.number ?? String(options.measureIndex + 1);
1677
+ const attrs = getAttributesAtMeasure(result, { part: options.partIndex, measure: measureNumber });
1678
+ const keySignature = attrs.key ?? { fifths: 0 };
1679
+ let noteCount = 0;
1680
+ for (const entry of measure.entries) {
1681
+ if (entry.type === "note" && !entry.rest) {
1682
+ if (noteCount === options.noteIndex) {
1683
+ if (!entry.pitch) {
1684
+ return failure([operationError("NOTE_NOT_FOUND", "Note has no pitch", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1685
+ }
1686
+ const currentAlter = entry.pitch.alter ?? 0;
1687
+ const newAlter = currentAlter - 1;
1688
+ if (newAlter < -2) {
1689
+ return failure([operationError("ACCIDENTAL_OUT_OF_BOUNDS", `Cannot lower accidental beyond double-flat (current: ${currentAlter})`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1690
+ }
1691
+ entry.pitch.alter = newAlter === 0 ? void 0 : newAlter;
1692
+ const notePosition = getAbsolutePositionForNote(entry, measure);
1693
+ const accidentalsInMeasure = getAccidentalsInMeasure(measure, notePosition, entry.voice);
1694
+ const accidental = determineAccidental(entry.pitch, keySignature, accidentalsInMeasure);
1695
+ if (accidental) {
1696
+ entry.accidental = { value: accidental };
1697
+ } else {
1698
+ delete entry.accidental;
1699
+ }
1700
+ return success(result);
1701
+ }
1702
+ noteCount++;
1703
+ }
1704
+ }
1705
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
1706
+ }
1363
1707
  function addVoice(score, options) {
1364
1708
  if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
1365
1709
  return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
@@ -1383,7 +1727,7 @@ function addVoice(score, options) {
1383
1727
  const rest = createRest(measureDuration, options.voice, options.staff);
1384
1728
  const currentEnd = getMeasureEndPosition(measure);
1385
1729
  if (currentEnd > 0) {
1386
- measure.entries.push({ type: "backup", duration: currentEnd });
1730
+ measure.entries.push({ _id: generateId(), type: "backup", duration: currentEnd });
1387
1731
  }
1388
1732
  measure.entries.push(rest);
1389
1733
  return success(result);
@@ -1434,6 +1778,7 @@ function addPart(score, options) {
1434
1778
  const result = cloneScore(score);
1435
1779
  const insertIndex = options.insertIndex ?? result.parts.length;
1436
1780
  const partInfo = {
1781
+ _id: generateId(),
1437
1782
  type: "score-part",
1438
1783
  id: options.id,
1439
1784
  name: options.name,
@@ -1452,10 +1797,10 @@ function addPart(score, options) {
1452
1797
  }
1453
1798
  result.partList.splice(partListInsertIndex, 0, partInfo);
1454
1799
  const measureCount = result.parts.length > 0 ? result.parts[0].measures.length : 1;
1455
- const newPart = { id: options.id, measures: [] };
1800
+ const newPart = { _id: generateId(), id: options.id, measures: [] };
1456
1801
  for (let i = 0; i < measureCount; i++) {
1457
1802
  const measureNumber = result.parts.length > 0 ? result.parts[0].measures[i]?.number ?? String(i + 1) : String(i + 1);
1458
- const measure = { number: measureNumber, entries: [] };
1803
+ const measure = { _id: generateId(), number: measureNumber, entries: [] };
1459
1804
  if (i === 0) {
1460
1805
  measure.attributes = {
1461
1806
  divisions: options.divisions ?? 4,
@@ -1499,10 +1844,11 @@ function duplicatePart(score, options) {
1499
1844
  }
1500
1845
  const result = cloneScore(score);
1501
1846
  const sourcePart = result.parts[sourceIndex];
1502
- const newPart = JSON.parse(JSON.stringify(sourcePart));
1847
+ const newPart = clonePartWithNewIds(sourcePart);
1503
1848
  newPart.id = options.newPartId;
1504
1849
  const sourcePartInfo = result.partList.find((e) => e.type === "score-part" && e.id === options.sourcePartId);
1505
1850
  const newPartInfo = {
1851
+ _id: generateId(),
1506
1852
  type: "score-part",
1507
1853
  id: options.newPartId,
1508
1854
  name: options.newPartName ?? sourcePartInfo?.name,
@@ -1616,7 +1962,7 @@ function insertMeasure(score, options) {
1616
1962
  if (insertIndex === -1) continue;
1617
1963
  const numericPart = parseInt(targetMeasure, 10);
1618
1964
  const newMeasureNumber = String(isNaN(numericPart) ? insertIndex + 2 : numericPart + 1);
1619
- const newMeasure = { number: newMeasureNumber, entries: [] };
1965
+ const newMeasure = { _id: generateId(), number: newMeasureNumber, entries: [] };
1620
1966
  if (options.copyAttributes && part.measures[insertIndex].attributes) {
1621
1967
  newMeasure.attributes = { ...part.measures[insertIndex].attributes };
1622
1968
  }
@@ -1646,84 +1992,2826 @@ function deleteMeasure(score, measureNumber) {
1646
1992
  }
1647
1993
  return result;
1648
1994
  }
1649
- var addNote = (score, options) => {
1650
- const result = insertNote(score, {
1651
- partIndex: options.partIndex,
1652
- measureIndex: options.measureIndex,
1653
- voice: options.voice,
1654
- staff: options.staff,
1655
- position: options.position,
1656
- pitch: options.note.pitch ?? { step: "C", octave: 4 },
1657
- duration: options.note.duration,
1658
- noteType: options.note.noteType,
1659
- dots: options.note.dots
1995
+ function findNoteByIndex(measure, noteIndex) {
1996
+ let noteCount = 0;
1997
+ for (let i = 0; i < measure.entries.length; i++) {
1998
+ const entry = measure.entries[i];
1999
+ if (entry.type === "note" && !entry.rest) {
2000
+ if (noteCount === noteIndex) {
2001
+ return { note: entry, entryIndex: i };
2002
+ }
2003
+ noteCount++;
2004
+ }
2005
+ }
2006
+ return null;
2007
+ }
2008
+ function pitchesEqual(p1, p2) {
2009
+ return p1.step === p2.step && p1.octave === p2.octave && (p1.alter ?? 0) === (p2.alter ?? 0);
2010
+ }
2011
+ function addTie(score, options) {
2012
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2013
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2014
+ }
2015
+ const part = score.parts[options.partIndex];
2016
+ if (options.startMeasureIndex < 0 || options.startMeasureIndex >= part.measures.length) {
2017
+ return failure([operationError("MEASURE_NOT_FOUND", `Start measure index ${options.startMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
2018
+ }
2019
+ if (options.endMeasureIndex < 0 || options.endMeasureIndex >= part.measures.length) {
2020
+ return failure([operationError("MEASURE_NOT_FOUND", `End measure index ${options.endMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.endMeasureIndex })]);
2021
+ }
2022
+ const result = cloneScore(score);
2023
+ const startMeasure = result.parts[options.partIndex].measures[options.startMeasureIndex];
2024
+ const endMeasure = result.parts[options.partIndex].measures[options.endMeasureIndex];
2025
+ const startResult = findNoteByIndex(startMeasure, options.startNoteIndex);
2026
+ if (!startResult) {
2027
+ return failure([operationError("NOTE_NOT_FOUND", `Start note index ${options.startNoteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
2028
+ }
2029
+ const endResult = findNoteByIndex(endMeasure, options.endNoteIndex);
2030
+ if (!endResult) {
2031
+ return failure([operationError("NOTE_NOT_FOUND", `End note index ${options.endNoteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.endMeasureIndex })]);
2032
+ }
2033
+ const startNote = startResult.note;
2034
+ const endNote = endResult.note;
2035
+ if (!startNote.pitch || !endNote.pitch) {
2036
+ return failure([operationError("TIE_INVALID_TARGET", "Cannot tie notes without pitch", { partIndex: options.partIndex })]);
2037
+ }
2038
+ if (!pitchesEqual(startNote.pitch, endNote.pitch)) {
2039
+ return failure([operationError("TIE_PITCH_MISMATCH", "Tied notes must have the same pitch", { partIndex: options.partIndex }, { startPitch: startNote.pitch, endPitch: endNote.pitch })]);
2040
+ }
2041
+ if (startNote.tie?.type === "start" || startNote.tie?.type === "continue") {
2042
+ return failure([operationError("TIE_ALREADY_EXISTS", "Start note already has a tie start", { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
2043
+ }
2044
+ startNote.tie = { type: "start" };
2045
+ if (!startNote.notations) startNote.notations = [];
2046
+ startNote.notations.push({ type: "tied", tiedType: "start" });
2047
+ endNote.tie = { type: "stop" };
2048
+ if (!endNote.notations) endNote.notations = [];
2049
+ endNote.notations.push({ type: "tied", tiedType: "stop" });
2050
+ const validationResult = validate(result, { checkTies: true });
2051
+ const criticalErrors = validationResult.errors.filter((e) => e.level === "error");
2052
+ if (criticalErrors.length > 0) {
2053
+ return failure(criticalErrors);
2054
+ }
2055
+ return success(result, validationResult.warnings);
2056
+ }
2057
+ function removeTie(score, options) {
2058
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2059
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2060
+ }
2061
+ const part = score.parts[options.partIndex];
2062
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2063
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2064
+ }
2065
+ const result = cloneScore(score);
2066
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2067
+ const noteResult = findNoteByIndex(measure, options.noteIndex);
2068
+ if (!noteResult) {
2069
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2070
+ }
2071
+ const note = noteResult.note;
2072
+ if (!note.tie) {
2073
+ return failure([operationError("TIE_NOT_FOUND", "Note does not have a tie", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2074
+ }
2075
+ delete note.tie;
2076
+ delete note.ties;
2077
+ if (note.notations) {
2078
+ note.notations = note.notations.filter((n) => n.type !== "tied");
2079
+ if (note.notations.length === 0) {
2080
+ delete note.notations;
2081
+ }
2082
+ }
2083
+ return success(result);
2084
+ }
2085
+ function addSlur(score, options) {
2086
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2087
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2088
+ }
2089
+ const part = score.parts[options.partIndex];
2090
+ if (options.startMeasureIndex < 0 || options.startMeasureIndex >= part.measures.length) {
2091
+ return failure([operationError("MEASURE_NOT_FOUND", `Start measure index ${options.startMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
2092
+ }
2093
+ if (options.endMeasureIndex < 0 || options.endMeasureIndex >= part.measures.length) {
2094
+ return failure([operationError("MEASURE_NOT_FOUND", `End measure index ${options.endMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.endMeasureIndex })]);
2095
+ }
2096
+ const result = cloneScore(score);
2097
+ const startMeasure = result.parts[options.partIndex].measures[options.startMeasureIndex];
2098
+ const endMeasure = result.parts[options.partIndex].measures[options.endMeasureIndex];
2099
+ const startResult = findNoteByIndex(startMeasure, options.startNoteIndex);
2100
+ if (!startResult) {
2101
+ return failure([operationError("NOTE_NOT_FOUND", `Start note index ${options.startNoteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
2102
+ }
2103
+ const endResult = findNoteByIndex(endMeasure, options.endNoteIndex);
2104
+ if (!endResult) {
2105
+ return failure([operationError("NOTE_NOT_FOUND", `End note index ${options.endNoteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.endMeasureIndex })]);
2106
+ }
2107
+ const startNote = startResult.note;
2108
+ const endNote = endResult.note;
2109
+ const slurNumber = options.number ?? 1;
2110
+ if (startNote.notations?.some((n) => n.type === "slur" && n.slurType === "start" && (n.number ?? 1) === slurNumber)) {
2111
+ return failure([operationError("SLUR_ALREADY_EXISTS", `Slur ${slurNumber} already starts on this note`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
2112
+ }
2113
+ if (!startNote.notations) startNote.notations = [];
2114
+ startNote.notations.push({
2115
+ type: "slur",
2116
+ slurType: "start",
2117
+ number: slurNumber,
2118
+ placement: options.placement
1660
2119
  });
1661
- return result.success ? result.data : score;
1662
- };
1663
- var deleteNote = (score, options) => {
1664
- const result = removeNote(score, options);
1665
- return result.success ? result.data : score;
1666
- };
1667
- var addChordNote = (score, options) => {
1668
- const result = addChord(score, { ...options, noteIndex: options.afterNoteIndex });
1669
- return result.success ? result.data : score;
1670
- };
1671
- var modifyNotePitch = (score, options) => {
1672
- const result = setNotePitch(score, options);
1673
- return result.success ? result.data : score;
1674
- };
1675
- var modifyNoteDuration = (score, options) => {
1676
- const result = changeNoteDuration(score, { ...options, newDuration: options.duration });
1677
- return result.success ? result.data : score;
1678
- };
1679
- var addNoteChecked = (score, options) => {
1680
- return insertNote(score, {
1681
- partIndex: options.partIndex,
1682
- measureIndex: options.measureIndex,
1683
- voice: options.voice,
1684
- staff: options.staff,
1685
- position: options.position,
1686
- pitch: options.note.pitch ?? { step: "C", octave: 4 },
1687
- duration: options.note.duration,
1688
- noteType: options.note.noteType,
1689
- dots: options.note.dots
2120
+ if (!endNote.notations) endNote.notations = [];
2121
+ endNote.notations.push({
2122
+ type: "slur",
2123
+ slurType: "stop",
2124
+ number: slurNumber
1690
2125
  });
1691
- };
1692
- var deleteNoteChecked = removeNote;
1693
- var addChordNoteChecked = (score, options) => {
1694
- return addChord(score, { ...options, noteIndex: options.afterNoteIndex });
1695
- };
1696
- var modifyNotePitchChecked = setNotePitch;
1697
- var modifyNoteDurationChecked = (score, options) => {
2126
+ const validationResult = validate(result, { checkSlurs: true });
2127
+ const criticalErrors = validationResult.errors.filter((e) => e.level === "error");
2128
+ if (criticalErrors.length > 0) {
2129
+ return failure(criticalErrors);
2130
+ }
2131
+ return success(result, validationResult.warnings);
2132
+ }
2133
+ function removeSlur(score, options) {
2134
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2135
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2136
+ }
2137
+ const part = score.parts[options.partIndex];
2138
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2139
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2140
+ }
2141
+ const result = cloneScore(score);
2142
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2143
+ const noteResult = findNoteByIndex(measure, options.noteIndex);
2144
+ if (!noteResult) {
2145
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2146
+ }
2147
+ const note = noteResult.note;
2148
+ const slurNumber = options.number ?? 1;
2149
+ if (!note.notations?.some((n) => n.type === "slur" && (n.number ?? 1) === slurNumber)) {
2150
+ return failure([operationError("SLUR_NOT_FOUND", `Slur ${slurNumber} not found on this note`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2151
+ }
2152
+ note.notations = note.notations.filter((n) => !(n.type === "slur" && (n.number ?? 1) === slurNumber));
2153
+ if (note.notations.length === 0) {
2154
+ delete note.notations;
2155
+ }
2156
+ return success(result);
2157
+ }
2158
+ function addArticulation(score, options) {
2159
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2160
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2161
+ }
2162
+ const part = score.parts[options.partIndex];
2163
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2164
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2165
+ }
2166
+ const result = cloneScore(score);
2167
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2168
+ const noteResult = findNoteByIndex(measure, options.noteIndex);
2169
+ if (!noteResult) {
2170
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2171
+ }
2172
+ const note = noteResult.note;
2173
+ if (note.notations?.some((n) => n.type === "articulation" && n.articulation === options.articulation)) {
2174
+ return failure([operationError("ARTICULATION_ALREADY_EXISTS", `Articulation ${options.articulation} already exists on this note`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2175
+ }
2176
+ if (!note.notations) note.notations = [];
2177
+ note.notations.push({
2178
+ type: "articulation",
2179
+ articulation: options.articulation,
2180
+ placement: options.placement
2181
+ });
2182
+ return success(result);
2183
+ }
2184
+ function removeArticulation(score, options) {
2185
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2186
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2187
+ }
2188
+ const part = score.parts[options.partIndex];
2189
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2190
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2191
+ }
2192
+ const result = cloneScore(score);
2193
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2194
+ const noteResult = findNoteByIndex(measure, options.noteIndex);
2195
+ if (!noteResult) {
2196
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2197
+ }
2198
+ const note = noteResult.note;
2199
+ if (!note.notations?.some((n) => n.type === "articulation" && n.articulation === options.articulation)) {
2200
+ return failure([operationError("ARTICULATION_NOT_FOUND", `Articulation ${options.articulation} not found on this note`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2201
+ }
2202
+ note.notations = note.notations.filter((n) => !(n.type === "articulation" && n.articulation === options.articulation));
2203
+ if (note.notations.length === 0) {
2204
+ delete note.notations;
2205
+ }
2206
+ return success(result);
2207
+ }
2208
+ function getInsertPositionForDirection(measure, targetPosition) {
2209
+ let position = 0;
2210
+ let insertIndex = 0;
2211
+ for (let i = 0; i < measure.entries.length; i++) {
2212
+ const entry = measure.entries[i];
2213
+ if (position >= targetPosition) {
2214
+ return insertIndex;
2215
+ }
2216
+ if (entry.type === "note" && !entry.chord) {
2217
+ position += entry.duration;
2218
+ } else if (entry.type === "backup") {
2219
+ position -= entry.duration;
2220
+ } else if (entry.type === "forward") {
2221
+ position += entry.duration;
2222
+ }
2223
+ insertIndex = i + 1;
2224
+ }
2225
+ return insertIndex;
2226
+ }
2227
+ function addDynamics(score, options) {
2228
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2229
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2230
+ }
2231
+ const part = score.parts[options.partIndex];
2232
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2233
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2234
+ }
2235
+ if (options.position < 0) {
2236
+ return failure([operationError("INVALID_POSITION", "Position cannot be negative", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2237
+ }
2238
+ const result = cloneScore(score);
2239
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2240
+ const directionEntry = {
2241
+ _id: generateId(),
2242
+ type: "direction",
2243
+ directionTypes: [{
2244
+ kind: "dynamics",
2245
+ value: options.dynamics
2246
+ }],
2247
+ placement: options.placement ?? "below",
2248
+ staff: options.staff
2249
+ };
2250
+ const insertIndex = getInsertPositionForDirection(measure, options.position);
2251
+ measure.entries.splice(insertIndex, 0, directionEntry);
2252
+ return success(result);
2253
+ }
2254
+ function removeDynamics(score, options) {
2255
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2256
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2257
+ }
2258
+ const part = score.parts[options.partIndex];
2259
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2260
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2261
+ }
2262
+ const result = cloneScore(score);
2263
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2264
+ let directionCount = 0;
2265
+ let targetIndex = -1;
2266
+ for (let i = 0; i < measure.entries.length; i++) {
2267
+ const entry = measure.entries[i];
2268
+ if (entry.type === "direction") {
2269
+ const hasDynamics = entry.directionTypes.some((dt) => dt.kind === "dynamics");
2270
+ if (hasDynamics) {
2271
+ if (directionCount === options.directionIndex) {
2272
+ targetIndex = i;
2273
+ break;
2274
+ }
2275
+ directionCount++;
2276
+ }
2277
+ }
2278
+ }
2279
+ if (targetIndex === -1) {
2280
+ return failure([operationError("DYNAMICS_NOT_FOUND", `Dynamics direction index ${options.directionIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2281
+ }
2282
+ measure.entries.splice(targetIndex, 1);
2283
+ return success(result);
2284
+ }
2285
+ function insertClefChange(score, options) {
2286
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2287
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2288
+ }
2289
+ const part = score.parts[options.partIndex];
2290
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2291
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2292
+ }
2293
+ if (options.position < 0) {
2294
+ return failure([operationError("INVALID_POSITION", "Position cannot be negative", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2295
+ }
2296
+ const validSigns = ["G", "F", "C", "percussion", "TAB"];
2297
+ if (!validSigns.includes(options.clef.sign)) {
2298
+ return failure([operationError("INVALID_CLEF", `Invalid clef sign: ${options.clef.sign}`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2299
+ }
2300
+ const result = cloneScore(score);
2301
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2302
+ if (options.position === 0) {
2303
+ if (!measure.attributes) {
2304
+ measure.attributes = {};
2305
+ }
2306
+ const staff = options.clef.staff ?? 1;
2307
+ if (!measure.attributes.clef) {
2308
+ measure.attributes.clef = [];
2309
+ }
2310
+ const existingIndex = measure.attributes.clef.findIndex((c) => (c.staff ?? 1) === staff);
2311
+ if (existingIndex >= 0) {
2312
+ measure.attributes.clef[existingIndex] = options.clef;
2313
+ } else {
2314
+ measure.attributes.clef.push(options.clef);
2315
+ }
2316
+ } else {
2317
+ const attributesEntry = {
2318
+ _id: generateId(),
2319
+ type: "attributes",
2320
+ attributes: {
2321
+ clef: [options.clef]
2322
+ }
2323
+ };
2324
+ const insertIndex = getInsertPositionForDirection(measure, options.position);
2325
+ measure.entries.splice(insertIndex, 0, attributesEntry);
2326
+ }
2327
+ const validationResult = validate(result, { checkStaffStructure: true });
2328
+ const criticalErrors = validationResult.errors.filter((e) => e.level === "error");
2329
+ if (criticalErrors.length > 0) {
2330
+ return failure(criticalErrors);
2331
+ }
2332
+ return success(result, validationResult.warnings);
2333
+ }
2334
+ var addNote = (score, options) => {
2335
+ const result = insertNote(score, {
2336
+ partIndex: options.partIndex,
2337
+ measureIndex: options.measureIndex,
2338
+ voice: options.voice,
2339
+ staff: options.staff,
2340
+ position: options.position,
2341
+ pitch: options.note.pitch ?? { step: "C", octave: 4 },
2342
+ duration: options.note.duration,
2343
+ noteType: options.note.noteType,
2344
+ dots: options.note.dots
2345
+ });
2346
+ return result.success ? result.data : score;
2347
+ };
2348
+ var deleteNote = (score, options) => {
2349
+ const result = removeNote(score, options);
2350
+ return result.success ? result.data : score;
2351
+ };
2352
+ var addChordNote = (score, options) => {
2353
+ const result = addChord(score, { ...options, noteIndex: options.afterNoteIndex });
2354
+ return result.success ? result.data : score;
2355
+ };
2356
+ var modifyNotePitch = (score, options) => {
2357
+ const result = setNotePitch(score, options);
2358
+ return result.success ? result.data : score;
2359
+ };
2360
+ var modifyNoteDuration = (score, options) => {
2361
+ const result = changeNoteDuration(score, { ...options, newDuration: options.duration });
2362
+ return result.success ? result.data : score;
2363
+ };
2364
+ var addNoteChecked = (score, options) => {
2365
+ return insertNote(score, {
2366
+ partIndex: options.partIndex,
2367
+ measureIndex: options.measureIndex,
2368
+ voice: options.voice,
2369
+ staff: options.staff,
2370
+ position: options.position,
2371
+ pitch: options.note.pitch ?? { step: "C", octave: 4 },
2372
+ duration: options.note.duration,
2373
+ noteType: options.note.noteType,
2374
+ dots: options.note.dots
2375
+ });
2376
+ };
2377
+ var deleteNoteChecked = removeNote;
2378
+ var addChordNoteChecked = (score, options) => {
2379
+ return addChord(score, { ...options, noteIndex: options.afterNoteIndex });
2380
+ };
2381
+ var modifyNotePitchChecked = setNotePitch;
2382
+ var modifyNoteDurationChecked = (score, options) => {
1698
2383
  return changeNoteDuration(score, { ...options, newDuration: options.duration });
1699
2384
  };
1700
2385
  var transposeChecked = transpose;
2386
+ function createTuplet(score, options) {
2387
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2388
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2389
+ }
2390
+ const part = score.parts[options.partIndex];
2391
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2392
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2393
+ }
2394
+ if (options.noteCount < 2) {
2395
+ return failure([operationError("INVALID_DURATION", "Tuplet must contain at least 2 notes", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2396
+ }
2397
+ if (options.actualNotes < 2 || options.normalNotes < 1) {
2398
+ return failure([operationError("INVALID_DURATION", "Invalid tuplet ratio", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2399
+ }
2400
+ const result = cloneScore(score);
2401
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2402
+ const notes = [];
2403
+ let noteCount = 0;
2404
+ for (let i = 0; i < measure.entries.length; i++) {
2405
+ const entry = measure.entries[i];
2406
+ if (entry.type === "note" && !entry.rest && !entry.chord) {
2407
+ if (noteCount >= options.startNoteIndex && noteCount < options.startNoteIndex + options.noteCount) {
2408
+ notes.push({ note: entry, entryIndex: i });
2409
+ }
2410
+ noteCount++;
2411
+ }
2412
+ }
2413
+ if (notes.length !== options.noteCount) {
2414
+ return failure([operationError("NOTE_NOT_FOUND", `Could not find ${options.noteCount} notes starting at index ${options.startNoteIndex}`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2415
+ }
2416
+ const voice = notes[0].note.voice;
2417
+ const staff = notes[0].note.staff;
2418
+ if (!notes.every((n) => n.note.voice === voice)) {
2419
+ return failure([operationError("NOTE_CONFLICT", "All notes in a tuplet must be in the same voice", { partIndex: options.partIndex, measureIndex: options.measureIndex, voice })]);
2420
+ }
2421
+ if (!notes.every((n) => n.note.staff === staff)) {
2422
+ return failure([operationError("NOTE_CONFLICT", "All notes in a tuplet must be on the same staff", { partIndex: options.partIndex, measureIndex: options.measureIndex, staff })]);
2423
+ }
2424
+ const tupletNumber = 1;
2425
+ for (let i = 0; i < notes.length; i++) {
2426
+ const { note } = notes[i];
2427
+ note.timeModification = {
2428
+ actualNotes: options.actualNotes,
2429
+ normalNotes: options.normalNotes
2430
+ };
2431
+ if (!note.notations) note.notations = [];
2432
+ if (i === 0) {
2433
+ note.notations.push({
2434
+ type: "tuplet",
2435
+ tupletType: "start",
2436
+ number: tupletNumber,
2437
+ bracket: options.bracket ?? true,
2438
+ showNumber: options.showNumber ?? "actual",
2439
+ tupletActual: { tupletNumber: options.actualNotes },
2440
+ tupletNormal: { tupletNumber: options.normalNotes }
2441
+ });
2442
+ } else if (i === notes.length - 1) {
2443
+ note.notations.push({
2444
+ type: "tuplet",
2445
+ tupletType: "stop",
2446
+ number: tupletNumber
2447
+ });
2448
+ }
2449
+ }
2450
+ const context = getMeasureContext(result, options.partIndex, options.measureIndex);
2451
+ const errors = validateMeasureLocal(measure, context, {
2452
+ checkTuplets: true,
2453
+ checkMeasureDuration: true
2454
+ });
2455
+ const criticalErrors = errors.filter((e) => e.level === "error");
2456
+ if (criticalErrors.length > 0) {
2457
+ return failure(criticalErrors);
2458
+ }
2459
+ return success(result, errors.filter((e) => e.level !== "error"));
2460
+ }
2461
+ function removeTuplet(score, options) {
2462
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2463
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2464
+ }
2465
+ const part = score.parts[options.partIndex];
2466
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2467
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2468
+ }
2469
+ const result = cloneScore(score);
2470
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2471
+ let noteCount = 0;
2472
+ let targetNote = null;
2473
+ let targetEntryIndex = -1;
2474
+ for (let i = 0; i < measure.entries.length; i++) {
2475
+ const entry = measure.entries[i];
2476
+ if (entry.type === "note" && !entry.rest) {
2477
+ if (noteCount === options.noteIndex) {
2478
+ targetNote = entry;
2479
+ targetEntryIndex = i;
2480
+ break;
2481
+ }
2482
+ noteCount++;
2483
+ }
2484
+ }
2485
+ if (!targetNote || targetEntryIndex === -1) {
2486
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2487
+ }
2488
+ if (!targetNote.timeModification) {
2489
+ return failure([operationError("NOTE_NOT_FOUND", "Note is not part of a tuplet", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2490
+ }
2491
+ const voice = targetNote.voice;
2492
+ const staff = targetNote.staff;
2493
+ const actualNotes = targetNote.timeModification.actualNotes;
2494
+ const normalNotes = targetNote.timeModification.normalNotes;
2495
+ const tupletNotes = [];
2496
+ let inTuplet = false;
2497
+ let currentTupletNumber;
2498
+ for (const entry of measure.entries) {
2499
+ if (entry.type !== "note" || entry.rest) continue;
2500
+ if (entry.voice !== voice || entry.staff !== staff) continue;
2501
+ const hasSameTimeModification = entry.timeModification?.actualNotes === actualNotes && entry.timeModification?.normalNotes === normalNotes;
2502
+ const tupletStart = entry.notations?.find(
2503
+ (n) => n.type === "tuplet" && n.tupletType === "start"
2504
+ );
2505
+ const tupletStop = entry.notations?.find(
2506
+ (n) => n.type === "tuplet" && n.tupletType === "stop" && (currentTupletNumber === void 0 || n.number === currentTupletNumber)
2507
+ );
2508
+ if (tupletStart && tupletStart.type === "tuplet") {
2509
+ inTuplet = true;
2510
+ currentTupletNumber = tupletStart.number;
2511
+ }
2512
+ if (inTuplet && hasSameTimeModification) {
2513
+ tupletNotes.push(entry);
2514
+ }
2515
+ if (tupletStop && inTuplet) {
2516
+ if (tupletNotes.includes(targetNote)) {
2517
+ break;
2518
+ } else {
2519
+ tupletNotes.length = 0;
2520
+ inTuplet = false;
2521
+ currentTupletNumber = void 0;
2522
+ }
2523
+ }
2524
+ }
2525
+ if (tupletNotes.length === 0) {
2526
+ tupletNotes.push(targetNote);
2527
+ }
2528
+ for (const note of tupletNotes) {
2529
+ delete note.timeModification;
2530
+ if (note.notations) {
2531
+ note.notations = note.notations.filter((n) => n.type !== "tuplet");
2532
+ if (note.notations.length === 0) {
2533
+ delete note.notations;
2534
+ }
2535
+ }
2536
+ }
2537
+ return success(result);
2538
+ }
2539
+ function addBeam(score, options) {
2540
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2541
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2542
+ }
2543
+ const part = score.parts[options.partIndex];
2544
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2545
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2546
+ }
2547
+ if (options.noteCount < 2) {
2548
+ return failure([operationError("INVALID_DURATION", "Beam must contain at least 2 notes", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2549
+ }
2550
+ const result = cloneScore(score);
2551
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2552
+ const beamLevel = options.beamLevel ?? 1;
2553
+ const notes = [];
2554
+ let noteCount = 0;
2555
+ for (const entry of measure.entries) {
2556
+ if (entry.type === "note" && !entry.rest && !entry.chord) {
2557
+ if (noteCount >= options.startNoteIndex && noteCount < options.startNoteIndex + options.noteCount) {
2558
+ notes.push(entry);
2559
+ }
2560
+ noteCount++;
2561
+ }
2562
+ }
2563
+ if (notes.length !== options.noteCount) {
2564
+ return failure([operationError("NOTE_NOT_FOUND", `Could not find ${options.noteCount} notes starting at index ${options.startNoteIndex}`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2565
+ }
2566
+ const voice = notes[0].voice;
2567
+ if (!notes.every((n) => n.voice === voice)) {
2568
+ return failure([operationError("NOTE_CONFLICT", "All beamed notes must be in the same voice", { partIndex: options.partIndex, measureIndex: options.measureIndex, voice })]);
2569
+ }
2570
+ for (let i = 0; i < notes.length; i++) {
2571
+ const note = notes[i];
2572
+ if (!note.beam) {
2573
+ note.beam = [];
2574
+ }
2575
+ note.beam = note.beam.filter((b) => b.number !== beamLevel);
2576
+ let beamType;
2577
+ if (i === 0) {
2578
+ beamType = "begin";
2579
+ } else if (i === notes.length - 1) {
2580
+ beamType = "end";
2581
+ } else {
2582
+ beamType = "continue";
2583
+ }
2584
+ note.beam.push({
2585
+ number: beamLevel,
2586
+ type: beamType
2587
+ });
2588
+ }
2589
+ const context = getMeasureContext(result, options.partIndex, options.measureIndex);
2590
+ const errors = validateMeasureLocal(measure, context, { checkBeams: true });
2591
+ const criticalErrors = errors.filter((e) => e.level === "error");
2592
+ if (criticalErrors.length > 0) {
2593
+ return failure(criticalErrors);
2594
+ }
2595
+ return success(result, errors.filter((e) => e.level !== "error"));
2596
+ }
2597
+ function removeBeam(score, options) {
2598
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2599
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2600
+ }
2601
+ const part = score.parts[options.partIndex];
2602
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2603
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2604
+ }
2605
+ const result = cloneScore(score);
2606
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2607
+ let noteCount = 0;
2608
+ let targetNote = null;
2609
+ for (const entry of measure.entries) {
2610
+ if (entry.type === "note" && !entry.rest) {
2611
+ if (noteCount === options.noteIndex) {
2612
+ targetNote = entry;
2613
+ break;
2614
+ }
2615
+ noteCount++;
2616
+ }
2617
+ }
2618
+ if (!targetNote) {
2619
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} not found`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2620
+ }
2621
+ if (!targetNote.beam || targetNote.beam.length === 0) {
2622
+ return failure([operationError("NOTE_NOT_FOUND", "Note is not part of a beam group", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2623
+ }
2624
+ const voice = targetNote.voice;
2625
+ const staff = targetNote.staff;
2626
+ const beamNotes = [];
2627
+ let inBeam = false;
2628
+ const targetBeamLevel = options.beamLevel ?? targetNote.beam[0]?.number ?? 1;
2629
+ for (const entry of measure.entries) {
2630
+ if (entry.type !== "note" || entry.rest) continue;
2631
+ if (entry.voice !== voice || entry.staff !== staff) continue;
2632
+ const beamInfo = entry.beam?.find((b) => b.number === targetBeamLevel);
2633
+ if (!beamInfo) {
2634
+ if (inBeam) {
2635
+ break;
2636
+ }
2637
+ continue;
2638
+ }
2639
+ if (beamInfo.type === "begin") {
2640
+ inBeam = true;
2641
+ beamNotes.push(entry);
2642
+ } else if (beamInfo.type === "continue") {
2643
+ if (inBeam) beamNotes.push(entry);
2644
+ } else if (beamInfo.type === "end") {
2645
+ beamNotes.push(entry);
2646
+ if (beamNotes.includes(targetNote)) {
2647
+ break;
2648
+ } else {
2649
+ beamNotes.length = 0;
2650
+ inBeam = false;
2651
+ }
2652
+ }
2653
+ }
2654
+ if (beamNotes.length === 0) {
2655
+ beamNotes.push(targetNote);
2656
+ }
2657
+ for (const note of beamNotes) {
2658
+ if (note.beam) {
2659
+ if (options.beamLevel !== void 0) {
2660
+ note.beam = note.beam.filter((b) => b.number !== options.beamLevel);
2661
+ } else {
2662
+ note.beam = [];
2663
+ }
2664
+ if (note.beam.length === 0) {
2665
+ delete note.beam;
2666
+ }
2667
+ }
2668
+ }
2669
+ return success(result);
2670
+ }
2671
+ function autoBeam(score, options) {
2672
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2673
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2674
+ }
2675
+ const part = score.parts[options.partIndex];
2676
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2677
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2678
+ }
2679
+ const result = cloneScore(score);
2680
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2681
+ const context = getMeasureContext(result, options.partIndex, options.measureIndex);
2682
+ const divisions = context.divisions;
2683
+ const time = context.time ?? { beats: "4", beatType: 4 };
2684
+ const beatDuration = 4 / time.beatType * divisions;
2685
+ for (const entry of measure.entries) {
2686
+ if (entry.type === "note") {
2687
+ delete entry.beam;
2688
+ }
2689
+ }
2690
+ const notesByVoice = /* @__PURE__ */ new Map();
2691
+ let position = 0;
2692
+ for (const entry of measure.entries) {
2693
+ if (entry.type === "note") {
2694
+ if (!entry.chord && !entry.rest) {
2695
+ const voice = entry.voice;
2696
+ if (options.voice === void 0 || voice === options.voice) {
2697
+ if (!notesByVoice.has(voice)) {
2698
+ notesByVoice.set(voice, []);
2699
+ }
2700
+ notesByVoice.get(voice).push({ note: entry, position });
2701
+ }
2702
+ }
2703
+ if (!entry.chord) {
2704
+ position += entry.duration;
2705
+ }
2706
+ } else if (entry.type === "backup") {
2707
+ position -= entry.duration;
2708
+ } else if (entry.type === "forward") {
2709
+ position += entry.duration;
2710
+ }
2711
+ }
2712
+ for (const [, notes] of notesByVoice) {
2713
+ const beatGroups = [];
2714
+ let currentBeat = -1;
2715
+ let currentGroup = [];
2716
+ for (const { note, position: notePos } of notes) {
2717
+ if (note.duration > beatDuration / 2) {
2718
+ if (currentGroup.length >= 2) {
2719
+ beatGroups.push(currentGroup);
2720
+ }
2721
+ currentGroup = [];
2722
+ currentBeat = -1;
2723
+ continue;
2724
+ }
2725
+ const beat = Math.floor(notePos / beatDuration);
2726
+ if (options.groupByBeat !== false && beat !== currentBeat) {
2727
+ if (currentGroup.length >= 2) {
2728
+ beatGroups.push(currentGroup);
2729
+ }
2730
+ currentGroup = [{ note, position: notePos }];
2731
+ currentBeat = beat;
2732
+ } else {
2733
+ currentGroup.push({ note, position: notePos });
2734
+ }
2735
+ }
2736
+ if (currentGroup.length >= 2) {
2737
+ beatGroups.push(currentGroup);
2738
+ }
2739
+ for (const group of beatGroups) {
2740
+ for (let i = 0; i < group.length; i++) {
2741
+ const { note } = group[i];
2742
+ if (!note.beam) {
2743
+ note.beam = [];
2744
+ }
2745
+ let beamType;
2746
+ if (i === 0) {
2747
+ beamType = "begin";
2748
+ } else if (i === group.length - 1) {
2749
+ beamType = "end";
2750
+ } else {
2751
+ beamType = "continue";
2752
+ }
2753
+ note.beam.push({
2754
+ number: 1,
2755
+ type: beamType
2756
+ });
2757
+ }
2758
+ }
2759
+ }
2760
+ const errors = validateMeasureLocal(measure, context, { checkBeams: true });
2761
+ const criticalErrors = errors.filter((e) => e.level === "error");
2762
+ if (criticalErrors.length > 0) {
2763
+ return failure(criticalErrors);
2764
+ }
2765
+ return success(result, errors.filter((e) => e.level !== "error"));
2766
+ }
2767
+ function copyNotes(score, options) {
2768
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2769
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2770
+ }
2771
+ const part = score.parts[options.partIndex];
2772
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2773
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2774
+ }
2775
+ if (options.startPosition >= options.endPosition) {
2776
+ return failure([operationError("INVALID_POSITION", "Start position must be less than end position", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2777
+ }
2778
+ const measure = part.measures[options.measureIndex];
2779
+ const copiedNotes = [];
2780
+ let position = 0;
2781
+ for (const entry of measure.entries) {
2782
+ if (entry.type === "note") {
2783
+ if (entry.voice === options.voice && (options.staff === void 0 || (entry.staff ?? 1) === options.staff)) {
2784
+ if (!entry.chord) {
2785
+ const noteEnd = position + entry.duration;
2786
+ if (position < options.endPosition && noteEnd > options.startPosition) {
2787
+ const clonedNote = cloneNoteWithNewId(entry);
2788
+ if (clonedNote.tie) {
2789
+ }
2790
+ copiedNotes.push({
2791
+ relativePosition: position - options.startPosition,
2792
+ note: clonedNote
2793
+ });
2794
+ }
2795
+ position += entry.duration;
2796
+ } else {
2797
+ if (copiedNotes.length > 0) {
2798
+ const lastCopied = copiedNotes[copiedNotes.length - 1];
2799
+ if (lastCopied.note.voice === entry.voice && (options.staff === void 0 || (lastCopied.note.staff ?? 1) === (entry.staff ?? 1))) {
2800
+ const clonedNote = cloneNoteWithNewId(entry);
2801
+ copiedNotes.push({
2802
+ relativePosition: lastCopied.relativePosition,
2803
+ note: clonedNote
2804
+ });
2805
+ }
2806
+ }
2807
+ }
2808
+ } else if (!entry.chord) {
2809
+ position += entry.duration;
2810
+ }
2811
+ } else if (entry.type === "backup") {
2812
+ position -= entry.duration;
2813
+ } else if (entry.type === "forward") {
2814
+ position += entry.duration;
2815
+ }
2816
+ }
2817
+ if (copiedNotes.length === 0) {
2818
+ return failure([operationError("NOTE_NOT_FOUND", "No notes found in the specified range", { partIndex: options.partIndex, measureIndex: options.measureIndex, voice: options.voice })]);
2819
+ }
2820
+ const selection = {
2821
+ source: {
2822
+ partIndex: options.partIndex,
2823
+ measureIndex: options.measureIndex,
2824
+ startPosition: options.startPosition,
2825
+ endPosition: options.endPosition,
2826
+ voice: options.voice,
2827
+ staff: options.staff
2828
+ },
2829
+ notes: copiedNotes,
2830
+ duration: options.endPosition - options.startPosition
2831
+ };
2832
+ return success(selection);
2833
+ }
2834
+ function pasteNotes(score, options) {
2835
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2836
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2837
+ }
2838
+ const part = score.parts[options.partIndex];
2839
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
2840
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2841
+ }
2842
+ if (options.position < 0) {
2843
+ return failure([operationError("INVALID_POSITION", "Position cannot be negative", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
2844
+ }
2845
+ const result = cloneScore(score);
2846
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2847
+ const context = getMeasureContext(result, options.partIndex, options.measureIndex);
2848
+ const measureDuration = context.time ? getMeasureDuration(context.divisions, context.time) : context.divisions * 4;
2849
+ const targetVoice = options.voice ?? options.selection.source.voice;
2850
+ const targetStaff = options.staff ?? options.selection.source.staff;
2851
+ const pasteEnd = options.position + options.selection.duration;
2852
+ if (pasteEnd > measureDuration) {
2853
+ return failure([operationError(
2854
+ "EXCEEDS_MEASURE",
2855
+ `Paste would exceed measure duration (ends at ${pasteEnd}, measure is ${measureDuration})`,
2856
+ { partIndex: options.partIndex, measureIndex: options.measureIndex },
2857
+ { pasteEnd, measureDuration }
2858
+ )]);
2859
+ }
2860
+ const voiceEntries = getVoiceEntries(measure, targetVoice, targetStaff);
2861
+ if (options.overwrite !== false) {
2862
+ const entriesToKeep = voiceEntries.filter((e) => {
2863
+ if (e.entry.type !== "note") return true;
2864
+ const note = e.entry;
2865
+ if (note.rest) return true;
2866
+ return e.endPosition <= options.position || e.position >= pasteEnd;
2867
+ });
2868
+ const newEntries = [];
2869
+ for (const { position, entry } of entriesToKeep) {
2870
+ if (entry.type === "note") {
2871
+ newEntries.push({ position, entry });
2872
+ }
2873
+ }
2874
+ for (const { relativePosition, note } of options.selection.notes) {
2875
+ const pastePosition = options.position + Math.max(0, relativePosition);
2876
+ const newNote = cloneNoteWithNewId(note);
2877
+ newNote.voice = targetVoice;
2878
+ if (targetStaff !== void 0) {
2879
+ newNote.staff = targetStaff;
2880
+ }
2881
+ delete newNote.tie;
2882
+ delete newNote.ties;
2883
+ if (newNote.notations) {
2884
+ newNote.notations = newNote.notations.filter((n) => n.type !== "tied");
2885
+ if (newNote.notations.length === 0) {
2886
+ delete newNote.notations;
2887
+ }
2888
+ }
2889
+ newEntries.push({ position: pastePosition, entry: newNote });
2890
+ }
2891
+ measure.entries = rebuildMeasureWithVoice(
2892
+ measure,
2893
+ targetVoice,
2894
+ newEntries,
2895
+ measureDuration,
2896
+ targetStaff
2897
+ );
2898
+ } else {
2899
+ const { hasNotes, conflictingNotes } = hasNotesInRange(voiceEntries, options.position, pasteEnd);
2900
+ if (hasNotes) {
2901
+ return failure([operationError(
2902
+ "NOTE_CONFLICT",
2903
+ `Paste range ${options.position}-${pasteEnd} conflicts with existing notes`,
2904
+ { partIndex: options.partIndex, measureIndex: options.measureIndex, voice: targetVoice },
2905
+ { conflictingPositions: conflictingNotes.map((n) => ({ start: n.position, end: n.endPosition })) }
2906
+ )]);
2907
+ }
2908
+ const existingNotes = voiceEntries.filter((e) => e.entry.type === "note").map((e) => ({ position: e.position, entry: e.entry }));
2909
+ for (const { relativePosition, note } of options.selection.notes) {
2910
+ const pastePosition = options.position + Math.max(0, relativePosition);
2911
+ const newNote = cloneNoteWithNewId(note);
2912
+ newNote.voice = targetVoice;
2913
+ if (targetStaff !== void 0) {
2914
+ newNote.staff = targetStaff;
2915
+ }
2916
+ delete newNote.tie;
2917
+ delete newNote.ties;
2918
+ if (newNote.notations) {
2919
+ newNote.notations = newNote.notations.filter((n) => n.type !== "tied");
2920
+ if (newNote.notations.length === 0) {
2921
+ delete newNote.notations;
2922
+ }
2923
+ }
2924
+ existingNotes.push({ position: pastePosition, entry: newNote });
2925
+ }
2926
+ measure.entries = rebuildMeasureWithVoice(
2927
+ measure,
2928
+ targetVoice,
2929
+ existingNotes,
2930
+ measureDuration,
2931
+ targetStaff
2932
+ );
2933
+ }
2934
+ const errors = validateMeasureLocal(measure, context, {
2935
+ checkMeasureDuration: true,
2936
+ checkPosition: true,
2937
+ checkVoiceStaff: true
2938
+ });
2939
+ const criticalErrors = errors.filter((e) => e.level === "error");
2940
+ if (criticalErrors.length > 0) {
2941
+ return failure(criticalErrors);
2942
+ }
2943
+ return success(result, errors.filter((e) => e.level !== "error"));
2944
+ }
2945
+ function cutNotes(score, options) {
2946
+ const copyResult = copyNotes(score, options);
2947
+ if (!copyResult.success) {
2948
+ return failure(copyResult.errors);
2949
+ }
2950
+ const selection = copyResult.data;
2951
+ const result = cloneScore(score);
2952
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
2953
+ const context = getMeasureContext(result, options.partIndex, options.measureIndex);
2954
+ const measureDuration = context.time ? getMeasureDuration(context.divisions, context.time) : context.divisions * 4;
2955
+ const voiceEntries = getVoiceEntries(measure, options.voice, options.staff);
2956
+ const entriesToKeep = voiceEntries.filter((e) => {
2957
+ if (e.entry.type !== "note") return true;
2958
+ const note = e.entry;
2959
+ if (note.rest) return true;
2960
+ return e.endPosition <= options.startPosition || e.position >= options.endPosition;
2961
+ });
2962
+ const newEntries = [];
2963
+ for (const { position, entry } of entriesToKeep) {
2964
+ if (entry.type === "note") {
2965
+ newEntries.push({ position, entry });
2966
+ }
2967
+ }
2968
+ measure.entries = rebuildMeasureWithVoice(
2969
+ measure,
2970
+ options.voice,
2971
+ newEntries,
2972
+ measureDuration,
2973
+ options.staff
2974
+ );
2975
+ const errors = validateMeasureLocal(measure, context, {
2976
+ checkMeasureDuration: true,
2977
+ checkPosition: true,
2978
+ checkVoiceStaff: true
2979
+ });
2980
+ const criticalErrors = errors.filter((e) => e.level === "error");
2981
+ if (criticalErrors.length > 0) {
2982
+ return failure(criticalErrors);
2983
+ }
2984
+ return success(
2985
+ { score: result, selection },
2986
+ errors.filter((e) => e.level !== "error")
2987
+ );
2988
+ }
2989
+ function copyNotesMultiMeasure(score, options) {
2990
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
2991
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
2992
+ }
2993
+ const part = score.parts[options.partIndex];
2994
+ if (options.startMeasureIndex < 0 || options.startMeasureIndex >= part.measures.length) {
2995
+ return failure([operationError("MEASURE_NOT_FOUND", `Start measure index ${options.startMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
2996
+ }
2997
+ if (options.endMeasureIndex < options.startMeasureIndex || options.endMeasureIndex >= part.measures.length) {
2998
+ return failure([operationError("MEASURE_NOT_FOUND", `End measure index ${options.endMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.endMeasureIndex })]);
2999
+ }
3000
+ const selection = {
3001
+ source: {
3002
+ partIndex: options.partIndex,
3003
+ startMeasureIndex: options.startMeasureIndex,
3004
+ endMeasureIndex: options.endMeasureIndex,
3005
+ voice: options.voice,
3006
+ staff: options.staff
3007
+ },
3008
+ measures: []
3009
+ };
3010
+ for (let measureIndex = options.startMeasureIndex; measureIndex <= options.endMeasureIndex; measureIndex++) {
3011
+ const measure = part.measures[measureIndex];
3012
+ const measureOffset = measureIndex - options.startMeasureIndex;
3013
+ const copiedNotes = [];
3014
+ let position = 0;
3015
+ for (const entry of measure.entries) {
3016
+ if (entry.type === "note") {
3017
+ if (entry.voice === options.voice && (options.staff === void 0 || (entry.staff ?? 1) === options.staff)) {
3018
+ if (!entry.chord && !entry.rest) {
3019
+ const clonedNote = cloneNoteWithNewId(entry);
3020
+ copiedNotes.push({
3021
+ relativePosition: position,
3022
+ note: clonedNote
3023
+ });
3024
+ position += entry.duration;
3025
+ } else if (entry.chord && copiedNotes.length > 0) {
3026
+ const clonedNote = cloneNoteWithNewId(entry);
3027
+ copiedNotes.push({
3028
+ relativePosition: copiedNotes[copiedNotes.length - 1].relativePosition,
3029
+ note: clonedNote
3030
+ });
3031
+ } else if (!entry.chord) {
3032
+ position += entry.duration;
3033
+ }
3034
+ } else if (!entry.chord) {
3035
+ position += entry.duration;
3036
+ }
3037
+ } else if (entry.type === "backup") {
3038
+ position -= entry.duration;
3039
+ } else if (entry.type === "forward") {
3040
+ position += entry.duration;
3041
+ }
3042
+ }
3043
+ if (copiedNotes.length > 0) {
3044
+ selection.measures.push({
3045
+ measureOffset,
3046
+ notes: copiedNotes
3047
+ });
3048
+ }
3049
+ }
3050
+ if (selection.measures.length === 0) {
3051
+ return failure([operationError("NOTE_NOT_FOUND", "No notes found in the specified range", { partIndex: options.partIndex, voice: options.voice })]);
3052
+ }
3053
+ return success(selection);
3054
+ }
3055
+ function pasteNotesMultiMeasure(score, options) {
3056
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3057
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3058
+ }
3059
+ const part = score.parts[options.partIndex];
3060
+ const measureCount = options.selection.measures.length > 0 ? options.selection.measures[options.selection.measures.length - 1].measureOffset + 1 : 0;
3061
+ if (options.startMeasureIndex + measureCount > part.measures.length) {
3062
+ return failure([operationError("MEASURE_NOT_FOUND", `Not enough measures to paste (need ${measureCount})`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
3063
+ }
3064
+ let result = cloneScore(score);
3065
+ const targetVoice = options.voice ?? options.selection.source.voice;
3066
+ const targetStaff = options.staff ?? options.selection.source.staff;
3067
+ for (const measureData of options.selection.measures) {
3068
+ const measureIndex = options.startMeasureIndex + measureData.measureOffset;
3069
+ const measure = result.parts[options.partIndex].measures[measureIndex];
3070
+ const context = getMeasureContext(result, options.partIndex, measureIndex);
3071
+ const measureDuration = context.time ? getMeasureDuration(context.divisions, context.time) : context.divisions * 4;
3072
+ const voiceEntries = getVoiceEntries(measure, targetVoice, targetStaff);
3073
+ let entriesToKeep;
3074
+ if (options.overwrite !== false) {
3075
+ entriesToKeep = voiceEntries.filter((e) => e.entry.type === "note" && e.entry.rest).map((e) => ({ position: e.position, entry: e.entry }));
3076
+ } else {
3077
+ entriesToKeep = voiceEntries.filter((e) => e.entry.type === "note").map((e) => ({ position: e.position, entry: e.entry }));
3078
+ }
3079
+ for (const { relativePosition, note } of measureData.notes) {
3080
+ const newNote = cloneNoteWithNewId(note);
3081
+ newNote.voice = targetVoice;
3082
+ if (targetStaff !== void 0) {
3083
+ newNote.staff = targetStaff;
3084
+ }
3085
+ delete newNote.tie;
3086
+ delete newNote.ties;
3087
+ if (newNote.notations) {
3088
+ newNote.notations = newNote.notations.filter((n) => n.type !== "tied");
3089
+ if (newNote.notations.length === 0) {
3090
+ delete newNote.notations;
3091
+ }
3092
+ }
3093
+ entriesToKeep.push({ position: relativePosition, entry: newNote });
3094
+ }
3095
+ measure.entries = rebuildMeasureWithVoice(
3096
+ measure,
3097
+ targetVoice,
3098
+ entriesToKeep,
3099
+ measureDuration,
3100
+ targetStaff
3101
+ );
3102
+ const errors = validateMeasureLocal(measure, context, {
3103
+ checkMeasureDuration: true,
3104
+ checkPosition: true,
3105
+ checkVoiceStaff: true
3106
+ });
3107
+ const criticalErrors = errors.filter((e) => e.level === "error");
3108
+ if (criticalErrors.length > 0) {
3109
+ return failure(criticalErrors);
3110
+ }
3111
+ }
3112
+ return success(result);
3113
+ }
3114
+ function addTempo(score, options) {
3115
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3116
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3117
+ }
3118
+ const part = score.parts[options.partIndex];
3119
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3120
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3121
+ }
3122
+ if (options.bpm <= 0) {
3123
+ return failure([operationError("INVALID_DURATION", "BPM must be positive", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3124
+ }
3125
+ const result = cloneScore(score);
3126
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3127
+ const directionTypes = [];
3128
+ directionTypes.push({
3129
+ kind: "metronome",
3130
+ beatUnit: options.beatUnit ?? "quarter",
3131
+ beatUnitDot: options.beatUnitDot,
3132
+ perMinute: options.bpm
3133
+ });
3134
+ if (options.text) {
3135
+ directionTypes.push({
3136
+ kind: "words",
3137
+ text: options.text,
3138
+ fontWeight: "bold"
3139
+ });
3140
+ }
3141
+ const direction = {
3142
+ _id: generateId(),
3143
+ type: "direction",
3144
+ directionTypes,
3145
+ placement: options.placement ?? "above",
3146
+ sound: { tempo: options.bpm }
3147
+ };
3148
+ insertDirectionAtPosition(measure, direction, options.position);
3149
+ return success(result);
3150
+ }
3151
+ function removeTempo(score, options) {
3152
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3153
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3154
+ }
3155
+ const part = score.parts[options.partIndex];
3156
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3157
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3158
+ }
3159
+ const result = cloneScore(score);
3160
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3161
+ const tempoDirectionIndices = [];
3162
+ for (let i = 0; i < measure.entries.length; i++) {
3163
+ const entry = measure.entries[i];
3164
+ if (entry.type === "direction" && entry.directionTypes.some((dt) => dt.kind === "metronome")) {
3165
+ tempoDirectionIndices.push(i);
3166
+ }
3167
+ }
3168
+ if (tempoDirectionIndices.length === 0) {
3169
+ return failure([operationError("TEMPO_NOT_FOUND", "No tempo marking found in measure", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3170
+ }
3171
+ const targetIndex = options.directionIndex ?? 0;
3172
+ if (targetIndex < 0 || targetIndex >= tempoDirectionIndices.length) {
3173
+ return failure([operationError("TEMPO_NOT_FOUND", `Tempo direction index ${targetIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3174
+ }
3175
+ measure.entries.splice(tempoDirectionIndices[targetIndex], 1);
3176
+ return success(result);
3177
+ }
3178
+ function addWedge(score, options) {
3179
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3180
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3181
+ }
3182
+ const part = score.parts[options.partIndex];
3183
+ if (options.startMeasureIndex < 0 || options.startMeasureIndex >= part.measures.length) {
3184
+ return failure([operationError("MEASURE_NOT_FOUND", `Start measure index ${options.startMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.startMeasureIndex })]);
3185
+ }
3186
+ if (options.endMeasureIndex < 0 || options.endMeasureIndex >= part.measures.length) {
3187
+ return failure([operationError("MEASURE_NOT_FOUND", `End measure index ${options.endMeasureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.endMeasureIndex })]);
3188
+ }
3189
+ if (options.endMeasureIndex < options.startMeasureIndex || options.endMeasureIndex === options.startMeasureIndex && options.endPosition <= options.startPosition) {
3190
+ return failure([operationError("INVALID_RANGE", "End position must be after start position", { partIndex: options.partIndex })]);
3191
+ }
3192
+ const result = cloneScore(score);
3193
+ const startMeasure = result.parts[options.partIndex].measures[options.startMeasureIndex];
3194
+ const startDirection = {
3195
+ _id: generateId(),
3196
+ type: "direction",
3197
+ directionTypes: [{
3198
+ kind: "wedge",
3199
+ type: options.type
3200
+ }],
3201
+ placement: options.placement ?? "below",
3202
+ staff: options.staff
3203
+ };
3204
+ insertDirectionAtPosition(startMeasure, startDirection, options.startPosition);
3205
+ const endMeasure = result.parts[options.partIndex].measures[options.endMeasureIndex];
3206
+ const endDirection = {
3207
+ _id: generateId(),
3208
+ type: "direction",
3209
+ directionTypes: [{
3210
+ kind: "wedge",
3211
+ type: "stop"
3212
+ }],
3213
+ placement: options.placement ?? "below",
3214
+ staff: options.staff
3215
+ };
3216
+ insertDirectionAtPosition(endMeasure, endDirection, options.endPosition);
3217
+ return success(result);
3218
+ }
3219
+ function removeWedge(score, options) {
3220
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3221
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3222
+ }
3223
+ const part = score.parts[options.partIndex];
3224
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3225
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3226
+ }
3227
+ const result = cloneScore(score);
3228
+ const wedgeStarts = [];
3229
+ for (let mi = options.measureIndex; mi < result.parts[options.partIndex].measures.length; mi++) {
3230
+ const measure = result.parts[options.partIndex].measures[mi];
3231
+ for (let ei = 0; ei < measure.entries.length; ei++) {
3232
+ const entry = measure.entries[ei];
3233
+ if (entry.type === "direction") {
3234
+ const wedgeType = entry.directionTypes.find((dt) => dt.kind === "wedge");
3235
+ if (wedgeType && wedgeType.kind === "wedge" && (wedgeType.type === "crescendo" || wedgeType.type === "diminuendo")) {
3236
+ wedgeStarts.push({ measureIndex: mi, entryIndex: ei });
3237
+ }
3238
+ }
3239
+ }
3240
+ if (mi === options.measureIndex && wedgeStarts.length > 0) break;
3241
+ }
3242
+ if (wedgeStarts.length === 0) {
3243
+ return failure([operationError("WEDGE_NOT_FOUND", "No wedge found starting in measure", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3244
+ }
3245
+ const targetIndex = options.directionIndex ?? 0;
3246
+ if (targetIndex >= wedgeStarts.length) {
3247
+ return failure([operationError("WEDGE_NOT_FOUND", `Wedge direction index ${targetIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3248
+ }
3249
+ const startInfo = wedgeStarts[targetIndex];
3250
+ const startMeasure = result.parts[options.partIndex].measures[startInfo.measureIndex];
3251
+ startMeasure.entries.splice(startInfo.entryIndex, 1);
3252
+ for (let mi = startInfo.measureIndex; mi < result.parts[options.partIndex].measures.length; mi++) {
3253
+ const measure = result.parts[options.partIndex].measures[mi];
3254
+ for (let ei = 0; ei < measure.entries.length; ei++) {
3255
+ const entry = measure.entries[ei];
3256
+ if (entry.type === "direction") {
3257
+ const wedgeType = entry.directionTypes.find((dt) => dt.kind === "wedge" && dt.type === "stop");
3258
+ if (wedgeType) {
3259
+ measure.entries.splice(ei, 1);
3260
+ return success(result);
3261
+ }
3262
+ }
3263
+ }
3264
+ }
3265
+ return success(result);
3266
+ }
3267
+ function addFermata(score, options) {
3268
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3269
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3270
+ }
3271
+ const part = score.parts[options.partIndex];
3272
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3273
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3274
+ }
3275
+ const result = cloneScore(score);
3276
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3277
+ const notes = measure.entries.filter((e) => e.type === "note" && !e.rest);
3278
+ if (options.noteIndex < 0 || options.noteIndex >= notes.length) {
3279
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3280
+ }
3281
+ const note = notes[options.noteIndex];
3282
+ if (note.notations?.some((n) => n.type === "fermata")) {
3283
+ return failure([operationError("FERMATA_ALREADY_EXISTS", "Note already has a fermata", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3284
+ }
3285
+ if (!note.notations) {
3286
+ note.notations = [];
3287
+ }
3288
+ const fermataNotation = {
3289
+ type: "fermata",
3290
+ shape: options.shape ?? "normal",
3291
+ fermataType: options.fermataType ?? "upright",
3292
+ placement: options.placement ?? "above"
3293
+ };
3294
+ note.notations.push(fermataNotation);
3295
+ return success(result);
3296
+ }
3297
+ function removeFermata(score, options) {
3298
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3299
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3300
+ }
3301
+ const part = score.parts[options.partIndex];
3302
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3303
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3304
+ }
3305
+ const result = cloneScore(score);
3306
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3307
+ const notes = measure.entries.filter((e) => e.type === "note" && !e.rest);
3308
+ if (options.noteIndex < 0 || options.noteIndex >= notes.length) {
3309
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3310
+ }
3311
+ const note = notes[options.noteIndex];
3312
+ const fermataIndex = note.notations?.findIndex((n) => n.type === "fermata");
3313
+ if (fermataIndex === void 0 || fermataIndex === -1) {
3314
+ return failure([operationError("FERMATA_NOT_FOUND", "Note does not have a fermata", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3315
+ }
3316
+ note.notations.splice(fermataIndex, 1);
3317
+ if (note.notations.length === 0) {
3318
+ delete note.notations;
3319
+ }
3320
+ return success(result);
3321
+ }
3322
+ function addOrnament(score, options) {
3323
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3324
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3325
+ }
3326
+ const part = score.parts[options.partIndex];
3327
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3328
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3329
+ }
3330
+ const result = cloneScore(score);
3331
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3332
+ const notes = measure.entries.filter((e) => e.type === "note" && !e.rest);
3333
+ if (options.noteIndex < 0 || options.noteIndex >= notes.length) {
3334
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3335
+ }
3336
+ const note = notes[options.noteIndex];
3337
+ if (note.notations?.some((n) => n.type === "ornament" && n.ornament === options.ornament)) {
3338
+ return failure([operationError("ORNAMENT_ALREADY_EXISTS", `Note already has ornament: ${options.ornament}`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3339
+ }
3340
+ if (!note.notations) {
3341
+ note.notations = [];
3342
+ }
3343
+ const ornamentNotation = {
3344
+ type: "ornament",
3345
+ ornament: options.ornament,
3346
+ placement: options.placement,
3347
+ accidentalMark: options.accidentalMark
3348
+ };
3349
+ note.notations.push(ornamentNotation);
3350
+ return success(result);
3351
+ }
3352
+ function removeOrnament(score, options) {
3353
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3354
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3355
+ }
3356
+ const part = score.parts[options.partIndex];
3357
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3358
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3359
+ }
3360
+ const result = cloneScore(score);
3361
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3362
+ const notes = measure.entries.filter((e) => e.type === "note" && !e.rest);
3363
+ if (options.noteIndex < 0 || options.noteIndex >= notes.length) {
3364
+ return failure([operationError("NOTE_NOT_FOUND", `Note index ${options.noteIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3365
+ }
3366
+ const note = notes[options.noteIndex];
3367
+ const ornamentIndex = options.ornament ? note.notations?.findIndex((n) => n.type === "ornament" && n.ornament === options.ornament) : note.notations?.findIndex((n) => n.type === "ornament");
3368
+ if (ornamentIndex === void 0 || ornamentIndex === -1) {
3369
+ return failure([operationError("ORNAMENT_NOT_FOUND", "Note does not have the specified ornament", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3370
+ }
3371
+ note.notations.splice(ornamentIndex, 1);
3372
+ if (note.notations.length === 0) {
3373
+ delete note.notations;
3374
+ }
3375
+ return success(result);
3376
+ }
3377
+ function addPedal(score, options) {
3378
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3379
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3380
+ }
3381
+ const part = score.parts[options.partIndex];
3382
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3383
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3384
+ }
3385
+ const result = cloneScore(score);
3386
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3387
+ const direction = {
3388
+ _id: generateId(),
3389
+ type: "direction",
3390
+ directionTypes: [{
3391
+ kind: "pedal",
3392
+ type: options.pedalType,
3393
+ line: options.line
3394
+ }],
3395
+ placement: options.placement ?? "below"
3396
+ };
3397
+ insertDirectionAtPosition(measure, direction, options.position);
3398
+ return success(result);
3399
+ }
3400
+ function removePedal(score, options) {
3401
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3402
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3403
+ }
3404
+ const part = score.parts[options.partIndex];
3405
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3406
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3407
+ }
3408
+ const result = cloneScore(score);
3409
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3410
+ const pedalIndices = [];
3411
+ for (let i = 0; i < measure.entries.length; i++) {
3412
+ const entry = measure.entries[i];
3413
+ if (entry.type === "direction" && entry.directionTypes.some((dt) => dt.kind === "pedal")) {
3414
+ pedalIndices.push(i);
3415
+ }
3416
+ }
3417
+ if (pedalIndices.length === 0) {
3418
+ return failure([operationError("PEDAL_NOT_FOUND", "No pedal marking found in measure", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3419
+ }
3420
+ const targetIndex = options.directionIndex ?? 0;
3421
+ if (targetIndex < 0 || targetIndex >= pedalIndices.length) {
3422
+ return failure([operationError("PEDAL_NOT_FOUND", `Pedal direction index ${targetIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3423
+ }
3424
+ measure.entries.splice(pedalIndices[targetIndex], 1);
3425
+ return success(result);
3426
+ }
3427
+ function addTextDirection(score, options) {
3428
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3429
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3430
+ }
3431
+ const part = score.parts[options.partIndex];
3432
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3433
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3434
+ }
3435
+ if (!options.text.trim()) {
3436
+ return failure([operationError("INVALID_TEXT", "Text cannot be empty", { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3437
+ }
3438
+ const result = cloneScore(score);
3439
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3440
+ const direction = {
3441
+ _id: generateId(),
3442
+ type: "direction",
3443
+ directionTypes: [{
3444
+ kind: "words",
3445
+ text: options.text,
3446
+ fontStyle: options.fontStyle,
3447
+ fontWeight: options.fontWeight
3448
+ }],
3449
+ placement: options.placement ?? "above"
3450
+ };
3451
+ insertDirectionAtPosition(measure, direction, options.position);
3452
+ return success(result);
3453
+ }
3454
+ function addRehearsalMark(score, options) {
3455
+ if (options.partIndex < 0 || options.partIndex >= score.parts.length) {
3456
+ return failure([operationError("PART_NOT_FOUND", `Part index ${options.partIndex} out of bounds`, { partIndex: options.partIndex })]);
3457
+ }
3458
+ const part = score.parts[options.partIndex];
3459
+ if (options.measureIndex < 0 || options.measureIndex >= part.measures.length) {
3460
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${options.measureIndex} out of bounds`, { partIndex: options.partIndex, measureIndex: options.measureIndex })]);
3461
+ }
3462
+ const result = cloneScore(score);
3463
+ const measure = result.parts[options.partIndex].measures[options.measureIndex];
3464
+ const direction = {
3465
+ _id: generateId(),
3466
+ type: "direction",
3467
+ directionTypes: [{
3468
+ kind: "rehearsal",
3469
+ text: options.text,
3470
+ enclosure: options.enclosure ?? "square"
3471
+ }],
3472
+ placement: "above"
3473
+ };
3474
+ insertDirectionAtPosition(measure, direction, 0);
3475
+ return success(result);
3476
+ }
3477
+ function insertDirectionAtPosition(measure, direction, position) {
3478
+ let currentPosition = 0;
3479
+ let insertIndex = 0;
3480
+ for (let i = 0; i < measure.entries.length; i++) {
3481
+ const entry = measure.entries[i];
3482
+ if (currentPosition >= position) {
3483
+ insertIndex = i;
3484
+ break;
3485
+ }
3486
+ if (entry.type === "note" && !entry.chord) {
3487
+ currentPosition += entry.duration;
3488
+ } else if (entry.type === "forward") {
3489
+ currentPosition += entry.duration;
3490
+ } else if (entry.type === "backup") {
3491
+ currentPosition -= entry.duration;
3492
+ }
3493
+ insertIndex = i + 1;
3494
+ }
3495
+ measure.entries.splice(insertIndex, 0, direction);
3496
+ }
3497
+ function addRepeatBarline(score, options) {
3498
+ const { partIndex, measureIndex, direction, times } = options;
3499
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3500
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3501
+ }
3502
+ const part = score.parts[partIndex];
3503
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3504
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3505
+ }
3506
+ const result = cloneScore(score);
3507
+ const location = direction === "forward" ? "left" : "right";
3508
+ const barStyle = direction === "forward" ? "heavy-light" : "light-heavy";
3509
+ for (const p of result.parts) {
3510
+ if (measureIndex >= p.measures.length) continue;
3511
+ const measure = p.measures[measureIndex];
3512
+ if (!measure.barlines) {
3513
+ measure.barlines = [];
3514
+ }
3515
+ const existingIndex = measure.barlines.findIndex((b) => b.location === location && b.repeat);
3516
+ if (existingIndex >= 0) {
3517
+ return failure([operationError("REPEAT_ALREADY_EXISTS", `Repeat barline already exists at ${location} of measure ${measureIndex}`, { partIndex, measureIndex })]);
3518
+ }
3519
+ const nonRepeatIndex = measure.barlines.findIndex((b) => b.location === location && !b.repeat);
3520
+ if (nonRepeatIndex >= 0) {
3521
+ measure.barlines.splice(nonRepeatIndex, 1);
3522
+ }
3523
+ measure.barlines.push({
3524
+ _id: generateId(),
3525
+ location,
3526
+ barStyle,
3527
+ repeat: {
3528
+ direction,
3529
+ times
3530
+ }
3531
+ });
3532
+ }
3533
+ return success(result);
3534
+ }
3535
+ function removeRepeatBarline(score, options) {
3536
+ const { partIndex, measureIndex, location } = options;
3537
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3538
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3539
+ }
3540
+ const part = score.parts[partIndex];
3541
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3542
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3543
+ }
3544
+ const measure = part.measures[measureIndex];
3545
+ if (!measure.barlines) {
3546
+ return failure([operationError("REPEAT_NOT_FOUND", `No repeat barline found at ${location} of measure ${measureIndex}`, { partIndex, measureIndex })]);
3547
+ }
3548
+ const existingIndex = measure.barlines.findIndex((b) => b.location === location && b.repeat);
3549
+ if (existingIndex < 0) {
3550
+ return failure([operationError("REPEAT_NOT_FOUND", `No repeat barline found at ${location} of measure ${measureIndex}`, { partIndex, measureIndex })]);
3551
+ }
3552
+ const result = cloneScore(score);
3553
+ for (const p of result.parts) {
3554
+ if (measureIndex >= p.measures.length) continue;
3555
+ const m = p.measures[measureIndex];
3556
+ if (m.barlines) {
3557
+ const idx = m.barlines.findIndex((b) => b.location === location && b.repeat);
3558
+ if (idx >= 0) {
3559
+ m.barlines.splice(idx, 1);
3560
+ }
3561
+ if (m.barlines.length === 0) {
3562
+ delete m.barlines;
3563
+ }
3564
+ }
3565
+ }
3566
+ return success(result);
3567
+ }
3568
+ function addEnding(score, options) {
3569
+ const { partIndex, measureIndex, number, type } = options;
3570
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3571
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3572
+ }
3573
+ const part = score.parts[partIndex];
3574
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3575
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3576
+ }
3577
+ const result = cloneScore(score);
3578
+ const location = type === "start" ? "left" : "right";
3579
+ for (const p of result.parts) {
3580
+ if (measureIndex >= p.measures.length) continue;
3581
+ const measure = p.measures[measureIndex];
3582
+ if (!measure.barlines) {
3583
+ measure.barlines = [];
3584
+ }
3585
+ let barline = measure.barlines.find((b) => b.location === location);
3586
+ if (!barline) {
3587
+ barline = { _id: generateId(), location };
3588
+ measure.barlines.push(barline);
3589
+ }
3590
+ if (barline.ending) {
3591
+ return failure([operationError("ENDING_ALREADY_EXISTS", `Ending already exists at ${location} of measure ${measureIndex}`, { partIndex, measureIndex })]);
3592
+ }
3593
+ barline.ending = { number, type };
3594
+ }
3595
+ return success(result);
3596
+ }
3597
+ function removeEnding(score, options) {
3598
+ const { partIndex, measureIndex, location } = options;
3599
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3600
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3601
+ }
3602
+ const part = score.parts[partIndex];
3603
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3604
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3605
+ }
3606
+ const measure = part.measures[measureIndex];
3607
+ const barline = measure.barlines?.find((b) => b.location === location && b.ending);
3608
+ if (!barline) {
3609
+ return failure([operationError("ENDING_NOT_FOUND", `No ending found at ${location} of measure ${measureIndex}`, { partIndex, measureIndex })]);
3610
+ }
3611
+ const result = cloneScore(score);
3612
+ for (const p of result.parts) {
3613
+ if (measureIndex >= p.measures.length) continue;
3614
+ const m = p.measures[measureIndex];
3615
+ if (m.barlines) {
3616
+ const bl = m.barlines.find((b) => b.location === location);
3617
+ if (bl) {
3618
+ delete bl.ending;
3619
+ if (!bl.barStyle && !bl.repeat && !bl.ending) {
3620
+ const idx = m.barlines.indexOf(bl);
3621
+ m.barlines.splice(idx, 1);
3622
+ }
3623
+ }
3624
+ if (m.barlines.length === 0) {
3625
+ delete m.barlines;
3626
+ }
3627
+ }
3628
+ }
3629
+ return success(result);
3630
+ }
3631
+ function changeBarline(score, options) {
3632
+ const { partIndex, measureIndex, location, barStyle } = options;
3633
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3634
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3635
+ }
3636
+ const part = score.parts[partIndex];
3637
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3638
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3639
+ }
3640
+ const result = cloneScore(score);
3641
+ for (const p of result.parts) {
3642
+ if (measureIndex >= p.measures.length) continue;
3643
+ const measure = p.measures[measureIndex];
3644
+ if (!measure.barlines) {
3645
+ measure.barlines = [];
3646
+ }
3647
+ let barline = measure.barlines.find((b) => b.location === location);
3648
+ if (!barline) {
3649
+ barline = { _id: generateId(), location };
3650
+ measure.barlines.push(barline);
3651
+ }
3652
+ barline.barStyle = barStyle;
3653
+ }
3654
+ return success(result);
3655
+ }
3656
+ function addSegno(score, options) {
3657
+ const { partIndex, measureIndex, position = 0 } = options;
3658
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3659
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3660
+ }
3661
+ const part = score.parts[partIndex];
3662
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3663
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3664
+ }
3665
+ const result = cloneScore(score);
3666
+ const measure = result.parts[partIndex].measures[measureIndex];
3667
+ const direction = {
3668
+ _id: generateId(),
3669
+ type: "direction",
3670
+ directionTypes: [{ kind: "segno" }],
3671
+ placement: "above"
3672
+ };
3673
+ insertDirectionAtPosition(measure, direction, position);
3674
+ return success(result);
3675
+ }
3676
+ function addCoda(score, options) {
3677
+ const { partIndex, measureIndex, position = 0 } = options;
3678
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3679
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3680
+ }
3681
+ const part = score.parts[partIndex];
3682
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3683
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3684
+ }
3685
+ const result = cloneScore(score);
3686
+ const measure = result.parts[partIndex].measures[measureIndex];
3687
+ const direction = {
3688
+ _id: generateId(),
3689
+ type: "direction",
3690
+ directionTypes: [{ kind: "coda" }],
3691
+ placement: "above"
3692
+ };
3693
+ insertDirectionAtPosition(measure, direction, position);
3694
+ return success(result);
3695
+ }
3696
+ function addDaCapo(score, options) {
3697
+ const { partIndex, measureIndex, position } = options;
3698
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3699
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3700
+ }
3701
+ const part = score.parts[partIndex];
3702
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3703
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3704
+ }
3705
+ const result = cloneScore(score);
3706
+ const measure = result.parts[partIndex].measures[measureIndex];
3707
+ const attrs = getAttributesAtMeasure(result, { part: partIndex, measure: measureIndex });
3708
+ const measureDuration = getMeasureDuration(attrs.divisions ?? 1, attrs.time ?? { beats: "4", beatType: 4 });
3709
+ const insertPos = position ?? measureDuration;
3710
+ const direction = {
3711
+ _id: generateId(),
3712
+ type: "direction",
3713
+ directionTypes: [{ kind: "words", text: "D.C." }],
3714
+ placement: "above"
3715
+ };
3716
+ insertDirectionAtPosition(measure, direction, insertPos);
3717
+ const sound = {
3718
+ _id: generateId(),
3719
+ type: "sound",
3720
+ dacapo: true
3721
+ };
3722
+ measure.entries.push(sound);
3723
+ return success(result);
3724
+ }
3725
+ function addDalSegno(score, options) {
3726
+ const { partIndex, measureIndex, position } = options;
3727
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3728
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3729
+ }
3730
+ const part = score.parts[partIndex];
3731
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3732
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3733
+ }
3734
+ const result = cloneScore(score);
3735
+ const measure = result.parts[partIndex].measures[measureIndex];
3736
+ const attrs = getAttributesAtMeasure(result, { part: partIndex, measure: measureIndex });
3737
+ const measureDuration = getMeasureDuration(attrs.divisions ?? 1, attrs.time ?? { beats: "4", beatType: 4 });
3738
+ const insertPos = position ?? measureDuration;
3739
+ const direction = {
3740
+ _id: generateId(),
3741
+ type: "direction",
3742
+ directionTypes: [{ kind: "words", text: "D.S." }],
3743
+ placement: "above"
3744
+ };
3745
+ insertDirectionAtPosition(measure, direction, insertPos);
3746
+ const sound = {
3747
+ _id: generateId(),
3748
+ type: "sound",
3749
+ dalsegno: "segno"
3750
+ };
3751
+ measure.entries.push(sound);
3752
+ return success(result);
3753
+ }
3754
+ function addFine(score, options) {
3755
+ const { partIndex, measureIndex, position } = options;
3756
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3757
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3758
+ }
3759
+ const part = score.parts[partIndex];
3760
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3761
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3762
+ }
3763
+ const result = cloneScore(score);
3764
+ const measure = result.parts[partIndex].measures[measureIndex];
3765
+ const attrs = getAttributesAtMeasure(result, { part: partIndex, measure: measureIndex });
3766
+ const measureDuration = getMeasureDuration(attrs.divisions ?? 1, attrs.time ?? { beats: "4", beatType: 4 });
3767
+ const insertPos = position ?? measureDuration;
3768
+ const direction = {
3769
+ _id: generateId(),
3770
+ type: "direction",
3771
+ directionTypes: [{ kind: "words", text: "Fine" }],
3772
+ placement: "above"
3773
+ };
3774
+ insertDirectionAtPosition(measure, direction, insertPos);
3775
+ const sound = {
3776
+ _id: generateId(),
3777
+ type: "sound",
3778
+ fine: true
3779
+ };
3780
+ measure.entries.push(sound);
3781
+ return success(result);
3782
+ }
3783
+ function addToCoda(score, options) {
3784
+ const { partIndex, measureIndex, position } = options;
3785
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3786
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3787
+ }
3788
+ const part = score.parts[partIndex];
3789
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3790
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3791
+ }
3792
+ const result = cloneScore(score);
3793
+ const measure = result.parts[partIndex].measures[measureIndex];
3794
+ const attrs = getAttributesAtMeasure(result, { part: partIndex, measure: measureIndex });
3795
+ const measureDuration = getMeasureDuration(attrs.divisions ?? 1, attrs.time ?? { beats: "4", beatType: 4 });
3796
+ const insertPos = position ?? measureDuration;
3797
+ const direction = {
3798
+ _id: generateId(),
3799
+ type: "direction",
3800
+ directionTypes: [{ kind: "words", text: "To Coda" }],
3801
+ placement: "above"
3802
+ };
3803
+ insertDirectionAtPosition(measure, direction, insertPos);
3804
+ const sound = {
3805
+ _id: generateId(),
3806
+ type: "sound",
3807
+ tocoda: "coda"
3808
+ };
3809
+ measure.entries.push(sound);
3810
+ return success(result);
3811
+ }
3812
+ function addGraceNote(score, options) {
3813
+ const { partIndex, measureIndex, targetNoteIndex, pitch, noteType = "eighth", slash = true, voice, staff } = options;
3814
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3815
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3816
+ }
3817
+ const part = score.parts[partIndex];
3818
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3819
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3820
+ }
3821
+ const measure = part.measures[measureIndex];
3822
+ let noteCount = 0;
3823
+ let targetEntryIndex = -1;
3824
+ let targetNote = null;
3825
+ for (let i = 0; i < measure.entries.length; i++) {
3826
+ const entry = measure.entries[i];
3827
+ if (entry.type === "note" && !entry.chord) {
3828
+ if (noteCount === targetNoteIndex) {
3829
+ targetEntryIndex = i;
3830
+ targetNote = entry;
3831
+ break;
3832
+ }
3833
+ noteCount++;
3834
+ }
3835
+ }
3836
+ if (targetEntryIndex < 0 || !targetNote) {
3837
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${targetNoteIndex} not found`, { partIndex, measureIndex })]);
3838
+ }
3839
+ const result = cloneScore(score);
3840
+ const resultMeasure = result.parts[partIndex].measures[measureIndex];
3841
+ const graceNote = {
3842
+ _id: generateId(),
3843
+ type: "note",
3844
+ pitch,
3845
+ duration: 0,
3846
+ // Grace notes have no duration
3847
+ voice: voice ?? targetNote.voice,
3848
+ staff: staff ?? targetNote.staff,
3849
+ noteType,
3850
+ grace: {
3851
+ slash
3852
+ }
3853
+ };
3854
+ resultMeasure.entries.splice(targetEntryIndex, 0, graceNote);
3855
+ return success(result);
3856
+ }
3857
+ function removeGraceNote(score, options) {
3858
+ const { partIndex, measureIndex, graceNoteIndex } = options;
3859
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3860
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3861
+ }
3862
+ const part = score.parts[partIndex];
3863
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3864
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3865
+ }
3866
+ const measure = part.measures[measureIndex];
3867
+ let graceCount = 0;
3868
+ let targetEntryIndex = -1;
3869
+ for (let i = 0; i < measure.entries.length; i++) {
3870
+ const entry = measure.entries[i];
3871
+ if (entry.type === "note" && entry.grace) {
3872
+ if (graceCount === graceNoteIndex) {
3873
+ targetEntryIndex = i;
3874
+ break;
3875
+ }
3876
+ graceCount++;
3877
+ }
3878
+ }
3879
+ if (targetEntryIndex < 0) {
3880
+ return failure([operationError("GRACE_NOTE_NOT_FOUND", `Grace note at index ${graceNoteIndex} not found`, { partIndex, measureIndex })]);
3881
+ }
3882
+ const result = cloneScore(score);
3883
+ result.parts[partIndex].measures[measureIndex].entries.splice(targetEntryIndex, 1);
3884
+ return success(result);
3885
+ }
3886
+ function convertToGrace(score, options) {
3887
+ const { partIndex, measureIndex, noteIndex, slash = true } = options;
3888
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3889
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3890
+ }
3891
+ const part = score.parts[partIndex];
3892
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3893
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3894
+ }
3895
+ const measure = part.measures[measureIndex];
3896
+ let noteCount = 0;
3897
+ let targetEntryIndex = -1;
3898
+ for (let i = 0; i < measure.entries.length; i++) {
3899
+ const entry = measure.entries[i];
3900
+ if (entry.type === "note" && !entry.chord) {
3901
+ if (noteCount === noteIndex) {
3902
+ targetEntryIndex = i;
3903
+ break;
3904
+ }
3905
+ noteCount++;
3906
+ }
3907
+ }
3908
+ if (targetEntryIndex < 0) {
3909
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
3910
+ }
3911
+ const targetEntry = measure.entries[targetEntryIndex];
3912
+ if (targetEntry.type !== "note") {
3913
+ return failure([operationError("NOTE_NOT_FOUND", `Entry at index is not a note`, { partIndex, measureIndex })]);
3914
+ }
3915
+ if (targetEntry.grace) {
3916
+ return failure([operationError("INVALID_GRACE_NOTE", `Note is already a grace note`, { partIndex, measureIndex })]);
3917
+ }
3918
+ const result = cloneScore(score);
3919
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
3920
+ resultNote.grace = { slash };
3921
+ resultNote.duration = 0;
3922
+ return success(result);
3923
+ }
3924
+ function addLyric(score, options) {
3925
+ const { partIndex, measureIndex, noteIndex, text, syllabic = "single", verse = 1, extend = false } = options;
3926
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3927
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3928
+ }
3929
+ const part = score.parts[partIndex];
3930
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3931
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3932
+ }
3933
+ const measure = part.measures[measureIndex];
3934
+ let noteCount = 0;
3935
+ let targetEntryIndex = -1;
3936
+ for (let i = 0; i < measure.entries.length; i++) {
3937
+ const entry = measure.entries[i];
3938
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
3939
+ if (noteCount === noteIndex) {
3940
+ targetEntryIndex = i;
3941
+ break;
3942
+ }
3943
+ noteCount++;
3944
+ }
3945
+ }
3946
+ if (targetEntryIndex < 0) {
3947
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
3948
+ }
3949
+ const targetEntry = measure.entries[targetEntryIndex];
3950
+ if (targetEntry.type !== "note") {
3951
+ return failure([operationError("NOTE_NOT_FOUND", `Entry is not a note`, { partIndex, measureIndex })]);
3952
+ }
3953
+ if (targetEntry.lyrics?.some((l) => l.number === verse)) {
3954
+ return failure([operationError("LYRIC_ALREADY_EXISTS", `Lyric for verse ${verse} already exists on this note`, { partIndex, measureIndex })]);
3955
+ }
3956
+ const result = cloneScore(score);
3957
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
3958
+ if (!resultNote.lyrics) {
3959
+ resultNote.lyrics = [];
3960
+ }
3961
+ const lyric = {
3962
+ number: verse,
3963
+ syllabic,
3964
+ text,
3965
+ extend
3966
+ };
3967
+ resultNote.lyrics.push(lyric);
3968
+ return success(result);
3969
+ }
3970
+ function removeLyric(score, options) {
3971
+ const { partIndex, measureIndex, noteIndex, verse } = options;
3972
+ if (partIndex < 0 || partIndex >= score.parts.length) {
3973
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
3974
+ }
3975
+ const part = score.parts[partIndex];
3976
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
3977
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
3978
+ }
3979
+ const measure = part.measures[measureIndex];
3980
+ let noteCount = 0;
3981
+ let targetEntryIndex = -1;
3982
+ for (let i = 0; i < measure.entries.length; i++) {
3983
+ const entry = measure.entries[i];
3984
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
3985
+ if (noteCount === noteIndex) {
3986
+ targetEntryIndex = i;
3987
+ break;
3988
+ }
3989
+ noteCount++;
3990
+ }
3991
+ }
3992
+ if (targetEntryIndex < 0) {
3993
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
3994
+ }
3995
+ const targetEntry = measure.entries[targetEntryIndex];
3996
+ if (targetEntry.type !== "note" || !targetEntry.lyrics || targetEntry.lyrics.length === 0) {
3997
+ return failure([operationError("LYRIC_NOT_FOUND", `No lyrics found on note`, { partIndex, measureIndex })]);
3998
+ }
3999
+ if (verse !== void 0) {
4000
+ const lyricIndex = targetEntry.lyrics.findIndex((l) => l.number === verse);
4001
+ if (lyricIndex < 0) {
4002
+ return failure([operationError("LYRIC_NOT_FOUND", `Lyric for verse ${verse} not found on note`, { partIndex, measureIndex })]);
4003
+ }
4004
+ }
4005
+ const result = cloneScore(score);
4006
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4007
+ if (verse !== void 0) {
4008
+ resultNote.lyrics = resultNote.lyrics.filter((l) => l.number !== verse);
4009
+ } else {
4010
+ delete resultNote.lyrics;
4011
+ }
4012
+ if (resultNote.lyrics && resultNote.lyrics.length === 0) {
4013
+ delete resultNote.lyrics;
4014
+ }
4015
+ return success(result);
4016
+ }
4017
+ function updateLyric(score, options) {
4018
+ const { partIndex, measureIndex, noteIndex, verse = 1, text, syllabic, extend } = options;
4019
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4020
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4021
+ }
4022
+ const part = score.parts[partIndex];
4023
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4024
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4025
+ }
4026
+ const measure = part.measures[measureIndex];
4027
+ let noteCount = 0;
4028
+ let targetEntryIndex = -1;
4029
+ for (let i = 0; i < measure.entries.length; i++) {
4030
+ const entry = measure.entries[i];
4031
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4032
+ if (noteCount === noteIndex) {
4033
+ targetEntryIndex = i;
4034
+ break;
4035
+ }
4036
+ noteCount++;
4037
+ }
4038
+ }
4039
+ if (targetEntryIndex < 0) {
4040
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4041
+ }
4042
+ const targetEntry = measure.entries[targetEntryIndex];
4043
+ if (targetEntry.type !== "note" || !targetEntry.lyrics) {
4044
+ return failure([operationError("LYRIC_NOT_FOUND", `No lyrics found on note`, { partIndex, measureIndex })]);
4045
+ }
4046
+ const lyricIndex = targetEntry.lyrics.findIndex((l) => l.number === verse);
4047
+ if (lyricIndex < 0) {
4048
+ return failure([operationError("LYRIC_NOT_FOUND", `Lyric for verse ${verse} not found on note`, { partIndex, measureIndex })]);
4049
+ }
4050
+ const result = cloneScore(score);
4051
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4052
+ const lyric = resultNote.lyrics[lyricIndex];
4053
+ if (text !== void 0) {
4054
+ lyric.text = text;
4055
+ }
4056
+ if (syllabic !== void 0) {
4057
+ lyric.syllabic = syllabic;
4058
+ }
4059
+ if (extend !== void 0) {
4060
+ lyric.extend = extend;
4061
+ }
4062
+ return success(result);
4063
+ }
4064
+ function addHarmony(score, options) {
4065
+ const { partIndex, measureIndex, position, root, kind, kindText, bass, degrees, staff, placement = "above" } = options;
4066
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4067
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4068
+ }
4069
+ const part = score.parts[partIndex];
4070
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4071
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4072
+ }
4073
+ const validSteps = ["A", "B", "C", "D", "E", "F", "G"];
4074
+ if (!validSteps.includes(root.step.toUpperCase())) {
4075
+ return failure([operationError("INVALID_HARMONY", `Invalid root step: ${root.step}`, { partIndex, measureIndex })]);
4076
+ }
4077
+ if (bass && !validSteps.includes(bass.step.toUpperCase())) {
4078
+ return failure([operationError("INVALID_HARMONY", `Invalid bass step: ${bass.step}`, { partIndex, measureIndex })]);
4079
+ }
4080
+ const result = cloneScore(score);
4081
+ const measure = result.parts[partIndex].measures[measureIndex];
4082
+ const harmony = {
4083
+ _id: generateId(),
4084
+ type: "harmony",
4085
+ root: {
4086
+ rootStep: root.step.toUpperCase(),
4087
+ rootAlter: root.alter
4088
+ },
4089
+ kind,
4090
+ kindText,
4091
+ bass: bass ? {
4092
+ bassStep: bass.step.toUpperCase(),
4093
+ bassAlter: bass.alter
4094
+ } : void 0,
4095
+ degrees: degrees?.map((d) => ({
4096
+ degreeValue: d.value,
4097
+ degreeAlter: d.alter,
4098
+ degreeType: d.type
4099
+ })),
4100
+ staff,
4101
+ placement
4102
+ };
4103
+ let currentPosition = 0;
4104
+ let insertIndex = 0;
4105
+ for (let i = 0; i < measure.entries.length; i++) {
4106
+ const entry = measure.entries[i];
4107
+ if (currentPosition >= position) {
4108
+ insertIndex = i;
4109
+ break;
4110
+ }
4111
+ if (entry.type === "note" && !entry.chord) {
4112
+ currentPosition += entry.duration;
4113
+ } else if (entry.type === "forward") {
4114
+ currentPosition += entry.duration;
4115
+ } else if (entry.type === "backup") {
4116
+ currentPosition -= entry.duration;
4117
+ }
4118
+ insertIndex = i + 1;
4119
+ }
4120
+ measure.entries.splice(insertIndex, 0, harmony);
4121
+ return success(result);
4122
+ }
4123
+ function removeHarmony(score, options) {
4124
+ const { partIndex, measureIndex, harmonyIndex } = options;
4125
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4126
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4127
+ }
4128
+ const part = score.parts[partIndex];
4129
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4130
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4131
+ }
4132
+ const measure = part.measures[measureIndex];
4133
+ let harmonyCount = 0;
4134
+ let targetEntryIndex = -1;
4135
+ for (let i = 0; i < measure.entries.length; i++) {
4136
+ const entry = measure.entries[i];
4137
+ if (entry.type === "harmony") {
4138
+ if (harmonyCount === harmonyIndex) {
4139
+ targetEntryIndex = i;
4140
+ break;
4141
+ }
4142
+ harmonyCount++;
4143
+ }
4144
+ }
4145
+ if (targetEntryIndex < 0) {
4146
+ return failure([operationError("HARMONY_NOT_FOUND", `Harmony at index ${harmonyIndex} not found`, { partIndex, measureIndex })]);
4147
+ }
4148
+ const result = cloneScore(score);
4149
+ result.parts[partIndex].measures[measureIndex].entries.splice(targetEntryIndex, 1);
4150
+ return success(result);
4151
+ }
4152
+ function updateHarmony(score, options) {
4153
+ const { partIndex, measureIndex, harmonyIndex, root, kind, kindText, bass, degrees } = options;
4154
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4155
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4156
+ }
4157
+ const part = score.parts[partIndex];
4158
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4159
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4160
+ }
4161
+ const measure = part.measures[measureIndex];
4162
+ let harmonyCount = 0;
4163
+ let targetEntryIndex = -1;
4164
+ for (let i = 0; i < measure.entries.length; i++) {
4165
+ const entry = measure.entries[i];
4166
+ if (entry.type === "harmony") {
4167
+ if (harmonyCount === harmonyIndex) {
4168
+ targetEntryIndex = i;
4169
+ break;
4170
+ }
4171
+ harmonyCount++;
4172
+ }
4173
+ }
4174
+ if (targetEntryIndex < 0) {
4175
+ return failure([operationError("HARMONY_NOT_FOUND", `Harmony at index ${harmonyIndex} not found`, { partIndex, measureIndex })]);
4176
+ }
4177
+ const validSteps = ["A", "B", "C", "D", "E", "F", "G"];
4178
+ if (root && !validSteps.includes(root.step.toUpperCase())) {
4179
+ return failure([operationError("INVALID_HARMONY", `Invalid root step: ${root.step}`, { partIndex, measureIndex })]);
4180
+ }
4181
+ if (bass && !validSteps.includes(bass.step.toUpperCase())) {
4182
+ return failure([operationError("INVALID_HARMONY", `Invalid bass step: ${bass.step}`, { partIndex, measureIndex })]);
4183
+ }
4184
+ const result = cloneScore(score);
4185
+ const harmony = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4186
+ if (root) {
4187
+ harmony.root = {
4188
+ rootStep: root.step.toUpperCase(),
4189
+ rootAlter: root.alter
4190
+ };
4191
+ }
4192
+ if (kind !== void 0) {
4193
+ harmony.kind = kind;
4194
+ }
4195
+ if (kindText !== void 0) {
4196
+ harmony.kindText = kindText;
4197
+ }
4198
+ if (bass !== void 0) {
4199
+ if (bass === null) {
4200
+ delete harmony.bass;
4201
+ } else {
4202
+ harmony.bass = {
4203
+ bassStep: bass.step.toUpperCase(),
4204
+ bassAlter: bass.alter
4205
+ };
4206
+ }
4207
+ }
4208
+ if (degrees !== void 0) {
4209
+ if (degrees === null) {
4210
+ delete harmony.degrees;
4211
+ } else {
4212
+ harmony.degrees = degrees.map((d) => ({
4213
+ degreeValue: d.value,
4214
+ degreeAlter: d.alter,
4215
+ degreeType: d.type
4216
+ }));
4217
+ }
4218
+ }
4219
+ return success(result);
4220
+ }
4221
+ function addFingering(score, options) {
4222
+ const { partIndex, measureIndex, noteIndex, fingering, substitution = false, alternate = false, placement } = options;
4223
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4224
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4225
+ }
4226
+ const part = score.parts[partIndex];
4227
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4228
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4229
+ }
4230
+ const measure = part.measures[measureIndex];
4231
+ let noteCount = 0;
4232
+ let targetEntryIndex = -1;
4233
+ for (let i = 0; i < measure.entries.length; i++) {
4234
+ const entry = measure.entries[i];
4235
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4236
+ if (noteCount === noteIndex) {
4237
+ targetEntryIndex = i;
4238
+ break;
4239
+ }
4240
+ noteCount++;
4241
+ }
4242
+ }
4243
+ if (targetEntryIndex < 0) {
4244
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4245
+ }
4246
+ const result = cloneScore(score);
4247
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4248
+ if (!resultNote.notations) {
4249
+ resultNote.notations = [];
4250
+ }
4251
+ resultNote.notations.push({
4252
+ type: "technical",
4253
+ technical: "fingering",
4254
+ fingering,
4255
+ fingeringSubstitution: substitution || void 0,
4256
+ fingeringAlternate: alternate || void 0,
4257
+ placement
4258
+ });
4259
+ return success(result);
4260
+ }
4261
+ function removeFingering(score, options) {
4262
+ const { partIndex, measureIndex, noteIndex } = options;
4263
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4264
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4265
+ }
4266
+ const part = score.parts[partIndex];
4267
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4268
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4269
+ }
4270
+ const measure = part.measures[measureIndex];
4271
+ let noteCount = 0;
4272
+ let targetEntryIndex = -1;
4273
+ for (let i = 0; i < measure.entries.length; i++) {
4274
+ const entry = measure.entries[i];
4275
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4276
+ if (noteCount === noteIndex) {
4277
+ targetEntryIndex = i;
4278
+ break;
4279
+ }
4280
+ noteCount++;
4281
+ }
4282
+ }
4283
+ if (targetEntryIndex < 0) {
4284
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4285
+ }
4286
+ const targetEntry = measure.entries[targetEntryIndex];
4287
+ if (targetEntry.type !== "note" || !targetEntry.notations) {
4288
+ return failure([operationError("NOTE_NOT_FOUND", `No notations found on note`, { partIndex, measureIndex })]);
4289
+ }
4290
+ const fingeringIndex = targetEntry.notations.findIndex(
4291
+ (n) => n.type === "technical" && n.technical === "fingering"
4292
+ );
4293
+ if (fingeringIndex < 0) {
4294
+ return failure([operationError("NOTE_NOT_FOUND", `No fingering found on note`, { partIndex, measureIndex })]);
4295
+ }
4296
+ const result = cloneScore(score);
4297
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4298
+ resultNote.notations.splice(fingeringIndex, 1);
4299
+ if (resultNote.notations.length === 0) {
4300
+ delete resultNote.notations;
4301
+ }
4302
+ return success(result);
4303
+ }
4304
+ function addBowing(score, options) {
4305
+ const { partIndex, measureIndex, noteIndex, bowingType, placement } = options;
4306
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4307
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4308
+ }
4309
+ const part = score.parts[partIndex];
4310
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4311
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4312
+ }
4313
+ const measure = part.measures[measureIndex];
4314
+ let noteCount = 0;
4315
+ let targetEntryIndex = -1;
4316
+ for (let i = 0; i < measure.entries.length; i++) {
4317
+ const entry = measure.entries[i];
4318
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4319
+ if (noteCount === noteIndex) {
4320
+ targetEntryIndex = i;
4321
+ break;
4322
+ }
4323
+ noteCount++;
4324
+ }
4325
+ }
4326
+ if (targetEntryIndex < 0) {
4327
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4328
+ }
4329
+ const result = cloneScore(score);
4330
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4331
+ if (!resultNote.notations) {
4332
+ resultNote.notations = [];
4333
+ }
4334
+ resultNote.notations.push({
4335
+ type: "technical",
4336
+ technical: bowingType,
4337
+ placement
4338
+ });
4339
+ return success(result);
4340
+ }
4341
+ function removeBowing(score, options) {
4342
+ const { partIndex, measureIndex, noteIndex, bowingType } = options;
4343
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4344
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4345
+ }
4346
+ const part = score.parts[partIndex];
4347
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4348
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4349
+ }
4350
+ const measure = part.measures[measureIndex];
4351
+ let noteCount = 0;
4352
+ let targetEntryIndex = -1;
4353
+ for (let i = 0; i < measure.entries.length; i++) {
4354
+ const entry = measure.entries[i];
4355
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4356
+ if (noteCount === noteIndex) {
4357
+ targetEntryIndex = i;
4358
+ break;
4359
+ }
4360
+ noteCount++;
4361
+ }
4362
+ }
4363
+ if (targetEntryIndex < 0) {
4364
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4365
+ }
4366
+ const targetEntry = measure.entries[targetEntryIndex];
4367
+ if (targetEntry.type !== "note" || !targetEntry.notations) {
4368
+ return failure([operationError("NOTE_NOT_FOUND", `No notations found on note`, { partIndex, measureIndex })]);
4369
+ }
4370
+ const bowingIndex = targetEntry.notations.findIndex((n) => {
4371
+ if (n.type !== "technical") return false;
4372
+ if (bowingType) return n.technical === bowingType;
4373
+ return n.technical === "up-bow" || n.technical === "down-bow";
4374
+ });
4375
+ if (bowingIndex < 0) {
4376
+ return failure([operationError("NOTE_NOT_FOUND", `No bowing found on note`, { partIndex, measureIndex })]);
4377
+ }
4378
+ const result = cloneScore(score);
4379
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4380
+ resultNote.notations.splice(bowingIndex, 1);
4381
+ if (resultNote.notations.length === 0) {
4382
+ delete resultNote.notations;
4383
+ }
4384
+ return success(result);
4385
+ }
4386
+ function addStringNumber(score, options) {
4387
+ const { partIndex, measureIndex, noteIndex, stringNumber, placement } = options;
4388
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4389
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4390
+ }
4391
+ const part = score.parts[partIndex];
4392
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4393
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4394
+ }
4395
+ if (stringNumber < 1) {
4396
+ return failure([operationError("INVALID_POSITION", `String number must be positive`, { partIndex, measureIndex })]);
4397
+ }
4398
+ const measure = part.measures[measureIndex];
4399
+ let noteCount = 0;
4400
+ let targetEntryIndex = -1;
4401
+ for (let i = 0; i < measure.entries.length; i++) {
4402
+ const entry = measure.entries[i];
4403
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4404
+ if (noteCount === noteIndex) {
4405
+ targetEntryIndex = i;
4406
+ break;
4407
+ }
4408
+ noteCount++;
4409
+ }
4410
+ }
4411
+ if (targetEntryIndex < 0) {
4412
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4413
+ }
4414
+ const result = cloneScore(score);
4415
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4416
+ if (!resultNote.notations) {
4417
+ resultNote.notations = [];
4418
+ }
4419
+ resultNote.notations.push({
4420
+ type: "technical",
4421
+ technical: "string",
4422
+ string: stringNumber,
4423
+ placement
4424
+ });
4425
+ return success(result);
4426
+ }
4427
+ function removeStringNumber(score, options) {
4428
+ const { partIndex, measureIndex, noteIndex } = options;
4429
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4430
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4431
+ }
4432
+ const part = score.parts[partIndex];
4433
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4434
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4435
+ }
4436
+ const measure = part.measures[measureIndex];
4437
+ let noteCount = 0;
4438
+ let targetEntryIndex = -1;
4439
+ for (let i = 0; i < measure.entries.length; i++) {
4440
+ const entry = measure.entries[i];
4441
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4442
+ if (noteCount === noteIndex) {
4443
+ targetEntryIndex = i;
4444
+ break;
4445
+ }
4446
+ noteCount++;
4447
+ }
4448
+ }
4449
+ if (targetEntryIndex < 0) {
4450
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4451
+ }
4452
+ const targetEntry = measure.entries[targetEntryIndex];
4453
+ if (targetEntry.type !== "note" || !targetEntry.notations) {
4454
+ return failure([operationError("NOTE_NOT_FOUND", `No notations found on note`, { partIndex, measureIndex })]);
4455
+ }
4456
+ const stringIndex = targetEntry.notations.findIndex(
4457
+ (n) => n.type === "technical" && n.technical === "string"
4458
+ );
4459
+ if (stringIndex < 0) {
4460
+ return failure([operationError("NOTE_NOT_FOUND", `No string number found on note`, { partIndex, measureIndex })]);
4461
+ }
4462
+ const result = cloneScore(score);
4463
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4464
+ resultNote.notations.splice(stringIndex, 1);
4465
+ if (resultNote.notations.length === 0) {
4466
+ delete resultNote.notations;
4467
+ }
4468
+ return success(result);
4469
+ }
4470
+ function addOctaveShift(score, options) {
4471
+ const { partIndex, measureIndex, position, shiftType, size = 8 } = options;
4472
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4473
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4474
+ }
4475
+ const part = score.parts[partIndex];
4476
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4477
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4478
+ }
4479
+ const result = cloneScore(score);
4480
+ const measure = result.parts[partIndex].measures[measureIndex];
4481
+ const direction = {
4482
+ _id: generateId(),
4483
+ type: "direction",
4484
+ directionTypes: [{
4485
+ kind: "octave-shift",
4486
+ type: shiftType,
4487
+ size
4488
+ }],
4489
+ placement: shiftType === "down" ? "above" : "below"
4490
+ };
4491
+ insertDirectionAtPosition(measure, direction, position);
4492
+ return success(result);
4493
+ }
4494
+ function stopOctaveShift(score, options) {
4495
+ const { partIndex, measureIndex, position, size = 8 } = options;
4496
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4497
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4498
+ }
4499
+ const part = score.parts[partIndex];
4500
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4501
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4502
+ }
4503
+ const result = cloneScore(score);
4504
+ const measure = result.parts[partIndex].measures[measureIndex];
4505
+ const direction = {
4506
+ _id: generateId(),
4507
+ type: "direction",
4508
+ directionTypes: [{
4509
+ kind: "octave-shift",
4510
+ type: "stop",
4511
+ size
4512
+ }]
4513
+ };
4514
+ insertDirectionAtPosition(measure, direction, position);
4515
+ return success(result);
4516
+ }
4517
+ function removeOctaveShift(score, options) {
4518
+ const { partIndex, measureIndex, octaveShiftIndex = 0 } = options;
4519
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4520
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4521
+ }
4522
+ const part = score.parts[partIndex];
4523
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4524
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4525
+ }
4526
+ const measure = part.measures[measureIndex];
4527
+ let shiftCount = 0;
4528
+ let targetEntryIndex = -1;
4529
+ for (let i = 0; i < measure.entries.length; i++) {
4530
+ const entry = measure.entries[i];
4531
+ if (entry.type === "direction") {
4532
+ const hasOctaveShift = entry.directionTypes.some((dt) => dt.kind === "octave-shift");
4533
+ if (hasOctaveShift) {
4534
+ if (shiftCount === octaveShiftIndex) {
4535
+ targetEntryIndex = i;
4536
+ break;
4537
+ }
4538
+ shiftCount++;
4539
+ }
4540
+ }
4541
+ }
4542
+ if (targetEntryIndex < 0) {
4543
+ return failure([operationError("NOTE_NOT_FOUND", `Octave shift at index ${octaveShiftIndex} not found`, { partIndex, measureIndex })]);
4544
+ }
4545
+ const result = cloneScore(score);
4546
+ result.parts[partIndex].measures[measureIndex].entries.splice(targetEntryIndex, 1);
4547
+ return success(result);
4548
+ }
4549
+ function addBreathMark(score, options) {
4550
+ const { partIndex, measureIndex, noteIndex, placement = "above" } = options;
4551
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4552
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4553
+ }
4554
+ const part = score.parts[partIndex];
4555
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4556
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4557
+ }
4558
+ const measure = part.measures[measureIndex];
4559
+ let noteCount = 0;
4560
+ let targetEntryIndex = -1;
4561
+ for (let i = 0; i < measure.entries.length; i++) {
4562
+ const entry = measure.entries[i];
4563
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4564
+ if (noteCount === noteIndex) {
4565
+ targetEntryIndex = i;
4566
+ break;
4567
+ }
4568
+ noteCount++;
4569
+ }
4570
+ }
4571
+ if (targetEntryIndex < 0) {
4572
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4573
+ }
4574
+ const result = cloneScore(score);
4575
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4576
+ if (!resultNote.notations) {
4577
+ resultNote.notations = [];
4578
+ }
4579
+ const existingBreathMark = resultNote.notations.find(
4580
+ (n) => n.type === "articulation" && n.articulation === "breath-mark"
4581
+ );
4582
+ if (existingBreathMark) {
4583
+ return failure([operationError("ARTICULATION_ALREADY_EXISTS", `Breath mark already exists on note`, { partIndex, measureIndex })]);
4584
+ }
4585
+ resultNote.notations.push({
4586
+ type: "articulation",
4587
+ articulation: "breath-mark",
4588
+ placement
4589
+ });
4590
+ return success(result);
4591
+ }
4592
+ function removeBreathMark(score, options) {
4593
+ const { partIndex, measureIndex, noteIndex } = options;
4594
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4595
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4596
+ }
4597
+ const part = score.parts[partIndex];
4598
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4599
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4600
+ }
4601
+ const measure = part.measures[measureIndex];
4602
+ let noteCount = 0;
4603
+ let targetEntryIndex = -1;
4604
+ for (let i = 0; i < measure.entries.length; i++) {
4605
+ const entry = measure.entries[i];
4606
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4607
+ if (noteCount === noteIndex) {
4608
+ targetEntryIndex = i;
4609
+ break;
4610
+ }
4611
+ noteCount++;
4612
+ }
4613
+ }
4614
+ if (targetEntryIndex < 0) {
4615
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4616
+ }
4617
+ const targetEntry = measure.entries[targetEntryIndex];
4618
+ if (targetEntry.type !== "note" || !targetEntry.notations) {
4619
+ return failure([operationError("ARTICULATION_NOT_FOUND", `No notations found on note`, { partIndex, measureIndex })]);
4620
+ }
4621
+ const breathMarkIndex = targetEntry.notations.findIndex(
4622
+ (n) => n.type === "articulation" && n.articulation === "breath-mark"
4623
+ );
4624
+ if (breathMarkIndex < 0) {
4625
+ return failure([operationError("ARTICULATION_NOT_FOUND", `No breath mark found on note`, { partIndex, measureIndex })]);
4626
+ }
4627
+ const result = cloneScore(score);
4628
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4629
+ resultNote.notations.splice(breathMarkIndex, 1);
4630
+ if (resultNote.notations.length === 0) {
4631
+ delete resultNote.notations;
4632
+ }
4633
+ return success(result);
4634
+ }
4635
+ function addCaesura(score, options) {
4636
+ const { partIndex, measureIndex, noteIndex, placement = "above" } = options;
4637
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4638
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4639
+ }
4640
+ const part = score.parts[partIndex];
4641
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4642
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4643
+ }
4644
+ const measure = part.measures[measureIndex];
4645
+ let noteCount = 0;
4646
+ let targetEntryIndex = -1;
4647
+ for (let i = 0; i < measure.entries.length; i++) {
4648
+ const entry = measure.entries[i];
4649
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4650
+ if (noteCount === noteIndex) {
4651
+ targetEntryIndex = i;
4652
+ break;
4653
+ }
4654
+ noteCount++;
4655
+ }
4656
+ }
4657
+ if (targetEntryIndex < 0) {
4658
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4659
+ }
4660
+ const result = cloneScore(score);
4661
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4662
+ if (!resultNote.notations) {
4663
+ resultNote.notations = [];
4664
+ }
4665
+ const existingCaesura = resultNote.notations.find(
4666
+ (n) => n.type === "articulation" && n.articulation === "caesura"
4667
+ );
4668
+ if (existingCaesura) {
4669
+ return failure([operationError("ARTICULATION_ALREADY_EXISTS", `Caesura already exists on note`, { partIndex, measureIndex })]);
4670
+ }
4671
+ resultNote.notations.push({
4672
+ type: "articulation",
4673
+ articulation: "caesura",
4674
+ placement
4675
+ });
4676
+ return success(result);
4677
+ }
4678
+ function removeCaesura(score, options) {
4679
+ const { partIndex, measureIndex, noteIndex } = options;
4680
+ if (partIndex < 0 || partIndex >= score.parts.length) {
4681
+ return failure([operationError("PART_NOT_FOUND", `Part index ${partIndex} out of bounds`, { partIndex })]);
4682
+ }
4683
+ const part = score.parts[partIndex];
4684
+ if (measureIndex < 0 || measureIndex >= part.measures.length) {
4685
+ return failure([operationError("MEASURE_NOT_FOUND", `Measure index ${measureIndex} out of bounds`, { partIndex, measureIndex })]);
4686
+ }
4687
+ const measure = part.measures[measureIndex];
4688
+ let noteCount = 0;
4689
+ let targetEntryIndex = -1;
4690
+ for (let i = 0; i < measure.entries.length; i++) {
4691
+ const entry = measure.entries[i];
4692
+ if (entry.type === "note" && !entry.chord && !entry.rest) {
4693
+ if (noteCount === noteIndex) {
4694
+ targetEntryIndex = i;
4695
+ break;
4696
+ }
4697
+ noteCount++;
4698
+ }
4699
+ }
4700
+ if (targetEntryIndex < 0) {
4701
+ return failure([operationError("NOTE_NOT_FOUND", `Note at index ${noteIndex} not found`, { partIndex, measureIndex })]);
4702
+ }
4703
+ const targetEntry = measure.entries[targetEntryIndex];
4704
+ if (targetEntry.type !== "note" || !targetEntry.notations) {
4705
+ return failure([operationError("ARTICULATION_NOT_FOUND", `No notations found on note`, { partIndex, measureIndex })]);
4706
+ }
4707
+ const caesuraIndex = targetEntry.notations.findIndex(
4708
+ (n) => n.type === "articulation" && n.articulation === "caesura"
4709
+ );
4710
+ if (caesuraIndex < 0) {
4711
+ return failure([operationError("ARTICULATION_NOT_FOUND", `No caesura found on note`, { partIndex, measureIndex })]);
4712
+ }
4713
+ const result = cloneScore(score);
4714
+ const resultNote = result.parts[partIndex].measures[measureIndex].entries[targetEntryIndex];
4715
+ resultNote.notations.splice(caesuraIndex, 1);
4716
+ if (resultNote.notations.length === 0) {
4717
+ delete resultNote.notations;
4718
+ }
4719
+ return success(result);
4720
+ }
1701
4721
  export {
4722
+ addArticulation,
4723
+ addBeam,
4724
+ addBowing,
4725
+ addBreathMark,
4726
+ addCaesura,
1702
4727
  addChord,
1703
4728
  addChordNote,
1704
4729
  addChordNoteChecked,
4730
+ addCoda,
4731
+ addDaCapo,
4732
+ addDalSegno,
4733
+ addDynamics,
4734
+ addEnding,
4735
+ addFermata,
4736
+ addFine,
4737
+ addFingering,
4738
+ addGraceNote,
4739
+ addHarmony,
4740
+ addLyric,
1705
4741
  addNote,
1706
4742
  addNoteChecked,
4743
+ addOctaveShift,
4744
+ addOrnament,
1707
4745
  addPart,
4746
+ addPedal,
4747
+ addRehearsalMark,
4748
+ addRepeatBarline,
4749
+ addSegno,
4750
+ addSlur,
4751
+ addStringNumber,
4752
+ addTempo,
4753
+ addTextDirection,
4754
+ addTie,
4755
+ addToCoda,
1708
4756
  addVoice,
4757
+ addWedge,
4758
+ autoBeam,
4759
+ changeBarline,
1709
4760
  changeKey,
1710
4761
  changeNoteDuration,
1711
4762
  changeTime,
4763
+ convertToGrace,
4764
+ copyNotes,
4765
+ copyNotesMultiMeasure,
4766
+ createTuplet,
4767
+ cutNotes,
1712
4768
  deleteMeasure,
1713
4769
  deleteNote,
1714
4770
  deleteNoteChecked,
1715
4771
  duplicatePart,
4772
+ insertClefChange,
1716
4773
  insertMeasure,
1717
4774
  insertNote,
4775
+ lowerAccidental,
1718
4776
  modifyNoteDuration,
1719
4777
  modifyNoteDurationChecked,
1720
4778
  modifyNotePitch,
1721
4779
  modifyNotePitchChecked,
1722
4780
  moveNoteToStaff,
4781
+ pasteNotes,
4782
+ pasteNotesMultiMeasure,
4783
+ raiseAccidental,
4784
+ removeArticulation,
4785
+ removeBeam,
4786
+ removeBowing,
4787
+ removeBreathMark,
4788
+ removeCaesura,
4789
+ removeDynamics,
4790
+ removeEnding,
4791
+ removeFermata,
4792
+ removeFingering,
4793
+ removeGraceNote,
4794
+ removeHarmony,
4795
+ removeLyric,
1723
4796
  removeNote,
4797
+ removeOctaveShift,
4798
+ removeOrnament,
1724
4799
  removePart,
4800
+ removePedal,
4801
+ removeRepeatBarline,
4802
+ removeSlur,
4803
+ removeStringNumber,
4804
+ removeTempo,
4805
+ removeTie,
4806
+ removeTuplet,
4807
+ removeWedge,
1725
4808
  setNotePitch,
4809
+ setNotePitchBySemitone,
1726
4810
  setStaves,
4811
+ shiftNotePitch,
4812
+ stopOctaveShift,
1727
4813
  transpose,
1728
- transposeChecked
4814
+ transposeChecked,
4815
+ updateHarmony,
4816
+ updateLyric
1729
4817
  };