jpfun 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -11
- 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/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +51 -31
- package/dist/functions/ASTtypes.js +59 -38
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- 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.d.ts +8 -0
- package/dist/functions/bar/index.js +62 -25
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +33 -16
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +4 -3
- package/dist/functions/beam/layout.js +43 -65
- package/dist/functions/box/index.js +37 -36
- package/dist/functions/br/index.js +10 -8
- package/dist/functions/dash/index.js +40 -7
- package/dist/functions/default.js +19 -8
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +23 -25
- package/dist/functions/dyn/index.d.ts +2 -0
- package/dist/functions/dyn/index.js +242 -0
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +84 -79
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +496 -0
- package/dist/functions/key/index.js +98 -3
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +155 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +106 -79
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +11 -2
- package/dist/functions/page/index.js +65 -4
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +30 -0
- package/dist/functions/set/index.js +43 -32
- package/dist/functions/stack/index.js +26 -22
- package/dist/functions/symbol/index.d.ts +31 -0
- package/dist/functions/symbol/index.js +139 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +24 -0
- package/dist/functions/symbol/shape.js +52 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/ds.d.ts +2 -0
- 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/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/fine.d.ts +2 -0
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +56 -0
- 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.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +71 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +57 -4
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +86 -27
- package/dist/functions/tie/index.js +136 -152
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +218 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +258 -182
- package/dist/functions/voice/index.js +203 -109
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +230 -0
- 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 -17
- package/dist/index.js +3 -17
- 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 +166 -0
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +6 -12
- package/dist/layout/engine.js +115 -64
- package/dist/layout/model.d.ts +12 -5
- package/dist/layout/model.js +31 -18
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +71 -20
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +20 -20
- package/dist/lowering/loweringContext.js +153 -101
- package/dist/lowering/track.d.ts +5 -9
- package/dist/lowering/track.js +7 -2
- package/dist/lowering/types.d.ts +26 -112
- package/dist/lowering/types.js +1 -92
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +10 -0
- package/dist/parser/parse-utils/string-utils.js +24 -0
- package/dist/parser/parserContext.d.ts +11 -9
- package/dist/parser/parserContext.js +65 -55
- package/dist/parser/preprocess.js +17 -11
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +414 -0
- package/dist/playback/event.d.ts +105 -0
- package/dist/playback/event.js +105 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +129 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +22 -0
- package/dist/render/text.js +42 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +132 -13
- package/package.json +19 -6
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { Fraction } from "../../fraction.js";
|
|
3
|
+
import { ANCHOR_KEY, TemporalNodeBase } from "../temporal.js";
|
|
4
|
+
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
5
|
+
class MeterFunction extends ASTFunctionNode {
|
|
6
|
+
static def = {
|
|
7
|
+
name: ["meter"],
|
|
8
|
+
description: "设置拍号",
|
|
9
|
+
example: `@meter(6, 8) 设置 6/8 拍号`,
|
|
10
|
+
allowExtraArgs: false,
|
|
11
|
+
args: [
|
|
12
|
+
{
|
|
13
|
+
name: "num",
|
|
14
|
+
type: "number",
|
|
15
|
+
default: null
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: "den",
|
|
19
|
+
type: "number",
|
|
20
|
+
default: null
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "size",
|
|
24
|
+
type: "length",
|
|
25
|
+
default: { value: 0.7, unit: "em" },
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
numerator;
|
|
30
|
+
denominator;
|
|
31
|
+
measureDuration;
|
|
32
|
+
size;
|
|
33
|
+
strict;
|
|
34
|
+
font;
|
|
35
|
+
constructor(span, args, ctx, parent = null) {
|
|
36
|
+
super(span, parent);
|
|
37
|
+
let [num, den, size] = this.getArgValue(args, ctx);
|
|
38
|
+
this.font = ctx.variables.numberfont;
|
|
39
|
+
if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num <= 0
|
|
40
|
+
|| !Number.isSafeInteger(den) || den <= 0) {
|
|
41
|
+
const message = `@meter 的分子和分母必须是正整数`;
|
|
42
|
+
if (ctx.variables.strict)
|
|
43
|
+
throw new ErrorDiagnostic("E_METER_INVALID", message, span);
|
|
44
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_METER_INVALID", `${message},已回落为 4/4`, span));
|
|
45
|
+
num = den = 4;
|
|
46
|
+
}
|
|
47
|
+
this.numerator = num;
|
|
48
|
+
this.denominator = den;
|
|
49
|
+
// QN 以四分音符为 1;拍号不修改音符自身时值
|
|
50
|
+
this.measureDuration = new Fraction(num * 4, den);
|
|
51
|
+
this.size = ctx.length2px(size);
|
|
52
|
+
this.strict = ctx.variables.strict;
|
|
53
|
+
}
|
|
54
|
+
loweringEnter() {
|
|
55
|
+
return [new MeterTemporal(this)];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 校验小节拍数
|
|
59
|
+
* 第一小节特殊规则:只允许不满拍,但不能超拍
|
|
60
|
+
*/
|
|
61
|
+
static loweringFinalize = (result) => {
|
|
62
|
+
const measureStart = new Fraction();
|
|
63
|
+
const measureLength = new Fraction();
|
|
64
|
+
let active = null;
|
|
65
|
+
let validationStarted = false;
|
|
66
|
+
// 诊断范围从上一边界之后延伸到当前关闭边界
|
|
67
|
+
let sourceStart = 0;
|
|
68
|
+
const closeMeasure = (end, sourceEnd) => {
|
|
69
|
+
if (!active || end.equals(measureStart))
|
|
70
|
+
return;
|
|
71
|
+
// Fraction 运算会原地修改接收者,不能直接对时间线字段做减法
|
|
72
|
+
measureLength.copyFrom(end).sub(measureStart);
|
|
73
|
+
if (!measureLength.equals(active.measureDuration)
|
|
74
|
+
&& (validationStarted || measureLength.compare(active.measureDuration) > 0)) {
|
|
75
|
+
const message = `小节时长为 ${measureLength} QN,不满足 ${active.numerator}/${active.denominator} 拍号`;
|
|
76
|
+
const span = { start: sourceStart, end: sourceEnd };
|
|
77
|
+
if (active.strict)
|
|
78
|
+
throw new ErrorDiagnostic("E_METER_MISMATCH", message, span);
|
|
79
|
+
result.diagnostics.push(new WarningDiagnostic("W_METER_MISMATCH", message, span));
|
|
80
|
+
}
|
|
81
|
+
measureStart.copyFrom(end);
|
|
82
|
+
};
|
|
83
|
+
for (const column of result.columns) {
|
|
84
|
+
const time = column[0].t;
|
|
85
|
+
const meter = column.find(node => node instanceof MeterTemporal);
|
|
86
|
+
if (meter) {
|
|
87
|
+
closeMeasure(time, meter.ast.sourceSpan.start);
|
|
88
|
+
active = meter.ast;
|
|
89
|
+
measureStart.copyFrom(time);
|
|
90
|
+
sourceStart = meter.ast.sourceSpan.end;
|
|
91
|
+
}
|
|
92
|
+
const anchor = column.find(node => node.mergeKey === ANCHOR_KEY);
|
|
93
|
+
if (anchor) {
|
|
94
|
+
closeMeasure(time, anchor.ast.sourceSpan.end);
|
|
95
|
+
sourceStart = anchor.ast.sourceSpan.end;
|
|
96
|
+
validationStarted = true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (active) {
|
|
100
|
+
const span = result.columns.at(-1)?.at(-1)?.ast.sourceSpan ?? active.sourceSpan;
|
|
101
|
+
closeMeasure(result.duration, span.end);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
toString() {
|
|
105
|
+
return `@meter(${this.numerator}, ${this.denominator})`;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export const MeterNode = MeterFunction;
|
|
109
|
+
class MeterTemporal extends TemporalNodeBase {
|
|
110
|
+
style;
|
|
111
|
+
numeratorBaseline = 0;
|
|
112
|
+
denominatorBaseline = 0;
|
|
113
|
+
lineY = 0;
|
|
114
|
+
lineHeight = 0;
|
|
115
|
+
constructor(ast) {
|
|
116
|
+
super();
|
|
117
|
+
this.ast = ast;
|
|
118
|
+
this.style = {
|
|
119
|
+
fontSize: ast.size,
|
|
120
|
+
fontFamily: ast.font,
|
|
121
|
+
textAlign: "center",
|
|
122
|
+
fill: "#000",
|
|
123
|
+
};
|
|
124
|
+
this.initLayoutBox();
|
|
125
|
+
}
|
|
126
|
+
emitPlayback(emitter) {
|
|
127
|
+
emitter.emit({
|
|
128
|
+
kind: "time-signature",
|
|
129
|
+
at: emitter.start,
|
|
130
|
+
numerator: this.ast.numerator,
|
|
131
|
+
denominator: this.ast.denominator,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
prepareLayout(context) {
|
|
135
|
+
const num = context.textMeasurer.measureText(String(this.ast.numerator), this.style);
|
|
136
|
+
const den = context.textMeasurer.measureText(String(this.ast.denominator), this.style);
|
|
137
|
+
const gap = this.ast.size * 0.08;
|
|
138
|
+
const padding = this.ast.size * 0.08;
|
|
139
|
+
this.lineHeight = Math.max(1, this.ast.size * 0.06);
|
|
140
|
+
this.numeratorBaseline = num.baseline;
|
|
141
|
+
this.lineY = num.h + gap;
|
|
142
|
+
this.denominatorBaseline = this.lineY + this.lineHeight + gap + den.baseline;
|
|
143
|
+
this.box.w = Math.max(num.w, den.w) + padding * 2;
|
|
144
|
+
this.box.h = this.lineY + this.lineHeight + gap + den.h;
|
|
145
|
+
this.box.anchor = this.box.w / 2;
|
|
146
|
+
// 分数线是拍号的视觉中心
|
|
147
|
+
this.box.visualAxis = this.lineY + this.lineHeight / 2;
|
|
148
|
+
}
|
|
149
|
+
paint(painter) {
|
|
150
|
+
const center = this.box.x + this.box.anchor;
|
|
151
|
+
painter.drawText(String(this.ast.numerator), center, this.box.y + this.numeratorBaseline, this.style);
|
|
152
|
+
painter.drawRect(this.box.x, this.box.y + this.lineY, this.box.w, this.lineHeight, { fill: "#000" });
|
|
153
|
+
painter.drawText(String(this.ast.denominator), center, this.box.y + this.denominatorBaseline, this.style);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PathCommand } from "../../render/types.js";
|
|
1
|
+
import type { Painter, PathCommand } from "../../render/types.js";
|
|
2
2
|
export interface PreparedAccidental {
|
|
3
3
|
w: number;
|
|
4
4
|
h: number;
|
|
@@ -6,5 +6,26 @@ export interface PreparedAccidental {
|
|
|
6
6
|
strokeWidth: number;
|
|
7
7
|
commands: readonly PathCommand[];
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
10
|
-
export
|
|
9
|
+
/** 已经定位到宿主盒局部坐标的升降号 */
|
|
10
|
+
export interface PlacedAccidental {
|
|
11
|
+
shape: PreparedAccidental;
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 把升降号串按同一基线依次排在 x 右侧
|
|
17
|
+
* @param size 宿主数字的字号
|
|
18
|
+
* @param baselineY 宿主文字基线在宿主盒局部坐标中的 y
|
|
19
|
+
* @param gap 相邻升降号之间的间隔;末尾也留一份,宿主据此得到 right
|
|
20
|
+
* @returns right 是下一个字形的起点,top 是最高处(可能为负,宿主自行决定是否下移)
|
|
21
|
+
*/
|
|
22
|
+
export declare function placeAccidentals(symbols: string, size: number, x: number, baselineY: number, gap: number): {
|
|
23
|
+
placed: PlacedAccidental[];
|
|
24
|
+
right: number;
|
|
25
|
+
top: number;
|
|
26
|
+
};
|
|
27
|
+
/** commands 是 0..1 归一化坐标,必须配回字形自己的 w/h 才不会画歪 */
|
|
28
|
+
export declare function paintAccidental(painter: Painter, { shape, x, y }: PlacedAccidental, origin: {
|
|
29
|
+
x: number;
|
|
30
|
+
y: number;
|
|
31
|
+
}, color: string): void;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/** 字形相对数字字号的缩放,线宽则取与数字笔画相当的固定比例 */
|
|
2
2
|
const GLYPH_SCALE = 0.72;
|
|
3
3
|
const STROKE_RATIO = 0.06;
|
|
4
|
+
/** 升降号写在宿主基线之上多少,贴住数字左上角 */
|
|
5
|
+
const RAISE_RATIO = 0.42;
|
|
4
6
|
const ACCIDENTALS = {
|
|
5
7
|
"#": {
|
|
6
8
|
w: 0.46,
|
|
@@ -45,7 +47,7 @@ const ACCIDENTALS = {
|
|
|
45
47
|
},
|
|
46
48
|
};
|
|
47
49
|
/** size 是数字字号,升降号自己决定缩小多少 */
|
|
48
|
-
|
|
50
|
+
function prepareAccidental(symbol, size) {
|
|
49
51
|
const definition = ACCIDENTALS[symbol];
|
|
50
52
|
if (!definition)
|
|
51
53
|
return null;
|
|
@@ -58,3 +60,31 @@ export function prepareAccidental(symbol, size) {
|
|
|
58
60
|
commands: definition.commands,
|
|
59
61
|
};
|
|
60
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* 把升降号串按同一基线依次排在 x 右侧
|
|
65
|
+
* @param size 宿主数字的字号
|
|
66
|
+
* @param baselineY 宿主文字基线在宿主盒局部坐标中的 y
|
|
67
|
+
* @param gap 相邻升降号之间的间隔;末尾也留一份,宿主据此得到 right
|
|
68
|
+
* @returns right 是下一个字形的起点,top 是最高处(可能为负,宿主自行决定是否下移)
|
|
69
|
+
*/
|
|
70
|
+
export function placeAccidentals(symbols, size, x, baselineY, gap) {
|
|
71
|
+
const placed = [];
|
|
72
|
+
const raise = size * RAISE_RATIO;
|
|
73
|
+
let right = x;
|
|
74
|
+
let top = 0;
|
|
75
|
+
for (const symbol of symbols) {
|
|
76
|
+
const shape = prepareAccidental(symbol, size);
|
|
77
|
+
if (!shape)
|
|
78
|
+
continue;
|
|
79
|
+
const y = baselineY - shape.baseline - raise;
|
|
80
|
+
placed.push({ shape, x: right, y });
|
|
81
|
+
if (y < top)
|
|
82
|
+
top = y;
|
|
83
|
+
right += shape.w + gap;
|
|
84
|
+
}
|
|
85
|
+
return { placed, right, top };
|
|
86
|
+
}
|
|
87
|
+
/** commands 是 0..1 归一化坐标,必须配回字形自己的 w/h 才不会画歪 */
|
|
88
|
+
export function paintAccidental(painter, { shape, x, y }, origin, color) {
|
|
89
|
+
painter.drawPath(shape.commands, { stroke: color, strokeWidth: shape.strokeWidth }, { x: origin.x + x, y: origin.y + y, scaleX: shape.w, scaleY: shape.h });
|
|
90
|
+
}
|
|
@@ -1,7 +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 {
|
|
4
|
+
import { paintAccidental, placeAccidentals } from "./accidentals.js";
|
|
5
|
+
const JE_OCTAVE_OFFSET = "note.jeOctaveOffset";
|
|
5
6
|
class NoteFunction extends ASTFunctionNode {
|
|
6
7
|
static def = {
|
|
7
8
|
name: ["note", "n"],
|
|
@@ -9,17 +10,20 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
9
10
|
example: `@note(name, acc, octave, color)
|
|
10
11
|
参数说明:
|
|
11
12
|
- name: [必填]音符名,可以是大写字母或者数字,不允许小写字母(会和降号冲突)。此参数写法有语法糖,见下。
|
|
12
|
-
其中 0 是休止符,8 是隐形占位,9 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
|
|
13
|
+
其中 0 或 Z 是休止符,8 是隐形占位,9 或 X 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
|
|
13
14
|
- acc: [可选]的额外升降号字符串,例如 "##" 表示再升两个半音,"b" 表示再降一个半音。
|
|
14
15
|
- octave: [可选]八度,类型为数字。如果 name 是字母,则此项代表绝对八度;如果是数字,则此项代表相对八度。
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
例:
|
|
18
|
-
也支持
|
|
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
|
|
19
23
|
|
|
20
24
|
支持 数字音名, 此时 octave 为相对八度
|
|
21
25
|
支持使用数字音名时使用相对八度,如 "1,," 代表在当前基准音(由上下文属性"1=?"决定)的基础上降低两倍八度,"1'" 代表在当前基准音的基础上提高一个八度
|
|
22
|
-
|
|
26
|
+
例:#1' === @note("#1'") === @note(1, #, 1)
|
|
23
27
|
`,
|
|
24
28
|
allowExtraArgs: false,
|
|
25
29
|
args: [
|
|
@@ -46,12 +50,35 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
46
50
|
]
|
|
47
51
|
};
|
|
48
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
|
+
// 解析音符名
|
|
49
76
|
const parseResult = parseNoteName(source, start, end);
|
|
50
77
|
if (parseResult instanceof Diagnostic)
|
|
51
78
|
return null;
|
|
52
79
|
const argMap = new Map();
|
|
53
80
|
argMap.set("name", parseResult.name);
|
|
54
|
-
if (parseResult.octave)
|
|
81
|
+
if (parseResult.octave !== null)
|
|
55
82
|
argMap.set("octave", parseResult.octave);
|
|
56
83
|
if (parseResult.acc)
|
|
57
84
|
argMap.set("acc", parseResult.acc);
|
|
@@ -65,6 +92,16 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
65
92
|
};
|
|
66
93
|
return { next: parseResult.next, node };
|
|
67
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
|
+
}
|
|
68
105
|
labelable() { return this; }
|
|
69
106
|
loweringEnter() {
|
|
70
107
|
return [new NoteTemporalNode(this)];
|
|
@@ -75,10 +112,14 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
75
112
|
acc;
|
|
76
113
|
color;
|
|
77
114
|
size;
|
|
115
|
+
font;
|
|
116
|
+
// JE 八度偏移量
|
|
117
|
+
jeOctaveOffset;
|
|
78
118
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
79
119
|
super(sourceSpan, parent);
|
|
80
120
|
[this.name, this.acc, this.octave, this.color] = this.getArgValue(args, ctx);
|
|
81
|
-
this.
|
|
121
|
+
this.font = ctx.variables.numberfont;
|
|
122
|
+
this.size = ctx.variables.fontsize;
|
|
82
123
|
// 创建时就固化参数值
|
|
83
124
|
// 校验 note name
|
|
84
125
|
const parseResult = parseNoteName(this.name);
|
|
@@ -86,7 +127,9 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
86
127
|
parseResult.span = sourceSpan; // 定位到整个函数调用
|
|
87
128
|
throw parseResult;
|
|
88
129
|
}
|
|
89
|
-
|
|
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;
|
|
90
133
|
// 校验octave
|
|
91
134
|
const inputOctave = args.get("octave") ?? args.get(2);
|
|
92
135
|
if (inputOctave !== void 0) {
|
|
@@ -94,10 +137,13 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
94
137
|
throw new ErrorDiagnostic("E_NOTE_OCTAVE_CONFLICT", `函数 @note 的参数 "octave" 与音符名称中的八度信息冲突: ${inputOctave} != ${parseResult.octave}`, sourceSpan);
|
|
95
138
|
}
|
|
96
139
|
else {
|
|
97
|
-
//
|
|
98
|
-
if (
|
|
99
|
-
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;
|
|
100
145
|
}
|
|
146
|
+
this.jeOctaveOffset = ctx.variables[JE_OCTAVE_OFFSET] ?? 0;
|
|
101
147
|
// 补充acc
|
|
102
148
|
if (parseResult.acc !== null)
|
|
103
149
|
this.acc = parseResult.acc + this.acc;
|
|
@@ -107,28 +153,21 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
107
153
|
}
|
|
108
154
|
}
|
|
109
155
|
export const NoteNode = NoteFunction;
|
|
110
|
-
import { DEFAULT_KEY, TemporalNodeBase } from "
|
|
111
|
-
import { resolveLetterNameToJianpu, resolveNoteMidi } from "../../parser/parse-utils/note-utils.js";
|
|
112
|
-
// 8 是隐形占位,9
|
|
156
|
+
import { DEFAULT_KEY, DEFAULT_VELOCITY, TemporalNodeBase } from "../temporal.js";
|
|
157
|
+
import { createDiatonicTranspose, resolveLetterNameToJianpu, resolveNoteMidi, } from "../../parser/parse-utils/note-utils.js";
|
|
158
|
+
// 8 是隐形占位,9 是固定打击音的节拍记号,空串表示不绘制
|
|
113
159
|
const NOTE_GLYPH = { "8": "", "9": "X" };
|
|
114
|
-
function createNumberStyle(ast) {
|
|
115
|
-
return {
|
|
116
|
-
fontSize: ast.size,
|
|
117
|
-
fontFamily: '"Cascadia Mono", Consolas, "Liberation Mono", "Noto Sans Mono", monospace',
|
|
118
|
-
fontWeight: 400,
|
|
119
|
-
textAlign: "center",
|
|
120
|
-
fill: ast.color,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
160
|
class NoteTemporalNode extends TemporalNodeBase {
|
|
124
161
|
// 时间固化后的参数
|
|
125
|
-
activeBpm = null; // 用于播放的时候的速度
|
|
126
162
|
resolvedMidi = null; // MIDI音高 name是数字则需要在onTimeState中基于当前调性偏移 播放的音高
|
|
163
|
+
transposeDiatonic;
|
|
127
164
|
name; // 数字
|
|
128
165
|
acc; // 升降号
|
|
129
166
|
octave; // 绝对值为点的个数
|
|
130
167
|
/** prepareLayout 生成的局部升降号位置 */
|
|
131
168
|
accidentals = [];
|
|
169
|
+
/** 字号和颜色在解析时已经固化,样式随之不再变 */
|
|
170
|
+
numberStyle;
|
|
132
171
|
/** 上八度点属于主体,下八度点通过通用装饰流排在其他下方符号之后 */
|
|
133
172
|
upperOctaveDotY = [];
|
|
134
173
|
lowerOctaveDotY = [];
|
|
@@ -137,39 +176,25 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
137
176
|
constructor(ast) {
|
|
138
177
|
super();
|
|
139
178
|
this.ast = ast;
|
|
140
|
-
this.T
|
|
141
|
-
this.t = 0;
|
|
179
|
+
this.T.set(1);
|
|
142
180
|
this.mergeKey = DEFAULT_KEY;
|
|
143
181
|
this.name = ast.name;
|
|
144
182
|
this.acc = ast.acc;
|
|
145
|
-
this.octave = ast.octave;
|
|
183
|
+
this.octave = ast.octave + ast.jeOctaveOffset;
|
|
184
|
+
this.numberStyle = {
|
|
185
|
+
fontSize: ast.size,
|
|
186
|
+
fontFamily: ast.font,
|
|
187
|
+
fontWeight: 400,
|
|
188
|
+
textAlign: "center",
|
|
189
|
+
fill: ast.color,
|
|
190
|
+
};
|
|
146
191
|
this.initLayoutBox();
|
|
147
192
|
}
|
|
148
193
|
prepareLayout(context) {
|
|
149
|
-
this.accidentals.length = 0;
|
|
150
194
|
this.upperOctaveDotY.length = 0;
|
|
151
195
|
this.lowerOctaveDotY.length = 0;
|
|
152
196
|
const { size, color } = this.ast;
|
|
153
|
-
const
|
|
154
|
-
const mainMetrics = context.textMeasurer.measureText("0", numberStyle);
|
|
155
|
-
// 升降号贴近数字左上角,但不参与数字中心 anchor 的计算
|
|
156
|
-
// 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
|
|
157
|
-
const accidentalGap = size * 0.01;
|
|
158
|
-
const accidentalRaise = size * 0.42;
|
|
159
|
-
const accidentalShapes = [];
|
|
160
|
-
// 把每个升降字符转换为 note 私有的局部路径
|
|
161
|
-
for (const accidental of this.acc) {
|
|
162
|
-
const shape = prepareAccidental(accidental, size);
|
|
163
|
-
if (shape)
|
|
164
|
-
accidentalShapes.push(shape);
|
|
165
|
-
}
|
|
166
|
-
let accidentalWidth = 0;
|
|
167
|
-
// 汇总升降号区域宽度,为数字单元保留固定起点
|
|
168
|
-
for (const shape of accidentalShapes) {
|
|
169
|
-
if (accidentalWidth > 0)
|
|
170
|
-
accidentalWidth += accidentalGap;
|
|
171
|
-
accidentalWidth += shape.w;
|
|
172
|
-
}
|
|
197
|
+
const mainMetrics = context.textMeasurer.measureText("0", this.numberStyle);
|
|
173
198
|
const dotCount = Math.abs(this.octave);
|
|
174
199
|
this.octaveDotRadius = size * 0.065;
|
|
175
200
|
const dotGap = size * 0.08;
|
|
@@ -177,14 +202,17 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
177
202
|
const octaveSpace = dotCount === 0
|
|
178
203
|
? 0
|
|
179
204
|
: dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap + dotGap;
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
205
|
+
const mainY = this.octave > 0 ? octaveSpace : 0;
|
|
206
|
+
this.numberY = mainY + mainMetrics.baseline;
|
|
207
|
+
// 升降号贴近数字左上角,按同一 baseline 排在数字左侧;
|
|
208
|
+
// 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
|
|
209
|
+
const accidentalGap = size * 0.01;
|
|
210
|
+
const { placed, right: mainX } = placeAccidentals(this.acc, size, 0, this.numberY, accidentalGap);
|
|
211
|
+
this.accidentals = placed;
|
|
183
212
|
this.box.w = mainX + mainMetrics.w;
|
|
184
|
-
this.box.h =
|
|
213
|
+
this.box.h = mainY + mainMetrics.h;
|
|
185
214
|
this.box.anchor = mainX + mainMetrics.w / 2;
|
|
186
215
|
this.box.visualAxis = mainY + mainMetrics.h / 2;
|
|
187
|
-
this.numberY = mainY + mainMetrics.baseline;
|
|
188
216
|
// 升降号和附点参与完整盒布局,但不计入数字主体范围,减时线因此只画在数字下
|
|
189
217
|
this.ports["body.left"] = { x: this.box.anchor - mainMetrics.w / 2, y: this.box.visualAxis };
|
|
190
218
|
this.ports["body.right"] = { x: this.box.anchor + mainMetrics.w / 2, y: this.box.visualAxis };
|
|
@@ -193,21 +221,10 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
193
221
|
x: this.box.w,
|
|
194
222
|
y: mainY + mainMetrics.h * 0.64,
|
|
195
223
|
};
|
|
196
|
-
// 升降号位于完整盒的左侧区域,但不会进入核心有效范围
|
|
197
|
-
let accidentalX = 0;
|
|
198
|
-
// 所有升降号按同一数字 baseline 排列在数字左侧
|
|
199
|
-
for (const shape of accidentalShapes) {
|
|
200
|
-
this.accidentals.push({
|
|
201
|
-
shape,
|
|
202
|
-
x: accidentalX,
|
|
203
|
-
y: this.numberY - shape.baseline - accidentalRaise,
|
|
204
|
-
});
|
|
205
|
-
accidentalX += shape.w + accidentalGap;
|
|
206
|
-
}
|
|
207
224
|
if (this.octave > 0) {
|
|
208
225
|
// 上八度点从靠近数字的位置向上依次排列
|
|
209
226
|
for (let i = 0; i < dotCount; i++) {
|
|
210
|
-
this.upperOctaveDotY.push(
|
|
227
|
+
this.upperOctaveDotY.push(mainY - dotGap - this.octaveDotRadius - i * dotStep);
|
|
211
228
|
}
|
|
212
229
|
return;
|
|
213
230
|
}
|
|
@@ -242,18 +259,11 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
242
259
|
}
|
|
243
260
|
paint(painter) {
|
|
244
261
|
const color = this.ast.color;
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
painter.drawPath(accidental.shape.commands, { stroke: color, strokeWidth: accidental.shape.strokeWidth }, {
|
|
248
|
-
x: this.box.x + accidental.x,
|
|
249
|
-
y: this.box.y + accidental.y,
|
|
250
|
-
scaleX: accidental.shape.w,
|
|
251
|
-
scaleY: accidental.shape.h,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
262
|
+
for (const accidental of this.accidentals)
|
|
263
|
+
paintAccidental(painter, accidental, this.box, color);
|
|
254
264
|
const glyph = NOTE_GLYPH[this.name] ?? this.name;
|
|
255
265
|
if (glyph) {
|
|
256
|
-
painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, numberStyle);
|
|
266
|
+
painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, this.numberStyle);
|
|
257
267
|
}
|
|
258
268
|
// 上八度点属于主体绘制,不参与下方装饰流
|
|
259
269
|
for (const y of this.upperOctaveDotY) {
|
|
@@ -261,9 +271,10 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
261
271
|
}
|
|
262
272
|
}
|
|
263
273
|
onTimeState(state) {
|
|
264
|
-
const keySignature =
|
|
265
|
-
this.
|
|
274
|
+
const keySignature = state.keySignature;
|
|
275
|
+
this.playbackState = { bpm: state.bpm, velocity: state.velocity, program: state.program };
|
|
266
276
|
this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
|
|
277
|
+
this.transposeDiatonic = createDiatonicTranspose(this.name, this.acc, this.octave, keySignature);
|
|
267
278
|
// 数字音名本身就是简谱显示形式,直接保留原始数字和相对八度
|
|
268
279
|
if (this.name >= "0" && this.name <= "9") {
|
|
269
280
|
if (this.name === "0" || this.name >= "8") {
|
|
@@ -281,4 +292,20 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
281
292
|
}
|
|
282
293
|
}
|
|
283
294
|
}
|
|
295
|
+
emitPlayback(emitter) {
|
|
296
|
+
const percussion = this.ast.name === "9";
|
|
297
|
+
const midi = percussion ? 37 : this.resolvedMidi;
|
|
298
|
+
if (midi === null || emitter.end.compare(emitter.start) <= 0)
|
|
299
|
+
return;
|
|
300
|
+
const noteId = emitter.nextNoteId();
|
|
301
|
+
emitter.emit({
|
|
302
|
+
kind: "note-on",
|
|
303
|
+
at: emitter.start,
|
|
304
|
+
noteId,
|
|
305
|
+
midi,
|
|
306
|
+
velocity: this.playbackState?.velocity ?? DEFAULT_VELOCITY,
|
|
307
|
+
...(percussion ? { percussion: true } : { transpose: this.transposeDiatonic }),
|
|
308
|
+
});
|
|
309
|
+
emitter.emit({ kind: "note-off", at: emitter.end, noteId });
|
|
310
|
+
}
|
|
284
311
|
}
|