jpfun 0.4.1 → 0.5.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.
Files changed (73) hide show
  1. package/README.md +53 -113
  2. package/dist/converter/midi/convert.js +14 -7
  3. package/dist/converter/musicxml/features.d.ts +57 -16
  4. package/dist/converter/musicxml/features.js +204 -42
  5. package/dist/converter/musicxml/index.d.ts +7 -2
  6. package/dist/converter/musicxml/index.js +256 -268
  7. package/dist/converter/musicxml/model.d.ts +36 -7
  8. package/dist/converter/musicxml/resolve.d.ts +10 -0
  9. package/dist/converter/musicxml/resolve.js +152 -0
  10. package/dist/core.d.ts +1 -0
  11. package/dist/core.js +1 -0
  12. package/dist/fraction.js +7 -1
  13. package/dist/functions/ASTtypes.d.ts +2 -3
  14. package/dist/functions/ASTtypes.js +5 -9
  15. package/dist/functions/adjust/index.js +4 -48
  16. package/dist/functions/arpeggio/index.js +21 -13
  17. package/dist/functions/box/index.js +81 -82
  18. package/dist/functions/dash/index.js +20 -11
  19. package/dist/functions/div/index.d.ts +1 -1
  20. package/dist/functions/div/index.js +7 -7
  21. package/dist/functions/dot/index.js +4 -5
  22. package/dist/functions/dyn/index.js +28 -32
  23. package/dist/functions/grace/index.d.ts +19 -6
  24. package/dist/functions/grace/index.js +123 -115
  25. package/dist/functions/key/index.js +1 -1
  26. package/dist/functions/meter/index.js +14 -1
  27. package/dist/functions/note/index.d.ts +6 -1
  28. package/dist/functions/note/index.js +37 -14
  29. package/dist/functions/note/source-transform.d.ts +21 -0
  30. package/dist/functions/note/source-transform.js +164 -0
  31. package/dist/functions/page/index.d.ts +5 -0
  32. package/dist/functions/page/index.js +8 -5
  33. package/dist/functions/set/index.js +5 -1
  34. package/dist/functions/symbol/index.d.ts +2 -0
  35. package/dist/functions/symbol/index.js +1 -1
  36. package/dist/functions/symbol/ornament.d.ts +3 -3
  37. package/dist/functions/symbol/ornament.js +21 -35
  38. package/dist/functions/symbol/symbols/accent.js +5 -5
  39. package/dist/functions/symbol/symbols/fermata.js +5 -4
  40. package/dist/functions/tie/index.js +22 -32
  41. package/dist/functions/tuplet/index.js +19 -23
  42. package/dist/functions/up/index.js +9 -9
  43. package/dist/functions/voice/index.js +4 -3
  44. package/dist/functions/volta/index.js +7 -12
  45. package/dist/jpfun.core.min.js +40 -37
  46. package/dist/jpfun.from-midi.min.js +5 -5
  47. package/dist/jpfun.from-musicxml.min.js +8 -8
  48. package/dist/jpfun.min.js +49 -46
  49. package/dist/layout/engine.d.ts +11 -1
  50. package/dist/layout/engine.js +299 -96
  51. package/dist/layout/page.js +2 -0
  52. package/dist/layout/range.d.ts +26 -0
  53. package/dist/layout/range.js +75 -0
  54. package/dist/layout/types.d.ts +33 -4
  55. package/dist/lowering/types.d.ts +5 -0
  56. package/dist/parser/parse-utils/length-utils.js +2 -0
  57. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  58. package/dist/parser/parse-utils/note-utils.js +6 -2
  59. package/dist/parser/parserContext.js +1 -1
  60. package/dist/playback/compile.d.ts +2 -9
  61. package/dist/playback/compile.js +297 -186
  62. package/dist/playback/event.d.ts +8 -16
  63. package/dist/playback/event.js +13 -3
  64. package/dist/playback/types.d.ts +76 -18
  65. package/dist/playback/types.js +1 -0
  66. package/dist/render/index.d.ts +1 -1
  67. package/dist/render/index.js +1 -1
  68. package/dist/render/recording.d.ts +1 -0
  69. package/dist/render/recording.js +21 -0
  70. package/dist/render/translate.d.ts +14 -0
  71. package/dist/render/translate.js +48 -0
  72. package/grammar.md +13 -6
  73. package/package.json +4 -4
@@ -1,7 +1,31 @@
1
1
  import type { Fraction } from "../../fraction.js";
2
- export type MusicXmlArpeggio = "none" | "up" | "down";
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 *= 2 ** power;
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) {
@@ -128,9 +128,8 @@ export declare class ASTFunctionNode extends ASTNodeBase {
128
128
  static findLabelable(node: ASTNodeBase): ASTFunctionNode | null;
129
129
  /**
130
130
  * 用于语法糖向前结合
131
- * 一般向前结合都是寻找brace或者function
132
- * 文本节点会打断连接 注意解析时已经跳过了空格 因此允许语法糖之间有空格
131
+ * 从指定位置向前寻找最后一个可由后缀语法糖包装的节点,跳过不产生语义对象的文本
133
132
  */
134
- static findLastFuncContentNode(nodes: ASTNodeBase[], i: number): ASTNodeBase | null;
133
+ static findLastFuncContentIndex(nodes: ASTNodeBase[], i: number): number;
135
134
  }
136
135
  export type ASTFunctionClass = (new (sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent: ASTNodeBase | null) => ASTFunctionNode) & Pick<typeof ASTFunctionNode, "prototype" | "def" | "deSugarAtom" | "deSugarRelation" | "loweringAugment" | "loweringFinalize" | "layoutDecorationHandler">;
@@ -192,17 +192,13 @@ export class ASTFunctionNode extends ASTNodeBase {
192
192
  }
193
193
  /**
194
194
  * 用于语法糖向前结合
195
- * 一般向前结合都是寻找brace或者function
196
- * 文本节点会打断连接 注意解析时已经跳过了空格 因此允许语法糖之间有空格
195
+ * 从指定位置向前寻找最后一个可由后缀语法糖包装的节点,跳过不产生语义对象的文本
197
196
  */
198
- static findLastFuncContentNode(nodes, i) {
197
+ static findLastFuncContentIndex(nodes, i) {
199
198
  for (; i >= 0; i--) {
200
- const n = nodes[i];
201
- if (n instanceof ASTTextNode)
202
- return null;
203
- if (n instanceof ASTFunctionNode || n instanceof ASTBraceNode)
204
- return n;
199
+ if (nodes[i] instanceof ASTFunctionNode || nodes[i] instanceof ASTBraceNode)
200
+ return i;
205
201
  }
206
- return null;
202
+ return -1;
207
203
  }
208
204
  }
@@ -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
  /** 在原对象上组合布局生命周期,保持布局索引依赖的对象身份 */
@@ -150,51 +154,3 @@ function offsetAttachment(attachment, targets, dx, dy) {
150
154
  };
151
155
  };
152
156
  }
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
- }
@@ -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
  {
@@ -136,38 +136,46 @@ class ArpeggioTemporal extends TemporalNodeBase {
136
136
  this.host.box.y = this.box.y;
137
137
  this.host.onPlaced?.();
138
138
  }
139
+ /**
140
+ * 先发布宿主,再将各成员的内部区间缩放到延迟后的剩余窗口
141
+ * 这里只调整结构区间,系统控制事件仍保留原来的声明时刻
142
+ */
139
143
  emitPlayback(emitter) {
140
144
  emitter.play(this.host);
141
145
  const total = emitter.end.clone().sub(emitter.start);
142
146
  const step = new Fraction(1, 8);
147
+ // 短音段限制总延迟,让最后一个成员仍保留至少一半的宿主窗口。
143
148
  const maxStep = total.clone().div(2).div(this.playbackMembers.length - 1);
144
149
  if (step.compare(maxStep) > 0)
145
150
  step.copyFrom(maxStep);
151
+ // 槽位取自成员顺序;无声成员也占槽,不能按实际发声音符重新编号。
146
152
  const slots = new Map(this.playbackMembers.map((member, index) => [member, index]));
147
153
  const owner = this;
148
154
  emitter.defer(context => {
149
155
  let rootOrigin;
150
- for (let index = context.events.length - 1; index >= 0; index--) {
151
- const event = context.events[index];
152
- if (!event.origins.some(origin => origin.node === owner))
156
+ // 反向锁定最近一次宿主访问的来源身份,避免修改前一遍反复留下的区间。
157
+ for (let index = context.spans.length - 1; index >= 0; index--) {
158
+ const note = context.spans[index];
159
+ if (!note.origins.some(origin => origin.node === owner))
153
160
  continue;
154
- rootOrigin = event.origins[0];
161
+ rootOrigin = note.origins[0];
155
162
  break;
156
163
  }
157
164
  if (!rootOrigin)
158
165
  return;
159
- for (const event of context.events) {
160
- if ((event.kind !== "note-on" && event.kind !== "note-off")
161
- || event.origins[0] !== rootOrigin)
166
+ // 先限制到同一次顶层访问,再由成员来源筛出本宿主的全部有声、无声子区间。
167
+ for (const note of context.spans) {
168
+ if (note.origins[0] !== rootOrigin)
162
169
  continue;
163
- const member = event.origins.find(origin => slots.has(origin.node))?.node;
170
+ const member = note.origins.find(origin => slots.has(origin.node))?.node;
164
171
  const slot = member ? slots.get(member) : undefined;
165
172
  if (!slot)
166
173
  continue;
167
- event.at.add(step.clone().mul(slot));
168
- if (event.kind === "note-off" && event.at.compare(emitter.end) > 0) {
169
- event.at.copyFrom(emitter.end);
170
- }
174
+ // 起止一起缩放再平移,保留宿主终点及成员内部节奏,避免只钳制尾部造成负时值。
175
+ const delay = step.clone().mul(slot);
176
+ const scale = total.clone().sub(delay).div(total);
177
+ note.start.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
178
+ note.end.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
171
179
  }
172
180
  });
173
181
  }