jpfun 0.4.0 → 0.5.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 +53 -113
- package/dist/converter/midi/convert.js +14 -7
- package/dist/converter/musicxml/features.d.ts +57 -16
- package/dist/converter/musicxml/features.js +204 -42
- package/dist/converter/musicxml/index.d.ts +7 -2
- package/dist/converter/musicxml/index.js +256 -268
- package/dist/converter/musicxml/model.d.ts +36 -7
- package/dist/converter/musicxml/resolve.d.ts +10 -0
- package/dist/converter/musicxml/resolve.js +152 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/fraction.js +7 -1
- package/dist/functions/ASTtypes.d.ts +4 -4
- package/dist/functions/ASTtypes.js +5 -9
- package/dist/functions/adjust/index.js +16 -53
- package/dist/functions/arpeggio/index.js +27 -15
- package/dist/functions/bar/index.js +7 -8
- package/dist/functions/beam/index.js +6 -1
- package/dist/functions/beam/layout.d.ts +1 -1
- package/dist/functions/beam/layout.js +5 -3
- package/dist/functions/box/index.js +88 -82
- package/dist/functions/br/index.js +9 -5
- package/dist/functions/dash/index.js +24 -13
- package/dist/functions/div/index.d.ts +5 -2
- package/dist/functions/div/index.js +15 -14
- package/dist/functions/dot/index.js +13 -11
- package/dist/functions/dyn/index.js +35 -39
- package/dist/functions/grace/index.d.ts +19 -6
- package/dist/functions/grace/index.js +136 -121
- package/dist/functions/head/index.js +19 -14
- package/dist/functions/key/index.js +8 -4
- package/dist/functions/meter/index.js +22 -2
- package/dist/functions/note/index.d.ts +6 -1
- package/dist/functions/note/index.js +48 -30
- package/dist/functions/note/source-transform.d.ts +21 -0
- package/dist/functions/note/source-transform.js +164 -0
- package/dist/functions/page/index.d.ts +8 -1
- package/dist/functions/page/index.js +23 -13
- package/dist/functions/program/index.js +11 -2
- package/dist/functions/set/index.js +14 -8
- package/dist/functions/stack/index.js +7 -5
- package/dist/functions/symbol/index.d.ts +2 -0
- package/dist/functions/symbol/index.js +11 -7
- package/dist/functions/symbol/ornament.d.ts +3 -3
- package/dist/functions/symbol/ornament.js +21 -35
- package/dist/functions/symbol/symbols/accent.js +5 -5
- package/dist/functions/symbol/symbols/fermata.js +5 -4
- package/dist/functions/tempo/index.js +7 -1
- package/dist/functions/text/index.js +11 -3
- package/dist/functions/tie/index.js +28 -32
- package/dist/functions/tuplet/index.js +26 -24
- package/dist/functions/up/index.js +24 -21
- package/dist/functions/voice/index.js +36 -26
- package/dist/functions/volta/index.js +16 -17
- package/dist/jpfun.core.min.js +98 -129
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +107 -138
- package/dist/layout/engine.d.ts +11 -1
- package/dist/layout/engine.js +299 -96
- package/dist/layout/page.js +2 -0
- package/dist/layout/range.d.ts +26 -0
- package/dist/layout/range.js +75 -0
- package/dist/layout/types.d.ts +33 -4
- package/dist/lowering/types.d.ts +5 -0
- package/dist/parser/parse-utils/length-utils.js +2 -0
- package/dist/parser/parse-utils/note-utils.d.ts +2 -0
- package/dist/parser/parse-utils/note-utils.js +6 -2
- package/dist/parser/parserContext.js +1 -1
- package/dist/playback/compile.d.ts +2 -9
- package/dist/playback/compile.js +297 -186
- package/dist/playback/event.d.ts +8 -16
- package/dist/playback/event.js +13 -3
- package/dist/playback/types.d.ts +76 -18
- package/dist/playback/types.js +1 -0
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/recording.d.ts +1 -0
- package/dist/render/recording.js +21 -0
- package/dist/render/translate.d.ts +14 -0
- package/dist/render/translate.js +48 -0
- package/grammar.md +103 -78
- package/package.json +4 -4
|
@@ -2,48 +2,57 @@ import { ASTTextNode, ASTFunctionNode } from "../ASTtypes.js";
|
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { parseNoteName } from "./noteNameFSM.js";
|
|
4
4
|
import { paintAccidental, placeAccidentals } from "./accidentals.js";
|
|
5
|
+
import { rewriteNotes } from "./source-transform.js";
|
|
6
|
+
/** 根据同一源码的 lowering 生成局部修改;传入 ranges 时只处理完整落入任一选区的音符 */
|
|
7
|
+
export function transformNotes(source, lowering, operation, ranges) {
|
|
8
|
+
const error = lowering.diagnostics.find(item => item instanceof ErrorDiagnostic);
|
|
9
|
+
if (error)
|
|
10
|
+
throw error;
|
|
11
|
+
const notes = [...lowering.astToTemporal.values()].flat()
|
|
12
|
+
.filter((node) => node instanceof NoteTemporalNode);
|
|
13
|
+
const invalid = lowering.diagnostics.find(item => notes.some(note => item.span.start < note.ast.sourceSpan.end && item.span.end > note.ast.sourceSpan.start));
|
|
14
|
+
if (invalid)
|
|
15
|
+
throw invalid;
|
|
16
|
+
return rewriteNotes(source, notes, operation, ranges);
|
|
17
|
+
}
|
|
5
18
|
const JE_OCTAVE_OFFSET = "note.jeOctaveOffset";
|
|
6
19
|
class NoteFunction extends ASTFunctionNode {
|
|
7
20
|
static def = {
|
|
8
21
|
name: ["note", "n"],
|
|
9
22
|
description: "创建音符",
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
兼容无歧义的后置升降号:A3# 等价于 A#3,6#,, 等价于 #6,,
|
|
22
|
-
如果整段升降号后紧跟数字,则升降号属于下一个音符:6#3 解析为 6 和 #3
|
|
23
|
-
|
|
24
|
-
支持 数字音名, 此时 octave 为相对八度
|
|
25
|
-
支持使用数字音名时使用相对八度,如 "1,," 代表在当前基准音(由上下文属性"1=?"决定)的基础上降低两倍八度,"1'" 代表在当前基准音的基础上提高一个八度
|
|
26
|
-
例:#1' === @note("#1'") === @note(1, #, 1)
|
|
27
|
-
`,
|
|
23
|
+
details: `\
|
|
24
|
+
~~~jpfun
|
|
25
|
+
@note(A, acc=#, octave=3, color=#c33)
|
|
26
|
+
~~~
|
|
27
|
+
**简写**:数字音名按“升降号、音名、相对八度”书写;字母音名按“音名、升降号、绝对八度”书写。\`1,,\` 比当前调性基准低两个八度,\`1'\` 高一个八度。
|
|
28
|
+
- 简写的升降号完全由自身决定,省略时为空,不继承 \`@set(note.acc=...)\`;显式调用省略 \`acc\` 时仍继承默认值
|
|
29
|
+
- \`A#3\` 等价于 \`@note(A, #, 3)\`,默认 \`acc\` 为空时也等价于 \`@note(A#3)\`
|
|
30
|
+
- \`#1'\` 等价于 \`@note(1, #, 1)\`,默认 \`acc\` 为空时也等价于 \`@note("#1'")\`
|
|
31
|
+
- 支持组合升降号,如 \`A##bn99\`;无歧义时也接受后置写法,如 \`A3#\`、\`6#,,\`
|
|
32
|
+
- 升降号后紧跟数字时归入下一音符:\`6#3\` 解析为 \`6\` 和 \`#3\``,
|
|
28
33
|
allowExtraArgs: false,
|
|
29
34
|
args: [
|
|
30
35
|
{
|
|
31
36
|
name: "name",
|
|
37
|
+
description: "数字音名或大写字母音名,可自带升降号与八度;`0` / `Z` 为休止符,`8` 为隐形占位,`9` / `X` 为节拍记号,后三类不显示升降号和八度点",
|
|
32
38
|
type: "string",
|
|
33
39
|
default: null,
|
|
34
40
|
},
|
|
35
41
|
{
|
|
36
42
|
name: "acc",
|
|
43
|
+
description: "额外升降号,`##` 再升两个半音,`b` 再降一个半音",
|
|
37
44
|
type: "string",
|
|
38
45
|
default: "",
|
|
39
46
|
},
|
|
40
47
|
{
|
|
41
48
|
name: "octave",
|
|
49
|
+
description: "字母音名使用绝对八度;数字音名使用相对八度,省略时为 0",
|
|
42
50
|
type: "number",
|
|
43
51
|
default: 4, // 如果是数字则默认值为0 需要代码中区分
|
|
44
52
|
},
|
|
45
53
|
{
|
|
46
54
|
name: "color",
|
|
55
|
+
description: "音符颜色,使用 CSS 颜色值",
|
|
47
56
|
type: "string",
|
|
48
57
|
default: "#000",
|
|
49
58
|
}
|
|
@@ -80,8 +89,7 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
80
89
|
argMap.set("name", parseResult.name);
|
|
81
90
|
if (parseResult.octave !== null)
|
|
82
91
|
argMap.set("octave", parseResult.octave);
|
|
83
|
-
|
|
84
|
-
argMap.set("acc", parseResult.acc);
|
|
92
|
+
argMap.set("acc", parseResult.acc ?? ""); // 必须覆盖不然会用到 set 的默认值
|
|
85
93
|
const node = {
|
|
86
94
|
kind: "call",
|
|
87
95
|
typed: true,
|
|
@@ -115,8 +123,11 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
115
123
|
font;
|
|
116
124
|
// JE 八度偏移量
|
|
117
125
|
jeOctaveOffset;
|
|
126
|
+
// 源码改写时判断是否需要覆盖默认升降号
|
|
127
|
+
hasDefaultAcc;
|
|
118
128
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
119
129
|
super(sourceSpan, parent);
|
|
130
|
+
this.hasDefaultAcc = Boolean(ctx.variables["note.acc"]);
|
|
120
131
|
[this.name, this.acc, this.octave, this.color] = this.getArgValue(args, ctx);
|
|
121
132
|
this.font = ctx.variables.numberfont;
|
|
122
133
|
this.size = ctx.variables.fontsize;
|
|
@@ -149,7 +160,7 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
149
160
|
this.acc = parseResult.acc + this.acc;
|
|
150
161
|
}
|
|
151
162
|
toString() {
|
|
152
|
-
return `@n(${this.name}, ${this.acc}, ${this.octave}, ${this.color})`;
|
|
163
|
+
return `@n(${this.name}, ${this.acc || '""'}, ${this.octave}, ${this.color})`;
|
|
153
164
|
}
|
|
154
165
|
}
|
|
155
166
|
export const NoteNode = NoteFunction;
|
|
@@ -160,6 +171,7 @@ const NOTE_GLYPH = { "8": "", "9": "X" };
|
|
|
160
171
|
class NoteTemporalNode extends TemporalNodeBase {
|
|
161
172
|
// 时间固化后的参数
|
|
162
173
|
resolvedMidi = null; // MIDI音高 name是数字则需要在onTimeState中基于当前调性偏移 播放的音高
|
|
174
|
+
keySignature = "C4"; // 用于转调时记录大环境
|
|
163
175
|
transposeDiatonic;
|
|
164
176
|
name; // 数字
|
|
165
177
|
acc; // 升降号
|
|
@@ -196,7 +208,7 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
196
208
|
const { size, color } = this.ast;
|
|
197
209
|
const mainMetrics = context.textMeasurer.measureText("0", this.numberStyle);
|
|
198
210
|
const dotCount = Math.abs(this.octave);
|
|
199
|
-
this.octaveDotRadius = size * 0.
|
|
211
|
+
this.octaveDotRadius = size * 0.075;
|
|
200
212
|
const dotGap = size * 0.08;
|
|
201
213
|
const dotStep = this.octaveDotRadius * 2 + dotGap;
|
|
202
214
|
const octaveSpace = dotCount === 0
|
|
@@ -216,6 +228,7 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
216
228
|
// 升降号和附点参与完整盒布局,但不计入数字主体范围,减时线因此只画在数字下
|
|
217
229
|
this.ports["body.left"] = { x: this.box.anchor - mainMetrics.w / 2, y: this.box.visualAxis };
|
|
218
230
|
this.ports["body.right"] = { x: this.box.anchor + mainMetrics.w / 2, y: this.box.visualAxis };
|
|
231
|
+
this.ports["shoulder"] = { x: this.box.anchor, y: mainY };
|
|
219
232
|
// 覆盖 dot 的默认“右边界 + 视觉轴”,让附点贴合数字字形的视觉位置
|
|
220
233
|
this.ports["dot"] = {
|
|
221
234
|
x: this.box.w,
|
|
@@ -270,8 +283,9 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
270
283
|
painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, this.octaveDotRadius, { fill: color });
|
|
271
284
|
}
|
|
272
285
|
}
|
|
286
|
+
/** 固化记谱位置的音高、力度和音色,并保留基于该调性的相对移调能力 */
|
|
273
287
|
onTimeState(state) {
|
|
274
|
-
const keySignature = state.keySignature;
|
|
288
|
+
const keySignature = this.keySignature = state.keySignature;
|
|
275
289
|
this.playbackState = { bpm: state.bpm, velocity: state.velocity, program: state.program };
|
|
276
290
|
this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
|
|
277
291
|
this.transposeDiatonic = createDiatonicTranspose(this.name, this.acc, this.octave, keySignature);
|
|
@@ -292,20 +306,24 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
292
306
|
}
|
|
293
307
|
}
|
|
294
308
|
}
|
|
309
|
+
/** 发布完整音段或无声区间,实际 NoteOn/NoteOff 留到声音展开完成后生成 */
|
|
295
310
|
emitPlayback(emitter) {
|
|
296
311
|
const percussion = this.ast.name === "9";
|
|
297
312
|
const midi = percussion ? 37 : this.resolvedMidi;
|
|
298
|
-
if (
|
|
313
|
+
if (emitter.end.compare(emitter.start) <= 0)
|
|
314
|
+
return;
|
|
315
|
+
// 休止符和占位符也要保留结构边界,速度效果与后续延长才能覆盖这段时间。
|
|
316
|
+
if (midi === null) {
|
|
317
|
+
emitter.span({ start: emitter.start, end: emitter.end });
|
|
299
318
|
return;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
319
|
+
}
|
|
320
|
+
// 这里只提供发声属性;轨道、来源和最终音符身份由核心统一补齐。
|
|
321
|
+
emitter.note({
|
|
322
|
+
start: emitter.start,
|
|
323
|
+
end: emitter.end,
|
|
305
324
|
midi,
|
|
306
325
|
velocity: this.playbackState?.velocity ?? DEFAULT_VELOCITY,
|
|
307
326
|
...(percussion ? { percussion: true } : { transpose: this.transposeDiatonic }),
|
|
308
327
|
});
|
|
309
|
-
emitter.emit({ kind: "note-off", at: emitter.end, noteId });
|
|
310
328
|
}
|
|
311
329
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ASTNodeBase } from "../ASTtypes.js";
|
|
2
|
+
import type { SourceSpan } from "../../parser/types.js";
|
|
3
|
+
export type NoteTransform = "to-absolute" | "to-relative" | "semitone-up" | "semitone-down";
|
|
4
|
+
export interface SourceEdit {
|
|
5
|
+
span: SourceSpan;
|
|
6
|
+
text: string;
|
|
7
|
+
}
|
|
8
|
+
interface NoteSource {
|
|
9
|
+
ast: ASTNodeBase & {
|
|
10
|
+
name: string;
|
|
11
|
+
acc: string;
|
|
12
|
+
octave: number;
|
|
13
|
+
hasDefaultAcc: boolean;
|
|
14
|
+
jeOctaveOffset: number;
|
|
15
|
+
};
|
|
16
|
+
resolvedMidi: number | null;
|
|
17
|
+
keySignature: string;
|
|
18
|
+
}
|
|
19
|
+
/** edits 始终引用原文;参数只改值,默认参数只在当前调用覆盖,避免影响其他音符 */
|
|
20
|
+
export declare function rewriteNotes(source: string, notes: readonly NoteSource[], operation: NoteTransform, ranges?: readonly SourceSpan[]): SourceEdit[];
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { preprocessSource } from "../../parser/preprocess.js";
|
|
3
|
+
import { readCall } from "../../parser/parse-utils/call-utils.js";
|
|
4
|
+
import { findClosingQuote, quote } from "../../parser/parse-utils/string-utils.js";
|
|
5
|
+
import { NoteNameMap, SHARP_NOTE_NAMES, FLAT_NOTE_NAMES, resolveLetterNameToJianpu, tonality2Midi } from "../../parser/parse-utils/note-utils.js";
|
|
6
|
+
import { parseNoteName } from "./noteNameFSM.js";
|
|
7
|
+
const LETTERS = "CDEFGAB";
|
|
8
|
+
const mod = (value, base) => ((value % base) + base) % base;
|
|
9
|
+
const accidental = (offset) => offset < 0 ? "b".repeat(-offset) : "#".repeat(offset);
|
|
10
|
+
function fail(span) {
|
|
11
|
+
throw new ErrorDiagnostic("E_NOTE_TRANSFORM", "无法安全改写此音符的音高", span);
|
|
12
|
+
}
|
|
13
|
+
/** 音高计算只读固化事实;互转保留调内级数,半音操作采用最少升降号的拼写 */
|
|
14
|
+
function targetPitch(note, operation) {
|
|
15
|
+
const absolute = operation === "to-absolute"
|
|
16
|
+
|| operation !== "to-relative" && LETTERS.includes(note.ast.name);
|
|
17
|
+
const midi = note.resolvedMidi + (operation === "semitone-up" ? 1 : operation === "semitone-down" ? -1 : 0);
|
|
18
|
+
const tonic = tonality2Midi(note.keySignature, 4);
|
|
19
|
+
if (![midi, tonic, note.ast.octave, note.ast.jeOctaveOffset].every(Number.isSafeInteger))
|
|
20
|
+
fail(note.ast.sourceSpan);
|
|
21
|
+
const spelling = operation === "semitone-down" ? FLAT_NOTE_NAMES : SHARP_NOTE_NAMES;
|
|
22
|
+
let name, acc, octave;
|
|
23
|
+
if (operation === "to-absolute") {
|
|
24
|
+
const tonicLetter = LETTERS.indexOf(note.keySignature[0]);
|
|
25
|
+
const base = tonicLetter < 0 ? LETTERS.indexOf(SHARP_NOTE_NAMES[mod(tonic, 12)][0]) : tonicLetter;
|
|
26
|
+
const degree = Number(note.ast.name) - 1;
|
|
27
|
+
name = LETTERS[(base + degree) % 7];
|
|
28
|
+
const tonicAcc = mod(tonic - NoteNameMap[LETTERS[base]] + 6, 12) - 6;
|
|
29
|
+
octave = Math.floor((tonic - tonicAcc) / 12) - 1 + Math.floor((base + degree) / 7)
|
|
30
|
+
+ note.ast.octave + note.ast.jeOctaveOffset;
|
|
31
|
+
acc = accidental(midi - ((octave + 1) * 12 + NoteNameMap[name]));
|
|
32
|
+
}
|
|
33
|
+
else if (operation === "to-relative") {
|
|
34
|
+
const ast = note.ast;
|
|
35
|
+
const key = LETTERS.includes(note.keySignature[0]) ? note.keySignature
|
|
36
|
+
: SHARP_NOTE_NAMES[mod(tonic, 12)] + (Math.floor(tonic / 12) - 1);
|
|
37
|
+
const relative = resolveLetterNameToJianpu(ast.name, ast.acc, ast.octave + ast.jeOctaveOffset, key);
|
|
38
|
+
if (!relative)
|
|
39
|
+
fail(ast.sourceSpan);
|
|
40
|
+
({ renderName: name, renderAcc: acc, renderOctave: octave } = relative);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const value = absolute ? midi : midi - tonic;
|
|
44
|
+
const pitch = spelling[mod(value, 12)];
|
|
45
|
+
name = absolute ? pitch[0] : String(LETTERS.indexOf(pitch[0]) + 1);
|
|
46
|
+
acc = pitch.slice(1);
|
|
47
|
+
octave = Math.floor(value / 12) - (absolute ? 1 : 0);
|
|
48
|
+
}
|
|
49
|
+
return { name, acc, octave: octave - note.ast.jeOctaveOffset, absolute };
|
|
50
|
+
}
|
|
51
|
+
/** 仅在函数参数的顶层保护低八度逗号;已有大括号内的简写无需再包装 */
|
|
52
|
+
function needsBraces(masked, ast) {
|
|
53
|
+
for (let parent = ast.parent; parent; parent = parent.parent) {
|
|
54
|
+
const call = readCall(masked, parent.sourceSpan.start, parent.sourceSpan.end);
|
|
55
|
+
if (!call)
|
|
56
|
+
continue;
|
|
57
|
+
const arg = call.args.find(item => item.valueSpan.start <= ast.sourceSpan.start
|
|
58
|
+
&& item.valueSpan.end >= ast.sourceSpan.end);
|
|
59
|
+
if (!arg)
|
|
60
|
+
continue;
|
|
61
|
+
let depth = 0;
|
|
62
|
+
for (let position = arg.valueSpan.start; position < ast.sourceSpan.start; position++) {
|
|
63
|
+
if (masked[position] === '"')
|
|
64
|
+
position = findClosingQuote(masked, position, arg.valueSpan.end);
|
|
65
|
+
else if (masked[position] === "{")
|
|
66
|
+
depth++;
|
|
67
|
+
else if (masked[position] === "}")
|
|
68
|
+
depth--;
|
|
69
|
+
}
|
|
70
|
+
return depth === 0;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
/** edits 始终引用原文;参数只改值,默认参数只在当前调用覆盖,避免影响其他音符 */
|
|
75
|
+
export function rewriteNotes(source, notes, operation, ranges) {
|
|
76
|
+
const { maskedSource: masked } = preprocessSource(source);
|
|
77
|
+
const edits = [];
|
|
78
|
+
const tokens = [];
|
|
79
|
+
function replace(span, text) {
|
|
80
|
+
if (source.slice(span.start, span.end) === text)
|
|
81
|
+
return;
|
|
82
|
+
if (source.slice(span.start, span.end) !== masked.slice(span.start, span.end))
|
|
83
|
+
fail(span);
|
|
84
|
+
edits.push({ span, text });
|
|
85
|
+
}
|
|
86
|
+
for (const note of notes) {
|
|
87
|
+
const { ast } = note;
|
|
88
|
+
const span = ast.sourceSpan;
|
|
89
|
+
const call = readCall(masked, span.start, span.end);
|
|
90
|
+
const explicit = call?.span.end === span.end && call.closeParenSpan;
|
|
91
|
+
const already = operation === "to-absolute" && LETTERS.includes(ast.name)
|
|
92
|
+
|| operation === "to-relative" && /^[1-7]$/.test(ast.name);
|
|
93
|
+
if (!explicit)
|
|
94
|
+
tokens.push({ span, text: source.slice(span.start, span.end) });
|
|
95
|
+
if (ranges && !ranges.some(range => range.start <= span.start && range.end >= span.end))
|
|
96
|
+
continue;
|
|
97
|
+
if (note.resolvedMidi === null || already)
|
|
98
|
+
continue;
|
|
99
|
+
const pitch = targetPitch(note, operation);
|
|
100
|
+
const octaveText = pitch.absolute ? String(pitch.octave)
|
|
101
|
+
: pitch.octave < 0 ? ",".repeat(-pitch.octave) : "'".repeat(pitch.octave);
|
|
102
|
+
const nameText = (acc, octave) => pitch.absolute
|
|
103
|
+
? pitch.name + acc + octave : acc + pitch.name + octave;
|
|
104
|
+
if (!explicit) {
|
|
105
|
+
let text = nameText(pitch.acc, octaveText);
|
|
106
|
+
if (text.includes(",") && needsBraces(masked, ast))
|
|
107
|
+
text = `{${text}}`;
|
|
108
|
+
tokens[tokens.length - 1].text = text;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const slots = new Map();
|
|
112
|
+
call.args.forEach((arg, index) => {
|
|
113
|
+
if (arg.valueSpan.start === arg.valueSpan.end)
|
|
114
|
+
return;
|
|
115
|
+
slots.set(arg.nameSpan ? masked.slice(arg.nameSpan.start, arg.nameSpan.end).toLowerCase() : index, arg.valueSpan);
|
|
116
|
+
});
|
|
117
|
+
const nameSlot = slots.get("name") ?? slots.get(0);
|
|
118
|
+
const accSlot = slots.get("acc") ?? slots.get(1);
|
|
119
|
+
const octaveSlot = slots.get("octave") ?? slots.get(2);
|
|
120
|
+
const additions = [];
|
|
121
|
+
function write(slot, name, value) {
|
|
122
|
+
const text = !value || value.includes(",")
|
|
123
|
+
|| slot && source[slot.start] === '"' ? quote(value) : value;
|
|
124
|
+
if (slot)
|
|
125
|
+
replace(slot, text);
|
|
126
|
+
else
|
|
127
|
+
additions.push(`${name}=${text}`);
|
|
128
|
+
}
|
|
129
|
+
write(nameSlot, "name", nameText(accSlot ? "" : pitch.acc, octaveSlot ? "" : octaveText));
|
|
130
|
+
if (accSlot)
|
|
131
|
+
write(accSlot, "acc", pitch.acc);
|
|
132
|
+
else if (ast.hasDefaultAcc)
|
|
133
|
+
additions.push('acc=""');
|
|
134
|
+
if (octaveSlot)
|
|
135
|
+
replace(octaveSlot, String(pitch.octave));
|
|
136
|
+
if (additions.length) {
|
|
137
|
+
const at = call.closeParenSpan.start;
|
|
138
|
+
const prefix = masked.slice(call.openParenSpan.end, at).trimEnd();
|
|
139
|
+
replace({ start: at, end: at }, (prefix ? prefix.endsWith(",") ? " " : ", " : "") + additions.join(", "));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// 相邻原子改写后仍须是两个音符,例如 C4D4 -> C4 2,不能合并成 C42。
|
|
143
|
+
tokens.sort((left, right) => left.span.start - right.span.start);
|
|
144
|
+
for (let index = 0; index < tokens.length - 1; index++) {
|
|
145
|
+
const left = tokens[index], right = tokens[index + 1];
|
|
146
|
+
if (left.span.end !== right.span.start || left.text.endsWith("}"))
|
|
147
|
+
continue;
|
|
148
|
+
const parsed = parseNoteName(left.text + right.text);
|
|
149
|
+
if (parsed instanceof Diagnostic || parsed.next !== left.text.length) {
|
|
150
|
+
if (left.text !== source.slice(left.span.start, left.span.end))
|
|
151
|
+
left.text += " ";
|
|
152
|
+
else
|
|
153
|
+
right.text = " " + right.text;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const token of tokens)
|
|
157
|
+
replace(token.span, token.text);
|
|
158
|
+
edits.sort((left, right) => left.span.start - right.span.start);
|
|
159
|
+
for (let index = 1; index < edits.length; index++) {
|
|
160
|
+
if (edits[index - 1].span.end > edits[index].span.start)
|
|
161
|
+
fail(edits[index].span);
|
|
162
|
+
}
|
|
163
|
+
return edits;
|
|
164
|
+
}
|
|
@@ -7,16 +7,23 @@ export declare class PageFunction extends ASTFunctionNode {
|
|
|
7
7
|
static def: {
|
|
8
8
|
name: string[];
|
|
9
9
|
description: string;
|
|
10
|
-
|
|
10
|
+
details: string;
|
|
11
11
|
allowExtraArgs: boolean;
|
|
12
12
|
args: ({
|
|
13
13
|
name: string;
|
|
14
|
+
description: string;
|
|
14
15
|
type: "length";
|
|
15
16
|
default: LengthValue;
|
|
16
17
|
} | {
|
|
17
18
|
name: string;
|
|
19
|
+
description: string;
|
|
18
20
|
type: "string";
|
|
19
21
|
default: string;
|
|
22
|
+
} | {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
type: "number";
|
|
26
|
+
default: number;
|
|
20
27
|
})[];
|
|
21
28
|
};
|
|
22
29
|
readonly config: PageConfig | null;
|
|
@@ -45,20 +45,28 @@ class PageNumberAttachment {
|
|
|
45
45
|
export class PageFunction extends ASTFunctionNode {
|
|
46
46
|
static def = {
|
|
47
47
|
name: ["page"],
|
|
48
|
-
description: "
|
|
49
|
-
|
|
48
|
+
description: "设置文档页面",
|
|
49
|
+
details: `\
|
|
50
|
+
~~~jpfun
|
|
51
|
+
@page(
|
|
52
|
+
width=794px, height=1123px, top=48px, bottom=48px,
|
|
53
|
+
left=40px, right=40px, gap=1em, numbering="1/1", fillRatio=0.5
|
|
54
|
+
)
|
|
55
|
+
~~~
|
|
56
|
+
尺寸接受 \`px\` 或 \`em\`。页边距和行距须非负;左右边距之和须小于页宽,固定页高时上下边距之和须小于页高。`,
|
|
50
57
|
allowExtraArgs: false,
|
|
51
58
|
args: [
|
|
52
|
-
{ name: "width", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
|
|
53
|
-
{ name: "height", type: "length", default: px(0) },
|
|
54
|
-
{ name: "top", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginTop) },
|
|
55
|
-
{ name: "bottom", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginBottom) },
|
|
56
|
-
{ name: "left", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
|
|
57
|
-
{ name: "right", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
|
|
58
|
-
{ name: "gap", type: "length", default: { value: 1, unit: "em" } },
|
|
59
|
+
{ name: "width", description: "页面宽度,须大于左右页边距之和", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
|
|
60
|
+
{ name: "height", description: "页面高度,0 表示不分页、高度随内容增长;宽度为 `794px` 时,A4 对应高度约为 `1123px`", type: "length", default: px(0) },
|
|
61
|
+
{ name: "top", description: "页面上边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginTop) },
|
|
62
|
+
{ name: "bottom", description: "页面下边距,启用页码时至少为 `16px`", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginBottom) },
|
|
63
|
+
{ name: "left", description: "页面左边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
|
|
64
|
+
{ name: "right", description: "页面右边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
|
|
65
|
+
{ name: "gap", description: "谱面行之间的最小间距", type: "length", default: { value: 1, unit: "em" } },
|
|
59
66
|
// 单个 1 取当前页;多个 1 中最后一个取总页数
|
|
60
|
-
{ name: "numbering", type: "string", default: "" },
|
|
61
|
-
{ name: "font", type: "string", default: "" },
|
|
67
|
+
{ name: "numbering", description: "页码格式,空值隐藏;`1` 显示当前页,`1/1` 显示当前页/总页数", type: "string", default: "" },
|
|
68
|
+
{ name: "font", description: "页码字体,空值沿用当前字体设置", type: "string", default: "" },
|
|
69
|
+
{ name: "fillRatio", description: "自然宽度达到该比例后撑满", type: "number", default: DEFAULT_PAGE_CONFIG.fillRatio },
|
|
62
70
|
],
|
|
63
71
|
};
|
|
64
72
|
config;
|
|
@@ -79,6 +87,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
79
87
|
}
|
|
80
88
|
ctx.documentDeclarations["page"] = true;
|
|
81
89
|
const values = this.getArgValue(args, ctx);
|
|
90
|
+
const fillRatio = values.pop();
|
|
82
91
|
this.font = values.pop() || ctx.variables.font;
|
|
83
92
|
const numbering = values.pop();
|
|
84
93
|
const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
|
|
@@ -102,6 +111,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
102
111
|
marginLeft: left,
|
|
103
112
|
marginRight: right,
|
|
104
113
|
lineGap: gap,
|
|
114
|
+
fillRatio,
|
|
105
115
|
});
|
|
106
116
|
this.numbering = numbering;
|
|
107
117
|
}
|
|
@@ -117,8 +127,8 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
117
127
|
toString() {
|
|
118
128
|
if (!this.config)
|
|
119
129
|
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)})`;
|
|
130
|
+
const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap, fillRatio } = this.config;
|
|
131
|
+
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)}, fillRatio=${fillRatio})`;
|
|
122
132
|
}
|
|
123
133
|
}
|
|
124
134
|
export const PageNode = PageFunction;
|
|
@@ -5,9 +5,18 @@ class ProgramFunction extends ASTFunctionNode {
|
|
|
5
5
|
static def = {
|
|
6
6
|
name: ["program", "instrument"],
|
|
7
7
|
description: "设置当前音轨的 MIDI 音色",
|
|
8
|
-
|
|
8
|
+
details: `\
|
|
9
|
+
~~~jpfun
|
|
10
|
+
@program(40) 1 2 3
|
|
11
|
+
~~~
|
|
12
|
+
切换为小提琴音色。设置将作用于当前音轨的后续音符。`,
|
|
9
13
|
allowExtraArgs: false,
|
|
10
|
-
args: [{
|
|
14
|
+
args: [{
|
|
15
|
+
name: "program",
|
|
16
|
+
description: "从 0 开始编号的 MIDI 音色,须为 `0..127` 的整数",
|
|
17
|
+
type: "number",
|
|
18
|
+
default: null
|
|
19
|
+
}],
|
|
11
20
|
};
|
|
12
21
|
program;
|
|
13
22
|
constructor(span, args, ctx, parent = null) {
|
|
@@ -6,14 +6,16 @@ class SetFunction extends ASTFunctionNode {
|
|
|
6
6
|
static def = {
|
|
7
7
|
name: ["set"],
|
|
8
8
|
description: "设置局部默认值",
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@set(strict=true)
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
details: `\
|
|
10
|
+
~~~jpfun
|
|
11
|
+
@set(fontsize=20, strict=true)
|
|
12
|
+
{ @set(note.octave=5, note.color=#f00) C D E }
|
|
13
|
+
~~~
|
|
14
|
+
接受命名参数,可一次设置多项:
|
|
15
|
+
- **系统选项**:如 \`fontsize=20\` 将字号设为 20px,\`strict=true\` 开启严格模式
|
|
16
|
+
- **函数默认参数**:按 \`函数主名.参数名=值\` 书写,例如 \`note.octave=5\`、\`note.color=#f00\`;显式传参优先
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
`,
|
|
18
|
+
设置从当前位置起生效,限于当前作用域及其子作用域;离开内容块后恢复外层设置。它按源码作用域解析,与演奏时间无关。`,
|
|
17
19
|
allowExtraArgs: true,
|
|
18
20
|
args: []
|
|
19
21
|
};
|
|
@@ -60,7 +62,11 @@ class SetFunction extends ASTFunctionNode {
|
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
toString() {
|
|
63
|
-
return `@set(${Array.from(this.args, ([key, value]) =>
|
|
65
|
+
return `@set(${Array.from(this.args, ([key, value]) => {
|
|
66
|
+
const text = typeof value === "string" ? quote(value)
|
|
67
|
+
: value && typeof value === "object" && "unit" in value ? `${value.value}${value.unit}` : value;
|
|
68
|
+
return `${key}=${text}`;
|
|
69
|
+
}).join(", ")})`;
|
|
64
70
|
}
|
|
65
71
|
}
|
|
66
72
|
export const SetNode = SetFunction;
|
|
@@ -39,11 +39,13 @@ class StackFunction extends ASTFunctionNode {
|
|
|
39
39
|
static def = {
|
|
40
40
|
name: ["stack"],
|
|
41
41
|
description: "临时多声部",
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
{
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
details: `\
|
|
43
|
+
~~~jpfun
|
|
44
|
+
@stack({1 2}, {3/ 4/ 5})
|
|
45
|
+
~~~
|
|
46
|
+
接受至少两个内容参数,各分支同时开始、保留各自时值,整体时长取最长分支。适合节奏不同的临时多声部。
|
|
47
|
+
|
|
48
|
+
**简写**:\`{1 2} & {3/ 4/ 5}\`。可继续用 \`&\` 连接更多分支;若要写和弦,请使用 \`up\` / \`down\`。`,
|
|
47
49
|
allowExtraArgs: true,
|
|
48
50
|
extraArgType: "content",
|
|
49
51
|
args: [],
|
|
@@ -19,6 +19,8 @@ export interface SymbolDefinition {
|
|
|
19
19
|
};
|
|
20
20
|
/** 符号高度 = size × weight,用来修正宽扁字形的视觉重量;默认 1 */
|
|
21
21
|
readonly weight?: number;
|
|
22
|
+
/** 图形下方留白,使用符号自身坐标并随 scale 缩放;默认 0 */
|
|
23
|
+
readonly paddingBottom?: number;
|
|
22
24
|
/** 写入本位置之后持续生效的状态,例如力度记号的 velocity */
|
|
23
25
|
readonly onTimeState?: (state: TimeState) => void;
|
|
24
26
|
/** 发布系统控制,或注册对同一折叠序列后续音符的局部变换 */
|
|
@@ -19,21 +19,25 @@ class SymbolFunction extends ASTFunctionNode {
|
|
|
19
19
|
static def = {
|
|
20
20
|
name: ["symbol", "$"],
|
|
21
21
|
description: "创建一个乐谱符号",
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
details: `\
|
|
23
|
+
~~~jpfun
|
|
24
|
+
1 ^ @symbol(p, size=0.5em)
|
|
25
|
+
~~~
|
|
26
|
+
**简写**:\`$name\`,如 \`1 ^ $p\`
|
|
27
|
+
|
|
28
|
+
**内置记号**
|
|
29
|
+
${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.description}`).join("\n")}`,
|
|
28
30
|
allowExtraArgs: false,
|
|
29
31
|
args: [
|
|
30
32
|
{
|
|
31
33
|
name: "name",
|
|
34
|
+
description: "内置记号名称,见函数说明中的记号列表",
|
|
32
35
|
type: "string",
|
|
33
36
|
default: null
|
|
34
37
|
},
|
|
35
38
|
{
|
|
36
39
|
name: "size",
|
|
40
|
+
description: "记号尺寸",
|
|
37
41
|
type: "length",
|
|
38
42
|
default: { value: 0.5, unit: "em" },
|
|
39
43
|
},
|
|
@@ -117,7 +121,7 @@ class SymbolTemporal extends TemporalNodeBase {
|
|
|
117
121
|
else {
|
|
118
122
|
this.scale = this.height / this.bounds.h;
|
|
119
123
|
this.box.w = this.bounds.w * this.scale;
|
|
120
|
-
this.box.h = this.height;
|
|
124
|
+
this.box.h = this.height + (this.ast.definition.paddingBottom ?? 0) * this.scale;
|
|
121
125
|
}
|
|
122
126
|
this.box.anchor = this.box.w / 2;
|
|
123
127
|
this.box.visualAxis = this.box.h / 2;
|
|
@@ -6,10 +6,10 @@ interface OrnamentPattern {
|
|
|
6
6
|
readonly repeat?: boolean;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* 按调内音级模式拆分传入音段,使用结构完成后的范围和最终播放速度
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
11
|
+
* 重复式模式按速度计算次数,固定模式在整段时值内均分。
|
|
12
|
+
* 派生音段继承轨道和来源,仍只声明音段,不直接发布 NoteOn/NoteOff。
|
|
13
13
|
*/
|
|
14
14
|
export declare function ornament(pattern: OrnamentPattern): PlaybackTransform;
|
|
15
15
|
export {};
|