@nodelyra/not-angka 0.0.0-stage → 0.1.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 (251) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -2
  3. package/THIRD_PARTY.md +24 -0
  4. package/dist/esm/cursor/activeChord.d.ts +12 -0
  5. package/dist/esm/cursor/activeChord.js +36 -0
  6. package/dist/esm/cursor/beatDot.d.ts +30 -0
  7. package/dist/esm/cursor/beatDot.js +49 -0
  8. package/dist/esm/cursor/drawnTop.d.ts +9 -0
  9. package/dist/esm/cursor/drawnTop.js +16 -0
  10. package/dist/esm/cursor/pageBoxes.d.ts +28 -0
  11. package/dist/esm/cursor/pageBoxes.js +75 -0
  12. package/dist/esm/cursor/playbackCursor.d.ts +25 -0
  13. package/dist/esm/cursor/playbackCursor.js +42 -0
  14. package/dist/esm/cursor/playingStyle.d.ts +16 -0
  15. package/dist/esm/cursor/playingStyle.js +34 -0
  16. package/dist/esm/cursor/playingTones.d.ts +8 -0
  17. package/dist/esm/cursor/playingTones.js +16 -0
  18. package/dist/esm/cursor/target.d.ts +47 -0
  19. package/dist/esm/cursor/target.js +63 -0
  20. package/dist/esm/export/pageText.d.ts +8 -0
  21. package/dist/esm/export/pageText.js +80 -0
  22. package/dist/esm/export/svgExport.d.ts +46 -0
  23. package/dist/esm/export/svgExport.js +88 -0
  24. package/dist/esm/geometry/metrics.d.ts +42 -0
  25. package/dist/esm/geometry/metrics.js +52 -0
  26. package/dist/esm/geometry/pageSetup.d.ts +61 -0
  27. package/dist/esm/geometry/pageSetup.js +85 -0
  28. package/dist/esm/geometry/paper.d.ts +16 -0
  29. package/dist/esm/geometry/paper.js +16 -0
  30. package/dist/esm/index.d.ts +35 -0
  31. package/dist/esm/index.js +23 -0
  32. package/dist/esm/layout/LayoutEngine.d.ts +64 -0
  33. package/dist/esm/layout/LayoutEngine.js +92 -0
  34. package/dist/esm/layout/arcs.d.ts +20 -0
  35. package/dist/esm/layout/arcs.js +53 -0
  36. package/dist/esm/layout/barlineShapes.d.ts +52 -0
  37. package/dist/esm/layout/barlineShapes.js +71 -0
  38. package/dist/esm/layout/directions.d.ts +48 -0
  39. package/dist/esm/layout/directions.js +122 -0
  40. package/dist/esm/layout/elementBoxes.d.ts +17 -0
  41. package/dist/esm/layout/elementBoxes.js +72 -0
  42. package/dist/esm/layout/floating.d.ts +29 -0
  43. package/dist/esm/layout/floating.js +39 -0
  44. package/dist/esm/layout/keyMarks.d.ts +26 -0
  45. package/dist/esm/layout/keyMarks.js +30 -0
  46. package/dist/esm/layout/lyricAlign.d.ts +6 -0
  47. package/dist/esm/layout/lyricAlign.js +15 -0
  48. package/dist/esm/layout/lyricWords.d.ts +13 -0
  49. package/dist/esm/layout/lyricWords.js +27 -0
  50. package/dist/esm/layout/lyricsLayout.d.ts +55 -0
  51. package/dist/esm/layout/lyricsLayout.js +122 -0
  52. package/dist/esm/layout/markings.d.ts +97 -0
  53. package/dist/esm/layout/markings.js +150 -0
  54. package/dist/esm/layout/measurePlan.d.ts +74 -0
  55. package/dist/esm/layout/measurePlan.js +232 -0
  56. package/dist/esm/layout/measureSegments.d.ts +24 -0
  57. package/dist/esm/layout/measureSegments.js +64 -0
  58. package/dist/esm/layout/notationMetrics.d.ts +110 -0
  59. package/dist/esm/layout/notationMetrics.js +122 -0
  60. package/dist/esm/layout/systemBreaking.d.ts +59 -0
  61. package/dist/esm/layout/systemBreaking.js +137 -0
  62. package/dist/esm/layout/systemLayout.d.ts +138 -0
  63. package/dist/esm/layout/systemLayout.js +266 -0
  64. package/dist/esm/layout/tupletSpacing.d.ts +9 -0
  65. package/dist/esm/layout/tupletSpacing.js +33 -0
  66. package/dist/esm/layout/tuplets.d.ts +30 -0
  67. package/dist/esm/layout/tuplets.js +39 -0
  68. package/dist/esm/model/hiddenParts.d.ts +23 -0
  69. package/dist/esm/model/hiddenParts.js +39 -0
  70. package/dist/esm/model/locate.d.ts +18 -0
  71. package/dist/esm/model/locate.js +31 -0
  72. package/dist/esm/model/lowVoices.d.ts +9 -0
  73. package/dist/esm/model/lowVoices.js +20 -0
  74. package/dist/esm/model/partColors.d.ts +13 -0
  75. package/dist/esm/model/partColors.js +10 -0
  76. package/dist/esm/model/partSpan.d.ts +25 -0
  77. package/dist/esm/model/partSpan.js +49 -0
  78. package/dist/esm/model/rowPresence.d.ts +14 -0
  79. package/dist/esm/model/rowPresence.js +19 -0
  80. package/dist/esm/model/sharedLyrics.d.ts +23 -0
  81. package/dist/esm/model/sharedLyrics.js +68 -0
  82. package/dist/esm/model/textBars.d.ts +12 -0
  83. package/dist/esm/model/textBars.js +21 -0
  84. package/dist/esm/model/tickRanges.d.ts +22 -0
  85. package/dist/esm/model/tickRanges.js +34 -0
  86. package/dist/esm/model/unisons.d.ts +69 -0
  87. package/dist/esm/model/unisons.js +181 -0
  88. package/dist/esm/notation/chords.d.ts +12 -0
  89. package/dist/esm/notation/chords.js +29 -0
  90. package/dist/esm/notation/elision.d.ts +24 -0
  91. package/dist/esm/notation/elision.js +47 -0
  92. package/dist/esm/notation/keys.d.ts +50 -0
  93. package/dist/esm/notation/keys.js +59 -0
  94. package/dist/esm/notation/lyricAlign.d.ts +7 -0
  95. package/dist/esm/notation/lyricAlign.js +15 -0
  96. package/dist/esm/notation/navigation.d.ts +3 -0
  97. package/dist/esm/notation/navigation.js +5 -0
  98. package/dist/esm/notation/pitch.d.ts +39 -0
  99. package/dist/esm/notation/pitch.js +74 -0
  100. package/dist/esm/notation/pitchName.d.ts +2 -0
  101. package/dist/esm/notation/pitchName.js +3 -0
  102. package/dist/esm/notation/profile.d.ts +35 -0
  103. package/dist/esm/notation/profile.js +16 -0
  104. package/dist/esm/notation/rhythm.d.ts +59 -0
  105. package/dist/esm/notation/rhythm.js +116 -0
  106. package/dist/esm/notation/time.d.ts +98 -0
  107. package/dist/esm/notation/time.js +85 -0
  108. package/dist/esm/performance/compilePerformance.d.ts +7 -0
  109. package/dist/esm/performance/compilePerformance.js +8 -0
  110. package/dist/esm/performance/dynamics.d.ts +6 -0
  111. package/dist/esm/performance/dynamics.js +78 -0
  112. package/dist/esm/performance/model.d.ts +56 -0
  113. package/dist/esm/performance/model.js +1 -0
  114. package/dist/esm/performance/playOrder.d.ts +16 -0
  115. package/dist/esm/performance/playOrder.js +64 -0
  116. package/dist/esm/performance/readScore.d.ts +4 -0
  117. package/dist/esm/performance/readScore.js +58 -0
  118. package/dist/esm/performance/timeline.d.ts +10 -0
  119. package/dist/esm/performance/timeline.js +144 -0
  120. package/dist/esm/playback/audioOutput.d.ts +46 -0
  121. package/dist/esm/playback/audioOutput.js +106 -0
  122. package/dist/esm/playback/clock.d.ts +16 -0
  123. package/dist/esm/playback/clock.js +44 -0
  124. package/dist/esm/playback/createPlayerStore.d.ts +79 -0
  125. package/dist/esm/playback/createPlayerStore.js +135 -0
  126. package/dist/esm/playback/cues.d.ts +34 -0
  127. package/dist/esm/playback/cues.js +52 -0
  128. package/dist/esm/playback/errors.d.ts +6 -0
  129. package/dist/esm/playback/errors.js +6 -0
  130. package/dist/esm/playback/metronome.d.ts +13 -0
  131. package/dist/esm/playback/metronome.js +15 -0
  132. package/dist/esm/playback/mix.d.ts +9 -0
  133. package/dist/esm/playback/mix.js +8 -0
  134. package/dist/esm/playback/pass.d.ts +27 -0
  135. package/dist/esm/playback/pass.js +36 -0
  136. package/dist/esm/playback/schedule.d.ts +7 -0
  137. package/dist/esm/playback/schedule.js +12 -0
  138. package/dist/esm/playback/session.d.ts +46 -0
  139. package/dist/esm/playback/session.js +107 -0
  140. package/dist/esm/playback/source.d.ts +6 -0
  141. package/dist/esm/playback/source.js +1 -0
  142. package/dist/esm/playback/start.d.ts +28 -0
  143. package/dist/esm/playback/start.js +38 -0
  144. package/dist/esm/playback/timing.d.ts +14 -0
  145. package/dist/esm/playback/timing.js +11 -0
  146. package/dist/esm/playback/types.d.ts +60 -0
  147. package/dist/esm/playback/types.js +1 -0
  148. package/dist/esm/playback/volume.d.ts +12 -0
  149. package/dist/esm/playback/volume.js +8 -0
  150. package/dist/esm/player/ControlBar.d.ts +22 -0
  151. package/dist/esm/player/ControlBar.js +77 -0
  152. package/dist/esm/player/CursorPainter.d.ts +17 -0
  153. package/dist/esm/player/CursorPainter.js +53 -0
  154. package/dist/esm/player/InfoLine.d.ts +6 -0
  155. package/dist/esm/player/InfoLine.js +23 -0
  156. package/dist/esm/player/PageView.d.ts +10 -0
  157. package/dist/esm/player/PageView.js +16 -0
  158. package/dist/esm/player/actions.d.ts +24 -0
  159. package/dist/esm/player/actions.js +26 -0
  160. package/dist/esm/player/balanceSystems.d.ts +8 -0
  161. package/dist/esm/player/balanceSystems.js +18 -0
  162. package/dist/esm/player/createExcerptPlayer.d.ts +33 -0
  163. package/dist/esm/player/createExcerptPlayer.js +169 -0
  164. package/dist/esm/player/createScorePlayer.d.ts +24 -0
  165. package/dist/esm/player/createScorePlayer.js +118 -0
  166. package/dist/esm/player/dom.d.ts +6 -0
  167. package/dist/esm/player/dom.js +15 -0
  168. package/dist/esm/player/embeddedFonts.d.ts +1 -0
  169. package/dist/esm/player/embeddedFonts.js +3 -0
  170. package/dist/esm/player/exclusivePlayback.d.ts +10 -0
  171. package/dist/esm/player/exclusivePlayback.js +13 -0
  172. package/dist/esm/player/followCursor.d.ts +5 -0
  173. package/dist/esm/player/followCursor.js +16 -0
  174. package/dist/esm/player/icons.d.ts +7 -0
  175. package/dist/esm/player/icons.js +9 -0
  176. package/dist/esm/player/injectFonts.d.ts +8 -0
  177. package/dist/esm/player/injectFonts.js +19 -0
  178. package/dist/esm/player/loadExcerpt.d.ts +21 -0
  179. package/dist/esm/player/loadExcerpt.js +123 -0
  180. package/dist/esm/player/loadScore.d.ts +12 -0
  181. package/dist/esm/player/loadScore.js +12 -0
  182. package/dist/esm/player/mountExcerpts.d.ts +30 -0
  183. package/dist/esm/player/mountExcerpts.js +109 -0
  184. package/dist/esm/player/playhead.d.ts +12 -0
  185. package/dist/esm/player/playhead.js +14 -0
  186. package/dist/esm/player/shadowStyles.d.ts +5 -0
  187. package/dist/esm/player/shadowStyles.js +23 -0
  188. package/dist/esm/player/staticSvg.d.ts +7 -0
  189. package/dist/esm/player/staticSvg.js +10 -0
  190. package/dist/esm/player/styles.d.ts +28 -0
  191. package/dist/esm/player/styles.js +98 -0
  192. package/dist/esm/player/theme.d.ts +8 -0
  193. package/dist/esm/player/theme.js +13 -0
  194. package/dist/esm/player/types.d.ts +76 -0
  195. package/dist/esm/player/types.js +12 -0
  196. package/dist/esm/rendering/SystemRenderer.d.ts +6 -0
  197. package/dist/esm/rendering/SystemRenderer.js +58 -0
  198. package/dist/esm/rendering/accidentalRenderer.d.ts +4 -0
  199. package/dist/esm/rendering/accidentalRenderer.js +41 -0
  200. package/dist/esm/rendering/arcRenderer.d.ts +9 -0
  201. package/dist/esm/rendering/arcRenderer.js +32 -0
  202. package/dist/esm/rendering/barlineRenderer.d.ts +8 -0
  203. package/dist/esm/rendering/barlineRenderer.js +59 -0
  204. package/dist/esm/rendering/directionRenderer.d.ts +4 -0
  205. package/dist/esm/rendering/directionRenderer.js +50 -0
  206. package/dist/esm/rendering/durationLineRenderer.d.ts +9 -0
  207. package/dist/esm/rendering/durationLineRenderer.js +27 -0
  208. package/dist/esm/rendering/dynamicGlyphs.d.ts +25 -0
  209. package/dist/esm/rendering/dynamicGlyphs.js +56 -0
  210. package/dist/esm/rendering/frameRenderer.d.ts +17 -0
  211. package/dist/esm/rendering/frameRenderer.js +252 -0
  212. package/dist/esm/rendering/keyMarkRenderer.d.ts +8 -0
  213. package/dist/esm/rendering/keyMarkRenderer.js +57 -0
  214. package/dist/esm/rendering/lyricsRenderer.d.ts +4 -0
  215. package/dist/esm/rendering/lyricsRenderer.js +44 -0
  216. package/dist/esm/rendering/primitives.d.ts +64 -0
  217. package/dist/esm/rendering/primitives.js +1 -0
  218. package/dist/esm/rendering/sliceRenderer.d.ts +19 -0
  219. package/dist/esm/rendering/sliceRenderer.js +79 -0
  220. package/dist/esm/rendering/textBarRenderer.d.ts +8 -0
  221. package/dist/esm/rendering/textBarRenderer.js +28 -0
  222. package/dist/esm/rendering/tupletRenderer.d.ts +7 -0
  223. package/dist/esm/rendering/tupletRenderer.js +42 -0
  224. package/dist/esm/schema/normalize.d.ts +14 -0
  225. package/dist/esm/schema/normalize.js +123 -0
  226. package/dist/esm/schema/pageMeta.d.ts +11 -0
  227. package/dist/esm/schema/pageMeta.js +1 -0
  228. package/dist/esm/schema/scoreJson.d.ts +59 -0
  229. package/dist/esm/schema/scoreJson.js +1 -0
  230. package/dist/esm/schema/scoreView.d.ts +16 -0
  231. package/dist/esm/schema/scoreView.js +24 -0
  232. package/dist/esm/schema/types.d.ts +339 -0
  233. package/dist/esm/schema/types.js +1 -0
  234. package/dist/esm/text/creditLines.d.ts +12 -0
  235. package/dist/esm/text/creditLines.js +11 -0
  236. package/dist/esm/text/fonts.d.ts +50 -0
  237. package/dist/esm/text/fonts.js +98 -0
  238. package/dist/esm/text/headerFooter.d.ts +13 -0
  239. package/dist/esm/text/headerFooter.js +21 -0
  240. package/dist/esm/text/textLines.d.ts +7 -0
  241. package/dist/esm/text/textLines.js +13 -0
  242. package/dist/index.cjs +65 -0
  243. package/dist/index.cjs.map +1 -0
  244. package/dist/nodelyra-player.global.js +65 -0
  245. package/dist/nodelyra-player.global.js.map +1 -0
  246. package/dist/nodelyra-player.lite.global.js +63 -0
  247. package/dist/nodelyra-player.lite.global.js.map +1 -0
  248. package/examples/blog-excerpt.html +54 -0
  249. package/examples/twinkle.html +23 -0
  250. package/examples/twinkle.json +91 -0
  251. package/package.json +70 -4
@@ -0,0 +1,59 @@
1
+ import type { ScoreEvent } from "../schema/types.js";
2
+ import { type Meter } from "./time.js";
3
+ /**
4
+ * Musical duration -> the written symbols of numbered notation (PRD FR-029..FR-034).
5
+ *
6
+ * A measure is read beat by beat. Inside a beat, each event is cut into binary-aligned pieces
7
+ * (a half beat, a quarter beat, ...). The first piece of an event is its head (a digit, or the
8
+ * rest glyph); every later piece is a continuation (a dot in Not Angka). A piece shorter than a
9
+ * beat carries duration lines: one for a half beat, two for a quarter, and so on.
10
+ *
11
+ * dotted eighth + sixteenth in one beat: 3 . 2 lines 1 1 1 / 0 2 2 (as in the reference score)
12
+ * a two-beat note in 2/4: 1 . no lines
13
+ *
14
+ * This is pure: the layout positions these pieces, the renderers draw them.
15
+ */
16
+ export type Slice = {
17
+ eventId: string;
18
+ /** Index of the event in its measure. */
19
+ eventIndex: number;
20
+ type: ScoreEvent["type"];
21
+ /** True on the event's first piece (drawn as its digit or rest), false on a continuation. */
22
+ head: boolean;
23
+ /** Ticks from the start of the measure. */
24
+ onset: number;
25
+ length: number;
26
+ /**
27
+ * The beamed group of the bar this piece is in (0-based: a beat, or three eighths in 6/8;
28
+ * counted in the meter, so a pickup bar's pieces start past 0; may pass the meter's last group
29
+ * in an overfull measure).
30
+ */
31
+ beat: number;
32
+ /** Duration lines over this piece: 0 for a beat or more, 1 for half a beat, 2 for a quarter (in a compound meter one more: every group of three eighths has a line). */
33
+ lines: number;
34
+ };
35
+ /**
36
+ * Every written piece of a measure, in time order. `phase` is how far into its bar the measure
37
+ * starts (a pickup bar's, see `BarFrame`); onsets still count from the measure's start.
38
+ */
39
+ export declare function sliceMeasure(events: readonly ScoreEvent[], meter: Meter, phase?: number): Slice[];
40
+ /** A run of slices that share duration lines: [first, last] indexes into the measure's slices. */
41
+ export type BeamGroup = {
42
+ first: number;
43
+ last: number;
44
+ };
45
+ /**
46
+ * Which pieces share their duration lines. By default the pieces of one beat that have lines
47
+ * form a group, and so do the notes of one tuplet (even across a beat line); an event's
48
+ * `beam: "break"` starts a new group at its head, `beam: "join"` joins its head to the group
49
+ * before it (across a beat boundary).
50
+ */
51
+ export declare function beamGroups(slices: readonly Slice[], events: readonly ScoreEvent[]): BeamGroup[];
52
+ /** A duration line: drawn over slices [first, last] at `level` (1 = nearest the digits). */
53
+ export type DurationLine = {
54
+ first: number;
55
+ last: number;
56
+ level: number;
57
+ };
58
+ /** The duration lines of one group: at each level, every run of pieces with that many lines. */
59
+ export declare function durationLines(slices: readonly Slice[], group: BeamGroup): DurationLine[];
@@ -0,0 +1,116 @@
1
+ import { TICK_STEP, writtenDuration } from "./time.js";
2
+ /** The largest binary piece of the beat that starts at `offset` and fits in `remaining`. */
3
+ function pieceLength(beatTicks, offset, remaining) {
4
+ let piece = beatTicks;
5
+ while (piece / 2 >= TICK_STEP &&
6
+ Number.isInteger(piece / 2) &&
7
+ (offset % piece !== 0 || piece > remaining))
8
+ piece /= 2;
9
+ // Lengths that are not binary (tuplets, odd ticks) keep their remainder in one last piece.
10
+ return offset % piece === 0 && piece <= remaining ? piece : remaining;
11
+ }
12
+ const linesFor = (unitTicks, length) => length >= unitTicks ? 0 : Math.max(1, Math.round(Math.log2(unitTicks / length)));
13
+ /**
14
+ * The duration lines of a piece `length` ticks long: the notes shorter than a beat as in any meter,
15
+ * plus the one line every group has in a compound meter (an eighth in 6/8 has one, a sixteenth two,
16
+ * and a dotted quarter, which is a whole group, one).
17
+ */
18
+ const linesOfPiece = (meter, length) => linesFor(meter.beatTicks, length) + (meter.groupTicks > meter.beatTicks ? 1 : 0);
19
+ /**
20
+ * A tuplet note is one piece, however it falls across beats: it is drawn as its written value
21
+ * (a triplet eighth with an eighth's line), never cut into continuation dots.
22
+ */
23
+ function tupletSlice(event, eventIndex, onset, meter, phase) {
24
+ return {
25
+ eventId: event.id,
26
+ eventIndex,
27
+ type: event.type,
28
+ head: true,
29
+ onset,
30
+ length: event.duration,
31
+ beat: Math.floor((onset + phase) / meter.groupTicks),
32
+ lines: linesOfPiece(meter, writtenDuration(event)),
33
+ };
34
+ }
35
+ /** Cut one event (starting at `onset`, in a bar that starts `phase` ticks in) into its written pieces. */
36
+ function sliceEvent(event, eventIndex, onset, meter, phase) {
37
+ const slices = [];
38
+ const end = onset + event.duration;
39
+ let at = onset;
40
+ while (at < end) {
41
+ // Beats are counted in the bar's own time, so a pickup's notes group by the real beats.
42
+ const beatStart = Math.floor((at + phase) / meter.beatTicks) * meter.beatTicks - phase;
43
+ const inBeat = Math.min(end, beatStart + meter.beatTicks) - at;
44
+ const length = pieceLength(meter.beatTicks, at - beatStart, inBeat);
45
+ slices.push({
46
+ eventId: event.id,
47
+ eventIndex,
48
+ type: event.type,
49
+ head: at === onset,
50
+ onset: at,
51
+ length,
52
+ beat: Math.floor((at + phase) / meter.groupTicks),
53
+ lines: linesOfPiece(meter, length),
54
+ });
55
+ at += length;
56
+ }
57
+ return slices;
58
+ }
59
+ /**
60
+ * Every written piece of a measure, in time order. `phase` is how far into its bar the measure
61
+ * starts (a pickup bar's, see `BarFrame`); onsets still count from the measure's start.
62
+ */
63
+ export function sliceMeasure(events, meter, phase = 0) {
64
+ const slices = [];
65
+ let onset = 0;
66
+ events.forEach((event, index) => {
67
+ slices.push(...(event.tuplet ? [tupletSlice(event, index, onset, meter, phase)] : sliceEvent(event, index, onset, meter, phase)));
68
+ onset += event.duration;
69
+ });
70
+ return slices;
71
+ }
72
+ /**
73
+ * Which pieces share their duration lines. By default the pieces of one beat that have lines
74
+ * form a group, and so do the notes of one tuplet (even across a beat line); an event's
75
+ * `beam: "break"` starts a new group at its head, `beam: "join"` joins its head to the group
76
+ * before it (across a beat boundary).
77
+ */
78
+ export function beamGroups(slices, events) {
79
+ const groups = [];
80
+ slices.forEach((slice, index) => {
81
+ if (slice.lines === 0)
82
+ return;
83
+ const previous = groups.at(-1);
84
+ const hint = slice.head ? events[slice.eventIndex]?.beam : null;
85
+ const adjacent = previous !== undefined && previous.last === index - 1;
86
+ const sameBeat = adjacent && slices[index - 1].beat === slice.beat;
87
+ const tuplet = events[slice.eventIndex]?.tuplet?.id;
88
+ const sameTuplet = adjacent && tuplet !== undefined && events[slices[index - 1].eventIndex]?.tuplet?.id === tuplet;
89
+ const extend = adjacent && hint !== "break" && (sameBeat || sameTuplet || hint === "join");
90
+ if (extend)
91
+ previous.last = index;
92
+ else
93
+ groups.push({ first: index, last: index });
94
+ });
95
+ return groups;
96
+ }
97
+ /** The duration lines of one group: at each level, every run of pieces with that many lines. */
98
+ export function durationLines(slices, group) {
99
+ const result = [];
100
+ let deepest = 0;
101
+ for (let i = group.first; i <= group.last; i++)
102
+ deepest = Math.max(deepest, slices[i].lines);
103
+ for (let level = 1; level <= deepest; level++) {
104
+ let start = null;
105
+ for (let i = group.first; i <= group.last + 1; i++) {
106
+ const reaches = i <= group.last && slices[i].lines >= level;
107
+ if (reaches && start === null)
108
+ start = i;
109
+ if (!reaches && start !== null) {
110
+ result.push({ first: start, last: i - 1, level });
111
+ start = null;
112
+ }
113
+ }
114
+ }
115
+ return result;
116
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Musical time: integer ticks, never floats or seconds. Same resolution as the backend
3
+ * (`apps/scores/timing.py`), so a duration means the same everywhere.
4
+ */
5
+ export declare const TICKS_PER_QUARTER = 480;
6
+ /** The shortest duration a score may hold (a 64th note). */
7
+ export declare const TICK_STEP = 30;
8
+ export declare const MAX_DURATION_TICKS: number;
9
+ /** The finer grid tuplet notes sound on (a triplet eighth is 160 ticks); same as the backend. */
10
+ export declare const TUPLET_TICK_STEP = 10;
11
+ /** The value an event is written as: a tuplet note's sounding ticks times `of / in` (160 -> 240). */
12
+ export declare const writtenDuration: ({ duration, tuplet }: {
13
+ duration: number;
14
+ tuplet?: {
15
+ of: number;
16
+ in: number;
17
+ } | null;
18
+ }) => number;
19
+ /**
20
+ * A time signature resolved to ticks: `beats` beats of `beatTicks` each (an eighth in 6/8: the
21
+ * editor counts, and a pickup is made of, the time signature's own beats).
22
+ *
23
+ * How the notes are *written* follows the pulse instead: `groupTicks` is the span whose notes
24
+ * share a line and are spaced as one (a dotted quarter in 6/8: three eighths). In a compound meter
25
+ * (`groupTicks` > `beatTicks`) every group is drawn under one line, whatever it holds (a dotted
26
+ * quarter, a quarter and an eighth, three eighths), and shorter notes add lines to it. In a
27
+ * simple meter the group is the beat and only notes shorter than a beat have lines.
28
+ */
29
+ export type Meter = {
30
+ beats: number;
31
+ beatUnit: number;
32
+ beatTicks: number;
33
+ groupTicks: number;
34
+ measureTicks: number;
35
+ };
36
+ /** "2/4" -> 2 beats of a quarter. Anything unreadable falls back to 4/4. */
37
+ export declare function parseMeter(time: string): Meter;
38
+ /** A meter written as a time signature, "3/4": the same for every spelling of it. */
39
+ export declare const meterName: ({ beats, beatUnit }: Pick<Meter, "beats" | "beatUnit">) => string;
40
+ export declare const isValidTime: (time: string) => boolean;
41
+ /** The meter in force for every measure: a measure's own `time` holds until the next change. */
42
+ export declare function metersOf(measures: readonly {
43
+ time: string | null;
44
+ }[], scoreTime: string): Meter[];
45
+ /**
46
+ * A bar as the score frames it: its meter, the ticks it should hold, and how far into the meter
47
+ * its music starts. Only a pickup bar starts late (its first note is not beat one).
48
+ */
49
+ export type BarFrame = {
50
+ meter: Meter;
51
+ length: number;
52
+ phase: number;
53
+ };
54
+ /** Whether the music can start `pickup` ticks before the first downbeat: whole beats, under a bar. */
55
+ export declare const isValidPickup: (pickup: number, meter: Meter) => boolean;
56
+ /** `pickup` under the time signature `time`: kept when it still fits a bar, else 0. */
57
+ export declare const pickupInTime: (pickup: number, time: string) => number;
58
+ /** The pickup for "starts on beat `beat`" (1 = on the downbeat, no pickup). */
59
+ export declare const pickupForStartBeat: (meter: Meter, beat: number) => number;
60
+ /** The beat a score with `pickup` starts on (1 when it has none, or one that does not fit). */
61
+ export declare const startBeatOf: (meter: Meter, pickup: number) => number;
62
+ type FramedMusic = {
63
+ measures: readonly {
64
+ time: string | null;
65
+ text_bar?: {
66
+ bars: number;
67
+ } | null;
68
+ }[];
69
+ parts: readonly {
70
+ measures: readonly {
71
+ events: readonly {
72
+ duration: number;
73
+ }[];
74
+ }[];
75
+ }[];
76
+ };
77
+ /**
78
+ * Every bar's frame. With a pickup (that fits the first bar), the first bar holds just the pickup
79
+ * and starts that far before its bar's end; the final bar holds the rest of a bar, unless its
80
+ * music already runs past that (a score still being written), when it is a full bar again.
81
+ */
82
+ export declare function framesOf(music: FramedMusic, scoreTime: string, pickup?: number): BarFrame[];
83
+ /** The beamed group (a beat in a simple meter, three eighths in 6/8) that tick `at` of a bar falls in. */
84
+ export declare const groupOf: ({ meter, phase }: Pick<BarFrame, "meter" | "phase">, at: number) => number;
85
+ /** Where each beat of a frame starts, in ticks from the start of the bar. */
86
+ export declare const beatOnsets: ({ meter, length, phase }: BarFrame) => number[];
87
+ /**
88
+ * The start of the beat that tick `at` of a bar falls in (a pickup bar's beats count from the
89
+ * downbeat it leads to). Whatever is written there, a digit or a continuation dot, is that beat.
90
+ */
91
+ export declare const beatStartOf: ({ meter, phase }: Pick<BarFrame, "meter" | "phase">, at: number) => number;
92
+ /** Snap a duration to the tick grid and the allowed range. */
93
+ export declare const clampDuration: (ticks: number) => number;
94
+ /** Total length of a list of events. */
95
+ export declare const totalTicks: (events: readonly {
96
+ duration: number;
97
+ }[]) => number;
98
+ export {};
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Musical time: integer ticks, never floats or seconds. Same resolution as the backend
3
+ * (`apps/scores/timing.py`), so a duration means the same everywhere.
4
+ */
5
+ export const TICKS_PER_QUARTER = 480;
6
+ /** The shortest duration a score may hold (a 64th note). */
7
+ export const TICK_STEP = 30;
8
+ export const MAX_DURATION_TICKS = 16 * TICKS_PER_QUARTER;
9
+ /** The finer grid tuplet notes sound on (a triplet eighth is 160 ticks); same as the backend. */
10
+ export const TUPLET_TICK_STEP = 10;
11
+ /** The value an event is written as: a tuplet note's sounding ticks times `of / in` (160 -> 240). */
12
+ export const writtenDuration = ({ duration, tuplet }) => tuplet ? (duration * tuplet.of) / tuplet.in : duration;
13
+ const TIME_PATTERN = /^(\d{1,2})\/(1|2|4|8|16)$/;
14
+ /** 6/8, 9/8 and 12/8: eighths felt in threes. (3/8 is one bar of three eighths: left simple.) */
15
+ const isCompound = (beats, beatUnit) => beatUnit === 8 && beats >= 6 && beats % 3 === 0;
16
+ /** "2/4" -> 2 beats of a quarter. Anything unreadable falls back to 4/4. */
17
+ export function parseMeter(time) {
18
+ const match = TIME_PATTERN.exec(time.trim());
19
+ const beats = match ? Math.max(1, Number(match[1])) : 4;
20
+ const beatUnit = match ? Number(match[2]) : 4;
21
+ const beatTicks = (TICKS_PER_QUARTER * 4) / beatUnit;
22
+ const compound = isCompound(beats, beatUnit);
23
+ return {
24
+ beats,
25
+ beatUnit,
26
+ beatTicks,
27
+ groupTicks: compound ? beatTicks * 3 : beatTicks,
28
+ measureTicks: beats * beatTicks,
29
+ };
30
+ }
31
+ /** A meter written as a time signature, "3/4": the same for every spelling of it. */
32
+ export const meterName = ({ beats, beatUnit }) => `${beats}/${beatUnit}`;
33
+ export const isValidTime = (time) => TIME_PATTERN.test(time.trim());
34
+ /** The meter in force for every measure: a measure's own `time` holds until the next change. */
35
+ export function metersOf(measures, scoreTime) {
36
+ let current = parseMeter(scoreTime);
37
+ return measures.map((m) => {
38
+ if (m.time)
39
+ current = parseMeter(m.time);
40
+ return current;
41
+ });
42
+ }
43
+ /** Whether the music can start `pickup` ticks before the first downbeat: whole beats, under a bar. */
44
+ export const isValidPickup = (pickup, meter) => pickup === 0 || (pickup > 0 && pickup < meter.measureTicks && pickup % meter.beatTicks === 0);
45
+ /** `pickup` under the time signature `time`: kept when it still fits a bar, else 0. */
46
+ export const pickupInTime = (pickup, time) => isValidPickup(pickup, parseMeter(time)) ? pickup : 0;
47
+ /** The pickup for "starts on beat `beat`" (1 = on the downbeat, no pickup). */
48
+ export const pickupForStartBeat = (meter, beat) => beat <= 1 || beat > meter.beats ? 0 : (meter.beats - beat + 1) * meter.beatTicks;
49
+ /** The beat a score with `pickup` starts on (1 when it has none, or one that does not fit). */
50
+ export const startBeatOf = (meter, pickup) => pickup && isValidPickup(pickup, meter) ? meter.beats - pickup / meter.beatTicks + 1 : 1;
51
+ const contentOf = (music, index) => Math.max(0, ...music.parts.map((p) => totalTicks(p.measures[index]?.events ?? [])));
52
+ /**
53
+ * Every bar's frame. With a pickup (that fits the first bar), the first bar holds just the pickup
54
+ * and starts that far before its bar's end; the final bar holds the rest of a bar, unless its
55
+ * music already runs past that (a score still being written), when it is a full bar again.
56
+ */
57
+ export function framesOf(music, scoreTime, pickup = 0) {
58
+ const meters = metersOf(music.measures, scoreTime);
59
+ const lead = meters.length && isValidPickup(pickup, meters[0]) ? pickup : 0;
60
+ const last = meters.length - 1;
61
+ return meters.map((meter, index) => {
62
+ // A text bar stands for whole bars of rest, however the score starts or ends.
63
+ const textBar = music.measures[index].text_bar;
64
+ if (textBar)
65
+ return { meter, length: textBar.bars * meter.measureTicks, phase: 0 };
66
+ if (lead && index === 0)
67
+ return { meter, length: lead, phase: meter.measureTicks - lead };
68
+ const rest = meter.measureTicks - lead;
69
+ const shortEnd = lead && index === last && contentOf(music, index) <= rest;
70
+ return { meter, length: shortEnd ? rest : meter.measureTicks, phase: 0 };
71
+ });
72
+ }
73
+ /** The beamed group (a beat in a simple meter, three eighths in 6/8) that tick `at` of a bar falls in. */
74
+ export const groupOf = ({ meter, phase }, at) => Math.floor((at + phase) / meter.groupTicks);
75
+ /** Where each beat of a frame starts, in ticks from the start of the bar. */
76
+ export const beatOnsets = ({ meter, length, phase }) => Array.from({ length: Math.ceil((phase + length) / meter.beatTicks) }, (_, beat) => beat * meter.beatTicks - phase).filter((onset) => onset >= 0 && onset < length);
77
+ /**
78
+ * The start of the beat that tick `at` of a bar falls in (a pickup bar's beats count from the
79
+ * downbeat it leads to). Whatever is written there, a digit or a continuation dot, is that beat.
80
+ */
81
+ export const beatStartOf = ({ meter, phase }, at) => Math.max(0, Math.floor((at + phase) / meter.beatTicks) * meter.beatTicks - phase);
82
+ /** Snap a duration to the tick grid and the allowed range. */
83
+ export const clampDuration = (ticks) => Math.min(MAX_DURATION_TICKS, Math.max(TICK_STEP, Math.round(ticks / TICK_STEP) * TICK_STEP));
84
+ /** Total length of a list of events. */
85
+ export const totalTicks = (events) => events.reduce((sum, e) => sum + e.duration, 0);
@@ -0,0 +1,7 @@
1
+ import type { PerformanceSource } from "../playback/source.js";
2
+ import type { Performance } from "../playback/types.js";
3
+ import type { ScoreInput } from "../schema/types.js";
4
+ /** Compiles a score document into the notes a player schedules: when each sounds, at what pitch. */
5
+ export declare const compilePerformance: (score: ScoreInput) => Performance;
6
+ /** A performance source that compiles in the browser: no server needed. */
7
+ export declare const localPerformanceSource: PerformanceSource;
@@ -0,0 +1,8 @@
1
+ import { readScore } from "./readScore.js";
2
+ import { buildPerformance } from "./timeline.js";
3
+ /** Compiles a score document into the notes a player schedules: when each sounds, at what pitch. */
4
+ export const compilePerformance = (score) => buildPerformance(readScore(score));
5
+ /** A performance source that compiles in the browser: no server needed. */
6
+ export const localPerformanceSource = {
7
+ perform: async (score) => compilePerformance(score),
8
+ };
@@ -0,0 +1,6 @@
1
+ import type { PlayedMeasure } from "../playback/types.js";
2
+ import type { MeasureMark } from "./model.js";
3
+ /** Loudness of each dynamic, 0..1 (the output scales its note level by it). */
4
+ export declare const LEVELS: Readonly<Record<string, number>>;
5
+ /** The loudness at any tick of the performance, from its dynamics and hairpins (ADR 0011). */
6
+ export declare function levelCurve(measures: readonly MeasureMark[], played: readonly PlayedMeasure[], indexes: readonly number[]): (tick: number) => number;
@@ -0,0 +1,78 @@
1
+ /** Loudness of each dynamic, 0..1 (the output scales its note level by it). */
2
+ export const LEVELS = {
3
+ ppp: 0.25, pp: 0.35, p: 0.45, mp: 0.6, mf: 0.72, f: 0.85, ff: 0.95, fff: 1.0,
4
+ };
5
+ const DEFAULT_LEVEL = LEVELS.mf;
6
+ const ORDER = Object.values(LEVELS);
7
+ function levelOf(segment, tick) {
8
+ const { end, target, level, start } = segment;
9
+ if (end === undefined || target === undefined || tick >= end)
10
+ return target ?? level;
11
+ return level + (target - level) * ((tick - start) / (end - start));
12
+ }
13
+ /** One dynamic step louder (crescendo) or softer (diminuendo) than `level`. */
14
+ const step = (level, shape) => shape === "cresc"
15
+ ? (ORDER.find((v) => v > level + 1e-9) ?? ORDER[ORDER.length - 1])
16
+ : ([...ORDER].reverse().find((v) => v < level - 1e-9) ?? ORDER[0]);
17
+ /** Every dynamic and hairpin in playing order: a repeated bar's marks apply again each pass. */
18
+ function placedMarks(measures, played, indexes) {
19
+ const dynamics = [];
20
+ const hairpins = [];
21
+ played.forEach((measure, position) => {
22
+ const mark = measures[indexes[position]];
23
+ for (const d of mark.dynamics)
24
+ dynamics.push([measure.start + d.at, LEVELS[d.level]]);
25
+ for (const h of mark.hairpins) {
26
+ const endBar = played.slice(position).find((p) => p.measure_id === h.endMeasure);
27
+ const end = endBar ? endBar.start + h.endAt : null;
28
+ if (end !== null && end > measure.start + h.at)
29
+ hairpins.push([measure.start + h.at, end, h.shape]);
30
+ }
31
+ });
32
+ return { dynamics: dynamics.sort((a, b) => a[0] - b[0] || a[1] - b[1]), hairpins: hairpins.sort((a, b) => a[0] - b[0] || a[1] - b[1] || (a[2] < b[2] ? -1 : a[2] > b[2] ? 1 : 0)) };
33
+ }
34
+ /** The level's course: a step at each dynamic, a ramp over each hairpin, in time order. */
35
+ function segmentsOf(dynamics, hairpins) {
36
+ const written = new Map(dynamics);
37
+ // Dynamics before hairpins at the same tick: a hairpin starts from the mark written there.
38
+ const events = [
39
+ ...dynamics.map(([tick, level]) => ({ tick, order: 0, level })),
40
+ ...hairpins.map(([start, end, shape]) => ({ tick: start, order: 1, end, shape })),
41
+ ].sort((a, b) => a.tick - b.tick || a.order - b.order);
42
+ const segments = [{ start: -1, level: DEFAULT_LEVEL }];
43
+ let current = DEFAULT_LEVEL;
44
+ for (const event of events) {
45
+ if (event.order === 0) {
46
+ current = event.level;
47
+ segments.push({ start: event.tick, level: current });
48
+ }
49
+ else {
50
+ const target = written.get(event.end) ?? step(current, event.shape);
51
+ segments.push({ start: event.tick, level: current, end: event.end, target });
52
+ current = target;
53
+ }
54
+ }
55
+ return segments;
56
+ }
57
+ /**
58
+ * Python's `round(x, 3)`: both round the exact value of the double, unlike `Math.round(x * 1000)`,
59
+ * which rounds a product that may already have drifted past a half.
60
+ */
61
+ const round3 = (x) => Number(x.toFixed(3));
62
+ /** The loudness at any tick of the performance, from its dynamics and hairpins (ADR 0011). */
63
+ export function levelCurve(measures, played, indexes) {
64
+ const { dynamics, hairpins } = placedMarks(measures, played, indexes);
65
+ const segments = segmentsOf(dynamics, hairpins);
66
+ return (tick) => {
67
+ let low = 0;
68
+ let high = segments.length;
69
+ while (low < high) {
70
+ const middle = (low + high) >> 1;
71
+ if (segments[middle].start <= tick)
72
+ low = middle + 1;
73
+ else
74
+ high = middle;
75
+ }
76
+ return round3(levelOf(segments[low - 1], tick));
77
+ };
78
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * What the compiler reads: the part of a score that sounds, in a flat typed form (a port of
3
+ * `backend/apps/playback/model.py`). The output is the `Performance` of `../playback/types`.
4
+ */
5
+ import type { PartKind } from "../schema/types.js";
6
+ export type WrittenNote = {
7
+ id: string;
8
+ duration: number;
9
+ /** Null for a rest. */
10
+ degree: number | null;
11
+ octave: number;
12
+ accidental: "sharp" | "flat" | null;
13
+ tie: boolean;
14
+ };
15
+ export type Dynamic = {
16
+ at: number;
17
+ level: string;
18
+ };
19
+ export type Hairpin = {
20
+ at: number;
21
+ shape: string;
22
+ endMeasure: string;
23
+ endAt: number;
24
+ };
25
+ /** What a measure is for every part: its meter, bar line, repeat marks, key and dynamics. */
26
+ export type MeasureMark = {
27
+ id: string;
28
+ time: string | null;
29
+ barline: string;
30
+ repeatStart: boolean;
31
+ volta: number | null;
32
+ /** A jump mark at the end of the bar (Fine, D.C. al Fine); null = none. */
33
+ navigation: "fine" | "dc_al_fine" | null;
34
+ /** A text bar stands for this many bars of rest; 0 = an ordinary measure. */
35
+ restBars: number;
36
+ key: string | null;
37
+ keyAt: number;
38
+ /** The tempo from this measure on (schema 4); null = the tempo in force. */
39
+ tempo: number | null;
40
+ dynamics: Dynamic[];
41
+ hairpins: Hairpin[];
42
+ };
43
+ export type PlaybackPart = {
44
+ id: string;
45
+ name: string;
46
+ kind: PartKind;
47
+ measures: WrittenNote[][];
48
+ };
49
+ export type PlaybackScore = {
50
+ key: string;
51
+ time: string;
52
+ tempo: number;
53
+ pickup: number;
54
+ measures: MeasureMark[];
55
+ parts: PlaybackPart[];
56
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { MeasureMark } from "./model.js";
2
+ type OrderMark = Pick<MeasureMark, "barline" | "repeatStart" | "volta"> & Partial<Pick<MeasureMark, "navigation">>;
3
+ /**
4
+ * The order measures are played in, as indexes into `measures`: repeats and 1st/2nd endings
5
+ * unrolled, then any "D.C. al Fine" followed. A `repeat_end` bar line jumps back once to the
6
+ * nearest repeat start before it (or to the start of the score, or to just after the previous
7
+ * repeat that was finished); a measure with an ending number plays only on that pass. Every repeat
8
+ * jumps back at most once, so the order is always finite.
9
+ *
10
+ * A "D.C. al Fine" (the first one counts) ends the first pass at its bar, after that bar's own
11
+ * repeat: the music goes back to the start and plays to the first bar marked "Fine" (to the D.C.
12
+ * bar itself when there is none). That second pass takes no repeats and plays only the last
13
+ * ending of a repeated passage. A "Fine" does nothing on the first pass.
14
+ */
15
+ export declare function playOrder(measures: readonly OrderMark[]): number[];
16
+ export {};
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The order measures are played in, as indexes into `measures`: repeats and 1st/2nd endings
3
+ * unrolled, then any "D.C. al Fine" followed. A `repeat_end` bar line jumps back once to the
4
+ * nearest repeat start before it (or to the start of the score, or to just after the previous
5
+ * repeat that was finished); a measure with an ending number plays only on that pass. Every repeat
6
+ * jumps back at most once, so the order is always finite.
7
+ *
8
+ * A "D.C. al Fine" (the first one counts) ends the first pass at its bar, after that bar's own
9
+ * repeat: the music goes back to the start and plays to the first bar marked "Fine" (to the D.C.
10
+ * bar itself when there is none). That second pass takes no repeats and plays only the last
11
+ * ending of a repeated passage. A "Fine" does nothing on the first pass.
12
+ */
13
+ export function playOrder(measures) {
14
+ const order = unrolled(measures);
15
+ const dc = measures.findIndex((m) => m.navigation === "dc_al_fine");
16
+ const lastDc = order.lastIndexOf(dc);
17
+ if (lastDc < 0)
18
+ return order; // the bar is in an ending that is never played: nothing to jump from
19
+ return [...order.slice(0, lastDc + 1), ...againToFine(measures, dc)];
20
+ }
21
+ /** The second pass of a D.C.: from the first bar to the Fine, or to the D.C. bar without one. */
22
+ function againToFine(measures, dc) {
23
+ const fine = measures.findIndex((m) => m.navigation === "fine");
24
+ const last = fine >= 0 ? fine : dc;
25
+ const finalEnding = Math.max(0, ...measures.map((m) => m.volta ?? 0));
26
+ const again = [];
27
+ for (let index = 0; index <= last; index += 1) {
28
+ const volta = measures[index].volta;
29
+ if (volta === null || volta === finalEnding)
30
+ again.push(index);
31
+ }
32
+ return again;
33
+ }
34
+ /** Repeats and endings unrolled; jump marks are not looked at here. */
35
+ function unrolled(measures) {
36
+ const order = [];
37
+ const jumped = new Set();
38
+ let sectionStart = 0;
39
+ let passNumber = 1;
40
+ let lastJump = -1;
41
+ let index = 0;
42
+ while (index < measures.length) {
43
+ const measure = measures[index];
44
+ const pastFinishedRepeat = index > lastJump && measure.volta === null && passNumber > 1;
45
+ if ((measure.repeatStart && index !== sectionStart) || pastFinishedRepeat) {
46
+ sectionStart = index;
47
+ passNumber = 1;
48
+ }
49
+ if (measure.volta !== null && measure.volta !== passNumber) {
50
+ index += 1;
51
+ continue;
52
+ }
53
+ order.push(index);
54
+ if (measure.barline === "repeat_end" && !jumped.has(index)) {
55
+ jumped.add(index);
56
+ lastJump = index;
57
+ passNumber += 1;
58
+ index = sectionStart;
59
+ continue;
60
+ }
61
+ index += 1;
62
+ }
63
+ return order;
64
+ }
@@ -0,0 +1,4 @@
1
+ import type { ScoreInput } from "../schema/types.js";
2
+ import type { PlaybackScore } from "./model.js";
3
+ /** A validated score document as the compiler reads it (port of `score_from_document`). */
4
+ export declare function readScore(score: ScoreInput): PlaybackScore;