jpfun 0.6.0 → 0.8.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.
- package/Bravura-LICENSE.txt +93 -0
- package/README.md +49 -146
- package/dist/Bravura-LICENSE.txt +93 -0
- package/dist/converter/midi/convert.d.ts +3 -11
- package/dist/converter/midi/convert.js +89 -67
- package/dist/converter/musicxml/features.d.ts +34 -12
- package/dist/converter/musicxml/features.js +131 -24
- package/dist/converter/musicxml/index.js +221 -54
- package/dist/converter/musicxml/model.d.ts +24 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/diagnostic.d.ts +7 -1
- package/dist/diagnostic.js +40 -88
- package/dist/fraction.js +3 -2
- package/dist/functions/ASTtypes.d.ts +2 -18
- package/dist/functions/ASTtypes.js +0 -16
- package/dist/functions/arpeggio/index.js +17 -22
- package/dist/functions/arpeggio/shape.d.ts +1 -1
- package/dist/functions/arpeggio/shape.js +27 -21
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +139 -36
- package/dist/functions/beam/auto.d.ts +1 -2
- package/dist/functions/beam/auto.js +44 -27
- package/dist/functions/beam/index.js +2 -3
- package/dist/functions/beam/layout.d.ts +1 -1
- package/dist/functions/beam/layout.js +16 -24
- package/dist/functions/br/index.d.ts +34 -2
- package/dist/functions/br/index.js +8 -8
- package/dist/functions/dash/index.js +6 -6
- package/dist/functions/default.js +10 -5
- package/dist/functions/div/index.d.ts +1 -0
- package/dist/functions/div/index.js +8 -4
- package/dist/functions/dot/index.js +2 -2
- package/dist/functions/dyn/index.js +49 -31
- package/dist/functions/gliss/index.d.ts +2 -0
- package/dist/functions/gliss/index.js +202 -0
- package/dist/functions/grace/index.d.ts +1 -7
- package/dist/functions/grace/index.js +11 -19
- package/dist/functions/head/index.js +41 -60
- package/dist/functions/key/index.js +8 -10
- package/dist/functions/meter/index.d.ts +56 -1
- package/dist/functions/meter/index.js +30 -24
- package/dist/functions/note/accidentals.js +10 -16
- package/dist/functions/note/index.js +18 -8
- package/dist/functions/note/noteName.d.ts +16 -0
- package/dist/functions/note/noteName.js +88 -0
- package/dist/functions/note/source-transform.js +6 -5
- package/dist/functions/ottava/index.d.ts +2 -0
- package/dist/functions/ottava/index.js +233 -0
- package/dist/functions/page/index.d.ts +1 -0
- package/dist/functions/page/index.js +22 -13
- package/dist/functions/pagebreak/index.d.ts +2 -0
- package/dist/functions/pagebreak/index.js +36 -0
- package/dist/functions/rest/index.js +15 -15
- package/dist/functions/set/index.js +4 -4
- package/dist/functions/slide/index.d.ts +2 -0
- package/dist/functions/slide/index.js +132 -0
- package/dist/functions/stack/index.js +8 -15
- package/dist/functions/symbol/index.d.ts +30 -2
- package/dist/functions/symbol/index.js +93 -19
- package/dist/functions/symbol/ornament.d.ts +1 -2
- package/dist/functions/symbol/ornament.js +14 -19
- package/dist/functions/symbol/shape.d.ts +6 -0
- package/dist/functions/symbol/shape.js +20 -4
- package/dist/functions/symbol/symbols/coda.d.ts +4 -0
- package/dist/functions/symbol/symbols/coda.js +56 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
- package/dist/functions/symbol/symbols/dynamics.js +18 -8
- package/dist/functions/symbol/symbols/fine.js +1 -1
- package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
- package/dist/functions/symbol/symbols/harmonic.js +11 -0
- package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
- package/dist/functions/symbol/symbols/marcato.js +22 -0
- package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
- package/dist/functions/symbol/symbols/staccato.js +24 -0
- package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
- package/dist/functions/symbol/symbols/tenuto.js +11 -0
- package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
- package/dist/functions/symbol/symbols/tocoda.js +16 -0
- package/dist/functions/symbol/symbols/tr.js +46 -1
- package/dist/functions/symbol/symbols/turn.d.ts +3 -0
- package/dist/functions/symbol/symbols/turn.js +50 -0
- package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
- package/dist/functions/symbol/symbols/wheel.js +36 -0
- package/dist/functions/temporal.d.ts +17 -6
- package/dist/functions/temporal.js +17 -6
- package/dist/functions/text/index.js +15 -6
- package/dist/functions/tie/index.js +64 -30
- package/dist/functions/tremolo/index.d.ts +2 -0
- package/dist/functions/tremolo/index.js +319 -0
- package/dist/functions/tuplet/index.js +4 -8
- package/dist/functions/up/index.js +7 -9
- package/dist/functions/voice/connections.js +7 -5
- package/dist/functions/voice/index.js +19 -11
- package/dist/functions/volta/index.js +41 -21
- package/dist/jpfun.core.min.js +64 -80
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +69 -85
- package/dist/layout/default.d.ts +0 -1
- package/dist/layout/default.js +0 -1
- package/dist/layout/engine.d.ts +1 -0
- package/dist/layout/engine.js +42 -11
- package/dist/layout/page.d.ts +3 -2
- package/dist/layout/page.js +6 -5
- package/dist/layout/types.d.ts +4 -0
- package/dist/lowering/loweringContext.d.ts +11 -13
- package/dist/lowering/loweringContext.js +30 -197
- package/dist/lowering/types.d.ts +1 -3
- package/dist/parser/parse-utils/note-utils.d.ts +1 -1
- package/dist/parser/parse-utils/note-utils.js +8 -9
- package/dist/parser/parse-utils/string-utils.d.ts +3 -2
- package/dist/parser/parse-utils/string-utils.js +16 -3
- package/dist/parser/parserContext.d.ts +2 -0
- package/dist/parser/parserContext.js +36 -8
- package/dist/pipeline.js +0 -1
- package/dist/playback/compile.js +131 -72
- package/dist/playback/event.d.ts +1 -1
- package/dist/playback/event.js +9 -9
- package/dist/playback/time.js +42 -31
- package/dist/playback/types.d.ts +7 -3
- package/dist/render/canvas.d.ts +2 -0
- package/dist/render/canvas.js +25 -22
- package/dist/render/recording.js +1 -4
- package/dist/render/svg.js +1 -0
- package/dist/render/text.d.ts +1 -1
- package/dist/render/text.js +3 -2
- package/dist/render/translate.d.ts +0 -1
- package/dist/render/translate.js +6 -23
- package/dist/render/types.d.ts +1 -0
- package/grammar.md +52 -25
- package/package.json +3 -3
- package/dist/functions/note/noteNameFSM.d.ts +0 -8
- package/dist/functions/note/noteNameFSM.js +0 -181
|
@@ -1,26 +1,38 @@
|
|
|
1
1
|
import { type TimeState } from "../temporal.js";
|
|
2
2
|
import type { PlaybackCursor, PlaybackEmitter, PlaybackFlowAction } from "../../playback/types.js";
|
|
3
|
+
import type { TextStyle } from "../../render/types.js";
|
|
3
4
|
import { type ASTFunctionClass } from "../ASTtypes.js";
|
|
4
|
-
import {
|
|
5
|
+
import { ornament } from "./ornament.js";
|
|
6
|
+
import { symbolBounds, type SymbolShape } from "./shape.js";
|
|
7
|
+
export type { OrnamentPattern } from "./ornament.js";
|
|
8
|
+
export type { SymbolShape };
|
|
5
9
|
/**
|
|
6
10
|
* 一个乐谱符号:固定图形加可选的播放语义
|
|
7
11
|
*
|
|
8
|
-
*
|
|
12
|
+
* 每个符号或符号族自成一个文件,新增符号就是新增对象字面量,可复用已有图形和播放能力
|
|
9
13
|
*/
|
|
10
14
|
export interface SymbolDefinition {
|
|
11
15
|
readonly name: string;
|
|
12
16
|
readonly description: string;
|
|
17
|
+
/** 查表未命中时使用;忽略大小写,从名称开头匹配,不允许 g/y */
|
|
18
|
+
readonly pattern?: {
|
|
19
|
+
readonly regex: RegExp;
|
|
20
|
+
readonly resolve: (match: RegExpExecArray) => SymbolDefinition;
|
|
21
|
+
};
|
|
13
22
|
/** 固定图形;文字记号改用 text */
|
|
14
23
|
readonly shapes?: readonly SymbolShape[];
|
|
15
24
|
/** 交给宿主字体绘制的文字记号,与 shapes 二选一 */
|
|
16
25
|
readonly text?: {
|
|
17
26
|
readonly content: string;
|
|
18
27
|
readonly fontFamily: string;
|
|
28
|
+
readonly fontStyle?: TextStyle["fontStyle"];
|
|
19
29
|
};
|
|
20
30
|
/** 符号高度 = size × weight,用来修正宽扁字形的视觉重量;默认 1 */
|
|
21
31
|
readonly weight?: number;
|
|
22
32
|
/** 图形下方留白,使用符号自身坐标并随 scale 缩放;默认 0 */
|
|
23
33
|
readonly paddingBottom?: number;
|
|
34
|
+
/** 横向对齐点,使用符号自身坐标;默认采用完整图形的中心 */
|
|
35
|
+
readonly anchor?: number;
|
|
24
36
|
/** 写入本位置之后持续生效的状态,例如力度记号的 velocity */
|
|
25
37
|
readonly onTimeState?: (state: TimeState) => void;
|
|
26
38
|
/** 发布系统控制,或注册对同一折叠序列后续音符的局部变换 */
|
|
@@ -30,4 +42,20 @@ export interface SymbolDefinition {
|
|
|
30
42
|
/** 播放到达本符号所在列时决定去留;符号恒占一列,所以生效范围不由符号自己声明 */
|
|
31
43
|
readonly onVisit?: (cursor: PlaybackCursor, at: number) => PlaybackFlowAction | undefined;
|
|
32
44
|
}
|
|
45
|
+
export declare const symbols: Readonly<{
|
|
46
|
+
/** 内置符号的只读备份列表 */
|
|
47
|
+
core: readonly SymbolDefinition[];
|
|
48
|
+
/** 当前注册表的只读视图 */
|
|
49
|
+
table: ReadonlyMap<string, SymbolDefinition>;
|
|
50
|
+
/** 注册或同名覆盖一个或多个符号定义 */
|
|
51
|
+
register: (...definitions: readonly SymbolDefinition[]) => void;
|
|
52
|
+
/** 清空所有已注册符号(含内置符号) */
|
|
53
|
+
clear: () => void;
|
|
54
|
+
/** 清空并恢复到最初的 symbols.core 状态 */
|
|
55
|
+
reset: () => void;
|
|
56
|
+
/** 装饰音播放展开工具 */
|
|
57
|
+
ornament: typeof ornament;
|
|
58
|
+
/** 计算符号图元在自身坐标系中的固有包围盒 */
|
|
59
|
+
bounds: typeof symbolBounds;
|
|
60
|
+
}>;
|
|
33
61
|
export declare const SymbolNode: ASTFunctionClass;
|
|
@@ -1,37 +1,95 @@
|
|
|
1
1
|
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
|
|
3
3
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
|
+
import { ornament } from "./ornament.js";
|
|
4
5
|
import { paintSymbol, symbolBounds } from "./shape.js";
|
|
5
6
|
import { accentSymbol } from "./symbols/accent.js";
|
|
7
|
+
import { codaSymbol } from "./symbols/coda.js";
|
|
6
8
|
import { dcSymbol } from "./symbols/dc.js";
|
|
7
9
|
import { dsSymbol } from "./symbols/ds.js";
|
|
8
10
|
import { dynamicSymbols } from "./symbols/dynamics.js";
|
|
9
11
|
import { fermataSymbol } from "./symbols/fermata.js";
|
|
10
12
|
import { fineSymbol } from "./symbols/fine.js";
|
|
13
|
+
import { harmonicSymbol } from "./symbols/harmonic.js";
|
|
14
|
+
import { marcatoSymbol } from "./symbols/marcato.js";
|
|
11
15
|
import { mordentSymbol } from "./symbols/mordent.js";
|
|
12
16
|
import { prallSymbol } from "./symbols/prall.js";
|
|
13
17
|
import { segnoSymbol } from "./symbols/segno.js";
|
|
18
|
+
import { staccatoSymbol } from "./symbols/staccato.js";
|
|
19
|
+
import { tenutoSymbol } from "./symbols/tenuto.js";
|
|
20
|
+
import { tocodaSymbol } from "./symbols/tocoda.js";
|
|
14
21
|
import { trSymbol } from "./symbols/tr.js";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
import { iturnSymbol, turnSymbol } from "./symbols/turn.js";
|
|
23
|
+
import { wheelSymbol } from "./symbols/wheel.js";
|
|
24
|
+
const symbolTable = new Map();
|
|
25
|
+
const symbolPatterns = new Map();
|
|
26
|
+
export const symbols = Object.freeze({
|
|
27
|
+
/** 内置符号的只读备份列表 */
|
|
28
|
+
core: Object.freeze([
|
|
29
|
+
trSymbol, fermataSymbol, prallSymbol, mordentSymbol, accentSymbol, wheelSymbol, harmonicSymbol,
|
|
30
|
+
tenutoSymbol, marcatoSymbol, staccatoSymbol, turnSymbol, iturnSymbol,
|
|
31
|
+
...dynamicSymbols, dcSymbol, dsSymbol, segnoSymbol, fineSymbol, codaSymbol, tocodaSymbol,
|
|
32
|
+
]),
|
|
33
|
+
/** 当前注册表的只读视图 */
|
|
34
|
+
table: symbolTable,
|
|
35
|
+
/** 注册或同名覆盖一个或多个符号定义 */
|
|
36
|
+
register: (...definitions) => {
|
|
37
|
+
const entries = definitions.map(definition => {
|
|
38
|
+
const name = definition.name.toLowerCase();
|
|
39
|
+
let pattern = definition.pattern;
|
|
40
|
+
if (pattern) {
|
|
41
|
+
const { regex } = pattern;
|
|
42
|
+
if (regex.global || regex.sticky) {
|
|
43
|
+
throw new Error(`Symbol pattern must not use g or y flags: ${name}`);
|
|
44
|
+
}
|
|
45
|
+
if (!regex.ignoreCase)
|
|
46
|
+
pattern = { ...pattern, regex: new RegExp(regex.source, `${regex.flags}i`) };
|
|
47
|
+
}
|
|
48
|
+
return { name, definition, pattern };
|
|
49
|
+
});
|
|
50
|
+
for (const { name, definition, pattern } of entries) {
|
|
51
|
+
symbolTable.set(name, definition);
|
|
52
|
+
if (pattern)
|
|
53
|
+
symbolPatterns.set(name, pattern);
|
|
54
|
+
else
|
|
55
|
+
symbolPatterns.delete(name);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
/** 清空所有已注册符号(含内置符号) */
|
|
59
|
+
clear: () => {
|
|
60
|
+
symbolTable.clear();
|
|
61
|
+
symbolPatterns.clear();
|
|
62
|
+
},
|
|
63
|
+
/** 清空并恢复到最初的 symbols.core 状态 */
|
|
64
|
+
reset: () => {
|
|
65
|
+
symbols.clear();
|
|
66
|
+
symbols.register(...symbols.core);
|
|
67
|
+
},
|
|
68
|
+
/** 装饰音播放展开工具 */
|
|
69
|
+
ornament,
|
|
70
|
+
/** 计算符号图元在自身坐标系中的固有包围盒 */
|
|
71
|
+
bounds: symbolBounds,
|
|
72
|
+
});
|
|
73
|
+
symbols.reset();
|
|
18
74
|
class SymbolFunction extends ASTFunctionNode {
|
|
19
75
|
static def = {
|
|
20
76
|
name: ["symbol", "$"],
|
|
21
77
|
description: "创建一个乐谱符号",
|
|
22
|
-
details
|
|
78
|
+
get details() {
|
|
79
|
+
return `\
|
|
23
80
|
~~~jpfun
|
|
24
81
|
1 ^ @symbol(p, size=0.5em)
|
|
25
82
|
~~~
|
|
26
|
-
**简写**:\`$name\`,如 \`1 ^ $p
|
|
83
|
+
**简写**:\`$name\`,如 \`1 ^ $p\`。名称读取到下一个空白或当前内容区间末尾,连续的标点也属于名称。
|
|
27
84
|
|
|
28
|
-
|
|
29
|
-
${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.description}`).join("\n")}
|
|
85
|
+
**已注册记号**
|
|
86
|
+
${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.description}`).join("\n")}`;
|
|
87
|
+
},
|
|
30
88
|
allowExtraArgs: false,
|
|
31
89
|
args: [
|
|
32
90
|
{
|
|
33
91
|
name: "name",
|
|
34
|
-
description: "
|
|
92
|
+
description: "已注册记号名称,见函数说明中的记号列表",
|
|
35
93
|
type: "string",
|
|
36
94
|
default: null
|
|
37
95
|
},
|
|
@@ -44,12 +102,12 @@ ${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.descriptio
|
|
|
44
102
|
],
|
|
45
103
|
};
|
|
46
104
|
static deSugarAtom(source, start, end) {
|
|
47
|
-
if (source[start] !== "$"
|
|
105
|
+
if (source[start] !== "$")
|
|
106
|
+
return null;
|
|
107
|
+
const name = /^\S+/.exec(source.slice(start + 1, end))?.[0];
|
|
108
|
+
if (!name)
|
|
48
109
|
return null;
|
|
49
|
-
|
|
50
|
-
while (next < end && /[A-Za-z0-9_-]/.test(source[next]))
|
|
51
|
-
next++;
|
|
52
|
-
const name = source.slice(start + 1, next);
|
|
110
|
+
const next = start + 1 + name.length;
|
|
53
111
|
const node = {
|
|
54
112
|
kind: "call",
|
|
55
113
|
typed: true,
|
|
@@ -66,12 +124,26 @@ ${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.descriptio
|
|
|
66
124
|
super(sourceSpan, parent);
|
|
67
125
|
const [name, size] = this.getArgValue(args, ctx);
|
|
68
126
|
this.size = ctx.length2px(size);
|
|
69
|
-
const
|
|
127
|
+
const normalized = name.toLowerCase();
|
|
128
|
+
let definition = symbolTable.get(normalized);
|
|
129
|
+
if (!definition) {
|
|
130
|
+
let matched;
|
|
131
|
+
for (const pattern of symbolPatterns.values()) {
|
|
132
|
+
const match = pattern.regex.exec(normalized);
|
|
133
|
+
if (!match || match.index !== 0 || match[0] !== normalized)
|
|
134
|
+
continue;
|
|
135
|
+
if (matched)
|
|
136
|
+
throw new Error(`Ambiguous symbol patterns: ${normalized}`);
|
|
137
|
+
matched = { pattern, match };
|
|
138
|
+
}
|
|
139
|
+
definition = matched?.pattern.resolve(matched.match);
|
|
140
|
+
}
|
|
70
141
|
if (!definition) {
|
|
71
|
-
const message =
|
|
142
|
+
const message = "@symbol: 未知符号: ${name}";
|
|
143
|
+
const params = { name };
|
|
72
144
|
if (ctx.variables.strict)
|
|
73
|
-
throw new ErrorDiagnostic("E_UNKNOWN_SYMBOL", message, sourceSpan);
|
|
74
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_UNKNOWN_SYMBOL", message, sourceSpan));
|
|
145
|
+
throw new ErrorDiagnostic("E_UNKNOWN_SYMBOL", message, sourceSpan, params);
|
|
146
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_UNKNOWN_SYMBOL", message, sourceSpan, params));
|
|
75
147
|
return;
|
|
76
148
|
}
|
|
77
149
|
this.definition = definition;
|
|
@@ -107,7 +179,7 @@ class SymbolTemporal extends TemporalNodeBase {
|
|
|
107
179
|
// 文字记号的字号也是 size × weight,所以与图形记号视觉等高
|
|
108
180
|
this.label = text && {
|
|
109
181
|
content: text.content,
|
|
110
|
-
style: { fontSize: this.height, fontFamily: text.fontFamily },
|
|
182
|
+
style: { fontSize: this.height, fontFamily: text.fontFamily, fontStyle: text.fontStyle },
|
|
111
183
|
};
|
|
112
184
|
this.initLayoutBox();
|
|
113
185
|
}
|
|
@@ -123,7 +195,9 @@ class SymbolTemporal extends TemporalNodeBase {
|
|
|
123
195
|
this.box.w = this.bounds.w * this.scale;
|
|
124
196
|
this.box.h = this.height + (this.ast.definition.paddingBottom ?? 0) * this.scale;
|
|
125
197
|
}
|
|
126
|
-
this.box.anchor = this.
|
|
198
|
+
this.box.anchor = !this.label && this.ast.definition.anchor !== undefined
|
|
199
|
+
? (this.ast.definition.anchor - this.bounds.x) * this.scale
|
|
200
|
+
: this.box.w / 2;
|
|
127
201
|
this.box.visualAxis = this.box.h / 2;
|
|
128
202
|
}
|
|
129
203
|
paint(painter) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PlaybackTransform } from "../../playback/types.js";
|
|
2
|
-
interface OrnamentPattern {
|
|
2
|
+
export interface OrnamentPattern {
|
|
3
3
|
/** 调内音级偏移序列,0 表示本音 */
|
|
4
4
|
readonly body: readonly number[];
|
|
5
5
|
/** 主体是否重复到填满宿主时值 */
|
|
@@ -12,4 +12,3 @@ interface OrnamentPattern {
|
|
|
12
12
|
* 派生音段继承轨道和来源,仍只声明音段,不直接发布 NoteOn/NoteOff。
|
|
13
13
|
*/
|
|
14
14
|
export declare function ornament(pattern: OrnamentPattern): PlaybackTransform;
|
|
15
|
-
export {};
|
|
@@ -22,13 +22,21 @@ export function ornament(pattern) {
|
|
|
22
22
|
continue;
|
|
23
23
|
}
|
|
24
24
|
const duration = note.end.clone().sub(note.start);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
let count = pattern.body.length;
|
|
26
|
+
if (pattern.repeat) {
|
|
27
|
+
// 此处读取最终速度,结构延长和其他声部的速度效果都已计入。
|
|
28
|
+
const bpm = context.stateAt(note.start).effectiveBpm;
|
|
29
|
+
// 名义密度与速度无关(秒数里的 bpm 与频率里的 bpm 恰好抵消),只在 bpm 越界时按比例修正
|
|
30
|
+
const ratio = Math.min(MAX_BPM, Math.max(MIN_BPM, bpm)) / bpm;
|
|
31
|
+
// 预算按完整音型计算,避免在达到子音上限时留下半个音型。
|
|
32
|
+
const limit = Math.floor(MAX_SUBNOTES / pattern.body.length);
|
|
33
|
+
const cycles = Math.min(limit, Math.max(1, Math.round(duration.toNumber() * NOMINAL_RATE * ratio / pattern.body.length)));
|
|
34
|
+
count *= cycles;
|
|
35
|
+
}
|
|
36
|
+
const step = duration.div(count);
|
|
29
37
|
// 用 Fraction 等分最终区间,保证首尾边界准确且不会积累浮点时间误差。
|
|
30
|
-
for (let index = 0; index <
|
|
31
|
-
const offset =
|
|
38
|
+
for (let index = 0; index < count; index++) {
|
|
39
|
+
const offset = pattern.body[index % pattern.body.length];
|
|
32
40
|
const start = step.clone().mul(index).add(note.start);
|
|
33
41
|
result.push({
|
|
34
42
|
...note,
|
|
@@ -44,16 +52,3 @@ export function ornament(pattern) {
|
|
|
44
52
|
return result;
|
|
45
53
|
};
|
|
46
54
|
}
|
|
47
|
-
/** 按最终速度计算完整音型的重复次数,并限制总子音数 */
|
|
48
|
-
function repeatToFill(body, duration, bpm) {
|
|
49
|
-
// 名义密度与速度无关(秒数里的 bpm 与频率里的 bpm 恰好抵消),只在 bpm 越界时按比例修正
|
|
50
|
-
const ratio = Math.min(MAX_BPM, Math.max(MIN_BPM, bpm)) / bpm;
|
|
51
|
-
// 预算按完整音型计算,避免在达到子音上限时留下半个音型。
|
|
52
|
-
const limit = Math.floor(MAX_SUBNOTES / body.length);
|
|
53
|
-
const cycles = Math.min(limit, Math.max(1, Math.round(duration * NOMINAL_RATE * ratio / body.length)));
|
|
54
|
-
const result = [];
|
|
55
|
-
// 先得到完整音级序列,调用方再按实际子音总数统一分配宿主时值。
|
|
56
|
-
for (let i = 0; i < cycles; i++)
|
|
57
|
-
result.push(...body);
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
@@ -10,6 +10,12 @@ import type { PaintStyle, Painter, PathCommand } from "../../render/types.js";
|
|
|
10
10
|
/** 符号的一个图元;坐标在符号自身坐标系里,缩放由布局决定 */
|
|
11
11
|
export interface SymbolShape {
|
|
12
12
|
readonly path?: readonly PathCommand[];
|
|
13
|
+
readonly line?: {
|
|
14
|
+
readonly x1: number;
|
|
15
|
+
readonly y1: number;
|
|
16
|
+
readonly x2: number;
|
|
17
|
+
readonly y2: number;
|
|
18
|
+
};
|
|
13
19
|
readonly circle?: {
|
|
14
20
|
readonly cx: number;
|
|
15
21
|
readonly cy: number;
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
* 否则墨迹会偏出框——符号原点不一定落在墨迹左上角。
|
|
7
7
|
*/
|
|
8
8
|
import { pathBounds } from "../../layout/path.js";
|
|
9
|
+
// 固定字形路径不可原地修改;共享路径的重复片段只求一次曲线极值
|
|
10
|
+
const pathBoundsCache = new WeakMap();
|
|
9
11
|
/** 全体图元的并集;宽高钳到正数,否则纯横线或纯竖线的符号会让缩放比例除零 */
|
|
10
12
|
export function symbolBounds(shapes) {
|
|
11
13
|
let left = Infinity;
|
|
@@ -20,12 +22,21 @@ export function symbolBounds(shapes) {
|
|
|
20
22
|
};
|
|
21
23
|
for (const shape of shapes) {
|
|
22
24
|
const dx = shape.dx ?? 0;
|
|
25
|
+
if (shape.line) {
|
|
26
|
+
const { x1, y1, x2, y2 } = shape.line;
|
|
27
|
+
const halfStroke = (shape.style.strokeWidth ?? 1) / 2;
|
|
28
|
+
merge(Math.min(x1, x2) + dx - halfStroke, Math.min(y1, y2) - halfStroke, Math.abs(x2 - x1) + halfStroke * 2, Math.abs(y2 - y1) + halfStroke * 2);
|
|
29
|
+
}
|
|
23
30
|
if (shape.circle) {
|
|
24
31
|
const { cx, cy, r } = shape.circle;
|
|
25
|
-
|
|
32
|
+
const stroke = shape.style.stroke && shape.style.stroke !== "none"
|
|
33
|
+
? (shape.style.strokeWidth ?? 1) / 2 : 0;
|
|
34
|
+
const outer = r + stroke;
|
|
35
|
+
merge(cx + dx - outer, cy - outer, 2 * outer, 2 * outer);
|
|
26
36
|
}
|
|
27
37
|
if (shape.path?.length) {
|
|
28
|
-
const rect = pathBounds(shape.path);
|
|
38
|
+
const rect = pathBoundsCache.get(shape.path) ?? pathBounds(shape.path);
|
|
39
|
+
pathBoundsCache.set(shape.path, rect);
|
|
29
40
|
merge(rect.x + dx, rect.y, rect.w, rect.h);
|
|
30
41
|
}
|
|
31
42
|
}
|
|
@@ -38,10 +49,15 @@ export function paintSymbol(painter, shapes, bounds, box, scale) {
|
|
|
38
49
|
const originY = box.y - bounds.y * scale;
|
|
39
50
|
for (const shape of shapes) {
|
|
40
51
|
const style = shape.style;
|
|
41
|
-
const
|
|
52
|
+
const strokeWidth = style.strokeWidth ?? (shape.line || shape.circle ? 1 : undefined);
|
|
53
|
+
const scaled = strokeWidth === undefined
|
|
42
54
|
? style
|
|
43
|
-
: { ...style, strokeWidth:
|
|
55
|
+
: { ...style, strokeWidth: strokeWidth * scale };
|
|
44
56
|
const x = originX + (shape.dx ?? 0) * scale;
|
|
57
|
+
if (shape.line) {
|
|
58
|
+
const { x1, y1, x2, y2 } = shape.line;
|
|
59
|
+
painter.drawLine(x + x1 * scale, originY + y1 * scale, x + x2 * scale, originY + y2 * scale, scaled);
|
|
60
|
+
}
|
|
45
61
|
if (shape.path) {
|
|
46
62
|
painter.drawPath(shape.path, scaled, { x, y: originY, scaleX: scale, scaleY: scale });
|
|
47
63
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** $tocoda 的跳转目标 */
|
|
2
|
+
export const CODA_MARK = "repeat.coda";
|
|
3
|
+
/*!
|
|
4
|
+
* Bravura coda (U+E048), Y axis flipped; https://github.com/steinbergmedia/bravura
|
|
5
|
+
* Copyright (c) 2026, Steinberg Media Technologies GmbH. Reserved Font Name "Bravura".
|
|
6
|
+
* SPDX-License-Identifier: OFL-1.1
|
|
7
|
+
* Full license: Bravura-LICENSE.txt in the package root and dist.
|
|
8
|
+
*/
|
|
9
|
+
const codaPath = [
|
|
10
|
+
{ op: "M", x: -4, y: -376 },
|
|
11
|
+
{ op: "C", cx1: -4, cy1: -363, cx2: 0, cy2: -352, x: 14, y: -352 },
|
|
12
|
+
{ op: "L", x: 146, y: -352 },
|
|
13
|
+
{ op: "C", cx1: 157, cy1: -165, cx2: 296, cy2: -13, x: 458, y: 0 },
|
|
14
|
+
{ op: "L", x: 458, y: 140 },
|
|
15
|
+
{ op: "C", cx1: 458, cy1: 154, cx2: 469, cy2: 158, x: 482, y: 158 },
|
|
16
|
+
{ op: "C", cx1: 495, cy1: 158, cx2: 506, cy2: 154, x: 506, y: 140 },
|
|
17
|
+
{ op: "L", x: 506, y: 0 },
|
|
18
|
+
{ op: "C", cx1: 668, cy1: -13, cx2: 808, cy2: -165, x: 818, y: -352 },
|
|
19
|
+
{ op: "L", x: 937, y: -352 },
|
|
20
|
+
{ op: "C", cx1: 951, cy1: -352, cx2: 955, cy2: -363, x: 955, y: -376 },
|
|
21
|
+
{ op: "C", cx1: 955, cy1: -389, cx2: 951, cy2: -400, x: 937, y: -400 },
|
|
22
|
+
{ op: "L", x: 818, y: -400 },
|
|
23
|
+
{ op: "C", cx1: 808, cy1: -588, cx2: 668, cy2: -739, x: 506, y: -752 },
|
|
24
|
+
{ op: "L", x: 506, y: -881 },
|
|
25
|
+
{ op: "C", cx1: 506, cy1: -894, cx2: 495, cy2: -898, x: 482, y: -898 },
|
|
26
|
+
{ op: "C", cx1: 469, cy1: -898, cx2: 458, cy2: -894, x: 458, y: -881 },
|
|
27
|
+
{ op: "L", x: 458, y: -752 },
|
|
28
|
+
{ op: "C", cx1: 296, cy1: -739, cx2: 157, cy2: -589, x: 146, y: -400 },
|
|
29
|
+
{ op: "L", x: 14, y: -400 },
|
|
30
|
+
{ op: "C", cx1: 0, cy1: -400, cx2: -4, cy2: -389, x: -4, y: -376 },
|
|
31
|
+
{ op: "Z" },
|
|
32
|
+
{ op: "M", x: 653, y: -400 },
|
|
33
|
+
{ op: "L", x: 506, y: -400 },
|
|
34
|
+
{ op: "L", x: 506, y: -696 },
|
|
35
|
+
{ op: "C", cx1: 646, cy1: -684, cx2: 653, cy2: -562, x: 653, y: -400 },
|
|
36
|
+
{ op: "Z" },
|
|
37
|
+
{ op: "M", x: 506, y: -352 },
|
|
38
|
+
{ op: "L", x: 653, y: -352 },
|
|
39
|
+
{ op: "C", cx1: 650, cy1: -199, cx2: 631, cy2: -63, x: 506, y: -48 },
|
|
40
|
+
{ op: "Z" },
|
|
41
|
+
{ op: "M", x: 316, y: -400 },
|
|
42
|
+
{ op: "C", cx1: 316, cy1: -562, cx2: 316, cy2: -684, x: 458, y: -696 },
|
|
43
|
+
{ op: "L", x: 458, y: -400 },
|
|
44
|
+
{ op: "Z" },
|
|
45
|
+
{ op: "M", x: 458, y: -352 },
|
|
46
|
+
{ op: "L", x: 458, y: -48 },
|
|
47
|
+
{ op: "C", cx1: 329, cy1: -63, cx2: 317, cy2: -198, x: 316, y: -352 },
|
|
48
|
+
{ op: "Z" },
|
|
49
|
+
];
|
|
50
|
+
export const codaSymbol = {
|
|
51
|
+
name: "coda",
|
|
52
|
+
description: "尾声记号:$tocoda 的跳转目标",
|
|
53
|
+
weight: 1.15,
|
|
54
|
+
playbackMarks: () => [CODA_MARK],
|
|
55
|
+
shapes: [{ path: codaPath, style: { fill: "#000" } }],
|
|
56
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 力度记号族:由 m / f / p 三个字母横向拼接而成
|
|
3
3
|
*
|
|
4
4
|
* 三个字母抠自同一套字体的 mp / mf 连字,坐标同属一套字体坐标系(共基线、同字号),
|
|
5
5
|
* 所以拼接只需要横向位移,字距也是从连字上量出来的。
|
|
@@ -140,6 +140,8 @@ const ADVANCE = {
|
|
|
140
140
|
const INK = { fill: "#000" };
|
|
141
141
|
/** size 是 f 的全高;p 系没有升部、盒子只有 f 的这么高,不按此缩小就会被放大 1.26 倍 */
|
|
142
142
|
const P_WEIGHT = 0.79341;
|
|
143
|
+
const P_VELOCITIES = [49, 33, 16, 10, 5, 1];
|
|
144
|
+
const F_VELOCITIES = [96, 112, 126, 127];
|
|
143
145
|
function dynamic(name, velocity, description) {
|
|
144
146
|
let dx = 0;
|
|
145
147
|
const shapes = Array.from(name, (letter, index) => {
|
|
@@ -155,14 +157,22 @@ function dynamic(name, velocity, description) {
|
|
|
155
157
|
onTimeState: state => { state.velocity = velocity; },
|
|
156
158
|
};
|
|
157
159
|
}
|
|
158
|
-
/**
|
|
160
|
+
/** 默认力度与 MuseScore 一致;mf 恰好等于 DEFAULT_VELOCITY */
|
|
159
161
|
export const dynamicSymbols = [
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
162
|
+
{
|
|
163
|
+
...dynamic("p", P_VELOCITIES[0], "弱;支持 `$p`、`$pp`、`$ppp` 等连续 p,整体居中"),
|
|
164
|
+
pattern: {
|
|
165
|
+
regex: /^p+$/,
|
|
166
|
+
resolve: ([name]) => dynamic(name, P_VELOCITIES[Math.min(name.length, P_VELOCITIES.length) - 1], "弱力度记号"),
|
|
167
|
+
},
|
|
168
|
+
},
|
|
163
169
|
dynamic("mp", 64, "中弱"),
|
|
164
170
|
dynamic("mf", 80, "中强"),
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
171
|
+
{
|
|
172
|
+
...dynamic("f", F_VELOCITIES[0], "强;支持 `$f`、`$ff`、`$fff` 等连续 f,整体居中"),
|
|
173
|
+
pattern: {
|
|
174
|
+
regex: /^f+$/,
|
|
175
|
+
resolve: ([name]) => dynamic(name, F_VELOCITIES[Math.min(name.length, F_VELOCITIES.length) - 1], "强力度记号"),
|
|
176
|
+
},
|
|
177
|
+
},
|
|
168
178
|
];
|
|
@@ -3,7 +3,7 @@ export const fineSymbol = {
|
|
|
3
3
|
name: "fine",
|
|
4
4
|
description: "终止记号:回跳之后经过这里就结束乐曲",
|
|
5
5
|
weight: 1.4,
|
|
6
|
-
text: { content: "Fine.", fontFamily: '"Times New Roman", serif' },
|
|
6
|
+
text: { content: "Fine.", fontFamily: '"Times New Roman", serif', fontStyle: "italic" },
|
|
7
7
|
// 判据是回跳已经发生,不是“第二次经过”;否则写在 |: :| 里会被反复线提前触发
|
|
8
8
|
onVisit: (cursor, at) => {
|
|
9
9
|
const jump = cursor.seek(JUMP_MARK, at, 1);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { accentSymbol } from "./accent.js";
|
|
2
|
+
export const marcatoSymbol = {
|
|
3
|
+
name: "marcato",
|
|
4
|
+
description: "强调音:尖顶记号;播放同重音,所附着音符力度增加 20,最高 127",
|
|
5
|
+
weight: 0.65,
|
|
6
|
+
shapes: [
|
|
7
|
+
{
|
|
8
|
+
path: [
|
|
9
|
+
{ op: "M", x: 0, y: 10 },
|
|
10
|
+
{ op: "L", x: 4.5, y: 0 },
|
|
11
|
+
{ op: "L", x: 5.5, y: 0 },
|
|
12
|
+
{ op: "L", x: 10, y: 10 },
|
|
13
|
+
{ op: "L", x: 8, y: 10 },
|
|
14
|
+
{ op: "L", x: 5, y: 3 },
|
|
15
|
+
{ op: "L", x: 2, y: 10 },
|
|
16
|
+
{ op: "Z" },
|
|
17
|
+
],
|
|
18
|
+
style: { fill: "#000" },
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
emitPlayback: accentSymbol.emitPlayback,
|
|
22
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const staccatoSymbol = {
|
|
2
|
+
name: "staccato",
|
|
3
|
+
description: "断奏:所附着音符的实际发声时长缩短为一半",
|
|
4
|
+
weight: 0.8,
|
|
5
|
+
shapes: [
|
|
6
|
+
{
|
|
7
|
+
path: [
|
|
8
|
+
{ op: "M", x: 0, y: 0 },
|
|
9
|
+
{ op: "L", x: 12, y: 0 },
|
|
10
|
+
{ op: "L", x: 6, y: 13 },
|
|
11
|
+
{ op: "Z" },
|
|
12
|
+
],
|
|
13
|
+
style: { fill: "#000" },
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
/** 将目标音段的实际发声区间压缩至前半段,保留原起点与子音相对比例 */
|
|
17
|
+
emitPlayback: emitter => emitter.affectFollowing((_context, notes) => {
|
|
18
|
+
const origin = notes[0].start.clone();
|
|
19
|
+
for (const note of notes) {
|
|
20
|
+
note.start.sub(origin).div(2).add(origin);
|
|
21
|
+
note.end.sub(origin).div(2).add(origin);
|
|
22
|
+
}
|
|
23
|
+
}),
|
|
24
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CODA_MARK } from "./coda.js";
|
|
2
|
+
import { JUMP_MARK } from "./dc.js";
|
|
3
|
+
export const tocodaSymbol = {
|
|
4
|
+
name: "tocoda",
|
|
5
|
+
description: "跳至尾声:回跳之后经过这里就跳向最近的 $coda",
|
|
6
|
+
weight: 1.4,
|
|
7
|
+
text: { content: "To Coda", fontFamily: '"Times New Roman", serif', fontStyle: "italic" },
|
|
8
|
+
// 判据是后方回跳已经发生,不是“第二次经过”;否则写在 |: :| 里会被反复线提前触发
|
|
9
|
+
onVisit: (cursor, at) => {
|
|
10
|
+
const jump = cursor.seek(JUMP_MARK, at, 1);
|
|
11
|
+
const coda = cursor.seek(CODA_MARK, at, 1);
|
|
12
|
+
return jump !== undefined && cursor.visits(jump) > 0 && coda !== undefined
|
|
13
|
+
? { kind: "jump", column: coda }
|
|
14
|
+
: undefined;
|
|
15
|
+
},
|
|
16
|
+
};
|