jpfun 0.3.0 → 0.4.1

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 (129) hide show
  1. package/README.md +73 -1
  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/functions/ASTtypes.d.ts +30 -26
  23. package/dist/functions/ASTtypes.js +25 -23
  24. package/dist/functions/adjust/index.js +13 -6
  25. package/dist/functions/arpeggio/index.d.ts +2 -0
  26. package/dist/functions/arpeggio/index.js +184 -0
  27. package/dist/functions/arpeggio/shape.d.ts +7 -0
  28. package/dist/functions/arpeggio/shape.js +122 -0
  29. package/dist/functions/bar/index.js +30 -23
  30. package/dist/functions/beam/auto.d.ts +1 -1
  31. package/dist/functions/beam/auto.js +1 -1
  32. package/dist/functions/beam/index.js +6 -1
  33. package/dist/functions/beam/layout.d.ts +3 -3
  34. package/dist/functions/beam/layout.js +5 -3
  35. package/dist/functions/box/index.js +10 -3
  36. package/dist/functions/br/index.js +10 -6
  37. package/dist/functions/dash/index.js +12 -9
  38. package/dist/functions/default.js +6 -2
  39. package/dist/functions/div/index.d.ts +4 -1
  40. package/dist/functions/div/index.js +8 -7
  41. package/dist/functions/dot/index.js +18 -25
  42. package/dist/functions/dyn/index.d.ts +2 -0
  43. package/dist/functions/dyn/index.js +242 -0
  44. package/dist/functions/grace/index.d.ts +1 -1
  45. package/dist/functions/grace/index.js +17 -11
  46. package/dist/functions/head/index.js +168 -159
  47. package/dist/functions/key/index.js +12 -7
  48. package/dist/functions/meter/index.js +14 -6
  49. package/dist/functions/note/index.js +73 -31
  50. package/dist/functions/note/noteNameFSM.js +52 -63
  51. package/dist/functions/page/index.d.ts +5 -1
  52. package/dist/functions/page/index.js +38 -16
  53. package/dist/functions/program/index.d.ts +2 -0
  54. package/dist/functions/program/index.js +39 -0
  55. package/dist/functions/set/index.js +13 -8
  56. package/dist/functions/stack/index.js +7 -5
  57. package/dist/functions/symbol/index.d.ts +13 -3
  58. package/dist/functions/symbol/index.js +71 -22
  59. package/dist/functions/symbol/shape.d.ts +2 -0
  60. package/dist/functions/symbol/shape.js +14 -10
  61. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  62. package/dist/functions/symbol/symbols/dc.js +82 -0
  63. package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
  64. package/dist/functions/symbol/symbols/ds.js +93 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  66. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  67. package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
  68. package/dist/functions/symbol/symbols/fine.js +12 -0
  69. package/dist/functions/symbol/symbols/mordent.js +44 -31
  70. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  71. package/dist/functions/symbol/symbols/prall.js +42 -0
  72. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  73. package/dist/functions/symbol/symbols/segno.js +82 -0
  74. package/dist/functions/symbol/symbols/tr.js +0 -4
  75. package/dist/functions/tempo/index.js +12 -4
  76. package/dist/functions/temporal.d.ts +138 -0
  77. package/dist/functions/temporal.js +101 -0
  78. package/dist/functions/text/index.js +25 -10
  79. package/dist/functions/tie/index.js +45 -30
  80. package/dist/functions/tuplet/index.js +21 -8
  81. package/dist/functions/up/index.js +38 -33
  82. package/dist/functions/voice/index.js +153 -68
  83. package/dist/functions/volta/index.js +62 -55
  84. package/dist/iife/core.d.ts +1 -0
  85. package/dist/iife/core.js +3 -0
  86. package/dist/iife/from-midi.d.ts +1 -0
  87. package/dist/iife/from-midi.js +15 -0
  88. package/dist/iife/from-musicxml.d.ts +1 -0
  89. package/dist/iife/from-musicxml.js +3 -0
  90. package/dist/iife/full.d.ts +1 -0
  91. package/dist/iife/full.js +3 -0
  92. package/dist/index.d.ts +3 -21
  93. package/dist/index.js +3 -21
  94. package/dist/jpfun.core.min.js +119 -0
  95. package/dist/jpfun.from-midi.min.js +9 -0
  96. package/dist/jpfun.from-musicxml.min.js +12 -0
  97. package/dist/jpfun.min.js +111 -122
  98. package/dist/layout/default.js +2 -3
  99. package/dist/layout/engine.d.ts +2 -1
  100. package/dist/layout/engine.js +71 -26
  101. package/dist/layout/model.d.ts +4 -4
  102. package/dist/layout/model.js +15 -12
  103. package/dist/layout/page.js +5 -4
  104. package/dist/layout/path.js +3 -3
  105. package/dist/layout/types.d.ts +20 -2
  106. package/dist/lowering/loweringContext.d.ts +3 -1
  107. package/dist/lowering/loweringContext.js +28 -14
  108. package/dist/lowering/track.d.ts +4 -8
  109. package/dist/lowering/track.js +6 -1
  110. package/dist/lowering/types.d.ts +5 -128
  111. package/dist/lowering/types.js +1 -98
  112. package/dist/parser/parse-utils/string-utils.d.ts +2 -0
  113. package/dist/parser/parse-utils/string-utils.js +4 -0
  114. package/dist/parser/parserContext.d.ts +7 -8
  115. package/dist/parser/parserContext.js +41 -36
  116. package/dist/pipeline.d.ts +6 -5
  117. package/dist/pipeline.js +4 -5
  118. package/dist/playback/compile.d.ts +1 -1
  119. package/dist/playback/compile.js +15 -2
  120. package/dist/playback/event.d.ts +17 -3
  121. package/dist/playback/event.js +30 -12
  122. package/dist/playback/time.js +1 -1
  123. package/dist/playback/types.d.ts +5 -4
  124. package/dist/render/text.d.ts +6 -1
  125. package/dist/render/text.js +8 -1
  126. package/grammar.md +111 -57
  127. package/package.json +11 -2
  128. package/dist/functions/symbol/symbols/f.js +0 -55
  129. package/dist/functions/symbol/symbols/p.js +0 -61
@@ -0,0 +1,242 @@
1
+ import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
2
+ import { isVisualTemporalNode, } from "../temporal.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ class DynFunction extends ASTFunctionNode {
5
+ static def = {
6
+ name: ["dyn"],
7
+ description: "渐强渐弱",
8
+ details: `\
9
+ ~~~jpfun
10
+ 1 ^ $p 2@a 3 4@b
11
+ @dyn(a, b, 24)
12
+ ~~~
13
+ 端点须位于同一音轨、不同时间;变化量沿区间线性增加,叠加在每个音符原有力度上。本例从 \`a\` 到 \`b\` 渐强,终点增加 24 的力度。`,
14
+ allowExtraArgs: false,
15
+ args: [
16
+ { name: "from", description: "渐变起点的标签", type: "label", default: null },
17
+ { name: "to", description: "渐变终点的标签", type: "label", default: null },
18
+ { name: "dv", description: "力度变化量,正数渐强,负数渐弱", type: "number", default: null },
19
+ ],
20
+ };
21
+ static loweringFinalize = (result) => {
22
+ const dynamics = result.attachments.filter((attachment) => attachment instanceof DynAttachment);
23
+ if (dynamics.length === 0)
24
+ return;
25
+ // astToTemporal 包含不进入全局 columns 的和弦、倚音成员;host 只用于统一时间和音轨
26
+ const records = [];
27
+ for (const nodes of result.astToTemporal.values()) {
28
+ for (const node of nodes) {
29
+ const velocity = node.playbackState?.velocity;
30
+ if (velocity === undefined)
31
+ continue;
32
+ let host = node;
33
+ while (host.foldedInto)
34
+ host = host.foldedInto;
35
+ records.push({ node, host, velocity, increment: 0 });
36
+ }
37
+ }
38
+ records.sort((left, right) => left.host.t.compare(right.host.t) || left.node.order - right.node.order);
39
+ // 先累计全部 dyn 的贡献,最后统一写回,避免声明顺序改变后续 dyn 读取的原始力度
40
+ for (const dynamic of dynamics) {
41
+ const endVelocity = dynamic.end.playbackState?.velocity
42
+ ?? records.find(record => record.host === dynamic.toHost)?.velocity;
43
+ if (endVelocity === undefined)
44
+ throw new Error("@dyn end endpoint has no velocity-bearing member");
45
+ // 终点后保持完整增量,直到原始力度相对终点再次发生变化
46
+ const stop = records.find(record => record.host.track === dynamic.track
47
+ && record.host.t.compare(dynamic.toHost.t) > 0
48
+ && record.velocity !== endVelocity)?.host.t;
49
+ const duration = dynamic.toHost.t.clone().sub(dynamic.fromHost.t);
50
+ for (const record of records) {
51
+ if (record.host.track !== dynamic.track)
52
+ continue;
53
+ const fromStart = record.host.t.compare(dynamic.fromHost.t);
54
+ if (fromStart < 0 || (stop && record.host.t.compare(stop) >= 0))
55
+ continue;
56
+ const progress = record.host.t.compare(dynamic.toHost.t) >= 0
57
+ ? 1
58
+ : record.host.t.clone().sub(dynamic.fromHost.t).div(duration).toNumber();
59
+ record.increment += dynamic.dv * progress;
60
+ }
61
+ }
62
+ for (const record of records) {
63
+ if (record.increment === 0)
64
+ continue;
65
+ const velocity = Math.max(1, Math.min(127, record.velocity + record.increment));
66
+ record.node.playbackState = { ...record.node.playbackState, velocity };
67
+ }
68
+ };
69
+ from;
70
+ to;
71
+ dv;
72
+ size;
73
+ loweringEnter(ctx) {
74
+ const from = ctx.getTemporalNodes(this.from).at(0);
75
+ const to = ctx.getTemporalNodes(this.to).at(-1);
76
+ if (!from || !to) {
77
+ ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("dyn", this.sourceSpan));
78
+ return [];
79
+ }
80
+ let fromHost = from;
81
+ let toHost = to;
82
+ while (fromHost.foldedInto)
83
+ fromHost = fromHost.foldedInto;
84
+ while (toHost.foldedInto)
85
+ toHost = toHost.foldedInto;
86
+ if (!isVisualTemporalNode(fromHost) || !isVisualTemporalNode(toHost)) {
87
+ ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("dyn", this.sourceSpan));
88
+ return [];
89
+ }
90
+ if (fromHost.track !== toHost.track) {
91
+ throw new ErrorDiagnostic("E_DYN_CROSS_TRACK", "@dyn 的两个端点必须位于同一音轨", this.sourceSpan);
92
+ }
93
+ if (fromHost.t.equals(toHost.t)) {
94
+ throw new ErrorDiagnostic("E_DYN_ZERO_SPAN", "@dyn 的两个端点必须位于不同时间", this.sourceSpan);
95
+ }
96
+ if (fromHost.t.compare(toHost.t) < 0) {
97
+ ctx.addAttachment(new DynAttachment(this, to, fromHost, toHost));
98
+ }
99
+ else {
100
+ ctx.addAttachment(new DynAttachment(this, from, toHost, fromHost));
101
+ }
102
+ return [];
103
+ }
104
+ constructor(span, args, ctx, parent = null) {
105
+ super(span, parent);
106
+ const [from, to, inputDv] = this.getArgValue(args, ctx);
107
+ let dv = inputDv;
108
+ if (!Number.isFinite(dv) || dv === 0) {
109
+ throw new ErrorDiagnostic("E_DYN_INVALID_DELTA", "@dyn 的 dv 必须是非零有限数", span);
110
+ }
111
+ dv = Math.max(-127, Math.min(127, dv));
112
+ if (dv !== inputDv) {
113
+ ctx.diagnostics.push(new WarningDiagnostic("W_DYN_DELTA_CLAMPED", `@dyn 的 dv 必须在 -127 到 127 之间,已被限制为 ${dv}`, span));
114
+ }
115
+ this.from = from;
116
+ this.to = to;
117
+ this.dv = dv;
118
+ this.size = ctx.variables.fontsize;
119
+ }
120
+ toString(source) {
121
+ const name = (node) => node.label ?? node.toString(source);
122
+ return `@dyn(${name(this.from)}, ${name(this.to)}, ${this.dv})`;
123
+ }
124
+ }
125
+ export const DynNode = DynFunction;
126
+ class DynAttachment {
127
+ ast;
128
+ end;
129
+ fromHost;
130
+ toHost;
131
+ layer = "foreground";
132
+ constructor(ast, end, fromHost, toHost) {
133
+ this.ast = ast;
134
+ this.end = end;
135
+ this.fromHost = fromHost;
136
+ this.toHost = toHost;
137
+ }
138
+ get sourceSpan() { return this.ast.sourceSpan; }
139
+ get dv() { return this.ast.dv; }
140
+ get track() { return this.fromHost.track; }
141
+ createGeometry(context) {
142
+ const locate = (host) => {
143
+ const view = context.lines[host.layoutLine];
144
+ return { line: host.layoutLine, view, column: view?.columnOf(host) ?? -1 };
145
+ };
146
+ const from = locate(this.fromHost);
147
+ const to = locate(this.toHost);
148
+ if (!from.view || !to.view || from.column < 0 || to.column < 0) {
149
+ return { regions: [], paint() { } };
150
+ }
151
+ const ranges = [];
152
+ let size = this.ast.size;
153
+ for (let line = from.line; line <= to.line; line++) {
154
+ const view = context.lines[line];
155
+ const start = line === from.line ? from.column : 0;
156
+ const end = line === to.line ? to.column : view.columns.length - 1;
157
+ let firstColumn = -1;
158
+ let lastColumn = -1;
159
+ for (let column = start; column <= end; column++) {
160
+ for (const host of view.columns[column]) {
161
+ if (host.track !== this.track)
162
+ continue;
163
+ firstColumn = firstColumn < 0 ? column : firstColumn;
164
+ lastColumn = column;
165
+ size = Math.max(size, host.ast.size);
166
+ }
167
+ }
168
+ if (firstColumn < 0)
169
+ continue;
170
+ ranges.push({
171
+ line,
172
+ startX: line === from.line ? this.fromHost.box.x : this.lineEntryX(context, line),
173
+ endX: line === to.line
174
+ ? this.toHost.box.x + this.toHost.box.w
175
+ : this.lineRightX(context, line),
176
+ columns: [firstColumn, lastColumn],
177
+ });
178
+ }
179
+ // 允许左边超出一点
180
+ for (const range of ranges) {
181
+ if (range.line > from.line)
182
+ range.startX -= size / 4;
183
+ }
184
+ const strokeWidth = Math.max(0.75, size * 0.035);
185
+ const valid = ranges.filter(range => range.startX !== range.endX);
186
+ const total = valid.reduce((sum, range) => sum + Math.abs(range.endX - range.startX), 0);
187
+ if (total === 0)
188
+ return { regions: [], paint() { } };
189
+ const aperture = size * 0.4; // 最终开口高度
190
+ const halfStroke = strokeWidth / 2;
191
+ let covered = 0;
192
+ const segments = valid.map(range => {
193
+ const width = Math.abs(range.endX - range.startX);
194
+ const start = covered / total;
195
+ const end = (covered + width) / total;
196
+ covered += width;
197
+ const extent = context.getRangeExtents(range.line, range.columns).get(this.track);
198
+ const axis = context.getVisualAxis(range.line, this.track);
199
+ const hostTop = extent ? axis + extent.top : axis;
200
+ return {
201
+ line: range.line,
202
+ startX: range.startX,
203
+ endX: range.endX,
204
+ lineY: hostTop - size * 0.62,
205
+ startOpen: aperture * (this.dv > 0 ? start : 1 - start),
206
+ endOpen: aperture * (this.dv > 0 ? end : 1 - end),
207
+ };
208
+ });
209
+ return {
210
+ regions: segments.map(segment => ({
211
+ x: Math.min(segment.startX, segment.endX) - halfStroke,
212
+ y: segment.lineY - Math.max(segment.startOpen, segment.endOpen) / 2 - halfStroke,
213
+ w: Math.abs(segment.endX - segment.startX) + strokeWidth,
214
+ h: Math.max(segment.startOpen, segment.endOpen) + strokeWidth,
215
+ line: segment.line,
216
+ track: this.track,
217
+ })),
218
+ paint(painter) {
219
+ const style = { stroke: "#000", strokeWidth };
220
+ for (const segment of segments) {
221
+ painter.drawLine(segment.startX, segment.lineY - segment.startOpen / 2, segment.endX, segment.lineY - segment.endOpen / 2, style);
222
+ painter.drawLine(segment.startX, segment.lineY + segment.startOpen / 2, segment.endX, segment.lineY + segment.endOpen / 2, style);
223
+ }
224
+ },
225
+ };
226
+ }
227
+ lineEntryX(context, line) {
228
+ const host = context.lines[line].trackRuns.get(this.track)?.find(item => !item.T.isZero());
229
+ return host?.box.x ?? context.originX;
230
+ }
231
+ lineRightX(context, line) {
232
+ const runs = context.lines[line].trackRuns;
233
+ if (runs.size === 0)
234
+ return context.originX + context.width;
235
+ let right = context.originX;
236
+ for (const run of runs.values()) {
237
+ for (const host of run)
238
+ right = Math.max(right, host.box.x + host.box.w);
239
+ }
240
+ return right;
241
+ }
242
+ }
@@ -1,7 +1,7 @@
1
1
  import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
2
2
  import type { LoweringContext } from "../../lowering/loweringContext.js";
3
3
  import type { Track } from "../../lowering/track.js";
4
- import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../../lowering/types.js";
4
+ import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../temporal.js";
5
5
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
6
6
  import { ParserContext } from "../../parser/parserContext.js";
7
7
  import type { Painter } from "../../render/types.js";
@@ -1,7 +1,7 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
2
  import { prepareLayoutHost } from "../../layout/engine.js";
3
3
  import { Fraction } from "../../fraction.js";
4
- import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
4
+ import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
5
5
  import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, } from "../ASTtypes.js";
6
6
  import { createBeamLayoutAttachment } from "../beam/layout.js";
7
7
  import { DIV_ADDON_KEY } from "../div/index.js";
@@ -31,27 +31,34 @@ class GraceFunction extends ASTFunctionNode {
31
31
  static def = {
32
32
  name: ["grace"],
33
33
  description: "倚音",
34
- example: `前倚音: @grace(宿主, 倚音, side=pre) 后倚音: @grace(宿主, 倚音, side=post)
35
- 语法糖:'>' 与 '<',箭头永远指向宿主
36
- 2>1 前倚音:2 是 1 的倚音,画在 1 的左上角
37
- 1<2 后倚音:2 是 1 的倚音,画在 1 的右上角
38
- {3 2}>1 多个倚音用大括号括起来
39
- 1>2>3 允许嵌套:1 是 2 的倚音,2 是 3 的倚音
40
- 倚音默认就是八分音符(自带一条减时线),再写 '/' 变成十六分音符`,
41
- allowExtraArgs: true,
34
+ details: `\
35
+ ~~~jpfun
36
+ @grace(1, {3 2}, side=pre)
37
+ ~~~
38
+ **简写**:箭头始终指向宿主
39
+ - \`2>1\`:\`2\` 是 \`1\` 的前倚音,位于左上方
40
+ - \`1<2\`:\`2\` 是 \`1\` 的后倚音,位于右上方
41
+ - \`{3 2}>1\`:一组前倚音,等价于上例
42
+ - \`1>2>3\`:允许嵌套,\`1\` 修饰 \`2\`,\`2\` 修饰 \`3\`
43
+
44
+ 倚音默认带一条减时线,按八分音符书写;再加 \`/\` 变为十六分音符`,
45
+ allowExtraArgs: false,
42
46
  args: [
43
47
  {
44
48
  name: "host",
49
+ description: "被倚音修饰的宿主内容",
45
50
  type: "content",
46
51
  default: null,
47
52
  },
48
53
  {
49
54
  name: "grace",
55
+ description: "倚音内容,多个音符可用大括号组合",
50
56
  type: "content",
51
57
  default: null,
52
58
  },
53
59
  {
54
60
  name: "side",
61
+ description: "`pre` 为前倚音,`post` 为后倚音",
55
62
  type: "string",
56
63
  default: "pre",
57
64
  }
@@ -180,14 +187,13 @@ class GraceFunction extends ASTFunctionNode {
180
187
  }
181
188
  constructor(span, args, ctx, parent = null, scale = GRACE_SCALE) {
182
189
  super(span, parent);
183
- this.size = ctx.fontSize;
190
+ this.size = ctx.variables.fontsize;
184
191
  [this.host, this.grace, this.side] = this.getArgValue(args, ctx);
185
192
  if (this.side !== "pre" && this.side !== "post") {
186
193
  throw new ErrorDiagnostic("E_GRACE_INVALID_SIDE", "@grace 的 side 参数必须是 'pre' 或 'post'", span);
187
194
  }
188
195
  this.adopt(this.host);
189
196
  this.setGrace(this.grace, scale);
190
- this.size = ctx.fontSize;
191
197
  }
192
198
  setGrace(node, factor = GRACE_SCALE) {
193
199
  scaleFontSize(node, factor);