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