jpfun 0.2.0 → 0.4.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 (167) hide show
  1. package/README.md +141 -11
  2. package/dist/converter/midi/convert.d.ts +50 -0
  3. package/dist/converter/midi/convert.js +700 -0
  4. package/dist/converter/midi/index.d.ts +5 -0
  5. package/dist/converter/midi/index.js +11 -0
  6. package/dist/converter/midi/json.d.ts +38 -0
  7. package/dist/converter/midi/json.js +1 -0
  8. package/dist/converter/midi/triplet.d.ts +30 -0
  9. package/dist/converter/midi/triplet.js +75 -0
  10. package/dist/converter/musicxml/dom.d.ts +25 -0
  11. package/dist/converter/musicxml/dom.js +65 -0
  12. package/dist/converter/musicxml/features.d.ts +51 -0
  13. package/dist/converter/musicxml/features.js +232 -0
  14. package/dist/converter/musicxml/index.d.ts +9 -0
  15. package/dist/converter/musicxml/index.js +1140 -0
  16. package/dist/converter/musicxml/model.d.ts +123 -0
  17. package/dist/converter/musicxml/model.js +1 -0
  18. package/dist/converter/source.d.ts +22 -0
  19. package/dist/converter/source.js +51 -0
  20. package/dist/core.d.ts +22 -0
  21. package/dist/core.js +22 -0
  22. package/dist/diagnostic.js +5 -2
  23. package/dist/fraction.d.ts +24 -0
  24. package/dist/fraction.js +132 -0
  25. package/dist/functions/ASTtypes.d.ts +51 -31
  26. package/dist/functions/ASTtypes.js +59 -38
  27. package/dist/functions/adjust/index.d.ts +2 -0
  28. package/dist/functions/adjust/index.js +193 -0
  29. package/dist/functions/arpeggio/index.d.ts +2 -0
  30. package/dist/functions/arpeggio/index.js +180 -0
  31. package/dist/functions/arpeggio/shape.d.ts +7 -0
  32. package/dist/functions/arpeggio/shape.js +122 -0
  33. package/dist/functions/bar/index.d.ts +8 -0
  34. package/dist/functions/bar/index.js +62 -25
  35. package/dist/functions/beam/auto.d.ts +1 -1
  36. package/dist/functions/beam/auto.js +33 -16
  37. package/dist/functions/beam/index.js +10 -6
  38. package/dist/functions/beam/layout.d.ts +4 -3
  39. package/dist/functions/beam/layout.js +43 -65
  40. package/dist/functions/box/index.js +37 -36
  41. package/dist/functions/br/index.js +10 -8
  42. package/dist/functions/dash/index.js +40 -7
  43. package/dist/functions/default.js +19 -8
  44. package/dist/functions/div/index.d.ts +16 -0
  45. package/dist/functions/div/index.js +45 -17
  46. package/dist/functions/dot/index.js +23 -25
  47. package/dist/functions/dyn/index.d.ts +2 -0
  48. package/dist/functions/dyn/index.js +242 -0
  49. package/dist/functions/grace/index.d.ts +8 -11
  50. package/dist/functions/grace/index.js +84 -79
  51. package/dist/functions/head/index.d.ts +2 -0
  52. package/dist/functions/head/index.js +496 -0
  53. package/dist/functions/key/index.js +98 -3
  54. package/dist/functions/meter/index.d.ts +2 -0
  55. package/dist/functions/meter/index.js +155 -0
  56. package/dist/functions/note/accidentals.d.ts +24 -3
  57. package/dist/functions/note/accidentals.js +31 -1
  58. package/dist/functions/note/index.js +106 -79
  59. package/dist/functions/note/noteNameFSM.js +52 -63
  60. package/dist/functions/page/index.d.ts +11 -2
  61. package/dist/functions/page/index.js +65 -4
  62. package/dist/functions/program/index.d.ts +2 -0
  63. package/dist/functions/program/index.js +30 -0
  64. package/dist/functions/set/index.js +43 -32
  65. package/dist/functions/stack/index.js +26 -22
  66. package/dist/functions/symbol/index.d.ts +31 -0
  67. package/dist/functions/symbol/index.js +139 -0
  68. package/dist/functions/symbol/ornament.d.ts +15 -0
  69. package/dist/functions/symbol/ornament.js +73 -0
  70. package/dist/functions/symbol/shape.d.ts +24 -0
  71. package/dist/functions/symbol/shape.js +52 -0
  72. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  73. package/dist/functions/symbol/symbols/accent.js +24 -0
  74. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  75. package/dist/functions/symbol/symbols/dc.js +82 -0
  76. package/dist/functions/symbol/symbols/ds.d.ts +2 -0
  77. package/dist/functions/symbol/symbols/ds.js +93 -0
  78. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  79. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  80. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  81. package/dist/functions/symbol/symbols/fermata.js +24 -0
  82. package/dist/functions/symbol/symbols/fine.d.ts +2 -0
  83. package/dist/functions/symbol/symbols/fine.js +12 -0
  84. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  85. package/dist/functions/symbol/symbols/mordent.js +56 -0
  86. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  87. package/dist/functions/symbol/symbols/prall.js +42 -0
  88. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  89. package/dist/functions/symbol/symbols/segno.js +82 -0
  90. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  91. package/dist/functions/symbol/symbols/tr.js +71 -0
  92. package/dist/functions/tempo/glyph.d.ts +12 -0
  93. package/dist/functions/tempo/glyph.js +30 -0
  94. package/dist/functions/tempo/index.js +57 -4
  95. package/dist/functions/temporal.d.ts +138 -0
  96. package/dist/functions/temporal.js +101 -0
  97. package/dist/functions/text/index.js +86 -27
  98. package/dist/functions/tie/index.js +136 -152
  99. package/dist/functions/tuplet/index.d.ts +2 -0
  100. package/dist/functions/tuplet/index.js +218 -0
  101. package/dist/functions/up/index.d.ts +1 -0
  102. package/dist/functions/up/index.js +258 -182
  103. package/dist/functions/voice/index.js +203 -109
  104. package/dist/functions/volta/index.d.ts +2 -0
  105. package/dist/functions/volta/index.js +230 -0
  106. package/dist/iife/core.d.ts +1 -0
  107. package/dist/iife/core.js +3 -0
  108. package/dist/iife/from-midi.d.ts +1 -0
  109. package/dist/iife/from-midi.js +15 -0
  110. package/dist/iife/from-musicxml.d.ts +1 -0
  111. package/dist/iife/from-musicxml.js +3 -0
  112. package/dist/iife/full.d.ts +1 -0
  113. package/dist/iife/full.js +3 -0
  114. package/dist/index.d.ts +3 -17
  115. package/dist/index.js +3 -17
  116. package/dist/jpfun.core.min.js +153 -0
  117. package/dist/jpfun.from-midi.min.js +9 -0
  118. package/dist/jpfun.from-musicxml.min.js +12 -0
  119. package/dist/jpfun.min.js +166 -0
  120. package/dist/layout/default.js +2 -3
  121. package/dist/layout/engine.d.ts +6 -12
  122. package/dist/layout/engine.js +115 -64
  123. package/dist/layout/model.d.ts +12 -5
  124. package/dist/layout/model.js +31 -18
  125. package/dist/layout/page.js +5 -4
  126. package/dist/layout/path.d.ts +19 -0
  127. package/dist/layout/path.js +101 -0
  128. package/dist/layout/types.d.ts +71 -20
  129. package/dist/layout/types.js +3 -1
  130. package/dist/lowering/loweringContext.d.ts +20 -20
  131. package/dist/lowering/loweringContext.js +153 -101
  132. package/dist/lowering/track.d.ts +5 -9
  133. package/dist/lowering/track.js +7 -2
  134. package/dist/lowering/types.d.ts +26 -112
  135. package/dist/lowering/types.js +1 -92
  136. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  137. package/dist/parser/parse-utils/call-utils.js +0 -6
  138. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  139. package/dist/parser/parse-utils/note-utils.js +35 -13
  140. package/dist/parser/parse-utils/string-utils.d.ts +10 -0
  141. package/dist/parser/parse-utils/string-utils.js +24 -0
  142. package/dist/parser/parserContext.d.ts +11 -9
  143. package/dist/parser/parserContext.js +65 -55
  144. package/dist/parser/preprocess.js +17 -11
  145. package/dist/pipeline.d.ts +6 -5
  146. package/dist/pipeline.js +4 -5
  147. package/dist/playback/compile.d.ts +18 -0
  148. package/dist/playback/compile.js +414 -0
  149. package/dist/playback/event.d.ts +105 -0
  150. package/dist/playback/event.js +105 -0
  151. package/dist/playback/time.d.ts +14 -0
  152. package/dist/playback/time.js +81 -0
  153. package/dist/playback/types.d.ts +129 -0
  154. package/dist/playback/types.js +3 -0
  155. package/dist/render/canvas.d.ts +4 -4
  156. package/dist/render/canvas.js +20 -5
  157. package/dist/render/index.d.ts +1 -0
  158. package/dist/render/index.js +1 -0
  159. package/dist/render/paint.d.ts +8 -0
  160. package/dist/render/paint.js +101 -0
  161. package/dist/render/svg.d.ts +3 -3
  162. package/dist/render/svg.js +10 -8
  163. package/dist/render/text.d.ts +22 -0
  164. package/dist/render/text.js +42 -0
  165. package/dist/render/types.d.ts +1 -1
  166. package/grammar.md +132 -13
  167. package/package.json +19 -6
@@ -1,8 +1,9 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { isVisualTemporalNode } from "../../lowering/types.js";
2
+ import { isVisualTemporalNode } from "../temporal.js";
3
3
  import { ErrorDiagnostic } from "../../diagnostic.js";
4
4
  import { unionLayoutBoxes } from "../../layout/engine.js";
5
5
  import { layoutHorizontalRegion } from "../../layout/model.js";
6
+ import { isLayoutAttachment } from "../../layout/types.js";
6
7
  class BoxFunction extends ASTFunctionNode {
7
8
  static def = {
8
9
  name: ["box"],
@@ -56,18 +57,18 @@ class BoxFunction extends ASTFunctionNode {
56
57
  * 进入目标内容前只开始收集所有成员矩形的引用
57
58
  */
58
59
  loweringEnter(ctx) {
59
- const members = [];
60
60
  const temporalMembers = [];
61
+ const childAttachments = [];
61
62
  ctx.beginLoweringGroup(this, {
62
- attachment: new BoxLayoutAttachment(members, temporalMembers, this),
63
+ attachment: new BoxLayoutAttachment(temporalMembers, childAttachments, this),
63
64
  onTemporal(node) {
64
65
  if (!isVisualTemporalNode(node))
65
66
  return;
66
- members.push(node.box);
67
67
  temporalMembers.push(node);
68
68
  },
69
69
  onAttachment(attachment) {
70
- members.push(attachment.box);
70
+ if (isLayoutAttachment(attachment))
71
+ childAttachments.push(attachment);
71
72
  },
72
73
  });
73
74
  return [];
@@ -89,22 +90,17 @@ class BoxFunction extends ASTFunctionNode {
89
90
  }
90
91
  export const BoxNode = BoxFunction;
91
92
  class BoxLayoutAttachment {
92
- box = {
93
- x: 0,
94
- y: 0,
95
- w: 0,
96
- h: 0,
97
- };
98
93
  layer = "background";
99
- /** 同一个数组会在 lowering 递归过程中持续加入成员盒引用 */
100
- members;
101
94
  temporalMembers;
95
+ /** 写在 box 内的关系对象;lowering 递归中持续加入,因而总在 box 自身之前完成几何 */
96
+ childAttachments;
102
97
  owner;
103
98
  fixedStart = null;
104
99
  wallOffset = 0;
105
- constructor(members, temporalMembers, owner) {
106
- this.members = members;
100
+ get sourceSpan() { return this.owner.sourceSpan; }
101
+ constructor(temporalMembers, childAttachments, owner) {
107
102
  this.temporalMembers = temporalMembers;
103
+ this.childAttachments = childAttachments;
108
104
  this.owner = owner;
109
105
  }
110
106
  /** 定宽 box 在成员覆盖的全局列上运行局部墙布局 */
@@ -158,7 +154,7 @@ class BoxLayoutAttachment {
158
154
  throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CROSSING", "定宽 @box 可以嵌套,但不能部分交叉", this.owner.sourceSpan);
159
155
  }
160
156
  const regionX = X.subarray(start, end + 1);
161
- layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), this.owner.width, options, true);
157
+ layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), this.owner.width, options, 0);
162
158
  const actualWidth = regionX[regionX.length - 1] - regionX[0] + leftInset + rightInset;
163
159
  if (Math.abs(actualWidth - this.owner.width) > 1e-6) {
164
160
  throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CONFLICT", "多个 @box 为相同内容指定了不同宽度", this.owner.sourceSpan);
@@ -171,32 +167,37 @@ class BoxLayoutAttachment {
171
167
  * box 必须等所有成员获得最终 x 和 y 后再求边界
172
168
  * stroke 以矩形边界为中心绘制,因此外接盒额外包含半个线宽
173
169
  */
174
- layout() {
170
+ createGeometry(context) {
175
171
  const { padding, stroke } = this.owner;
176
172
  const rect = { x: 0, y: 0, w: 0, h: 0 };
173
+ const members = [
174
+ ...this.temporalMembers.map(member => member.box),
175
+ ...this.childAttachments.map(attachment => context.getAttachmentBox(attachment)),
176
+ ];
177
177
  // 未命名歌词等 attachment 会保留全零盒,不能让它把边框拉到文档原点
178
- if (!unionLayoutBoxes(rect, this.members.filter(member => member.w > 0 || member.h > 0)))
179
- return [];
178
+ if (!unionLayoutBoxes(rect, members.filter(member => member.w > 0 || member.h > 0))) {
179
+ return { regions: [], paint() { } };
180
+ }
180
181
  const inset = padding + stroke / 2;
181
- // 边框不抢轨道纵向空间,只参与画布边界;this.box 由引擎写回
182
+ // 边框不抢轨道纵向空间,只参与画布边界
182
183
  const fixedX = this.fixedStart
183
184
  ? this.fixedStart.box.x + this.fixedStart.box.anchor + this.wallOffset
184
185
  : rect.x;
185
- return [{
186
- x: fixedX - inset,
187
- y: rect.y - inset,
188
- w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
189
- h: rect.h + inset * 2,
190
- }];
191
- }
192
- paint(painter) {
193
- if (this.box.w === 0 && this.box.h === 0)
194
- return;
195
- const stroke = this.owner.stroke;
196
- const inset = stroke / 2;
197
- painter.drawRect(this.box.x + inset, this.box.y + inset, Math.max(0, this.box.w - stroke), Math.max(0, this.box.h - stroke), {
198
- stroke: "#000",
199
- strokeWidth: stroke,
200
- });
186
+ const region = {
187
+ x: fixedX - inset,
188
+ y: rect.y - inset,
189
+ w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
190
+ h: rect.h + inset * 2,
191
+ };
192
+ return {
193
+ regions: [region],
194
+ paint(painter) {
195
+ const strokeInset = stroke / 2;
196
+ painter.drawRect(region.x + strokeInset, region.y + strokeInset, Math.max(0, region.w - stroke), Math.max(0, region.h - stroke), {
197
+ stroke: "#000",
198
+ strokeWidth: stroke,
199
+ });
200
+ },
201
+ };
201
202
  }
202
203
  }
@@ -1,7 +1,8 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { skipSpaces, skipSpacesBack } from "../../parser/parserContext.js";
3
- import { TemporalNodeBase } from "../../lowering/types.js";
3
+ import { TemporalNodeBase } from "../temporal.js";
4
4
  import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
5
+ import { Fraction } from "../../fraction.js";
5
6
  class BrFunction extends ASTFunctionNode {
6
7
  static def = {
7
8
  name: ["br"],
@@ -44,7 +45,6 @@ class BrFunction extends ASTFunctionNode {
44
45
  loweringEnter() {
45
46
  const br = new TemporalNodeBase();
46
47
  br.ast = this;
47
- br.T = 0;
48
48
  br.mergeKey = -1;
49
49
  br.breakBefore = this.offset;
50
50
  return [br];
@@ -54,17 +54,19 @@ class BrFunction extends ASTFunctionNode {
54
54
  * 换行会把整条时间线切开,因此跨越换行点的持续事件无法被完整绘制
55
55
  */
56
56
  static loweringFinalize = (result) => {
57
- let latestEnd = -Infinity;
57
+ const latestEnd = new Fraction();
58
+ const candidateEnd = new Fraction();
58
59
  let latestNode = null;
59
60
  for (const column of result.columns) {
60
- const t = column[0].t;
61
- if (latestEnd > t + 1e-6 && column.some(node => node.breakBefore > 0)) {
61
+ if (latestNode && latestEnd.compare(column[0].t) > 0 && column.some(node => node.breakBefore > 0)) {
62
62
  throw Diagnostic.error.BreakInsideEvent(latestNode.ast.sourceSpan);
63
63
  }
64
64
  for (const node of column) {
65
- const end = node.t + node.T;
66
- if (node.T > 1e-6 && end > latestEnd) {
67
- latestEnd = end;
65
+ if (node.T.isZero())
66
+ continue;
67
+ candidateEnd.copyFrom(node.t).add(node.T);
68
+ if (!latestNode || candidateEnd.compare(latestEnd) > 0) {
69
+ latestEnd.copyFrom(candidateEnd);
68
70
  latestNode = node;
69
71
  }
70
72
  }
@@ -1,5 +1,6 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
2
+ import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
3
+ import { WarningDiagnostic } from "../../diagnostic.js";
3
4
  class DashFunction extends ASTFunctionNode {
4
5
  static def = {
5
6
  name: ["dash", "-"],
@@ -27,7 +28,7 @@ class DashFunction extends ASTFunctionNode {
27
28
  size;
28
29
  constructor(span, _args, ctx, parent = null) {
29
30
  super(span, parent);
30
- this.size = ctx.fontSize;
31
+ this.size = ctx.variables.fontsize;
31
32
  }
32
33
  loweringEnter() {
33
34
  return [new DashTemporalNode(this)];
@@ -36,26 +37,58 @@ class DashFunction extends ASTFunctionNode {
36
37
  }
37
38
  export const DashNode = DashFunction;
38
39
  class DashTemporalNode extends TemporalNodeBase {
40
+ lineWidth = 0;
39
41
  lineY = 0;
40
42
  constructor(ast) {
41
43
  super();
42
44
  this.ast = ast;
43
- this.T = 1;
45
+ this.T.set(1);
44
46
  this.mergeKey = DEFAULT_KEY;
45
47
  this.initLayoutBox();
46
48
  }
49
+ onTimeState(state) {
50
+ this.playbackState = { bpm: state.bpm };
51
+ }
47
52
  prepareLayout() {
48
53
  const size = this.ast.size;
49
- const width = size * 0.5;
54
+ this.lineWidth = size * 0.42;
50
55
  // dash 与数字音符共享视觉中心和完整字号高度
51
56
  // 线本身位于数字视觉中心,不使用极小的 glyph 高度作为轨道高度
52
- this.box.w = width;
57
+ this.box.w = this.lineWidth;
53
58
  this.box.h = size;
54
- this.box.anchor = width / 2;
59
+ this.box.anchor = this.lineWidth / 2;
55
60
  this.lineY = size * 0.5;
56
61
  this.box.visualAxis = this.lineY;
57
62
  }
63
+ emitPlayback(emitter) {
64
+ const start = emitter.start.clone();
65
+ const end = emitter.end.clone();
66
+ const track = emitter.track;
67
+ const sourceSpan = this.ast.sourceSpan;
68
+ emitter.defer(context => {
69
+ let rootOrigin;
70
+ for (let i = context.events.length - 1; i >= 0; i--) {
71
+ const event = context.events[i];
72
+ if (event.kind !== "note-off" || event.origins.at(-1).node.track !== track)
73
+ continue;
74
+ if (rootOrigin === undefined) {
75
+ if (!event.at.equals(start))
76
+ continue;
77
+ rootOrigin = event.origins[0];
78
+ }
79
+ else if (event.origins[0] !== rootOrigin) {
80
+ break;
81
+ }
82
+ if (event.at.equals(start)) {
83
+ event.at.copyFrom(end);
84
+ }
85
+ }
86
+ if (rootOrigin === undefined) {
87
+ context.diagnostics.push(new WarningDiagnostic("W_PLAYBACK_SUSTAIN_WITHOUT_TARGET", "增时线前没有可延续的发声音符", sourceSpan));
88
+ }
89
+ });
90
+ }
58
91
  paint(painter) {
59
- painter.drawLine(this.box.x, this.box.y + this.lineY, this.box.x + this.box.w, this.box.y + this.lineY, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
92
+ painter.drawLine(this.box.x, this.box.y + this.lineY, this.box.x + this.lineWidth, this.box.y + this.lineY, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
60
93
  }
61
94
  }
@@ -1,32 +1,43 @@
1
+ import { AdjustNode } from "./adjust/index.js";
2
+ import { ArpeggioNode } from "./arpeggio/index.js";
1
3
  import { BarNode } from "./bar/index.js";
2
4
  import { BeamNode } from "./beam/index.js";
3
5
  import { BoxNode } from "./box/index.js";
4
6
  import { DashNode } from "./dash/index.js";
5
7
  import { DivNode } from "./div/index.js";
6
8
  import { DotNode } from "./dot/index.js";
9
+ import { DynNode } from "./dyn/index.js";
7
10
  import { GraceNode } from "./grace/index.js";
11
+ import { HeadNode } from "./head/index.js";
8
12
  import { KeyNode } from "./key/index.js";
13
+ import { MeterNode } from "./meter/index.js";
9
14
  import { BrNode } from "./br/index.js";
10
15
  import { NoteNode } from "./note/index.js";
11
- import { UpNode } from "./up/index.js";
16
+ import { UpNode, DownNode } from "./up/index.js";
12
17
  import { PageNode } from "./page/index.js";
18
+ import { ProgramNode } from "./program/index.js";
13
19
  import { StackNode } from "./stack/index.js";
20
+ import { SymbolNode } from "./symbol/index.js";
14
21
  import { SetNode } from "./set/index.js";
15
22
  import { TempoNode } from "./tempo/index.js";
16
23
  import { TextNode } from "./text/index.js";
24
+ import { VoltaNode } from "./volta/index.js";
17
25
  import { TieNode } from "./tie/index.js";
26
+ import { TupletNode } from "./tuplet/index.js";
18
27
  import { VoiceNode, VoicesNode } from "./voice/index.js";
19
28
  export const defaultFunctions = [
20
29
  NoteNode, DashNode, BarNode, // 有实体
21
- DivNode, DotNode, // 装饰性
30
+ DivNode, DotNode, TupletNode, // 装饰性
22
31
  VoiceNode, VoicesNode, // 歌词和声部
23
32
  BrNode, // 排版
24
- PageNode, // 文档页面
33
+ PageNode, HeadNode, // 文档页面与谱头
25
34
  StackNode, // 时间同步
26
- UpNode,
35
+ UpNode, DownNode,
36
+ ArpeggioNode,
27
37
  GraceNode, // 倚音
28
- TieNode, BeamNode,
29
- BoxNode,
30
- SetNode, KeyNode, TempoNode, // 设置
31
- TextNode,
38
+ TieNode, BeamNode, DynNode,
39
+ BoxNode, VoltaNode,
40
+ AdjustNode, // 手工微调
41
+ SetNode, KeyNode, MeterNode, TempoNode, ProgramNode, // 设置
42
+ TextNode, SymbolNode,
32
43
  ];
@@ -6,6 +6,18 @@ export declare const DIV_ADDON_KEY: "@div";
6
6
  /** div 暴露自己的线段端口,不依赖任何关系函数 */
7
7
  export declare function divLinePortName(level: number, edge: "left" | "right"): string;
8
8
  export declare function claimDivLine(host: LayoutHost, level: number): void;
9
+ /** 接管绘制的函数必须用同一条公式,否则同一条减时线会因为谁画而粗细不一 */
10
+ export declare function divLineWidth(size: number): number;
11
+ /**
12
+ * 逐级产出连续提供该级端口的下标区间(含两端,长度至少为 2)
13
+ *
14
+ * 端口按级递增排布是 div 的内部约定,接管绘制的函数只按这里给出的区间连线
15
+ */
16
+ export declare function divLineRuns(hosts: readonly LayoutHost[]): Generator<{
17
+ level: number;
18
+ from: number;
19
+ to: number;
20
+ }, void, unknown>;
9
21
  declare class DivFunction extends ASTFunctionNode {
10
22
  static def: {
11
23
  name: string[];
@@ -20,6 +32,10 @@ declare class DivFunction extends ASTFunctionNode {
20
32
  name: string;
21
33
  type: "number";
22
34
  default: number;
35
+ } | {
36
+ name: string;
37
+ type: "boolean";
38
+ default: boolean;
23
39
  })[];
24
40
  };
25
41
  static deSugarAtom(source: string, start: number, end: number): {
@@ -1,4 +1,5 @@
1
1
  import { ASTFunctionNode, functionAddonKey } from "../ASTtypes.js";
2
+ import { WarningDiagnostic } from "../../diagnostic.js";
2
3
  const DIV_FUNC_NAME = "div";
3
4
  export const DIV_ADDON_KEY = functionAddonKey(DIV_FUNC_NAME);
4
5
  /** div 暴露自己的线段端口,不依赖任何关系函数 */
@@ -10,19 +11,38 @@ export function claimDivLine(host, level) {
10
11
  if (port)
11
12
  port.claimed = true;
12
13
  }
13
- function parseAutoBeamFlag(raw) {
14
- if (typeof raw === "boolean")
15
- return raw;
16
- if (typeof raw === "number")
17
- return raw !== 0;
18
- if (typeof raw === "string") {
19
- const normalized = raw.trim().toLowerCase();
20
- if (normalized === "true" || normalized === "1" || normalized === "on")
21
- return true;
22
- if (normalized === "false" || normalized === "0" || normalized === "off")
23
- return false;
14
+ /** 接管绘制的函数必须用同一条公式,否则同一条减时线会因为谁画而粗细不一 */
15
+ export function divLineWidth(size) {
16
+ return Math.max(1, size * 0.055);
17
+ }
18
+ /**
19
+ * 逐级产出连续提供该级端口的下标区间(含两端,长度至少为 2)
20
+ *
21
+ * 端口按级递增排布是 div 的内部约定,接管绘制的函数只按这里给出的区间连线
22
+ */
23
+ export function* divLineRuns(hosts) {
24
+ for (let level = 0;; level++) {
25
+ let hasPorts = false;
26
+ let from = -1;
27
+ // 多扫一位,让最后一段也走同一条收尾分支
28
+ for (let i = 0; i <= hosts.length; i++) {
29
+ const ports = hosts[i]?.ports;
30
+ const left = ports?.[divLinePortName(level, "left")];
31
+ const right = ports?.[divLinePortName(level, "right")];
32
+ if (left || right)
33
+ hasPorts = true;
34
+ if (left && right) {
35
+ if (from < 0)
36
+ from = i;
37
+ continue;
38
+ }
39
+ if (from >= 0 && i - from >= 2)
40
+ yield { level, from, to: i - 1 };
41
+ from = -1;
42
+ }
43
+ if (!hasPorts)
44
+ return;
24
45
  }
25
- return true;
26
46
  }
27
47
  class DivFunction extends ASTFunctionNode {
28
48
  static def = {
@@ -46,6 +66,11 @@ class DivFunction extends ASTFunctionNode {
46
66
  type: "number",
47
67
  default: 1,
48
68
  },
69
+ {
70
+ name: "autobeam",
71
+ type: "boolean",
72
+ default: true,
73
+ },
49
74
  ]
50
75
  };
51
76
  static deSugarAtom(source, start, end) {
@@ -104,7 +129,7 @@ class DivFunction extends ASTFunctionNode {
104
129
  if (count === 0)
105
130
  return null;
106
131
  const lineGap = host.ast.size * 0.14;
107
- const strokeWidth = Math.max(1, host.ast.size * 0.055);
132
+ const strokeWidth = divLineWidth(host.ast.size);
108
133
  const lineBlockHeight = (count - 1) * lineGap + strokeWidth;
109
134
  let lineLeft = 0;
110
135
  let lineRight = 0;
@@ -155,7 +180,7 @@ class DivFunction extends ASTFunctionNode {
155
180
  return;
156
181
  const addon = node.addon ??= {};
157
182
  addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + count;
158
- node.T /= 2 ** count;
183
+ node.T.divPow2(count);
159
184
  },
160
185
  });
161
186
  return [];
@@ -177,11 +202,14 @@ class DivFunction extends ASTFunctionNode {
177
202
  }
178
203
  constructor(sourceSpan, args, ctx, parent = null) {
179
204
  super(sourceSpan, parent);
180
- [this.content, this.n] = this.getArgValue(args, ctx);
181
- // beam loweringAugment 只读取这个快照,不读取之后可能已经变化的 ParserContext
182
- this.autoBeamEnabled = parseAutoBeamFlag(ctx.variables["autobeam"]);
205
+ [this.content, this.n, this.autoBeamEnabled] = this.getArgValue(args, ctx);
183
206
  // div 允许修饰任意 都会加下划线
184
207
  this.content.parent = this;
208
+ const n = Math.max(0, Math.trunc(this.n));
209
+ if (n !== this.n) {
210
+ ctx.diagnostics.push(new WarningDiagnostic("W_DIV_INVALID_N", `@div 的数量参数 n 必须为非负整数,已被修正为 ${n}`, this.sourceSpan));
211
+ this.n = n;
212
+ }
185
213
  }
186
214
  toString(source) {
187
215
  return `@div(${this.content.toString(source)}, ${this.n})`;
@@ -1,19 +1,20 @@
1
1
  import { ASTFunctionNode, functionAddonKey } from "../ASTtypes.js";
2
- import { ErrorDiagnostic } from "../../diagnostic.js";
2
+ import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
3
3
  const DOT_FUNC_NAME = "dot";
4
4
  const DOT_ADDON_KEY = functionAddonKey(DOT_FUNC_NAME);
5
5
  function durationFactor(count) {
6
- return 2 - Math.pow(2, -count);
6
+ const denominator = 2 ** count;
7
+ return [2 * denominator - 1, denominator];
7
8
  }
8
9
  class DotFunction extends ASTFunctionNode {
9
10
  static def = {
10
11
  name: [DOT_FUNC_NAME, "."],
11
12
  description: "附点",
12
13
  example: `@dot(C1, 2): C1右侧创建2个点 仅接收一个可接收元素
13
- 语法糖:在音符后加斜杠'.',可以多个
14
+ 语法糖:在目标后加'.',可以多个
14
15
  @dot(C1, 2) === C1..
15
- @dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个元素
16
- 设置命名参数前缀: dot
16
+ {1^2}. === {1.^2}
17
+ @dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个可见时间元素
17
18
  `,
18
19
  allowExtraArgs: false,
19
20
  args: [
@@ -106,21 +107,29 @@ class DotFunction extends ASTFunctionNode {
106
107
  };
107
108
  loweringEnter(ctx) {
108
109
  const count = this.n;
109
- ctx.beginLoweringGroup(this, {
110
+ const group = {
111
+ temporalCount: 0,
110
112
  onTemporal(node) {
113
+ group.temporalCount++;
111
114
  if (count === 0)
112
115
  return;
113
116
  const addon = node.addon ??= {};
114
117
  const current = Number(addon[DOT_ADDON_KEY]) || 0;
115
118
  const total = current + count;
116
- node.T *= durationFactor(total) / durationFactor(current);
119
+ const [totalNumerator, totalDenominator] = durationFactor(total);
120
+ const [currentNumerator, currentDenominator] = durationFactor(current);
121
+ node.T.mul(totalNumerator * currentDenominator, totalDenominator * currentNumerator);
117
122
  addon[DOT_ADDON_KEY] = total;
118
123
  },
119
- });
124
+ };
125
+ ctx.beginLoweringGroup(this, group);
120
126
  return [];
121
127
  }
122
128
  loweringExit(ctx) {
123
- ctx.endLoweringGroup(this);
129
+ const group = ctx.endLoweringGroup(this);
130
+ if (group.temporalCount !== 1) {
131
+ throw new ErrorDiagnostic("E_DOT_INVALID_CONTENT", `函数 @dot 只能接收 1个元素,但找到了 ${group.temporalCount} 个`, this.sourceSpan);
132
+ }
124
133
  return [];
125
134
  }
126
135
  timeFlowModel() {
@@ -135,26 +144,15 @@ class DotFunction extends ASTFunctionNode {
135
144
  constructor(sourceSpan, args, ctx, parent = null) {
136
145
  super(sourceSpan, parent);
137
146
  [this.content, this.n] = this.getArgValue(args, ctx);
138
- const contentJudge = DotFunction.leafNum(this.content);
139
- if (contentJudge !== 1) {
140
- throw new ErrorDiagnostic("E_DOT_INVALID_CONTENT", `函数 @dot 只能接收 1个元素,但找到了 ${contentJudge} 个`, sourceSpan);
141
- }
142
147
  this.content.parent = this;
148
+ const n = Math.max(0, Math.trunc(this.n));
149
+ if (n !== this.n) {
150
+ ctx.diagnostics.push(new WarningDiagnostic("W_DOT_INVALID_N", `@dot 的数量参数 n 必须为非负整数,已被修正为 ${n}`, this.sourceSpan));
151
+ this.n = n;
152
+ }
143
153
  }
144
154
  toString(source) {
145
155
  return `@dot(${this.content.toString(source)}, ${this.n})`;
146
156
  }
147
- // 判断叶节点数量
148
- static leafNum(node) {
149
- let count = 0;
150
- const chs = node.children;
151
- if (chs) { // 不是叶节点
152
- for (const child of chs)
153
- count += DotFunction.leafNum(child);
154
- }
155
- else
156
- count++;
157
- return count;
158
- }
159
157
  }
160
158
  export const DotNode = DotFunction;
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const DynNode: ASTFunctionClass;