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.
Files changed (167) hide show
  1. package/README.md +141 -11
  2. package/dist/converter/midi/convert.d.ts +50 -0
  3. package/dist/converter/midi/convert.js +700 -0
  4. package/dist/converter/midi/index.d.ts +5 -0
  5. package/dist/converter/midi/index.js +11 -0
  6. package/dist/converter/midi/json.d.ts +38 -0
  7. package/dist/converter/midi/json.js +1 -0
  8. package/dist/converter/midi/triplet.d.ts +30 -0
  9. package/dist/converter/midi/triplet.js +75 -0
  10. package/dist/converter/musicxml/dom.d.ts +25 -0
  11. package/dist/converter/musicxml/dom.js +65 -0
  12. package/dist/converter/musicxml/features.d.ts +51 -0
  13. package/dist/converter/musicxml/features.js +232 -0
  14. package/dist/converter/musicxml/index.d.ts +9 -0
  15. package/dist/converter/musicxml/index.js +1140 -0
  16. package/dist/converter/musicxml/model.d.ts +123 -0
  17. package/dist/converter/musicxml/model.js +1 -0
  18. package/dist/converter/source.d.ts +22 -0
  19. package/dist/converter/source.js +51 -0
  20. package/dist/core.d.ts +22 -0
  21. package/dist/core.js +22 -0
  22. package/dist/diagnostic.js +5 -2
  23. package/dist/fraction.d.ts +24 -0
  24. package/dist/fraction.js +132 -0
  25. package/dist/functions/ASTtypes.d.ts +51 -31
  26. package/dist/functions/ASTtypes.js +59 -38
  27. package/dist/functions/adjust/index.d.ts +2 -0
  28. package/dist/functions/adjust/index.js +193 -0
  29. package/dist/functions/arpeggio/index.d.ts +2 -0
  30. package/dist/functions/arpeggio/index.js +180 -0
  31. package/dist/functions/arpeggio/shape.d.ts +7 -0
  32. package/dist/functions/arpeggio/shape.js +122 -0
  33. package/dist/functions/bar/index.d.ts +8 -0
  34. package/dist/functions/bar/index.js +62 -25
  35. package/dist/functions/beam/auto.d.ts +1 -1
  36. package/dist/functions/beam/auto.js +33 -16
  37. package/dist/functions/beam/index.js +10 -6
  38. package/dist/functions/beam/layout.d.ts +4 -3
  39. package/dist/functions/beam/layout.js +43 -65
  40. package/dist/functions/box/index.js +37 -36
  41. package/dist/functions/br/index.js +10 -8
  42. package/dist/functions/dash/index.js +40 -7
  43. package/dist/functions/default.js +19 -8
  44. package/dist/functions/div/index.d.ts +16 -0
  45. package/dist/functions/div/index.js +45 -17
  46. package/dist/functions/dot/index.js +23 -25
  47. package/dist/functions/dyn/index.d.ts +2 -0
  48. package/dist/functions/dyn/index.js +242 -0
  49. package/dist/functions/grace/index.d.ts +8 -11
  50. package/dist/functions/grace/index.js +84 -79
  51. package/dist/functions/head/index.d.ts +2 -0
  52. package/dist/functions/head/index.js +496 -0
  53. package/dist/functions/key/index.js +98 -3
  54. package/dist/functions/meter/index.d.ts +2 -0
  55. package/dist/functions/meter/index.js +155 -0
  56. package/dist/functions/note/accidentals.d.ts +24 -3
  57. package/dist/functions/note/accidentals.js +31 -1
  58. package/dist/functions/note/index.js +106 -79
  59. package/dist/functions/note/noteNameFSM.js +52 -63
  60. package/dist/functions/page/index.d.ts +11 -2
  61. package/dist/functions/page/index.js +65 -4
  62. package/dist/functions/program/index.d.ts +2 -0
  63. package/dist/functions/program/index.js +30 -0
  64. package/dist/functions/set/index.js +43 -32
  65. package/dist/functions/stack/index.js +26 -22
  66. package/dist/functions/symbol/index.d.ts +31 -0
  67. package/dist/functions/symbol/index.js +139 -0
  68. package/dist/functions/symbol/ornament.d.ts +15 -0
  69. package/dist/functions/symbol/ornament.js +73 -0
  70. package/dist/functions/symbol/shape.d.ts +24 -0
  71. package/dist/functions/symbol/shape.js +52 -0
  72. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  73. package/dist/functions/symbol/symbols/accent.js +24 -0
  74. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  75. package/dist/functions/symbol/symbols/dc.js +82 -0
  76. package/dist/functions/symbol/symbols/ds.d.ts +2 -0
  77. package/dist/functions/symbol/symbols/ds.js +93 -0
  78. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  79. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  80. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  81. package/dist/functions/symbol/symbols/fermata.js +24 -0
  82. package/dist/functions/symbol/symbols/fine.d.ts +2 -0
  83. package/dist/functions/symbol/symbols/fine.js +12 -0
  84. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  85. package/dist/functions/symbol/symbols/mordent.js +56 -0
  86. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  87. package/dist/functions/symbol/symbols/prall.js +42 -0
  88. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  89. package/dist/functions/symbol/symbols/segno.js +82 -0
  90. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  91. package/dist/functions/symbol/symbols/tr.js +71 -0
  92. package/dist/functions/tempo/glyph.d.ts +12 -0
  93. package/dist/functions/tempo/glyph.js +30 -0
  94. package/dist/functions/tempo/index.js +57 -4
  95. package/dist/functions/temporal.d.ts +138 -0
  96. package/dist/functions/temporal.js +101 -0
  97. package/dist/functions/text/index.js +86 -27
  98. package/dist/functions/tie/index.js +136 -152
  99. package/dist/functions/tuplet/index.d.ts +2 -0
  100. package/dist/functions/tuplet/index.js +218 -0
  101. package/dist/functions/up/index.d.ts +1 -0
  102. package/dist/functions/up/index.js +258 -182
  103. package/dist/functions/voice/index.js +203 -109
  104. package/dist/functions/volta/index.d.ts +2 -0
  105. package/dist/functions/volta/index.js +230 -0
  106. package/dist/iife/core.d.ts +1 -0
  107. package/dist/iife/core.js +3 -0
  108. package/dist/iife/from-midi.d.ts +1 -0
  109. package/dist/iife/from-midi.js +15 -0
  110. package/dist/iife/from-musicxml.d.ts +1 -0
  111. package/dist/iife/from-musicxml.js +3 -0
  112. package/dist/iife/full.d.ts +1 -0
  113. package/dist/iife/full.js +3 -0
  114. package/dist/index.d.ts +3 -17
  115. package/dist/index.js +3 -17
  116. package/dist/jpfun.core.min.js +153 -0
  117. package/dist/jpfun.from-midi.min.js +9 -0
  118. package/dist/jpfun.from-musicxml.min.js +12 -0
  119. package/dist/jpfun.min.js +166 -0
  120. package/dist/layout/default.js +2 -3
  121. package/dist/layout/engine.d.ts +6 -12
  122. package/dist/layout/engine.js +115 -64
  123. package/dist/layout/model.d.ts +12 -5
  124. package/dist/layout/model.js +31 -18
  125. package/dist/layout/page.js +5 -4
  126. package/dist/layout/path.d.ts +19 -0
  127. package/dist/layout/path.js +101 -0
  128. package/dist/layout/types.d.ts +71 -20
  129. package/dist/layout/types.js +3 -1
  130. package/dist/lowering/loweringContext.d.ts +20 -20
  131. package/dist/lowering/loweringContext.js +153 -101
  132. package/dist/lowering/track.d.ts +5 -9
  133. package/dist/lowering/track.js +7 -2
  134. package/dist/lowering/types.d.ts +26 -112
  135. package/dist/lowering/types.js +1 -92
  136. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  137. package/dist/parser/parse-utils/call-utils.js +0 -6
  138. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  139. package/dist/parser/parse-utils/note-utils.js +35 -13
  140. package/dist/parser/parse-utils/string-utils.d.ts +10 -0
  141. package/dist/parser/parse-utils/string-utils.js +24 -0
  142. package/dist/parser/parserContext.d.ts +11 -9
  143. package/dist/parser/parserContext.js +65 -55
  144. package/dist/parser/preprocess.js +17 -11
  145. package/dist/pipeline.d.ts +6 -5
  146. package/dist/pipeline.js +4 -5
  147. package/dist/playback/compile.d.ts +18 -0
  148. package/dist/playback/compile.js +414 -0
  149. package/dist/playback/event.d.ts +105 -0
  150. package/dist/playback/event.js +105 -0
  151. package/dist/playback/time.d.ts +14 -0
  152. package/dist/playback/time.js +81 -0
  153. package/dist/playback/types.d.ts +129 -0
  154. package/dist/playback/types.js +3 -0
  155. package/dist/render/canvas.d.ts +4 -4
  156. package/dist/render/canvas.js +20 -5
  157. package/dist/render/index.d.ts +1 -0
  158. package/dist/render/index.js +1 -0
  159. package/dist/render/paint.d.ts +8 -0
  160. package/dist/render/paint.js +101 -0
  161. package/dist/render/svg.d.ts +3 -3
  162. package/dist/render/svg.js +10 -8
  163. package/dist/render/text.d.ts +22 -0
  164. package/dist/render/text.js +42 -0
  165. package/dist/render/types.d.ts +1 -1
  166. package/grammar.md +132 -13
  167. package/package.json +19 -6
@@ -1,38 +1,53 @@
1
- import { ASTNodeBase } from "../functions/ASTtypes.js";
1
+ import type { ASTNodeBase } from "../functions/ASTtypes.js";
2
+ import type { TemporalNodeBase } from "../functions/temporal.js";
2
3
  import type { Diagnostic } from "../diagnostic.js";
3
- import type { LayoutAttachment, HorizontalSpringConfig, LayoutBox, LayoutDecoration, LayoutHost, PageConfig, LayoutPoint, LayoutPrepareContext, HorizontalLineView, TimeLineEvent } from "../layout/types.js";
4
- import type { Painter } from "../render/types.js";
4
+ import type { SourceSpan } from "../parser/types.js";
5
+ import type { Fraction } from "../fraction.js";
6
+ import type { PageConfig } from "../layout/types.js";
5
7
  import type { Track, TrackArrangement } from "./track.js";
6
- /** 时间流固化后生成额外 attachment;所有 augmenter 读取同一份输入快照 */
7
- export type LoweringAugmenter = (result: LoweringResult) => Iterable<LayoutAttachment>;
8
+ /** lowering 产生的不推进时间的附属对象;具体能力由 layout/playback 子协议声明 */
9
+ export interface LoweringAttachment {
10
+ readonly sourceSpan?: SourceSpan;
11
+ }
12
+ /** 时间流固化后生成额外 attachment;读取统一追加前的结果,不直接修改结果 */
13
+ export type LoweringAugmenter = (result: LoweringResult) => Iterable<LoweringAttachment>;
8
14
  /** 所有 augmenter 结果追加完成后的最终处理;按注册顺序执行,可修改完整结果或抛出语义错误 */
9
15
  export type LoweringFinalizer = (result: LoweringResult) => void;
10
16
  /**
11
17
  * lowering 的输出
12
18
  *
13
19
  * - columns 时间列,是横向弹簧模型的输入
14
- * - attachments 保存 tie、beam、box 等不直接推进时间的附属布局对象
20
+ * - attachments 保存不直接推进时间的布局附件(如 tie、box)、播放关系与控制声明
15
21
  * - astToTemporal 从 AST 到 Temporal 的映射(一对多)
16
22
  * - rootTrack 纵向音轨树的根,layout 沿它递归求解每行的纵向轴
17
23
  */
18
24
  export interface LoweringResult {
19
25
  diagnostics: Diagnostic[];
20
26
  columns: TemporalNodeBase[][];
21
- attachments: LayoutAttachment[];
27
+ attachments: LoweringAttachment[];
22
28
  astToTemporal: Map<ASTNodeBase, TemporalNodeBase[]>;
23
- duration: number;
29
+ duration: Fraction;
24
30
  rootTrack: Track;
31
+ tracks: readonly Track[];
25
32
  page?: PageConfig;
26
33
  }
27
34
  /**
28
35
  * lowering 递归作用域的通用观察者
29
36
  *
30
- * onTemporal 在事件加入时间列、推进时间游标前调用。
37
+ * 回调按当前作用域栈由内向外执行;onTemporal 在事件加入时间列、推进时间游标前调用。
31
38
  */
32
39
  export interface LoweringGroup {
33
- attachment?: LayoutAttachment;
40
+ /**
41
+ * 当前分组退出后会自动注册的 attachment
42
+ *
43
+ * attachment 在 layout 的时候会按照顺序求解,对于 box 这样的必须让子元素先被测量,才能确定尺寸,所以需要最后加入
44
+ * 之所以有这个字段(而不是在 endLoweringGroup 之后手动 push)是为了收集到的信息可以直接传递,参考 box
45
+ */
46
+ attachment?: LoweringAttachment;
47
+ /** 观察组内生成的事件 */
34
48
  onTemporal?(node: TemporalNodeBase): void;
35
- onAttachment?(attachment: LayoutAttachment): void;
49
+ /** 观察组内注册的附属对象 */
50
+ onAttachment?(attachment: LoweringAttachment): void;
36
51
  }
37
52
  /**
38
53
  * 时间流动模式(由 AST 节点的 timeFlowModel 返回)
@@ -50,104 +65,3 @@ export type TimeFlowModel = {
50
65
  children: ASTNodeBase[];
51
66
  tracks: TrackArrangement;
52
67
  };
53
- /**
54
- * 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
55
- *
56
- * 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
57
- * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
58
- */
59
- /** 时间对齐点(小节线);必须是最小值,最先出队 */
60
- export declare const ANCHOR_KEY: number;
61
- /** 普通事件的公共组 */
62
- export declare const DEFAULT_KEY: number;
63
- /**
64
- * 时间线事件
65
- *
66
- * 由 AST 节点在 lowering 阶段产生,负责得到时间、设置布局参数
67
- */
68
- export declare class TemporalNodeBase implements TimeLineEvent {
69
- t: number;
70
- T: number;
71
- track: Track;
72
- ast: ASTNodeBase;
73
- order: number;
74
- addon?: Record<string, any>;
75
- mergeKey: number;
76
- /**
77
- * 被折叠进哪个宿主的盒子(由宿主自己设置)
78
- *
79
- * 折叠成员不进入全局 columns,在时间流里没有独立位置,
80
- * 因此它对外(标签、关系函数端点)一律由宿主代表;嵌套折叠沿链上溯。
81
- */
82
- foldedInto?: TemporalNodeBase;
83
- /**
84
- * 请求在自己所在时间列之前换几行,默认 0
85
- */
86
- breakBefore: number;
87
- /** 由 LoweringContext 在列归并后固化的谱面行号 */
88
- layoutLine: number;
89
- /** 不可见状态事件保持 undefined */
90
- box?: LayoutBox;
91
- /**
92
- * 横向弹簧布局参数
93
- * 大部分对象使用空对象即可采用布局器默认值
94
- */
95
- springConfig: HorizontalSpringConfig;
96
- /**
97
- * 关系函数可使用的命名局部坐标
98
- * prepareLayout 每次执行时由具体节点和装饰函数重新生成
99
- */
100
- ports: Record<string, LayoutPoint>;
101
- /**
102
- * 当前一次 layout 已经实例化并完成测量的装饰对象
103
- *
104
- * 起因:addon 只保存 lowering 阶段冻结的语义值,例如 `@div: 2`,它没有字号、局部坐标,也不能表达 note 自身生成的下八度点。
105
- * 布局需要把这些语义统一转换成可排序、可占高、可绘制的 LayoutDecoration 实例。
106
- *
107
- * 生命周期:
108
- * 1. layout 的 prepareLayoutHost 开始时创建或清空数组;
109
- * 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
110
- * 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
111
- * 4. arrangeBelowDecorations 按 below.order 分配主体下方空间并调用 below.place;
112
- * 5. 最终 paintLayout 在主体 paint 后依次调用 decoration.paint。
113
- *
114
- * 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
115
- * prepare 阶段生成、place 后冻结、paint 后失效
116
- */
117
- decorations: LayoutDecoration[];
118
- /**
119
- * 创建并返回当前事件唯一的 LayoutBox
120
- * 可见子类应在构造函数中调用一次(或自行填写)
121
- */
122
- protected initLayoutBox(): LayoutBox;
123
- /**
124
- * 时间状态 修改&冻结 入口
125
- * 调用时机在时间位置已经确定之后,处理“调性、速度、拍号”等时间信息的固化
126
- */
127
- onTimeState?(state: Record<string, any>): void;
128
- /**
129
- * 根据已经固化的语义生成固有尺寸
130
- * 一般为测量函数自有图形或使用 LayoutPrepareContext.textMeasurer 测量文本,填写 box.w/h/anchor/visualAxis
131
- * 此时 x 和 y 仍然没有最终含义
132
- */
133
- prepareLayout(_ctx: LayoutPrepareContext): void;
134
- /** 横向拓扑建立后调整弹簧参数或注册横向布局 hook */
135
- prepareHorizontal?(_line: HorizontalLineView): void;
136
- /**
137
- * 得到了最终的 box 后被调用(和 prepareLayout 之间隔了 decoration 的布局计算)
138
- * 可在这里发布依赖最终 box.w/box.h 的端口
139
- */
140
- finalizeLayout?(_ctx: LayoutPrepareContext): void;
141
- /**
142
- * 布局器每次改变 x 或 y 后调用
143
- * 普通叶节点不需要处理,具有内部几何的复合节点可用它同步子对象坐标
144
- */
145
- onPlaced?(): void;
146
- /**
147
- * 只执行绘制,不允许在这里重新测量或改变 LayoutBox
148
- */
149
- paint(_painter: Painter): void;
150
- }
151
- /** 可以进入视觉布局的 Temporal,同时满足 decoration 所需的 LayoutHost 协议 */
152
- export type VisualTemporalNode = TemporalNodeBase & LayoutHost;
153
- export declare function isVisualTemporalNode(node: TemporalNodeBase): node is VisualTemporalNode;
@@ -1,92 +1 @@
1
- /**
2
- * 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
3
- *
4
- * 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
5
- * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
6
- */
7
- /** 时间对齐点(小节线);必须是最小值,最先出队 */
8
- export const ANCHOR_KEY = -Infinity;
9
- /** 普通事件的公共组 */
10
- export const DEFAULT_KEY = Infinity;
11
- /**
12
- * 时间线事件
13
- *
14
- * 由 AST 节点在 lowering 阶段产生,负责得到时间、设置布局参数
15
- */
16
- export class TemporalNodeBase {
17
- // 下面这批字段统一由 LoweringContext.initEvent 原地补全
18
- // 横向 layout 要用的三个属性
19
- t; // 事件发生的时间点
20
- T; // 事件的持续时间
21
- track; // 所在的纵向音轨;同轨判断使用引用相等
22
- ast; // 对应的 AST 节点
23
- order; // 事件创建序号,作为id。可用于区分同时发生的父子、排序同一时刻发生的事件
24
- addon; // 其他任意字段,仅在存在已固化的函数语义时创建,比如存储 div 和 dot 的数目
25
- mergeKey; // 时间列合并组
26
- /**
27
- * 被折叠进哪个宿主的盒子(由宿主自己设置)
28
- *
29
- * 折叠成员不进入全局 columns,在时间流里没有独立位置,
30
- * 因此它对外(标签、关系函数端点)一律由宿主代表;嵌套折叠沿链上溯。
31
- */
32
- foldedInto;
33
- /**
34
- * 请求在自己所在时间列之前换几行,默认 0
35
- */
36
- breakBefore = 0;
37
- /** 由 LoweringContext 在列归并后固化的谱面行号 */
38
- layoutLine = 0;
39
- /** 不可见状态事件保持 undefined */
40
- box;
41
- /**
42
- * 横向弹簧布局参数
43
- * 大部分对象使用空对象即可采用布局器默认值
44
- */
45
- springConfig;
46
- /**
47
- * 关系函数可使用的命名局部坐标
48
- * prepareLayout 每次执行时由具体节点和装饰函数重新生成
49
- */
50
- ports;
51
- /**
52
- * 当前一次 layout 已经实例化并完成测量的装饰对象
53
- *
54
- * 起因:addon 只保存 lowering 阶段冻结的语义值,例如 `@div: 2`,它没有字号、局部坐标,也不能表达 note 自身生成的下八度点。
55
- * 布局需要把这些语义统一转换成可排序、可占高、可绘制的 LayoutDecoration 实例。
56
- *
57
- * 生命周期:
58
- * 1. layout 的 prepareLayoutHost 开始时创建或清空数组;
59
- * 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
60
- * 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
61
- * 4. arrangeBelowDecorations 按 below.order 分配主体下方空间并调用 below.place;
62
- * 5. 最终 paintLayout 在主体 paint 后依次调用 decoration.paint。
63
- *
64
- * 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
65
- * prepare 阶段生成、place 后冻结、paint 后失效
66
- */
67
- decorations;
68
- /**
69
- * 创建并返回当前事件唯一的 LayoutBox
70
- * 可见子类应在构造函数中调用一次(或自行填写)
71
- */
72
- initLayoutBox() {
73
- return this.box = {
74
- x: 0, y: 0,
75
- w: 0, h: 0,
76
- anchor: 0, visualAxis: 0,
77
- };
78
- }
79
- /**
80
- * 根据已经固化的语义生成固有尺寸
81
- * 一般为测量函数自有图形或使用 LayoutPrepareContext.textMeasurer 测量文本,填写 box.w/h/anchor/visualAxis
82
- * 此时 x 和 y 仍然没有最终含义
83
- */
84
- prepareLayout(_ctx) { }
85
- /**
86
- * 只执行绘制,不允许在这里重新测量或改变 LayoutBox
87
- */
88
- paint(_painter) { }
89
- }
90
- export function isVisualTemporalNode(node) {
91
- return node.box !== undefined;
92
- }
1
+ export {};
@@ -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 tonicLetter = keySignature[0];
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,10 @@
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;
9
+ /** 生成双引号包裹的 jpFun 源码,仅转义反斜杠和双引号;与 JSON.stringify 不同,保留真实换行和制表符 */
10
+ export declare function quote(value: string): string;
@@ -0,0 +1,24 @@
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
+ }
21
+ /** 生成双引号包裹的 jpFun 源码,仅转义反斜杠和双引号;与 JSON.stringify 不同,保留真实换行和制表符 */
22
+ export function quote(value) {
23
+ return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
24
+ }
@@ -5,6 +5,11 @@ import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, paramType, paramValue }
5
5
  export declare const DEFAULT_FONT_SIZE = 22;
6
6
  export declare const DEFAULT_OCTAVE = 4;
7
7
  export declare class ParserContext {
8
+ /** 系统变量,供 `@set` 读取 */
9
+ static systemVariables: Record<string, {
10
+ type: paramType;
11
+ default: paramValue;
12
+ }>;
8
13
  /** 根解析器为 0,内容参数和大括号子解析器依次加一 */
9
14
  readonly scopeDepth: number;
10
15
  /** 源代码 */
@@ -23,15 +28,15 @@ export declare class ParserContext {
23
28
  deSugarAtomFns: deSugarAtomFunction[];
24
29
  /** 第二轮关系型去糖:得到函数节点。用于依赖 ASTNode 的语法糖 */
25
30
  deSugarRelationFns: deSugarRelationFunction[];
26
- /** .labelable() 返回的承载节点会被加入其中,供标签绑定使用 */
27
- labelableNodes: ASTFunctionNode[];
31
+ /** .labelable() 返回的承载节点;null 是边界标记,标签绑定不能越过它 */
32
+ labelableNodes: (ASTFunctionNode | null)[];
28
33
  nodes: ASTNodeBase[];
29
34
  constructor(ctx: ParserContext | {
30
35
  source: string;
31
36
  diagnostics?: Diagnostic[];
32
- variables?: Map<string, any>;
37
+ variables?: Record<string, any>;
33
38
  functions?: Map<string, ASTFunctionClass>;
34
- labelableNodes?: ASTFunctionNode[];
39
+ labelableNodes?: (ASTFunctionNode | null)[];
35
40
  toConsume?: ASTNodeBase[];
36
41
  commentSpans?: SourceSpan[];
37
42
  });
@@ -40,10 +45,7 @@ export declare class ParserContext {
40
45
  deSugarRelationFunction[]
41
46
  ];
42
47
  pushNode(node: ASTNodeBase): void;
43
- get fontSize(): number;
44
- set fontSize(size: number | string);
45
- get strict(): boolean;
46
- set strict(value: boolean);
48
+ setVariable(name: string, value: paramValue | undefined): void;
47
49
  registerFunctions(functionClasses: ASTFunctionClass[]): void;
48
50
  /**
49
51
  * 扫描 GrammarNode 并构造 AST;不产出 syntax
@@ -85,7 +87,7 @@ export declare class ParserContext {
85
87
  * funcStart 是为了找到该函数之前的label的位置 因为如果content先解析会污染 labelableNodes
86
88
  * 解析成功返回值 解析失败返回null
87
89
  */
88
- parseArgWithType(argStart: number, argEnd: number, type?: paramType, funcStart?: number): paramValue | null;
90
+ parseArgWithType(span: SourceSpan, type?: paramType, funcStart?: number): paramValue | null;
89
91
  length2px(length: LengthValue): number;
90
92
  }
91
93
  export declare function skipSpaces(s: string, i: number, end: number): number;