jpfun 0.4.1 → 0.6.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/README.md +53 -113
- package/dist/converter/midi/convert.js +14 -7
- package/dist/converter/musicxml/features.d.ts +57 -16
- package/dist/converter/musicxml/features.js +204 -42
- package/dist/converter/musicxml/index.d.ts +7 -2
- package/dist/converter/musicxml/index.js +256 -268
- package/dist/converter/musicxml/model.d.ts +36 -7
- package/dist/converter/musicxml/resolve.d.ts +10 -0
- package/dist/converter/musicxml/resolve.js +152 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/fraction.js +7 -1
- package/dist/functions/ASTtypes.d.ts +6 -3
- package/dist/functions/ASTtypes.js +30 -23
- package/dist/functions/adjust/index.js +10 -50
- package/dist/functions/arpeggio/index.js +40 -25
- package/dist/functions/beam/auto.js +2 -11
- package/dist/functions/box/index.js +132 -82
- package/dist/functions/dash/index.js +20 -11
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +1 -1
- package/dist/functions/div/index.js +9 -10
- package/dist/functions/dot/index.js +4 -5
- package/dist/functions/dyn/index.js +28 -32
- package/dist/functions/grace/index.d.ts +23 -8
- package/dist/functions/grace/index.js +138 -119
- package/dist/functions/head/index.js +11 -4
- package/dist/functions/key/index.js +1 -1
- package/dist/functions/meter/index.js +40 -7
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/index.d.ts +6 -1
- package/dist/functions/note/index.js +60 -57
- package/dist/functions/note/source-transform.d.ts +21 -0
- package/dist/functions/note/source-transform.js +164 -0
- package/dist/functions/page/index.d.ts +5 -0
- package/dist/functions/page/index.js +8 -5
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +233 -0
- package/dist/functions/set/index.js +5 -1
- package/dist/functions/symbol/index.d.ts +2 -0
- package/dist/functions/symbol/index.js +1 -1
- package/dist/functions/symbol/ornament.d.ts +3 -3
- package/dist/functions/symbol/ornament.js +21 -35
- package/dist/functions/symbol/symbols/accent.js +5 -5
- package/dist/functions/symbol/symbols/fermata.js +5 -4
- package/dist/functions/temporal.d.ts +21 -7
- package/dist/functions/temporal.js +14 -4
- package/dist/functions/tie/index.js +36 -42
- package/dist/functions/tuplet/index.js +20 -24
- package/dist/functions/up/index.js +54 -34
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +88 -0
- package/dist/functions/voice/index.js +248 -102
- package/dist/functions/volta/index.js +7 -12
- package/dist/jpfun.core.min.js +61 -41
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +70 -50
- package/dist/layout/engine.d.ts +13 -1
- package/dist/layout/engine.js +458 -129
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.js +3 -1
- package/dist/layout/range.d.ts +26 -0
- package/dist/layout/range.js +75 -0
- package/dist/layout/types.d.ts +63 -49
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +34 -4
- package/dist/lowering/types.d.ts +5 -0
- package/dist/parser/parse-utils/length-utils.js +2 -0
- package/dist/parser/parse-utils/note-utils.d.ts +2 -0
- package/dist/parser/parse-utils/note-utils.js +6 -2
- package/dist/parser/parserContext.js +5 -9
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.d.ts +2 -9
- package/dist/playback/compile.js +297 -186
- package/dist/playback/event.d.ts +8 -16
- package/dist/playback/event.js +13 -3
- package/dist/playback/types.d.ts +76 -18
- package/dist/playback/types.js +1 -0
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.js +1 -1
- package/dist/render/recording.d.ts +1 -0
- package/dist/render/recording.js +21 -0
- package/dist/render/translate.d.ts +14 -0
- package/dist/render/translate.js +48 -0
- package/grammar.md +200 -250
- package/package.json +4 -4
|
@@ -1,7 +1,31 @@
|
|
|
1
1
|
import type { Fraction } from "../../fraction.js";
|
|
2
|
-
export
|
|
2
|
+
export interface MusicXmlDiagnostic {
|
|
3
|
+
code: "W_MUSICXML_PERCUSSION_SKIPPED" | "W_MUSICXML_APPROXIMATED" | "W_MUSICXML_UNSUPPORTED_ELEMENT" | "W_MUSICXML_UNATTACHED_CONTENT" | "W_MUSICXML_UNRESOLVED_RELATION";
|
|
4
|
+
severity: "warning";
|
|
5
|
+
message: string;
|
|
6
|
+
location: {
|
|
7
|
+
element: string;
|
|
8
|
+
partId?: string;
|
|
9
|
+
measureIndex?: number;
|
|
10
|
+
measureNumber?: string;
|
|
11
|
+
staff?: string;
|
|
12
|
+
voice?: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
type MusicXmlPartLocation = MusicXmlDiagnostic["location"] & {
|
|
16
|
+
partId: string;
|
|
17
|
+
measureIndex: number;
|
|
18
|
+
};
|
|
19
|
+
export interface MusicXmlArpeggio {
|
|
20
|
+
direction: "none" | "up" | "down";
|
|
21
|
+
location: MusicXmlPitch["location"];
|
|
22
|
+
}
|
|
3
23
|
/** 单个书面音高,tie 标记保留在音高上以支持和弦成员独立连音 */
|
|
4
24
|
export interface MusicXmlPitch {
|
|
25
|
+
location: MusicXmlPartLocation & {
|
|
26
|
+
staff: string;
|
|
27
|
+
voice: string;
|
|
28
|
+
};
|
|
5
29
|
step: string;
|
|
6
30
|
alter: number;
|
|
7
31
|
octave: number;
|
|
@@ -43,6 +67,10 @@ export interface MusicXmlLane {
|
|
|
43
67
|
staff: string;
|
|
44
68
|
voice: string;
|
|
45
69
|
events: MusicXmlEvent[];
|
|
70
|
+
dynamics?: ({
|
|
71
|
+
at: Fraction;
|
|
72
|
+
location: MusicXmlDiagnostic["location"];
|
|
73
|
+
} & MusicXmlEvent["modifiers"][number])[];
|
|
46
74
|
}
|
|
47
75
|
/** 全谱时间流上的拍号状态点 */
|
|
48
76
|
export interface MusicXmlMeterPoint {
|
|
@@ -58,10 +86,10 @@ export interface MusicXmlKeyPoint {
|
|
|
58
86
|
}
|
|
59
87
|
/** 尚未附着到事件的力度或文字方向 */
|
|
60
88
|
export interface MusicXmlDirectionPoint {
|
|
89
|
+
location: MusicXmlPartLocation & {
|
|
90
|
+
staff: string;
|
|
91
|
+
};
|
|
61
92
|
at: Fraction;
|
|
62
|
-
partId: string;
|
|
63
|
-
staff: string;
|
|
64
|
-
voice: string;
|
|
65
93
|
placement: "above" | "below";
|
|
66
94
|
dynamic?: string;
|
|
67
95
|
texts: {
|
|
@@ -71,10 +99,8 @@ export interface MusicXmlDirectionPoint {
|
|
|
71
99
|
}
|
|
72
100
|
/** 参与楔形线配对的原始起止点 */
|
|
73
101
|
export interface MusicXmlWedgePoint {
|
|
102
|
+
location: MusicXmlDirectionPoint["location"];
|
|
74
103
|
at: Fraction;
|
|
75
|
-
partId: string;
|
|
76
|
-
staff: string;
|
|
77
|
-
voice: string;
|
|
78
104
|
number: string;
|
|
79
105
|
type: "crescendo" | "diminuendo" | "stop";
|
|
80
106
|
}
|
|
@@ -85,6 +111,7 @@ export interface MusicXmlWedgeSpan {
|
|
|
85
111
|
}
|
|
86
112
|
/** 参与房子配对的原始起止点 */
|
|
87
113
|
export interface MusicXmlEndingPoint {
|
|
114
|
+
location: MusicXmlPartLocation;
|
|
88
115
|
at: Fraction;
|
|
89
116
|
type: "start" | "stop" | "discontinue";
|
|
90
117
|
passes: number[];
|
|
@@ -113,6 +140,7 @@ export interface ParsedMusicXmlScore {
|
|
|
113
140
|
tempos: {
|
|
114
141
|
at: Fraction;
|
|
115
142
|
bpm: number;
|
|
143
|
+
location?: MusicXmlDiagnostic["location"];
|
|
116
144
|
}[];
|
|
117
145
|
wedges: MusicXmlWedgeSpan[];
|
|
118
146
|
endings: MusicXmlEndingSpan[];
|
|
@@ -121,3 +149,4 @@ export interface ParsedMusicXmlScore {
|
|
|
121
149
|
lineBreaks: Set<string>;
|
|
122
150
|
end: Fraction;
|
|
123
151
|
}
|
|
152
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MusicXmlDiagnostic, MusicXmlDirectionPoint as DirectionPoint, MusicXmlEndingPoint as EndingPoint, MusicXmlEndingSpan as EndingSpan, MusicXmlLane as Lane, MusicXmlWedgePoint as WedgePoint, MusicXmlWedgeSpan as WedgeSpan } from "./model.js";
|
|
2
|
+
/** 省略 voice 才广播到同 staff;显式目标缺失时不得改写其他声部 */
|
|
3
|
+
export declare function matchingLanes(lanes: readonly Lane[], { partId, staff, voice }: DirectionPoint["location"]): Lane[];
|
|
4
|
+
/** 所有 part 扫描结束后配对关系;此阶段只处理语义点,不读取 DOM 或推进游标 */
|
|
5
|
+
export declare function resolveRelations(wedgePoints: WedgePoint[], endingPoints: EndingPoint[], diagnostics: MusicXmlDiagnostic[]): {
|
|
6
|
+
wedges: WedgeSpan[];
|
|
7
|
+
endings: EndingSpan[];
|
|
8
|
+
};
|
|
9
|
+
/** 锚点先于附着,附着先于拆声部,避免重复文字或遗漏派生声部的力度 */
|
|
10
|
+
export declare function resolveVoices(lanes: Lane[], endings: readonly EndingSpan[], directions: readonly DirectionPoint[], nextOrder: number, partNames: ReadonlyMap<string, string>, diagnostics: MusicXmlDiagnostic[]): Lane[];
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { quote } from "../source.js";
|
|
2
|
+
import { reportDiagnostic } from "./features.js";
|
|
3
|
+
function eventEnd(event) {
|
|
4
|
+
return event.start.clone().add(event.duration);
|
|
5
|
+
}
|
|
6
|
+
/** 省略 voice 才广播到同 staff;显式目标缺失时不得改写其他声部 */
|
|
7
|
+
export function matchingLanes(lanes, { partId, staff, voice }) {
|
|
8
|
+
return lanes.filter(lane => lane.partId === partId && lane.staff === staff && (!voice || lane.voice === voice));
|
|
9
|
+
}
|
|
10
|
+
/** 所有 part 扫描结束后配对关系;此阶段只处理语义点,不读取 DOM 或推进游标 */
|
|
11
|
+
export function resolveRelations(wedgePoints, endingPoints, diagnostics) {
|
|
12
|
+
const wedges = [];
|
|
13
|
+
const activeWedges = new Map();
|
|
14
|
+
for (const point of wedgePoints.sort((left, right) => left.at.compare(right.at))) {
|
|
15
|
+
const { partId, staff, voice = "" } = point.location;
|
|
16
|
+
const key = `${partId}\0${staff}\0${voice}\0${point.number}`;
|
|
17
|
+
if (point.type === "stop") {
|
|
18
|
+
const from = activeWedges.get(key);
|
|
19
|
+
if (from)
|
|
20
|
+
wedges.push({ from, end: point.at });
|
|
21
|
+
else
|
|
22
|
+
reportDiagnostic(diagnostics, "wedgeStart", point.location);
|
|
23
|
+
activeWedges.delete(key);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const previous = activeWedges.get(key);
|
|
27
|
+
if (previous)
|
|
28
|
+
reportDiagnostic(diagnostics, "wedgeReplaced", previous.location);
|
|
29
|
+
activeWedges.set(key, point);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const point of activeWedges.values())
|
|
33
|
+
reportDiagnostic(diagnostics, "wedgeStop", point.location);
|
|
34
|
+
const endings = [];
|
|
35
|
+
let activeEnding;
|
|
36
|
+
for (const point of endingPoints.sort((left, right) => left.at.compare(right.at))) {
|
|
37
|
+
if (point.type === "start") {
|
|
38
|
+
if (activeEnding)
|
|
39
|
+
reportDiagnostic(diagnostics, "endingReplaced", activeEnding.location);
|
|
40
|
+
activeEnding = point;
|
|
41
|
+
}
|
|
42
|
+
else if (activeEnding) {
|
|
43
|
+
endings.push({ from: activeEnding, end: point.at });
|
|
44
|
+
activeEnding = undefined;
|
|
45
|
+
}
|
|
46
|
+
else
|
|
47
|
+
reportDiagnostic(diagnostics, "endingStart", point.location);
|
|
48
|
+
}
|
|
49
|
+
if (activeEnding)
|
|
50
|
+
reportDiagnostic(diagnostics, "endingStop", activeEnding.location);
|
|
51
|
+
return { wedges, endings };
|
|
52
|
+
}
|
|
53
|
+
/** 为无起音的有效房子补休止端点;锚点不得覆盖已有长音,并延续解析阶段的事件次序 */
|
|
54
|
+
function anchorEndings(lanes, endings, directions, order, partNames) {
|
|
55
|
+
const topLane = lanes[0];
|
|
56
|
+
if (!topLane)
|
|
57
|
+
return;
|
|
58
|
+
for (const ending of endings) {
|
|
59
|
+
if (ending.from.at.compare(ending.end) >= 0)
|
|
60
|
+
continue;
|
|
61
|
+
const direction = directions.find(item => item.at.compare(ending.from.at) >= 0 && item.at.compare(ending.end) < 0);
|
|
62
|
+
const hostPartId = direction?.location.partId ?? topLane.partId;
|
|
63
|
+
const hostStaff = direction?.location.staff ?? topLane.staff;
|
|
64
|
+
const hostLanes = lanes.filter(lane => lane.partId === hostPartId && lane.staff === hostStaff);
|
|
65
|
+
if (hostLanes.some(lane => lane.events.some(event => event.start.compare(ending.from.at) >= 0 && event.start.compare(ending.end) < 0)))
|
|
66
|
+
continue;
|
|
67
|
+
let hostLane = hostLanes.find(lane => !lane.events.some(event => event.start.compare(ending.end) < 0 && eventEnd(event).compare(ending.from.at) > 0));
|
|
68
|
+
if (!hostLane) {
|
|
69
|
+
const voices = hostLanes.map(lane => Number(lane.voice)).filter(Number.isFinite);
|
|
70
|
+
hostLane = {
|
|
71
|
+
partId: hostPartId,
|
|
72
|
+
partName: partNames.get(hostPartId) ?? hostPartId,
|
|
73
|
+
staff: hostStaff,
|
|
74
|
+
voice: String(Math.max(0, ...voices) + 1),
|
|
75
|
+
events: [],
|
|
76
|
+
};
|
|
77
|
+
lanes.push(hostLane);
|
|
78
|
+
}
|
|
79
|
+
hostLane.events.push({
|
|
80
|
+
start: ending.from.at.clone(),
|
|
81
|
+
duration: ending.end.clone().sub(ending.from.at),
|
|
82
|
+
pitches: [],
|
|
83
|
+
rest: true,
|
|
84
|
+
order: order++,
|
|
85
|
+
modifiers: [],
|
|
86
|
+
annotations: [],
|
|
87
|
+
preGraces: [],
|
|
88
|
+
postGraces: [],
|
|
89
|
+
lyrics: new Map(),
|
|
90
|
+
});
|
|
91
|
+
hostLane.events.sort((left, right) => left.start.compare(right.start) || left.order - right.order);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** 无 voice 的力度作用于同 staff 的全部 lane;文字只附着一次,允许使用刚补出的房子锚点 */
|
|
95
|
+
function attachDirections(lanes, directions, diagnostics) {
|
|
96
|
+
for (const direction of directions) {
|
|
97
|
+
const targets = matchingLanes(lanes, direction.location);
|
|
98
|
+
if (direction.dynamic && targets.length === 0) {
|
|
99
|
+
reportDiagnostic(diagnostics, "dynamicHost", { ...direction.location, element: "dynamics" }, direction.dynamic);
|
|
100
|
+
}
|
|
101
|
+
if (direction.dynamic) {
|
|
102
|
+
const dynamic = {
|
|
103
|
+
at: direction.at,
|
|
104
|
+
name: direction.dynamic,
|
|
105
|
+
placement: direction.placement,
|
|
106
|
+
location: { ...direction.location, element: "dynamics" },
|
|
107
|
+
};
|
|
108
|
+
for (const lane of targets)
|
|
109
|
+
(lane.dynamics ??= []).push(dynamic);
|
|
110
|
+
}
|
|
111
|
+
if (direction.texts.length === 0)
|
|
112
|
+
continue;
|
|
113
|
+
let target;
|
|
114
|
+
findTarget: for (const lane of targets)
|
|
115
|
+
for (const event of lane.events) {
|
|
116
|
+
const position = event.start.compare(direction.at);
|
|
117
|
+
if (position < 0 && eventEnd(event).compare(direction.at) > 0) {
|
|
118
|
+
target = event;
|
|
119
|
+
break findTarget;
|
|
120
|
+
}
|
|
121
|
+
if (!target || target.start.compare(direction.at) < 0 || position >= 0
|
|
122
|
+
&& (event.start.compare(target.start) < 0 || event.start.equals(target.start) && event.order < target.order))
|
|
123
|
+
target = event;
|
|
124
|
+
}
|
|
125
|
+
for (const annotation of direction.texts) {
|
|
126
|
+
if (target)
|
|
127
|
+
target.annotations.push({ ...annotation, placement: direction.placement });
|
|
128
|
+
else
|
|
129
|
+
reportDiagnostic(diagnostics, "textHost", { ...direction.location, element: annotation.boxed ? "rehearsal" : "words" }, quote(annotation.text));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** 附着完成后拆分重叠事件;所有派生 lane 继承原有的声部身份及精确力度控制点 */
|
|
134
|
+
function splitOverlappingLanes(lanes) {
|
|
135
|
+
return lanes.flatMap(lane => {
|
|
136
|
+
const parallel = [];
|
|
137
|
+
for (const event of [...lane.events].sort((left, right) => left.start.compare(right.start) || left.order - right.order)) {
|
|
138
|
+
const target = parallel.find(candidate => eventEnd(candidate.events.at(-1)).compare(event.start) <= 0);
|
|
139
|
+
if (target)
|
|
140
|
+
target.events.push(event);
|
|
141
|
+
else
|
|
142
|
+
parallel.push({ ...lane, events: [event] });
|
|
143
|
+
}
|
|
144
|
+
return parallel.length ? parallel : [lane];
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/** 锚点先于附着,附着先于拆声部,避免重复文字或遗漏派生声部的力度 */
|
|
148
|
+
export function resolveVoices(lanes, endings, directions, nextOrder, partNames, diagnostics) {
|
|
149
|
+
anchorEndings(lanes, endings, directions, nextOrder, partNames);
|
|
150
|
+
attachDirections(lanes, directions, diagnostics);
|
|
151
|
+
return splitOverlappingLanes(lanes);
|
|
152
|
+
}
|
package/dist/core.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./pipeline.js";
|
|
|
2
2
|
export * from "./functions/ASTtypes.js";
|
|
3
3
|
export * from "./functions/temporal.js";
|
|
4
4
|
export * from "./functions/default.js";
|
|
5
|
+
export { transformNotes, type NoteTransform, type SourceEdit } from "./functions/note/index.js";
|
|
5
6
|
export * from "./diagnostic.js";
|
|
6
7
|
export * from "./fraction.js";
|
|
7
8
|
export * from "./parser/types.js";
|
package/dist/core.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./pipeline.js";
|
|
|
2
2
|
export * from "./functions/ASTtypes.js";
|
|
3
3
|
export * from "./functions/temporal.js";
|
|
4
4
|
export * from "./functions/default.js";
|
|
5
|
+
export { transformNotes } from "./functions/note/index.js";
|
|
5
6
|
export * from "./diagnostic.js";
|
|
6
7
|
export * from "./fraction.js";
|
|
7
8
|
export * from "./parser/types.js";
|
package/dist/fraction.js
CHANGED
|
@@ -24,6 +24,9 @@ export class Fraction {
|
|
|
24
24
|
return this._denominator;
|
|
25
25
|
}
|
|
26
26
|
set(numerator, denominator = 1) {
|
|
27
|
+
if (!Number.isFinite(numerator) || !Number.isFinite(denominator)) {
|
|
28
|
+
throw new RangeError("Fraction numerator and denominator must be finite");
|
|
29
|
+
}
|
|
27
30
|
if (denominator === 0)
|
|
28
31
|
throw new RangeError("Denominator must not be zero");
|
|
29
32
|
if (denominator < 0) {
|
|
@@ -96,7 +99,10 @@ export class Fraction {
|
|
|
96
99
|
this._numerator /= 2;
|
|
97
100
|
power--;
|
|
98
101
|
}
|
|
99
|
-
this._denominator
|
|
102
|
+
const denominator = this._denominator * 2 ** power;
|
|
103
|
+
if (!Number.isFinite(denominator))
|
|
104
|
+
throw new RangeError("Fraction denominator overflow");
|
|
105
|
+
this._denominator = denominator;
|
|
100
106
|
return this;
|
|
101
107
|
}
|
|
102
108
|
compare(value, denominator = 1) {
|
|
@@ -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` 的前缀必须用同一个 */
|
|
@@ -128,9 +132,8 @@ export declare class ASTFunctionNode extends ASTNodeBase {
|
|
|
128
132
|
static findLabelable(node: ASTNodeBase): ASTFunctionNode | null;
|
|
129
133
|
/**
|
|
130
134
|
* 用于语法糖向前结合
|
|
131
|
-
*
|
|
132
|
-
* 文本节点会打断连接 注意解析时已经跳过了空格 因此允许语法糖之间有空格
|
|
135
|
+
* 从指定位置向前寻找最后一个可由后缀语法糖包装的节点,跳过不产生语义对象的文本
|
|
133
136
|
*/
|
|
134
|
-
static
|
|
137
|
+
static findLastFuncContentIndex(nodes: ASTNodeBase[], i: number): number;
|
|
135
138
|
}
|
|
136
139
|
export type ASTFunctionClass = (new (sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent: ASTNodeBase | null) => ASTFunctionNode) & Pick<typeof ASTFunctionNode, "prototype" | "def" | "deSugarAtom" | "deSugarRelation" | "loweringAugment" | "loweringFinalize" | "layoutDecorationHandler">;
|
|
@@ -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) {
|
|
@@ -192,17 +203,13 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
192
203
|
}
|
|
193
204
|
/**
|
|
194
205
|
* 用于语法糖向前结合
|
|
195
|
-
*
|
|
196
|
-
* 文本节点会打断连接 注意解析时已经跳过了空格 因此允许语法糖之间有空格
|
|
206
|
+
* 从指定位置向前寻找最后一个可由后缀语法糖包装的节点,跳过不产生语义对象的文本
|
|
197
207
|
*/
|
|
198
|
-
static
|
|
208
|
+
static findLastFuncContentIndex(nodes, i) {
|
|
199
209
|
for (; i >= 0; i--) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
return null;
|
|
203
|
-
if (n instanceof ASTFunctionNode || n instanceof ASTBraceNode)
|
|
204
|
-
return n;
|
|
210
|
+
if (nodes[i] instanceof ASTFunctionNode || nodes[i] instanceof ASTBraceNode)
|
|
211
|
+
return i;
|
|
205
212
|
}
|
|
206
|
-
return
|
|
213
|
+
return -1;
|
|
207
214
|
}
|
|
208
215
|
}
|
|
@@ -2,6 +2,7 @@ import { ASTFunctionNode } from "../ASTtypes.js";
|
|
|
2
2
|
import { WarningDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { isVisualTemporalNode } from "../temporal.js";
|
|
4
4
|
import { isLayoutAttachment, } from "../../layout/types.js";
|
|
5
|
+
import { TranslatingPainter } from "../../render/translate.js";
|
|
5
6
|
class AdjustFunction extends ASTFunctionNode {
|
|
6
7
|
static def = {
|
|
7
8
|
name: ["adjust", "adj"],
|
|
@@ -95,6 +96,9 @@ class AdjustFunction extends ASTFunctionNode {
|
|
|
95
96
|
this.dw = ctx.length2px(dw);
|
|
96
97
|
this.dh = ctx.length2px(dh);
|
|
97
98
|
}
|
|
99
|
+
toString(source) {
|
|
100
|
+
return `@adjust(${this.target.toString(source)}, dx=${this.dx}px, dy=${this.dy}px, dw=${this.dw}px, dh=${this.dh}px)`;
|
|
101
|
+
}
|
|
98
102
|
}
|
|
99
103
|
export const AdjustNode = AdjustFunction;
|
|
100
104
|
/** 在原对象上组合布局生命周期,保持布局索引依赖的对象身份 */
|
|
@@ -102,6 +106,9 @@ function offsetTemporal(node, owner) {
|
|
|
102
106
|
const { dx, dy, dw, dh } = owner;
|
|
103
107
|
if (dx === 0 && dy === 0 && dw === 0 && dh === 0)
|
|
104
108
|
return;
|
|
109
|
+
if (dx !== 0 || dy !== 0) {
|
|
110
|
+
node.placementOffset = { x: (node.placementOffset?.x ?? 0) + dx, y: (node.placementOffset?.y ?? 0) + dy };
|
|
111
|
+
}
|
|
105
112
|
let placedX;
|
|
106
113
|
let placedY;
|
|
107
114
|
const finalize = node.finalizeLayout;
|
|
@@ -130,12 +137,13 @@ function offsetTemporal(node, owner) {
|
|
|
130
137
|
* 作用域里只要有对象被平移过,关系对象就已经跟着端点走了,这里不能再平移第二次
|
|
131
138
|
*/
|
|
132
139
|
function offsetAttachment(attachment, targets, dx, dy) {
|
|
133
|
-
if (dx === 0 && dy === 0)
|
|
140
|
+
if (targets.temporal > 0 || (dx === 0 && dy === 0))
|
|
134
141
|
return;
|
|
142
|
+
attachment.placementOffset = { x: (attachment.placementOffset?.x ?? 0) + dx, y: (attachment.placementOffset?.y ?? 0) + dy };
|
|
135
143
|
const createGeometry = attachment.createGeometry;
|
|
136
144
|
attachment.createGeometry = (context) => {
|
|
137
145
|
const geometry = createGeometry.call(attachment, context);
|
|
138
|
-
if (targets.temporal > 0)
|
|
146
|
+
if (targets.temporal > 0 || context.layoutOnly)
|
|
139
147
|
return geometry;
|
|
140
148
|
const shift = (region) => region.line === void 0
|
|
141
149
|
? { x: region.x + dx, y: region.y + dy, w: region.w, h: region.h }
|
|
@@ -150,51 +158,3 @@ function offsetAttachment(attachment, targets, dx, dy) {
|
|
|
150
158
|
};
|
|
151
159
|
};
|
|
152
160
|
}
|
|
153
|
-
/** Painter 没有变换栈,被微调的关系对象只能逐条命令平移 */
|
|
154
|
-
class TranslatingPainter {
|
|
155
|
-
target;
|
|
156
|
-
dx;
|
|
157
|
-
dy;
|
|
158
|
-
constructor(target, dx, dy) {
|
|
159
|
-
this.target = target;
|
|
160
|
-
this.dx = dx;
|
|
161
|
-
this.dy = dy;
|
|
162
|
-
}
|
|
163
|
-
drawText(text, x, y, style) {
|
|
164
|
-
this.target.drawText(text, x + this.dx, y + this.dy, style);
|
|
165
|
-
}
|
|
166
|
-
drawLine(x1, y1, x2, y2, style) {
|
|
167
|
-
this.target.drawLine(x1 + this.dx, y1 + this.dy, x2 + this.dx, y2 + this.dy, style);
|
|
168
|
-
}
|
|
169
|
-
drawRect(x, y, w, h, style) {
|
|
170
|
-
this.target.drawRect(x + this.dx, y + this.dy, w, h, style);
|
|
171
|
-
}
|
|
172
|
-
drawCircle(cx, cy, r, style) {
|
|
173
|
-
this.target.drawCircle(cx + this.dx, cy + this.dy, r, style);
|
|
174
|
-
}
|
|
175
|
-
drawPath(commands, style, transform) {
|
|
176
|
-
if (transform) {
|
|
177
|
-
const moved = { ...transform, x: transform.x + this.dx, y: transform.y + this.dy };
|
|
178
|
-
this.target.drawPath(commands, style, moved);
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
this.target.drawPath(commands.map(command => this.translateCommand(command)), style);
|
|
182
|
-
}
|
|
183
|
-
translateCommand(command) {
|
|
184
|
-
switch (command.op) {
|
|
185
|
-
case "Z": return command;
|
|
186
|
-
case "Q": return {
|
|
187
|
-
op: "Q",
|
|
188
|
-
cx: command.cx + this.dx, cy: command.cy + this.dy,
|
|
189
|
-
x: command.x + this.dx, y: command.y + this.dy,
|
|
190
|
-
};
|
|
191
|
-
case "C": return {
|
|
192
|
-
op: "C",
|
|
193
|
-
cx1: command.cx1 + this.dx, cy1: command.cy1 + this.dy,
|
|
194
|
-
cx2: command.cx2 + this.dx, cy2: command.cy2 + this.dy,
|
|
195
|
-
x: command.x + this.dx, y: command.y + this.dy,
|
|
196
|
-
};
|
|
197
|
-
default: return { op: command.op, x: command.x + this.dx, y: command.y + this.dy };
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
@@ -1,6 +1,6 @@
|
|
|
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
5
|
import { TemporalNodeBase } from "../temporal.js";
|
|
6
6
|
import { prepareArpeggioShape } from "./shape.js";
|
|
@@ -22,7 +22,7 @@ class ArpeggioFunction extends ASTFunctionNode {
|
|
|
22
22
|
~~~jpfun
|
|
23
23
|
@arp({1 ^ 3 ^ 5}, direction=up)
|
|
24
24
|
~~~
|
|
25
|
-
|
|
25
|
+
给和弦绘制琶音记号,播放时按方向错开成员起点并保持共同终点。复合成员的内部音符按比例压缩到剩余时值,装饰音在确定后的区间内展开。`,
|
|
26
26
|
allowExtraArgs: false,
|
|
27
27
|
args: [
|
|
28
28
|
{
|
|
@@ -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,7 +107,12 @@ 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
118
|
if (this.addon) {
|
|
@@ -116,69 +120,80 @@ class ArpeggioTemporal extends TemporalNodeBase {
|
|
|
116
120
|
this.addon = void 0;
|
|
117
121
|
}
|
|
118
122
|
prepareLayoutHost(this.host, context);
|
|
123
|
+
this.springConfig = { ...this.host.springConfig, ...this.springConfig };
|
|
119
124
|
const em = this.ast.size;
|
|
120
125
|
const gap = em * 0.16;
|
|
121
|
-
const
|
|
126
|
+
const contentBounds = getLayoutBounds(this.host, true);
|
|
127
|
+
const bounds = getLayoutBounds(this.host);
|
|
128
|
+
const { commands, bounds: markBounds } = prepareArpeggioShape(contentBounds.h, em, this.ast.direction);
|
|
122
129
|
this.markCommands = commands;
|
|
123
130
|
this.markX = -markBounds.x;
|
|
124
131
|
this.hostX = markBounds.w + gap;
|
|
125
132
|
this.box.w = this.hostX + this.host.box.w;
|
|
126
|
-
this.box.h =
|
|
133
|
+
this.box.h = bounds.h;
|
|
127
134
|
this.box.anchor = this.hostX + this.host.box.anchor;
|
|
128
|
-
this.box.visualAxis = this.host.box.visualAxis;
|
|
135
|
+
this.box.visualAxis = this.host.box.visualAxis - bounds.y;
|
|
129
136
|
for (const name in this.host.ports) {
|
|
130
137
|
const port = this.host.ports[name];
|
|
131
|
-
this.ports[name] = { x: this.hostX + port.x, y: port.y };
|
|
138
|
+
this.ports[name] = { x: this.hostX + port.x, y: port.y - bounds.y };
|
|
132
139
|
}
|
|
133
140
|
}
|
|
134
141
|
onPlaced() {
|
|
135
142
|
this.host.box.x = this.box.x + this.hostX;
|
|
136
|
-
this.host.box.y = this.box.y;
|
|
143
|
+
this.host.box.y = this.box.y - (getLayoutBounds(this.host).y - this.host.box.y);
|
|
137
144
|
this.host.onPlaced?.();
|
|
138
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* 先发布宿主,再将各成员的内部区间缩放到延迟后的剩余窗口
|
|
148
|
+
* 这里只调整结构区间,系统控制事件仍保留原来的声明时刻
|
|
149
|
+
*/
|
|
139
150
|
emitPlayback(emitter) {
|
|
140
151
|
emitter.play(this.host);
|
|
141
152
|
const total = emitter.end.clone().sub(emitter.start);
|
|
142
153
|
const step = new Fraction(1, 8);
|
|
154
|
+
// 短音段限制总延迟,让最后一个成员仍保留至少一半的宿主窗口。
|
|
143
155
|
const maxStep = total.clone().div(2).div(this.playbackMembers.length - 1);
|
|
144
156
|
if (step.compare(maxStep) > 0)
|
|
145
157
|
step.copyFrom(maxStep);
|
|
158
|
+
// 槽位取自成员顺序;无声成员也占槽,不能按实际发声音符重新编号。
|
|
146
159
|
const slots = new Map(this.playbackMembers.map((member, index) => [member, index]));
|
|
147
160
|
const owner = this;
|
|
148
161
|
emitter.defer(context => {
|
|
149
162
|
let rootOrigin;
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
163
|
+
// 反向锁定最近一次宿主访问的来源身份,避免修改前一遍反复留下的区间。
|
|
164
|
+
for (let index = context.spans.length - 1; index >= 0; index--) {
|
|
165
|
+
const note = context.spans[index];
|
|
166
|
+
if (!note.origins.some(origin => origin.node === owner))
|
|
153
167
|
continue;
|
|
154
|
-
rootOrigin =
|
|
168
|
+
rootOrigin = note.origins[0];
|
|
155
169
|
break;
|
|
156
170
|
}
|
|
157
171
|
if (!rootOrigin)
|
|
158
172
|
return;
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
173
|
+
// 先限制到同一次顶层访问,再由成员来源筛出本宿主的全部有声、无声子区间。
|
|
174
|
+
for (const note of context.spans) {
|
|
175
|
+
if (note.origins[0] !== rootOrigin)
|
|
162
176
|
continue;
|
|
163
|
-
const member =
|
|
177
|
+
const member = note.origins.find(origin => slots.has(origin.node))?.node;
|
|
164
178
|
const slot = member ? slots.get(member) : undefined;
|
|
165
179
|
if (!slot)
|
|
166
180
|
continue;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
181
|
+
// 起止一起缩放再平移,保留宿主终点及成员内部节奏,避免只钳制尾部造成负时值。
|
|
182
|
+
const delay = step.clone().mul(slot);
|
|
183
|
+
const scale = total.clone().sub(delay).div(total);
|
|
184
|
+
note.start.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
|
|
185
|
+
note.end.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
|
|
171
186
|
}
|
|
172
187
|
});
|
|
173
188
|
}
|
|
174
189
|
paint(painter) {
|
|
175
190
|
const originX = this.box.x;
|
|
176
|
-
const originY = this.
|
|
191
|
+
const originY = getLayoutBounds(this.host, true).y;
|
|
177
192
|
painter.drawPath(this.markCommands, { fill: "#000" }, {
|
|
178
193
|
x: originX + this.markX, y: originY, scaleX: 1, scaleY: 1,
|
|
179
194
|
});
|
|
180
195
|
this.host.paint(painter);
|
|
181
196
|
for (const decoration of this.host.decorations)
|
|
182
|
-
decoration.paint(painter);
|
|
197
|
+
decoration.paint?.(painter);
|
|
183
198
|
}
|
|
184
199
|
}
|