jpfun 0.3.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 (125) hide show
  1. package/README.md +73 -1
  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/functions/ASTtypes.d.ts +28 -25
  23. package/dist/functions/ASTtypes.js +25 -23
  24. package/dist/functions/adjust/index.js +1 -1
  25. package/dist/functions/arpeggio/index.d.ts +2 -0
  26. package/dist/functions/arpeggio/index.js +180 -0
  27. package/dist/functions/arpeggio/shape.d.ts +7 -0
  28. package/dist/functions/arpeggio/shape.js +122 -0
  29. package/dist/functions/bar/index.js +23 -15
  30. package/dist/functions/beam/auto.d.ts +1 -1
  31. package/dist/functions/beam/auto.js +1 -1
  32. package/dist/functions/beam/layout.d.ts +2 -2
  33. package/dist/functions/beam/layout.js +1 -1
  34. package/dist/functions/box/index.js +2 -2
  35. package/dist/functions/br/index.js +1 -1
  36. package/dist/functions/dash/index.js +7 -6
  37. package/dist/functions/default.js +6 -2
  38. package/dist/functions/dot/index.js +12 -22
  39. package/dist/functions/dyn/index.d.ts +2 -0
  40. package/dist/functions/dyn/index.js +242 -0
  41. package/dist/functions/grace/index.d.ts +1 -1
  42. package/dist/functions/grace/index.js +2 -3
  43. package/dist/functions/head/index.js +149 -145
  44. package/dist/functions/key/index.js +5 -4
  45. package/dist/functions/meter/index.js +6 -5
  46. package/dist/functions/note/index.js +68 -21
  47. package/dist/functions/note/noteNameFSM.js +52 -63
  48. package/dist/functions/page/index.d.ts +2 -0
  49. package/dist/functions/page/index.js +22 -7
  50. package/dist/functions/program/index.d.ts +2 -0
  51. package/dist/functions/program/index.js +30 -0
  52. package/dist/functions/set/index.js +8 -5
  53. package/dist/functions/symbol/index.d.ts +13 -3
  54. package/dist/functions/symbol/index.js +61 -16
  55. package/dist/functions/symbol/shape.d.ts +2 -0
  56. package/dist/functions/symbol/shape.js +14 -10
  57. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  58. package/dist/functions/symbol/symbols/dc.js +82 -0
  59. package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
  60. package/dist/functions/symbol/symbols/ds.js +93 -0
  61. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  62. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  63. package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
  64. package/dist/functions/symbol/symbols/fine.js +12 -0
  65. package/dist/functions/symbol/symbols/mordent.js +44 -31
  66. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  67. package/dist/functions/symbol/symbols/prall.js +42 -0
  68. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  69. package/dist/functions/symbol/symbols/segno.js +82 -0
  70. package/dist/functions/symbol/symbols/tr.js +0 -4
  71. package/dist/functions/tempo/index.js +5 -3
  72. package/dist/functions/temporal.d.ts +138 -0
  73. package/dist/functions/temporal.js +101 -0
  74. package/dist/functions/text/index.js +14 -7
  75. package/dist/functions/tie/index.js +37 -28
  76. package/dist/functions/tuplet/index.js +14 -7
  77. package/dist/functions/up/index.js +23 -21
  78. package/dist/functions/voice/index.js +121 -45
  79. package/dist/functions/volta/index.js +51 -48
  80. package/dist/iife/core.d.ts +1 -0
  81. package/dist/iife/core.js +3 -0
  82. package/dist/iife/from-midi.d.ts +1 -0
  83. package/dist/iife/from-midi.js +15 -0
  84. package/dist/iife/from-musicxml.d.ts +1 -0
  85. package/dist/iife/from-musicxml.js +3 -0
  86. package/dist/iife/full.d.ts +1 -0
  87. package/dist/iife/full.js +3 -0
  88. package/dist/index.d.ts +3 -21
  89. package/dist/index.js +3 -21
  90. package/dist/jpfun.core.min.js +153 -0
  91. package/dist/jpfun.from-midi.min.js +9 -0
  92. package/dist/jpfun.from-musicxml.min.js +12 -0
  93. package/dist/jpfun.min.js +71 -48
  94. package/dist/layout/default.js +2 -3
  95. package/dist/layout/engine.d.ts +2 -1
  96. package/dist/layout/engine.js +71 -26
  97. package/dist/layout/model.d.ts +4 -4
  98. package/dist/layout/model.js +15 -12
  99. package/dist/layout/page.js +5 -4
  100. package/dist/layout/path.js +3 -3
  101. package/dist/layout/types.d.ts +20 -2
  102. package/dist/lowering/loweringContext.d.ts +3 -1
  103. package/dist/lowering/loweringContext.js +28 -14
  104. package/dist/lowering/track.d.ts +4 -8
  105. package/dist/lowering/track.js +6 -1
  106. package/dist/lowering/types.d.ts +5 -128
  107. package/dist/lowering/types.js +1 -98
  108. package/dist/parser/parse-utils/string-utils.d.ts +2 -0
  109. package/dist/parser/parse-utils/string-utils.js +4 -0
  110. package/dist/parser/parserContext.d.ts +7 -8
  111. package/dist/parser/parserContext.js +41 -36
  112. package/dist/pipeline.d.ts +6 -5
  113. package/dist/pipeline.js +4 -5
  114. package/dist/playback/compile.d.ts +1 -1
  115. package/dist/playback/compile.js +15 -2
  116. package/dist/playback/event.d.ts +17 -3
  117. package/dist/playback/event.js +30 -12
  118. package/dist/playback/time.js +1 -1
  119. package/dist/playback/types.d.ts +5 -4
  120. package/dist/render/text.d.ts +6 -1
  121. package/dist/render/text.js +8 -1
  122. package/grammar.md +44 -8
  123. package/package.json +11 -2
  124. package/dist/functions/symbol/symbols/f.js +0 -55
  125. package/dist/functions/symbol/symbols/p.js +0 -61
package/README.md CHANGED
@@ -4,6 +4,8 @@ A functional numbered musical notation typesetting engine for TypeScript.
4
4
 
5
5
  jpFun compiles a compact numbered-notation DSL into a reusable document layout that can be rendered as SVG or Canvas.
6
6
 
7
+ [Documentation](https://madderscientist.github.io/jpFun/)
8
+
7
9
  ## Features
8
10
 
9
11
  - Compact DSL with syntax sugar and function-based extensibility
@@ -50,6 +52,36 @@ For ES modules, jsDelivr bundles the package on the fly:
50
52
 
51
53
  Both URLs resolve to the latest published version. In production, append an exact version (`jpfun@x.y.z`) so that publishing a new release cannot change an existing page.
52
54
 
55
+ The default URL is the complete build. Plain pages can instead load only the capabilities they use; every script extends the same `jpfun` global:
56
+
57
+ ```html
58
+ <!-- Parse, layout, render, and playback without format converters. -->
59
+ <script src="https://unpkg.com/jpfun/dist/jpfun.core.min.js"></script>
60
+
61
+ <!-- MusicXML import works by itself. -->
62
+ <script src="https://unpkg.com/jpfun/dist/jpfun.from-musicxml.min.js"></script>
63
+ <script>
64
+ const document = new DOMParser().parseFromString(musicXmlText, "application/xml");
65
+ const musicXmlSource = jpfun.musicXmlToJpFun(document.documentElement);
66
+ </script>
67
+
68
+ <!-- MIDI JSON import with a fixed measure count works by itself. -->
69
+ <script src="https://unpkg.com/jpfun/dist/jpfun.from-midi.min.js"></script>
70
+ <script>
71
+ const midiSource = jpfun.midiJsonToJpFun(parsedMidiJson, { barsPerLine: 4 });
72
+ </script>
73
+ ```
74
+
75
+ Measured automatic MIDI line breaking uses the layout engine. Load both scripts before calling the converter with an omitted or non-positive `barsPerLine`:
76
+
77
+ ```html
78
+ <script src="https://unpkg.com/jpfun/dist/jpfun.core.min.js"></script>
79
+ <script src="https://unpkg.com/jpfun/dist/jpfun.from-midi.min.js"></script>
80
+ <script>
81
+ const source = jpfun.midiJsonToJpFun(parsedMidiJson);
82
+ </script>
83
+ ```
84
+
53
85
  ## Quick Start
54
86
 
55
87
  ```ts
@@ -139,7 +171,47 @@ const playback = compilePlayback(result.lowering, { maxFlowSteps: 200_000 });
139
171
 
140
172
  The default is 65,536 visited columns. Exceeding it throws instead of returning a partial playback plan.
141
173
 
142
- The plan stores device-independent `note-on`, `note-off`, and `tempo` events. Its track number is the same channel identity used by the visual Track; Web Audio, Web MIDI, and Standard MIDI File adapters consume that identity and the exact `Fraction` timestamps at their boundary.
174
+ The plan stores device-independent `note-on`, `note-off`, `tempo`, `time-signature`, and `program-change` events. Its track number is the same channel identity used by the visual Track; Web Audio, Web MIDI, and Standard MIDI File adapters consume that identity and the exact `Fraction` timestamps at their boundary.
175
+
176
+ ## MIDI JSON Conversion
177
+
178
+ `midiJsonToJpFun` converts the JSON returned by madderscientist's [`midi.js`](https://madderscientist.github.io/noteDigger/lib/midi.js) `JSON()` method into jpFun source. MIDI byte parsing stays in the application boundary; the core converter only consumes structured data and has no runtime dependency on a MIDI parser.
179
+
180
+ ```ts
181
+ import { midiJsonToJpFun } from "jpfun";
182
+
183
+ const source = midiJsonToJpFun(parsedMidiJson, {
184
+ title: "My Score",
185
+ alignRate: 4,
186
+ barsPerLine: 0,
187
+ });
188
+ ```
189
+
190
+ `pitchMode` defaults to `"absolute"`; use `"relative"` for C-based numbered notation. `alignRate` controls adaptive binary quantization. Equal start/end groups become `@up` chords, overlapping lanes become `@stack`, and separate MIDI tracks become named voices. `barsPerLine` defaults to `0`; non-positive values select measured automatic breaks, while a positive value fixes the measures per system. `title` overrides the MIDI header name.
191
+
192
+ With classic scripts, measured automatic breaks require `jpfun.core.min.js`; a positive `barsPerLine` lets `jpfun.from-midi.min.js` run independently.
193
+
194
+ The converter preserves notes, track names, tempo, time signatures, and MIDI programs, and recognizes standard 3:2 quarter-, eighth-, and sixteenth-note triplets. Other tuplets are unsupported. Percussion channel 10 is skipped; velocity, control changes, pitch bends, lyrics, and other MIDI metadata are ignored. Time-signature changes snap to measure boundaries, and continuations that cannot use `-` are emitted as tied notes.
195
+
196
+ ## MusicXML Conversion
197
+
198
+ `musicXmlToJpFun` converts a parsed MusicXML root element into jpFun source. XML parsing stays at the application boundary, so the core package has no XML parser dependency. In a browser, use the native `DOMParser`:
199
+
200
+ ```ts
201
+ import { musicXmlToJpFun } from "jpfun";
202
+
203
+ const xml = await file.text();
204
+ const document = new DOMParser().parseFromString(xml, "application/xml");
205
+ if (document.querySelector("parsererror")) throw new SyntaxError("Invalid MusicXML");
206
+
207
+ const source = musicXmlToJpFun(document.documentElement, {
208
+ barsPerLine: 4,
209
+ });
210
+ ```
211
+
212
+ In Node.js, use any DOM implementation and pass a compatible root element. The converter supports partwise and timewise scores, parts/staves/voices, exact durations, rests, chords, arpeggios, grace notes, ties, tuplets, lyrics, score state, dynamics, repeats/endings, and basic system, page, and title metadata. `pitchMode` defaults to `"absolute"`; `"relative"` uses the active MusicXML key. Generated source uses compact jpFun syntax where unambiguous.
213
+
214
+ Compressed `.mxl` archives are unsupported. Decompress them first or export `.musicxml`. Percussion channel 10 is skipped; pedal, cross-staff arpeggio grouping, detailed engraving coordinates, and other control information are ignored.
143
215
 
144
216
  ## Syntax Example
145
217
 
@@ -0,0 +1,50 @@
1
+ /**
2
+ * MIDI JSON -> jpFun 的转换流水线:
3
+ * 1. 校验输入,选择足以表达原时值的二进制精度;
4
+ * 2. 在原始 tick 上识别 3:2 三连音,再把其余音符量化到二进制网格;
5
+ * 3. 合并同起止音为和弦,把重叠内容拆到不同 lane;
6
+ * 4. 生成单行源码,以真实自然排版宽度选择换行点;
7
+ * 5. 重新生成最终源码,并用 tie 表达跨小节或不足一拍的持续音。
8
+ *
9
+ * MIDI 字节解析不属于 core;这里接收的是 midi.js 已解析好的 JSON。
10
+ */
11
+ import type { MidiJson } from "./json.js";
12
+ import { type PitchMode } from "../source.js";
13
+ export type * from "./json.js";
14
+ export interface MidiToJpFunOptions {
15
+ pitchMode?: PitchMode;
16
+ alignRate?: number;
17
+ barsPerLine?: number;
18
+ title?: string;
19
+ }
20
+ export interface MidiLayoutCapability {
21
+ compileScore: (source: string) => {
22
+ layout: {
23
+ objects: {
24
+ T: {
25
+ isZero(): boolean;
26
+ };
27
+ t: {
28
+ toNumber(): number;
29
+ };
30
+ layoutLine: number;
31
+ mergeKey: number;
32
+ box: {
33
+ x: number;
34
+ w: number;
35
+ };
36
+ }[];
37
+ };
38
+ };
39
+ anchorKey: number;
40
+ page: {
41
+ width: number;
42
+ marginLeft: number;
43
+ marginRight: number;
44
+ };
45
+ }
46
+ /**
47
+ * 公开转换入口。options 只控制表示方式;量化后的 track/note 仍保留原始字段,
48
+ * 方便后续加入力度、乐器和踏板时直接扩展现有时间线。
49
+ */
50
+ export declare function convertMidiJsonToJpFun(input: MidiJson, options?: MidiToJpFunOptions, layoutCapability?: MidiLayoutCapability): string;