jpfun 0.2.0 → 0.3.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 +69 -11
- 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 +24 -7
- package/dist/functions/ASTtypes.js +35 -16
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +39 -10
- package/dist/functions/beam/auto.js +32 -15
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +3 -2
- package/dist/functions/beam/layout.js +42 -64
- package/dist/functions/box/index.js +35 -34
- package/dist/functions/br/index.js +9 -7
- package/dist/functions/dash/index.js +33 -1
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +11 -3
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +82 -76
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +97 -3
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +154 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +45 -65
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +37 -29
- package/dist/functions/stack/index.js +26 -22
- package/dist/functions/symbol/index.d.ts +21 -0
- package/dist/functions/symbol/index.js +94 -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 +22 -0
- package/dist/functions/symbol/shape.js +48 -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/f.d.ts +2 -0
- package/dist/functions/symbol/symbols/f.js +55 -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/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +43 -0
- package/dist/functions/symbol/symbols/p.d.ts +2 -0
- package/dist/functions/symbol/symbols/p.js +61 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +75 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +53 -2
- package/dist/functions/text/index.js +76 -24
- package/dist/functions/tie/index.js +102 -127
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +245 -171
- package/dist/functions/voice/index.js +86 -68
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +227 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/jpfun.min.js +143 -0
- package/dist/layout/engine.d.ts +4 -11
- package/dist/layout/engine.js +65 -59
- package/dist/layout/model.d.ts +8 -1
- package/dist/layout/model.js +16 -6
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +51 -18
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +18 -20
- package/dist/lowering/loweringContext.js +132 -94
- package/dist/lowering/track.d.ts +1 -1
- package/dist/lowering/track.js +1 -1
- package/dist/lowering/types.d.ts +49 -12
- package/dist/lowering/types.js +9 -3
- 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 +8 -0
- package/dist/parser/parse-utils/string-utils.js +20 -0
- package/dist/parser/parserContext.d.ts +8 -5
- package/dist/parser/parserContext.js +38 -33
- package/dist/parser/preprocess.js +17 -11
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +401 -0
- package/dist/playback/event.d.ts +91 -0
- package/dist/playback/event.js +87 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +128 -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 +17 -0
- package/dist/render/text.js +35 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +93 -10
- package/package.json +10 -6
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Fraction } from "../fraction.js";
|
|
3
|
+
import { ANCHOR_KEY, DEFAULT_BPM, DEFAULT_TONALITY, DEFAULT_VELOCITY, } from "./types.js";
|
|
3
4
|
import { Track } from "./track.js";
|
|
4
5
|
/**
|
|
5
6
|
* 把 AST 展开并固化为时间列、attachment 和页面配置
|
|
@@ -13,7 +14,7 @@ import { Track } from "./track.js";
|
|
|
13
14
|
* 大多数 AST 函数通过以下入口参与 lowering(按时间顺序):
|
|
14
15
|
* - [hook] loweringEnter/loweringExit:进入或离开节点时生成 TemporalNode、管理作用域
|
|
15
16
|
* - [API] LoweringContext.beginLoweringGroup/endLoweringGroup:通常在 enter/exit 中被调用,以栈方式观察当前子树产生的 TemporalNode 和 attachment
|
|
16
|
-
* - [API] LoweringContext.
|
|
17
|
+
* - [API] LoweringContext.addAttachment:注册不推进时间的附属对象;当前外层分组会收到该对象
|
|
17
18
|
* - [config] timeFlowModel:声明子节点按 sequence 或 parallel 推进时间;parallel 还要声明音轨复用键与纵向排列策略
|
|
18
19
|
* - [hook] Temporal.onTimeState:锚点归并后固化调性、速度等状态
|
|
19
20
|
* - [hook] loweringAugment:最终列和 layoutLine 固化后扫描结果并生成要追加的 attachment
|
|
@@ -23,7 +24,8 @@ export class LoweringContext {
|
|
|
23
24
|
loweringAugmenters = [];
|
|
24
25
|
loweringFinalizers = [];
|
|
25
26
|
cnt = 0; // 生成唯一id的计数器
|
|
26
|
-
|
|
27
|
+
usedTracks = new Set();
|
|
28
|
+
/** 不推进时间的附属对象 */
|
|
27
29
|
attachments = [];
|
|
28
30
|
/** 当前完整文档的页面配置 */
|
|
29
31
|
page;
|
|
@@ -66,20 +68,23 @@ export class LoweringContext {
|
|
|
66
68
|
*/
|
|
67
69
|
lowerDocument(node) {
|
|
68
70
|
this.cnt = 0;
|
|
71
|
+
this.usedTracks.clear();
|
|
69
72
|
this.attachments = [];
|
|
70
73
|
this.page = void 0;
|
|
71
74
|
this.astToTemporal = new Map();
|
|
72
75
|
this.activeLoweringGroups = [];
|
|
73
76
|
const rootTrack = new Track();
|
|
74
|
-
const
|
|
77
|
+
const duration = new Fraction();
|
|
78
|
+
const columns = this.trackedEvents(node, duration, rootTrack);
|
|
75
79
|
this.solidifyColumns(columns);
|
|
76
80
|
return this.postprocessResult({
|
|
77
81
|
diagnostics: this.diagnostics,
|
|
78
82
|
columns,
|
|
79
83
|
attachments: this.attachments,
|
|
80
84
|
astToTemporal: this.astToTemporal,
|
|
81
|
-
duration
|
|
85
|
+
duration,
|
|
82
86
|
rootTrack,
|
|
87
|
+
tracks: [...this.usedTracks],
|
|
83
88
|
page: this.page,
|
|
84
89
|
});
|
|
85
90
|
}
|
|
@@ -100,7 +105,11 @@ export class LoweringContext {
|
|
|
100
105
|
column.layoutLine = line;
|
|
101
106
|
}
|
|
102
107
|
// 行号全部就位后再固化时间状态,up 等节点才能把行号转发给内部成员
|
|
103
|
-
const state = {
|
|
108
|
+
const state = {
|
|
109
|
+
bpm: DEFAULT_BPM,
|
|
110
|
+
velocity: DEFAULT_VELOCITY,
|
|
111
|
+
keySignature: DEFAULT_TONALITY,
|
|
112
|
+
};
|
|
104
113
|
for (const column of columns) {
|
|
105
114
|
for (const node of column)
|
|
106
115
|
node.onTimeState?.(state);
|
|
@@ -124,7 +133,8 @@ export class LoweringContext {
|
|
|
124
133
|
if (active.owner !== owner)
|
|
125
134
|
throw new Error("Lowering groups must end in reverse order");
|
|
126
135
|
if (active.group.attachment)
|
|
127
|
-
this.
|
|
136
|
+
this.addAttachment(active.group.attachment);
|
|
137
|
+
return active.group;
|
|
128
138
|
}
|
|
129
139
|
/** 先统一生成派生 attachment,再按注册顺序执行最终处理 */
|
|
130
140
|
postprocessResult(result) {
|
|
@@ -136,7 +146,7 @@ export class LoweringContext {
|
|
|
136
146
|
additions.push(attachment);
|
|
137
147
|
}
|
|
138
148
|
for (const attachment of additions)
|
|
139
|
-
this.
|
|
149
|
+
this.addAttachment(attachment);
|
|
140
150
|
// 进行校验或者其他动作
|
|
141
151
|
for (const finalize of this.loweringFinalizers)
|
|
142
152
|
finalize(result);
|
|
@@ -146,27 +156,16 @@ export class LoweringContext {
|
|
|
146
156
|
* 注册不推进时间的排版对象
|
|
147
157
|
* 被 tie、beam、box 等函数使用
|
|
148
158
|
*/
|
|
149
|
-
|
|
159
|
+
addAttachment(attachment) {
|
|
150
160
|
this.attachments.push(attachment);
|
|
151
|
-
|
|
152
|
-
|
|
161
|
+
// 由内向外分组,符合嵌套作用域语义
|
|
162
|
+
for (let i = this.activeLoweringGroups.length - 1; i >= 0; i--) {
|
|
163
|
+
this.activeLoweringGroups[i].group.onAttachment?.(attachment);
|
|
153
164
|
}
|
|
154
165
|
}
|
|
155
|
-
/**
|
|
156
|
-
* 查询 AST 节点在当前 lowering 中产生的所有 temporal 节点
|
|
157
|
-
*
|
|
158
|
-
* 被折叠进别人盒子的成员没有独立的全局位置,统一上溯到宿主,
|
|
159
|
-
* 所以写在成员上的标签等价于写在整个复合节点上。
|
|
160
|
-
*/
|
|
166
|
+
/** 查询 AST 自己产生的 temporal;是否沿 foldedInto 投影由调用者决定 */
|
|
161
167
|
getTemporalNodes(ast) {
|
|
162
|
-
|
|
163
|
-
if (!nodes)
|
|
164
|
-
return [];
|
|
165
|
-
return nodes.map(node => {
|
|
166
|
-
while (node.foldedInto)
|
|
167
|
-
node = node.foldedInto;
|
|
168
|
-
return node;
|
|
169
|
-
});
|
|
168
|
+
return this.astToTemporal.get(ast) ?? [];
|
|
170
169
|
}
|
|
171
170
|
/**
|
|
172
171
|
* 展开内容,但外层分组看不见它们
|
|
@@ -189,8 +188,8 @@ export class LoweringContext {
|
|
|
189
188
|
* 原地补全 hook: loweringEnter / loweringExit 返回的事件
|
|
190
189
|
*/
|
|
191
190
|
initEvent(event, owner, timeOffset, track) {
|
|
192
|
-
event.t
|
|
193
|
-
|
|
191
|
+
event.t.add(timeOffset);
|
|
192
|
+
this.usedTracks.add(track);
|
|
194
193
|
event.track = track;
|
|
195
194
|
event.ast ??= owner;
|
|
196
195
|
event.order = this.cnt++;
|
|
@@ -198,12 +197,12 @@ export class LoweringContext {
|
|
|
198
197
|
}
|
|
199
198
|
/**
|
|
200
199
|
* 递归展开一棵子树,得到它的时间列与结束时间
|
|
201
|
-
* @param timeOffset 当前节点的时间偏移量(单位QN)
|
|
200
|
+
* @param timeOffset 当前节点的时间偏移量(单位QN) 由父节点传入,会被原地修改
|
|
202
201
|
* @param track 当前所在的纵向音轨
|
|
203
202
|
*/
|
|
204
203
|
trackedEvents(node, timeOffset, track) {
|
|
205
204
|
const columns = [];
|
|
206
|
-
|
|
205
|
+
this.appendEvents(node.loweringEnter(this, track), node, timeOffset, track, columns);
|
|
207
206
|
// 处理子元素
|
|
208
207
|
const model = node.timeFlowModel();
|
|
209
208
|
if (model) {
|
|
@@ -211,9 +210,7 @@ export class LoweringContext {
|
|
|
211
210
|
case "sequence":
|
|
212
211
|
{
|
|
213
212
|
for (const c of model.children) {
|
|
214
|
-
const
|
|
215
|
-
timeOffset = result.timeOffset;
|
|
216
|
-
for (const column of result.columns)
|
|
213
|
+
for (const column of this.trackedEvents(c, timeOffset, track))
|
|
217
214
|
columns.push(column);
|
|
218
215
|
}
|
|
219
216
|
}
|
|
@@ -233,37 +230,50 @@ export class LoweringContext {
|
|
|
233
230
|
? track
|
|
234
231
|
: group.members[hostIndex === null || i < hostIndex ? i : i - 1];
|
|
235
232
|
// 每个分支从头开始
|
|
236
|
-
const
|
|
237
|
-
|
|
233
|
+
const branchTime = timeOffset.clone();
|
|
234
|
+
const branch = this.trackedEvents(c, branchTime, branchTrack);
|
|
235
|
+
if (branch.length === 0) {
|
|
238
236
|
this.diagnostics.push(Diagnostic.warning.ZeroTimeTrack(c.sourceSpan, i + 1));
|
|
239
237
|
}
|
|
240
|
-
branches.push(
|
|
238
|
+
branches.push(branch);
|
|
241
239
|
}
|
|
242
240
|
// 归并 局部归并以限制对齐作用域
|
|
243
241
|
for (const column of LoweringContext.anchorAlign(branches))
|
|
244
242
|
columns.push(column);
|
|
245
243
|
const lastCol = columns[columns.length - 1];
|
|
246
|
-
if (lastCol)
|
|
247
|
-
|
|
244
|
+
if (lastCol) {
|
|
245
|
+
let maxDuration = lastCol[0].T;
|
|
246
|
+
for (let i = 1; i < lastCol.length; i++) {
|
|
247
|
+
if (lastCol[i].T.compare(maxDuration) > 0)
|
|
248
|
+
maxDuration = lastCol[i].T;
|
|
249
|
+
}
|
|
250
|
+
timeOffset.copyFrom(lastCol.t).add(maxDuration);
|
|
251
|
+
}
|
|
248
252
|
}
|
|
249
253
|
break;
|
|
250
254
|
}
|
|
251
255
|
}
|
|
252
|
-
|
|
253
|
-
return
|
|
256
|
+
this.appendEvents(node.loweringExit(this, track, timeOffset), node, timeOffset, track, columns);
|
|
257
|
+
return columns;
|
|
254
258
|
}
|
|
255
|
-
/**
|
|
259
|
+
/**
|
|
260
|
+
* 将 hook 返回的事件规范化、加入索引与分组,再追加到时间列
|
|
261
|
+
* 原地修改 timeOffset
|
|
262
|
+
*/
|
|
256
263
|
appendEvents(events, owner, timeOffset, track, columns) {
|
|
264
|
+
// 由于大部分时候不会有事件,所以延迟创建
|
|
265
|
+
let eventEnd;
|
|
257
266
|
for (const event of events) {
|
|
258
267
|
this.initEvent(event, owner, timeOffset, track);
|
|
259
268
|
this.indexTemporal(event);
|
|
260
|
-
for (
|
|
261
|
-
group.onTemporal?.(event);
|
|
269
|
+
for (let i = this.activeLoweringGroups.length - 1; i >= 0; i--) {
|
|
270
|
+
this.activeLoweringGroups[i].group.onTemporal?.(event);
|
|
262
271
|
}
|
|
263
|
-
|
|
272
|
+
const end = (eventEnd ??= new Fraction()).copyFrom(event.t).add(event.T);
|
|
273
|
+
if (end.compare(timeOffset) > 0)
|
|
274
|
+
timeOffset.copyFrom(end);
|
|
264
275
|
columns.push(new TimeColumn(event));
|
|
265
276
|
}
|
|
266
|
-
return timeOffset;
|
|
267
277
|
}
|
|
268
278
|
/** 记录 AST->TemporalNode 的映射 */
|
|
269
279
|
indexTemporal(node) {
|
|
@@ -295,9 +305,9 @@ export class LoweringContext {
|
|
|
295
305
|
const track0 = tracks[0], track1 = tracks[1];
|
|
296
306
|
const l0 = track0.length, l1 = track1.length;
|
|
297
307
|
let i = 0, j = 0;
|
|
298
|
-
|
|
308
|
+
const idt = new Fraction(), jdt = new Fraction(); // 应用时间偏移一定要紧在 push 前
|
|
299
309
|
let a = track0[0], b = track1[0];
|
|
300
|
-
|
|
310
|
+
const t0 = a.t.clone(), t1 = b.t.clone();
|
|
301
311
|
while (true) {
|
|
302
312
|
if (a.mergeKey === ANCHOR_KEY) {
|
|
303
313
|
// 等待b也遇到anchor
|
|
@@ -305,17 +315,20 @@ export class LoweringContext {
|
|
|
305
315
|
result.push(b);
|
|
306
316
|
b = track1[++j];
|
|
307
317
|
while (j < l1 && b.mergeKey !== ANCHOR_KEY) {
|
|
308
|
-
if (jdt)
|
|
309
|
-
b.
|
|
318
|
+
if (!jdt.isZero())
|
|
319
|
+
b.shiftTime(jdt);
|
|
310
320
|
result.push(b);
|
|
311
321
|
b = track1[++j];
|
|
312
322
|
}
|
|
313
|
-
if (b)
|
|
314
|
-
b.
|
|
323
|
+
if (b) {
|
|
324
|
+
b.shiftTime(jdt);
|
|
325
|
+
t1.copyFrom(b.t);
|
|
326
|
+
}
|
|
315
327
|
}
|
|
316
328
|
if (j < l1) { // b 也是 anchor
|
|
317
|
-
const maxT =
|
|
318
|
-
idt
|
|
329
|
+
const maxT = t0.compare(t1) >= 0 ? t0 : t1;
|
|
330
|
+
idt.add(maxT).sub(t0);
|
|
331
|
+
jdt.add(maxT).sub(t1);
|
|
319
332
|
a.push(...b);
|
|
320
333
|
a.t = maxT;
|
|
321
334
|
b = track1[++j];
|
|
@@ -323,10 +336,14 @@ export class LoweringContext {
|
|
|
323
336
|
result.push(a);
|
|
324
337
|
a = track0[++i];
|
|
325
338
|
// 准备下一个
|
|
326
|
-
if (a)
|
|
327
|
-
a.
|
|
328
|
-
|
|
329
|
-
|
|
339
|
+
if (a) {
|
|
340
|
+
a.shiftTime(idt);
|
|
341
|
+
t0.copyFrom(a.t);
|
|
342
|
+
}
|
|
343
|
+
if (b) {
|
|
344
|
+
b.shiftTime(jdt);
|
|
345
|
+
t1.copyFrom(b.t);
|
|
346
|
+
}
|
|
330
347
|
if (!a || !b)
|
|
331
348
|
break;
|
|
332
349
|
}
|
|
@@ -335,40 +352,51 @@ export class LoweringContext {
|
|
|
335
352
|
result.push(a);
|
|
336
353
|
a = track0[++i];
|
|
337
354
|
while (i < l0 && a.mergeKey !== ANCHOR_KEY) {
|
|
338
|
-
if (idt)
|
|
339
|
-
a.
|
|
355
|
+
if (!idt.isZero())
|
|
356
|
+
a.shiftTime(idt);
|
|
340
357
|
result.push(a);
|
|
341
358
|
a = track0[++i];
|
|
342
359
|
}
|
|
343
|
-
if (a)
|
|
344
|
-
a.
|
|
360
|
+
if (a) {
|
|
361
|
+
a.shiftTime(idt);
|
|
362
|
+
t0.copyFrom(a.t);
|
|
363
|
+
}
|
|
345
364
|
}
|
|
346
365
|
if (i < l0) {
|
|
347
|
-
const maxT =
|
|
348
|
-
idt
|
|
366
|
+
const maxT = t0.compare(t1) >= 0 ? t0 : t1;
|
|
367
|
+
idt.add(maxT).sub(t0);
|
|
368
|
+
jdt.add(maxT).sub(t1);
|
|
349
369
|
b.push(...a);
|
|
350
370
|
b.t = maxT;
|
|
351
371
|
a = track0[++i];
|
|
352
372
|
}
|
|
353
373
|
result.push(b);
|
|
354
374
|
b = track1[++j];
|
|
355
|
-
if (a)
|
|
356
|
-
a.
|
|
357
|
-
|
|
358
|
-
|
|
375
|
+
if (a) {
|
|
376
|
+
a.shiftTime(idt);
|
|
377
|
+
t0.copyFrom(a.t);
|
|
378
|
+
}
|
|
379
|
+
if (b) {
|
|
380
|
+
b.shiftTime(jdt);
|
|
381
|
+
t1.copyFrom(b.t);
|
|
382
|
+
}
|
|
359
383
|
if (!a || !b)
|
|
360
384
|
break;
|
|
361
385
|
}
|
|
362
|
-
if (
|
|
386
|
+
if (t0.equals(t1)) {
|
|
363
387
|
if (a.mergeKey === b.mergeKey) {
|
|
364
388
|
a.push(...b);
|
|
365
389
|
result.push(a);
|
|
366
390
|
a = track0[++i];
|
|
367
391
|
b = track1[++j];
|
|
368
|
-
if (a)
|
|
369
|
-
a.
|
|
370
|
-
|
|
371
|
-
|
|
392
|
+
if (a) {
|
|
393
|
+
a.shiftTime(idt);
|
|
394
|
+
t0.copyFrom(a.t);
|
|
395
|
+
}
|
|
396
|
+
if (b) {
|
|
397
|
+
b.shiftTime(jdt);
|
|
398
|
+
t1.copyFrom(b.t);
|
|
399
|
+
}
|
|
372
400
|
if (!a || !b)
|
|
373
401
|
break;
|
|
374
402
|
}
|
|
@@ -376,7 +404,8 @@ export class LoweringContext {
|
|
|
376
404
|
result.push(a);
|
|
377
405
|
a = track0[++i];
|
|
378
406
|
if (a) {
|
|
379
|
-
a.
|
|
407
|
+
a.shiftTime(idt);
|
|
408
|
+
t0.copyFrom(a.t);
|
|
380
409
|
}
|
|
381
410
|
else
|
|
382
411
|
break;
|
|
@@ -385,28 +414,31 @@ export class LoweringContext {
|
|
|
385
414
|
result.push(b);
|
|
386
415
|
b = track1[++j];
|
|
387
416
|
if (b) {
|
|
388
|
-
b.
|
|
417
|
+
b.shiftTime(jdt);
|
|
418
|
+
t1.copyFrom(b.t);
|
|
389
419
|
}
|
|
390
420
|
else
|
|
391
421
|
break;
|
|
392
422
|
}
|
|
393
423
|
continue;
|
|
394
424
|
}
|
|
395
|
-
if (t0 <
|
|
425
|
+
if (t0.compare(t1) < 0) {
|
|
396
426
|
result.push(a);
|
|
397
427
|
a = track0[++i];
|
|
398
428
|
if (a) {
|
|
399
|
-
a.
|
|
429
|
+
a.shiftTime(idt);
|
|
430
|
+
t0.copyFrom(a.t);
|
|
400
431
|
continue;
|
|
401
432
|
}
|
|
402
433
|
else
|
|
403
434
|
break;
|
|
404
435
|
}
|
|
405
|
-
if (t1 <
|
|
436
|
+
if (t1.compare(t0) < 0) {
|
|
406
437
|
result.push(b);
|
|
407
438
|
b = track1[++j];
|
|
408
439
|
if (b) {
|
|
409
|
-
b.
|
|
440
|
+
b.shiftTime(jdt);
|
|
441
|
+
t1.copyFrom(b.t);
|
|
410
442
|
continue;
|
|
411
443
|
}
|
|
412
444
|
else
|
|
@@ -417,23 +449,23 @@ export class LoweringContext {
|
|
|
417
449
|
result.push(a);
|
|
418
450
|
while (++i < l0) {
|
|
419
451
|
a = track0[i];
|
|
420
|
-
if (idt)
|
|
421
|
-
a.
|
|
452
|
+
if (!idt.isZero())
|
|
453
|
+
a.shiftTime(idt);
|
|
422
454
|
result.push(a);
|
|
423
455
|
}
|
|
424
456
|
if (b)
|
|
425
457
|
result.push(b);
|
|
426
458
|
while (++j < l1) {
|
|
427
459
|
b = track1[j];
|
|
428
|
-
if (jdt)
|
|
429
|
-
b.
|
|
460
|
+
if (!jdt.isZero())
|
|
461
|
+
b.shiftTime(jdt);
|
|
430
462
|
result.push(b);
|
|
431
463
|
}
|
|
432
464
|
return result;
|
|
433
465
|
}
|
|
434
466
|
// 归并多个轨道
|
|
435
467
|
const p = new Uint16Array(l); // 当前指向第几个
|
|
436
|
-
const dt = new
|
|
468
|
+
const dt = Array.from({ length: l }, () => new Fraction()); // 每个轨道的偏移量
|
|
437
469
|
const anchorBuffer = [];
|
|
438
470
|
const heap = new HeapSort(l); // 保证每个轨在堆里最多一个
|
|
439
471
|
for (let i = 0; i < l; i++)
|
|
@@ -441,8 +473,8 @@ export class LoweringContext {
|
|
|
441
473
|
const fetchNext = (i) => {
|
|
442
474
|
const e = tracks[i][++p[i]];
|
|
443
475
|
if (e) {
|
|
444
|
-
if (dt[i])
|
|
445
|
-
e.
|
|
476
|
+
if (!dt[i].isZero())
|
|
477
|
+
e.shiftTime(dt[i]);
|
|
446
478
|
heap.push({ e, i });
|
|
447
479
|
}
|
|
448
480
|
};
|
|
@@ -454,14 +486,16 @@ export class LoweringContext {
|
|
|
454
486
|
const first = anchorBuffer[0];
|
|
455
487
|
const container = first.e;
|
|
456
488
|
let maxT = container.t;
|
|
457
|
-
for (let j = 1; j < anchorBuffer.length; j++)
|
|
458
|
-
|
|
459
|
-
|
|
489
|
+
for (let j = 1; j < anchorBuffer.length; j++) {
|
|
490
|
+
if (anchorBuffer[j].e.t.compare(maxT) > 0)
|
|
491
|
+
maxT = anchorBuffer[j].e.t;
|
|
492
|
+
}
|
|
493
|
+
dt[first.i].add(maxT).sub(container.t);
|
|
460
494
|
fetchNext(first.i);
|
|
461
495
|
for (let j = 1; j < anchorBuffer.length; j++) {
|
|
462
496
|
const item = anchorBuffer[j];
|
|
463
497
|
container.push(...item.e);
|
|
464
|
-
dt[item.i]
|
|
498
|
+
dt[item.i].add(maxT).sub(item.e.t);
|
|
465
499
|
fetchNext(item.i);
|
|
466
500
|
}
|
|
467
501
|
anchorBuffer.length = 0;
|
|
@@ -476,12 +510,11 @@ export class LoweringContext {
|
|
|
476
510
|
continue;
|
|
477
511
|
}
|
|
478
512
|
// 找同时刻的同组
|
|
479
|
-
const minT = front.e.t + 1e-6; // 避免浮点数误差
|
|
480
513
|
const consumed = [front.i];
|
|
481
514
|
while (true) {
|
|
482
515
|
const f = heap.front();
|
|
483
516
|
// 堆序保证同组相邻,遇到别的组即说明本组已取完
|
|
484
|
-
if (!f || f.e.t
|
|
517
|
+
if (!f || !f.e.t.equals(front.e.t) || f.e.mergeKey !== front.e.mergeKey)
|
|
485
518
|
break;
|
|
486
519
|
heap.pop();
|
|
487
520
|
front.e.push(...f.e);
|
|
@@ -506,8 +539,12 @@ class TimeColumn extends Array {
|
|
|
506
539
|
return this[0].t;
|
|
507
540
|
}
|
|
508
541
|
set t(value) {
|
|
509
|
-
for (const
|
|
510
|
-
|
|
542
|
+
for (const node of this)
|
|
543
|
+
node.t.copyFrom(value);
|
|
544
|
+
}
|
|
545
|
+
shiftTime(offset) {
|
|
546
|
+
for (const node of this)
|
|
547
|
+
node.t.add(offset);
|
|
511
548
|
}
|
|
512
549
|
/** 将当前列所有事件的行号固化为 lowering 后的实际行号 */
|
|
513
550
|
set layoutLine(value) {
|
|
@@ -516,8 +553,9 @@ class TimeColumn extends Array {
|
|
|
516
553
|
}
|
|
517
554
|
}
|
|
518
555
|
function heapItemCmp(a, b) {
|
|
519
|
-
|
|
520
|
-
|
|
556
|
+
const timeOrder = a.e.t.compare(b.e.t);
|
|
557
|
+
if (timeOrder !== 0)
|
|
558
|
+
return timeOrder;
|
|
521
559
|
// 有无穷所以不能直接减
|
|
522
560
|
if (a.e.mergeKey !== b.e.mergeKey)
|
|
523
561
|
return a.e.mergeKey < b.e.mergeKey ? -1 : 1;
|
package/dist/lowering/track.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ export interface TrackGroup {
|
|
|
59
59
|
/**
|
|
60
60
|
* 纵向音轨
|
|
61
61
|
*
|
|
62
|
-
* 一条 Track
|
|
62
|
+
* 一条 Track 就是谱面上的一条基线。它描述“谁挂在谁上面/下面”的静态拓扑,不保存任何布局求解结果
|
|
63
63
|
* 每一行的实际纵向轴由 layout 独立解出并存在自己的表里,因此 Track 本身是可以安全共享的不可变值
|
|
64
64
|
*
|
|
65
65
|
* “音乐上是不是同一条轨”与“视觉上是不是同一条基线”在这里被合并成同一个对象
|
package/dist/lowering/track.js
CHANGED
package/dist/lowering/types.d.ts
CHANGED
|
@@ -1,38 +1,69 @@
|
|
|
1
1
|
import { ASTNodeBase } from "../functions/ASTtypes.js";
|
|
2
2
|
import type { Diagnostic } from "../diagnostic.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { SourceSpan } from "../parser/types.js";
|
|
4
|
+
import { Fraction } from "../fraction.js";
|
|
5
|
+
import type { HorizontalSpringConfig, LayoutBox, LayoutDecoration, LayoutHost, PageConfig, LayoutPoint, LayoutPrepareContext, HorizontalLineView, TimeLineEvent } from "../layout/types.js";
|
|
4
6
|
import type { Painter } from "../render/types.js";
|
|
7
|
+
import type { PlaybackEmitter, PlaybackState } from "../playback/types.js";
|
|
5
8
|
import type { Track, TrackArrangement } from "./track.js";
|
|
9
|
+
/** lowering 产生的不推进时间的附属对象;具体能力由 layout/playback 子协议声明 */
|
|
10
|
+
export interface LoweringAttachment {
|
|
11
|
+
readonly sourceSpan?: SourceSpan;
|
|
12
|
+
}
|
|
6
13
|
/** 时间流固化后生成额外 attachment;所有 augmenter 读取同一份输入快照 */
|
|
7
|
-
export type LoweringAugmenter = (result: LoweringResult) => Iterable<
|
|
14
|
+
export type LoweringAugmenter = (result: LoweringResult) => Iterable<LoweringAttachment>;
|
|
8
15
|
/** 所有 augmenter 结果追加完成后的最终处理;按注册顺序执行,可修改完整结果或抛出语义错误 */
|
|
9
16
|
export type LoweringFinalizer = (result: LoweringResult) => void;
|
|
17
|
+
/**
|
|
18
|
+
* 沿记谱顺序流动的时间状态
|
|
19
|
+
*
|
|
20
|
+
* 速度、力度、调性是系统级字段:有确定的类型和初值,任何时刻都能直接读,不用写兼容分支。
|
|
21
|
+
* 其余键由具体函数自行约定,核心不认识。
|
|
22
|
+
*/
|
|
23
|
+
export interface TimeState {
|
|
24
|
+
bpm: number;
|
|
25
|
+
velocity: number;
|
|
26
|
+
keySignature: string;
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
}
|
|
29
|
+
export declare const DEFAULT_BPM = 120;
|
|
30
|
+
export declare const DEFAULT_VELOCITY = 80;
|
|
31
|
+
export declare const DEFAULT_TONALITY = "C4";
|
|
10
32
|
/**
|
|
11
33
|
* lowering 的输出
|
|
12
34
|
*
|
|
13
35
|
* - columns 时间列,是横向弹簧模型的输入
|
|
14
|
-
* - attachments
|
|
36
|
+
* - attachments 保存不直接推进时间的布局附件(如 tie、box)、播放关系与控制声明
|
|
15
37
|
* - astToTemporal 从 AST 到 Temporal 的映射(一对多)
|
|
16
38
|
* - rootTrack 纵向音轨树的根,layout 沿它递归求解每行的纵向轴
|
|
17
39
|
*/
|
|
18
40
|
export interface LoweringResult {
|
|
19
41
|
diagnostics: Diagnostic[];
|
|
20
42
|
columns: TemporalNodeBase[][];
|
|
21
|
-
attachments:
|
|
43
|
+
attachments: LoweringAttachment[];
|
|
22
44
|
astToTemporal: Map<ASTNodeBase, TemporalNodeBase[]>;
|
|
23
|
-
duration:
|
|
45
|
+
duration: Fraction;
|
|
24
46
|
rootTrack: Track;
|
|
47
|
+
tracks: readonly Track[];
|
|
25
48
|
page?: PageConfig;
|
|
26
49
|
}
|
|
27
50
|
/**
|
|
28
51
|
* lowering 递归作用域的通用观察者
|
|
29
52
|
*
|
|
30
|
-
* onTemporal 在事件加入时间列、推进时间游标前调用。
|
|
53
|
+
* 回调按当前作用域栈由内向外执行;onTemporal 在事件加入时间列、推进时间游标前调用。
|
|
31
54
|
*/
|
|
32
55
|
export interface LoweringGroup {
|
|
33
|
-
|
|
56
|
+
/**
|
|
57
|
+
* 当前分组退出后会自动注册的 attachment
|
|
58
|
+
*
|
|
59
|
+
* attachment 在 layout 的时候会按照顺序求解,对于 box 这样的必须让子元素先被测量,才能确定尺寸,所以需要最后加入
|
|
60
|
+
* 之所以有这个字段(而不是在 endLoweringGroup 之后手动 push)是为了收集到的信息可以直接传递,参考 box
|
|
61
|
+
*/
|
|
62
|
+
attachment?: LoweringAttachment;
|
|
63
|
+
/** 观察组内生成的事件 */
|
|
34
64
|
onTemporal?(node: TemporalNodeBase): void;
|
|
35
|
-
|
|
65
|
+
/** 观察组内注册的附属对象 */
|
|
66
|
+
onAttachment?(attachment: LoweringAttachment): void;
|
|
36
67
|
}
|
|
37
68
|
/**
|
|
38
69
|
* 时间流动模式(由 AST 节点的 timeFlowModel 返回)
|
|
@@ -66,8 +97,8 @@ export declare const DEFAULT_KEY: number;
|
|
|
66
97
|
* 由 AST 节点在 lowering 阶段产生,负责得到时间、设置布局参数
|
|
67
98
|
*/
|
|
68
99
|
export declare class TemporalNodeBase implements TimeLineEvent {
|
|
69
|
-
t:
|
|
70
|
-
T:
|
|
100
|
+
readonly t: Fraction;
|
|
101
|
+
readonly T: Fraction;
|
|
71
102
|
track: Track;
|
|
72
103
|
ast: ASTNodeBase;
|
|
73
104
|
order: number;
|
|
@@ -77,7 +108,7 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
77
108
|
* 被折叠进哪个宿主的盒子(由宿主自己设置)
|
|
78
109
|
*
|
|
79
110
|
* 折叠成员不进入全局 columns,在时间流里没有独立位置,
|
|
80
|
-
*
|
|
111
|
+
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
81
112
|
*/
|
|
82
113
|
foldedInto?: TemporalNodeBase;
|
|
83
114
|
/**
|
|
@@ -124,7 +155,13 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
124
155
|
* 时间状态 修改&冻结 入口
|
|
125
156
|
* 调用时机在时间位置已经确定之后,处理“调性、速度、拍号”等时间信息的固化
|
|
126
157
|
*/
|
|
127
|
-
onTimeState?(state:
|
|
158
|
+
onTimeState?(state: TimeState): void;
|
|
159
|
+
/** 把已固化的音乐语义声明为播放事件;不执行实时播放 */
|
|
160
|
+
emitPlayback?(emitter: PlaybackEmitter): void;
|
|
161
|
+
/** 给自己所在的时间列贴播放标记,供控制流游标查询 */
|
|
162
|
+
playbackMarks?(): readonly string[];
|
|
163
|
+
/** 向播放系统告知自己这个记谱位置上的速度与力度 */
|
|
164
|
+
playbackState?: PlaybackState;
|
|
128
165
|
/**
|
|
129
166
|
* 根据已经固化的语义生成固有尺寸
|
|
130
167
|
* 一般为测量函数自有图形或使用 LayoutPrepareContext.textMeasurer 测量文本,填写 box.w/h/anchor/visualAxis
|
package/dist/lowering/types.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { Fraction } from "../fraction.js";
|
|
2
|
+
export const DEFAULT_BPM = 120;
|
|
3
|
+
export const DEFAULT_VELOCITY = 80;
|
|
4
|
+
export const DEFAULT_TONALITY = "C4";
|
|
1
5
|
/**
|
|
2
6
|
* 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
|
|
3
7
|
*
|
|
@@ -16,8 +20,8 @@ export const DEFAULT_KEY = Infinity;
|
|
|
16
20
|
export class TemporalNodeBase {
|
|
17
21
|
// 下面这批字段统一由 LoweringContext.initEvent 原地补全
|
|
18
22
|
// 横向 layout 要用的三个属性
|
|
19
|
-
t; // 事件发生的时间点
|
|
20
|
-
T; // 事件的持续时间
|
|
23
|
+
t = new Fraction(); // 事件发生的时间点
|
|
24
|
+
T = new Fraction(); // 事件的持续时间
|
|
21
25
|
track; // 所在的纵向音轨;同轨判断使用引用相等
|
|
22
26
|
ast; // 对应的 AST 节点
|
|
23
27
|
order; // 事件创建序号,作为id。可用于区分同时发生的父子、排序同一时刻发生的事件
|
|
@@ -27,7 +31,7 @@ export class TemporalNodeBase {
|
|
|
27
31
|
* 被折叠进哪个宿主的盒子(由宿主自己设置)
|
|
28
32
|
*
|
|
29
33
|
* 折叠成员不进入全局 columns,在时间流里没有独立位置,
|
|
30
|
-
*
|
|
34
|
+
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
31
35
|
*/
|
|
32
36
|
foldedInto;
|
|
33
37
|
/**
|
|
@@ -76,6 +80,8 @@ export class TemporalNodeBase {
|
|
|
76
80
|
anchor: 0, visualAxis: 0,
|
|
77
81
|
};
|
|
78
82
|
}
|
|
83
|
+
/** 向播放系统告知自己这个记谱位置上的速度与力度 */
|
|
84
|
+
playbackState;
|
|
79
85
|
/**
|
|
80
86
|
* 根据已经固化的语义生成固有尺寸
|
|
81
87
|
* 一般为测量函数自有图形或使用 LayoutPrepareContext.textMeasurer 测量文本,填写 box.w/h/anchor/visualAxis
|
|
@@ -20,5 +20,3 @@ export declare function readCall(source: string, atPos: number, end: number): Ca
|
|
|
20
20
|
export declare function trimRange(source: string, start: number, end: number): SourceSpan;
|
|
21
21
|
export declare function findTopLevelEquals(text: string, start?: number, end?: number): number;
|
|
22
22
|
export declare function findRightParen(source: string, start: number, end: number): number;
|
|
23
|
-
/** 识别字符串参数时用于剥离双引号;单引号不是字符串语法 */
|
|
24
|
-
export declare function removeQuote(source: string): string;
|