jpfun 0.1.0 → 0.3.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 (114) hide show
  1. package/LICENSE +200 -200
  2. package/README.md +191 -133
  3. package/dist/diagnostic.js +5 -2
  4. package/dist/fraction.d.ts +24 -0
  5. package/dist/fraction.js +132 -0
  6. package/dist/functions/ASTtypes.d.ts +24 -7
  7. package/dist/functions/ASTtypes.js +35 -16
  8. package/dist/functions/adjust/index.d.ts +2 -0
  9. package/dist/functions/adjust/index.js +193 -0
  10. package/dist/functions/bar/index.d.ts +8 -0
  11. package/dist/functions/bar/index.js +48 -19
  12. package/dist/functions/beam/auto.js +35 -18
  13. package/dist/functions/beam/index.js +10 -6
  14. package/dist/functions/beam/layout.d.ts +3 -2
  15. package/dist/functions/beam/layout.js +42 -64
  16. package/dist/functions/box/index.js +35 -34
  17. package/dist/functions/br/index.js +14 -12
  18. package/dist/functions/dash/index.js +37 -5
  19. package/dist/functions/default.js +14 -7
  20. package/dist/functions/div/index.d.ts +16 -0
  21. package/dist/functions/div/index.js +51 -23
  22. package/dist/functions/dot/index.js +16 -8
  23. package/dist/functions/grace/index.d.ts +8 -18
  24. package/dist/functions/grace/index.js +98 -99
  25. package/dist/functions/head/index.d.ts +2 -0
  26. package/dist/functions/head/index.js +492 -0
  27. package/dist/functions/key/index.js +98 -5
  28. package/dist/functions/meter/index.d.ts +2 -0
  29. package/dist/functions/meter/index.js +154 -0
  30. package/dist/functions/note/accidentals.d.ts +24 -3
  31. package/dist/functions/note/accidentals.js +31 -1
  32. package/dist/functions/note/index.js +60 -80
  33. package/dist/functions/page/index.d.ts +9 -2
  34. package/dist/functions/page/index.js +50 -4
  35. package/dist/functions/set/index.js +42 -34
  36. package/dist/functions/stack/index.js +39 -34
  37. package/dist/functions/symbol/index.d.ts +21 -0
  38. package/dist/functions/symbol/index.js +94 -0
  39. package/dist/functions/symbol/ornament.d.ts +15 -0
  40. package/dist/functions/symbol/ornament.js +73 -0
  41. package/dist/functions/symbol/shape.d.ts +22 -0
  42. package/dist/functions/symbol/shape.js +48 -0
  43. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  44. package/dist/functions/symbol/symbols/accent.js +24 -0
  45. package/dist/functions/symbol/symbols/f.d.ts +2 -0
  46. package/dist/functions/symbol/symbols/f.js +55 -0
  47. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  48. package/dist/functions/symbol/symbols/fermata.js +24 -0
  49. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  50. package/dist/functions/symbol/symbols/mordent.js +43 -0
  51. package/dist/functions/symbol/symbols/p.d.ts +2 -0
  52. package/dist/functions/symbol/symbols/p.js +61 -0
  53. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  54. package/dist/functions/symbol/symbols/tr.js +75 -0
  55. package/dist/functions/tempo/glyph.d.ts +12 -0
  56. package/dist/functions/tempo/glyph.js +30 -0
  57. package/dist/functions/tempo/index.js +53 -3
  58. package/dist/functions/text/index.js +78 -26
  59. package/dist/functions/tie/index.js +103 -128
  60. package/dist/functions/tuplet/index.d.ts +2 -0
  61. package/dist/functions/tuplet/index.js +211 -0
  62. package/dist/functions/{over → up}/index.d.ts +1 -0
  63. package/dist/functions/up/index.js +370 -0
  64. package/dist/functions/voice/index.js +125 -108
  65. package/dist/functions/volta/index.d.ts +2 -0
  66. package/dist/functions/volta/index.js +227 -0
  67. package/dist/index.d.ts +4 -0
  68. package/dist/index.js +4 -0
  69. package/dist/jpfun.min.js +143 -0
  70. package/dist/layout/engine.d.ts +4 -11
  71. package/dist/layout/engine.js +100 -66
  72. package/dist/layout/model.d.ts +8 -1
  73. package/dist/layout/model.js +16 -6
  74. package/dist/layout/path.d.ts +19 -0
  75. package/dist/layout/path.js +101 -0
  76. package/dist/layout/types.d.ts +51 -18
  77. package/dist/layout/types.js +3 -1
  78. package/dist/lowering/loweringContext.d.ts +19 -23
  79. package/dist/lowering/loweringContext.js +179 -187
  80. package/dist/lowering/track.d.ts +33 -10
  81. package/dist/lowering/track.js +6 -6
  82. package/dist/lowering/types.d.ts +57 -43
  83. package/dist/lowering/types.js +20 -4
  84. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  85. package/dist/parser/parse-utils/call-utils.js +0 -6
  86. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  87. package/dist/parser/parse-utils/note-utils.js +35 -13
  88. package/dist/parser/parse-utils/string-utils.d.ts +8 -0
  89. package/dist/parser/parse-utils/string-utils.js +20 -0
  90. package/dist/parser/parserContext.d.ts +8 -5
  91. package/dist/parser/parserContext.js +38 -33
  92. package/dist/parser/preprocess.js +17 -11
  93. package/dist/playback/compile.d.ts +18 -0
  94. package/dist/playback/compile.js +401 -0
  95. package/dist/playback/event.d.ts +91 -0
  96. package/dist/playback/event.js +87 -0
  97. package/dist/playback/time.d.ts +14 -0
  98. package/dist/playback/time.js +81 -0
  99. package/dist/playback/types.d.ts +128 -0
  100. package/dist/playback/types.js +3 -0
  101. package/dist/render/canvas.d.ts +4 -4
  102. package/dist/render/canvas.js +20 -5
  103. package/dist/render/index.d.ts +1 -0
  104. package/dist/render/index.js +1 -0
  105. package/dist/render/paint.d.ts +8 -0
  106. package/dist/render/paint.js +101 -0
  107. package/dist/render/svg.d.ts +3 -3
  108. package/dist/render/svg.js +10 -8
  109. package/dist/render/text.d.ts +17 -0
  110. package/dist/render/text.js +35 -0
  111. package/dist/render/types.d.ts +1 -1
  112. package/grammar.md +217 -122
  113. package/package.json +14 -9
  114. package/dist/functions/over/index.js +0 -307
@@ -1,51 +1,59 @@
1
- import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
1
+ import { ASTFunctionNode, primaryName, resolveArgType } from "../ASTtypes.js";
2
+ import { WarningDiagnostic } from "../../diagnostic.js";
3
+ import { SYSTEM_VARIABLE_TYPES } from "../../parser/parserContext.js";
3
4
  class SetFunction extends ASTFunctionNode {
4
5
  static def = {
5
6
  name: ["set"],
6
7
  description: "设置局部默认值",
7
- example: `@set(fontSize=20) 则当前块内默认字体变为20px
8
- 可以设置其他函数的默认值: {函数名}.{属性}=值,例:
9
- @set(note.octave=5, note.color=#f00)
10
-
11
- 注意:@set 作用域仅在其所处 '{}' 内、其出现之后。离开当前作用域会恢复为之前的设置。此设置和时序无关,类似于局部变量。
8
+ example: `@set(fontSize=20) 则当前块内默认字体变为20px
9
+ 可以设置其他函数的默认值: {函数名}.{属性}=值,例:
10
+ @set(note.octave=5, note.color=#f00)
11
+
12
+ 注意:@set 作用域仅在其所处 '{}' 内、其出现之后。离开当前作用域会恢复为之前的设置。此设置和时序无关,类似于局部变量。
12
13
  `,
13
14
  allowExtraArgs: true,
14
15
  args: []
15
16
  };
17
+ /** 值的类型由目标参数决定:`函数名.参数名` 查该函数的声明,无点号的是内置变量 */
18
+ static resolveTarget(ctx, key, nameSpan) {
19
+ const dot = key.lastIndexOf(".");
20
+ if (dot < 0)
21
+ return [key, SYSTEM_VARIABLE_TYPES[key] ?? "string"];
22
+ const argName = key.slice(dot + 1);
23
+ const def = ctx.functions.get(key.slice(0, dot))?.prototype.def;
24
+ const type = def && resolveArgType(def, argName, -1);
25
+ if (!def || !type) {
26
+ ctx.diagnostics.push(new WarningDiagnostic("W_SET_UNKNOWN_TARGET", `@set 的参数 [${key}] 没有对应的函数或其参数,将被忽略`, nameSpan));
27
+ return null;
28
+ }
29
+ if (type === "content" || type === "label") {
30
+ ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE_TYPE", `@set 的参数 [${key}] 内容和标签不能作为默认值,将被忽略`, nameSpan));
31
+ return null;
32
+ }
33
+ // 别名统一成主名,否则 getArgValue 按主名查不到
34
+ return [`${primaryName(def)}.${argName}`.toLowerCase(), type];
35
+ }
16
36
  args = new Map();
17
37
  constructor(sourceSpan, args, ctx, parent = null) {
18
38
  super(sourceSpan, parent);
19
39
  for (const [key, value] of args) {
20
- if (typeof key === "string") {
21
- // 先用string提取出raw text
22
- const v = ctx.parseArgWithType(value.start, value.end, "string", sourceSpan.start);
23
- if (v === null) {
24
- ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE", `函数 @set 的参数值解析失败, 参数[${key}]将被忽略`, value));
25
- continue;
26
- }
27
- let k = key.toLowerCase(); // 统一小写处理
28
- try {
29
- // ctx内置的setter可能报错
30
- ctx.variables[k] = v;
31
- this.args.set(k, v);
32
- if (k === "fontsize")
33
- ctx.fontSize = v;
34
- else if (k === "strict")
35
- ctx.strict = v;
36
- }
37
- catch (e) {
38
- if (e instanceof Diagnostic) {
39
- e.span = value;
40
- if (e.code[0] === "E")
41
- throw e;
42
- ctx.diagnostics.push(e);
43
- }
44
- }
40
+ const arg = value;
41
+ if (typeof key !== "string") {
42
+ ctx.diagnostics.push(new WarningDiagnostic("W_SET_POSITIONAL_ARG", `函数 @set 不接收位置参数, 位置参数[${key}]将被忽略`, arg.span));
43
+ continue;
45
44
  }
46
- else {
47
- ctx.diagnostics.push(new WarningDiagnostic("W_SET_POSITIONAL_ARG", `函数 @set 不接收位置参数, 位置参数[${key}]将被忽略`, value));
45
+ const target = SetFunction.resolveTarget(ctx, key.toLowerCase(), arg.nameSpan ?? arg.span);
46
+ if (target === null)
47
+ continue;
48
+ const [k, type] = target;
49
+ // 按目标参数的声明类型固化,否则消费者只能拿到 raw text 各自再解析一遍
50
+ const v = ctx.parseArgWithType(arg.valueSpan, type, sourceSpan.start);
51
+ if (v === null) {
52
+ ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE", `函数 @set 的参数值解析失败, 参数[${key}]将被忽略`, arg.valueSpan));
53
+ continue;
48
54
  }
55
+ ctx.setVariable(k, v);
56
+ this.args.set(k, v);
49
57
  }
50
58
  }
51
59
  toString() {
@@ -5,23 +5,23 @@ import { ErrorDiagnostic } from "../../diagnostic.js";
5
5
  * 间隙作用在盒边界之间,因此即使很小也不会互相穿模
6
6
  */
7
7
  const GAP_RATIO = 0.4;
8
+ const placeAbove = (host, group, gap) => host.top - gap * GAP_RATIO - group.bottom;
8
9
  /**
9
- * stack 的成员依次贴在宿主上方:书写顺序即自下而上
10
- * cursor 从宿主已占用的最上沿开始,因此同一宿主上的多个分组会自内向外依次堆叠
10
+ * stack 的成员在局部坐标中按书写顺序自下而上排列
11
11
  */
12
- const arrangeAbove = (host, members, gap) => {
12
+ const measureAbove = (members, gap) => {
13
13
  const step = gap * GAP_RATIO;
14
14
  const placements = [];
15
- let cursor = host.top;
15
+ let cursor = 0;
16
16
  for (const extent of members) {
17
17
  // 临时伴奏在本行没有内容就不占位,避免共用音轨白白抬高行高
18
18
  if (!extent) {
19
19
  placements.push(null);
20
20
  continue;
21
21
  }
22
- const offset = cursor - step - extent.bottom;
22
+ const offset = cursor - extent.bottom;
23
23
  placements.push({ offset, extent });
24
- cursor = offset + extent.top;
24
+ cursor = offset + extent.top - step;
25
25
  }
26
26
  return placements;
27
27
  };
@@ -32,16 +32,17 @@ const arrangeAbove = (host, members, gap) => {
32
32
  */
33
33
  const STACK_TRACKS = {
34
34
  laneKey: "stack",
35
- arrange: arrangeAbove,
35
+ measure: measureAbove,
36
+ place: placeAbove,
36
37
  };
37
38
  class StackFunction extends ASTFunctionNode {
38
39
  static def = {
39
40
  name: ["stack"],
40
- description: "时间对齐的上下层叠",
41
- example: `@stack(content1, content2, ...)
42
- 语法糖: &
43
- {content1} & {content2} & ...
44
- 表示content1和content2在时间上完全重叠,通常用于和声等需要对齐的场景。可以有任意多个参数,至少需要两个参数。
41
+ description: "临时多声部",
42
+ example: `@stack(content1, content2, ...)
43
+ 语法糖: &
44
+ {content1} & {content2} & ...
45
+ 表示content1和content2在时间上完全重叠,通常用于和声等需要对齐的场景。可以有任意多个参数,至少需要两个参数。
45
46
  `,
46
47
  allowExtraArgs: true,
47
48
  extraArgType: "content",
@@ -70,31 +71,34 @@ class StackFunction extends ASTFunctionNode {
70
71
  continue;
71
72
  break;
72
73
  }
73
- let overNode = left >= 0 ? ctx.nodes[left] : null;
74
- if (overNode === null) {
74
+ let leftNode = left >= 0 ? ctx.nodes[left] : null;
75
+ if (leftNode === null) {
75
76
  throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 左边没有找到可叠加的目标", n.span);
76
77
  }
77
78
  /** 左操作数在 ctx.nodes 中的起点;只有这一段会被 stack 吞并,更早的节点必须保留 */
78
79
  let replaceFrom = left;
79
80
  // 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
80
- if (overNode instanceof ASTLabelNode) {
81
- const tgt = overNode.parent;
81
+ if (leftNode instanceof ASTLabelNode) {
82
+ const labelNode = leftNode;
83
+ let tgt = labelNode.target;
84
+ // 在本层这个节点位于哪里(标签节点可能被wrap)
85
+ while (tgt && !ctx.nodes.includes(tgt))
86
+ tgt = tgt.parent;
82
87
  for (let j = left - 1; j >= 0; j--) {
83
88
  if (ctx.nodes[j] === tgt) {
84
- overNode = new ASTBraceNode({
89
+ leftNode = new ASTBraceNode({
85
90
  start: tgt.sourceSpan.start,
86
- end: overNode.sourceSpan.end,
91
+ end: labelNode.sourceSpan.end,
87
92
  }, ctx.nodes.slice(j, left + 1), null);
88
93
  replaceFrom = j;
89
94
  break;
90
95
  }
91
96
  }
92
97
  }
93
- if (!(overNode instanceof StackFunction)) {
94
- const newNode = new StackFunction(n.span, new Map(), ctx);
95
- newNode.addContent(overNode);
96
- overNode = newNode;
97
- }
98
+ // 左操作数收敛成一个 stack,后续合并都在它身上进行
99
+ const host = leftNode instanceof StackFunction
100
+ ? leftNode
101
+ : new StackFunction(n.span, new Map(), ctx).addContent(leftNode);
98
102
  // 找到下一个非文本节点 通过全量后续解析的方式进行 还是有些trick
99
103
  const storage = ctx.nodes;
100
104
  ctx.nodes = [];
@@ -104,9 +108,9 @@ class StackFunction extends ASTFunctionNode {
104
108
  const right = ctx.nodes[i];
105
109
  if (right instanceof ASTTextNode)
106
110
  continue;
107
- overNode.addContent(right);
111
+ host.addContent(right);
108
112
  storage.length = replaceFrom;
109
- storage.push(overNode);
113
+ storage.push(host);
110
114
  while (++i < ctx.nodes.length)
111
115
  storage.push(ctx.nodes[i]);
112
116
  ctx.nodes = storage;
@@ -130,27 +134,28 @@ class StackFunction extends ASTFunctionNode {
130
134
  this.addContent(value);
131
135
  continue;
132
136
  }
133
- const c = ctx.parseArgWithType(value.start, value.end, "content", span.start);
137
+ const c = ctx.parseArgWithType(value.valueSpan, "content", span.start);
134
138
  if (c !== null) {
135
139
  this.addContent(c);
136
140
  }
137
141
  }
138
142
  }
143
+ /** 遇到另一个 stack 就展平;返回存活的那个,调用者必须改用返回值 */
139
144
  addContent(node) {
140
145
  if (node instanceof StackFunction)
141
- this.combine(node);
142
- else {
143
- this.contents.push(node);
144
- node.parent = this;
145
- const s = node.sourceSpan;
146
- this.sourceSpan.start = Math.min(this.sourceSpan.start, s.start);
147
- this.sourceSpan.end = Math.max(this.sourceSpan.end, s.end);
148
- }
146
+ return this.combine(node);
147
+ this.contents.push(node);
148
+ node.parent = this;
149
+ const s = node.sourceSpan;
150
+ this.sourceSpan.start = Math.min(this.sourceSpan.start, s.start);
151
+ this.sourceSpan.end = Math.max(this.sourceSpan.end, s.end);
152
+ return this;
149
153
  }
150
154
  toString(source) {
151
155
  const contentStrs = this.contents.map(c => c.toString(source)).join(',\n');
152
156
  return `@stack(\n ${contentStrs.split('\n').join('\n ')}\n)`;
153
157
  }
158
+ // stack 不实现 labelable、不进候选表,所以被吞并的空壳不会被 @tie() 取到,无需像 up 那样撤销登记
154
159
  combine(ano) {
155
160
  this.sourceSpan.start = Math.min(this.sourceSpan.start, ano.sourceSpan.start);
156
161
  this.sourceSpan.end = Math.max(this.sourceSpan.end, ano.sourceSpan.end);
@@ -0,0 +1,21 @@
1
+ import { type TimeState } from "../../lowering/types.js";
2
+ import type { PlaybackEmitter } from "../../playback/types.js";
3
+ import { type ASTFunctionClass } from "../ASTtypes.js";
4
+ import { type SymbolShape } from "./shape.js";
5
+ /**
6
+ * 一个乐谱符号:固定图形加可选的播放语义
7
+ *
8
+ * 每个符号自成一个文件、彼此没有依赖,新增一个符号就是新增一个对象字面量
9
+ */
10
+ export interface SymbolDefinition {
11
+ readonly name: string;
12
+ readonly description: string;
13
+ readonly shapes: readonly SymbolShape[];
14
+ /** 符号高度 = size × weight,用来修正宽扁字形的视觉重量;默认 1 */
15
+ readonly weight?: number;
16
+ /** 写入本位置之后持续生效的状态,例如力度记号的 velocity */
17
+ readonly onTimeState?: (state: TimeState) => void;
18
+ /** 发布系统控制,或注册对同一折叠序列后续音符的局部变换 */
19
+ readonly emitPlayback?: (emitter: PlaybackEmitter) => void;
20
+ }
21
+ export declare const SymbolNode: ASTFunctionClass;
@@ -0,0 +1,94 @@
1
+ import { ErrorDiagnostic } from "../../diagnostic.js";
2
+ import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ import { paintSymbol, symbolBounds } from "./shape.js";
5
+ import { accentSymbol } from "./symbols/accent.js";
6
+ import { fermataSymbol } from "./symbols/fermata.js";
7
+ import { fSymbol } from "./symbols/f.js";
8
+ import { mordentSymbol } from "./symbols/mordent.js";
9
+ import { pSymbol } from "./symbols/p.js";
10
+ import { trSymbol } from "./symbols/tr.js";
11
+ const symbolTable = new Map([trSymbol, fSymbol, pSymbol, fermataSymbol, mordentSymbol, accentSymbol]
12
+ .map(definition => [definition.name.toLowerCase(), definition]));
13
+ class SymbolFunction extends ASTFunctionNode {
14
+ static def = {
15
+ name: ["symbol", "$"],
16
+ description: "创建一个乐谱符号",
17
+ example: [
18
+ "@symbol(name, size)",
19
+ "语法糖: $name",
20
+ "内置:",
21
+ ...[...symbolTable.values()].map(item => ` $${item.name} ${item.description}`),
22
+ ].join("\n"),
23
+ allowExtraArgs: false,
24
+ args: [
25
+ {
26
+ name: "name",
27
+ type: "string",
28
+ default: null
29
+ },
30
+ {
31
+ name: "size",
32
+ type: "length",
33
+ default: { value: 0.5, unit: "em" },
34
+ },
35
+ ],
36
+ };
37
+ static deSugarAtom(source, start, end) {
38
+ if (source[start] !== "$" || !/[A-Za-z]/.test(source[start + 1] ?? ""))
39
+ return null;
40
+ let next = start + 2;
41
+ while (next < end && /[A-Za-z0-9_-]/.test(source[next]))
42
+ next++;
43
+ const name = source.slice(start + 1, next);
44
+ const node = {
45
+ kind: "call",
46
+ typed: true,
47
+ name: "symbol",
48
+ args: new Map([[0, name]]),
49
+ span: { start, end: next },
50
+ syntaxKind: "atom",
51
+ };
52
+ return { next, node };
53
+ }
54
+ size;
55
+ definition;
56
+ constructor(sourceSpan, args, ctx, parent = null) {
57
+ super(sourceSpan, parent);
58
+ const [name, size] = this.getArgValue(args, ctx);
59
+ this.size = ctx.length2px(size);
60
+ const definition = symbolTable.get(name.toLowerCase());
61
+ if (!definition) {
62
+ throw new ErrorDiagnostic("E_UNKNOWN_SYMBOL", `@symbol: 未知符号: $${name}`, sourceSpan);
63
+ }
64
+ this.definition = definition;
65
+ }
66
+ loweringEnter() { return [new SymbolTemporal(this)]; }
67
+ toString() { return `@symbol(${this.definition.name})`; }
68
+ }
69
+ export const SymbolNode = SymbolFunction;
70
+ class SymbolTemporal extends TemporalNodeBase {
71
+ /** 符号自身坐标系里的固有包围盒,与布局轮次无关 */
72
+ bounds;
73
+ scale = 1;
74
+ constructor(ast) {
75
+ super();
76
+ this.ast = ast;
77
+ this.mergeKey = DEFAULT_KEY;
78
+ this.onTimeState = ast.definition.onTimeState;
79
+ this.emitPlayback = ast.definition.emitPlayback;
80
+ this.bounds = symbolBounds(ast.definition.shapes);
81
+ this.initLayoutBox();
82
+ }
83
+ prepareLayout() {
84
+ const height = this.ast.size * (this.ast.definition.weight ?? 1);
85
+ this.scale = height / this.bounds.h;
86
+ this.box.w = this.bounds.w * this.scale;
87
+ this.box.h = height;
88
+ this.box.anchor = this.box.w / 2;
89
+ this.box.visualAxis = height / 2;
90
+ }
91
+ paint(painter) {
92
+ paintSymbol(painter, this.ast.definition.shapes, this.bounds, this.box, this.scale);
93
+ }
94
+ }
@@ -0,0 +1,15 @@
1
+ import type { PlaybackTransform } from "../../playback/types.js";
2
+ interface OrnamentPattern {
3
+ /** 调内音级偏移序列,0 表示本音 */
4
+ readonly body: readonly number[];
5
+ /** 主体是否重复到填满宿主时值 */
6
+ readonly repeat?: boolean;
7
+ }
8
+ /**
9
+ * 把一个音按固定音级模式展开成装饰音序列
10
+ *
11
+ * 密度按发声时的速度决定而不是固定的记谱时值:固定 1/8 四分音符在慢速下
12
+ * 每秒只有两三次,听起来根本不是颤音。默认速度下仍然恰好是 1/8 四分音符。
13
+ */
14
+ export declare function ornament(pattern: OrnamentPattern): PlaybackTransform;
15
+ export {};
@@ -0,0 +1,73 @@
1
+ /** 每个四分音符的名义子音数 */
2
+ const NOMINAL_RATE = 8;
3
+ /** 密度只在极端速度下偏离名义值:区间内 bpm 会在算式里精确抵消 */
4
+ const MIN_BPM = 60;
5
+ const MAX_BPM = 480;
6
+ /** 一个装饰音能展开的子音上限;防的是 `@tempo(0.01)` 这类手滑 */
7
+ const MAX_SUBNOTES = 256;
8
+ /**
9
+ * 把一个音按固定音级模式展开成装饰音序列
10
+ *
11
+ * 密度按发声时的速度决定而不是固定的记谱时值:固定 1/8 四分音符在慢速下
12
+ * 每秒只有两三次,听起来根本不是颤音。默认速度下仍然恰好是 1/8 四分音符。
13
+ */
14
+ export function ornament(pattern) {
15
+ return (context, owned) => {
16
+ const noteOffs = new Map();
17
+ for (const event of owned) {
18
+ if (event.kind === "note-off")
19
+ noteOffs.set(event.noteId, event);
20
+ }
21
+ const removed = new Set();
22
+ const added = [];
23
+ for (const note of owned) {
24
+ if (note.kind !== "note-on" || !note.transpose)
25
+ continue;
26
+ const off = noteOffs.get(note.noteId);
27
+ if (!off)
28
+ continue;
29
+ const duration = off.at.clone().sub(note.at);
30
+ const bpm = context.stateAt(note.at).effectiveBpm;
31
+ const steps = pattern.repeat ? repeatToFill(pattern.body, duration.toNumber(), bpm) : pattern.body;
32
+ const step = duration.div(steps.length);
33
+ removed.add(note);
34
+ removed.add(off);
35
+ for (let index = 0; index < steps.length; index++) {
36
+ const offset = steps[index];
37
+ const noteId = context.nextNoteId();
38
+ const at = note.at.clone().add(step.clone().mul(index));
39
+ const order = note.order + index / steps.length;
40
+ const on = {
41
+ ...note,
42
+ at,
43
+ noteId,
44
+ order,
45
+ // 本音保留主体的临时升降号,只有偏移音才走调内换算
46
+ midi: offset === 0 ? note.midi : note.transpose(offset),
47
+ // 后续 ornament 要从这个派生音继续移调,不能仍以最初的宿主为基准
48
+ transpose: delta => note.transpose(offset + delta),
49
+ origins: [...note.origins],
50
+ sourceSpans: note.sourceSpans.map(span => ({ ...span })),
51
+ };
52
+ added.push(on, {
53
+ ...off,
54
+ at: at.clone().add(step),
55
+ noteId,
56
+ order,
57
+ origins: [...off.origins],
58
+ });
59
+ }
60
+ }
61
+ return [...owned.filter(event => !removed.has(event)), ...added];
62
+ };
63
+ }
64
+ function repeatToFill(body, duration, bpm) {
65
+ // 名义密度与速度无关(秒数里的 bpm 与频率里的 bpm 恰好抵消),只在 bpm 越界时按比例修正
66
+ const ratio = Math.min(MAX_BPM, Math.max(MIN_BPM, bpm)) / bpm;
67
+ const limit = Math.floor(MAX_SUBNOTES / body.length);
68
+ const cycles = Math.min(limit, Math.max(1, Math.round(duration * NOMINAL_RATE * ratio / body.length)));
69
+ const result = [];
70
+ for (let i = 0; i < cycles; i++)
71
+ result.push(...body);
72
+ return result;
73
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * 符号自身坐标系与布局坐标系之间的换算
3
+ *
4
+ * `symbolBounds` 量出图元在符号自身坐标系里的固有包围盒,布局据此定 box;
5
+ * `paintSymbol` 反过来把同一批图元放到最终 box 上。两者必须读同一份 bounds,
6
+ * 否则墨迹会偏出框——符号原点不一定落在墨迹左上角。
7
+ */
8
+ import type { LayoutBox, Rect } from "../../layout/types.js";
9
+ import type { PaintStyle, Painter, PathCommand } from "../../render/types.js";
10
+ /** 符号的一个图元;坐标在符号自身坐标系里,缩放由布局决定 */
11
+ export interface SymbolShape {
12
+ readonly path?: readonly PathCommand[];
13
+ readonly circle?: {
14
+ readonly cx: number;
15
+ readonly cy: number;
16
+ readonly r: number;
17
+ };
18
+ readonly style: PaintStyle;
19
+ }
20
+ /** 全体图元的并集;宽高钳到正数,否则纯横线或纯竖线的符号会让缩放比例除零 */
21
+ export declare function symbolBounds(shapes: readonly SymbolShape[]): Rect;
22
+ export declare function paintSymbol(painter: Painter, shapes: readonly SymbolShape[], bounds: Rect, box: Readonly<LayoutBox>, scale: number): void;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * 符号自身坐标系与布局坐标系之间的换算
3
+ *
4
+ * `symbolBounds` 量出图元在符号自身坐标系里的固有包围盒,布局据此定 box;
5
+ * `paintSymbol` 反过来把同一批图元放到最终 box 上。两者必须读同一份 bounds,
6
+ * 否则墨迹会偏出框——符号原点不一定落在墨迹左上角。
7
+ */
8
+ import { pathBounds } from "../../layout/path.js";
9
+ /** 全体图元的并集;宽高钳到正数,否则纯横线或纯竖线的符号会让缩放比例除零 */
10
+ export function symbolBounds(shapes) {
11
+ let left = Infinity;
12
+ let top = Infinity;
13
+ let right = -Infinity;
14
+ let bottom = -Infinity;
15
+ const merge = (rect) => {
16
+ left = Math.min(left, rect.x);
17
+ top = Math.min(top, rect.y);
18
+ right = Math.max(right, rect.x + rect.w);
19
+ bottom = Math.max(bottom, rect.y + rect.h);
20
+ };
21
+ for (const shape of shapes) {
22
+ if (shape.circle) {
23
+ const { cx, cy, r } = shape.circle;
24
+ merge({ x: cx - r, y: cy - r, w: 2 * r, h: 2 * r });
25
+ }
26
+ if (shape.path?.length)
27
+ merge(pathBounds(shape.path));
28
+ }
29
+ return Number.isFinite(left)
30
+ ? { x: left, y: top, w: Math.max(1e-9, right - left), h: Math.max(1e-9, bottom - top) }
31
+ : { x: 0, y: 0, w: 1e-9, h: 1e-9 };
32
+ }
33
+ export function paintSymbol(painter, shapes, bounds, box, scale) {
34
+ const originX = box.x - bounds.x * scale;
35
+ const originY = box.y - bounds.y * scale;
36
+ for (const shape of shapes) {
37
+ const style = shape.style;
38
+ const scaled = style.strokeWidth === undefined
39
+ ? style
40
+ : { ...style, strokeWidth: style.strokeWidth * scale };
41
+ if (shape.path) {
42
+ painter.drawPath(shape.path, scaled, { x: originX, y: originY, scaleX: scale, scaleY: scale });
43
+ }
44
+ if (shape.circle) {
45
+ painter.drawCircle(originX + shape.circle.cx * scale, originY + shape.circle.cy * scale, shape.circle.r * scale, scaled);
46
+ }
47
+ }
48
+ }
@@ -0,0 +1,2 @@
1
+ import type { SymbolDefinition } from "../index.js";
2
+ export declare const accentSymbol: SymbolDefinition;
@@ -0,0 +1,24 @@
1
+ const ACCENT_BOOST = 20;
2
+ const MAX_VELOCITY = 127;
3
+ export const accentSymbol = {
4
+ name: "accent",
5
+ description: "重音",
6
+ weight: 0.5,
7
+ shapes: [
8
+ {
9
+ path: [
10
+ { op: "M", x: 0.21413, y: 0.4818 },
11
+ { op: "L", x: 8.21413, y: 3.72071 },
12
+ { op: "L", x: 0.29494, y: 6.92724 },
13
+ ],
14
+ style: { stroke: "#000", strokeWidth: 1 },
15
+ },
16
+ ],
17
+ emitPlayback: emitter => emitter.affectFollowing((_context, events) => {
18
+ for (const event of events) {
19
+ if (event.kind === "note-on") {
20
+ event.velocity = Math.min(MAX_VELOCITY, event.velocity + ACCENT_BOOST);
21
+ }
22
+ }
23
+ }),
24
+ };
@@ -0,0 +1,2 @@
1
+ import type { SymbolDefinition } from "../index.js";
2
+ export declare const fSymbol: SymbolDefinition;
@@ -0,0 +1,55 @@
1
+ const FORTE_VELOCITY = 96;
2
+ export const fSymbol = {
3
+ name: "f",
4
+ description: "强",
5
+ shapes: [
6
+ {
7
+ path: [
8
+ { op: "M", x: 6.805, y: 3.24553 },
9
+ { op: "L", x: 8.5437, y: 3.24553 },
10
+ { op: "L", x: 8.5437, y: 3.89757 },
11
+ { op: "L", x: 6.60698, y: 3.89757 },
12
+ { op: "C", cx1: 6.05169, cy1: 5.7957, cx2: 5.56385, cy2: 7.20105, x: 5.14365, y: 8.11383 },
13
+ { op: "C", cx1: 4.83447, cy1: 8.76586, cx2: 4.53022, cy2: 9.3261, x: 4.23564, y: 9.79455 },
14
+ { op: "C", cx1: 3.94108, cy1: 10.26319, cx2: 3.57392, cy2: 10.664, x: 3.13455, y: 10.99733 },
15
+ { op: "C", cx1: 2.69497, cy1: 11.33542, cx2: 2.17827, cy2: 11.53344, x: 1.58419, y: 11.59615 },
16
+ { op: "C", cx1: 1.18814, cy1: 11.59615, cx2: 0.82592, cy2: 11.46084, x: 0.49258, y: 11.19535 },
17
+ { op: "C", cx1: 0.16419, cy1: 10.92968, cx2: 0, cy2: 10.59161, x: 0, y: 10.17141 },
18
+ { op: "C", cx1: 0, cy1: 9.90098, cx2: 0.07735, cy2: 9.64006, x: 0.2366, y: 9.38407 },
19
+ { op: "C", cx1: 0.39605, cy1: 9.12808, cx2: 0.65204, cy2: 8.97831, x: 1.00951, y: 8.92531 },
20
+ { op: "C", cx1: 1.24611, cy1: 8.92531, cx2: 1.44887, cy2: 9.00266, x: 1.62771, y: 9.15716 },
21
+ { op: "C", cx1: 1.80634, cy1: 9.31643, cx2: 1.89813, cy2: 9.50494, x: 1.89813, y: 9.73186 },
22
+ { op: "C", cx1: 1.89813, cy1: 9.88637, cx2: 1.85936, cy2: 10.01197, x: 1.78695, y: 10.10852 },
23
+ { op: "C", cx1: 1.71455, cy1: 10.21001, cx2: 1.59387, cy2: 10.33066, x: 1.43443, y: 10.48043 },
24
+ { op: "C", cx1: 1.27497, cy1: 10.63017, cx2: 1.16874, cy2: 10.76055, x: 1.12521, y: 10.86203 },
25
+ { op: "C", cx1: 1.15905, cy1: 10.9924, cx2: 1.23639, cy2: 11.06006, x: 1.34757, y: 11.06006 },
26
+ { op: "C", cx1: 1.74837, cy1: 11.06006, cx2: 2.10584, cy2: 10.81377, x: 2.4196, y: 10.32118 },
27
+ { op: "C", cx1: 2.73355, cy1: 9.83335, cx2: 2.99447, cy2: 9.26339, x: 3.2022, y: 8.61136 },
28
+ { op: "C", cx1: 3.40971, cy1: 7.95932, cx2: 3.64651, cy2: 7.10927, x: 3.92168, y: 6.06119 },
29
+ { op: "C", cx1: 4.19706, cy1: 5.00836, cx2: 4.40952, cy2: 4.28867, x: 4.54959, y: 3.89757 },
30
+ { op: "L", x: 3.24552, y: 3.89757 },
31
+ { op: "L", x: 3.24552, y: 3.24553 },
32
+ { op: "L", x: 4.82496, y: 3.24553 },
33
+ { op: "C", cx1: 5.05187, cy1: 2.68054, cx2: 5.36582, cy2: 2.15393, x: 5.77156, y: 1.6566 },
34
+ { op: "C", cx1: 6.18204, cy1: 1.15907, cx2: 6.65546, cy2: 0.76302, x: 7.19155, y: 0.45876 },
35
+ { op: "C", cx1: 7.72766, cy1: 0.1545, cx2: 8.29265, cy2: 0, x: 8.88176, y: 0 },
36
+ { op: "C", cx1: 9.36486, cy1: 0, cx2: 9.67384, cy2: 0.14975, x: 9.85268, y: 0.34777 },
37
+ { op: "C", cx1: 10.03131, cy1: 0.5458, cx2: 10.14727, cy2: 0.83541, x: 10.20998, y: 0.97073 },
38
+ { op: "C", cx1: 10.20998, cy1: 1.3282, cx2: 10.10374, cy2: 1.68548, x: 9.89602, y: 2.038 },
39
+ { op: "C", cx1: 9.68851, cy1: 2.39072, cx2: 9.38899, cy2: 2.57905, x: 9.00264, y: 2.59349 },
40
+ { op: "C", cx1: 8.9544, cy1: 2.54522, cx2: 8.86733, cy2: 2.49695, x: 8.74666, y: 2.44373 },
41
+ { op: "C", cx1: 8.51006, cy1: 2.33275, cx2: 8.3892, cy2: 2.24571, x: 8.35061, y: 1.94145 },
42
+ { op: "C", cx1: 8.39412, cy1: 1.49714, cx2: 8.59689, cy2: 1.19762, x: 8.95912, y: 1.04807 },
43
+ { op: "C", cx1: 9.19573, cy1: 0.9901, cx2: 9.3166, cy2: 0.89831, x: 9.3166, y: 0.77269 },
44
+ { op: "C", cx1: 9.3166, cy1: 0.5988, cx2: 9.17159, cy2: 0.49257, x: 8.88177, y: 0.45399 },
45
+ { op: "C", cx1: 8.39416, cy1: 0.45399, cx2: 7.95931, cy2: 0.79208, x: 7.57793, y: 1.4633 },
46
+ { op: "C", cx1: 7.32192, cy1: 1.84491, cx2: 7.06593, cy2: 2.43897, x: 6.805, y: 3.24551 },
47
+ { op: "L", x: 6.805, y: 3.24551 },
48
+ { op: "L", x: 6.805, y: 3.24553 },
49
+ { op: "Z" },
50
+ ],
51
+ style: { fill: "#000" },
52
+ },
53
+ ],
54
+ onTimeState: state => { state.velocity = FORTE_VELOCITY; },
55
+ };
@@ -0,0 +1,2 @@
1
+ import type { SymbolDefinition } from "../index.js";
2
+ export declare const fermataSymbol: SymbolDefinition;
@@ -0,0 +1,24 @@
1
+ export const fermataSymbol = {
2
+ name: "fermata",
3
+ description: "延长记号:覆盖区间速度减半,目标音实际时长翻倍",
4
+ weight: 0.6,
5
+ shapes: [
6
+ {
7
+ path: [
8
+ { op: "M", x: 0, y: 7.46666 },
9
+ { op: "C", cx1: 3.65834, cy1: 0, cx2: 8.53611, cy2: 0, x: 12.19445, y: 7.46666 },
10
+ { op: "M", x: 12.19445, y: 7.46666 },
11
+ { op: "C", cx1: 8.53611, cy1: 0.74666, cx2: 3.65833, cy2: 0.74666, x: 0, y: 7.46666 },
12
+ ],
13
+ style: { fill: "#000", stroke: "#000", strokeWidth: 1 },
14
+ },
15
+ {
16
+ circle: { cx: 6.09723, cy: 6.26666, r: 1.2 },
17
+ style: { fill: "#000" },
18
+ },
19
+ ],
20
+ emitPlayback: emitter => {
21
+ emitter.control(emitter.start, state => state.bpmScale.div(2));
22
+ emitter.control(emitter.end, state => state.bpmScale.mul(2));
23
+ },
24
+ };
@@ -0,0 +1,2 @@
1
+ import type { SymbolDefinition } from "../index.js";
2
+ export declare const mordentSymbol: SymbolDefinition;