jpfun 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -11
- package/dist/converter/midi/convert.d.ts +50 -0
- package/dist/converter/midi/convert.js +700 -0
- package/dist/converter/midi/index.d.ts +5 -0
- package/dist/converter/midi/index.js +11 -0
- package/dist/converter/midi/json.d.ts +38 -0
- package/dist/converter/midi/json.js +1 -0
- package/dist/converter/midi/triplet.d.ts +30 -0
- package/dist/converter/midi/triplet.js +75 -0
- package/dist/converter/musicxml/dom.d.ts +25 -0
- package/dist/converter/musicxml/dom.js +65 -0
- package/dist/converter/musicxml/features.d.ts +51 -0
- package/dist/converter/musicxml/features.js +232 -0
- package/dist/converter/musicxml/index.d.ts +9 -0
- package/dist/converter/musicxml/index.js +1140 -0
- package/dist/converter/musicxml/model.d.ts +123 -0
- package/dist/converter/musicxml/model.js +1 -0
- package/dist/converter/source.d.ts +22 -0
- package/dist/converter/source.js +51 -0
- package/dist/core.d.ts +22 -0
- package/dist/core.js +22 -0
- package/dist/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +51 -31
- package/dist/functions/ASTtypes.js +59 -38
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/arpeggio/index.d.ts +2 -0
- package/dist/functions/arpeggio/index.js +180 -0
- package/dist/functions/arpeggio/shape.d.ts +7 -0
- package/dist/functions/arpeggio/shape.js +122 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +62 -25
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +33 -16
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +4 -3
- package/dist/functions/beam/layout.js +43 -65
- package/dist/functions/box/index.js +37 -36
- package/dist/functions/br/index.js +10 -8
- package/dist/functions/dash/index.js +40 -7
- package/dist/functions/default.js +19 -8
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +23 -25
- package/dist/functions/dyn/index.d.ts +2 -0
- package/dist/functions/dyn/index.js +242 -0
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +84 -79
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +496 -0
- package/dist/functions/key/index.js +98 -3
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +155 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +106 -79
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +11 -2
- package/dist/functions/page/index.js +65 -4
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +30 -0
- package/dist/functions/set/index.js +43 -32
- package/dist/functions/stack/index.js +26 -22
- package/dist/functions/symbol/index.d.ts +31 -0
- package/dist/functions/symbol/index.js +139 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +24 -0
- package/dist/functions/symbol/shape.js +52 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/ds.d.ts +2 -0
- package/dist/functions/symbol/symbols/ds.js +93 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +168 -0
- package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/fine.d.ts +2 -0
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +56 -0
- package/dist/functions/symbol/symbols/prall.d.ts +2 -0
- package/dist/functions/symbol/symbols/prall.js +42 -0
- package/dist/functions/symbol/symbols/segno.d.ts +4 -0
- package/dist/functions/symbol/symbols/segno.js +82 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +71 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +57 -4
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +86 -27
- package/dist/functions/tie/index.js +136 -152
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +218 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +258 -182
- package/dist/functions/voice/index.js +203 -109
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +230 -0
- package/dist/iife/core.d.ts +1 -0
- package/dist/iife/core.js +3 -0
- package/dist/iife/from-midi.d.ts +1 -0
- package/dist/iife/from-midi.js +15 -0
- package/dist/iife/from-musicxml.d.ts +1 -0
- package/dist/iife/from-musicxml.js +3 -0
- package/dist/iife/full.d.ts +1 -0
- package/dist/iife/full.js +3 -0
- package/dist/index.d.ts +3 -17
- package/dist/index.js +3 -17
- package/dist/jpfun.core.min.js +153 -0
- package/dist/jpfun.from-midi.min.js +9 -0
- package/dist/jpfun.from-musicxml.min.js +12 -0
- package/dist/jpfun.min.js +166 -0
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +6 -12
- package/dist/layout/engine.js +115 -64
- package/dist/layout/model.d.ts +12 -5
- package/dist/layout/model.js +31 -18
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +71 -20
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +20 -20
- package/dist/lowering/loweringContext.js +153 -101
- package/dist/lowering/track.d.ts +5 -9
- package/dist/lowering/track.js +7 -2
- package/dist/lowering/types.d.ts +26 -112
- package/dist/lowering/types.js +1 -92
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +10 -0
- package/dist/parser/parse-utils/string-utils.js +24 -0
- package/dist/parser/parserContext.d.ts +11 -9
- package/dist/parser/parserContext.js +65 -55
- package/dist/parser/preprocess.js +17 -11
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +414 -0
- package/dist/playback/event.d.ts +105 -0
- package/dist/playback/event.js +105 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +129 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +22 -0
- package/dist/render/text.js +42 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +132 -13
- package/package.json +19 -6
|
@@ -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_PROGRAM, DEFAULT_TONALITY, DEFAULT_VELOCITY, } from "../functions/temporal.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;
|
|
@@ -46,8 +48,7 @@ export class LoweringContext {
|
|
|
46
48
|
registerFunctions(functionClasses) {
|
|
47
49
|
const augmenters = this.loweringAugmenters = [];
|
|
48
50
|
const finalizers = this.loweringFinalizers = [];
|
|
49
|
-
for (const
|
|
50
|
-
const functionClass = cls;
|
|
51
|
+
for (const functionClass of functionClasses) {
|
|
51
52
|
if (functionClass.loweringAugment)
|
|
52
53
|
augmenters.push(functionClass.loweringAugment);
|
|
53
54
|
if (functionClass.loweringFinalize)
|
|
@@ -66,20 +67,23 @@ export class LoweringContext {
|
|
|
66
67
|
*/
|
|
67
68
|
lowerDocument(node) {
|
|
68
69
|
this.cnt = 0;
|
|
70
|
+
this.usedTracks.clear();
|
|
69
71
|
this.attachments = [];
|
|
70
72
|
this.page = void 0;
|
|
71
73
|
this.astToTemporal = new Map();
|
|
72
74
|
this.activeLoweringGroups = [];
|
|
73
75
|
const rootTrack = new Track();
|
|
74
|
-
const
|
|
76
|
+
const duration = new Fraction();
|
|
77
|
+
const columns = this.trackedEvents(node, duration, rootTrack);
|
|
75
78
|
this.solidifyColumns(columns);
|
|
76
79
|
return this.postprocessResult({
|
|
77
80
|
diagnostics: this.diagnostics,
|
|
78
81
|
columns,
|
|
79
82
|
attachments: this.attachments,
|
|
80
83
|
astToTemporal: this.astToTemporal,
|
|
81
|
-
duration
|
|
84
|
+
duration,
|
|
82
85
|
rootTrack,
|
|
86
|
+
tracks: [...this.usedTracks],
|
|
83
87
|
page: this.page,
|
|
84
88
|
});
|
|
85
89
|
}
|
|
@@ -100,10 +104,25 @@ export class LoweringContext {
|
|
|
100
104
|
column.layoutLine = line;
|
|
101
105
|
}
|
|
102
106
|
// 行号全部就位后再固化时间状态,up 等节点才能把行号转发给内部成员
|
|
103
|
-
const state = {
|
|
107
|
+
const state = {
|
|
108
|
+
bpm: DEFAULT_BPM,
|
|
109
|
+
velocity: DEFAULT_VELOCITY,
|
|
110
|
+
keySignature: DEFAULT_TONALITY,
|
|
111
|
+
program: DEFAULT_PROGRAM,
|
|
112
|
+
};
|
|
113
|
+
// 力度和音色按音轨各自流动,新分叉的声部继承分叉处父轨的值
|
|
114
|
+
const velocities = new Map();
|
|
115
|
+
const programs = new Map();
|
|
116
|
+
const velocityOf = (track) => velocities.get(track) ?? (track.parent ? velocityOf(track.parent) : DEFAULT_VELOCITY);
|
|
117
|
+
const programOf = (track) => programs.get(track) ?? (track.parent ? programOf(track.parent) : DEFAULT_PROGRAM);
|
|
104
118
|
for (const column of columns) {
|
|
105
|
-
for (const node of column)
|
|
119
|
+
for (const node of column) {
|
|
120
|
+
state.velocity = velocityOf(node.track);
|
|
121
|
+
state.program = programOf(node.track);
|
|
106
122
|
node.onTimeState?.(state);
|
|
123
|
+
velocities.set(node.track, state.velocity);
|
|
124
|
+
programs.set(node.track, state.program);
|
|
125
|
+
}
|
|
107
126
|
}
|
|
108
127
|
}
|
|
109
128
|
/**
|
|
@@ -124,7 +143,8 @@ export class LoweringContext {
|
|
|
124
143
|
if (active.owner !== owner)
|
|
125
144
|
throw new Error("Lowering groups must end in reverse order");
|
|
126
145
|
if (active.group.attachment)
|
|
127
|
-
this.
|
|
146
|
+
this.addAttachment(active.group.attachment);
|
|
147
|
+
return active.group;
|
|
128
148
|
}
|
|
129
149
|
/** 先统一生成派生 attachment,再按注册顺序执行最终处理 */
|
|
130
150
|
postprocessResult(result) {
|
|
@@ -136,7 +156,7 @@ export class LoweringContext {
|
|
|
136
156
|
additions.push(attachment);
|
|
137
157
|
}
|
|
138
158
|
for (const attachment of additions)
|
|
139
|
-
this.
|
|
159
|
+
this.addAttachment(attachment);
|
|
140
160
|
// 进行校验或者其他动作
|
|
141
161
|
for (const finalize of this.loweringFinalizers)
|
|
142
162
|
finalize(result);
|
|
@@ -146,27 +166,16 @@ export class LoweringContext {
|
|
|
146
166
|
* 注册不推进时间的排版对象
|
|
147
167
|
* 被 tie、beam、box 等函数使用
|
|
148
168
|
*/
|
|
149
|
-
|
|
169
|
+
addAttachment(attachment) {
|
|
150
170
|
this.attachments.push(attachment);
|
|
151
|
-
|
|
152
|
-
|
|
171
|
+
// 由内向外分组,符合嵌套作用域语义
|
|
172
|
+
for (let i = this.activeLoweringGroups.length - 1; i >= 0; i--) {
|
|
173
|
+
this.activeLoweringGroups[i].group.onAttachment?.(attachment);
|
|
153
174
|
}
|
|
154
175
|
}
|
|
155
|
-
/**
|
|
156
|
-
* 查询 AST 节点在当前 lowering 中产生的所有 temporal 节点
|
|
157
|
-
*
|
|
158
|
-
* 被折叠进别人盒子的成员没有独立的全局位置,统一上溯到宿主,
|
|
159
|
-
* 所以写在成员上的标签等价于写在整个复合节点上。
|
|
160
|
-
*/
|
|
176
|
+
/** 查询 AST 自己产生的 temporal;是否沿 foldedInto 投影由调用者决定 */
|
|
161
177
|
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
|
-
});
|
|
178
|
+
return this.astToTemporal.get(ast) ?? [];
|
|
170
179
|
}
|
|
171
180
|
/**
|
|
172
181
|
* 展开内容,但外层分组看不见它们
|
|
@@ -189,8 +198,8 @@ export class LoweringContext {
|
|
|
189
198
|
* 原地补全 hook: loweringEnter / loweringExit 返回的事件
|
|
190
199
|
*/
|
|
191
200
|
initEvent(event, owner, timeOffset, track) {
|
|
192
|
-
event.t
|
|
193
|
-
|
|
201
|
+
event.t.add(timeOffset);
|
|
202
|
+
this.usedTracks.add(track);
|
|
194
203
|
event.track = track;
|
|
195
204
|
event.ast ??= owner;
|
|
196
205
|
event.order = this.cnt++;
|
|
@@ -198,22 +207,20 @@ export class LoweringContext {
|
|
|
198
207
|
}
|
|
199
208
|
/**
|
|
200
209
|
* 递归展开一棵子树,得到它的时间列与结束时间
|
|
201
|
-
* @param timeOffset 当前节点的时间偏移量(单位QN)
|
|
210
|
+
* @param timeOffset 当前节点的时间偏移量(单位QN) 由父节点传入,会被原地修改
|
|
202
211
|
* @param track 当前所在的纵向音轨
|
|
203
212
|
*/
|
|
204
213
|
trackedEvents(node, timeOffset, track) {
|
|
205
214
|
const columns = [];
|
|
206
|
-
|
|
215
|
+
this.appendEvents(node.loweringEnter(this, track), node, timeOffset, track, columns);
|
|
207
216
|
// 处理子元素
|
|
208
|
-
const model = node.timeFlowModel();
|
|
217
|
+
const model = node.timeFlowModel(this);
|
|
209
218
|
if (model) {
|
|
210
219
|
switch (model.mode) {
|
|
211
220
|
case "sequence":
|
|
212
221
|
{
|
|
213
222
|
for (const c of model.children) {
|
|
214
|
-
const
|
|
215
|
-
timeOffset = result.timeOffset;
|
|
216
|
-
for (const column of result.columns)
|
|
223
|
+
for (const column of this.trackedEvents(c, timeOffset, track))
|
|
217
224
|
columns.push(column);
|
|
218
225
|
}
|
|
219
226
|
}
|
|
@@ -233,37 +240,51 @@ export class LoweringContext {
|
|
|
233
240
|
? track
|
|
234
241
|
: group.members[hostIndex === null || i < hostIndex ? i : i - 1];
|
|
235
242
|
// 每个分支从头开始
|
|
236
|
-
const
|
|
237
|
-
|
|
243
|
+
const branchTime = timeOffset.clone();
|
|
244
|
+
const branch = this.trackedEvents(c, branchTime, branchTrack);
|
|
245
|
+
if (branch.length === 0) {
|
|
238
246
|
this.diagnostics.push(Diagnostic.warning.ZeroTimeTrack(c.sourceSpan, i + 1));
|
|
239
247
|
}
|
|
240
|
-
branches.push(
|
|
248
|
+
branches.push(branch);
|
|
241
249
|
}
|
|
242
250
|
// 归并 局部归并以限制对齐作用域
|
|
251
|
+
const mergeStart = columns.length;
|
|
243
252
|
for (const column of LoweringContext.anchorAlign(branches))
|
|
244
253
|
columns.push(column);
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
254
|
+
// 最长的事件未必落在最后一列,所以要扫全部合并列取最晚终点
|
|
255
|
+
const eventEnd = new Fraction();
|
|
256
|
+
for (let i = mergeStart; i < columns.length; i++) {
|
|
257
|
+
for (const node of columns[i]) {
|
|
258
|
+
eventEnd.copyFrom(node.t).add(node.T);
|
|
259
|
+
if (eventEnd.compare(timeOffset) > 0)
|
|
260
|
+
timeOffset.copyFrom(eventEnd);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
248
263
|
}
|
|
249
264
|
break;
|
|
250
265
|
}
|
|
251
266
|
}
|
|
252
|
-
|
|
253
|
-
return
|
|
267
|
+
this.appendEvents(node.loweringExit(this, track, timeOffset), node, timeOffset, track, columns);
|
|
268
|
+
return columns;
|
|
254
269
|
}
|
|
255
|
-
/**
|
|
270
|
+
/**
|
|
271
|
+
* 将 hook 返回的事件规范化、加入索引与分组,再追加到时间列
|
|
272
|
+
* 原地修改 timeOffset
|
|
273
|
+
*/
|
|
256
274
|
appendEvents(events, owner, timeOffset, track, columns) {
|
|
275
|
+
// 由于大部分时候不会有事件,所以延迟创建
|
|
276
|
+
let eventEnd;
|
|
257
277
|
for (const event of events) {
|
|
258
278
|
this.initEvent(event, owner, timeOffset, track);
|
|
259
279
|
this.indexTemporal(event);
|
|
260
|
-
for (
|
|
261
|
-
group.onTemporal?.(event);
|
|
280
|
+
for (let i = this.activeLoweringGroups.length - 1; i >= 0; i--) {
|
|
281
|
+
this.activeLoweringGroups[i].group.onTemporal?.(event);
|
|
262
282
|
}
|
|
263
|
-
|
|
283
|
+
const end = (eventEnd ??= new Fraction()).copyFrom(event.t).add(event.T);
|
|
284
|
+
if (end.compare(timeOffset) > 0)
|
|
285
|
+
timeOffset.copyFrom(end);
|
|
264
286
|
columns.push(new TimeColumn(event));
|
|
265
287
|
}
|
|
266
|
-
return timeOffset;
|
|
267
288
|
}
|
|
268
289
|
/** 记录 AST->TemporalNode 的映射 */
|
|
269
290
|
indexTemporal(node) {
|
|
@@ -285,19 +306,20 @@ export class LoweringContext {
|
|
|
285
306
|
tracks.splice(i, 1);
|
|
286
307
|
}
|
|
287
308
|
const l = tracks.length;
|
|
309
|
+
// 分支全空;调用方已经逐条报过 ZeroTimeTrack
|
|
288
310
|
if (l === 0)
|
|
289
|
-
|
|
311
|
+
return [];
|
|
290
312
|
if (l === 1)
|
|
291
313
|
return tracks[0];
|
|
292
314
|
const result = [];
|
|
293
315
|
if (l === 2) {
|
|
294
|
-
//
|
|
316
|
+
// 双轨特化:与下面的堆归并语义等价,改规则必须两边同时改
|
|
295
317
|
const track0 = tracks[0], track1 = tracks[1];
|
|
296
318
|
const l0 = track0.length, l1 = track1.length;
|
|
297
319
|
let i = 0, j = 0;
|
|
298
|
-
|
|
320
|
+
const idt = new Fraction(), jdt = new Fraction(); // 应用时间偏移一定要紧在 push 前
|
|
299
321
|
let a = track0[0], b = track1[0];
|
|
300
|
-
|
|
322
|
+
const t0 = a.t.clone(), t1 = b.t.clone();
|
|
301
323
|
while (true) {
|
|
302
324
|
if (a.mergeKey === ANCHOR_KEY) {
|
|
303
325
|
// 等待b也遇到anchor
|
|
@@ -305,17 +327,20 @@ export class LoweringContext {
|
|
|
305
327
|
result.push(b);
|
|
306
328
|
b = track1[++j];
|
|
307
329
|
while (j < l1 && b.mergeKey !== ANCHOR_KEY) {
|
|
308
|
-
if (jdt)
|
|
309
|
-
b.
|
|
330
|
+
if (!jdt.isZero())
|
|
331
|
+
b.shiftTime(jdt);
|
|
310
332
|
result.push(b);
|
|
311
333
|
b = track1[++j];
|
|
312
334
|
}
|
|
313
|
-
if (b)
|
|
314
|
-
b.
|
|
335
|
+
if (b) {
|
|
336
|
+
b.shiftTime(jdt);
|
|
337
|
+
t1.copyFrom(b.t);
|
|
338
|
+
}
|
|
315
339
|
}
|
|
316
340
|
if (j < l1) { // b 也是 anchor
|
|
317
|
-
const maxT =
|
|
318
|
-
idt
|
|
341
|
+
const maxT = t0.compare(t1) >= 0 ? t0 : t1;
|
|
342
|
+
idt.add(maxT).sub(t0);
|
|
343
|
+
jdt.add(maxT).sub(t1);
|
|
319
344
|
a.push(...b);
|
|
320
345
|
a.t = maxT;
|
|
321
346
|
b = track1[++j];
|
|
@@ -323,10 +348,14 @@ export class LoweringContext {
|
|
|
323
348
|
result.push(a);
|
|
324
349
|
a = track0[++i];
|
|
325
350
|
// 准备下一个
|
|
326
|
-
if (a)
|
|
327
|
-
a.
|
|
328
|
-
|
|
329
|
-
|
|
351
|
+
if (a) {
|
|
352
|
+
a.shiftTime(idt);
|
|
353
|
+
t0.copyFrom(a.t);
|
|
354
|
+
}
|
|
355
|
+
if (b) {
|
|
356
|
+
b.shiftTime(jdt);
|
|
357
|
+
t1.copyFrom(b.t);
|
|
358
|
+
}
|
|
330
359
|
if (!a || !b)
|
|
331
360
|
break;
|
|
332
361
|
}
|
|
@@ -335,40 +364,51 @@ export class LoweringContext {
|
|
|
335
364
|
result.push(a);
|
|
336
365
|
a = track0[++i];
|
|
337
366
|
while (i < l0 && a.mergeKey !== ANCHOR_KEY) {
|
|
338
|
-
if (idt)
|
|
339
|
-
a.
|
|
367
|
+
if (!idt.isZero())
|
|
368
|
+
a.shiftTime(idt);
|
|
340
369
|
result.push(a);
|
|
341
370
|
a = track0[++i];
|
|
342
371
|
}
|
|
343
|
-
if (a)
|
|
344
|
-
a.
|
|
372
|
+
if (a) {
|
|
373
|
+
a.shiftTime(idt);
|
|
374
|
+
t0.copyFrom(a.t);
|
|
375
|
+
}
|
|
345
376
|
}
|
|
346
377
|
if (i < l0) {
|
|
347
|
-
const maxT =
|
|
348
|
-
idt
|
|
378
|
+
const maxT = t0.compare(t1) >= 0 ? t0 : t1;
|
|
379
|
+
idt.add(maxT).sub(t0);
|
|
380
|
+
jdt.add(maxT).sub(t1);
|
|
349
381
|
b.push(...a);
|
|
350
382
|
b.t = maxT;
|
|
351
383
|
a = track0[++i];
|
|
352
384
|
}
|
|
353
385
|
result.push(b);
|
|
354
386
|
b = track1[++j];
|
|
355
|
-
if (a)
|
|
356
|
-
a.
|
|
357
|
-
|
|
358
|
-
|
|
387
|
+
if (a) {
|
|
388
|
+
a.shiftTime(idt);
|
|
389
|
+
t0.copyFrom(a.t);
|
|
390
|
+
}
|
|
391
|
+
if (b) {
|
|
392
|
+
b.shiftTime(jdt);
|
|
393
|
+
t1.copyFrom(b.t);
|
|
394
|
+
}
|
|
359
395
|
if (!a || !b)
|
|
360
396
|
break;
|
|
361
397
|
}
|
|
362
|
-
if (
|
|
398
|
+
if (t0.equals(t1)) {
|
|
363
399
|
if (a.mergeKey === b.mergeKey) {
|
|
364
400
|
a.push(...b);
|
|
365
401
|
result.push(a);
|
|
366
402
|
a = track0[++i];
|
|
367
403
|
b = track1[++j];
|
|
368
|
-
if (a)
|
|
369
|
-
a.
|
|
370
|
-
|
|
371
|
-
|
|
404
|
+
if (a) {
|
|
405
|
+
a.shiftTime(idt);
|
|
406
|
+
t0.copyFrom(a.t);
|
|
407
|
+
}
|
|
408
|
+
if (b) {
|
|
409
|
+
b.shiftTime(jdt);
|
|
410
|
+
t1.copyFrom(b.t);
|
|
411
|
+
}
|
|
372
412
|
if (!a || !b)
|
|
373
413
|
break;
|
|
374
414
|
}
|
|
@@ -376,7 +416,8 @@ export class LoweringContext {
|
|
|
376
416
|
result.push(a);
|
|
377
417
|
a = track0[++i];
|
|
378
418
|
if (a) {
|
|
379
|
-
a.
|
|
419
|
+
a.shiftTime(idt);
|
|
420
|
+
t0.copyFrom(a.t);
|
|
380
421
|
}
|
|
381
422
|
else
|
|
382
423
|
break;
|
|
@@ -385,28 +426,31 @@ export class LoweringContext {
|
|
|
385
426
|
result.push(b);
|
|
386
427
|
b = track1[++j];
|
|
387
428
|
if (b) {
|
|
388
|
-
b.
|
|
429
|
+
b.shiftTime(jdt);
|
|
430
|
+
t1.copyFrom(b.t);
|
|
389
431
|
}
|
|
390
432
|
else
|
|
391
433
|
break;
|
|
392
434
|
}
|
|
393
435
|
continue;
|
|
394
436
|
}
|
|
395
|
-
if (t0 <
|
|
437
|
+
if (t0.compare(t1) < 0) {
|
|
396
438
|
result.push(a);
|
|
397
439
|
a = track0[++i];
|
|
398
440
|
if (a) {
|
|
399
|
-
a.
|
|
441
|
+
a.shiftTime(idt);
|
|
442
|
+
t0.copyFrom(a.t);
|
|
400
443
|
continue;
|
|
401
444
|
}
|
|
402
445
|
else
|
|
403
446
|
break;
|
|
404
447
|
}
|
|
405
|
-
if (t1 <
|
|
448
|
+
if (t1.compare(t0) < 0) {
|
|
406
449
|
result.push(b);
|
|
407
450
|
b = track1[++j];
|
|
408
451
|
if (b) {
|
|
409
|
-
b.
|
|
452
|
+
b.shiftTime(jdt);
|
|
453
|
+
t1.copyFrom(b.t);
|
|
410
454
|
continue;
|
|
411
455
|
}
|
|
412
456
|
else
|
|
@@ -417,23 +461,23 @@ export class LoweringContext {
|
|
|
417
461
|
result.push(a);
|
|
418
462
|
while (++i < l0) {
|
|
419
463
|
a = track0[i];
|
|
420
|
-
if (idt)
|
|
421
|
-
a.
|
|
464
|
+
if (!idt.isZero())
|
|
465
|
+
a.shiftTime(idt);
|
|
422
466
|
result.push(a);
|
|
423
467
|
}
|
|
424
468
|
if (b)
|
|
425
469
|
result.push(b);
|
|
426
470
|
while (++j < l1) {
|
|
427
471
|
b = track1[j];
|
|
428
|
-
if (jdt)
|
|
429
|
-
b.
|
|
472
|
+
if (!jdt.isZero())
|
|
473
|
+
b.shiftTime(jdt);
|
|
430
474
|
result.push(b);
|
|
431
475
|
}
|
|
432
476
|
return result;
|
|
433
477
|
}
|
|
434
478
|
// 归并多个轨道
|
|
435
479
|
const p = new Uint16Array(l); // 当前指向第几个
|
|
436
|
-
const dt = new
|
|
480
|
+
const dt = Array.from({ length: l }, () => new Fraction()); // 每个轨道的偏移量
|
|
437
481
|
const anchorBuffer = [];
|
|
438
482
|
const heap = new HeapSort(l); // 保证每个轨在堆里最多一个
|
|
439
483
|
for (let i = 0; i < l; i++)
|
|
@@ -441,8 +485,8 @@ export class LoweringContext {
|
|
|
441
485
|
const fetchNext = (i) => {
|
|
442
486
|
const e = tracks[i][++p[i]];
|
|
443
487
|
if (e) {
|
|
444
|
-
if (dt[i])
|
|
445
|
-
e.
|
|
488
|
+
if (!dt[i].isZero())
|
|
489
|
+
e.shiftTime(dt[i]);
|
|
446
490
|
heap.push({ e, i });
|
|
447
491
|
}
|
|
448
492
|
};
|
|
@@ -454,14 +498,16 @@ export class LoweringContext {
|
|
|
454
498
|
const first = anchorBuffer[0];
|
|
455
499
|
const container = first.e;
|
|
456
500
|
let maxT = container.t;
|
|
457
|
-
for (let j = 1; j < anchorBuffer.length; j++)
|
|
458
|
-
|
|
459
|
-
|
|
501
|
+
for (let j = 1; j < anchorBuffer.length; j++) {
|
|
502
|
+
if (anchorBuffer[j].e.t.compare(maxT) > 0)
|
|
503
|
+
maxT = anchorBuffer[j].e.t;
|
|
504
|
+
}
|
|
505
|
+
dt[first.i].add(maxT).sub(container.t);
|
|
460
506
|
fetchNext(first.i);
|
|
461
507
|
for (let j = 1; j < anchorBuffer.length; j++) {
|
|
462
508
|
const item = anchorBuffer[j];
|
|
463
509
|
container.push(...item.e);
|
|
464
|
-
dt[item.i]
|
|
510
|
+
dt[item.i].add(maxT).sub(item.e.t);
|
|
465
511
|
fetchNext(item.i);
|
|
466
512
|
}
|
|
467
513
|
anchorBuffer.length = 0;
|
|
@@ -476,12 +522,11 @@ export class LoweringContext {
|
|
|
476
522
|
continue;
|
|
477
523
|
}
|
|
478
524
|
// 找同时刻的同组
|
|
479
|
-
const minT = front.e.t + 1e-6; // 避免浮点数误差
|
|
480
525
|
const consumed = [front.i];
|
|
481
526
|
while (true) {
|
|
482
527
|
const f = heap.front();
|
|
483
528
|
// 堆序保证同组相邻,遇到别的组即说明本组已取完
|
|
484
|
-
if (!f || f.e.t
|
|
529
|
+
if (!f || !f.e.t.equals(front.e.t) || f.e.mergeKey !== front.e.mergeKey)
|
|
485
530
|
break;
|
|
486
531
|
heap.pop();
|
|
487
532
|
front.e.push(...f.e);
|
|
@@ -496,6 +541,8 @@ export class LoweringContext {
|
|
|
496
541
|
}
|
|
497
542
|
}
|
|
498
543
|
class TimeColumn extends Array {
|
|
544
|
+
// map/filter/slice 默认会用本类重建,而构造器会把长度当事件用;统一退回普通数组
|
|
545
|
+
static get [Symbol.species]() { return Array; }
|
|
499
546
|
mergeKey;
|
|
500
547
|
constructor(n) {
|
|
501
548
|
super(1);
|
|
@@ -506,8 +553,12 @@ class TimeColumn extends Array {
|
|
|
506
553
|
return this[0].t;
|
|
507
554
|
}
|
|
508
555
|
set t(value) {
|
|
509
|
-
for (const
|
|
510
|
-
|
|
556
|
+
for (const node of this)
|
|
557
|
+
node.t.copyFrom(value);
|
|
558
|
+
}
|
|
559
|
+
shiftTime(offset) {
|
|
560
|
+
for (const node of this)
|
|
561
|
+
node.t.add(offset);
|
|
511
562
|
}
|
|
512
563
|
/** 将当前列所有事件的行号固化为 lowering 后的实际行号 */
|
|
513
564
|
set layoutLine(value) {
|
|
@@ -516,8 +567,9 @@ class TimeColumn extends Array {
|
|
|
516
567
|
}
|
|
517
568
|
}
|
|
518
569
|
function heapItemCmp(a, b) {
|
|
519
|
-
|
|
520
|
-
|
|
570
|
+
const timeOrder = a.e.t.compare(b.e.t);
|
|
571
|
+
if (timeOrder !== 0)
|
|
572
|
+
return timeOrder;
|
|
521
573
|
// 有无穷所以不能直接减
|
|
522
574
|
if (a.e.mergeKey !== b.e.mergeKey)
|
|
523
575
|
return a.e.mergeKey < b.e.mergeKey ? -1 : 1;
|
package/dist/lowering/track.d.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* 相对某条基线的纵向占用
|
|
3
|
-
* 约定 top <= 0 <= bottom
|
|
4
|
-
*/
|
|
5
|
-
export interface Extent {
|
|
6
|
-
top: number;
|
|
7
|
-
bottom: number;
|
|
8
|
-
}
|
|
1
|
+
import type { Extent } from "../layout/types.js";
|
|
9
2
|
/** measure 给出的单个成员的局部位置 */
|
|
10
3
|
export interface TrackPlacement {
|
|
11
4
|
/**
|
|
@@ -59,18 +52,21 @@ export interface TrackGroup {
|
|
|
59
52
|
/**
|
|
60
53
|
* 纵向音轨
|
|
61
54
|
*
|
|
62
|
-
* 一条 Track
|
|
55
|
+
* 一条 Track 就是谱面上的一条基线。它描述“谁挂在谁上面/下面”的静态拓扑,不保存任何布局求解结果
|
|
63
56
|
* 每一行的实际纵向轴由 layout 独立解出并存在自己的表里,因此 Track 本身是可以安全共享的不可变值
|
|
64
57
|
*
|
|
65
58
|
* “音乐上是不是同一条轨”与“视觉上是不是同一条基线”在这里被合并成同一个对象
|
|
66
59
|
* 两次出现要不要共用基线,完全由申请音轨时给出的 laneKey 决定(见 group)
|
|
67
60
|
*/
|
|
68
61
|
export declare class Track {
|
|
62
|
+
readonly parent: Track | null;
|
|
69
63
|
/**
|
|
70
64
|
* 挂在本轨上的分组;无需宿主定位的先合并,其余按声明顺序向外放置
|
|
71
65
|
* 一个 group 理解为时间线上的一次分叉,但分叉模式相同会直接复用
|
|
72
66
|
*/
|
|
73
67
|
readonly groups: TrackGroup[];
|
|
68
|
+
/** 本轨从哪条基线分叉而来,根轨为 null;新声部据此继承力度这类持续状态 */
|
|
69
|
+
constructor(parent?: Track | null);
|
|
74
70
|
/**
|
|
75
71
|
* 申请一组分支音轨
|
|
76
72
|
*
|
package/dist/lowering/track.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 纵向音轨
|
|
3
3
|
*
|
|
4
|
-
* 一条 Track
|
|
4
|
+
* 一条 Track 就是谱面上的一条基线。它描述“谁挂在谁上面/下面”的静态拓扑,不保存任何布局求解结果
|
|
5
5
|
* 每一行的实际纵向轴由 layout 独立解出并存在自己的表里,因此 Track 本身是可以安全共享的不可变值
|
|
6
6
|
*
|
|
7
7
|
* “音乐上是不是同一条轨”与“视觉上是不是同一条基线”在这里被合并成同一个对象
|
|
8
8
|
* 两次出现要不要共用基线,完全由申请音轨时给出的 laneKey 决定(见 group)
|
|
9
9
|
*/
|
|
10
10
|
export class Track {
|
|
11
|
+
parent;
|
|
11
12
|
/**
|
|
12
13
|
* 挂在本轨上的分组;无需宿主定位的先合并,其余按声明顺序向外放置
|
|
13
14
|
* 一个 group 理解为时间线上的一次分叉,但分叉模式相同会直接复用
|
|
14
15
|
*/
|
|
15
16
|
groups = [];
|
|
17
|
+
/** 本轨从哪条基线分叉而来,根轨为 null;新声部据此继承力度这类持续状态 */
|
|
18
|
+
constructor(parent = null) {
|
|
19
|
+
this.parent = parent;
|
|
20
|
+
}
|
|
16
21
|
/**
|
|
17
22
|
* 申请一组分支音轨
|
|
18
23
|
*
|
|
@@ -29,7 +34,7 @@ export class Track {
|
|
|
29
34
|
this.groups.push(group);
|
|
30
35
|
}
|
|
31
36
|
while (group.members.length < count)
|
|
32
|
-
group.members.push(new Track());
|
|
37
|
+
group.members.push(new Track(this));
|
|
33
38
|
return group;
|
|
34
39
|
}
|
|
35
40
|
}
|