jpfun 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/converter/midi/convert.d.ts +3 -1
- package/dist/converter/midi/convert.js +78 -66
- package/dist/converter/musicxml/features.d.ts +19 -5
- package/dist/converter/musicxml/features.js +46 -8
- package/dist/converter/musicxml/index.js +142 -23
- package/dist/converter/musicxml/model.d.ts +15 -0
- package/dist/diagnostic.js +1 -1
- package/dist/functions/ASTtypes.d.ts +4 -0
- package/dist/functions/ASTtypes.js +25 -14
- package/dist/functions/adjust/index.js +6 -2
- package/dist/functions/arpeggio/index.js +21 -17
- package/dist/functions/arpeggio/shape.d.ts +1 -1
- package/dist/functions/arpeggio/shape.js +27 -21
- package/dist/functions/bar/index.js +37 -36
- package/dist/functions/beam/auto.js +18 -24
- package/dist/functions/beam/layout.js +15 -23
- package/dist/functions/box/index.js +52 -1
- package/dist/functions/br/index.js +3 -4
- package/dist/functions/default.js +10 -5
- package/dist/functions/div/index.js +2 -3
- package/dist/functions/gliss/index.d.ts +2 -0
- package/dist/functions/gliss/index.js +180 -0
- package/dist/functions/grace/index.d.ts +4 -2
- package/dist/functions/grace/index.js +19 -11
- package/dist/functions/head/index.js +50 -62
- package/dist/functions/meter/index.js +45 -24
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/accidentals.js +10 -16
- package/dist/functions/note/index.js +38 -48
- package/dist/functions/ottava/index.d.ts +2 -0
- package/dist/functions/ottava/index.js +217 -0
- package/dist/functions/page/index.js +3 -3
- package/dist/functions/pagebreak/index.d.ts +2 -0
- package/dist/functions/pagebreak/index.js +36 -0
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +231 -0
- package/dist/functions/slide/index.d.ts +2 -0
- package/dist/functions/slide/index.js +132 -0
- package/dist/functions/stack/index.js +8 -15
- package/dist/functions/symbol/index.d.ts +9 -0
- package/dist/functions/symbol/index.js +41 -12
- package/dist/functions/symbol/ornament.js +15 -20
- package/dist/functions/symbol/shape.js +4 -1
- package/dist/functions/symbol/symbols/fine.js +1 -1
- package/dist/functions/symbol/symbols/tr.js +46 -1
- package/dist/functions/temporal.d.ts +36 -12
- package/dist/functions/temporal.js +29 -9
- package/dist/functions/tie/index.js +46 -32
- package/dist/functions/tuplet/index.js +1 -1
- package/dist/functions/up/index.js +51 -33
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +90 -0
- package/dist/functions/voice/index.js +245 -99
- package/dist/functions/volta/index.js +41 -21
- package/dist/jpfun.core.min.js +48 -55
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +57 -64
- package/dist/layout/engine.d.ts +4 -1
- package/dist/layout/engine.js +232 -75
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.d.ts +3 -2
- package/dist/layout/page.js +7 -6
- package/dist/layout/types.d.ts +34 -45
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +41 -171
- package/dist/parser/parserContext.d.ts +2 -0
- package/dist/parser/parserContext.js +31 -14
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.js +12 -3
- package/dist/playback/types.d.ts +4 -2
- package/dist/render/paint.js +1 -1
- package/dist/render/svg.js +1 -0
- package/dist/render/text.d.ts +1 -1
- package/dist/render/text.js +3 -2
- package/dist/render/types.d.ts +1 -0
- package/grammar.md +221 -257
- package/package.json +1 -1
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
import { Fraction } from "../../fraction.js";
|
|
12
12
|
import { NoteNameMap } from "../../parser/parse-utils/note-utils.js";
|
|
13
13
|
import { child, children, nameOf, number, text, } from "./dom.js";
|
|
14
|
-
import { MusicXmlReader, acceptTuplets, directionDynamic, directionTexts, endingPasses, metronomeBpm, mergeArpeggio, noteArpeggio, noteLyrics, noteModifiers, parsePitch, parseTimeSignature, partMeasures, readInstrument, reportDiagnostic, scoreMetadata, timeModification, transposePitch, } from "./features.js";
|
|
14
|
+
import { MusicXmlReader, acceptTuplets, directionDynamic, directionTexts, endingPasses, metronomeBpm, mergeArpeggio, noteArpeggio, noteGlissandi, noteLyrics, noteModifiers, parsePitch, parseTimeSignature, partMeasures, readInstrument, reportDiagnostic, scoreMetadata, timeModification, transposePitch, } from "./features.js";
|
|
15
15
|
import { attachAbove, quote, renderHead, renderPitch, renderSystems } from "../source.js";
|
|
16
16
|
import { matchingLanes, resolveRelations, resolveVoices } from "./resolve.js";
|
|
17
17
|
const MAJOR_KEYS = ["Cb", "Gb", "Db", "Ab", "Eb", "Bb", "F", "C", "G", "D", "A", "E", "B", "F#", "C#"];
|
|
@@ -67,7 +67,7 @@ function setBar(bars, at, token) {
|
|
|
67
67
|
else if (token === "|:" && current.includes(":|")) {
|
|
68
68
|
bars.set(key, current === ":|" ? ":|:" : `${current} |:`);
|
|
69
69
|
}
|
|
70
|
-
else if (token === "
|
|
70
|
+
else if (token === "|||" && !current.includes("|:") && !current.includes(":|")) {
|
|
71
71
|
bars.set(key, token);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
@@ -108,6 +108,7 @@ function parseScore(root, diagnostics) {
|
|
|
108
108
|
const bars = new Map();
|
|
109
109
|
const measureBoundaries = new Set();
|
|
110
110
|
const lineBreaks = new Set();
|
|
111
|
+
const multipleRests = [];
|
|
111
112
|
let scoreEnd = new Fraction();
|
|
112
113
|
let order = 0;
|
|
113
114
|
// 主时间解析:逐 part、逐小节扫描 attributes/direction/note/barline
|
|
@@ -128,6 +129,8 @@ function parseScore(root, diagnostics) {
|
|
|
128
129
|
const pitches = [];
|
|
129
130
|
const instrumentChanges = [];
|
|
130
131
|
const eventInstruments = new Map();
|
|
132
|
+
const measureStarts = [];
|
|
133
|
+
const firstRest = multipleRests.length;
|
|
131
134
|
const instrumentAt = (id, at) => {
|
|
132
135
|
const value = { ...(partInstruments?.byId.get(id) ?? partInstruments?.fallback) };
|
|
133
136
|
for (const change of instrumentChanges) {
|
|
@@ -138,6 +141,7 @@ function parseScore(root, diagnostics) {
|
|
|
138
141
|
};
|
|
139
142
|
// 小节游标从零开始,partTime 保存当前小节在全谱中的绝对起点
|
|
140
143
|
for (let measureIndex = 0; measureIndex < measures.length; measureIndex++) {
|
|
144
|
+
measureStarts.push(partTime.clone());
|
|
141
145
|
const { body: measure, container: measureContainer } = measures[measureIndex];
|
|
142
146
|
const measureLocation = {
|
|
143
147
|
partId, measureIndex: measureIndex + 1,
|
|
@@ -171,7 +175,7 @@ function parseScore(root, diagnostics) {
|
|
|
171
175
|
setBar(bars, at, new Array(times - 1).fill(":|").join(" "));
|
|
172
176
|
}
|
|
173
177
|
else if (text(barline, "bar-style") === "light-heavy")
|
|
174
|
-
setBar(bars, at, "
|
|
178
|
+
setBar(bars, at, "|||");
|
|
175
179
|
const ending = child(barline, "ending");
|
|
176
180
|
const endingType = ending?.getAttribute("type");
|
|
177
181
|
if (ending && (endingType === "start" || endingType === "stop" || endingType === "discontinue")) {
|
|
@@ -221,6 +225,20 @@ function parseScore(root, diagnostics) {
|
|
|
221
225
|
mode: text(key, "mode") || "major",
|
|
222
226
|
});
|
|
223
227
|
}
|
|
228
|
+
for (const style of children(item, "measure-style")) {
|
|
229
|
+
if (!child(style, "multiple-rest"))
|
|
230
|
+
continue;
|
|
231
|
+
const count = number(style, "multiple-rest");
|
|
232
|
+
if (!Number.isSafeInteger(count) || count <= 0 || count > 0xffffffff) {
|
|
233
|
+
throw new RangeError("MusicXML multiple-rest must be a positive integer in 1..4294967295");
|
|
234
|
+
}
|
|
235
|
+
multipleRests.push({
|
|
236
|
+
location: { ...measureLocation, element: "multiple-rest",
|
|
237
|
+
staff: style.getAttribute("number") || undefined },
|
|
238
|
+
start: at.clone(),
|
|
239
|
+
count,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
224
242
|
continue;
|
|
225
243
|
}
|
|
226
244
|
// backup 和 forward 只重定位当前小节游标,用于交错 voice
|
|
@@ -318,6 +336,7 @@ function parseScore(root, diagnostics) {
|
|
|
318
336
|
const notationElements = children(item, "notations");
|
|
319
337
|
const notations = notationElements.length ? new MusicXmlReader(notationElements) : undefined;
|
|
320
338
|
const pitch = parsePitch(item, location, notations);
|
|
339
|
+
noteGlissandi(notations, pitch, diagnostics);
|
|
321
340
|
const rest = child(item, "rest") !== undefined;
|
|
322
341
|
if (!pitch && !rest) {
|
|
323
342
|
throw new TypeError("MusicXML note must contain pitch, unpitched, or rest");
|
|
@@ -391,7 +410,7 @@ function parseScore(root, diagnostics) {
|
|
|
391
410
|
throw new RangeError("MusicXML non-grace notes require a positive duration");
|
|
392
411
|
let event = chord ? previousEvent : undefined;
|
|
393
412
|
const lyrics = noteLyrics(item);
|
|
394
|
-
const modifiers = noteModifiers(notations, diagnostics, location);
|
|
413
|
+
const { modifiers, slides } = noteModifiers(notations, diagnostics, location);
|
|
395
414
|
const arpeggio = noteArpeggio(notations, location);
|
|
396
415
|
const tuplets = notations?.readAll("tuplet");
|
|
397
416
|
// chord 成员只有起点和时值都一致时才合并到前一事件
|
|
@@ -408,6 +427,7 @@ function parseScore(root, diagnostics) {
|
|
|
408
427
|
postGraces: [],
|
|
409
428
|
lyrics,
|
|
410
429
|
arpeggio,
|
|
430
|
+
slides,
|
|
411
431
|
timeModification: timeModification(item, tuplets),
|
|
412
432
|
};
|
|
413
433
|
pendingGraces.delete(laneKey);
|
|
@@ -417,6 +437,11 @@ function parseScore(root, diagnostics) {
|
|
|
417
437
|
}
|
|
418
438
|
else {
|
|
419
439
|
event.arpeggio = mergeArpeggio(event.arpeggio, arpeggio);
|
|
440
|
+
for (const slide of slides) {
|
|
441
|
+
if (!event.slides?.some(item => item.side === slide.side && item.direction === slide.direction)) {
|
|
442
|
+
(event.slides ??= []).push(slide);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
420
445
|
for (const modifier of modifiers) {
|
|
421
446
|
if (!event.modifiers.some(item => item.name === modifier.name && item.placement === modifier.placement)) {
|
|
422
447
|
event.modifiers.push(modifier);
|
|
@@ -463,6 +488,10 @@ function parseScore(root, diagnostics) {
|
|
|
463
488
|
if (scoreEnd.compare(partTime) < 0)
|
|
464
489
|
scoreEnd = partTime.clone();
|
|
465
490
|
}
|
|
491
|
+
measureStarts.push(partTime.clone());
|
|
492
|
+
for (const rest of multipleRests.slice(firstRest)) {
|
|
493
|
+
rest.end = measureStarts[rest.location.measureIndex - 1 + rest.count];
|
|
494
|
+
}
|
|
466
495
|
for (const groups of pendingGraces.values())
|
|
467
496
|
for (const group of groups)
|
|
468
497
|
for (const pitch of group) {
|
|
@@ -476,7 +505,7 @@ function parseScore(root, diagnostics) {
|
|
|
476
505
|
}
|
|
477
506
|
for (const [event, id] of eventInstruments)
|
|
478
507
|
event.program = instrumentAt(id, event.start).program;
|
|
479
|
-
setBar(bars, partTime, "
|
|
508
|
+
setBar(bars, partTime, "|||");
|
|
480
509
|
}
|
|
481
510
|
const laneList = [...lanes.values()];
|
|
482
511
|
const { wedges, endings } = resolveRelations(wedgePoints, [...endingPoints.values()], diagnostics);
|
|
@@ -504,6 +533,7 @@ function parseScore(root, diagnostics) {
|
|
|
504
533
|
bars,
|
|
505
534
|
measureBoundaries,
|
|
506
535
|
lineBreaks,
|
|
536
|
+
multipleRests,
|
|
507
537
|
end: scoreEnd,
|
|
508
538
|
};
|
|
509
539
|
}
|
|
@@ -580,6 +610,9 @@ function eventSource(event, mode, key, metadata, diagnostics, suffix = "") {
|
|
|
580
610
|
else
|
|
581
611
|
reportDiagnostic(diagnostics, "arpeggioOutput", event.arpeggio.location);
|
|
582
612
|
}
|
|
613
|
+
for (const slide of event.slides ?? []) {
|
|
614
|
+
source = `@slide(${source}, side=${slide.side}, direction=${slide.direction})`;
|
|
615
|
+
}
|
|
583
616
|
const graceSource = (groups) => groups.map(group => group.length === 1
|
|
584
617
|
? pitchSource(group[0], mode, key, metadata)
|
|
585
618
|
: `{${group.map(pitch => pitchSource(pitch, mode, key, metadata)).join(" ^ ")}}`);
|
|
@@ -796,12 +829,36 @@ function labelRelations(score, diagnostics) {
|
|
|
796
829
|
endpoint.label = `mx${labelIndex++}`;
|
|
797
830
|
return `${source}@${endpoint.label}`;
|
|
798
831
|
},
|
|
832
|
+
labelRest(source, events, end) {
|
|
833
|
+
const labeled = events.find(event => metadata.labels.has(event));
|
|
834
|
+
const endpoint = endingLabels.get(keyOf(end));
|
|
835
|
+
if (!labeled && !endpoint)
|
|
836
|
+
return source;
|
|
837
|
+
const label = labelOf(labeled ?? events[0]);
|
|
838
|
+
for (const event of events)
|
|
839
|
+
if (metadata.labels.has(event))
|
|
840
|
+
metadata.labels.set(event, label);
|
|
841
|
+
if (endpoint) {
|
|
842
|
+
endpoint.at = events[0].start;
|
|
843
|
+
endpoint.label = label;
|
|
844
|
+
}
|
|
845
|
+
return `${source}@${label}`;
|
|
846
|
+
},
|
|
799
847
|
};
|
|
800
848
|
const labelOf = (target) => {
|
|
801
849
|
const label = metadata.labels.get(target) ?? `mx${labelIndex++}`;
|
|
802
850
|
metadata.labels.set(target, label);
|
|
803
851
|
return label;
|
|
804
852
|
};
|
|
853
|
+
const appendRelation = (event, source) => {
|
|
854
|
+
let after = metadata.afterSources.get(event);
|
|
855
|
+
if (!after)
|
|
856
|
+
metadata.afterSources.set(event, after = []);
|
|
857
|
+
after.push(source);
|
|
858
|
+
};
|
|
859
|
+
const activeGlissandi = new Map();
|
|
860
|
+
const glissLineBreaks = [...score.lineBreaks].map(fractionFromKey);
|
|
861
|
+
const glissandi = [];
|
|
805
862
|
const active = new Map();
|
|
806
863
|
const unmatchedTies = new Set();
|
|
807
864
|
const endingActive = new Map();
|
|
@@ -826,13 +883,13 @@ function labelRelations(score, diagnostics) {
|
|
|
826
883
|
...event.postGraces.flat(),
|
|
827
884
|
];
|
|
828
885
|
for (const pitch of orderedPitches) {
|
|
886
|
+
if (pitch.glissandi?.length)
|
|
887
|
+
glissandi.push({ event, pitch });
|
|
829
888
|
const key = `${lane.partId}\0${lane.staff}\0${lane.voice}\0${pitch.step}\0${pitch.alter}\0${pitch.octave}`;
|
|
830
889
|
const previous = localActive.get(key) ?? (ending ? active.get(key) : undefined);
|
|
831
890
|
if (pitch.tieStop && previous) {
|
|
832
891
|
unmatchedTies.delete(previous);
|
|
833
|
-
|
|
834
|
-
after.push(`@tie(${labelOf(previous)}, ${labelOf(pitch)})`);
|
|
835
|
-
metadata.afterSources.set(event, after);
|
|
892
|
+
appendRelation(event, `@tie(${labelOf(previous)}, ${labelOf(pitch)})`);
|
|
836
893
|
}
|
|
837
894
|
else if (pitch.tieStop) {
|
|
838
895
|
reportDiagnostic(diagnostics, "tieStart", pitch.location);
|
|
@@ -848,6 +905,28 @@ function labelRelations(score, diagnostics) {
|
|
|
848
905
|
}
|
|
849
906
|
for (const pitch of unmatchedTies)
|
|
850
907
|
reportDiagnostic(diagnostics, "tieStop", pitch.location);
|
|
908
|
+
// 复用上面的音高遍历,仅排序滑音端点;无滑音时不额外遍历或排序全谱
|
|
909
|
+
for (const { event, pitch } of glissandi.sort((a, b) => a.event.start.compare(b.event.start) || a.event.order - b.event.order))
|
|
910
|
+
for (const mark of pitch.glissandi ?? []) {
|
|
911
|
+
const key = `${pitch.location.partId}\0${mark.kind}\0${mark.number}`;
|
|
912
|
+
const previous = activeGlissandi.get(key);
|
|
913
|
+
if (mark.type === "start") {
|
|
914
|
+
if (previous)
|
|
915
|
+
reportDiagnostic(diagnostics, "glissReplaced", previous.pitch.location);
|
|
916
|
+
activeGlissandi.set(key, { pitch, event });
|
|
917
|
+
continue;
|
|
918
|
+
}
|
|
919
|
+
activeGlissandi.delete(key);
|
|
920
|
+
if (!previous)
|
|
921
|
+
reportDiagnostic(diagnostics, "glissStart", pitch.location);
|
|
922
|
+
else if (previous.event.start.equals(event.start) || glissLineBreaks.some(time => time.compare(previous.event.start) > 0 && time.compare(event.start) <= 0)) {
|
|
923
|
+
reportDiagnostic(diagnostics, "glissEndpoints", pitch.location);
|
|
924
|
+
}
|
|
925
|
+
else
|
|
926
|
+
appendRelation(event, `@gliss(${labelOf(previous.pitch)}, ${labelOf(pitch)})`);
|
|
927
|
+
}
|
|
928
|
+
for (const { pitch } of activeGlissandi.values())
|
|
929
|
+
reportDiagnostic(diagnostics, "glissStop", pitch.location);
|
|
851
930
|
const eventLabel = (event) => labelOf(event.pitches.length === 1 ? event.pitches[0] : event);
|
|
852
931
|
for (const wedge of score.wedges) {
|
|
853
932
|
// 连续楔形区间落到实际起音端点后再输出离散的 dyn 关系
|
|
@@ -887,10 +966,10 @@ function labelRelations(score, diagnostics) {
|
|
|
887
966
|
}
|
|
888
967
|
const from = events[fromIndex];
|
|
889
968
|
const to = events[toIndex - 1];
|
|
890
|
-
|
|
969
|
+
eventLabel(from);
|
|
891
970
|
const endpoint = endingLabels.get(keyOf(ending.end)) ?? { at: to.start, label: eventLabel(to) };
|
|
892
971
|
endingLabels.set(keyOf(ending.end), endpoint);
|
|
893
|
-
relations.push(() => `@volta(${
|
|
972
|
+
relations.push(() => `@volta(${eventLabel(from)}, ${endpoint.label}, ${ending.from.passes.join(", ")})`);
|
|
894
973
|
}
|
|
895
974
|
return { relations, metadata };
|
|
896
975
|
}
|
|
@@ -950,8 +1029,6 @@ function renderScore(score, options, diagnostics) {
|
|
|
950
1029
|
}
|
|
951
1030
|
return programs;
|
|
952
1031
|
});
|
|
953
|
-
// 关系先分配标签,随后 eventSource 才能把标签写进正确音符
|
|
954
|
-
const { relations, metadata } = labelRelations(score, diagnostics);
|
|
955
1032
|
const barTimes = [...score.bars.keys()].map(fractionFromKey)
|
|
956
1033
|
.sort((left, right) => left.compare(right));
|
|
957
1034
|
const measureBars = [...score.measureBoundaries].map(fractionFromKey)
|
|
@@ -965,28 +1042,70 @@ function renderScore(score, options, diagnostics) {
|
|
|
965
1042
|
}
|
|
966
1043
|
}
|
|
967
1044
|
}
|
|
1045
|
+
// 换行确定后分配关系标签,随后 eventSource 写进正确音符
|
|
1046
|
+
const { relations, metadata } = labelRelations(score, diagnostics);
|
|
968
1047
|
const page = score.page
|
|
969
1048
|
? `@page(width=${score.page.width}px, height=${score.page.height}px, top=${score.page.top}px, bottom=${score.page.bottom}px, left=${score.page.left}px, right=${score.page.right}px)\n`
|
|
970
1049
|
: "";
|
|
971
|
-
// 明确的 MusicXML 换行优先;若会切开 tuplet
|
|
1050
|
+
// 明确的 MusicXML 换行优先;若会切开 tuplet 或多小节休止,则保留完整块
|
|
972
1051
|
const blocksByLane = score.lanes.map((lane, index) => renderBlocks(lane, pitchMode, score, keys, adjustmentsByLane[index], programsByLane[index], metadata, diagnostics));
|
|
973
|
-
const
|
|
1052
|
+
const acceptedRests = new Set();
|
|
1053
|
+
for (const rest of score.multipleRests) {
|
|
1054
|
+
const { start, end, count } = rest;
|
|
1055
|
+
const id = `${start}:${end}:${count}`;
|
|
1056
|
+
if (acceptedRests.has(id))
|
|
1057
|
+
continue;
|
|
1058
|
+
const interior = (at) => !!end && at.compare(start) > 0 && at.compare(end) < 0;
|
|
1059
|
+
const meter = pointAt(meters, start);
|
|
1060
|
+
const measureDuration = new Fraction(meter.numerator * 4, meter.denominator);
|
|
1061
|
+
// @rest 是全局休止列,只有所有声部同步休止才不会把其他声部的起音推迟。
|
|
1062
|
+
const covered = blocksByLane.map(blocks => blocks.slice(lowerBoundAt(blocks, start, block => block.start), lowerBoundAt(blocks, end ?? start, block => block.start)));
|
|
1063
|
+
const safe = end && end.clone().sub(start).equals(measureDuration.clone().mul(count))
|
|
1064
|
+
&& !meters.some(point => interior(point.at))
|
|
1065
|
+
&& ![...score.bars].some(([at, bar]) => interior(fractionFromKey(at)) && bar !== "|")
|
|
1066
|
+
&& !score.endings.some(span => interior(span.from.at) || interior(span.end))
|
|
1067
|
+
&& adjustmentsByLane.every(points => ![...points.values()].some(point => interior(point.at)))
|
|
1068
|
+
&& programsByLane.every(points => ![...points.keys()].some(at => interior(fractionFromKey(at))))
|
|
1069
|
+
&& covered.length > 0 && covered.every(blocks => blocks.length === count
|
|
1070
|
+
&& blocks.every((block, index) => {
|
|
1071
|
+
const event = block.event;
|
|
1072
|
+
return event?.rest && event.pitches.length === 0 && !event.arpeggio && !event.slides?.length
|
|
1073
|
+
&& event.modifiers.length === 0 && event.annotations.length === 0
|
|
1074
|
+
&& event.preGraces.length === 0 && event.postGraces.length === 0
|
|
1075
|
+
&& event.lyrics.size === 0 && !metadata.afterSources.has(event)
|
|
1076
|
+
&& block.start.equals(start.clone().add(measureDuration.clone().mul(index)))
|
|
1077
|
+
&& block.end.clone().sub(block.start).equals(measureDuration);
|
|
1078
|
+
}));
|
|
1079
|
+
if (!safe || !end) {
|
|
1080
|
+
reportDiagnostic(diagnostics, "multipleRest", rest.location);
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
acceptedRests.add(id);
|
|
1084
|
+
for (const [lane, blocks] of blocksByLane.entries()) {
|
|
1085
|
+
const program = programsByLane[lane].get(keyOf(start));
|
|
1086
|
+
const events = covered[lane].map(block => block.event);
|
|
1087
|
+
const source = (program === undefined ? "" : `@program(${program}) `)
|
|
1088
|
+
+ attachAdjustment(metadata.labelRest(`@rest(${count})`, events, end), adjustmentsByLane[lane].get(keyOf(start)));
|
|
1089
|
+
blocks.splice(lowerBoundAt(blocks, start, block => block.start), count, { start, end, source });
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
const atomicSpans = blocksByLane.flatMap(blocks => blocks
|
|
974
1093
|
.filter(block => block.source)
|
|
975
1094
|
.map(block => ({ start: block.start, end: block.end })))
|
|
976
1095
|
.sort((left, right) => left.start.compare(right.start) || left.end.compare(right.end));
|
|
977
|
-
const
|
|
978
|
-
for (const span of
|
|
979
|
-
const previous =
|
|
1096
|
+
const mergedAtomicSpans = [];
|
|
1097
|
+
for (const span of atomicSpans) {
|
|
1098
|
+
const previous = mergedAtomicSpans.at(-1);
|
|
980
1099
|
if (previous && span.start.compare(previous.end) < 0) {
|
|
981
1100
|
if (span.end.compare(previous.end) > 0)
|
|
982
1101
|
previous.end = span.end;
|
|
983
1102
|
}
|
|
984
1103
|
else
|
|
985
|
-
|
|
1104
|
+
mergedAtomicSpans.push(span);
|
|
986
1105
|
}
|
|
987
1106
|
const lineBreaks = new Map([...score.lineBreaks]
|
|
988
1107
|
.map(value => [value, fractionFromKey(value)])
|
|
989
|
-
.filter(([, at]) => !insideSpan(
|
|
1108
|
+
.filter(([, at]) => !insideSpan(mergedAtomicSpans, at)));
|
|
990
1109
|
// 每条 lane 独立沿同一组时间点输出;最后再按系统转置成完整 N: 组
|
|
991
1110
|
const voices = score.lanes.map((lane, laneIndex) => {
|
|
992
1111
|
const verses = new Set();
|
|
@@ -1007,7 +1126,7 @@ function renderScore(score, options, diagnostics) {
|
|
|
1007
1126
|
}
|
|
1008
1127
|
};
|
|
1009
1128
|
const blocks = blocksByLane[laneIndex];
|
|
1010
|
-
const
|
|
1129
|
+
const atomicBlocks = blocks.filter(block => block.source);
|
|
1011
1130
|
// 所有事件边界与控制点汇成一条时间线,每个相邻点只输出一个连续片段
|
|
1012
1131
|
const points = new Map();
|
|
1013
1132
|
points.set("0", new Fraction());
|
|
@@ -1017,13 +1136,13 @@ function renderScore(score, options, diagnostics) {
|
|
|
1017
1136
|
points.set(keyOf(block.end), block.end);
|
|
1018
1137
|
}
|
|
1019
1138
|
for (const at of barTimes)
|
|
1020
|
-
if (!insideSpan(
|
|
1139
|
+
if (!insideSpan(atomicBlocks, at))
|
|
1021
1140
|
points.set(keyOf(at), at);
|
|
1022
1141
|
for (const [value, at] of lineBreaks)
|
|
1023
1142
|
points.set(value, at);
|
|
1024
1143
|
const adjustments = adjustmentsByLane[laneIndex];
|
|
1025
1144
|
for (const [value, point] of adjustments) {
|
|
1026
|
-
if (!insideSpan(
|
|
1145
|
+
if (!insideSpan(atomicBlocks, point.at))
|
|
1027
1146
|
points.set(value, point.at);
|
|
1028
1147
|
}
|
|
1029
1148
|
const timeline = [...points.values()].sort((left, right) => left.compare(right));
|
|
@@ -1050,7 +1169,7 @@ function renderScore(score, options, diagnostics) {
|
|
|
1050
1169
|
const block = blocks[blockIndex];
|
|
1051
1170
|
if (block?.source) {
|
|
1052
1171
|
if (!block.start.equals(at) || !block.end.equals(next)) {
|
|
1053
|
-
throw new RangeError("MusicXML
|
|
1172
|
+
throw new RangeError("MusicXML atomic block cannot cross a barline or state change");
|
|
1054
1173
|
}
|
|
1055
1174
|
outputs.at(-1).push(block.source);
|
|
1056
1175
|
for (const item of block.lyricEvents ?? []) {
|
|
@@ -31,6 +31,11 @@ export interface MusicXmlPitch {
|
|
|
31
31
|
octave: number;
|
|
32
32
|
tieStart: boolean;
|
|
33
33
|
tieStop: boolean;
|
|
34
|
+
glissandi?: {
|
|
35
|
+
kind: "slide" | "glissando";
|
|
36
|
+
number: string;
|
|
37
|
+
type: "start" | "stop";
|
|
38
|
+
}[];
|
|
34
39
|
}
|
|
35
40
|
/** 一个 lane 上的原子事件,同起止音高已合并为一个和弦事件 */
|
|
36
41
|
export interface MusicXmlEvent {
|
|
@@ -52,6 +57,10 @@ export interface MusicXmlEvent {
|
|
|
52
57
|
postGraces: MusicXmlPitch[][];
|
|
53
58
|
lyrics: Map<string, string>;
|
|
54
59
|
arpeggio?: MusicXmlArpeggio;
|
|
60
|
+
slides?: {
|
|
61
|
+
side: "pre" | "post";
|
|
62
|
+
direction: "up" | "down";
|
|
63
|
+
}[];
|
|
55
64
|
program?: number;
|
|
56
65
|
timeModification?: {
|
|
57
66
|
actual: number;
|
|
@@ -147,6 +156,12 @@ export interface ParsedMusicXmlScore {
|
|
|
147
156
|
bars: Map<string, string>;
|
|
148
157
|
measureBoundaries: Set<string>;
|
|
149
158
|
lineBreaks: Set<string>;
|
|
159
|
+
multipleRests: {
|
|
160
|
+
location: MusicXmlPartLocation;
|
|
161
|
+
start: Fraction;
|
|
162
|
+
end?: Fraction;
|
|
163
|
+
count: number;
|
|
164
|
+
}[];
|
|
150
165
|
end: Fraction;
|
|
151
166
|
}
|
|
152
167
|
export {};
|
package/dist/diagnostic.js
CHANGED
|
@@ -89,7 +89,7 @@ Diagnostic.error = {
|
|
|
89
89
|
return new ErrorDiagnostic("E_INVALID_TONALITY", `无效的调性: "${tonality}",请提供有效的调性名称,必须 [一个大写字母][变音记号#bn只能一个][绝对八度数字],后二者可选`, span);
|
|
90
90
|
},
|
|
91
91
|
BreakInsideEvent: (span) => {
|
|
92
|
-
return new ErrorDiagnostic("E_BREAK_INSIDE_EVENT",
|
|
92
|
+
return new ErrorDiagnostic("E_BREAK_INSIDE_EVENT", `换行或换页切断了一个仍在持续的事件;请把长音写成多个事件,或调整换行、换页的位置`, span);
|
|
93
93
|
},
|
|
94
94
|
InvalidPageConfig: (message, span) => {
|
|
95
95
|
return new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", message, span);
|
|
@@ -64,6 +64,8 @@ export declare class ASTBraceNode extends ASTNodeBase {
|
|
|
64
64
|
}
|
|
65
65
|
export interface FunctionArgDef {
|
|
66
66
|
name?: string;
|
|
67
|
+
/** 仅接受命名传参,不占用位置参数序号 */
|
|
68
|
+
namedOnly?: boolean;
|
|
67
69
|
description?: string;
|
|
68
70
|
type: paramType;
|
|
69
71
|
/** 参数默认值 null表示必填 否则报错 */
|
|
@@ -77,6 +79,8 @@ export interface FunctionDef {
|
|
|
77
79
|
extraArgType?: paramType;
|
|
78
80
|
args: FunctionArgDef[];
|
|
79
81
|
}
|
|
82
|
+
/** 按名称或位置查找参数,位置查找跳过仅命名参数 */
|
|
83
|
+
export declare function resolveArgDef(def: FunctionDef, name: string | undefined, index: number): FunctionArgDef | undefined;
|
|
80
84
|
/** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
|
|
81
85
|
export declare function resolveArgType(def: FunctionDef, name: string | undefined, index: number): paramType | undefined;
|
|
82
86
|
/** 别名里的第一个是主名;`@set` 的 key 和 `getArgValue` 的前缀必须用同一个 */
|
|
@@ -97,11 +97,22 @@ export class ASTBraceNode extends ASTNodeBase {
|
|
|
97
97
|
return `{${this.content.map(item => item.toString(source)).join("")}}`;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
+
/** 按名称或位置查找参数,位置查找跳过仅命名参数 */
|
|
101
|
+
export function resolveArgDef(def, name, index) {
|
|
102
|
+
if (name) {
|
|
103
|
+
const key = name.toLowerCase();
|
|
104
|
+
return def.args.find(arg => arg.name?.toLowerCase() === key);
|
|
105
|
+
}
|
|
106
|
+
for (const arg of def.args) {
|
|
107
|
+
if (!arg.namedOnly && index-- === 0)
|
|
108
|
+
return arg;
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
100
112
|
/** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
|
|
101
113
|
export function resolveArgType(def, name, index) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return def.args[index]?.type ?? (def.allowExtraArgs ? def.extraArgType : undefined);
|
|
114
|
+
return resolveArgDef(def, name, index)?.type
|
|
115
|
+
?? (!name && def.allowExtraArgs ? def.extraArgType : undefined);
|
|
105
116
|
}
|
|
106
117
|
/** 别名里的第一个是主名;`@set` 的 key 和 `getArgValue` 的前缀必须用同一个 */
|
|
107
118
|
export function primaryName(def) {
|
|
@@ -126,23 +137,23 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
126
137
|
get deSugarAtom() { return this.constructor.deSugarAtom; }
|
|
127
138
|
static deSugarRelation = () => null;
|
|
128
139
|
get deSugarRelation() { return this.constructor.deSugarRelation; }
|
|
129
|
-
//
|
|
140
|
+
// 按声明顺序取值,位置编号独立于仅命名参数
|
|
130
141
|
getArgValue(args, ctx) {
|
|
131
142
|
const def = this.def;
|
|
132
143
|
if (!def)
|
|
133
144
|
return [];
|
|
134
|
-
const defArgs = def.args;
|
|
135
145
|
const prefix = primaryName(def);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
?? (
|
|
146
|
+
let position = 0;
|
|
147
|
+
return def.args.map((argDef, declaredIndex) => {
|
|
148
|
+
const index = argDef.namedOnly ? undefined : position++;
|
|
149
|
+
const name = argDef.name?.toLowerCase();
|
|
150
|
+
const value = (name ? args.get(name) : undefined)
|
|
151
|
+
?? (index === undefined ? undefined : args.get(index))
|
|
152
|
+
?? (name ? ctx.variables[`${prefix}.${name}`.toLowerCase()] : undefined)
|
|
142
153
|
?? argDef.default;
|
|
143
|
-
if (
|
|
144
|
-
throw Diagnostic.error.MissingArg(prefix, argDef.name || index, this.sourceSpan);
|
|
145
|
-
return
|
|
154
|
+
if (value === null)
|
|
155
|
+
throw Diagnostic.error.MissingArg(prefix, argDef.name || (index ?? declaredIndex), this.sourceSpan);
|
|
156
|
+
return value;
|
|
146
157
|
});
|
|
147
158
|
}
|
|
148
159
|
toString(source) {
|
|
@@ -106,6 +106,9 @@ function offsetTemporal(node, owner) {
|
|
|
106
106
|
const { dx, dy, dw, dh } = owner;
|
|
107
107
|
if (dx === 0 && dy === 0 && dw === 0 && dh === 0)
|
|
108
108
|
return;
|
|
109
|
+
if (dx !== 0 || dy !== 0) {
|
|
110
|
+
node.placementOffset = { x: (node.placementOffset?.x ?? 0) + dx, y: (node.placementOffset?.y ?? 0) + dy };
|
|
111
|
+
}
|
|
109
112
|
let placedX;
|
|
110
113
|
let placedY;
|
|
111
114
|
const finalize = node.finalizeLayout;
|
|
@@ -134,12 +137,13 @@ function offsetTemporal(node, owner) {
|
|
|
134
137
|
* 作用域里只要有对象被平移过,关系对象就已经跟着端点走了,这里不能再平移第二次
|
|
135
138
|
*/
|
|
136
139
|
function offsetAttachment(attachment, targets, dx, dy) {
|
|
137
|
-
if (dx === 0 && dy === 0)
|
|
140
|
+
if (targets.temporal > 0 || (dx === 0 && dy === 0))
|
|
138
141
|
return;
|
|
142
|
+
attachment.placementOffset = { x: (attachment.placementOffset?.x ?? 0) + dx, y: (attachment.placementOffset?.y ?? 0) + dy };
|
|
139
143
|
const createGeometry = attachment.createGeometry;
|
|
140
144
|
attachment.createGeometry = (context) => {
|
|
141
145
|
const geometry = createGeometry.call(attachment, context);
|
|
142
|
-
if (targets.temporal > 0)
|
|
146
|
+
if (targets.temporal > 0 || context.layoutOnly)
|
|
143
147
|
return geometry;
|
|
144
148
|
const shift = (region) => region.line === void 0
|
|
145
149
|
? { x: region.x + dx, y: region.y + dy, w: region.w, h: region.h }
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { Fraction } from "../../fraction.js";
|
|
3
|
-
import { prepareLayoutHost } from "../../layout/engine.js";
|
|
3
|
+
import { getLayoutBounds, prepareLayoutHost } from "../../layout/engine.js";
|
|
4
4
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
5
|
-
import { TemporalNodeBase } from "../temporal.js";
|
|
5
|
+
import { transferFunctionAddons, TemporalNodeBase } from "../temporal.js";
|
|
6
6
|
import { prepareArpeggioShape } from "./shape.js";
|
|
7
7
|
/** Fold 保持模块私有;琶音只在自己的边界读取它已经公开在实例上的结构。 */
|
|
8
8
|
function readFold(node) {
|
|
@@ -58,13 +58,12 @@ class ArpeggioFunction extends ASTFunctionNode {
|
|
|
58
58
|
content.parent = this;
|
|
59
59
|
}
|
|
60
60
|
loweringEnter(ctx, track) {
|
|
61
|
-
|
|
62
|
-
ctx.isolateFromLoweringGroups(() => {
|
|
61
|
+
const host = ctx.isolateFromLoweringGroups(() => {
|
|
63
62
|
const events = ctx.trackedEvents(this.content, new Fraction(), track).flat();
|
|
64
63
|
if (events.length !== 1 || !events[0].box) {
|
|
65
64
|
throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 的内容必须恰好产生一个同轨和弦", this.content.sourceSpan);
|
|
66
65
|
}
|
|
67
|
-
|
|
66
|
+
return events[0];
|
|
68
67
|
});
|
|
69
68
|
const fold = readFold(host);
|
|
70
69
|
if (!fold || fold.members.length < 2 || fold.members.some(member => member.track !== track)) {
|
|
@@ -108,32 +107,37 @@ class ArpeggioTemporal extends TemporalNodeBase {
|
|
|
108
107
|
this.host.T.copyFrom(this.T);
|
|
109
108
|
this.host.track = this.track;
|
|
110
109
|
this.host.layoutLine = this.layoutLine;
|
|
111
|
-
this.host.onTimeState?.(state);
|
|
110
|
+
const offset = this.host.onTimeState?.(state);
|
|
111
|
+
this.T.copyFrom(this.host.T);
|
|
112
|
+
return offset;
|
|
113
|
+
}
|
|
114
|
+
prepareHorizontal(line) {
|
|
115
|
+
this.host.prepareHorizontal?.(line);
|
|
112
116
|
}
|
|
113
117
|
prepareLayout(context) {
|
|
114
|
-
|
|
115
|
-
this.host.addon = this.addon;
|
|
116
|
-
this.addon = void 0;
|
|
117
|
-
}
|
|
118
|
+
transferFunctionAddons(this, this.host);
|
|
118
119
|
prepareLayoutHost(this.host, context);
|
|
120
|
+
this.springConfig = { ...this.host.springConfig, ...this.springConfig };
|
|
119
121
|
const em = this.ast.size;
|
|
120
122
|
const gap = em * 0.16;
|
|
121
|
-
const
|
|
123
|
+
const contentBounds = getLayoutBounds(this.host, true);
|
|
124
|
+
const bounds = getLayoutBounds(this.host);
|
|
125
|
+
const { commands, bounds: markBounds } = prepareArpeggioShape(contentBounds.h, em, this.ast.direction);
|
|
122
126
|
this.markCommands = commands;
|
|
123
127
|
this.markX = -markBounds.x;
|
|
124
128
|
this.hostX = markBounds.w + gap;
|
|
125
129
|
this.box.w = this.hostX + this.host.box.w;
|
|
126
|
-
this.box.h =
|
|
130
|
+
this.box.h = bounds.h;
|
|
127
131
|
this.box.anchor = this.hostX + this.host.box.anchor;
|
|
128
|
-
this.box.visualAxis = this.host.box.visualAxis;
|
|
132
|
+
this.box.visualAxis = this.host.box.visualAxis - bounds.y;
|
|
129
133
|
for (const name in this.host.ports) {
|
|
130
134
|
const port = this.host.ports[name];
|
|
131
|
-
this.ports[name] = { x: this.hostX + port.x, y: port.y };
|
|
135
|
+
this.ports[name] = { x: this.hostX + port.x, y: port.y - bounds.y };
|
|
132
136
|
}
|
|
133
137
|
}
|
|
134
138
|
onPlaced() {
|
|
135
139
|
this.host.box.x = this.box.x + this.hostX;
|
|
136
|
-
this.host.box.y = this.box.y;
|
|
140
|
+
this.host.box.y = this.box.y - (getLayoutBounds(this.host).y - this.host.box.y);
|
|
137
141
|
this.host.onPlaced?.();
|
|
138
142
|
}
|
|
139
143
|
/**
|
|
@@ -181,12 +185,12 @@ class ArpeggioTemporal extends TemporalNodeBase {
|
|
|
181
185
|
}
|
|
182
186
|
paint(painter) {
|
|
183
187
|
const originX = this.box.x;
|
|
184
|
-
const originY = this.
|
|
188
|
+
const originY = getLayoutBounds(this.host, true).y;
|
|
185
189
|
painter.drawPath(this.markCommands, { fill: "#000" }, {
|
|
186
190
|
x: originX + this.markX, y: originY, scaleX: 1, scaleY: 1,
|
|
187
191
|
});
|
|
188
192
|
this.host.paint(painter);
|
|
189
193
|
for (const decoration of this.host.decorations)
|
|
190
|
-
decoration.paint(painter);
|
|
194
|
+
decoration.paint?.(painter);
|
|
191
195
|
}
|
|
192
196
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Rect } from "../../layout/types.js";
|
|
2
2
|
import type { PathCommand } from "../../render/types.js";
|
|
3
|
-
/** 按 MuseScore
|
|
3
|
+
/** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
|
|
4
4
|
export declare function prepareArpeggioShape(height: number, em: number, direction: "" | "up" | "down"): {
|
|
5
5
|
commands: PathCommand[];
|
|
6
6
|
bounds: Rect;
|