jpfun 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Bravura-LICENSE.txt +93 -0
- package/README.md +49 -146
- package/dist/Bravura-LICENSE.txt +93 -0
- package/dist/converter/midi/convert.d.ts +0 -10
- package/dist/converter/midi/convert.js +11 -1
- package/dist/converter/musicxml/features.d.ts +15 -7
- package/dist/converter/musicxml/features.js +87 -18
- package/dist/converter/musicxml/index.js +80 -32
- package/dist/converter/musicxml/model.d.ts +9 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/diagnostic.d.ts +7 -1
- package/dist/diagnostic.js +40 -88
- package/dist/fraction.js +3 -2
- package/dist/functions/ASTtypes.d.ts +2 -18
- package/dist/functions/ASTtypes.js +0 -16
- package/dist/functions/arpeggio/index.js +15 -17
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +102 -0
- package/dist/functions/beam/auto.d.ts +1 -2
- package/dist/functions/beam/auto.js +30 -16
- package/dist/functions/beam/index.js +2 -3
- package/dist/functions/beam/layout.d.ts +1 -1
- package/dist/functions/beam/layout.js +1 -1
- package/dist/functions/br/index.d.ts +34 -2
- package/dist/functions/br/index.js +5 -4
- package/dist/functions/dash/index.js +6 -6
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +1 -0
- package/dist/functions/div/index.js +8 -4
- package/dist/functions/dot/index.js +2 -2
- package/dist/functions/dyn/index.js +49 -31
- package/dist/functions/gliss/index.js +75 -53
- package/dist/functions/grace/index.d.ts +1 -7
- package/dist/functions/grace/index.js +8 -13
- package/dist/functions/head/index.js +1 -1
- package/dist/functions/key/index.js +8 -10
- package/dist/functions/meter/index.d.ts +56 -1
- package/dist/functions/meter/index.js +12 -7
- package/dist/functions/note/index.js +5 -5
- package/dist/functions/note/noteName.d.ts +16 -0
- package/dist/functions/note/noteName.js +88 -0
- package/dist/functions/note/source-transform.js +6 -5
- package/dist/functions/ottava/index.js +24 -8
- package/dist/functions/page/index.d.ts +1 -0
- package/dist/functions/page/index.js +20 -11
- package/dist/functions/pagebreak/index.js +2 -2
- package/dist/functions/rest/index.js +3 -1
- package/dist/functions/set/index.js +4 -4
- package/dist/functions/symbol/index.d.ts +21 -2
- package/dist/functions/symbol/index.js +69 -24
- package/dist/functions/symbol/ornament.d.ts +1 -2
- package/dist/functions/symbol/shape.d.ts +6 -0
- package/dist/functions/symbol/shape.js +16 -3
- package/dist/functions/symbol/symbols/coda.d.ts +4 -0
- package/dist/functions/symbol/symbols/coda.js +56 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
- package/dist/functions/symbol/symbols/dynamics.js +18 -8
- package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
- package/dist/functions/symbol/symbols/harmonic.js +11 -0
- package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
- package/dist/functions/symbol/symbols/marcato.js +22 -0
- package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
- package/dist/functions/symbol/symbols/staccato.js +24 -0
- package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
- package/dist/functions/symbol/symbols/tenuto.js +11 -0
- package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
- package/dist/functions/symbol/symbols/tocoda.js +16 -0
- package/dist/functions/symbol/symbols/turn.d.ts +3 -0
- package/dist/functions/symbol/symbols/turn.js +50 -0
- package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
- package/dist/functions/symbol/symbols/wheel.js +36 -0
- package/dist/functions/temporal.d.ts +1 -0
- package/dist/functions/temporal.js +1 -0
- package/dist/functions/text/index.js +15 -6
- package/dist/functions/tie/index.js +30 -6
- package/dist/functions/tremolo/index.d.ts +2 -0
- package/dist/functions/tremolo/index.js +319 -0
- package/dist/functions/tuplet/index.js +4 -8
- package/dist/functions/up/index.js +2 -2
- package/dist/functions/voice/index.js +17 -10
- package/dist/jpfun.core.min.js +57 -49
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +62 -54
- package/dist/layout/default.d.ts +0 -1
- package/dist/layout/default.js +0 -1
- package/dist/lowering/loweringContext.d.ts +11 -13
- package/dist/lowering/loweringContext.js +26 -33
- package/dist/lowering/types.d.ts +1 -3
- package/dist/parser/parse-utils/note-utils.d.ts +1 -1
- package/dist/parser/parse-utils/note-utils.js +8 -9
- package/dist/parser/parse-utils/string-utils.d.ts +3 -2
- package/dist/parser/parse-utils/string-utils.js +16 -3
- package/dist/parser/parserContext.js +9 -2
- package/dist/pipeline.js +0 -1
- package/dist/playback/compile.js +120 -70
- package/dist/playback/event.d.ts +1 -1
- package/dist/playback/event.js +9 -9
- package/dist/playback/time.js +42 -31
- package/dist/playback/types.d.ts +3 -1
- package/dist/render/canvas.d.ts +2 -0
- package/dist/render/canvas.js +25 -22
- package/dist/render/recording.js +1 -4
- package/dist/render/translate.d.ts +0 -1
- package/dist/render/translate.js +6 -23
- package/grammar.md +26 -20
- package/package.json +3 -3
- package/dist/functions/note/noteNameFSM.d.ts +0 -8
- package/dist/functions/note/noteNameFSM.js +0 -181
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { isVisualTemporalNode, } from "../temporal.js";
|
|
3
3
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
|
+
function findRecordBoundary(records, time, after = false) {
|
|
5
|
+
let low = 0;
|
|
6
|
+
let high = records.length;
|
|
7
|
+
while (low < high) {
|
|
8
|
+
const middle = (low + high) >>> 1;
|
|
9
|
+
const cmp = records[middle].host.t.compare(time);
|
|
10
|
+
if (after ? cmp <= 0 : cmp < 0)
|
|
11
|
+
low = middle + 1;
|
|
12
|
+
else
|
|
13
|
+
high = middle;
|
|
14
|
+
}
|
|
15
|
+
return low;
|
|
16
|
+
}
|
|
4
17
|
class DynFunction extends ASTFunctionNode {
|
|
5
18
|
static def = {
|
|
6
19
|
name: ["dyn"],
|
|
@@ -20,12 +33,12 @@ class DynFunction extends ASTFunctionNode {
|
|
|
20
33
|
{ name: "dv", description: "力度变化量,正数渐强,负数渐弱", type: "number", default: null },
|
|
21
34
|
],
|
|
22
35
|
};
|
|
23
|
-
static loweringFinalize
|
|
36
|
+
static loweringFinalize(result) {
|
|
24
37
|
const dynamics = result.attachments.filter((attachment) => attachment instanceof DynAttachment);
|
|
25
38
|
if (dynamics.length === 0)
|
|
26
39
|
return;
|
|
27
|
-
//
|
|
28
|
-
const
|
|
40
|
+
// 仅收集存在 @dyn 的音轨上的记录,避免无关声部也分配包装对象与排序
|
|
41
|
+
const recordsByTrack = new Map(dynamics.map(d => [d.track, []]));
|
|
29
42
|
for (const nodes of result.astToTemporal.values()) {
|
|
30
43
|
for (const node of nodes) {
|
|
31
44
|
const velocity = node.playbackState?.velocity;
|
|
@@ -34,48 +47,52 @@ class DynFunction extends ASTFunctionNode {
|
|
|
34
47
|
let host = node;
|
|
35
48
|
while (host.foldedInto)
|
|
36
49
|
host = host.foldedInto;
|
|
37
|
-
|
|
50
|
+
recordsByTrack.get(host.track)?.push({ node, host, velocity, increment: 0 });
|
|
38
51
|
}
|
|
39
52
|
}
|
|
40
|
-
|
|
53
|
+
for (const list of recordsByTrack.values()) {
|
|
54
|
+
list.sort((left, right) => left.host.t.compare(right.host.t) || left.node.order - right.node.order);
|
|
55
|
+
}
|
|
41
56
|
// 先累计全部 dyn 的贡献,最后统一写回,避免声明顺序改变后续 dyn 读取的原始力度
|
|
42
57
|
for (const dynamic of dynamics) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
const records = recordsByTrack.get(dynamic.track);
|
|
59
|
+
const endStart = findRecordBoundary(records, dynamic.toHost.t);
|
|
60
|
+
const afterEnd = findRecordBoundary(records, dynamic.toHost.t, true);
|
|
61
|
+
let endVelocity = dynamic.end.playbackState?.velocity;
|
|
62
|
+
for (let i = endStart; endVelocity === undefined && i < afterEnd; i++) {
|
|
63
|
+
if (records[i].host === dynamic.toHost)
|
|
64
|
+
endVelocity = records[i].velocity;
|
|
50
65
|
}
|
|
66
|
+
endVelocity ??= records[afterEnd - 1]?.velocity;
|
|
51
67
|
if (endVelocity === undefined) {
|
|
52
68
|
result.diagnostics.push(new WarningDiagnostic("W_DYN_NO_VELOCITY", "@dyn 终点及此前同轨没有可用的力度基准,仅绘制渐变线", dynamic.sourceSpan));
|
|
53
69
|
continue;
|
|
54
70
|
}
|
|
55
71
|
// 终点后保持完整增量,直到原始力度相对终点再次发生变化
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
72
|
+
let stopIndex = records.length;
|
|
73
|
+
for (let i = afterEnd; i < records.length; i++) {
|
|
74
|
+
if (records[i].velocity !== endVelocity) {
|
|
75
|
+
stopIndex = findRecordBoundary(records, records[i].host.t);
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const startIndex = findRecordBoundary(records, dynamic.fromHost.t);
|
|
59
80
|
const duration = dynamic.toHost.t.clone().sub(dynamic.fromHost.t);
|
|
81
|
+
for (let i = startIndex; i < stopIndex; i++) {
|
|
82
|
+
const progress = i >= endStart ? 1
|
|
83
|
+
: records[i].host.t.clone().sub(dynamic.fromHost.t).div(duration).toNumber();
|
|
84
|
+
records[i].increment += dynamic.dv * progress;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
for (const records of recordsByTrack.values()) {
|
|
60
88
|
for (const record of records) {
|
|
61
|
-
if (record.
|
|
62
|
-
continue;
|
|
63
|
-
const fromStart = record.host.t.compare(dynamic.fromHost.t);
|
|
64
|
-
if (fromStart < 0 || (stop && record.host.t.compare(stop) >= 0))
|
|
89
|
+
if (record.increment === 0)
|
|
65
90
|
continue;
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
: record.host.t.clone().sub(dynamic.fromHost.t).div(duration).toNumber();
|
|
69
|
-
record.increment += dynamic.dv * progress;
|
|
91
|
+
const velocity = Math.max(1, Math.min(127, record.velocity + record.increment));
|
|
92
|
+
record.node.playbackState = { ...record.node.playbackState, velocity };
|
|
70
93
|
}
|
|
71
94
|
}
|
|
72
|
-
|
|
73
|
-
if (record.increment === 0)
|
|
74
|
-
continue;
|
|
75
|
-
const velocity = Math.max(1, Math.min(127, record.velocity + record.increment));
|
|
76
|
-
record.node.playbackState = { ...record.node.playbackState, velocity };
|
|
77
|
-
}
|
|
78
|
-
};
|
|
95
|
+
}
|
|
79
96
|
from;
|
|
80
97
|
to;
|
|
81
98
|
dv;
|
|
@@ -109,6 +126,7 @@ class DynFunction extends ASTFunctionNode {
|
|
|
109
126
|
else {
|
|
110
127
|
ctx.addAttachment(new DynAttachment(this, from, toHost, fromHost));
|
|
111
128
|
}
|
|
129
|
+
ctx.deferFinalize(DynFunction.loweringFinalize);
|
|
112
130
|
return [];
|
|
113
131
|
}
|
|
114
132
|
constructor(span, args, ctx, parent = null) {
|
|
@@ -120,7 +138,7 @@ class DynFunction extends ASTFunctionNode {
|
|
|
120
138
|
}
|
|
121
139
|
dv = Math.max(-127, Math.min(127, dv));
|
|
122
140
|
if (dv !== inputDv) {
|
|
123
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_DYN_DELTA_CLAMPED",
|
|
141
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_DYN_DELTA_CLAMPED", "@dyn 的 dv 必须在 -127 到 127 之间,已被限制为 {dv}", span, { dv }));
|
|
124
142
|
}
|
|
125
143
|
this.from = from;
|
|
126
144
|
this.to = to;
|
|
@@ -7,6 +7,16 @@ function pitchOf(node) {
|
|
|
7
7
|
const { resolvedMidi } = node;
|
|
8
8
|
return typeof resolvedMidi === "number" ? resolvedMidi : undefined;
|
|
9
9
|
}
|
|
10
|
+
function findPitchNode(columns, node) {
|
|
11
|
+
if ("resolvedMidi" in node)
|
|
12
|
+
return pitchOf(node) !== undefined ? node : undefined;
|
|
13
|
+
const host = layoutHost(node);
|
|
14
|
+
for (let i = columns.findIndex(col => col.includes(host)) - 1; i >= 0; i--) {
|
|
15
|
+
const previous = columns[i].find(item => item.track === host.track && "resolvedMidi" in item);
|
|
16
|
+
if (previous)
|
|
17
|
+
return pitchOf(previous) !== undefined ? previous : undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
10
20
|
class GlissFunction extends ASTFunctionNode {
|
|
11
21
|
static def = {
|
|
12
22
|
name: ["gliss"],
|
|
@@ -15,7 +25,7 @@ class GlissFunction extends ASTFunctionNode {
|
|
|
15
25
|
~~~jpfun
|
|
16
26
|
1 -@a 5@b @gliss(a, b)
|
|
17
27
|
~~~
|
|
18
|
-
|
|
28
|
+
若附属对象无音高,将使用前面最近的同轨有音高对象的音高;播放时从起点音段按半音阶离散过渡至终点音高。`,
|
|
19
29
|
allowExtraArgs: false,
|
|
20
30
|
args: [
|
|
21
31
|
{
|
|
@@ -42,7 +52,9 @@ class GlissFunction extends ASTFunctionNode {
|
|
|
42
52
|
if (!from || !to || !isVisualTemporalNode(from) || !isVisualTemporalNode(to)) {
|
|
43
53
|
throw new ErrorDiagnostic("E_GLISS_ENDPOINT", "@gliss 的标签必须指向已生成且具有排版盒子的对象", this.sourceSpan);
|
|
44
54
|
}
|
|
45
|
-
|
|
55
|
+
const attachment = new GlissAttachment(from, to, this.sourceSpan);
|
|
56
|
+
ctx.addAttachment(attachment);
|
|
57
|
+
ctx.deferFinalize(result => attachment.finalize(result), 1);
|
|
46
58
|
return [];
|
|
47
59
|
}
|
|
48
60
|
toString(source) {
|
|
@@ -59,69 +71,83 @@ function layoutHost(node, line) {
|
|
|
59
71
|
return node;
|
|
60
72
|
}
|
|
61
73
|
class GlissAttachment {
|
|
62
|
-
from;
|
|
63
|
-
to;
|
|
64
74
|
sourceSpan;
|
|
65
|
-
diagnostics;
|
|
66
75
|
layer = "foreground";
|
|
67
76
|
endPoints;
|
|
68
77
|
direction;
|
|
69
|
-
|
|
70
|
-
constructor(from, to, sourceSpan, diagnostics) {
|
|
71
|
-
this.from = from;
|
|
72
|
-
this.to = to;
|
|
78
|
+
constructor(from, to, sourceSpan) {
|
|
73
79
|
this.sourceSpan = sourceSpan;
|
|
74
|
-
this.diagnostics = diagnostics;
|
|
75
80
|
this.endPoints = [from, to];
|
|
76
81
|
}
|
|
77
|
-
|
|
82
|
+
finalize({ columns, diagnostics }) {
|
|
78
83
|
const [a, b] = this.endPoints;
|
|
79
84
|
const fromHost = layoutHost(a);
|
|
80
85
|
const toHost = layoutHost(b);
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (fromHost.layoutLine !== toHost.layoutLine)
|
|
85
|
-
throw new ErrorDiagnostic("E_GLISS_CROSS_LINE", "@gliss 不支持跨谱面行", sourceSpan);
|
|
86
|
-
|
|
86
|
+
if (fromHost.t.equals(toHost.t)) {
|
|
87
|
+
throw new ErrorDiagnostic("E_GLISS_ORDER", "@gliss 两个端点不能位于同一时刻", this.sourceSpan);
|
|
88
|
+
}
|
|
89
|
+
if (fromHost.layoutLine !== toHost.layoutLine) {
|
|
90
|
+
throw new ErrorDiagnostic("E_GLISS_CROSS_LINE", "@gliss 不支持跨谱面行", this.sourceSpan);
|
|
91
|
+
}
|
|
92
|
+
if (fromHost.t.compare(toHost.t) > 0)
|
|
93
|
+
this.endPoints = [b, a];
|
|
94
|
+
const [endpoint, target] = this.endPoints;
|
|
95
|
+
const sourcePitchNode = findPitchNode(columns, endpoint);
|
|
96
|
+
const targetPitchNode = findPitchNode(columns, target);
|
|
97
|
+
if (!sourcePitchNode || !targetPitchNode) {
|
|
98
|
+
diagnostics.push(new WarningDiagnostic("W_GLISS_NO_PITCH", "@gliss 端点没有可用音高", this.sourceSpan));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.direction = Math.sign(pitchOf(targetPitchNode) - pitchOf(sourcePitchNode));
|
|
102
|
+
const emit = endpoint.emitPlayback;
|
|
103
|
+
const targetMidi = pitchOf(targetPitchNode) + (targetPitchNode.playbackState?.pitchShift ?? 0);
|
|
104
|
+
endpoint.emitPlayback = emitter => {
|
|
105
|
+
emit?.call(endpoint, emitter);
|
|
106
|
+
const { start: glissStart, end: glissEnd } = emitter;
|
|
107
|
+
emitter.defer(context => {
|
|
108
|
+
for (let i = context.notes.length - 1; i >= 0; i--) {
|
|
109
|
+
const note = context.notes[i];
|
|
110
|
+
if (note.track !== emitter.track)
|
|
111
|
+
continue;
|
|
112
|
+
if (note.end.equals(glissEnd)) {
|
|
113
|
+
context.transform(note, (_ctx, owned) => {
|
|
114
|
+
const item = owned.at(-1);
|
|
115
|
+
const startFrom = glissStart.compare(item.start) > 0 ? glissStart : item.start;
|
|
116
|
+
const fromMidi = item.midi;
|
|
117
|
+
const steps = Math.abs(targetMidi - fromMidi);
|
|
118
|
+
if (item.end.compare(startFrom) <= 0 || steps <= 1)
|
|
119
|
+
return;
|
|
120
|
+
const dir = targetMidi > fromMidi ? 1 : -1;
|
|
121
|
+
const step = item.end.clone().sub(startFrom).div(steps);
|
|
122
|
+
const out = owned.slice(0, -1);
|
|
123
|
+
for (let k = 0; k < steps; k++) {
|
|
124
|
+
out.push({
|
|
125
|
+
...item,
|
|
126
|
+
start: k === 0 ? item.start.clone() : step.clone().mul(k).add(startFrom),
|
|
127
|
+
end: step.clone().mul(k + 1).add(startFrom),
|
|
128
|
+
midi: fromMidi + k * dir,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
};
|
|
87
138
|
}
|
|
88
|
-
/**
|
|
139
|
+
/** 定位端点所在时间列,并为箭头登记水平留白 */
|
|
89
140
|
prepareHorizontal(lines) {
|
|
90
|
-
const [endpoint, target] = this.
|
|
91
|
-
const
|
|
92
|
-
const index = endpoint.layoutLine;
|
|
93
|
-
const line = lines[index];
|
|
141
|
+
const [endpoint, target] = this.endPoints;
|
|
142
|
+
const line = lines[endpoint.layoutLine];
|
|
94
143
|
const from = line && layoutHost(endpoint, line);
|
|
95
144
|
const to = line && layoutHost(target, line);
|
|
96
145
|
const start = from ? line.columnOf(from) : -1;
|
|
97
146
|
const end = to ? line.columnOf(to) : -1;
|
|
98
147
|
if (!line || start < 0 || end <= start) {
|
|
99
|
-
throw new ErrorDiagnostic("E_GLISS_DOMAIN", "@gliss 的端点必须位于同一排版域的不同时间列", sourceSpan);
|
|
148
|
+
throw new ErrorDiagnostic("E_GLISS_DOMAIN", "@gliss 的端点必须位于同一排版域的不同时间列", this.sourceSpan);
|
|
100
149
|
}
|
|
101
|
-
const
|
|
102
|
-
if ("resolvedMidi" in node)
|
|
103
|
-
return pitchOf(node);
|
|
104
|
-
const track = layoutHost(node, line).track;
|
|
105
|
-
// 当前行找不到时继续查找前面的谱面行,不要求时值连续
|
|
106
|
-
for (let i = index; i >= 0; i--) {
|
|
107
|
-
const columns = lines[i].columns;
|
|
108
|
-
// 从端点前一列倒查同轨对象;首个带音高属性的对象即终止搜索,空值也不跳过
|
|
109
|
-
for (let j = i === index ? column - 1 : columns.length - 1; j >= 0; j--) {
|
|
110
|
-
const previous = columns[j].find(item => item.track === track && "resolvedMidi" in item);
|
|
111
|
-
if (previous)
|
|
112
|
-
return pitchOf(previous);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
const sourcePitch = resolvePitch(endpoint, start);
|
|
117
|
-
const targetPitch = resolvePitch(target, end);
|
|
118
|
-
this.direction = sourcePitch === undefined || targetPitch === undefined
|
|
119
|
-
? undefined : Math.sign(targetPitch - sourcePitch);
|
|
120
|
-
if (this.direction === undefined && !this.warned) {
|
|
121
|
-
this.diagnostics.push(new WarningDiagnostic("W_GLISS_NO_PITCH", "@gliss 端点没有可用音高", sourceSpan));
|
|
122
|
-
this.warned = true; // prepareHorizontal 不一定只执行一次 所以要防抖
|
|
123
|
-
}
|
|
124
|
-
const right = from.box.w - from.box.anchor + this.size * 1.4;
|
|
150
|
+
const right = from.box.w - from.box.anchor + Math.max(endpoint.ast.size, target.ast.size) * 1.4;
|
|
125
151
|
// 只扩张求解器占位,不改变端点图形、附点位置或下一轮的 box
|
|
126
152
|
line.registerHorizontalLayoutHook(from, to, ({ columns }) => {
|
|
127
153
|
// 留白作用于起点整列;取最大值,避免多条关系重复累加占位。
|
|
@@ -129,13 +155,9 @@ class GlissAttachment {
|
|
|
129
155
|
element.WR = Math.max(element.WR, right);
|
|
130
156
|
});
|
|
131
157
|
}
|
|
132
|
-
get size() {
|
|
133
|
-
return Math.max(this.from.ast.size, this.to.ast.size);
|
|
134
|
-
}
|
|
135
158
|
createGeometry() {
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
const size = this.size;
|
|
159
|
+
const [from, to] = this.endPoints;
|
|
160
|
+
const size = Math.max(from.ast.size, to.ast.size);
|
|
139
161
|
const gap = size * 0.12;
|
|
140
162
|
const start = { x: from.box.x + from.box.w + gap, y: from.box.y + from.box.visualAxis };
|
|
141
163
|
const end = { x: to.box.x - gap, y: to.box.y + to.box.visualAxis };
|
|
@@ -67,13 +67,7 @@ export declare class GraceTemporal extends TemporalNodeBase {
|
|
|
67
67
|
/** 成员共享全局时间状态,按发声顺序固化 */
|
|
68
68
|
onTimeState(state: TimeState): void | Fraction;
|
|
69
69
|
prepareHorizontal(line: HorizontalLineView): void;
|
|
70
|
-
/**
|
|
71
|
-
* 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
|
|
72
|
-
*
|
|
73
|
-
* 比例 = 倚音块字面时长 / 四分音符,而四分音符 T=1,所以直接取各声部最晚的局部终点。
|
|
74
|
-
* 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
|
|
75
|
-
* 局部时间按同一比例映射进借走的时值,各声部因此同时起奏。
|
|
76
|
-
*/
|
|
70
|
+
/** 按最长声部的书写时长 × 1/4 借时,最多占宿主一半;前倚音借开头,后倚音借末尾。 */
|
|
77
71
|
emitPlayback(emitter: PlaybackEmitter): void;
|
|
78
72
|
/**
|
|
79
73
|
* 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
|
|
@@ -267,8 +267,6 @@ const HOOK_COMMANDS = [
|
|
|
267
267
|
const HOOK_WIDTH = 0.07;
|
|
268
268
|
/** 倚音块底边到宿主肩线的视觉间隙;倚音线会探进这段空间指向宿主 */
|
|
269
269
|
const GRACE_RISE = 0.18;
|
|
270
|
-
/** 倚音向宿主借时值的上限,防止宿主被偷光;只读 */
|
|
271
|
-
const MAX_STEAL_RATIO = new Fraction(3, 4);
|
|
272
270
|
/** 倚音复合体:宿主与倚音都折叠在这一个盒子里,对外只是一个可见事件 */
|
|
273
271
|
export class GraceTemporal extends TemporalNodeBase {
|
|
274
272
|
host;
|
|
@@ -369,13 +367,7 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
369
367
|
prepareHorizontal(line) {
|
|
370
368
|
this.host.prepareHorizontal?.(line);
|
|
371
369
|
}
|
|
372
|
-
/**
|
|
373
|
-
* 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
|
|
374
|
-
*
|
|
375
|
-
* 比例 = 倚音块字面时长 / 四分音符,而四分音符 T=1,所以直接取各声部最晚的局部终点。
|
|
376
|
-
* 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
|
|
377
|
-
* 局部时间按同一比例映射进借走的时值,各声部因此同时起奏。
|
|
378
|
-
*/
|
|
370
|
+
/** 按最长声部的书写时长 × 1/4 借时,最多占宿主一半;前倚音借开头,后倚音借末尾。 */
|
|
379
371
|
emitPlayback(emitter) {
|
|
380
372
|
const written = new Fraction();
|
|
381
373
|
const end = new Fraction();
|
|
@@ -384,12 +376,15 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
384
376
|
if (end.compare(written) > 0)
|
|
385
377
|
written.copyFrom(end);
|
|
386
378
|
});
|
|
387
|
-
const ratio = written.compare(MAX_STEAL_RATIO) > 0 ? MAX_STEAL_RATIO : written;
|
|
388
379
|
const total = emitter.end.clone().sub(emitter.start);
|
|
389
|
-
const
|
|
380
|
+
const scale = new Fraction(1, 4);
|
|
381
|
+
const steal = written.clone().mul(scale);
|
|
382
|
+
const limit = total.clone().divPow2();
|
|
383
|
+
if (steal.compare(limit) > 0) {
|
|
384
|
+
scale.copyFrom(limit).div(written);
|
|
385
|
+
steal.copyFrom(limit);
|
|
386
|
+
}
|
|
390
387
|
const hostDuration = total.sub(steal);
|
|
391
|
-
// 全是零时长标记时块长为 0,成员都落在借时起点
|
|
392
|
-
const scale = written.isZero() ? new Fraction() : steal.clone().div(written);
|
|
393
388
|
const base = this.side === "pre" ? emitter.start.clone() : emitter.start.clone().add(hostDuration);
|
|
394
389
|
if (this.side === "post")
|
|
395
390
|
emitter.play(this.host, emitter.start.clone(), hostDuration);
|
|
@@ -382,7 +382,7 @@ H.tempo: 94
|
|
|
382
382
|
const last = skipSpacesBack(ctx.source, boundary - 1, start);
|
|
383
383
|
const span = { start, end: Math.max(start, last + 1) };
|
|
384
384
|
if (span.start >= span.end) {
|
|
385
|
-
throw new ErrorDiagnostic("E_HEAD_EMPTY_DECLARATION",
|
|
385
|
+
throw new ErrorDiagnostic("E_HEAD_EMPTY_DECLARATION", "H.{field}: 后必须有内容", sugar.span, { field });
|
|
386
386
|
}
|
|
387
387
|
const text = ctx.source.slice(span.start, span.end).trim();
|
|
388
388
|
let parsed;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DEFAULT_TONALITY, TemporalNodeBase } from "../temporal.js";
|
|
2
2
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
3
|
-
import {
|
|
4
|
-
import { parseNoteName } from "../note/
|
|
3
|
+
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
4
|
+
import { parseNoteName } from "../note/noteName.js";
|
|
5
5
|
import { acc2Offset, NoteNameMap, tonality2Midi } from "../../parser/parse-utils/note-utils.js";
|
|
6
6
|
import { paintAccidental, placeAccidentals } from "../note/accidentals.js";
|
|
7
7
|
/**
|
|
@@ -11,13 +11,11 @@ import { paintAccidental, placeAccidentals } from "../note/accidentals.js";
|
|
|
11
11
|
* `C###` 这种超过一个变音记号的写法按实际音高换字母(→ D#4),落在黑键上则跟随原来的升降方向拼写
|
|
12
12
|
*/
|
|
13
13
|
function normalizeTonality(tonality) {
|
|
14
|
-
|
|
15
|
-
tonality2Midi(tonality, 4);
|
|
14
|
+
if (tonality2Midi(tonality) !== null)
|
|
16
15
|
return tonality;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (parsed instanceof Diagnostic || parsed.next !== tonality.length)
|
|
16
|
+
const normalizedInput = tonality.replace(/^[a-g]/, letter => letter.toUpperCase());
|
|
17
|
+
const parsed = parseNoteName(normalizedInput, 0, normalizedInput.length, false);
|
|
18
|
+
if (!parsed || parsed.next !== normalizedInput.length)
|
|
21
19
|
return null;
|
|
22
20
|
const pitchClass = NoteNameMap[parsed.name];
|
|
23
21
|
if (pitchClass === undefined)
|
|
@@ -72,9 +70,9 @@ class KeyFunction extends ASTFunctionNode {
|
|
|
72
70
|
const normalized = normalizeTonality(tonality);
|
|
73
71
|
if (normalized !== tonality) {
|
|
74
72
|
if (ctx.variables.strict)
|
|
75
|
-
throw new ErrorDiagnostic("E_KEY_TONALITY",
|
|
73
|
+
throw new ErrorDiagnostic("E_KEY_TONALITY", '@key 无法解析调性 "{tonality}"', sourceSpan, { tonality });
|
|
76
74
|
this.tonality = normalized ?? DEFAULT_TONALITY;
|
|
77
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_KEY_TONALITY",
|
|
75
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_KEY_TONALITY", '@key 的调性 "{input}" 不是规范写法,已自动切换到 {tonality}', sourceSpan, { input: tonality, tonality: this.tonality }));
|
|
78
76
|
}
|
|
79
77
|
// 归一化保证了格式是 [音名][最多一个 #b][八度]
|
|
80
78
|
this.displayName = this.tonality[0];
|
|
@@ -1,2 +1,57 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Fraction } from "../../fraction.js";
|
|
2
|
+
import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
|
|
3
|
+
import { TemporalNodeBase, type TimeState } from "../temporal.js";
|
|
4
|
+
import type { LoweringContext } from "../../lowering/loweringContext.js";
|
|
5
|
+
import type { Painter } from "../../render/types.js";
|
|
6
|
+
import type { PlaybackEmitter } from "../../playback/types.js";
|
|
7
|
+
import { ASTFunctionNode, type ASTFunctionClass, type ASTNodeBase, type FunctionArgs, type LengthValue, type ParserContext, type SourceSpan } from "../ASTtypes.js";
|
|
8
|
+
export declare function meterChangeAt(node: TemporalNodeBase): MeterFunction | undefined;
|
|
9
|
+
declare class MeterFunction extends ASTFunctionNode {
|
|
10
|
+
static def: {
|
|
11
|
+
name: string[];
|
|
12
|
+
description: string;
|
|
13
|
+
details: string;
|
|
14
|
+
allowExtraArgs: boolean;
|
|
15
|
+
args: ({
|
|
16
|
+
name: string;
|
|
17
|
+
description: string;
|
|
18
|
+
type: "number";
|
|
19
|
+
default: null;
|
|
20
|
+
} | {
|
|
21
|
+
name: string;
|
|
22
|
+
description: string;
|
|
23
|
+
type: "length";
|
|
24
|
+
default: LengthValue;
|
|
25
|
+
})[];
|
|
26
|
+
};
|
|
27
|
+
readonly numerator: number;
|
|
28
|
+
readonly denominator: number;
|
|
29
|
+
readonly measureDuration: Fraction | undefined;
|
|
30
|
+
readonly size: number;
|
|
31
|
+
readonly strict: boolean;
|
|
32
|
+
readonly font: string;
|
|
33
|
+
constructor(span: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
34
|
+
loweringEnter(ctx: LoweringContext): MeterTemporal[];
|
|
35
|
+
/**
|
|
36
|
+
* 校验小节拍数
|
|
37
|
+
* 第一小节特殊规则:只允许不满拍,但不能超拍
|
|
38
|
+
*/
|
|
39
|
+
private static loweringFinalize;
|
|
40
|
+
toString(): string;
|
|
41
|
+
}
|
|
2
42
|
export declare const MeterNode: ASTFunctionClass;
|
|
43
|
+
declare class MeterTemporal extends TemporalNodeBase {
|
|
44
|
+
ast: MeterFunction;
|
|
45
|
+
box: LayoutBox;
|
|
46
|
+
private readonly style;
|
|
47
|
+
private numeratorBaseline;
|
|
48
|
+
private denominatorBaseline;
|
|
49
|
+
private lineY;
|
|
50
|
+
private lineHeight;
|
|
51
|
+
constructor(ast: MeterFunction);
|
|
52
|
+
onTimeState(state: TimeState): void;
|
|
53
|
+
emitPlayback(emitter: PlaybackEmitter): void;
|
|
54
|
+
prepareLayout(context: LayoutPrepareContext): void;
|
|
55
|
+
paint(painter: Painter): void;
|
|
56
|
+
}
|
|
57
|
+
export {};
|
|
@@ -4,8 +4,11 @@ import { ANCHOR_KEY, TemporalNodeBase } from "../temporal.js";
|
|
|
4
4
|
import { paintSymbol } from "../symbol/shape.js";
|
|
5
5
|
import { sanbanBounds, sanbanShapes } from "./shape.js";
|
|
6
6
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
7
|
-
//
|
|
7
|
+
// 同一执行位置只关闭一次旧小节,保留最终拍号和最后源码边界
|
|
8
8
|
const changesByPosition = new WeakMap();
|
|
9
|
+
export function meterChangeAt(node) {
|
|
10
|
+
return changesByPosition.get(node)?.meter;
|
|
11
|
+
}
|
|
9
12
|
function recordMeasureChange(state, node, meter) {
|
|
10
13
|
const position = state.currentEvent ?? node;
|
|
11
14
|
const span = node.ast.sourceSpan;
|
|
@@ -73,14 +76,15 @@ class MeterFunction extends ASTFunctionNode {
|
|
|
73
76
|
this.size = ctx.length2px(size);
|
|
74
77
|
this.strict = ctx.variables.strict;
|
|
75
78
|
}
|
|
76
|
-
loweringEnter() {
|
|
79
|
+
loweringEnter(ctx) {
|
|
80
|
+
ctx.deferFinalize(MeterFunction.loweringFinalize);
|
|
77
81
|
return [new MeterTemporal(this)];
|
|
78
82
|
}
|
|
79
83
|
/**
|
|
80
84
|
* 校验小节拍数
|
|
81
85
|
* 第一小节特殊规则:只允许不满拍,但不能超拍
|
|
82
86
|
*/
|
|
83
|
-
static loweringFinalize
|
|
87
|
+
static loweringFinalize(result) {
|
|
84
88
|
const measureStart = new Fraction();
|
|
85
89
|
const measureLength = new Fraction();
|
|
86
90
|
let active = null;
|
|
@@ -94,11 +98,12 @@ class MeterFunction extends ASTFunctionNode {
|
|
|
94
98
|
measureLength.copyFrom(end).sub(measureStart);
|
|
95
99
|
if (!measureLength.equals(active.measureDuration)
|
|
96
100
|
&& (validationStarted || measureLength.compare(active.measureDuration) > 0)) {
|
|
97
|
-
const message =
|
|
101
|
+
const message = "小节时长为 {duration} QN,不满足 {num}/{den} 拍号";
|
|
98
102
|
const span = { start: sourceStart, end: sourceEnd };
|
|
103
|
+
const params = { duration: measureLength, num: active.numerator, den: active.denominator };
|
|
99
104
|
if (active.strict)
|
|
100
|
-
throw new ErrorDiagnostic("E_METER_MISMATCH", message, span);
|
|
101
|
-
result.diagnostics.push(new WarningDiagnostic("W_METER_MISMATCH", message, span));
|
|
105
|
+
throw new ErrorDiagnostic("E_METER_MISMATCH", message, span, params);
|
|
106
|
+
result.diagnostics.push(new WarningDiagnostic("W_METER_MISMATCH", message, span, params));
|
|
102
107
|
}
|
|
103
108
|
measureStart.copyFrom(end);
|
|
104
109
|
};
|
|
@@ -124,7 +129,7 @@ class MeterFunction extends ASTFunctionNode {
|
|
|
124
129
|
const span = result.columns.at(-1)?.at(-1)?.ast.sourceSpan ?? active.sourceSpan;
|
|
125
130
|
closeMeasure(result.duration, span.end);
|
|
126
131
|
}
|
|
127
|
-
}
|
|
132
|
+
}
|
|
128
133
|
toString() {
|
|
129
134
|
return `@meter(${this.numerator}, ${this.denominator})`;
|
|
130
135
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ASTTextNode, ASTFunctionNode } from "../ASTtypes.js";
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import { parseNoteName } from "./
|
|
3
|
+
import { parseNoteName } from "./noteName.js";
|
|
4
4
|
import { getLayoutBounds } from "../../layout/engine.js";
|
|
5
5
|
import { paintAccidental, placeAccidentals } from "./accidentals.js";
|
|
6
6
|
import { rewriteNotes } from "./source-transform.js";
|
|
@@ -83,8 +83,8 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
83
83
|
},
|
|
84
84
|
};
|
|
85
85
|
// 解析音符名
|
|
86
|
-
const parseResult = parseNoteName(source, start, end);
|
|
87
|
-
if (parseResult
|
|
86
|
+
const parseResult = parseNoteName(source, start, end, false);
|
|
87
|
+
if (!parseResult)
|
|
88
88
|
return null;
|
|
89
89
|
const argMap = new Map();
|
|
90
90
|
argMap.set("name", parseResult.name);
|
|
@@ -140,13 +140,13 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
140
140
|
throw parseResult;
|
|
141
141
|
}
|
|
142
142
|
if (parseResult.next !== this.name.length)
|
|
143
|
-
throw new ErrorDiagnostic("E_WRONG_NOTE_NAME",
|
|
143
|
+
throw new ErrorDiagnostic("E_WRONG_NOTE_NAME", '函数 @note 的参数 [0]:"name" 格式错误: 无法解析尾部 "{tail}"', sourceSpan, { tail: this.name.slice(parseResult.next) });
|
|
144
144
|
this.name = parseResult.name === "X" ? "9" : parseResult.name === "Z" ? "0" : parseResult.name;
|
|
145
145
|
// 校验octave
|
|
146
146
|
const inputOctave = args.get("octave") ?? args.get(2);
|
|
147
147
|
if (inputOctave !== void 0) {
|
|
148
148
|
if (parseResult.octave !== null && parseResult.octave != inputOctave)
|
|
149
|
-
throw new ErrorDiagnostic("E_NOTE_OCTAVE_CONFLICT",
|
|
149
|
+
throw new ErrorDiagnostic("E_NOTE_OCTAVE_CONFLICT", '函数 @note 的参数 "octave" 与音符名称中的八度信息冲突: {inputOctave} != {noteOctave}', sourceSpan, { inputOctave: String(inputOctave), noteOctave: parseResult.octave });
|
|
150
150
|
}
|
|
151
151
|
else {
|
|
152
152
|
// 没有单独传入 octave 时,优先采用音名中的八度;数字音名缺省为相对 0。
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Diagnostic } from "../../diagnostic.js";
|
|
2
|
+
export interface ParsedNoteName {
|
|
3
|
+
name: string;
|
|
4
|
+
next: number;
|
|
5
|
+
acc: string | null;
|
|
6
|
+
octave: number | null;
|
|
7
|
+
absOctave: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 解析音符名称,支持三种语法分支:
|
|
11
|
+
* 1. 字母音名:`[A-GXZ]` + (`[绝对八度][后置升降号]` | `[升降号][绝对八度]`)
|
|
12
|
+
* 2. 数字音名:`[0-9]` + `[后置升降号]` + `[相对八度 ,']`
|
|
13
|
+
* 3. 前置升降号数字音名:`[#bn]+` + `[0-9]` + `[相对八度 ,']`
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseNoteName(str: string, start?: number, end?: number, diagnose?: true): ParsedNoteName | Diagnostic;
|
|
16
|
+
export declare function parseNoteName(str: string, start: number, end: number, diagnose: false): ParsedNoteName | null;
|