jpfun 0.6.0 → 0.8.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 (134) 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 +3 -11
  5. package/dist/converter/midi/convert.js +89 -67
  6. package/dist/converter/musicxml/features.d.ts +34 -12
  7. package/dist/converter/musicxml/features.js +131 -24
  8. package/dist/converter/musicxml/index.js +221 -54
  9. package/dist/converter/musicxml/model.d.ts +24 -0
  10. package/dist/core.d.ts +1 -0
  11. package/dist/core.js +1 -0
  12. package/dist/diagnostic.d.ts +7 -1
  13. package/dist/diagnostic.js +40 -88
  14. package/dist/fraction.js +3 -2
  15. package/dist/functions/ASTtypes.d.ts +2 -18
  16. package/dist/functions/ASTtypes.js +0 -16
  17. package/dist/functions/arpeggio/index.js +17 -22
  18. package/dist/functions/arpeggio/shape.d.ts +1 -1
  19. package/dist/functions/arpeggio/shape.js +27 -21
  20. package/dist/functions/bar/index.d.ts +8 -0
  21. package/dist/functions/bar/index.js +139 -36
  22. package/dist/functions/beam/auto.d.ts +1 -2
  23. package/dist/functions/beam/auto.js +44 -27
  24. package/dist/functions/beam/index.js +2 -3
  25. package/dist/functions/beam/layout.d.ts +1 -1
  26. package/dist/functions/beam/layout.js +16 -24
  27. package/dist/functions/br/index.d.ts +34 -2
  28. package/dist/functions/br/index.js +8 -8
  29. package/dist/functions/dash/index.js +6 -6
  30. package/dist/functions/default.js +10 -5
  31. package/dist/functions/div/index.d.ts +1 -0
  32. package/dist/functions/div/index.js +8 -4
  33. package/dist/functions/dot/index.js +2 -2
  34. package/dist/functions/dyn/index.js +49 -31
  35. package/dist/functions/gliss/index.d.ts +2 -0
  36. package/dist/functions/gliss/index.js +202 -0
  37. package/dist/functions/grace/index.d.ts +1 -7
  38. package/dist/functions/grace/index.js +11 -19
  39. package/dist/functions/head/index.js +41 -60
  40. package/dist/functions/key/index.js +8 -10
  41. package/dist/functions/meter/index.d.ts +56 -1
  42. package/dist/functions/meter/index.js +30 -24
  43. package/dist/functions/note/accidentals.js +10 -16
  44. package/dist/functions/note/index.js +18 -8
  45. package/dist/functions/note/noteName.d.ts +16 -0
  46. package/dist/functions/note/noteName.js +88 -0
  47. package/dist/functions/note/source-transform.js +6 -5
  48. package/dist/functions/ottava/index.d.ts +2 -0
  49. package/dist/functions/ottava/index.js +233 -0
  50. package/dist/functions/page/index.d.ts +1 -0
  51. package/dist/functions/page/index.js +22 -13
  52. package/dist/functions/pagebreak/index.d.ts +2 -0
  53. package/dist/functions/pagebreak/index.js +36 -0
  54. package/dist/functions/rest/index.js +15 -15
  55. package/dist/functions/set/index.js +4 -4
  56. package/dist/functions/slide/index.d.ts +2 -0
  57. package/dist/functions/slide/index.js +132 -0
  58. package/dist/functions/stack/index.js +8 -15
  59. package/dist/functions/symbol/index.d.ts +30 -2
  60. package/dist/functions/symbol/index.js +93 -19
  61. package/dist/functions/symbol/ornament.d.ts +1 -2
  62. package/dist/functions/symbol/ornament.js +14 -19
  63. package/dist/functions/symbol/shape.d.ts +6 -0
  64. package/dist/functions/symbol/shape.js +20 -4
  65. package/dist/functions/symbol/symbols/coda.d.ts +4 -0
  66. package/dist/functions/symbol/symbols/coda.js +56 -0
  67. package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
  68. package/dist/functions/symbol/symbols/dynamics.js +18 -8
  69. package/dist/functions/symbol/symbols/fine.js +1 -1
  70. package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
  71. package/dist/functions/symbol/symbols/harmonic.js +11 -0
  72. package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
  73. package/dist/functions/symbol/symbols/marcato.js +22 -0
  74. package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
  75. package/dist/functions/symbol/symbols/staccato.js +24 -0
  76. package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
  77. package/dist/functions/symbol/symbols/tenuto.js +11 -0
  78. package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
  79. package/dist/functions/symbol/symbols/tocoda.js +16 -0
  80. package/dist/functions/symbol/symbols/tr.js +46 -1
  81. package/dist/functions/symbol/symbols/turn.d.ts +3 -0
  82. package/dist/functions/symbol/symbols/turn.js +50 -0
  83. package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
  84. package/dist/functions/symbol/symbols/wheel.js +36 -0
  85. package/dist/functions/temporal.d.ts +17 -6
  86. package/dist/functions/temporal.js +17 -6
  87. package/dist/functions/text/index.js +15 -6
  88. package/dist/functions/tie/index.js +64 -30
  89. package/dist/functions/tremolo/index.d.ts +2 -0
  90. package/dist/functions/tremolo/index.js +319 -0
  91. package/dist/functions/tuplet/index.js +4 -8
  92. package/dist/functions/up/index.js +7 -9
  93. package/dist/functions/voice/connections.js +7 -5
  94. package/dist/functions/voice/index.js +19 -11
  95. package/dist/functions/volta/index.js +41 -21
  96. package/dist/jpfun.core.min.js +64 -80
  97. package/dist/jpfun.from-midi.min.js +5 -5
  98. package/dist/jpfun.from-musicxml.min.js +8 -8
  99. package/dist/jpfun.min.js +69 -85
  100. package/dist/layout/default.d.ts +0 -1
  101. package/dist/layout/default.js +0 -1
  102. package/dist/layout/engine.d.ts +1 -0
  103. package/dist/layout/engine.js +42 -11
  104. package/dist/layout/page.d.ts +3 -2
  105. package/dist/layout/page.js +6 -5
  106. package/dist/layout/types.d.ts +4 -0
  107. package/dist/lowering/loweringContext.d.ts +11 -13
  108. package/dist/lowering/loweringContext.js +30 -197
  109. package/dist/lowering/types.d.ts +1 -3
  110. package/dist/parser/parse-utils/note-utils.d.ts +1 -1
  111. package/dist/parser/parse-utils/note-utils.js +8 -9
  112. package/dist/parser/parse-utils/string-utils.d.ts +3 -2
  113. package/dist/parser/parse-utils/string-utils.js +16 -3
  114. package/dist/parser/parserContext.d.ts +2 -0
  115. package/dist/parser/parserContext.js +36 -8
  116. package/dist/pipeline.js +0 -1
  117. package/dist/playback/compile.js +131 -72
  118. package/dist/playback/event.d.ts +1 -1
  119. package/dist/playback/event.js +9 -9
  120. package/dist/playback/time.js +42 -31
  121. package/dist/playback/types.d.ts +7 -3
  122. package/dist/render/canvas.d.ts +2 -0
  123. package/dist/render/canvas.js +25 -22
  124. package/dist/render/recording.js +1 -4
  125. package/dist/render/svg.js +1 -0
  126. package/dist/render/text.d.ts +1 -1
  127. package/dist/render/text.js +3 -2
  128. package/dist/render/translate.d.ts +0 -1
  129. package/dist/render/translate.js +6 -23
  130. package/dist/render/types.d.ts +1 -0
  131. package/grammar.md +52 -25
  132. package/package.json +3 -3
  133. package/dist/functions/note/noteNameFSM.d.ts +0 -8
  134. package/dist/functions/note/noteNameFSM.js +0 -181
@@ -1,7 +1,17 @@
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 { DEFAULT_BPM } from "../../functions/temporal.js";
1
12
  import { identifyTriplets } from "./triplet.js";
2
13
  import { attachAbove, quote, renderHead, renderPitch, renderSystems } from "../source.js";
3
14
  import { SHARP_NOTE_NAMES as ABSOLUTE_NAMES } from "../../parser/parse-utils/note-utils.js";
4
- const DEFAULT_BPM = 120;
5
15
  const DEFAULT_TIME_SIGNATURE = [4, 4];
6
16
  const AUTO_LINE_TARGET_RATIO = 1.03;
7
17
  const AUTO_LINE_LIMIT_RATIO = 1.25;
@@ -263,12 +273,12 @@ function automaticLineBreaks(source, bars, scale, capability) {
263
273
  // 超宽页面关闭自动换行,从真实布局对象取得每个小节的自然宽度
264
274
  const naturalWidth = Math.max(1_000_000, source.length * 64);
265
275
  const naturalSource = `@page(width=${naturalWidth}px, height=0px, top=0px, bottom=0px, left=0px, right=0px)\n${source}`;
266
- const layout = capability.compileScore(naturalSource).layout;
267
- const bodyLine = Math.max(...layout.objects.filter(node => !node.T.isZero()).map(node => node.layoutLine));
268
- const bodyObjects = layout.objects.filter(node => node.layoutLine === bodyLine && node.box.w > 0);
276
+ const layout = capability.compileScore(naturalSource, { horizontalOnly: true }).layout;
269
277
  const measureBounds = bars.map(() => ({ left: Infinity, right: -Infinity }));
270
- // 按对象起点归入小节,小节线本身属于左侧刚结束的小节
271
- for (const node of bodyObjects) {
278
+ // 测宽候选不含 head,所有正文可见对象均位于第 0 行;按起点归入小节,小节线归入左侧刚结束的小节
279
+ for (const node of layout.objects) {
280
+ if (node.box.w <= 0)
281
+ continue;
272
282
  const at = Math.round(node.t.toNumber() * scale);
273
283
  let low = 0;
274
284
  let high = bars.length;
@@ -340,20 +350,22 @@ function itemHead(item, pitchMode, suffix = "", labels) {
340
350
  * dash 只允许延续本小节内已经出现的音;跨小节或不足一拍时重写同音,
341
351
  * 每个和弦成员分别收集完整标签链,音结束时一次输出 @tie(x, y, ...)。
342
352
  */
343
- function renderLane(lane, rangeStart, rangeEnd, rhythmScale, timeFactor, pitchMode, bars, lineBreaks, adjustments, programs, nextTieLabel) {
353
+ function renderLane(lane, rangeStart, rangeEnd, lineSpan, rhythmScale, timeFactor, pitchMode, bars, regionBars, lineBreaks, adjustments, programs, nextTieLabel) {
344
354
  // 事件边界与所有控制点共同切分时间线,保证每个片段内语义不变
345
355
  const points = new Set([rangeStart, rangeEnd]);
346
356
  for (const item of lane.items) {
347
357
  points.add(item.start);
348
358
  points.add(item.end);
349
359
  }
350
- for (const at of bars)
351
- if (at > rangeStart && at < rangeEnd)
352
- points.add(at);
353
- for (const at of adjustments?.keys() ?? []) {
354
- if (at >= rangeStart && at < rangeEnd)
355
- points.add(at);
360
+ for (const at of regionBars)
361
+ points.add(at);
362
+ if (adjustments) {
363
+ for (const at of adjustments.keys()) {
364
+ if (at >= rangeStart && at < rangeEnd)
365
+ points.add(at);
366
+ }
356
367
  }
368
+ // 测宽也保留 program 控制点,避免持续音的切分与完整源码不同。
357
369
  for (const at of programs.keys()) {
358
370
  if (at >= rangeStart && at < rangeEnd)
359
371
  points.add(at);
@@ -368,28 +380,33 @@ function renderLane(lane, rangeStart, rangeEnd, rhythmScale, timeFactor, pitchMo
368
380
  return label;
369
381
  });
370
382
  // 预扫描找出不能只用 dash 延续的音符,提前为其建立逐音高标签链
371
- for (const item of lane.items) {
372
- if ("members" in item)
373
- continue;
374
- const startIndex = timelineIndex.get(item.start);
375
- for (let index = startIndex; timeline[index] < item.end; index++) {
376
- const suffixes = rhythmSuffixes((timeline[index + 1] - timeline[index]) / timeFactor, rhythmScale);
377
- if ((timeline[index] > item.start && bars.has(timeline[index]))
378
- || suffixes.some((suffix, suffixIndex) => (timeline[index] > item.start || suffixIndex > 0) && suffix.includes("/"))) {
379
- tieChains.set(item, item.notes.map(() => []));
380
- break;
383
+ if (nextTieLabel) {
384
+ for (const item of lane.items) {
385
+ if ("members" in item)
386
+ continue;
387
+ const startIndex = timelineIndex.get(item.start);
388
+ for (let index = startIndex; timeline[index] < item.end; index++) {
389
+ const suffixes = rhythmSuffixes((timeline[index + 1] - timeline[index]) / timeFactor, rhythmScale);
390
+ if ((timeline[index] > item.start && bars.has(timeline[index]))
391
+ || suffixes.some((suffix, suffixIndex) => (timeline[index] > item.start || suffixIndex > 0) && suffix.includes("/"))) {
392
+ tieChains.set(item, item.notes.map(() => []));
393
+ break;
394
+ }
381
395
  }
382
396
  }
383
397
  }
384
- const outputs = Array.from({ length: lineBreaks.size + 1 }, () => []);
385
- let outputIndex = [...lineBreaks].filter(at => at <= rangeStart).length;
398
+ const outputs = Array.from({ length: lineSpan }, () => []);
399
+ let outputIndex = 0;
386
400
  let itemIndex = 0;
387
- let initialProgram = programs.size ? 0 : undefined;
388
- let programStart = -Infinity;
389
- for (const [at, program] of programs) {
390
- if (at <= rangeStart && at >= programStart) {
391
- initialProgram = program;
392
- programStart = at;
401
+ let initialProgram;
402
+ if (nextTieLabel && programs.size) {
403
+ initialProgram = 0;
404
+ let programStart = -Infinity;
405
+ for (const [at, program] of programs) {
406
+ if (at <= rangeStart && at >= programStart) {
407
+ initialProgram = program;
408
+ programStart = at;
409
+ }
393
410
  }
394
411
  }
395
412
  // 沿时间片单向推进当前事件,依次输出控制项、音符或补齐休止
@@ -404,9 +421,11 @@ function renderLane(lane, rangeStart, rangeEnd, rhythmScale, timeFactor, pitchMo
404
421
  const next = timeline[index + 1];
405
422
  if (next === undefined)
406
423
  continue;
407
- const program = at === rangeStart ? initialProgram : programs.get(at);
408
- if (program !== undefined)
409
- outputs[outputIndex].push(`@program(${program})`);
424
+ if (nextTieLabel) {
425
+ const program = at === rangeStart ? initialProgram : programs.get(at);
426
+ if (program !== undefined)
427
+ outputs[outputIndex].push(`@program(${program})`);
428
+ }
410
429
  const changes = adjustments?.get(at) ?? [];
411
430
  const item = lane.items[itemIndex];
412
431
  if (item && item.start <= at && item.end >= next) {
@@ -426,25 +445,11 @@ function renderLane(lane, rangeStart, rangeEnd, rhythmScale, timeFactor, pitchMo
426
445
  for (let suffixIndex = 0; suffixIndex < suffixes.length; suffixIndex++) {
427
446
  const suffix = suffixes[suffixIndex];
428
447
  const continuation = at > item.start || suffixIndex > 0;
429
- let token;
430
448
  const chains = tieChains.get(item);
431
- if (!continuation) {
432
- if (chains) {
433
- token = itemHead(item, pitchMode, suffix, extendTieChains(chains));
434
- }
435
- else {
436
- token = itemHead(item, pitchMode, suffix);
437
- }
438
- }
439
- else if ((suffixIndex === 0 && bars.has(at)) || suffix.includes("/")) {
440
- token = itemHead(item, pitchMode, suffix, extendTieChains(chains));
441
- }
442
- else {
443
- token = `-${suffix}`;
444
- }
445
- if (suffixIndex === 0)
446
- token = attachAbove(token, changes);
447
- outputs[outputIndex].push(token);
449
+ const token = !continuation || (suffixIndex === 0 && bars.has(at)) || suffix.includes("/")
450
+ ? itemHead(item, pitchMode, suffix, chains && extendTieChains(chains))
451
+ : `-${suffix}`;
452
+ outputs[outputIndex].push(suffixIndex === 0 ? attachAbove(token, changes) : token);
448
453
  if (chains && next === item.end && suffixIndex === suffixes.length - 1) {
449
454
  outputs[outputIndex].push(...chains.map(labels => `@tie(${labels.join(", ")})`));
450
455
  }
@@ -458,7 +463,7 @@ function renderLane(lane, rangeStart, rangeEnd, rhythmScale, timeFactor, pitchMo
458
463
  }
459
464
  return outputs.map(output => output.join(" "));
460
465
  }
461
- function renderTrack(track, scoreEnd, bars, lineBreaks, render, showName) {
466
+ function renderTrack(track, scoreEnd, bars, orderedBars, lineBreaks, render, showName) {
462
467
  const lineCount = lineBreaks.size + 1;
463
468
  const outputs = Array.from({ length: lineCount }, () => []);
464
469
  const segments = [];
@@ -472,21 +477,36 @@ function renderTrack(track, scoreEnd, bars, lineBreaks, render, showName) {
472
477
  }
473
478
  if (cursor < scoreEnd)
474
479
  segments.push({ start: cursor, end: scoreEnd, lanes: [{ items: [] }] });
475
- // 每个重叠区域独立渲染,多 lane 只在该区域局部组成 @stack
480
+ // segments 按时间递增且连续,小节线与行号可沿游标单向推进
481
+ let barCursor = 0;
482
+ let lineCursor = 0;
476
483
  for (const region of segments) {
477
484
  if (region.start > 0 && bars.has(region.start)) {
478
- const lineIndex = [...lineBreaks].filter(at => at < region.start).length;
479
- outputs[lineIndex].push("|");
485
+ outputs[lineCursor].push("|");
486
+ if (lineBreaks.has(region.start))
487
+ lineCursor++;
488
+ }
489
+ while (barCursor < orderedBars.length && orderedBars[barCursor] <= region.start)
490
+ barCursor++;
491
+ const barStart = barCursor;
492
+ let lineEnd = lineCursor;
493
+ while (barCursor < orderedBars.length && orderedBars[barCursor] < region.end) {
494
+ if (lineBreaks.has(orderedBars[barCursor]))
495
+ lineEnd++;
496
+ barCursor++;
480
497
  }
481
- const lanes = region.lanes.map(lane => render(region, lane));
482
- for (let lineIndex = 0; lineIndex < lineCount; lineIndex++) {
483
- if (!lanes.some(lines => lines[lineIndex]))
498
+ const regionBars = orderedBars.slice(barStart, barCursor);
499
+ const lineSpan = lineEnd - lineCursor + 1;
500
+ const lanes = region.lanes.map(lane => render(region, lane, regionBars, lineSpan));
501
+ for (let offset = 0; offset < lineSpan; offset++) {
502
+ if (!lanes.some(lines => lines[offset]))
484
503
  continue;
485
504
  const content = lanes.length === 1
486
- ? lanes[0][lineIndex]
487
- : `@stack(${lanes.map(lines => `{ ${lines[lineIndex]} }`).join(", ")})`;
488
- outputs[lineIndex].push(content);
505
+ ? lanes[0][offset]
506
+ : `@stack(${lanes.map(lines => `{ ${lines[offset]} }`).join(", ")})`;
507
+ outputs[lineCursor + offset].push(content);
489
508
  }
509
+ lineCursor = lineEnd;
490
510
  }
491
511
  if (bars.has(scoreEnd))
492
512
  outputs[lineCount - 1].push("|");
@@ -693,15 +713,17 @@ export function convertMidiJsonToJpFun(input, options = {}, layoutCapability) {
693
713
  });
694
714
  if (convertedTracks.length === 0)
695
715
  return head;
696
- // 第六阶段:先生成单行候选做自然宽度测量,再用选出的断点生成最终源码
716
+ // 第六阶段:先用不含 head/tie/program 的单行候选测自然宽度,再用选出的断点生成完整源码
697
717
  const renderScore = (lineBreaks) => {
698
718
  let tieIndex = 0;
699
- // 每次渲染重新编号 tie,单行测量与最终源码因此保持相同标签结构
700
- const tracks = convertedTracks.map((track, trackIndex) => renderTrack(track, scoreEnd, bars, lineBreaks, (region, lane) => renderLane(lane, region.start, region.end, rhythmScale, timeFactor, pitchMode, bars, lineBreaks, trackIndex === 0 && lane === region.lanes.at(-1) ? scoreAdjustments : undefined, track.programs, () => `mt${tieIndex++}`), convertedTracks.length > 1));
701
- return `${head}\n\n${renderSystems(tracks)}`;
719
+ const nextTieLabel = lineBreaks ? () => `mt${tieIndex++}` : undefined;
720
+ const breaks = lineBreaks ?? new Set();
721
+ const tracks = convertedTracks.map((track, trackIndex) => renderTrack(track, scoreEnd, bars, orderedBars, breaks, (region, lane, regionBars, lineSpan) => renderLane(lane, region.start, region.end, lineSpan, rhythmScale, timeFactor, pitchMode, bars, regionBars, breaks, trackIndex === 0 && lane === region.lanes.at(-1) ? scoreAdjustments : undefined, track.programs, nextTieLabel), convertedTracks.length > 1));
722
+ const body = renderSystems(tracks);
723
+ return lineBreaks ? `${head}\n\n${body}` : body;
702
724
  };
703
725
  const lineBreaks = barsPerLine <= 0
704
- ? automaticLineBreaks(renderScore(new Set()), orderedBars, scale, layoutCapability)
726
+ ? automaticLineBreaks(renderScore(), orderedBars, scale, layoutCapability)
705
727
  : new Set(orderedBars.filter((at, index) => (index + 1) % barsPerLine === 0 && at < scoreEnd));
706
728
  return renderScore(lineBreaks);
707
729
  }
@@ -1,10 +1,12 @@
1
1
  import { type MusicXmlElement } from "./dom.js";
2
- import type { MusicXmlArpeggio, MusicXmlDiagnostic, MusicXmlEvent, MusicXmlPitch, ParsedMusicXmlScore } from "./model.js";
2
+ import type { MusicXmlArpeggio, MusicXmlDiagnostic, MusicXmlEvent, MusicXmlPitch, MusicXmlRepeatPoint, MusicXmlRepeatSymbol, ParsedMusicXmlScore } from "./model.js";
3
3
  /** 统一两种 MusicXML 根结构中的小节内容与外层小节容器 */
4
4
  export interface MusicXmlMeasureSource {
5
5
  body: MusicXmlElement;
6
6
  container: MusicXmlElement;
7
7
  }
8
+ /** 识别 <words> 中的标准反复跳转与终止术语 */
9
+ export declare function repeatWordSymbol(value: string): MusicXmlRepeatSymbol | undefined;
8
10
  declare const DIAGNOSTICS: {
9
11
  readonly percussion: readonly ["PERCUSSION_SKIPPED", "MIDI channel 10 percussion note was omitted."];
10
12
  readonly displayPitch: readonly ["APPROXIMATED", "Unpitched note was converted to a melodic note using its display pitch."];
@@ -17,6 +19,7 @@ declare const DIAGNOSTICS: {
17
19
  readonly directionOutput: readonly ["UNATTACHED_CONTENT", "Direction mark has no generated output position and was omitted."];
18
20
  readonly tempoReplaced: readonly ["UNATTACHED_CONTENT", "Tempo {value} was replaced by a different tempo at the same time and was omitted."];
19
21
  readonly arpeggioOutput: readonly ["UNSUPPORTED_ELEMENT", "Arpeggiate mark requires at least two pitches in one event and was omitted."];
22
+ readonly multipleRest: readonly ["APPROXIMATED", "Multiple-rest grouping could not be preserved without discarding musical content; individual rests were retained."];
20
23
  readonly textHost: readonly ["UNATTACHED_CONTENT", "Text {value} has no target event and was omitted."];
21
24
  readonly wedgeStart: readonly ["UNRESOLVED_RELATION", "Wedge stop has no matching start and was omitted."];
22
25
  readonly wedgeReplaced: readonly ["UNRESOLVED_RELATION", "Wedge start was replaced before a matching stop and was omitted."];
@@ -29,6 +32,12 @@ declare const DIAGNOSTICS: {
29
32
  readonly endingEndpoints: readonly ["UNRESOLVED_RELATION", "Ending has no event endpoints in its interval and was omitted."];
30
33
  readonly tieStart: readonly ["UNRESOLVED_RELATION", "Tie stop has no matching start and was omitted."];
31
34
  readonly tieStop: readonly ["UNRESOLVED_RELATION", "Tie start has no matching stop in any ending branch and was omitted."];
35
+ readonly glissStart: readonly ["UNRESOLVED_RELATION", "Glissando stop has no matching start and was omitted."];
36
+ readonly glissStop: readonly ["UNRESOLVED_RELATION", "Glissando start has no matching stop and was omitted."];
37
+ readonly glissReplaced: readonly ["UNRESOLVED_RELATION", "Glissando start was replaced before a matching stop and was omitted."];
38
+ readonly glissEndpoints: readonly ["UNRESOLVED_RELATION", "Glissando endpoints are simultaneous or cross a score line and were omitted."];
39
+ readonly glissStyle: readonly ["APPROXIMATED", "MusicXML <{value}> was converted to a straight glissando arrow."];
40
+ readonly symbolStyle: readonly ["APPROXIMATED", "MusicXML <{value}> was mapped to the default symbol shape."];
32
41
  };
33
42
  export declare function reportDiagnostic(diagnostics: MusicXmlDiagnostic[], issue: keyof typeof DIAGNOSTICS, location: MusicXmlDiagnostic["location"], value?: string | number): void;
34
43
  /** 查询不改变覆盖状态;解析结果写入语义模型后显式确认,未确认项按 XML 顺序报告 */
@@ -53,14 +62,22 @@ export declare function scoreMetadata(root: MusicXmlElement): Pick<ParsedMusicXm
53
62
  /** 解析书面音高及该和弦成员自己的 tie 起止标记 */
54
63
  export declare function parsePitch(note: MusicXmlElement, location: MusicXmlPitch["location"], notations?: MusicXmlReader): MusicXmlPitch | null;
55
64
  export declare function transposePitch(pitch: MusicXmlPitch, chromatic: number, diatonic: number | undefined, octaves: number): void;
56
- /** 将支持的 fermata articulation 和 ornament 转成 jpFun 修饰符 */
65
+ /** 将支持的延长、奏法与装饰音记号转成 jpFun 修饰符 */
57
66
  export declare function noteModifiers(notations: MusicXmlReader | undefined, diagnostics: MusicXmlDiagnostic[], location: MusicXmlDiagnostic["location"]): {
58
- name: string;
59
- placement: "above" | "below";
60
- }[];
61
- /** 无箭头不覆盖显式方向,两个相反的显式方向不能属于同一和弦。 */
67
+ modifiers: {
68
+ name: string;
69
+ placement: "above" | "below";
70
+ }[];
71
+ slides: {
72
+ side: "pre" | "post";
73
+ direction: "up" | "down";
74
+ }[];
75
+ };
76
+ /** 双音 slide 与 glissando 均是标签关系;与 articulations 下的单音滑音区别处理 */
77
+ export declare function noteGlissandi(notations: MusicXmlReader | undefined, pitch: MusicXmlPitch | null, diagnostics: MusicXmlDiagnostic[]): void;
78
+ /** 无箭头不覆盖显式方向,两个相反的显式方向不能属于同一和弦 */
62
79
  export declare function mergeArpeggio(current?: MusicXmlArpeggio, next?: MusicXmlArpeggio): MusicXmlArpeggio | undefined;
63
- /** 读取和弦成员上的琶音方向;none 表示无箭头但仍按自下而上演奏。 */
80
+ /** 读取和弦成员上的琶音方向;none 表示无箭头但仍按自下而上演奏 */
64
81
  export declare function noteArpeggio(notations: MusicXmlReader | undefined, location: MusicXmlPitch["location"]): MusicXmlArpeggio | undefined;
65
82
  /** 解析 tuplet 的实际音数与正常音数,并保留显式组边界 */
66
83
  export declare function timeModification(note: MusicXmlElement, tuplets: readonly MusicXmlElement[] | undefined): {
@@ -75,11 +92,16 @@ export declare function acceptTuplets(notations: MusicXmlReader | undefined, tup
75
92
  export declare function noteLyrics(note: MusicXmlElement): Map<string, string>;
76
93
  /** 返回 dynamics 中首个受支持的力度记号 */
77
94
  export declare function directionDynamic(dynamics: MusicXmlElement | undefined, diagnostics: MusicXmlDiagnostic[], location: MusicXmlDiagnostic["location"]): string | undefined;
78
- /** 收集排练标记与普通文字,排练标记保留方框语义;空白文字视为空内容 */
79
- export declare function directionTexts(direction: MusicXmlReader): {
80
- text: string;
81
- boxed: boolean;
82
- }[];
95
+ /** 收集排练标记、反复记号与普通文字,排练标记保留方框语义;空白文字视为空内容 */
96
+ export declare function directionTexts(direction: MusicXmlReader, defaultPlacement?: "above" | "below", location?: MusicXmlDiagnostic["location"]): {
97
+ texts: {
98
+ text: string;
99
+ boxed: boolean;
100
+ }[];
101
+ repeats: Omit<MusicXmlRepeatPoint, "at">[];
102
+ };
103
+ /** 读取 <sound> 上的反复与跳转属性 */
104
+ export declare function soundRepeats(sound: MusicXmlElement | undefined, placement: "above" | "below", location: MusicXmlDiagnostic["location"]): Omit<MusicXmlRepeatPoint, "at">[];
83
105
  /** 将 metronome 的拍单位和附点折算成四分音符 BPM */
84
106
  export declare function metronomeBpm(metronome: MusicXmlElement | undefined): number | undefined;
85
107
  /** 将单一或复合拍号归一成一个等值分数 */
@@ -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."],
@@ -14,6 +35,7 @@ const DIAGNOSTICS = {
14
35
  directionOutput: ["UNATTACHED_CONTENT", "Direction mark has no generated output position and was omitted."],
15
36
  tempoReplaced: ["UNATTACHED_CONTENT", "Tempo {value} was replaced by a different tempo at the same time and was omitted."],
16
37
  arpeggioOutput: ["UNSUPPORTED_ELEMENT", "Arpeggiate mark requires at least two pitches in one event and was omitted."],
38
+ multipleRest: ["APPROXIMATED", "Multiple-rest grouping could not be preserved without discarding musical content; individual rests were retained."],
17
39
  textHost: ["UNATTACHED_CONTENT", "Text {value} has no target event and was omitted."],
18
40
  wedgeStart: ["UNRESOLVED_RELATION", "Wedge stop has no matching start and was omitted."],
19
41
  wedgeReplaced: ["UNRESOLVED_RELATION", "Wedge start was replaced before a matching stop and was omitted."],
@@ -26,6 +48,12 @@ const DIAGNOSTICS = {
26
48
  endingEndpoints: ["UNRESOLVED_RELATION", "Ending has no event endpoints in its interval and was omitted."],
27
49
  tieStart: ["UNRESOLVED_RELATION", "Tie stop has no matching start and was omitted."],
28
50
  tieStop: ["UNRESOLVED_RELATION", "Tie start has no matching stop in any ending branch and was omitted."],
51
+ glissStart: ["UNRESOLVED_RELATION", "Glissando stop has no matching start and was omitted."],
52
+ glissStop: ["UNRESOLVED_RELATION", "Glissando start has no matching stop and was omitted."],
53
+ glissReplaced: ["UNRESOLVED_RELATION", "Glissando start was replaced before a matching stop and was omitted."],
54
+ glissEndpoints: ["UNRESOLVED_RELATION", "Glissando endpoints are simultaneous or cross a score line and were omitted."],
55
+ glissStyle: ["APPROXIMATED", "MusicXML <{value}> was converted to a straight glissando arrow."],
56
+ symbolStyle: ["APPROXIMATED", "MusicXML <{value}> was mapped to the default symbol shape."],
29
57
  };
30
58
  export function reportDiagnostic(diagnostics, issue, location, value = "") {
31
59
  const [kind, message] = DIAGNOSTICS[issue];
@@ -195,46 +223,90 @@ export function transposePitch(pitch, chromatic, diatonic, octaves) {
195
223
  pitch.alter = midi - (pitch.octave + 1) * 12 - NoteNameMap[pitch.step];
196
224
  }
197
225
  }
198
- /** 将支持的 fermata articulation 和 ornament 转成 jpFun 修饰符 */
226
+ /** 将支持的延长、奏法与装饰音记号转成 jpFun 修饰符 */
199
227
  export function noteModifiers(notations, diagnostics, location) {
200
- const result = [];
228
+ const modifiers = [];
229
+ const slides = [];
201
230
  if (!notations)
202
- return result;
231
+ return { modifiers, slides };
203
232
  const fermata = notations.read("fermata");
204
233
  if (fermata) {
205
- result.push({ name: "fermata", placement: placementOf(fermata, fermata.getAttribute("type") === "inverted" ? "below" : "above") });
234
+ modifiers.push({ name: "fermata", placement: placementOf(fermata, fermata.getAttribute("type") === "inverted" ? "below" : "above") });
206
235
  notations.accept(fermata);
207
236
  }
208
237
  const articulations = notations.read("articulations");
209
238
  if (articulations) {
210
- const accent = notations.read("accent", false, articulations);
211
- if (accent) {
212
- result.push({ name: "accent", placement: placementOf(accent, "above") });
213
- 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
+ }
251
+ }
252
+ for (const [tag, side, direction] of [
253
+ ["scoop", "pre", "up"], ["plop", "pre", "down"],
254
+ ["doit", "post", "up"], ["falloff", "post", "down"],
255
+ ]) {
256
+ for (const articulation of children(articulations, tag)) {
257
+ slides.push({ side, direction });
258
+ notations.accept(articulation);
259
+ if (articulation.getAttribute("line-shape") === "curved"
260
+ || articulation.getAttribute("line-type") === "wavy") {
261
+ reportDiagnostic(diagnostics, "glissStyle", { ...location, element: tag }, tag);
262
+ }
263
+ }
214
264
  }
215
265
  notations.report(diagnostics, location, articulations);
216
266
  notations.accept(articulations);
217
267
  }
218
268
  const ornaments = notations.read("ornaments");
219
269
  if (ornaments) {
220
- const names = [
270
+ for (const [tag, name] of [
221
271
  ["trill-mark", "tr"],
222
272
  ["mordent", "mordent"],
223
273
  ["inverted-mordent", "prall"],
224
- ];
225
- for (const [tag, name] of names) {
274
+ ["turn", "turn"],
275
+ ["inverted-turn", "iturn"],
276
+ ]) {
226
277
  const ornament = notations.read(tag, false, ornaments);
227
- if (ornament) {
228
- result.push({ name, placement: placementOf(ornament, "above") });
229
- 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);
230
284
  }
231
285
  }
232
286
  notations.report(diagnostics, location, ornaments);
233
287
  notations.accept(ornaments);
234
288
  }
235
- return result;
289
+ return { modifiers, slides };
236
290
  }
237
- /** 无箭头不覆盖显式方向,两个相反的显式方向不能属于同一和弦。 */
291
+ /** 双音 slide 与 glissando 均是标签关系;与 articulations 下的单音滑音区别处理 */
292
+ export function noteGlissandi(notations, pitch, diagnostics) {
293
+ if (!notations || !pitch)
294
+ return;
295
+ for (const kind of ["slide", "glissando"]) {
296
+ for (const element of notations.readAll(kind)) {
297
+ const type = element.getAttribute("type");
298
+ if (type !== "start" && type !== "stop")
299
+ continue;
300
+ (pitch.glissandi ??= []).push({ kind, type, number: element.getAttribute("number") || "1" });
301
+ notations.accept(element);
302
+ if (type === "start" && (kind === "glissando" && element.getAttribute("line-type") !== "solid"
303
+ || element.getAttribute("line-type") === "wavy" || text(element))) {
304
+ reportDiagnostic(diagnostics, "glissStyle", { ...pitch.location, element: kind }, kind);
305
+ }
306
+ }
307
+ }
308
+ }
309
+ /** 无箭头不覆盖显式方向,两个相反的显式方向不能属于同一和弦 */
238
310
  export function mergeArpeggio(current, next) {
239
311
  if (!next || next.direction === "none")
240
312
  return current ?? next;
@@ -243,7 +315,7 @@ export function mergeArpeggio(current, next) {
243
315
  }
244
316
  return next;
245
317
  }
246
- /** 读取和弦成员上的琶音方向;none 表示无箭头但仍按自下而上演奏。 */
318
+ /** 读取和弦成员上的琶音方向;none 表示无箭头但仍按自下而上演奏 */
247
319
  export function noteArpeggio(notations, location) {
248
320
  if (!notations)
249
321
  return undefined;
@@ -306,24 +378,59 @@ export function directionDynamic(dynamics, diagnostics, location) {
306
378
  let dynamic;
307
379
  for (const element of children(dynamics)) {
308
380
  const name = nameOf(element);
309
- if (!dynamic && DYNAMIC_NAMES.has(name))
381
+ if (!dynamic && DYNAMIC_NAME.test(name))
310
382
  dynamic = name;
311
383
  else
312
384
  reportDiagnostic(diagnostics, "unsupported", { ...location, element: name }, name);
313
385
  }
314
386
  return dynamic;
315
387
  }
316
- /** 收集排练标记与普通文字,排练标记保留方框语义;空白文字视为空内容 */
317
- export function directionTexts(direction) {
318
- 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
+ }
319
402
  for (const name of ["rehearsal", "words"]) {
320
403
  for (const node of direction.readAll(name, true)) {
321
404
  const value = node.textContent?.trim();
322
- if (value)
323
- 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
+ }
324
418
  direction.accept(node);
325
419
  }
326
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
+ }
327
434
  return result;
328
435
  }
329
436
  /** 将 metronome 的拍单位和附点折算成四分音符 BPM */