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