jpfun 0.3.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.
- package/README.md +73 -1
- package/dist/converter/midi/convert.d.ts +50 -0
- package/dist/converter/midi/convert.js +700 -0
- package/dist/converter/midi/index.d.ts +5 -0
- package/dist/converter/midi/index.js +11 -0
- package/dist/converter/midi/json.d.ts +38 -0
- package/dist/converter/midi/json.js +1 -0
- package/dist/converter/midi/triplet.d.ts +30 -0
- package/dist/converter/midi/triplet.js +75 -0
- package/dist/converter/musicxml/dom.d.ts +25 -0
- package/dist/converter/musicxml/dom.js +65 -0
- package/dist/converter/musicxml/features.d.ts +51 -0
- package/dist/converter/musicxml/features.js +232 -0
- package/dist/converter/musicxml/index.d.ts +9 -0
- package/dist/converter/musicxml/index.js +1140 -0
- package/dist/converter/musicxml/model.d.ts +123 -0
- package/dist/converter/musicxml/model.js +1 -0
- package/dist/converter/source.d.ts +22 -0
- package/dist/converter/source.js +51 -0
- package/dist/core.d.ts +22 -0
- package/dist/core.js +22 -0
- package/dist/functions/ASTtypes.d.ts +28 -25
- package/dist/functions/ASTtypes.js +25 -23
- package/dist/functions/adjust/index.js +1 -1
- package/dist/functions/arpeggio/index.d.ts +2 -0
- package/dist/functions/arpeggio/index.js +180 -0
- package/dist/functions/arpeggio/shape.d.ts +7 -0
- package/dist/functions/arpeggio/shape.js +122 -0
- package/dist/functions/bar/index.js +23 -15
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +1 -1
- package/dist/functions/beam/layout.d.ts +2 -2
- package/dist/functions/beam/layout.js +1 -1
- package/dist/functions/box/index.js +2 -2
- package/dist/functions/br/index.js +1 -1
- package/dist/functions/dash/index.js +7 -6
- package/dist/functions/default.js +6 -2
- package/dist/functions/dot/index.js +12 -22
- package/dist/functions/dyn/index.d.ts +2 -0
- package/dist/functions/dyn/index.js +242 -0
- package/dist/functions/grace/index.d.ts +1 -1
- package/dist/functions/grace/index.js +2 -3
- package/dist/functions/head/index.js +149 -145
- package/dist/functions/key/index.js +5 -4
- package/dist/functions/meter/index.js +6 -5
- package/dist/functions/note/index.js +68 -21
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +2 -0
- package/dist/functions/page/index.js +22 -7
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +30 -0
- package/dist/functions/set/index.js +8 -5
- package/dist/functions/symbol/index.d.ts +13 -3
- package/dist/functions/symbol/index.js +61 -16
- package/dist/functions/symbol/shape.d.ts +2 -0
- package/dist/functions/symbol/shape.js +14 -10
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
- package/dist/functions/symbol/symbols/ds.js +93 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +168 -0
- package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.js +44 -31
- package/dist/functions/symbol/symbols/prall.d.ts +2 -0
- package/dist/functions/symbol/symbols/prall.js +42 -0
- package/dist/functions/symbol/symbols/segno.d.ts +4 -0
- package/dist/functions/symbol/symbols/segno.js +82 -0
- package/dist/functions/symbol/symbols/tr.js +0 -4
- package/dist/functions/tempo/index.js +5 -3
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +14 -7
- package/dist/functions/tie/index.js +37 -28
- package/dist/functions/tuplet/index.js +14 -7
- package/dist/functions/up/index.js +23 -21
- package/dist/functions/voice/index.js +121 -45
- package/dist/functions/volta/index.js +51 -48
- package/dist/iife/core.d.ts +1 -0
- package/dist/iife/core.js +3 -0
- package/dist/iife/from-midi.d.ts +1 -0
- package/dist/iife/from-midi.js +15 -0
- package/dist/iife/from-musicxml.d.ts +1 -0
- package/dist/iife/from-musicxml.js +3 -0
- package/dist/iife/full.d.ts +1 -0
- package/dist/iife/full.js +3 -0
- package/dist/index.d.ts +3 -21
- package/dist/index.js +3 -21
- package/dist/jpfun.core.min.js +153 -0
- package/dist/jpfun.from-midi.min.js +9 -0
- package/dist/jpfun.from-musicxml.min.js +12 -0
- package/dist/jpfun.min.js +71 -48
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +2 -1
- package/dist/layout/engine.js +71 -26
- package/dist/layout/model.d.ts +4 -4
- package/dist/layout/model.js +15 -12
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.js +3 -3
- package/dist/layout/types.d.ts +20 -2
- package/dist/lowering/loweringContext.d.ts +3 -1
- package/dist/lowering/loweringContext.js +28 -14
- package/dist/lowering/track.d.ts +4 -8
- package/dist/lowering/track.js +6 -1
- package/dist/lowering/types.d.ts +5 -128
- package/dist/lowering/types.js +1 -98
- package/dist/parser/parse-utils/string-utils.d.ts +2 -0
- package/dist/parser/parse-utils/string-utils.js +4 -0
- package/dist/parser/parserContext.d.ts +7 -8
- package/dist/parser/parserContext.js +41 -36
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +1 -1
- package/dist/playback/compile.js +15 -2
- package/dist/playback/event.d.ts +17 -3
- package/dist/playback/event.js +30 -12
- package/dist/playback/time.js +1 -1
- package/dist/playback/types.d.ts +5 -4
- package/dist/render/text.d.ts +6 -1
- package/dist/render/text.js +8 -1
- package/grammar.md +44 -8
- package/package.json +11 -2
- package/dist/functions/symbol/symbols/f.js +0 -55
- package/dist/functions/symbol/symbols/p.js +0 -61
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { DEFAULT_TONALITY, TemporalNodeBase } from "
|
|
1
|
+
import { DEFAULT_TONALITY, TemporalNodeBase } from "../temporal.js";
|
|
2
2
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
3
3
|
import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
4
4
|
import { parseNoteName } from "../note/noteNameFSM.js";
|
|
5
5
|
import { acc2Offset, NoteNameMap, tonality2Midi } from "../../parser/parse-utils/note-utils.js";
|
|
6
6
|
import { paintAccidental, placeAccidentals } from "../note/accidentals.js";
|
|
7
|
-
import { JIANPU_NUMBER_FONT } from "../../render/text.js";
|
|
8
7
|
/**
|
|
9
8
|
* 把宽松的调性写法收敛成 tonality2Midi 认得的 [音名][最多一个 #b][绝对八度]
|
|
10
9
|
*
|
|
@@ -56,17 +55,19 @@ class KeyFunction extends ASTFunctionNode {
|
|
|
56
55
|
};
|
|
57
56
|
tonality;
|
|
58
57
|
size;
|
|
58
|
+
font;
|
|
59
59
|
/** 只用于绘制:音名与升降号,八度不上谱 */
|
|
60
60
|
displayName;
|
|
61
61
|
displayAcc;
|
|
62
62
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
63
63
|
super(sourceSpan, parent);
|
|
64
64
|
const [tonality, size] = this.getArgValue(args, ctx);
|
|
65
|
+
this.font = ctx.variables.numberfont;
|
|
65
66
|
this.tonality = tonality;
|
|
66
67
|
this.size = ctx.length2px(size);
|
|
67
68
|
const normalized = normalizeTonality(tonality);
|
|
68
69
|
if (normalized !== tonality) {
|
|
69
|
-
if (ctx.strict)
|
|
70
|
+
if (ctx.variables.strict)
|
|
70
71
|
throw new ErrorDiagnostic("E_KEY_TONALITY", `@key 无法解析调性 "${tonality}"`, sourceSpan);
|
|
71
72
|
this.tonality = normalized ?? DEFAULT_TONALITY;
|
|
72
73
|
ctx.diagnostics.push(new WarningDiagnostic("W_KEY_TONALITY", `@key 的调性 "${tonality}" 不是规范写法,已自动切换到 ${this.tonality}`, sourceSpan));
|
|
@@ -95,7 +96,7 @@ class KeyTemporalNode extends TemporalNodeBase {
|
|
|
95
96
|
this.ast = ast;
|
|
96
97
|
this.style = {
|
|
97
98
|
fontSize: ast.size,
|
|
98
|
-
fontFamily:
|
|
99
|
+
fontFamily: ast.font,
|
|
99
100
|
fill: "#000",
|
|
100
101
|
};
|
|
101
102
|
this.initLayoutBox();
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { Fraction } from "../../fraction.js";
|
|
3
|
-
import { ANCHOR_KEY, TemporalNodeBase } from "
|
|
3
|
+
import { ANCHOR_KEY, TemporalNodeBase } from "../temporal.js";
|
|
4
4
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
5
|
-
import { JIANPU_NUMBER_FONT } from "../../render/text.js";
|
|
6
5
|
class MeterFunction extends ASTFunctionNode {
|
|
7
6
|
static def = {
|
|
8
7
|
name: ["meter"],
|
|
@@ -32,13 +31,15 @@ class MeterFunction extends ASTFunctionNode {
|
|
|
32
31
|
measureDuration;
|
|
33
32
|
size;
|
|
34
33
|
strict;
|
|
34
|
+
font;
|
|
35
35
|
constructor(span, args, ctx, parent = null) {
|
|
36
36
|
super(span, parent);
|
|
37
37
|
let [num, den, size] = this.getArgValue(args, ctx);
|
|
38
|
+
this.font = ctx.variables.numberfont;
|
|
38
39
|
if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num <= 0
|
|
39
40
|
|| !Number.isSafeInteger(den) || den <= 0) {
|
|
40
41
|
const message = `@meter 的分子和分母必须是正整数`;
|
|
41
|
-
if (ctx.strict)
|
|
42
|
+
if (ctx.variables.strict)
|
|
42
43
|
throw new ErrorDiagnostic("E_METER_INVALID", message, span);
|
|
43
44
|
ctx.diagnostics.push(new WarningDiagnostic("W_METER_INVALID", `${message},已回落为 4/4`, span));
|
|
44
45
|
num = den = 4;
|
|
@@ -48,7 +49,7 @@ class MeterFunction extends ASTFunctionNode {
|
|
|
48
49
|
// QN 以四分音符为 1;拍号不修改音符自身时值
|
|
49
50
|
this.measureDuration = new Fraction(num * 4, den);
|
|
50
51
|
this.size = ctx.length2px(size);
|
|
51
|
-
this.strict = ctx.strict;
|
|
52
|
+
this.strict = ctx.variables.strict;
|
|
52
53
|
}
|
|
53
54
|
loweringEnter() {
|
|
54
55
|
return [new MeterTemporal(this)];
|
|
@@ -116,7 +117,7 @@ class MeterTemporal extends TemporalNodeBase {
|
|
|
116
117
|
this.ast = ast;
|
|
117
118
|
this.style = {
|
|
118
119
|
fontSize: ast.size,
|
|
119
|
-
fontFamily:
|
|
120
|
+
fontFamily: ast.font,
|
|
120
121
|
textAlign: "center",
|
|
121
122
|
fill: "#000",
|
|
122
123
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
1
|
+
import { ASTTextNode, ASTFunctionNode } from "../ASTtypes.js";
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { parseNoteName } from "./noteNameFSM.js";
|
|
4
|
-
import { JIANPU_NUMBER_FONT } from "../../render/text.js";
|
|
5
4
|
import { paintAccidental, placeAccidentals } from "./accidentals.js";
|
|
5
|
+
const JE_OCTAVE_OFFSET = "note.jeOctaveOffset";
|
|
6
6
|
class NoteFunction extends ASTFunctionNode {
|
|
7
7
|
static def = {
|
|
8
8
|
name: ["note", "n"],
|
|
@@ -10,17 +10,20 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
10
10
|
example: `@note(name, acc, octave, color)
|
|
11
11
|
参数说明:
|
|
12
12
|
- name: [必填]音符名,可以是大写字母或者数字,不允许小写字母(会和降号冲突)。此参数写法有语法糖,见下。
|
|
13
|
-
其中 0 是休止符,8 是隐形占位,9 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
|
|
13
|
+
其中 0 或 Z 是休止符,8 是隐形占位,9 或 X 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
|
|
14
14
|
- acc: [可选]的额外升降号字符串,例如 "##" 表示再升两个半音,"b" 表示再降一个半音。
|
|
15
15
|
- octave: [可选]八度,类型为数字。如果 name 是字母,则此项代表绝对八度;如果是数字,则此项代表相对八度。
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
例:
|
|
19
|
-
也支持
|
|
17
|
+
语法糖:数字音名写作 [升降号][音名][相对八度];字母音名写作 [音名][升降号][绝对八度]
|
|
18
|
+
例:A#3 === @note(A#3) === @note(A, #, 3)
|
|
19
|
+
也支持 A##bn99 的写法。可以设置 note.octave 改变默认的绝对八度
|
|
20
|
+
|
|
21
|
+
兼容无歧义的后置升降号:A3# 等价于 A#3,6#,, 等价于 #6,,
|
|
22
|
+
如果整段升降号后紧跟数字,则升降号属于下一个音符:6#3 解析为 6 和 #3
|
|
20
23
|
|
|
21
24
|
支持 数字音名, 此时 octave 为相对八度
|
|
22
25
|
支持使用数字音名时使用相对八度,如 "1,," 代表在当前基准音(由上下文属性"1=?"决定)的基础上降低两倍八度,"1'" 代表在当前基准音的基础上提高一个八度
|
|
23
|
-
|
|
26
|
+
例:#1' === @note("#1'") === @note(1, #, 1)
|
|
24
27
|
`,
|
|
25
28
|
allowExtraArgs: false,
|
|
26
29
|
args: [
|
|
@@ -47,12 +50,35 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
47
50
|
]
|
|
48
51
|
};
|
|
49
52
|
static deSugarAtom(source, start, end) {
|
|
53
|
+
// JE 谱兼容
|
|
54
|
+
let jeOffset = 0;
|
|
55
|
+
switch (source[start]) {
|
|
56
|
+
case "(":
|
|
57
|
+
case "]":
|
|
58
|
+
jeOffset = -1;
|
|
59
|
+
break;
|
|
60
|
+
case "[":
|
|
61
|
+
case ")":
|
|
62
|
+
jeOffset = 1;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
if (jeOffset)
|
|
66
|
+
return {
|
|
67
|
+
next: start + 1,
|
|
68
|
+
node: {
|
|
69
|
+
kind: "sugar",
|
|
70
|
+
data: { jeOffset },
|
|
71
|
+
span: { start, end: start + 1 },
|
|
72
|
+
syntaxKind: "punctuation",
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
// 解析音符名
|
|
50
76
|
const parseResult = parseNoteName(source, start, end);
|
|
51
77
|
if (parseResult instanceof Diagnostic)
|
|
52
78
|
return null;
|
|
53
79
|
const argMap = new Map();
|
|
54
80
|
argMap.set("name", parseResult.name);
|
|
55
|
-
if (parseResult.octave)
|
|
81
|
+
if (parseResult.octave !== null)
|
|
56
82
|
argMap.set("octave", parseResult.octave);
|
|
57
83
|
if (parseResult.acc)
|
|
58
84
|
argMap.set("acc", parseResult.acc);
|
|
@@ -66,6 +92,16 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
66
92
|
};
|
|
67
93
|
return { next: parseResult.next, node };
|
|
68
94
|
}
|
|
95
|
+
static deSugarRelation(ctx, nodes, at) {
|
|
96
|
+
const node = nodes[at];
|
|
97
|
+
const jeOffset = node.data?.jeOffset;
|
|
98
|
+
if (typeof jeOffset !== "number")
|
|
99
|
+
return null;
|
|
100
|
+
ctx.variables[JE_OCTAVE_OFFSET] = (ctx.variables[JE_OCTAVE_OFFSET] ?? 0) + jeOffset;
|
|
101
|
+
// JE 谱兼容:保留括号源码,并让关系语法糖像跳过普通文本一样跳过它。
|
|
102
|
+
ctx.nodes.push(new ASTTextNode(node.span));
|
|
103
|
+
return at + 1;
|
|
104
|
+
}
|
|
69
105
|
labelable() { return this; }
|
|
70
106
|
loweringEnter() {
|
|
71
107
|
return [new NoteTemporalNode(this)];
|
|
@@ -76,10 +112,14 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
76
112
|
acc;
|
|
77
113
|
color;
|
|
78
114
|
size;
|
|
115
|
+
font;
|
|
116
|
+
// JE 八度偏移量
|
|
117
|
+
jeOctaveOffset;
|
|
79
118
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
80
119
|
super(sourceSpan, parent);
|
|
81
120
|
[this.name, this.acc, this.octave, this.color] = this.getArgValue(args, ctx);
|
|
82
|
-
this.
|
|
121
|
+
this.font = ctx.variables.numberfont;
|
|
122
|
+
this.size = ctx.variables.fontsize;
|
|
83
123
|
// 创建时就固化参数值
|
|
84
124
|
// 校验 note name
|
|
85
125
|
const parseResult = parseNoteName(this.name);
|
|
@@ -87,7 +127,9 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
87
127
|
parseResult.span = sourceSpan; // 定位到整个函数调用
|
|
88
128
|
throw parseResult;
|
|
89
129
|
}
|
|
90
|
-
|
|
130
|
+
if (parseResult.next !== this.name.length)
|
|
131
|
+
throw new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 无法解析尾部 "${this.name.slice(parseResult.next)}"`, sourceSpan);
|
|
132
|
+
this.name = parseResult.name === "X" ? "9" : parseResult.name === "Z" ? "0" : parseResult.name;
|
|
91
133
|
// 校验octave
|
|
92
134
|
const inputOctave = args.get("octave") ?? args.get(2);
|
|
93
135
|
if (inputOctave !== void 0) {
|
|
@@ -95,10 +137,13 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
95
137
|
throw new ErrorDiagnostic("E_NOTE_OCTAVE_CONFLICT", `函数 @note 的参数 "octave" 与音符名称中的八度信息冲突: ${inputOctave} != ${parseResult.octave}`, sourceSpan);
|
|
96
138
|
}
|
|
97
139
|
else {
|
|
98
|
-
//
|
|
99
|
-
if (
|
|
100
|
-
this.octave = parseResult.octave
|
|
140
|
+
// 没有单独传入 octave 时,优先采用音名中的八度;数字音名缺省为相对 0。
|
|
141
|
+
if (parseResult.octave !== null)
|
|
142
|
+
this.octave = parseResult.octave;
|
|
143
|
+
else if (!parseResult.absOctave)
|
|
144
|
+
this.octave = 0;
|
|
101
145
|
}
|
|
146
|
+
this.jeOctaveOffset = ctx.variables[JE_OCTAVE_OFFSET] ?? 0;
|
|
102
147
|
// 补充acc
|
|
103
148
|
if (parseResult.acc !== null)
|
|
104
149
|
this.acc = parseResult.acc + this.acc;
|
|
@@ -108,9 +153,9 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
108
153
|
}
|
|
109
154
|
}
|
|
110
155
|
export const NoteNode = NoteFunction;
|
|
111
|
-
import { DEFAULT_KEY, DEFAULT_VELOCITY, TemporalNodeBase } from "
|
|
156
|
+
import { DEFAULT_KEY, DEFAULT_VELOCITY, TemporalNodeBase } from "../temporal.js";
|
|
112
157
|
import { createDiatonicTranspose, resolveLetterNameToJianpu, resolveNoteMidi, } from "../../parser/parse-utils/note-utils.js";
|
|
113
|
-
// 8 是隐形占位,9
|
|
158
|
+
// 8 是隐形占位,9 是固定打击音的节拍记号,空串表示不绘制
|
|
114
159
|
const NOTE_GLYPH = { "8": "", "9": "X" };
|
|
115
160
|
class NoteTemporalNode extends TemporalNodeBase {
|
|
116
161
|
// 时间固化后的参数
|
|
@@ -135,10 +180,10 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
135
180
|
this.mergeKey = DEFAULT_KEY;
|
|
136
181
|
this.name = ast.name;
|
|
137
182
|
this.acc = ast.acc;
|
|
138
|
-
this.octave = ast.octave;
|
|
183
|
+
this.octave = ast.octave + ast.jeOctaveOffset;
|
|
139
184
|
this.numberStyle = {
|
|
140
185
|
fontSize: ast.size,
|
|
141
|
-
fontFamily:
|
|
186
|
+
fontFamily: ast.font,
|
|
142
187
|
fontWeight: 400,
|
|
143
188
|
textAlign: "center",
|
|
144
189
|
fill: ast.color,
|
|
@@ -227,7 +272,7 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
227
272
|
}
|
|
228
273
|
onTimeState(state) {
|
|
229
274
|
const keySignature = state.keySignature;
|
|
230
|
-
this.playbackState = { bpm: state.bpm, velocity: state.velocity };
|
|
275
|
+
this.playbackState = { bpm: state.bpm, velocity: state.velocity, program: state.program };
|
|
231
276
|
this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
|
|
232
277
|
this.transposeDiatonic = createDiatonicTranspose(this.name, this.acc, this.octave, keySignature);
|
|
233
278
|
// 数字音名本身就是简谱显示形式,直接保留原始数字和相对八度
|
|
@@ -248,16 +293,18 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
248
293
|
}
|
|
249
294
|
}
|
|
250
295
|
emitPlayback(emitter) {
|
|
251
|
-
|
|
296
|
+
const percussion = this.ast.name === "9";
|
|
297
|
+
const midi = percussion ? 37 : this.resolvedMidi;
|
|
298
|
+
if (midi === null || emitter.end.compare(emitter.start) <= 0)
|
|
252
299
|
return;
|
|
253
300
|
const noteId = emitter.nextNoteId();
|
|
254
301
|
emitter.emit({
|
|
255
302
|
kind: "note-on",
|
|
256
303
|
at: emitter.start,
|
|
257
304
|
noteId,
|
|
258
|
-
midi
|
|
305
|
+
midi,
|
|
259
306
|
velocity: this.playbackState?.velocity ?? DEFAULT_VELOCITY,
|
|
260
|
-
transpose: this.transposeDiatonic,
|
|
307
|
+
...(percussion ? { percussion: true } : { transpose: this.transposeDiatonic }),
|
|
261
308
|
});
|
|
262
309
|
emitter.emit({ kind: "note-off", at: emitter.end, noteId });
|
|
263
310
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
-
const LETTER_NOTE_START_RE = /^[A-
|
|
2
|
+
const LETTER_NOTE_START_RE = /^[A-GXZ]/;
|
|
3
3
|
const NUM_NOTE_START_RE = /^[0-9]/;
|
|
4
4
|
const SHARP_RE = /^[#bn]+/;
|
|
5
5
|
const ABS_OCTAVE_RE = /^[+-]?\d+/;
|
|
@@ -23,7 +23,7 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
23
23
|
else if (NUM_NOTE_START_RE.test(ch)) {
|
|
24
24
|
name = ch;
|
|
25
25
|
absOctave = false;
|
|
26
|
-
state =
|
|
26
|
+
state = 3;
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
29
|
const match = str.slice(pos, end).match(SHARP_RE);
|
|
@@ -31,26 +31,26 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
31
31
|
acc = match[0];
|
|
32
32
|
pos += acc.length - 1;
|
|
33
33
|
absOctave = false;
|
|
34
|
-
state =
|
|
34
|
+
state = 2;
|
|
35
35
|
}
|
|
36
36
|
else
|
|
37
|
-
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 首字符 "${ch}" 不合法,期望 A-G (a-g)
|
|
37
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 首字符 "${ch}" 不合法,期望 A-G (a-g)、0-9、X 或 Z 开头,或在开头直接使用升降号(#/b/n)`, { start: pos, end: pos + 1 });
|
|
38
38
|
}
|
|
39
39
|
break;
|
|
40
|
-
case 1:
|
|
40
|
+
case 1: // [字母模式] 已获得音名
|
|
41
41
|
const subs = str.slice(pos, end);
|
|
42
42
|
match = subs.match(ABS_OCTAVE_RE);
|
|
43
43
|
if (match) {
|
|
44
44
|
octave = parseInt(match[0], 10);
|
|
45
45
|
pos += match[0].length - 1;
|
|
46
|
-
state =
|
|
46
|
+
state = 4;
|
|
47
47
|
break;
|
|
48
48
|
}
|
|
49
49
|
match = subs.match(SHARP_RE);
|
|
50
50
|
if (match) {
|
|
51
51
|
acc = match[0];
|
|
52
52
|
pos += acc.length - 1;
|
|
53
|
-
state =
|
|
53
|
+
state = 5;
|
|
54
54
|
break;
|
|
55
55
|
}
|
|
56
56
|
return {
|
|
@@ -60,87 +60,82 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
60
60
|
octave: null,
|
|
61
61
|
absOctave,
|
|
62
62
|
};
|
|
63
|
-
case 2:
|
|
64
|
-
|
|
63
|
+
case 2: // [数字模式] 已获得升降号
|
|
64
|
+
if (NUM_NOTE_START_RE.test(ch)) {
|
|
65
|
+
name = ch;
|
|
66
|
+
state = 6;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
else
|
|
70
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "${ch}"`, { start: pos, end: pos + 1 });
|
|
71
|
+
case 3: // [数字模式] 已获得音名
|
|
72
|
+
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
65
73
|
if (match) {
|
|
66
|
-
|
|
74
|
+
const octave_str = match[0];
|
|
75
|
+
octave = relativeOctaveFromString(octave_str);
|
|
67
76
|
return {
|
|
68
77
|
name: name,
|
|
69
|
-
next: pos +
|
|
78
|
+
next: pos + octave_str.length,
|
|
70
79
|
acc,
|
|
71
|
-
octave,
|
|
80
|
+
octave: octave,
|
|
72
81
|
absOctave,
|
|
73
82
|
};
|
|
74
83
|
}
|
|
84
|
+
match = str.slice(pos, end).match(SHARP_RE);
|
|
85
|
+
if (match && !NUM_NOTE_START_RE.test(str[pos + match[0].length] ?? "")) {
|
|
86
|
+
const accidental = match[0];
|
|
87
|
+
acc = accidental;
|
|
88
|
+
pos += accidental.length - 1;
|
|
89
|
+
state = 6;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
75
92
|
return {
|
|
76
93
|
name: name,
|
|
77
94
|
next: pos,
|
|
78
|
-
acc
|
|
95
|
+
acc,
|
|
79
96
|
octave: octave,
|
|
80
97
|
absOctave,
|
|
81
98
|
};
|
|
82
|
-
case
|
|
83
|
-
match = str.slice(pos, end).match(
|
|
84
|
-
if (match) {
|
|
85
|
-
|
|
99
|
+
case 4: // [字母模式] 已获得音名和八度
|
|
100
|
+
match = str.slice(pos, end).match(SHARP_RE);
|
|
101
|
+
if (match && !NUM_NOTE_START_RE.test(str[pos + match[0].length] ?? "")) {
|
|
102
|
+
const accidental = match[0];
|
|
103
|
+
acc = accidental;
|
|
86
104
|
return {
|
|
87
105
|
name: name,
|
|
88
|
-
next: pos +
|
|
106
|
+
next: pos + accidental.length,
|
|
89
107
|
acc,
|
|
90
108
|
octave,
|
|
91
109
|
absOctave,
|
|
92
110
|
};
|
|
93
111
|
}
|
|
94
|
-
return {
|
|
95
|
-
name: name,
|
|
96
|
-
next: pos,
|
|
97
|
-
acc: acc,
|
|
98
|
-
octave: null,
|
|
99
|
-
absOctave,
|
|
100
|
-
};
|
|
101
|
-
case 4:
|
|
102
|
-
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
103
|
-
if (match) {
|
|
104
|
-
const octave_str = match[0];
|
|
105
|
-
octave = relativeOctaveFromString(octave_str);
|
|
106
|
-
pos += octave_str.length - 1;
|
|
107
|
-
state = 5;
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
match = str.slice(pos, end).match(SHARP_RE);
|
|
111
|
-
if (match) {
|
|
112
|
-
acc = match[0];
|
|
113
|
-
pos += acc.length - 1;
|
|
114
|
-
state = 6;
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
112
|
return {
|
|
118
113
|
name: name,
|
|
119
114
|
next: pos,
|
|
120
115
|
acc: null,
|
|
121
|
-
octave
|
|
116
|
+
octave,
|
|
122
117
|
absOctave,
|
|
123
118
|
};
|
|
124
|
-
case 5:
|
|
125
|
-
match = str.slice(pos, end).match(
|
|
119
|
+
case 5: // [字母模式] 已获得音名和半音
|
|
120
|
+
match = str.slice(pos, end).match(ABS_OCTAVE_RE);
|
|
126
121
|
if (match) {
|
|
127
|
-
|
|
122
|
+
octave = parseInt(match[0], 10);
|
|
128
123
|
return {
|
|
129
124
|
name: name,
|
|
130
|
-
next: pos +
|
|
125
|
+
next: pos + match[0].length,
|
|
131
126
|
acc,
|
|
132
|
-
octave
|
|
127
|
+
octave,
|
|
133
128
|
absOctave,
|
|
134
129
|
};
|
|
135
130
|
}
|
|
136
131
|
return {
|
|
137
132
|
name: name,
|
|
138
133
|
next: pos,
|
|
139
|
-
acc:
|
|
140
|
-
octave:
|
|
134
|
+
acc: acc,
|
|
135
|
+
octave: null,
|
|
141
136
|
absOctave,
|
|
142
137
|
};
|
|
143
|
-
case 6:
|
|
138
|
+
case 6: // [数字模式] 已获得升降号和音名
|
|
144
139
|
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
145
140
|
if (match) {
|
|
146
141
|
const octave_str = match[0];
|
|
@@ -148,32 +143,26 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
148
143
|
return {
|
|
149
144
|
name: name,
|
|
150
145
|
next: pos + octave_str.length,
|
|
151
|
-
acc
|
|
152
|
-
octave
|
|
146
|
+
acc,
|
|
147
|
+
octave,
|
|
153
148
|
absOctave,
|
|
154
149
|
};
|
|
155
150
|
}
|
|
156
151
|
return {
|
|
157
152
|
name: name,
|
|
158
153
|
next: pos,
|
|
159
|
-
acc
|
|
160
|
-
octave
|
|
154
|
+
acc,
|
|
155
|
+
octave,
|
|
161
156
|
absOctave,
|
|
162
157
|
};
|
|
163
|
-
case 7:
|
|
164
|
-
if (NUM_NOTE_START_RE.test(ch)) {
|
|
165
|
-
name = ch;
|
|
166
|
-
state = 6;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
else
|
|
170
|
-
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "${ch}"`, { start: pos, end: pos + 1 });
|
|
171
158
|
default:
|
|
172
159
|
return Diagnostic.error.Bug("音符名称解析状态机进入了未定义的状态", { start: pos, end });
|
|
173
160
|
}
|
|
174
161
|
}
|
|
162
|
+
if (name === null)
|
|
163
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但输入已结束`, { start, end });
|
|
175
164
|
return {
|
|
176
|
-
name
|
|
165
|
+
name,
|
|
177
166
|
next: end,
|
|
178
167
|
acc,
|
|
179
168
|
octave,
|
|
@@ -21,7 +21,9 @@ export declare class PageFunction extends ASTFunctionNode {
|
|
|
21
21
|
};
|
|
22
22
|
readonly config: PageConfig | null;
|
|
23
23
|
private readonly numbering;
|
|
24
|
+
readonly font: string;
|
|
24
25
|
constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
25
26
|
loweringEnter(ctx: LoweringContext): never[];
|
|
27
|
+
toString(): string;
|
|
26
28
|
}
|
|
27
29
|
export declare const PageNode: ASTFunctionClass;
|
|
@@ -1,30 +1,35 @@
|
|
|
1
1
|
import { DEFAULT_PAGE_CONFIG, normalizePageConfig, } from "../../layout/page.js";
|
|
2
2
|
import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { quote } from "../../parser/parse-utils/string-utils.js";
|
|
3
4
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
5
|
const px = (value) => ({ value, unit: "px" });
|
|
5
6
|
/** 页码字号,同时也是放得下页码所需的最小下边距 */
|
|
6
7
|
export const PAGE_NUMBER_FONT_SIZE = 16;
|
|
7
8
|
const PAGE_NUMBER_STYLE = { fontSize: PAGE_NUMBER_FONT_SIZE, textAlign: "center", fill: "#606060" };
|
|
8
|
-
/**
|
|
9
|
+
/** 单个 1 取当前页;多个 1 中最后一个取总页数,其余取当前页 */
|
|
9
10
|
function formatPageNumber(pattern, current, total) {
|
|
10
11
|
const last = pattern.lastIndexOf("1");
|
|
11
|
-
|
|
12
|
+
const hasTotal = pattern.indexOf("1") !== last;
|
|
13
|
+
return pattern.replace(/1/g, (_, index) => String(hasTotal && index === last ? total : current));
|
|
12
14
|
}
|
|
13
15
|
/** 浮在每页下边距带里的页码,不属于任何谱面行,因此不申报 Track 占用 */
|
|
14
16
|
class PageNumberAttachment {
|
|
15
17
|
sourceSpan;
|
|
16
18
|
pattern;
|
|
17
19
|
marginBottom;
|
|
20
|
+
style;
|
|
18
21
|
layer = "foreground";
|
|
19
|
-
constructor(sourceSpan, pattern, marginBottom) {
|
|
22
|
+
constructor(sourceSpan, pattern, marginBottom, style) {
|
|
20
23
|
this.sourceSpan = sourceSpan;
|
|
21
24
|
this.pattern = pattern;
|
|
22
25
|
this.marginBottom = marginBottom;
|
|
26
|
+
this.style = style;
|
|
23
27
|
}
|
|
24
28
|
createGeometry(context) {
|
|
29
|
+
const style = this.style;
|
|
25
30
|
const marks = context.pages.map((page, index) => {
|
|
26
31
|
const text = formatPageNumber(this.pattern, index + 1, context.pages.length);
|
|
27
|
-
const { w, h, baseline } = context.textMeasurer.measureText(text,
|
|
32
|
+
const { w, h, baseline } = context.textMeasurer.measureText(text, style);
|
|
28
33
|
const center = page.y + page.h - this.marginBottom / 2;
|
|
29
34
|
return { text, x: page.x + page.w / 2, y: center - h / 2 + baseline, w, h, baseline };
|
|
30
35
|
});
|
|
@@ -32,7 +37,7 @@ class PageNumberAttachment {
|
|
|
32
37
|
regions: marks.map(({ x, y, w, h, baseline }) => ({ x: x - w / 2, y: y - baseline, w, h })),
|
|
33
38
|
paint(painter) {
|
|
34
39
|
for (const mark of marks)
|
|
35
|
-
painter.drawText(mark.text, mark.x, mark.y,
|
|
40
|
+
painter.drawText(mark.text, mark.x, mark.y, style);
|
|
36
41
|
},
|
|
37
42
|
};
|
|
38
43
|
}
|
|
@@ -51,14 +56,17 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
51
56
|
{ name: "left", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
|
|
52
57
|
{ name: "right", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
|
|
53
58
|
{ name: "gap", type: "length", default: { value: 1, unit: "em" } },
|
|
54
|
-
//
|
|
59
|
+
// 单个 1 取当前页;多个 1 中最后一个取总页数
|
|
55
60
|
{ name: "numbering", type: "string", default: "" },
|
|
61
|
+
{ name: "font", type: "string", default: "" },
|
|
56
62
|
],
|
|
57
63
|
};
|
|
58
64
|
config;
|
|
59
65
|
numbering = "";
|
|
66
|
+
font;
|
|
60
67
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
61
68
|
super(sourceSpan, parent);
|
|
69
|
+
this.font = ctx.variables.font;
|
|
62
70
|
if (ctx.scopeDepth !== 0) {
|
|
63
71
|
ctx.diagnostics.push(new WarningDiagnostic("W_PAGE_NOT_TOP_LEVEL", "@page 只能在文档顶层声明;当前声明已忽略", sourceSpan));
|
|
64
72
|
this.config = null;
|
|
@@ -71,6 +79,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
71
79
|
}
|
|
72
80
|
ctx.documentDeclarations["page"] = true;
|
|
73
81
|
const values = this.getArgValue(args, ctx);
|
|
82
|
+
this.font = values.pop() || ctx.variables.font;
|
|
74
83
|
const numbering = values.pop();
|
|
75
84
|
const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
|
|
76
85
|
if (!Number.isFinite(width) || width <= 0)
|
|
@@ -101,9 +110,15 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
101
110
|
return [];
|
|
102
111
|
ctx.setPageConfig(this.config);
|
|
103
112
|
if (this.numbering) {
|
|
104
|
-
ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom));
|
|
113
|
+
ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom, { ...PAGE_NUMBER_STYLE, fontFamily: this.font }));
|
|
105
114
|
}
|
|
106
115
|
return [];
|
|
107
116
|
}
|
|
117
|
+
toString() {
|
|
118
|
+
if (!this.config)
|
|
119
|
+
return "@page()";
|
|
120
|
+
const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap } = this.config;
|
|
121
|
+
return `@page(width=${width}px, height=${Number.isFinite(height) ? height : 0}px, top=${marginTop}px, bottom=${marginBottom}px, left=${marginLeft}px, right=${marginRight}px, gap=${lineGap}px, numbering=${quote(this.numbering)}, font=${quote(this.font)})`;
|
|
122
|
+
}
|
|
108
123
|
}
|
|
109
124
|
export const PageNode = PageFunction;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { TemporalNodeBase } from "../temporal.js";
|
|
3
|
+
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
|
+
class ProgramFunction extends ASTFunctionNode {
|
|
5
|
+
static def = {
|
|
6
|
+
name: ["program", "instrument"],
|
|
7
|
+
description: "设置当前音轨的 MIDI 音色",
|
|
8
|
+
example: `@program(40) 将后续音符切换到 MIDI program 40`,
|
|
9
|
+
allowExtraArgs: false,
|
|
10
|
+
args: [{ name: "program", type: "number", default: null }],
|
|
11
|
+
};
|
|
12
|
+
program;
|
|
13
|
+
constructor(span, args, ctx, parent = null) {
|
|
14
|
+
super(span, parent);
|
|
15
|
+
const [program] = this.getArgValue(args, ctx);
|
|
16
|
+
if (!Number.isSafeInteger(program) || program < 0 || program > 127) {
|
|
17
|
+
throw new ErrorDiagnostic("E_PROGRAM_INVALID", "@program 必须是 0..127 的整数", span);
|
|
18
|
+
}
|
|
19
|
+
this.program = program;
|
|
20
|
+
}
|
|
21
|
+
loweringEnter() {
|
|
22
|
+
const node = new TemporalNodeBase();
|
|
23
|
+
node.ast = this;
|
|
24
|
+
node.playbackState = { program: this.program };
|
|
25
|
+
node.onTimeState = (state) => { state.program = this.program; };
|
|
26
|
+
return [node];
|
|
27
|
+
}
|
|
28
|
+
toString() { return `@program(${this.program})`; }
|
|
29
|
+
}
|
|
30
|
+
export const ProgramNode = ProgramFunction;
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { ASTFunctionNode, primaryName, resolveArgType } from "../ASTtypes.js";
|
|
2
2
|
import { WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import {
|
|
3
|
+
import { quote } from "../../parser/parse-utils/string-utils.js";
|
|
4
|
+
import { ParserContext } from "../../parser/parserContext.js";
|
|
4
5
|
class SetFunction extends ASTFunctionNode {
|
|
5
6
|
static def = {
|
|
6
7
|
name: ["set"],
|
|
7
8
|
description: "设置局部默认值",
|
|
8
|
-
example:
|
|
9
|
-
|
|
9
|
+
example: `当前有以下系统配置:
|
|
10
|
+
@set(fontSize=20) 则当前块内默认字体变为20px
|
|
11
|
+
@set(strict=true) 开启严格模式
|
|
12
|
+
还可以设置其他函数的默认值: {函数名}.{属性}=值,例:
|
|
10
13
|
@set(note.octave=5, note.color=#f00)
|
|
11
14
|
|
|
12
15
|
注意:@set 作用域仅在其所处 '{}' 内、其出现之后。离开当前作用域会恢复为之前的设置。此设置和时序无关,类似于局部变量。
|
|
@@ -18,7 +21,7 @@ class SetFunction extends ASTFunctionNode {
|
|
|
18
21
|
static resolveTarget(ctx, key, nameSpan) {
|
|
19
22
|
const dot = key.lastIndexOf(".");
|
|
20
23
|
if (dot < 0)
|
|
21
|
-
return [key,
|
|
24
|
+
return [key, Object.hasOwn(ParserContext.systemVariables, key) ? ParserContext.systemVariables[key].type : "string"];
|
|
22
25
|
const argName = key.slice(dot + 1);
|
|
23
26
|
const def = ctx.functions.get(key.slice(0, dot))?.prototype.def;
|
|
24
27
|
const type = def && resolveArgType(def, argName, -1);
|
|
@@ -57,7 +60,7 @@ class SetFunction extends ASTFunctionNode {
|
|
|
57
60
|
}
|
|
58
61
|
}
|
|
59
62
|
toString() {
|
|
60
|
-
return `@set(${Array.from(this.args
|
|
63
|
+
return `@set(${Array.from(this.args, ([key, value]) => `${key}=${typeof value === "string" ? quote(value) : value}`).join(", ")})`;
|
|
61
64
|
}
|
|
62
65
|
}
|
|
63
66
|
export const SetNode = SetFunction;
|