jpfun 0.7.0 → 0.9.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 (152) hide show
  1. package/Bravura-LICENSE.txt +93 -0
  2. package/README.md +49 -146
  3. package/dist/Bravura-LICENSE.txt +93 -0
  4. package/dist/converter/midi/convert.d.ts +0 -10
  5. package/dist/converter/midi/convert.js +19 -56
  6. package/dist/converter/musicxml/features.d.ts +16 -8
  7. package/dist/converter/musicxml/features.js +88 -19
  8. package/dist/converter/musicxml/index.js +88 -50
  9. package/dist/converter/musicxml/model.d.ts +9 -0
  10. package/dist/converter/source.d.ts +4 -7
  11. package/dist/converter/source.js +11 -9
  12. package/dist/core.d.ts +1 -0
  13. package/dist/core.js +1 -0
  14. package/dist/diagnostic.d.ts +7 -1
  15. package/dist/diagnostic.js +40 -88
  16. package/dist/fraction.js +3 -2
  17. package/dist/functions/ASTtypes.d.ts +3 -32
  18. package/dist/functions/ASTtypes.js +5 -52
  19. package/dist/functions/adjust/index.js +45 -33
  20. package/dist/functions/arpeggio/index.js +50 -55
  21. package/dist/functions/arpeggio/shape.d.ts +2 -3
  22. package/dist/functions/arpeggio/shape.js +20 -50
  23. package/dist/functions/bar/index.d.ts +8 -0
  24. package/dist/functions/bar/index.js +105 -8
  25. package/dist/functions/beam/auto.d.ts +1 -2
  26. package/dist/functions/beam/auto.js +80 -135
  27. package/dist/functions/beam/index.js +6 -12
  28. package/dist/functions/beam/layout.d.ts +1 -1
  29. package/dist/functions/beam/layout.js +1 -1
  30. package/dist/functions/box/index.js +0 -6
  31. package/dist/functions/br/index.d.ts +34 -2
  32. package/dist/functions/br/index.js +5 -4
  33. package/dist/functions/dash/index.js +9 -10
  34. package/dist/functions/default.js +2 -1
  35. package/dist/functions/div/index.d.ts +3 -4
  36. package/dist/functions/div/index.js +18 -14
  37. package/dist/functions/dot/index.js +5 -11
  38. package/dist/functions/dyn/index.js +55 -57
  39. package/dist/functions/gliss/index.js +131 -103
  40. package/dist/functions/grace/index.d.ts +3 -12
  41. package/dist/functions/grace/index.js +52 -102
  42. package/dist/functions/head/index.js +10 -7
  43. package/dist/functions/key/index.js +13 -15
  44. package/dist/functions/meter/index.d.ts +56 -1
  45. package/dist/functions/meter/index.js +15 -10
  46. package/dist/functions/note/index.js +6 -6
  47. package/dist/functions/note/noteName.d.ts +16 -0
  48. package/dist/functions/note/noteName.js +88 -0
  49. package/dist/functions/note/source-transform.js +8 -10
  50. package/dist/functions/ottava/index.js +35 -40
  51. package/dist/functions/page/index.d.ts +1 -0
  52. package/dist/functions/page/index.js +27 -21
  53. package/dist/functions/pagebreak/index.js +2 -2
  54. package/dist/functions/rest/index.js +5 -5
  55. package/dist/functions/set/index.js +4 -4
  56. package/dist/functions/slide/index.js +0 -1
  57. package/dist/functions/stack/index.js +8 -57
  58. package/dist/functions/symbol/index.d.ts +21 -2
  59. package/dist/functions/symbol/index.js +69 -24
  60. package/dist/functions/symbol/ornament.d.ts +1 -2
  61. package/dist/functions/symbol/shape.d.ts +6 -0
  62. package/dist/functions/symbol/shape.js +16 -3
  63. package/dist/functions/symbol/symbols/coda.d.ts +4 -0
  64. package/dist/functions/symbol/symbols/coda.js +56 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
  66. package/dist/functions/symbol/symbols/dynamics.js +18 -8
  67. package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
  68. package/dist/functions/symbol/symbols/harmonic.js +11 -0
  69. package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
  70. package/dist/functions/symbol/symbols/marcato.js +22 -0
  71. package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
  72. package/dist/functions/symbol/symbols/staccato.js +24 -0
  73. package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
  74. package/dist/functions/symbol/symbols/tenuto.js +11 -0
  75. package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
  76. package/dist/functions/symbol/symbols/tocoda.js +16 -0
  77. package/dist/functions/symbol/symbols/turn.d.ts +3 -0
  78. package/dist/functions/symbol/symbols/turn.js +50 -0
  79. package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
  80. package/dist/functions/symbol/symbols/wheel.js +36 -0
  81. package/dist/functions/temporal.d.ts +3 -2
  82. package/dist/functions/temporal.js +8 -0
  83. package/dist/functions/text/index.js +38 -35
  84. package/dist/functions/tie/index.js +44 -43
  85. package/dist/functions/tremolo/index.d.ts +2 -0
  86. package/dist/functions/tremolo/index.js +318 -0
  87. package/dist/functions/tuplet/index.js +4 -14
  88. package/dist/functions/up/index.js +81 -85
  89. package/dist/functions/voice/connections.d.ts +2 -1
  90. package/dist/functions/voice/connections.js +48 -10
  91. package/dist/functions/voice/index.d.ts +2 -3
  92. package/dist/functions/voice/index.js +2 -963
  93. package/dist/functions/voice/lyrics.d.ts +30 -0
  94. package/dist/functions/voice/lyrics.js +289 -0
  95. package/dist/functions/voice/voice.d.ts +63 -0
  96. package/dist/functions/voice/voice.js +373 -0
  97. package/dist/functions/voice/voices.d.ts +106 -0
  98. package/dist/functions/voice/voices.js +423 -0
  99. package/dist/functions/volta/index.js +5 -23
  100. package/dist/jpfun.core.min.js +76 -59
  101. package/dist/jpfun.from-midi.min.js +5 -5
  102. package/dist/jpfun.from-musicxml.min.js +8 -8
  103. package/dist/jpfun.min.js +81 -64
  104. package/dist/layout/default.d.ts +0 -1
  105. package/dist/layout/default.js +0 -1
  106. package/dist/layout/engine.js +30 -18
  107. package/dist/layout/model.d.ts +0 -1
  108. package/dist/layout/model.js +1 -4
  109. package/dist/layout/page.d.ts +2 -1
  110. package/dist/layout/page.js +14 -14
  111. package/dist/layout/range.d.ts +7 -6
  112. package/dist/layout/range.js +23 -14
  113. package/dist/layout/types.d.ts +9 -28
  114. package/dist/lowering/loweringContext.d.ts +18 -23
  115. package/dist/lowering/loweringContext.js +33 -46
  116. package/dist/lowering/track.d.ts +8 -1
  117. package/dist/lowering/types.d.ts +13 -11
  118. package/dist/parser/parse-utils/call-utils.d.ts +1 -2
  119. package/dist/parser/parse-utils/call-utils.js +6 -34
  120. package/dist/parser/parse-utils/note-utils.d.ts +3 -3
  121. package/dist/parser/parse-utils/note-utils.js +21 -33
  122. package/dist/parser/parse-utils/string-utils.d.ts +3 -2
  123. package/dist/parser/parse-utils/string-utils.js +20 -5
  124. package/dist/parser/parserContext.d.ts +9 -9
  125. package/dist/parser/parserContext.js +62 -11
  126. package/dist/pipeline.js +2 -3
  127. package/dist/playback/compile.js +131 -82
  128. package/dist/playback/event.d.ts +6 -6
  129. package/dist/playback/event.js +13 -49
  130. package/dist/playback/time.js +42 -31
  131. package/dist/playback/types.d.ts +3 -1
  132. package/dist/render/canvas.js +4 -22
  133. package/dist/render/recording.js +1 -4
  134. package/dist/render/svg.d.ts +0 -1
  135. package/dist/render/svg.js +33 -31
  136. package/dist/render/text.d.ts +36 -1
  137. package/dist/render/text.js +134 -0
  138. package/dist/render/translate.d.ts +0 -1
  139. package/dist/render/translate.js +6 -23
  140. package/dist/render/types.d.ts +0 -2
  141. package/grammar.md +39 -30
  142. package/package.json +3 -3
  143. package/dist/functions/note/noteNameFSM.d.ts +0 -8
  144. package/dist/functions/note/noteNameFSM.js +0 -181
  145. package/dist/iife/core.d.ts +0 -1
  146. package/dist/iife/core.js +0 -3
  147. package/dist/iife/from-midi.d.ts +0 -1
  148. package/dist/iife/from-midi.js +0 -15
  149. package/dist/iife/from-musicxml.d.ts +0 -1
  150. package/dist/iife/from-musicxml.js +0 -3
  151. package/dist/iife/full.d.ts +0 -1
  152. package/dist/iife/full.js +0 -3
@@ -1,7 +1,28 @@
1
1
  import { child, children, descendant, descendants, nameOf, number, text, } from "./dom.js";
2
2
  import { NoteNameMap, SHARP_NOTE_NAMES } from "../../parser/parse-utils/note-utils.js";
3
- const DYNAMIC_NAMES = new Set(["ppp", "pp", "p", "mp", "mf", "f", "ff", "fff"]);
3
+ const DYNAMIC_NAME = /^(?:p+|f+|mp|mf)$/;
4
4
  const MAX_ENDING_PASS = 256;
5
+ const SOUND_REPEATS = [
6
+ ["segno", "segno"],
7
+ ["coda", "coda"],
8
+ ["dacapo", "dc"],
9
+ ["dalsegno", "ds"],
10
+ ["fine", "fine"],
11
+ ["tocoda", "tocoda"],
12
+ ];
13
+ /** 识别 <words> 中的标准反复跳转与终止术语 */
14
+ export function repeatWordSymbol(value) {
15
+ const normalized = value.trim().replace(/\s+/g, " ");
16
+ if (/^(?:d\.\s*c\.?|da\s+capo)(?:\s+al\s+(?:fine|coda)\.?)?$/i.test(normalized))
17
+ return "dc";
18
+ if (/^(?:d\.\s*s\.?|dal\s+segno)(?:\s+al\s+(?:fine|coda)\.?)?$/i.test(normalized))
19
+ return "ds";
20
+ if (/^fine\.?$/i.test(normalized))
21
+ return "fine";
22
+ if (/^(?:to\s+coda|al\s+coda)\.?$/i.test(normalized))
23
+ return "tocoda";
24
+ return undefined;
25
+ }
5
26
  const DIAGNOSTICS = {
6
27
  percussion: ["PERCUSSION_SKIPPED", "MIDI channel 10 percussion note was omitted."],
7
28
  displayPitch: ["APPROXIMATED", "Unpitched note was converted to a melodic note using its display pitch."],
@@ -30,8 +51,9 @@ const DIAGNOSTICS = {
30
51
  glissStart: ["UNRESOLVED_RELATION", "Glissando stop has no matching start and was omitted."],
31
52
  glissStop: ["UNRESOLVED_RELATION", "Glissando start has no matching stop and was omitted."],
32
53
  glissReplaced: ["UNRESOLVED_RELATION", "Glissando start was replaced before a matching stop and was omitted."],
33
- glissEndpoints: ["UNRESOLVED_RELATION", "Glissando endpoints are simultaneous or cross a score line and were omitted."],
54
+ glissEndpoints: ["UNRESOLVED_RELATION", "Glissando endpoints are simultaneous and were omitted."],
34
55
  glissStyle: ["APPROXIMATED", "MusicXML <{value}> was converted to a straight glissando arrow."],
56
+ symbolStyle: ["APPROXIMATED", "MusicXML <{value}> was mapped to the default symbol shape."],
35
57
  };
36
58
  export function reportDiagnostic(diagnostics, issue, location, value = "") {
37
59
  const [kind, message] = DIAGNOSTICS[issue];
@@ -201,7 +223,7 @@ export function transposePitch(pitch, chromatic, diatonic, octaves) {
201
223
  pitch.alter = midi - (pitch.octave + 1) * 12 - NoteNameMap[pitch.step];
202
224
  }
203
225
  }
204
- /** 将支持的 fermata articulation 和 ornament 转成 jpFun 修饰符 */
226
+ /** 将支持的延长、奏法与装饰音记号转成 jpFun 修饰符 */
205
227
  export function noteModifiers(notations, diagnostics, location) {
206
228
  const modifiers = [];
207
229
  const slides = [];
@@ -214,10 +236,18 @@ export function noteModifiers(notations, diagnostics, location) {
214
236
  }
215
237
  const articulations = notations.read("articulations");
216
238
  if (articulations) {
217
- const accent = notations.read("accent", false, articulations);
218
- if (accent) {
219
- modifiers.push({ name: "accent", placement: placementOf(accent, "above") });
220
- notations.accept(accent);
239
+ for (const [tag, name] of [
240
+ ["accent", "accent"], ["staccato", "staccato"], ["tenuto", "tenuto"],
241
+ ["strong-accent", "marcato"], ["staccatissimo", "staccato"],
242
+ ]) {
243
+ const articulation = notations.read(tag, false, articulations);
244
+ if (!articulation)
245
+ continue;
246
+ modifiers.push({ name, placement: placementOf(articulation, "above") });
247
+ notations.accept(articulation);
248
+ if (tag === "strong-accent" && articulation.getAttribute("type") === "down") {
249
+ reportDiagnostic(diagnostics, "symbolStyle", { ...location, element: tag }, tag);
250
+ }
221
251
  }
222
252
  for (const [tag, side, direction] of [
223
253
  ["scoop", "pre", "up"], ["plop", "pre", "down"],
@@ -237,16 +267,20 @@ export function noteModifiers(notations, diagnostics, location) {
237
267
  }
238
268
  const ornaments = notations.read("ornaments");
239
269
  if (ornaments) {
240
- const names = [
270
+ for (const [tag, name] of [
241
271
  ["trill-mark", "tr"],
242
272
  ["mordent", "mordent"],
243
273
  ["inverted-mordent", "prall"],
244
- ];
245
- for (const [tag, name] of names) {
274
+ ["turn", "turn"],
275
+ ["inverted-turn", "iturn"],
276
+ ]) {
246
277
  const ornament = notations.read(tag, false, ornaments);
247
- if (ornament) {
248
- modifiers.push({ name, placement: placementOf(ornament, "above") });
249
- notations.accept(ornament);
278
+ if (!ornament)
279
+ continue;
280
+ modifiers.push({ name, placement: placementOf(ornament, "above") });
281
+ notations.accept(ornament);
282
+ if ((tag === "turn" || tag === "inverted-turn") && ornament.getAttribute("slash") === "yes") {
283
+ reportDiagnostic(diagnostics, "symbolStyle", { ...location, element: tag }, tag);
250
284
  }
251
285
  }
252
286
  notations.report(diagnostics, location, ornaments);
@@ -344,24 +378,59 @@ export function directionDynamic(dynamics, diagnostics, location) {
344
378
  let dynamic;
345
379
  for (const element of children(dynamics)) {
346
380
  const name = nameOf(element);
347
- if (!dynamic && DYNAMIC_NAMES.has(name))
381
+ if (!dynamic && DYNAMIC_NAME.test(name))
348
382
  dynamic = name;
349
383
  else
350
384
  reportDiagnostic(diagnostics, "unsupported", { ...location, element: name }, name);
351
385
  }
352
386
  return dynamic;
353
387
  }
354
- /** 收集排练标记与普通文字,排练标记保留方框语义;空白文字视为空内容 */
355
- export function directionTexts(direction) {
356
- const result = [];
388
+ /** 收集排练标记、反复记号与普通文字,排练标记保留方框语义;空白文字视为空内容 */
389
+ export function directionTexts(direction, defaultPlacement = "above", location) {
390
+ const texts = [];
391
+ const repeats = [];
392
+ for (const tag of ["segno", "coda"]) {
393
+ for (const node of direction.readAll(tag, true)) {
394
+ repeats.push({
395
+ symbol: tag,
396
+ placement: placementOf(node, defaultPlacement),
397
+ location: { ...(location ?? { element: tag }), element: tag },
398
+ });
399
+ direction.accept(node);
400
+ }
401
+ }
357
402
  for (const name of ["rehearsal", "words"]) {
358
403
  for (const node of direction.readAll(name, true)) {
359
404
  const value = node.textContent?.trim();
360
- if (value)
361
- result.push({ text: value, boxed: name === "rehearsal" });
405
+ if (value) {
406
+ const symbol = name === "words" ? repeatWordSymbol(value) : undefined;
407
+ if (symbol) {
408
+ repeats.push({
409
+ symbol,
410
+ placement: placementOf(node, defaultPlacement),
411
+ location: { ...(location ?? { element: name }), element: name },
412
+ });
413
+ }
414
+ else {
415
+ texts.push({ text: value, boxed: name === "rehearsal" });
416
+ }
417
+ }
362
418
  direction.accept(node);
363
419
  }
364
420
  }
421
+ return { texts, repeats };
422
+ }
423
+ /** 读取 <sound> 上的反复与跳转属性 */
424
+ export function soundRepeats(sound, placement, location) {
425
+ if (!sound)
426
+ return [];
427
+ const result = [];
428
+ for (const [attr, symbol] of SOUND_REPEATS) {
429
+ const value = sound.getAttribute(attr)?.trim().toLowerCase();
430
+ if (value && value !== "no") {
431
+ result.push({ symbol, placement, location: { ...location, element: "sound" } });
432
+ }
433
+ }
365
434
  return result;
366
435
  }
367
436
  /** 将 metronome 的拍单位和附点折算成四分音符 BPM */
@@ -11,13 +11,11 @@
11
11
  import { Fraction } from "../../fraction.js";
12
12
  import { NoteNameMap } from "../../parser/parse-utils/note-utils.js";
13
13
  import { child, children, nameOf, number, text, } from "./dom.js";
14
- import { MusicXmlReader, acceptTuplets, directionDynamic, directionTexts, endingPasses, metronomeBpm, mergeArpeggio, noteArpeggio, noteGlissandi, noteLyrics, noteModifiers, parsePitch, parseTimeSignature, partMeasures, readInstrument, reportDiagnostic, scoreMetadata, timeModification, transposePitch, } from "./features.js";
15
- import { attachAbove, quote, renderHead, renderPitch, renderSystems } from "../source.js";
14
+ import { MusicXmlReader, acceptTuplets, directionDynamic, directionTexts, endingPasses, metronomeBpm, mergeArpeggio, noteArpeggio, noteGlissandi, noteLyrics, noteModifiers, parsePitch, parseTimeSignature, partMeasures, readInstrument, reportDiagnostic, scoreMetadata, soundRepeats, timeModification, transposePitch, } from "./features.js";
15
+ import { attachAbove, quote, renderAbsolutePitch, renderHead, renderRelativePitch, renderSystems } from "../source.js";
16
16
  import { matchingLanes, resolveRelations, resolveVoices } from "./resolve.js";
17
17
  const MAJOR_KEYS = ["Cb", "Gb", "Db", "Ab", "Eb", "Bb", "F", "C", "G", "D", "A", "E", "B", "F#", "C#"];
18
18
  const MINOR_KEYS = ["Ab", "Eb", "Bb", "F", "C", "G", "D", "A", "E", "B", "F#", "C#", "G#", "D#", "A#"];
19
- // name = token[0] acc = token.slice(1)
20
- const RELATIVE_NAMES = ["1", "1#", "2", "2#", "3", "4", "4#", "5", "5#", "6", "6#", "7"];
21
19
  function add(left, right) {
22
20
  return left.clone().add(right);
23
21
  }
@@ -47,7 +45,7 @@ function insideSpan(spans, at) {
47
45
  function lyricToken(value) {
48
46
  const continuation = value.endsWith("-");
49
47
  const body = continuation ? value.slice(0, -1) : value;
50
- const escaped = body.replace(/[\\{}@-]/g, "\\$&") + (continuation ? "-" : "");
48
+ const escaped = body.replace(/[\\{}@\-*_^]/g, "\\$&") + (continuation ? "-" : "");
51
49
  // 花括号让会被歌词词法拆分的一个 XML lyric 仍只占一个歌词槽。
52
50
  const hasNonAscii = [...body].some(char => char.codePointAt(0) > 0x7f);
53
51
  return /\s/.test(body) || (hasNonAscii && ([...body].length > 1 || body.length > 1 || continuation))
@@ -102,6 +100,7 @@ function parseScore(root, diagnostics) {
102
100
  const meters = new Map();
103
101
  const keys = new Map();
104
102
  const tempos = new Map();
103
+ const repeats = new Map();
105
104
  const directions = [];
106
105
  const wedgePoints = [];
107
106
  const endingPoints = new Map();
@@ -111,6 +110,11 @@ function parseScore(root, diagnostics) {
111
110
  const multipleRests = [];
112
111
  let scoreEnd = new Fraction();
113
112
  let order = 0;
113
+ const recordRepeat = (point) => {
114
+ const key = `${keyOf(point.at)}\0${point.symbol}`;
115
+ if (!repeats.has(key))
116
+ repeats.set(key, point);
117
+ };
114
118
  // 主时间解析:逐 part、逐小节扫描 attributes/direction/note/barline
115
119
  const measuresByPart = partMeasures(root);
116
120
  const partOrder = new Map([...measuresByPart.keys()].map((id, index) => [id, index]));
@@ -188,6 +192,16 @@ function parseScore(root, diagnostics) {
188
192
  passes,
189
193
  });
190
194
  }
195
+ for (const tag of ["segno", "coda"]) {
196
+ const mark = child(barline, tag);
197
+ if (mark)
198
+ recordRepeat({
199
+ at: at.clone(),
200
+ symbol: tag,
201
+ placement: mark.getAttribute("placement") === "below" ? "below" : "above",
202
+ location: { ...measureLocation, element: tag },
203
+ });
204
+ }
191
205
  };
192
206
  // 必须按 XML 原始顺序扫描,backup forward 和 chord 都依赖当前游标或前一事件
193
207
  for (const item of measureItems) {
@@ -282,38 +296,44 @@ function parseScore(root, diagnostics) {
282
296
  ...measureLocation, element: metronome && markedBpm === bpm ? "metronome" : "sound",
283
297
  } });
284
298
  }
285
- if (!reader)
286
- continue;
287
- if (bpm !== undefined && markedBpm === bpm)
288
- reader.accept(metronome);
299
+ const placement = item.getAttribute("placement") === "below" ? "below" : "above";
289
300
  const staff = text(item, "staff") || "1";
290
301
  const voice = text(item, "voice");
291
302
  const location = { ...measureLocation, element: tag, staff, voice: voice || undefined };
292
- const dynamics = reader.read("dynamics", true);
293
- const dynamic = directionDynamic(dynamics, diagnostics, location);
294
- reader.accept(dynamics);
295
- const texts = directionTexts(reader);
296
- if (dynamic || texts.length > 0)
297
- directions.push({
298
- location,
299
- at,
300
- placement: item.getAttribute("placement") === "below" ? "below" : "above",
301
- dynamic,
302
- texts,
303
- });
304
- const wedge = reader.read("wedge", true);
305
- const wedgeType = wedge?.getAttribute("type");
306
- if (wedgeType === "crescendo" || wedgeType === "diminuendo" || wedgeType === "stop") {
307
- wedgePoints.push({
308
- location: { ...location, element: "wedge" },
309
- at,
310
- number: wedge?.getAttribute("number") || "1",
311
- type: wedgeType,
312
- });
313
- reader.accept(wedge);
303
+ if (reader) {
304
+ if (bpm !== undefined && markedBpm === bpm)
305
+ reader.accept(metronome);
306
+ const dynamics = reader.read("dynamics", true);
307
+ const dynamic = directionDynamic(dynamics, diagnostics, location);
308
+ reader.accept(dynamics);
309
+ const { texts, repeats: dirRepeats } = directionTexts(reader, placement, location);
310
+ for (const repeat of dirRepeats)
311
+ recordRepeat({ ...repeat, at: at.clone() });
312
+ if (dynamic || texts.length > 0)
313
+ directions.push({
314
+ location,
315
+ at,
316
+ placement,
317
+ dynamic,
318
+ texts,
319
+ });
320
+ const wedge = reader.read("wedge", true);
321
+ const wedgeType = wedge?.getAttribute("type");
322
+ if (wedgeType === "crescendo" || wedgeType === "diminuendo" || wedgeType === "stop") {
323
+ wedgePoints.push({
324
+ location: { ...location, element: "wedge" },
325
+ at,
326
+ number: wedge?.getAttribute("number") || "1",
327
+ type: wedgeType,
328
+ });
329
+ reader.accept(wedge);
330
+ }
331
+ for (const directionType of children(item, "direction-type")) {
332
+ reader.report(diagnostics, location, directionType);
333
+ }
314
334
  }
315
- for (const directionType of children(item, "direction-type")) {
316
- reader.report(diagnostics, location, directionType);
335
+ for (const repeat of soundRepeats(sound, placement, { ...measureLocation, element: "sound" })) {
336
+ recordRepeat({ ...repeat, at: at.clone() });
317
337
  }
318
338
  continue;
319
339
  }
@@ -528,6 +548,7 @@ function parseScore(root, diagnostics) {
528
548
  meters: [...meters.values()].sort((left, right) => left.at.compare(right.at)),
529
549
  keys: [...keys.values()].sort((left, right) => left.at.compare(right.at)),
530
550
  tempos: [...tempos.values()].sort((left, right) => left.at.compare(right.at)),
551
+ repeats: [...repeats.values()].sort((left, right) => left.at.compare(right.at)),
531
552
  wedges,
532
553
  endings,
533
554
  bars,
@@ -571,26 +592,19 @@ function pointAt(points, at) {
571
592
  }
572
593
  /** 生成单个音高;octave=0 等有歧义的情况回退到显式 note */
573
594
  function pitchSource(pitch, mode, key, metadata, suffix = "") {
574
- let name;
575
- let accidental;
576
- let octave;
595
+ let token;
577
596
  if (mode === "absolute") {
578
- name = pitch.step;
579
- accidental = pitch.alter > 0 ? "#".repeat(pitch.alter) : "b".repeat(-pitch.alter);
580
- octave = pitch.octave;
597
+ const accidental = pitch.alter > 0 ? "#".repeat(pitch.alter) : "b".repeat(-pitch.alter);
598
+ token = renderAbsolutePitch(pitch.step, accidental, pitch.octave);
581
599
  }
582
600
  else {
583
601
  const midi = (pitch.octave + 1) * 12 + NoteNameMap[pitch.step] + pitch.alter;
584
602
  const tonic = tonicName(key);
585
603
  const tonicAlter = tonic[1] === "#" ? 1 : tonic[1] === "b" ? -1 : 0;
586
- const difference = midi - 60 - NoteNameMap[tonic[0]] - tonicAlter;
587
- const pitchClass = ((difference % 12) + 12) % 12;
588
- name = RELATIVE_NAMES[pitchClass][0];
589
- accidental = RELATIVE_NAMES[pitchClass].slice(1);
590
- octave = Math.floor(difference / 12);
604
+ token = renderRelativePitch(midi - 60 - NoteNameMap[tonic[0]] - tonicAlter);
591
605
  }
592
606
  const label = metadata.labels.get(pitch);
593
- return renderPitch({ name, accidental, octave }, mode, suffix) + (label ? `@${label}` : "");
607
+ return `${token}${suffix}${label ? `@${label}` : ""}`;
594
608
  }
595
609
  /** 把和弦、倚音、力度和文字组合成一个 jpFun 原子表达式 */
596
610
  function eventSource(event, mode, key, metadata, diagnostics, suffix = "") {
@@ -857,7 +871,6 @@ function labelRelations(score, diagnostics) {
857
871
  after.push(source);
858
872
  };
859
873
  const activeGlissandi = new Map();
860
- const glissLineBreaks = [...score.lineBreaks].map(fractionFromKey);
861
874
  const glissandi = [];
862
875
  const active = new Map();
863
876
  const unmatchedTies = new Set();
@@ -919,7 +932,7 @@ function labelRelations(score, diagnostics) {
919
932
  activeGlissandi.delete(key);
920
933
  if (!previous)
921
934
  reportDiagnostic(diagnostics, "glissStart", pitch.location);
922
- else if (previous.event.start.equals(event.start) || glissLineBreaks.some(time => time.compare(previous.event.start) > 0 && time.compare(event.start) <= 0)) {
935
+ else if (previous.event.start.equals(event.start)) {
923
936
  reportDiagnostic(diagnostics, "glissEndpoints", pitch.location);
924
937
  }
925
938
  else
@@ -993,6 +1006,7 @@ function renderScore(score, options, diagnostics) {
993
1006
  : score.tempos;
994
1007
  // 初始状态进入 head;中途状态作为精确时间点只写在最上方 lane
995
1008
  const scoreAdjustments = new Map();
1009
+ const barAdjustments = new Map();
996
1010
  const addAdjustment = (adjustments, at, source, location, placement = "above") => {
997
1011
  const key = keyOf(at);
998
1012
  const point = adjustments.get(key) ?? { at, sources: [], pendingLocations: [] };
@@ -1010,6 +1024,10 @@ function renderScore(score, options, diagnostics) {
1010
1024
  addAdjustment(scoreAdjustments, item.at, `@meter(${item.numerator}, ${item.denominator})`);
1011
1025
  for (const item of tempos.slice(1))
1012
1026
  addAdjustment(scoreAdjustments, item.at, `@tempo(${item.bpm})`, item.location);
1027
+ for (const item of score.repeats) {
1028
+ const target = item.symbol === "segno" || item.symbol === "coda" ? scoreAdjustments : barAdjustments;
1029
+ addAdjustment(target, item.at, `$${item.symbol}`, item.location, item.placement);
1030
+ }
1013
1031
  const adjustmentsByLane = score.lanes.map((lane, index) => {
1014
1032
  const adjustments = index === 0 ? scoreAdjustments : new Map();
1015
1033
  for (const dynamic of lane.dynamics ?? []) {
@@ -1064,6 +1082,7 @@ function renderScore(score, options, diagnostics) {
1064
1082
  && !meters.some(point => interior(point.at))
1065
1083
  && ![...score.bars].some(([at, bar]) => interior(fractionFromKey(at)) && bar !== "|")
1066
1084
  && !score.endings.some(span => interior(span.from.at) || interior(span.end))
1085
+ && ![...barAdjustments.values()].some(point => interior(point.at))
1067
1086
  && adjustmentsByLane.every(points => ![...points.values()].some(point => interior(point.at)))
1068
1087
  && programsByLane.every(points => ![...points.keys()].some(at => interior(fractionFromKey(at))))
1069
1088
  && covered.length > 0 && covered.every(blocks => blocks.length === count
@@ -1145,6 +1164,12 @@ function renderScore(score, options, diagnostics) {
1145
1164
  if (!insideSpan(atomicBlocks, point.at))
1146
1165
  points.set(value, point.at);
1147
1166
  }
1167
+ if (laneIndex === 0) {
1168
+ for (const [value, point] of barAdjustments) {
1169
+ if (!insideSpan(atomicBlocks, point.at))
1170
+ points.set(value, point.at);
1171
+ }
1172
+ }
1148
1173
  const timeline = [...points.values()].sort((left, right) => left.compare(right));
1149
1174
  let blockIndex = 0;
1150
1175
  for (let index = 0; index < timeline.length; index++) {
@@ -1153,8 +1178,21 @@ function renderScore(score, options, diagnostics) {
1153
1178
  blockIndex++;
1154
1179
  }
1155
1180
  const bar = score.bars.get(keyOf(at));
1156
- if (bar && (!at.isZero() || bar !== "|"))
1157
- outputs.at(-1).push(bar);
1181
+ const barChanges = laneIndex === 0 ? barAdjustments.get(keyOf(at)) : undefined;
1182
+ if (bar && (!at.isZero() || bar !== "|")) {
1183
+ if (barChanges) {
1184
+ const parts = bar.split(" ");
1185
+ parts[0] = attachAdjustment(parts[0], barChanges);
1186
+ outputs.at(-1).push(parts.join(" "));
1187
+ }
1188
+ else {
1189
+ outputs.at(-1).push(bar);
1190
+ }
1191
+ }
1192
+ else if (barChanges && at.compare(score.end) <= 0) {
1193
+ outputs.at(-1).push(...barChanges.sources, ...(barChanges.below ?? []));
1194
+ barChanges.pendingLocations.length = 0;
1195
+ }
1158
1196
  if (lineBreaks.has(keyOf(at))) {
1159
1197
  outputs.push([]);
1160
1198
  lyricTokens.push(new Map(orderedVerses.map(verse => [verse, []])));
@@ -1218,7 +1256,7 @@ function renderScore(score, options, diagnostics) {
1218
1256
  });
1219
1257
  });
1220
1258
  const omitted = new Set();
1221
- for (const adjustments of [scoreAdjustments, ...adjustmentsByLane]) {
1259
+ for (const adjustments of [scoreAdjustments, barAdjustments, ...adjustmentsByLane]) {
1222
1260
  for (const point of adjustments.values()) {
1223
1261
  for (const location of point.pendingLocations)
1224
1262
  omitted.add(location);
@@ -93,6 +93,14 @@ export interface MusicXmlKeyPoint {
93
93
  fifths: number;
94
94
  mode: string;
95
95
  }
96
+ export type MusicXmlRepeatSymbol = "segno" | "coda" | "dc" | "ds" | "fine" | "tocoda";
97
+ /** 全谱时间流上的反复与跳转记号 */
98
+ export interface MusicXmlRepeatPoint {
99
+ at: Fraction;
100
+ symbol: MusicXmlRepeatSymbol;
101
+ placement: "above" | "below";
102
+ location: MusicXmlDiagnostic["location"];
103
+ }
96
104
  /** 尚未附着到事件的力度或文字方向 */
97
105
  export interface MusicXmlDirectionPoint {
98
106
  location: MusicXmlPartLocation & {
@@ -151,6 +159,7 @@ export interface ParsedMusicXmlScore {
151
159
  bpm: number;
152
160
  location?: MusicXmlDiagnostic["location"];
153
161
  }[];
162
+ repeats: MusicXmlRepeatPoint[];
154
163
  wedges: MusicXmlWedgeSpan[];
155
164
  endings: MusicXmlEndingSpan[];
156
165
  bars: Map<string, string>;
@@ -1,10 +1,5 @@
1
1
  export { quote } from "../parser/parse-utils/string-utils.js";
2
2
  export type PitchMode = "relative" | "absolute";
3
- export interface SourcePitch {
4
- name: string;
5
- accidental: string;
6
- octave: number;
7
- }
8
3
  interface HeadOptions {
9
4
  title?: string;
10
5
  subtitle?: string;
@@ -15,8 +10,10 @@ interface HeadOptions {
15
10
  }
16
11
  /** 按书写顺序把多个零时长标记叠到主体上方。 */
17
12
  export declare function attachAbove(source: string, additions: readonly string[]): string;
18
- /** 把已解释为目标记谱方式的音高写成 jpFun token。 */
19
- export declare function renderPitch(pitch: SourcePitch, mode: PitchMode, suffix?: string): string;
13
+ /** 将相对主音的半音差写成简谱音高 token。 */
14
+ export declare function renderRelativePitch(semitoneOffset: number): string;
15
+ /** 将绝对音名、升降号与八度写成 jpFun 音高 token。 */
16
+ export declare function renderAbsolutePitch(name: string, accidental: string, octave: number): string;
20
17
  export declare function renderHead(options: HeadOptions): string;
21
18
  /** 把“每个声部的各系统”转置成“每个系统的完整声部组” */
22
19
  export declare function renderSystems(groups: readonly (readonly string[])[]): string;
@@ -6,6 +6,7 @@ const HEAD_TEXT_PRESET = {
6
6
  subtitle: "size=0.85em, align=center",
7
7
  author: "size=0.8em, align=right",
8
8
  };
9
+ const RELATIVE_NAMES = ["1", "#1", "2", "#2", "3", "4", "#4", "5", "#5", "6", "#6", "7"];
9
10
  /** 按书写顺序把多个零时长标记叠到主体上方。 */
10
11
  export function attachAbove(source, additions) {
11
12
  for (let index = additions.length - 1; index >= 0; index--) {
@@ -13,15 +14,16 @@ export function attachAbove(source, additions) {
13
14
  }
14
15
  return source;
15
16
  }
16
- /** 把已解释为目标记谱方式的音高写成 jpFun token。 */
17
- export function renderPitch(pitch, mode, suffix = "") {
18
- if (mode === "absolute") {
19
- return pitch.octave === 0
20
- ? `@note(${pitch.name}, ${pitch.accidental}, 0)${suffix}`
21
- : `${pitch.name}${pitch.accidental}${pitch.octave}${suffix}`;
22
- }
23
- const octaveMarks = pitch.octave > 0 ? "'".repeat(pitch.octave) : ",".repeat(-pitch.octave);
24
- return `${pitch.accidental}${pitch.name}${octaveMarks}${suffix}`;
17
+ /** 将相对主音的半音差写成简谱音高 token。 */
18
+ export function renderRelativePitch(semitoneOffset) {
19
+ const pitchClass = ((semitoneOffset % 12) + 12) % 12;
20
+ const octave = Math.floor(semitoneOffset / 12);
21
+ const octaveMarks = octave > 0 ? "'".repeat(octave) : ",".repeat(-octave);
22
+ return `${RELATIVE_NAMES[pitchClass]}${octaveMarks}`;
23
+ }
24
+ /** 将绝对音名、升降号与八度写成 jpFun 音高 token。 */
25
+ export function renderAbsolutePitch(name, accidental, octave) {
26
+ return octave === 0 ? `@note(${name}, ${accidental}, 0)` : `${name}${accidental}${octave}`;
25
27
  }
26
28
  function renderHeadText(field, value) {
27
29
  // 会被注释、续行或 DSL 语法误读的文本改用显式 @text,其余保留简洁的 H.* 裸文本
package/dist/core.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./pipeline.js";
2
2
  export * from "./functions/ASTtypes.js";
3
3
  export * from "./functions/temporal.js";
4
4
  export * from "./functions/default.js";
5
+ export { symbols, type OrnamentPattern, type SymbolDefinition, type SymbolShape, } from "./functions/symbol/index.js";
5
6
  export { transformNotes, type NoteTransform, type SourceEdit } from "./functions/note/index.js";
6
7
  export * from "./diagnostic.js";
7
8
  export * from "./fraction.js";
package/dist/core.js CHANGED
@@ -2,6 +2,7 @@ export * from "./pipeline.js";
2
2
  export * from "./functions/ASTtypes.js";
3
3
  export * from "./functions/temporal.js";
4
4
  export * from "./functions/default.js";
5
+ export { symbols, } from "./functions/symbol/index.js";
5
6
  export { transformNotes } from "./functions/note/index.js";
6
7
  export * from "./diagnostic.js";
7
8
  export * from "./fraction.js";
@@ -5,6 +5,7 @@ export interface LineColRange {
5
5
  endLine: number;
6
6
  endColumn: number;
7
7
  }
8
+ export type DiagnosticParams = Record<string, string>;
8
9
  export declare abstract class Diagnostic {
9
10
  /** 诊断代码 */
10
11
  code: string;
@@ -12,7 +13,12 @@ export declare abstract class Diagnostic {
12
13
  message: string;
13
14
  /** 诊断位置,指示问题在源代码中的位置 */
14
15
  span: SourceSpan;
15
- constructor(code: string, message: string, span: SourceSpan);
16
+ /** 诊断消息中的结构化参数(构造时复制并字符串化,不修改或持有原对象) */
17
+ params?: DiagnosticParams;
18
+ static format(code: string, message: string, params?: DiagnosticParams): string;
19
+ constructor(code: string, message: string, span: SourceSpan, params?: Record<string, string | number | {
20
+ toString(): string;
21
+ }>);
16
22
  toLineCol(lineStarts: ArrayLike<number>): LineColRange;
17
23
  static offsetToLineCol(offset: number, lineStarts: ArrayLike<number>): {
18
24
  line: number;