jpfun 0.4.0 → 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 (83) 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 +4 -4
  14. package/dist/functions/ASTtypes.js +5 -9
  15. package/dist/functions/adjust/index.js +16 -53
  16. package/dist/functions/arpeggio/index.js +27 -15
  17. package/dist/functions/bar/index.js +7 -8
  18. package/dist/functions/beam/index.js +6 -1
  19. package/dist/functions/beam/layout.d.ts +1 -1
  20. package/dist/functions/beam/layout.js +5 -3
  21. package/dist/functions/box/index.js +88 -82
  22. package/dist/functions/br/index.js +9 -5
  23. package/dist/functions/dash/index.js +24 -13
  24. package/dist/functions/div/index.d.ts +5 -2
  25. package/dist/functions/div/index.js +15 -14
  26. package/dist/functions/dot/index.js +13 -11
  27. package/dist/functions/dyn/index.js +35 -39
  28. package/dist/functions/grace/index.d.ts +19 -6
  29. package/dist/functions/grace/index.js +136 -121
  30. package/dist/functions/head/index.js +19 -14
  31. package/dist/functions/key/index.js +8 -4
  32. package/dist/functions/meter/index.js +22 -2
  33. package/dist/functions/note/index.d.ts +6 -1
  34. package/dist/functions/note/index.js +48 -30
  35. package/dist/functions/note/source-transform.d.ts +21 -0
  36. package/dist/functions/note/source-transform.js +164 -0
  37. package/dist/functions/page/index.d.ts +8 -1
  38. package/dist/functions/page/index.js +23 -13
  39. package/dist/functions/program/index.js +11 -2
  40. package/dist/functions/set/index.js +14 -8
  41. package/dist/functions/stack/index.js +7 -5
  42. package/dist/functions/symbol/index.d.ts +2 -0
  43. package/dist/functions/symbol/index.js +11 -7
  44. package/dist/functions/symbol/ornament.d.ts +3 -3
  45. package/dist/functions/symbol/ornament.js +21 -35
  46. package/dist/functions/symbol/symbols/accent.js +5 -5
  47. package/dist/functions/symbol/symbols/fermata.js +5 -4
  48. package/dist/functions/tempo/index.js +7 -1
  49. package/dist/functions/text/index.js +11 -3
  50. package/dist/functions/tie/index.js +28 -32
  51. package/dist/functions/tuplet/index.js +26 -24
  52. package/dist/functions/up/index.js +24 -21
  53. package/dist/functions/voice/index.js +36 -26
  54. package/dist/functions/volta/index.js +16 -17
  55. package/dist/jpfun.core.min.js +98 -129
  56. package/dist/jpfun.from-midi.min.js +5 -5
  57. package/dist/jpfun.from-musicxml.min.js +8 -8
  58. package/dist/jpfun.min.js +107 -138
  59. package/dist/layout/engine.d.ts +11 -1
  60. package/dist/layout/engine.js +299 -96
  61. package/dist/layout/page.js +2 -0
  62. package/dist/layout/range.d.ts +26 -0
  63. package/dist/layout/range.js +75 -0
  64. package/dist/layout/types.d.ts +33 -4
  65. package/dist/lowering/types.d.ts +5 -0
  66. package/dist/parser/parse-utils/length-utils.js +2 -0
  67. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  68. package/dist/parser/parse-utils/note-utils.js +6 -2
  69. package/dist/parser/parserContext.js +1 -1
  70. package/dist/playback/compile.d.ts +2 -9
  71. package/dist/playback/compile.js +297 -186
  72. package/dist/playback/event.d.ts +8 -16
  73. package/dist/playback/event.js +13 -3
  74. package/dist/playback/types.d.ts +76 -18
  75. package/dist/playback/types.js +1 -0
  76. package/dist/render/index.d.ts +1 -1
  77. package/dist/render/index.js +1 -1
  78. package/dist/render/recording.d.ts +1 -0
  79. package/dist/render/recording.js +21 -0
  80. package/dist/render/translate.d.ts +14 -0
  81. package/dist/render/translate.js +48 -0
  82. package/grammar.md +103 -78
  83. 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) {
@@ -64,6 +64,7 @@ export declare class ASTBraceNode extends ASTNodeBase {
64
64
  }
65
65
  export interface FunctionArgDef {
66
66
  name?: string;
67
+ description?: string;
67
68
  type: paramType;
68
69
  /** 参数默认值 null表示必填 否则报错 */
69
70
  default: paramValue | null;
@@ -71,7 +72,7 @@ export interface FunctionArgDef {
71
72
  export interface FunctionDef {
72
73
  name: string | string[];
73
74
  description: string;
74
- example: string;
75
+ details: string;
75
76
  allowExtraArgs: boolean;
76
77
  extraArgType?: paramType;
77
78
  args: FunctionArgDef[];
@@ -127,9 +128,8 @@ export declare class ASTFunctionNode extends ASTNodeBase {
127
128
  static findLabelable(node: ASTNodeBase): ASTFunctionNode | null;
128
129
  /**
129
130
  * 用于语法糖向前结合
130
- * 一般向前结合都是寻找brace或者function
131
- * 文本节点会打断连接 注意解析时已经跳过了空格 因此允许语法糖之间有空格
131
+ * 从指定位置向前寻找最后一个可由后缀语法糖包装的节点,跳过不产生语义对象的文本
132
132
  */
133
- static findLastFuncContentNode(nodes: ASTNodeBase[], i: number): ASTNodeBase | null;
133
+ static findLastFuncContentIndex(nodes: ASTNodeBase[], i: number): number;
134
134
  }
135
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,37 +2,45 @@ 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"],
8
- description: "微调目标的位置与占位",
9
- example: `@adjust(content, dx=0px, dy=0px, dw=0px, dh=0px)
10
- dw/dh 在排版前增减占位,最终尺寸不小于 0;dw 按原左右占位比例分配,dh 调整下方;
11
- dx/dy 在排版完成后平移目标,邻居不会让开,因此可以故意重叠。
12
- 括住的若是关系对象(连音线等)且其中没有对象,平移的就是这条关系对象`,
9
+ description: "微调目标的绘制位置与排版占位",
10
+ details: `\
11
+ ~~~jpfun
12
+ @adjust(1, dx=2px, dy=-1px, dw=0.2em, dh=0px)
13
+ ~~~
14
+
15
+ 位置偏移在排版后应用,相邻对象保持原位;占位调整在排版前应用。只包裹关系对象时,平移作用于该关系对象本身。`,
13
16
  allowExtraArgs: false,
14
17
  args: [
15
18
  {
16
19
  type: "content",
20
+ description: "要微调的内容或关系对象",
17
21
  default: null,
18
22
  },
19
23
  {
20
24
  name: "dx",
25
+ description: "排版后的水平偏移,正值向右,不改变占位",
21
26
  type: "length",
22
27
  default: { value: 0, unit: "px" },
23
28
  },
24
29
  {
25
30
  name: "dy",
31
+ description: "排版后的垂直偏移,正值向下,不改变占位",
26
32
  type: "length",
27
33
  default: { value: 0, unit: "px" },
28
34
  },
29
35
  {
30
36
  name: "dw",
37
+ description: "增减水平占位,按原左右比例分配,最终宽度不小于 0",
31
38
  type: "length",
32
39
  default: { value: 0, unit: "px" },
33
40
  },
34
41
  {
35
42
  name: "dh",
43
+ description: "增减下方占位,最终高度不小于 0",
36
44
  type: "length",
37
45
  default: { value: 0, unit: "px" },
38
46
  },
@@ -88,6 +96,9 @@ class AdjustFunction extends ASTFunctionNode {
88
96
  this.dw = ctx.length2px(dw);
89
97
  this.dh = ctx.length2px(dh);
90
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
+ }
91
102
  }
92
103
  export const AdjustNode = AdjustFunction;
93
104
  /** 在原对象上组合布局生命周期,保持布局索引依赖的对象身份 */
@@ -143,51 +154,3 @@ function offsetAttachment(attachment, targets, dx, dy) {
143
154
  };
144
155
  };
145
156
  }
146
- /** Painter 没有变换栈,被微调的关系对象只能逐条命令平移 */
147
- class TranslatingPainter {
148
- target;
149
- dx;
150
- dy;
151
- constructor(target, dx, dy) {
152
- this.target = target;
153
- this.dx = dx;
154
- this.dy = dy;
155
- }
156
- drawText(text, x, y, style) {
157
- this.target.drawText(text, x + this.dx, y + this.dy, style);
158
- }
159
- drawLine(x1, y1, x2, y2, style) {
160
- this.target.drawLine(x1 + this.dx, y1 + this.dy, x2 + this.dx, y2 + this.dy, style);
161
- }
162
- drawRect(x, y, w, h, style) {
163
- this.target.drawRect(x + this.dx, y + this.dy, w, h, style);
164
- }
165
- drawCircle(cx, cy, r, style) {
166
- this.target.drawCircle(cx + this.dx, cy + this.dy, r, style);
167
- }
168
- drawPath(commands, style, transform) {
169
- if (transform) {
170
- const moved = { ...transform, x: transform.x + this.dx, y: transform.y + this.dy };
171
- this.target.drawPath(commands, style, moved);
172
- return;
173
- }
174
- this.target.drawPath(commands.map(command => this.translateCommand(command)), style);
175
- }
176
- translateCommand(command) {
177
- switch (command.op) {
178
- case "Z": return command;
179
- case "Q": return {
180
- op: "Q",
181
- cx: command.cx + this.dx, cy: command.cy + this.dy,
182
- x: command.x + this.dx, y: command.y + this.dy,
183
- };
184
- case "C": return {
185
- op: "C",
186
- cx1: command.cx1 + this.dx, cy1: command.cy1 + this.dy,
187
- cx2: command.cx2 + this.dx, cy2: command.cy2 + this.dy,
188
- x: command.x + this.dx, y: command.y + this.dy,
189
- };
190
- default: return { op: command.op, x: command.x + this.dx, y: command.y + this.dy };
191
- }
192
- }
193
- }
@@ -18,18 +18,22 @@ class ArpeggioFunction extends ASTFunctionNode {
18
18
  static def = {
19
19
  name: ["arp", "arpeggio"],
20
20
  description: "琶音",
21
- example: `@arp({1 ^ 3 ^ 5}) 默认从低到高,无箭头
22
- @arp({1 ^ 3 ^ 5}, direction=up) 从低到高,顶端带箭头
23
- @arp({1 ^ 3 ^ 5}, direction=down) 从高到低,底端带箭头`,
21
+ details: `\
22
+ ~~~jpfun
23
+ @arp({1 ^ 3 ^ 5}, direction=up)
24
+ ~~~
25
+ 给和弦绘制琶音记号,播放时按方向错开成员起点并保持共同终点。复合成员的内部音符按比例压缩到剩余时值,装饰音在确定后的区间内展开。`,
24
26
  allowExtraArgs: false,
25
27
  args: [
26
28
  {
27
29
  name: "content",
30
+ description: "由 `up` / `down` 堆叠形成的和弦",
28
31
  type: "content",
29
32
  default: null
30
33
  },
31
34
  {
32
35
  name: "direction",
36
+ description: "空值从低到高、不画箭头;`up` 从低到高、顶端带箭头;`down` 从高到低、底端带箭头",
33
37
  type: "string",
34
38
  default: ""
35
39
  },
@@ -132,38 +136,46 @@ class ArpeggioTemporal extends TemporalNodeBase {
132
136
  this.host.box.y = this.box.y;
133
137
  this.host.onPlaced?.();
134
138
  }
139
+ /**
140
+ * 先发布宿主,再将各成员的内部区间缩放到延迟后的剩余窗口
141
+ * 这里只调整结构区间,系统控制事件仍保留原来的声明时刻
142
+ */
135
143
  emitPlayback(emitter) {
136
144
  emitter.play(this.host);
137
145
  const total = emitter.end.clone().sub(emitter.start);
138
146
  const step = new Fraction(1, 8);
147
+ // 短音段限制总延迟,让最后一个成员仍保留至少一半的宿主窗口。
139
148
  const maxStep = total.clone().div(2).div(this.playbackMembers.length - 1);
140
149
  if (step.compare(maxStep) > 0)
141
150
  step.copyFrom(maxStep);
151
+ // 槽位取自成员顺序;无声成员也占槽,不能按实际发声音符重新编号。
142
152
  const slots = new Map(this.playbackMembers.map((member, index) => [member, index]));
143
153
  const owner = this;
144
154
  emitter.defer(context => {
145
155
  let rootOrigin;
146
- for (let index = context.events.length - 1; index >= 0; index--) {
147
- const event = context.events[index];
148
- 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))
149
160
  continue;
150
- rootOrigin = event.origins[0];
161
+ rootOrigin = note.origins[0];
151
162
  break;
152
163
  }
153
164
  if (!rootOrigin)
154
165
  return;
155
- for (const event of context.events) {
156
- if ((event.kind !== "note-on" && event.kind !== "note-off")
157
- || event.origins[0] !== rootOrigin)
166
+ // 先限制到同一次顶层访问,再由成员来源筛出本宿主的全部有声、无声子区间。
167
+ for (const note of context.spans) {
168
+ if (note.origins[0] !== rootOrigin)
158
169
  continue;
159
- const member = event.origins.find(origin => slots.has(origin.node))?.node;
170
+ const member = note.origins.find(origin => slots.has(origin.node))?.node;
160
171
  const slot = member ? slots.get(member) : undefined;
161
172
  if (!slot)
162
173
  continue;
163
- event.at.add(step.clone().mul(slot));
164
- if (event.kind === "note-off" && event.at.compare(emitter.end) > 0) {
165
- event.at.copyFrom(emitter.end);
166
- }
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);
167
179
  }
168
180
  });
169
181
  }
@@ -4,23 +4,22 @@ class BarFunction extends ASTFunctionNode {
4
4
  static def = {
5
5
  name: ["bar", "|"],
6
6
  description: "小节线",
7
- example: `@bar(type, lengthEM) 创建一个小节线
8
- 语法糖:
9
- - type0: '|' 普通小节线
10
- - type1: '||' 终止小节线 左细右粗
11
- - type2: '|:' 重复小节线 左粗右细
12
- - type3: ':|'
13
- - type4: ':|:' 左右反复
14
- `,
7
+ details: `\
8
+ ~~~jpfun
9
+ @bar(0, length=1.25em)
10
+ ~~~
11
+ 有语法糖简写,见 type 参数的说明。`,
15
12
  allowExtraArgs: false,
16
13
  args: [
17
14
  {
18
15
  name: "type",
16
+ description: "0:普通线 `|`;1:终止线 `||`(左细右粗);2:反复起点 `|:`;3:反复终点 `:|`;4:左右反复 `:|:`",
19
17
  type: "number",
20
18
  default: 0,
21
19
  },
22
20
  {
23
21
  name: "length",
22
+ description: "小节线高度",
24
23
  type: "length",
25
24
  default: {
26
25
  value: 1.25,
@@ -8,7 +8,12 @@ class BeamFunction extends ASTFunctionNode {
8
8
  static def = {
9
9
  name: ["beam"],
10
10
  description: "减时线连接",
11
- example: "@beam(label1, label2, ...): 将多个已标记音符按顺序连接为减时线组",
11
+ details: `\
12
+ 事后将多个减时线**强行**相连
13
+ ~~~jpfun
14
+ 1/@a 2/@b | 3/@c @beam(a, b, c)
15
+ ~~~
16
+ **位置参数**:已标记音符的标签,按传入顺序连接减时线,显式指定一组。先用 \`/\` 或 \`@div\` 为目标添加减时线,再用 \`@beam\` 控制连接。端点须同轨、同谱面行且相邻,可跨小节线;连线直接穿过小节线,保留原有间距。`,
12
17
  allowExtraArgs: true,
13
18
  extraArgType: "label",
14
19
  args: [],
@@ -4,7 +4,7 @@ import type { LayoutAttachment } from "../../layout/types.js";
4
4
  import type { LoweringAttachment, LoweringResult } from "../../lowering/types.js";
5
5
  /** 显式和自动分组共享同一个几何对象 */
6
6
  export declare function createBeamLayoutAttachment(endPoints: TemporalNodeBase[], explicit: boolean, sourceSpan: SourceSpan): LayoutAttachment;
7
- /** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
7
+ /** 显式 beam 连接同轨同谱面行中的相邻可见主体,忽略零时长小节线 */
8
8
  export declare function validateExplicitBeamAttachments(result: LoweringResult): void;
9
9
  /** 自动分组在 loweringAugment 当下读取已经注册的显式 beam 端点 */
10
10
  export declare function collectExplicitBeamEndpoints(attachments: readonly LoweringAttachment[]): Set<TemporalNodeBase>;
@@ -1,17 +1,19 @@
1
- import { isVisualTemporalNode, } from "../temporal.js";
1
+ import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { claimDivLine, divLinePortName, divLineRuns, divLineWidth, } from "../div/index.js";
4
4
  /** 显式和自动分组共享同一个几何对象 */
5
5
  export function createBeamLayoutAttachment(endPoints, explicit, sourceSpan) {
6
6
  return new BeamLayoutAttachment(endPoints, explicit, sourceSpan);
7
7
  }
8
- /** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
8
+ /** 显式 beam 连接同轨同谱面行中的相邻可见主体,忽略零时长小节线 */
9
9
  export function validateExplicitBeamAttachments(result) {
10
10
  const tracks = new Map();
11
11
  for (const column of result.columns) {
12
12
  for (const node of column) {
13
13
  if (!isVisualTemporalNode(node))
14
14
  continue;
15
+ if (node.mergeKey === ANCHOR_KEY && node.T.isZero())
16
+ continue;
15
17
  let lineTracks = tracks.get(node.layoutLine);
16
18
  if (!lineTracks) {
17
19
  lineTracks = new Map();
@@ -50,7 +52,7 @@ export function validateExplicitBeamAttachments(result) {
50
52
  && secondPosition.index === firstPosition.index + 1;
51
53
  if (adjacent)
52
54
  continue;
53
- throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素", attachment.sourceSpan);
55
+ throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素(可跨小节线)", attachment.sourceSpan);
54
56
  }
55
57
  }
56
58
  }