jpfun 0.1.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/LICENSE +200 -200
- package/README.md +191 -133
- 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 +48 -19
- package/dist/functions/beam/auto.js +35 -18
- 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 +14 -12
- package/dist/functions/dash/index.js +37 -5
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +51 -23
- package/dist/functions/dot/index.js +16 -8
- package/dist/functions/grace/index.d.ts +8 -18
- package/dist/functions/grace/index.js +98 -99
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +98 -5
- 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 +60 -80
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +42 -34
- package/dist/functions/stack/index.js +39 -34
- 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 -3
- package/dist/functions/text/index.js +78 -26
- package/dist/functions/tie/index.js +103 -128
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/{over → up}/index.d.ts +1 -0
- package/dist/functions/up/index.js +370 -0
- package/dist/functions/voice/index.js +125 -108
- 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 +100 -66
- 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 +19 -23
- package/dist/lowering/loweringContext.js +179 -187
- package/dist/lowering/track.d.ts +33 -10
- package/dist/lowering/track.js +6 -6
- package/dist/lowering/types.d.ts +57 -43
- package/dist/lowering/types.js +20 -4
- 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 +217 -122
- package/package.json +14 -9
- package/dist/functions/over/index.js +0 -307
package/dist/lowering/track.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface Extent {
|
|
|
6
6
|
top: number;
|
|
7
7
|
bottom: number;
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
9
|
+
/** measure 给出的单个成员的局部位置 */
|
|
10
10
|
export interface TrackPlacement {
|
|
11
11
|
/**
|
|
12
12
|
* 成员在本行实际占用的范围
|
|
@@ -17,26 +17,49 @@ export interface TrackPlacement {
|
|
|
17
17
|
offset: number;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* 测量一组成员在分组局部坐标中的位置
|
|
21
21
|
*
|
|
22
|
-
* @param host 宿主已占用的范围
|
|
23
22
|
* @param members 每个成员的完整子树占用;null 表示该成员本行没有任何内容
|
|
24
23
|
* @param gap 本行建议的最小间隙,按占用边界算而不是基线(缺省为该行最大字号的 0.75 倍)
|
|
25
24
|
* @returns 与 members 等长;null 表示该成员本行不占位
|
|
26
25
|
*/
|
|
27
|
-
export type
|
|
26
|
+
export type MeasureFn = (members: readonly (Extent | null)[], gap: number) => readonly (TrackPlacement | null)[];
|
|
27
|
+
/** 宿主与分组都完成测量后,决定整个分组相对宿主基线的偏移 */
|
|
28
|
+
export type PlaceFn = (host: Extent, group: Extent, gap: number) => number;
|
|
29
|
+
/**
|
|
30
|
+
* parallel 节点的分轨与纵向排列声明
|
|
31
|
+
*
|
|
32
|
+
* 这是具体函数唯一需要提供的纵向信息。LoweringContext 据此分配音轨,
|
|
33
|
+
* layout 据此求解纵向轴;两者都不需要知道声明它的是 stack、voices 还是别的什么。
|
|
34
|
+
*/
|
|
35
|
+
export interface TrackArrangement {
|
|
36
|
+
/**
|
|
37
|
+
* 音轨复用键:同一宿主上 laneKey 相同的多次出现共用同一批音轨(同一条基线)
|
|
38
|
+
* 例如 stack 用 "stack",voices 用 `voices/成员数`
|
|
39
|
+
*/
|
|
40
|
+
laneKey: string;
|
|
41
|
+
/**
|
|
42
|
+
* 哪个成员就地留在宿主轨上,缺省为 0
|
|
43
|
+
* stack 用默认值以保证宿主的旋律主线不被打断;voices 传 null 表示宿主不是成员
|
|
44
|
+
*/
|
|
45
|
+
hostIndex?: number | null;
|
|
46
|
+
/** 成员在分组局部坐标中怎么排列 */
|
|
47
|
+
measure: MeasureFn;
|
|
48
|
+
/** 需要完整宿主占用才能定位时声明;缺省表示分组直接落在宿主基线上 */
|
|
49
|
+
place?: PlaceFn;
|
|
50
|
+
}
|
|
28
51
|
/** 一组由同一个 parallel 函数派生出来的分支音轨 */
|
|
29
52
|
export interface TrackGroup {
|
|
30
53
|
readonly laneKey: string;
|
|
31
54
|
/** 只含分支音轨,不含宿主;顺序即书写顺序 */
|
|
32
55
|
readonly members: Track[];
|
|
33
|
-
|
|
34
|
-
readonly
|
|
56
|
+
readonly measure: MeasureFn;
|
|
57
|
+
readonly place?: PlaceFn;
|
|
35
58
|
}
|
|
36
59
|
/**
|
|
37
60
|
* 纵向音轨
|
|
38
61
|
*
|
|
39
|
-
* 一条 Track
|
|
62
|
+
* 一条 Track 就是谱面上的一条基线。它描述“谁挂在谁上面/下面”的静态拓扑,不保存任何布局求解结果
|
|
40
63
|
* 每一行的实际纵向轴由 layout 独立解出并存在自己的表里,因此 Track 本身是可以安全共享的不可变值
|
|
41
64
|
*
|
|
42
65
|
* “音乐上是不是同一条轨”与“视觉上是不是同一条基线”在这里被合并成同一个对象
|
|
@@ -44,7 +67,7 @@ export interface TrackGroup {
|
|
|
44
67
|
*/
|
|
45
68
|
export declare class Track {
|
|
46
69
|
/**
|
|
47
|
-
*
|
|
70
|
+
* 挂在本轨上的分组;无需宿主定位的先合并,其余按声明顺序向外放置
|
|
48
71
|
* 一个 group 理解为时间线上的一次分叉,但分叉模式相同会直接复用
|
|
49
72
|
*/
|
|
50
73
|
readonly groups: TrackGroup[];
|
|
@@ -55,7 +78,7 @@ export declare class Track {
|
|
|
55
78
|
* - stack 固定用 "stack",让同一行里先后出现的多个 stack 落在同一条伴奏轨上,基线不抖;
|
|
56
79
|
* - voices 用 `voices/成员数`,让成员数相同的块共线、成员数不同的块各自围绕宿主居中。
|
|
57
80
|
*
|
|
58
|
-
* 同一个 laneKey
|
|
81
|
+
* 同一个 laneKey 首次注册的排列策略生效,后续调用只负责按需扩充成员数量。
|
|
59
82
|
*/
|
|
60
|
-
group(
|
|
83
|
+
group(spec: TrackArrangement, count: number): TrackGroup;
|
|
61
84
|
}
|
package/dist/lowering/track.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 纵向音轨
|
|
3
3
|
*
|
|
4
|
-
* 一条 Track
|
|
4
|
+
* 一条 Track 就是谱面上的一条基线。它描述“谁挂在谁上面/下面”的静态拓扑,不保存任何布局求解结果
|
|
5
5
|
* 每一行的实际纵向轴由 layout 独立解出并存在自己的表里,因此 Track 本身是可以安全共享的不可变值
|
|
6
6
|
*
|
|
7
7
|
* “音乐上是不是同一条轨”与“视觉上是不是同一条基线”在这里被合并成同一个对象
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
export class Track {
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* 挂在本轨上的分组;无需宿主定位的先合并,其余按声明顺序向外放置
|
|
13
13
|
* 一个 group 理解为时间线上的一次分叉,但分叉模式相同会直接复用
|
|
14
14
|
*/
|
|
15
15
|
groups = [];
|
|
@@ -20,12 +20,12 @@ export class Track {
|
|
|
20
20
|
* - stack 固定用 "stack",让同一行里先后出现的多个 stack 落在同一条伴奏轨上,基线不抖;
|
|
21
21
|
* - voices 用 `voices/成员数`,让成员数相同的块共线、成员数不同的块各自围绕宿主居中。
|
|
22
22
|
*
|
|
23
|
-
* 同一个 laneKey
|
|
23
|
+
* 同一个 laneKey 首次注册的排列策略生效,后续调用只负责按需扩充成员数量。
|
|
24
24
|
*/
|
|
25
|
-
group(
|
|
26
|
-
let group = this.groups.find(group => group.laneKey === laneKey);
|
|
25
|
+
group(spec, count) {
|
|
26
|
+
let group = this.groups.find(group => group.laneKey === spec.laneKey);
|
|
27
27
|
if (!group) {
|
|
28
|
-
group = { laneKey, members: [],
|
|
28
|
+
group = { laneKey: spec.laneKey, members: [], measure: spec.measure, place: spec.place };
|
|
29
29
|
this.groups.push(group);
|
|
30
30
|
}
|
|
31
31
|
while (group.members.length < count)
|
package/dist/lowering/types.d.ts
CHANGED
|
@@ -1,58 +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";
|
|
5
|
-
import type {
|
|
7
|
+
import type { PlaybackEmitter, PlaybackState } from "../playback/types.js";
|
|
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
|
-
attachment?: LayoutAttachment;
|
|
34
|
-
onTemporal?(node: TemporalNodeBase): void;
|
|
35
|
-
onAttachment?(attachment: LayoutAttachment): void;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* parallel 节点的分轨与纵向排列声明
|
|
39
|
-
*
|
|
40
|
-
* 这是具体函数唯一需要提供的纵向信息。LoweringContext 据此分配音轨,
|
|
41
|
-
* layout 据此求解纵向轴;两者都不需要知道声明它的是 stack、voices 还是别的什么。
|
|
42
|
-
*/
|
|
43
|
-
export interface TrackArrangement {
|
|
44
|
-
/**
|
|
45
|
-
* 音轨复用键:同一宿主上 laneKey 相同的多次出现共用同一批音轨(同一条基线)
|
|
46
|
-
* 例如 stack 用 "stack",voices 用 `voices/成员数`
|
|
47
|
-
*/
|
|
48
|
-
laneKey: string;
|
|
49
56
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
57
|
+
* 当前分组退出后会自动注册的 attachment
|
|
58
|
+
*
|
|
59
|
+
* attachment 在 layout 的时候会按照顺序求解,对于 box 这样的必须让子元素先被测量,才能确定尺寸,所以需要最后加入
|
|
60
|
+
* 之所以有这个字段(而不是在 endLoweringGroup 之后手动 push)是为了收集到的信息可以直接传递,参考 box
|
|
52
61
|
*/
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
|
|
62
|
+
attachment?: LoweringAttachment;
|
|
63
|
+
/** 观察组内生成的事件 */
|
|
64
|
+
onTemporal?(node: TemporalNodeBase): void;
|
|
65
|
+
/** 观察组内注册的附属对象 */
|
|
66
|
+
onAttachment?(attachment: LoweringAttachment): void;
|
|
56
67
|
}
|
|
57
68
|
/**
|
|
58
69
|
* 时间流动模式(由 AST 节点的 timeFlowModel 返回)
|
|
@@ -71,36 +82,33 @@ export type TimeFlowModel = {
|
|
|
71
82
|
tracks: TrackArrangement;
|
|
72
83
|
};
|
|
73
84
|
/**
|
|
74
|
-
*
|
|
75
|
-
* - anchor: 时间对齐点,比如 bar
|
|
76
|
-
* - single: 需要单独成列,一般是设置或 br 等控制事件,避免与普通事件合列
|
|
77
|
-
* - default: 其他所有希望被分到同一个时间列的
|
|
85
|
+
* 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
|
|
78
86
|
*
|
|
79
|
-
*
|
|
87
|
+
* 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
|
|
88
|
+
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
|
|
80
89
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
90
|
+
/** 时间对齐点(小节线);必须是最小值,最先出队 */
|
|
91
|
+
export declare const ANCHOR_KEY: number;
|
|
92
|
+
/** 普通事件的公共组 */
|
|
93
|
+
export declare const DEFAULT_KEY: number;
|
|
86
94
|
/**
|
|
87
95
|
* 时间线事件
|
|
88
96
|
*
|
|
89
97
|
* 由 AST 节点在 lowering 阶段产生,负责得到时间、设置布局参数
|
|
90
98
|
*/
|
|
91
99
|
export declare class TemporalNodeBase implements TimeLineEvent {
|
|
92
|
-
t:
|
|
93
|
-
T:
|
|
100
|
+
readonly t: Fraction;
|
|
101
|
+
readonly T: Fraction;
|
|
94
102
|
track: Track;
|
|
95
103
|
ast: ASTNodeBase;
|
|
96
104
|
order: number;
|
|
97
105
|
addon?: Record<string, any>;
|
|
98
|
-
|
|
106
|
+
mergeKey: number;
|
|
99
107
|
/**
|
|
100
108
|
* 被折叠进哪个宿主的盒子(由宿主自己设置)
|
|
101
109
|
*
|
|
102
110
|
* 折叠成员不进入全局 columns,在时间流里没有独立位置,
|
|
103
|
-
*
|
|
111
|
+
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
104
112
|
*/
|
|
105
113
|
foldedInto?: TemporalNodeBase;
|
|
106
114
|
/**
|
|
@@ -147,7 +155,13 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
147
155
|
* 时间状态 修改&冻结 入口
|
|
148
156
|
* 调用时机在时间位置已经确定之后,处理“调性、速度、拍号”等时间信息的固化
|
|
149
157
|
*/
|
|
150
|
-
onTimeState?(state:
|
|
158
|
+
onTimeState?(state: TimeState): void;
|
|
159
|
+
/** 把已固化的音乐语义声明为播放事件;不执行实时播放 */
|
|
160
|
+
emitPlayback?(emitter: PlaybackEmitter): void;
|
|
161
|
+
/** 给自己所在的时间列贴播放标记,供控制流游标查询 */
|
|
162
|
+
playbackMarks?(): readonly string[];
|
|
163
|
+
/** 向播放系统告知自己这个记谱位置上的速度与力度 */
|
|
164
|
+
playbackState?: PlaybackState;
|
|
151
165
|
/**
|
|
152
166
|
* 根据已经固化的语义生成固有尺寸
|
|
153
167
|
* 一般为测量函数自有图形或使用 LayoutPrepareContext.textMeasurer 测量文本,填写 box.w/h/anchor/visualAxis
|
package/dist/lowering/types.js
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
import { Fraction } from "../fraction.js";
|
|
2
|
+
export const DEFAULT_BPM = 120;
|
|
3
|
+
export const DEFAULT_VELOCITY = 80;
|
|
4
|
+
export const DEFAULT_TONALITY = "C4";
|
|
5
|
+
/**
|
|
6
|
+
* 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
|
|
7
|
+
*
|
|
8
|
+
* 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
|
|
9
|
+
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
|
|
10
|
+
*/
|
|
11
|
+
/** 时间对齐点(小节线);必须是最小值,最先出队 */
|
|
12
|
+
export const ANCHOR_KEY = -Infinity;
|
|
13
|
+
/** 普通事件的公共组 */
|
|
14
|
+
export const DEFAULT_KEY = Infinity;
|
|
1
15
|
/**
|
|
2
16
|
* 时间线事件
|
|
3
17
|
*
|
|
@@ -6,18 +20,18 @@
|
|
|
6
20
|
export class TemporalNodeBase {
|
|
7
21
|
// 下面这批字段统一由 LoweringContext.initEvent 原地补全
|
|
8
22
|
// 横向 layout 要用的三个属性
|
|
9
|
-
t; // 事件发生的时间点
|
|
10
|
-
T; // 事件的持续时间
|
|
23
|
+
t = new Fraction(); // 事件发生的时间点
|
|
24
|
+
T = new Fraction(); // 事件的持续时间
|
|
11
25
|
track; // 所在的纵向音轨;同轨判断使用引用相等
|
|
12
26
|
ast; // 对应的 AST 节点
|
|
13
27
|
order; // 事件创建序号,作为id。可用于区分同时发生的父子、排序同一时刻发生的事件
|
|
14
28
|
addon; // 其他任意字段,仅在存在已固化的函数语义时创建,比如存储 div 和 dot 的数目
|
|
15
|
-
|
|
29
|
+
mergeKey; // 时间列合并组
|
|
16
30
|
/**
|
|
17
31
|
* 被折叠进哪个宿主的盒子(由宿主自己设置)
|
|
18
32
|
*
|
|
19
33
|
* 折叠成员不进入全局 columns,在时间流里没有独立位置,
|
|
20
|
-
*
|
|
34
|
+
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
21
35
|
*/
|
|
22
36
|
foldedInto;
|
|
23
37
|
/**
|
|
@@ -66,6 +80,8 @@ export class TemporalNodeBase {
|
|
|
66
80
|
anchor: 0, visualAxis: 0,
|
|
67
81
|
};
|
|
68
82
|
}
|
|
83
|
+
/** 向播放系统告知自己这个记谱位置上的速度与力度 */
|
|
84
|
+
playbackState;
|
|
69
85
|
/**
|
|
70
86
|
* 根据已经固化的语义生成固有尺寸
|
|
71
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;
|
|
@@ -170,9 +170,3 @@ export function findRightParen(source, start, end) {
|
|
|
170
170
|
}
|
|
171
171
|
return -1;
|
|
172
172
|
}
|
|
173
|
-
/** 识别字符串参数时用于剥离双引号;单引号不是字符串语法 */
|
|
174
|
-
export function removeQuote(source) {
|
|
175
|
-
if (source.startsWith('"') && source.endsWith('"'))
|
|
176
|
-
return source.slice(1, -1);
|
|
177
|
-
return source;
|
|
178
|
-
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
|
|
1
2
|
export declare const NoteNameMap: Record<string, number>;
|
|
2
3
|
export interface RelativeJianpuPitch {
|
|
3
4
|
renderName: string;
|
|
@@ -7,4 +8,11 @@ export interface RelativeJianpuPitch {
|
|
|
7
8
|
export declare function tonality2Midi(tonality: string, defaultOctave: number): number;
|
|
8
9
|
export declare function acc2Offset(acc: string, ignoreNatural?: boolean, initOffset?: number): number;
|
|
9
10
|
export declare function resolveNoteMidi(name: string, acc: string, octave: number, keySignature: string): number | null;
|
|
11
|
+
/**
|
|
12
|
+
* 固化音符在当前大调中的位置,返回“沿调内音级移动”的闭包
|
|
13
|
+
*
|
|
14
|
+
* 交出闭包而不是数据结构,播放层因此无需认识任何调性模型。
|
|
15
|
+
* 基准位置不包含主体的临时升降号。
|
|
16
|
+
*/
|
|
17
|
+
export declare function createDiatonicTranspose(name: string, acc: string, octave: number, keySignature: string): ((steps: number) => number) | undefined;
|
|
10
18
|
export declare function resolveLetterNameToJianpu(name: string, acc: string, octave: number, keySignature: string): RelativeJianpuPitch | null;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Diagnostic } from "../../diagnostic.js";
|
|
2
|
+
/** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
|
|
2
3
|
export const NoteNameMap = {
|
|
3
4
|
"C": 0,
|
|
4
5
|
"C#": 1, "Db": 1,
|
|
@@ -69,6 +70,34 @@ export function resolveNoteMidi(name, acc, octave, keySignature) {
|
|
|
69
70
|
}
|
|
70
71
|
return (octave + 1) * 12 + noteValue + offset;
|
|
71
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* 固化音符在当前大调中的位置,返回“沿调内音级移动”的闭包
|
|
75
|
+
*
|
|
76
|
+
* 交出闭包而不是数据结构,播放层因此无需认识任何调性模型。
|
|
77
|
+
* 基准位置不包含主体的临时升降号。
|
|
78
|
+
*/
|
|
79
|
+
export function createDiatonicTranspose(name, acc, octave, keySignature) {
|
|
80
|
+
let degreeIndex;
|
|
81
|
+
let relativeOctave;
|
|
82
|
+
if (name >= "1" && name <= "7") {
|
|
83
|
+
degreeIndex = Number(name) - 1;
|
|
84
|
+
relativeOctave = octave;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const relative = resolveLetterNameToJianpu(name, acc, octave, keySignature);
|
|
88
|
+
if (!relative)
|
|
89
|
+
return undefined;
|
|
90
|
+
degreeIndex = Number(relative.renderName) - 1;
|
|
91
|
+
relativeOctave = relative.renderOctave;
|
|
92
|
+
}
|
|
93
|
+
const tonicMidi = tonality2Midi(keySignature, 4);
|
|
94
|
+
const base = relativeOctave * 7 + degreeIndex;
|
|
95
|
+
return steps => {
|
|
96
|
+
const total = base + steps;
|
|
97
|
+
const shift = Math.floor(total / 7);
|
|
98
|
+
return tonicMidi + MAJOR_SCALE_OFFSETS[((total % 7) + 7) % 7] + shift * 12;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
72
101
|
function wrapPitchClass(value) {
|
|
73
102
|
return ((value % 12) + 12) % 12;
|
|
74
103
|
}
|
|
@@ -86,22 +115,15 @@ function offsetToAccidental(offset) {
|
|
|
86
115
|
// 把绝对字母音高转换成“相对当前调性的简谱显示信息”。
|
|
87
116
|
// 这里保留字母拼写带来的调内级数信息,因此像 key=D 时的 C 会被解释成 b7,而不是 #6。
|
|
88
117
|
export function resolveLetterNameToJianpu(name, acc, octave, keySignature) {
|
|
89
|
-
const
|
|
90
|
-
const tonicMidi = tonality2Midi(keySignature, 4);
|
|
91
|
-
const tonicPitchClass = wrapPitchClass(tonicMidi);
|
|
92
|
-
const notePitchClass = wrapPitchClass(resolveNoteMidi(name, acc, octave, keySignature) ?? NaN);
|
|
93
|
-
const tonicIndex = LETTER_ORDER.indexOf(tonicLetter);
|
|
118
|
+
const tonicIndex = LETTER_ORDER.indexOf(keySignature[0]);
|
|
94
119
|
const noteIndex = LETTER_ORDER.indexOf(name);
|
|
95
|
-
if (tonicIndex < 0 || noteIndex < 0 || Number.isNaN(notePitchClass)) {
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
const degreeIndex = (noteIndex - tonicIndex + LETTER_ORDER.length) % LETTER_ORDER.length;
|
|
99
|
-
const expectedPitchClass = wrapPitchClass(tonicPitchClass + MAJOR_SCALE_OFFSETS[degreeIndex]);
|
|
100
|
-
const accidentalOffset = normalizeAccidentalOffset(notePitchClass - expectedPitchClass);
|
|
101
120
|
const actualMidi = resolveNoteMidi(name, acc, octave, keySignature);
|
|
102
|
-
if (actualMidi === null)
|
|
121
|
+
if (tonicIndex < 0 || noteIndex < 0 || actualMidi === null)
|
|
103
122
|
return null;
|
|
104
|
-
|
|
123
|
+
const tonicMidi = tonality2Midi(keySignature, 4);
|
|
124
|
+
const degreeIndex = (noteIndex - tonicIndex + LETTER_ORDER.length) % LETTER_ORDER.length;
|
|
125
|
+
const expectedPitchClass = wrapPitchClass(tonicMidi + MAJOR_SCALE_OFFSETS[degreeIndex]);
|
|
126
|
+
const accidentalOffset = normalizeAccidentalOffset(wrapPitchClass(actualMidi) - expectedPitchClass);
|
|
105
127
|
const octaveOffset = (actualMidi - tonicMidi - MAJOR_SCALE_OFFSETS[degreeIndex] - accidentalOffset) / 12;
|
|
106
128
|
return {
|
|
107
129
|
renderName: String(degreeIndex + 1),
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字符串字面量的唯一规则来源:预处理、`@text` 糖、`L:` 歌词糖共用
|
|
3
|
+
* 扫描与反转义必须互为镜像,否则会出现“扫得对、还原错”
|
|
4
|
+
*/
|
|
5
|
+
/** 找与 quotePos 处开引号配对的闭引号;找不到返回 -1 */
|
|
6
|
+
export declare function findClosingQuote(source: string, quotePos: number, end: number): number;
|
|
7
|
+
/** 剥掉成对的双引号并反转义 `\X` -> `X`;裸参数没走转义扫描,原样返回 */
|
|
8
|
+
export declare function removeQuote(source: string): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字符串字面量的唯一规则来源:预处理、`@text` 糖、`L:` 歌词糖共用
|
|
3
|
+
* 扫描与反转义必须互为镜像,否则会出现“扫得对、还原错”
|
|
4
|
+
*/
|
|
5
|
+
/** 找与 quotePos 处开引号配对的闭引号;找不到返回 -1 */
|
|
6
|
+
export function findClosingQuote(source, quotePos, end) {
|
|
7
|
+
for (let i = quotePos + 1; i < end; i++) {
|
|
8
|
+
if (source[i] === "\\")
|
|
9
|
+
i++;
|
|
10
|
+
else if (source[i] === '"')
|
|
11
|
+
return i;
|
|
12
|
+
}
|
|
13
|
+
return -1;
|
|
14
|
+
}
|
|
15
|
+
/** 剥掉成对的双引号并反转义 `\X` -> `X`;裸参数没走转义扫描,原样返回 */
|
|
16
|
+
export function removeQuote(source) {
|
|
17
|
+
if (source.length < 2 || !source.startsWith('"') || !source.endsWith('"'))
|
|
18
|
+
return source;
|
|
19
|
+
return source.slice(1, -1).replace(/\\(.)/gs, "$1");
|
|
20
|
+
}
|
|
@@ -4,6 +4,8 @@ import { GrammarCallNode, GrammarNode, type SyntaxAnalysis } from "./grammarType
|
|
|
4
4
|
import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue } from "../functions/ASTtypes.js";
|
|
5
5
|
export declare const DEFAULT_FONT_SIZE = 22;
|
|
6
6
|
export declare const DEFAULT_OCTAVE = 4;
|
|
7
|
+
/** 系统变量,供 `@set` 读取 */
|
|
8
|
+
export declare const SYSTEM_VARIABLE_TYPES: Record<string, paramType>;
|
|
7
9
|
export declare class ParserContext {
|
|
8
10
|
/** 根解析器为 0,内容参数和大括号子解析器依次加一 */
|
|
9
11
|
readonly scopeDepth: number;
|
|
@@ -23,15 +25,15 @@ export declare class ParserContext {
|
|
|
23
25
|
deSugarAtomFns: deSugarAtomFunction[];
|
|
24
26
|
/** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
|
|
25
27
|
deSugarRelationFns: deSugarRelationFunction[];
|
|
26
|
-
/** .labelable()
|
|
27
|
-
labelableNodes: ASTFunctionNode[];
|
|
28
|
+
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
29
|
+
labelableNodes: (ASTFunctionNode | null)[];
|
|
28
30
|
nodes: ASTNodeBase[];
|
|
29
31
|
constructor(ctx: ParserContext | {
|
|
30
32
|
source: string;
|
|
31
33
|
diagnostics?: Diagnostic[];
|
|
32
34
|
variables?: Map<string, any>;
|
|
33
35
|
functions?: Map<string, ASTFunctionClass>;
|
|
34
|
-
labelableNodes?: ASTFunctionNode[];
|
|
36
|
+
labelableNodes?: (ASTFunctionNode | null)[];
|
|
35
37
|
toConsume?: ASTNodeBase[];
|
|
36
38
|
commentSpans?: SourceSpan[];
|
|
37
39
|
});
|
|
@@ -41,9 +43,10 @@ export declare class ParserContext {
|
|
|
41
43
|
];
|
|
42
44
|
pushNode(node: ASTNodeBase): void;
|
|
43
45
|
get fontSize(): number;
|
|
44
|
-
set fontSize(size: number
|
|
46
|
+
set fontSize(size: number);
|
|
45
47
|
get strict(): boolean;
|
|
46
48
|
set strict(value: boolean);
|
|
49
|
+
setVariable(name: string, value: paramValue): void;
|
|
47
50
|
registerFunctions(functionClasses: ASTFunctionClass[]): void;
|
|
48
51
|
/**
|
|
49
52
|
* 扫描 GrammarNode 并构造 AST;不产出 syntax
|
|
@@ -85,7 +88,7 @@ export declare class ParserContext {
|
|
|
85
88
|
* funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
|
|
86
89
|
* 解析成功返回值 解析失败返回null
|
|
87
90
|
*/
|
|
88
|
-
parseArgWithType(
|
|
91
|
+
parseArgWithType(span: SourceSpan, type?: paramType, funcStart?: number): paramValue | null;
|
|
89
92
|
length2px(length: LengthValue): number;
|
|
90
93
|
}
|
|
91
94
|
export declare function skipSpaces(s: string, i: number, end: number): number;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
-
import { readCall, trimRange
|
|
2
|
+
import { readCall, trimRange } from "./parse-utils/call-utils.js";
|
|
3
|
+
import { removeQuote } from "./parse-utils/string-utils.js";
|
|
3
4
|
import { readBrace } from "./parse-utils/brace-utils.js";
|
|
4
5
|
import { readLabel } from "./parse-utils/label-utils.js";
|
|
5
6
|
import { parseLength } from "./parse-utils/length-utils.js";
|
|
@@ -23,6 +24,11 @@ function classifySyntaxValue(type, text) {
|
|
|
23
24
|
export const DEFAULT_FONT_SIZE = 22;
|
|
24
25
|
const DEFAULT_STRICT_MODE = false;
|
|
25
26
|
export const DEFAULT_OCTAVE = 4;
|
|
27
|
+
/** 系统变量,供 `@set` 读取 */
|
|
28
|
+
export const SYSTEM_VARIABLE_TYPES = {
|
|
29
|
+
fontsize: "length",
|
|
30
|
+
strict: "boolean",
|
|
31
|
+
};
|
|
26
32
|
// 解析上下文
|
|
27
33
|
export class ParserContext {
|
|
28
34
|
/** 根解析器为 0,内容参数和大括号子解析器依次加一 */
|
|
@@ -43,7 +49,7 @@ export class ParserContext {
|
|
|
43
49
|
deSugarAtomFns;
|
|
44
50
|
/** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
|
|
45
51
|
deSugarRelationFns;
|
|
46
|
-
/** .labelable()
|
|
52
|
+
/** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
|
|
47
53
|
labelableNodes;
|
|
48
54
|
nodes; // 解析结果
|
|
49
55
|
constructor(ctx) {
|
|
@@ -97,25 +103,16 @@ export class ParserContext {
|
|
|
97
103
|
}
|
|
98
104
|
pushNode(node) {
|
|
99
105
|
this.nodes.push(node);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this.labelableNodes.push(target);
|
|
105
|
-
}
|
|
106
|
+
// 未实现的对标签透明不入表,null 入表成为边界
|
|
107
|
+
const target = node.labelable?.();
|
|
108
|
+
if (target !== undefined)
|
|
109
|
+
this.labelableNodes.push(target);
|
|
106
110
|
}
|
|
107
111
|
get fontSize() {
|
|
108
112
|
return this.variables["fontsize"] ?? DEFAULT_FONT_SIZE;
|
|
109
113
|
}
|
|
110
114
|
set fontSize(size) {
|
|
111
|
-
|
|
112
|
-
const l = parseLength(size);
|
|
113
|
-
if (l instanceof Diagnostic)
|
|
114
|
-
throw l;
|
|
115
|
-
this.variables["fontsize"] = this.length2px(l);
|
|
116
|
-
}
|
|
117
|
-
else
|
|
118
|
-
this.variables["fontsize"] = size;
|
|
115
|
+
this.variables["fontsize"] = size;
|
|
119
116
|
}
|
|
120
117
|
get strict() {
|
|
121
118
|
return this.variables["strict"] ?? DEFAULT_STRICT_MODE;
|
|
@@ -123,6 +120,14 @@ export class ParserContext {
|
|
|
123
120
|
set strict(value) {
|
|
124
121
|
this.variables["strict"] = value;
|
|
125
122
|
}
|
|
123
|
+
setVariable(name, value) {
|
|
124
|
+
if (name === "fontsize")
|
|
125
|
+
this.fontSize = this.length2px(value);
|
|
126
|
+
else if (name === "strict")
|
|
127
|
+
this.strict = value;
|
|
128
|
+
else
|
|
129
|
+
this.variables[name] = value;
|
|
130
|
+
}
|
|
126
131
|
registerFunctions(functionClasses) {
|
|
127
132
|
const map = this.functions;
|
|
128
133
|
for (const funcClass of functionClasses) {
|
|
@@ -247,20 +252,18 @@ export class ParserContext {
|
|
|
247
252
|
this.nodes.push(new ASTBraceNode(node.span, subParser.parse(node.span.start + 1, node.span.end - 1), null));
|
|
248
253
|
break;
|
|
249
254
|
case "label":
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const labelNode = new ASTLabelNode(node.span, node.label, tgt);
|
|
258
|
-
// 处理待消费列表只处理没有parent的 而label是有parent的 label其实无所谓parent
|
|
259
|
-
this.nodes.push(labelNode);
|
|
255
|
+
// 透明节点不入表,所以表尾就是绑定终点:null 是边界,已被占用的承载者同样到此为止
|
|
256
|
+
const tgt = this.labelableNodes.at(-1);
|
|
257
|
+
if (!tgt) {
|
|
258
|
+
this.diagnostics.push(Diagnostic.warning.LabelWithoutTarget(node.label, node.span));
|
|
259
|
+
}
|
|
260
|
+
else if (tgt.label !== void 0) {
|
|
261
|
+
this.diagnostics.push(Diagnostic.warning.TargetAlreadyLabeled(node.label, tgt.label, node.span));
|
|
260
262
|
}
|
|
261
263
|
else {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
+
tgt.label = node.label;
|
|
265
|
+
// 同时创建一个LabelNode 供编辑器等工具使用
|
|
266
|
+
this.nodes.push(new ASTLabelNode(node.span, node.label, tgt));
|
|
264
267
|
}
|
|
265
268
|
break;
|
|
266
269
|
case "sugar":
|
|
@@ -415,10 +418,10 @@ export class ParserContext {
|
|
|
415
418
|
this.diagnostics.push(Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.length, i + 1, arg.valueSpan));
|
|
416
419
|
}
|
|
417
420
|
else
|
|
418
|
-
args.set(key, arg
|
|
421
|
+
args.set(key, arg);
|
|
419
422
|
continue;
|
|
420
423
|
}
|
|
421
|
-
const value = this.parseArgWithType(arg.valueSpan
|
|
424
|
+
const value = this.parseArgWithType(arg.valueSpan, type, callNode.span.start);
|
|
422
425
|
if (value !== null)
|
|
423
426
|
args.set(key, value);
|
|
424
427
|
}
|
|
@@ -432,9 +435,9 @@ export class ParserContext {
|
|
|
432
435
|
* funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
|
|
433
436
|
* 解析成功返回值 解析失败返回null
|
|
434
437
|
*/
|
|
435
|
-
parseArgWithType(
|
|
438
|
+
parseArgWithType(span, type, funcStart) {
|
|
436
439
|
// 先去空白
|
|
437
|
-
const r = trimRange(this.source,
|
|
440
|
+
const r = trimRange(this.source, span.start, span.end);
|
|
438
441
|
const start = r.start, end = r.end;
|
|
439
442
|
if (start >= end)
|
|
440
443
|
return null; // 纯空白参数视为未提供
|
|
@@ -477,10 +480,12 @@ export class ParserContext {
|
|
|
477
480
|
}
|
|
478
481
|
case "label":
|
|
479
482
|
// 应对潜在的问题: 如果先解析了content会导致ctx.lavelableNodes被污染
|
|
480
|
-
funcStart ??=
|
|
483
|
+
funcStart ??= span.start;
|
|
481
484
|
// label情况下返回对象 从后向前查找以支持标签覆盖
|
|
482
485
|
for (let i = this.labelableNodes.length - 1; i >= 0; i--) {
|
|
483
486
|
const node = this.labelableNodes[i];
|
|
487
|
+
if (!node)
|
|
488
|
+
continue; // 边界标记不参与按名查找
|
|
484
489
|
if (node.sourceSpan.start >= funcStart)
|
|
485
490
|
continue; // 只能绑定在函数定义之前的节点上
|
|
486
491
|
if (node.label == text)
|