smoosic 2.0.3 → 2.0.4

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 (245) hide show
  1. package/build/smoosic.js +395 -67
  2. package/changes.md +85 -0
  3. package/package.json +98 -82
  4. package/release/smoosic.js +395 -67
  5. package/specs/032-split-multimeasure-rests/checklists/requirements.md +40 -0
  6. package/specs/032-split-multimeasure-rests/contracts/grouping-contract.md +53 -0
  7. package/specs/032-split-multimeasure-rests/data-model.md +70 -0
  8. package/specs/032-split-multimeasure-rests/plan.md +81 -0
  9. package/specs/032-split-multimeasure-rests/quickstart.md +46 -0
  10. package/specs/032-split-multimeasure-rests/research.md +63 -0
  11. package/specs/032-split-multimeasure-rests/spec.md +127 -0
  12. package/specs/032-split-multimeasure-rests/tasks.md +174 -0
  13. package/specs/033-key-distance-transposition/checklists/requirements.md +35 -0
  14. package/specs/033-key-distance-transposition/contracts/spelling-api.md +36 -0
  15. package/specs/033-key-distance-transposition/data-model.md +49 -0
  16. package/specs/033-key-distance-transposition/plan.md +87 -0
  17. package/specs/033-key-distance-transposition/quickstart.md +40 -0
  18. package/specs/033-key-distance-transposition/research.md +82 -0
  19. package/specs/033-key-distance-transposition/spec.md +132 -0
  20. package/specs/033-key-distance-transposition/tasks.md +139 -0
  21. package/specs/034-smz-compressed-scores/checklists/requirements.md +35 -0
  22. package/specs/034-smz-compressed-scores/contracts/archive-api.md +30 -0
  23. package/specs/034-smz-compressed-scores/data-model.md +48 -0
  24. package/specs/034-smz-compressed-scores/plan.md +89 -0
  25. package/specs/034-smz-compressed-scores/quickstart.md +44 -0
  26. package/specs/034-smz-compressed-scores/research.md +86 -0
  27. package/specs/034-smz-compressed-scores/spec.md +137 -0
  28. package/specs/034-smz-compressed-scores/tasks.md +148 -0
  29. package/specs/035-paste-special/checklists/requirements.md +35 -0
  30. package/specs/035-paste-special/contracts/paste-special-api.md +50 -0
  31. package/specs/035-paste-special/data-model.md +47 -0
  32. package/specs/035-paste-special/plan.md +83 -0
  33. package/specs/035-paste-special/quickstart.md +51 -0
  34. package/specs/035-paste-special/research.md +97 -0
  35. package/specs/035-paste-special/spec.md +170 -0
  36. package/specs/035-paste-special/tasks.md +149 -0
  37. package/specs/036-autosave-recovery/checklists/requirements.md +36 -0
  38. package/specs/036-autosave-recovery/contracts/autosave-api.md +111 -0
  39. package/specs/036-autosave-recovery/data-model.md +70 -0
  40. package/specs/036-autosave-recovery/plan.md +87 -0
  41. package/specs/036-autosave-recovery/quickstart.md +65 -0
  42. package/specs/036-autosave-recovery/research.md +105 -0
  43. package/specs/036-autosave-recovery/spec.md +143 -0
  44. package/specs/036-autosave-recovery/tasks.md +165 -0
  45. package/specs/037-fix-new-part-dialog/checklists/requirements.md +38 -0
  46. package/specs/037-fix-new-part-dialog/contracts/create-part-api.md +84 -0
  47. package/specs/037-fix-new-part-dialog/data-model.md +68 -0
  48. package/specs/037-fix-new-part-dialog/plan.md +83 -0
  49. package/specs/037-fix-new-part-dialog/quickstart.md +58 -0
  50. package/specs/037-fix-new-part-dialog/research.md +86 -0
  51. package/specs/037-fix-new-part-dialog/spec.md +144 -0
  52. package/specs/037-fix-new-part-dialog/tasks.md +174 -0
  53. package/specs/038-per-measure-beam-groups/checklists/requirements.md +36 -0
  54. package/specs/038-per-measure-beam-groups/contracts/beam-groups-api.md +74 -0
  55. package/specs/038-per-measure-beam-groups/data-model.md +75 -0
  56. package/specs/038-per-measure-beam-groups/plan.md +84 -0
  57. package/specs/038-per-measure-beam-groups/quickstart.md +53 -0
  58. package/specs/038-per-measure-beam-groups/research.md +72 -0
  59. package/specs/038-per-measure-beam-groups/spec.md +146 -0
  60. package/specs/038-per-measure-beam-groups/tasks.md +132 -0
  61. package/specs/039-measure-fill-submenu/checklists/requirements.md +35 -0
  62. package/specs/039-measure-fill-submenu/contracts/measure-fill-api.md +56 -0
  63. package/specs/039-measure-fill-submenu/data-model.md +79 -0
  64. package/specs/039-measure-fill-submenu/plan.md +81 -0
  65. package/specs/039-measure-fill-submenu/quickstart.md +62 -0
  66. package/specs/039-measure-fill-submenu/research.md +68 -0
  67. package/specs/039-measure-fill-submenu/spec.md +153 -0
  68. package/specs/039-measure-fill-submenu/tasks.md +121 -0
  69. package/specs/040-dynamics-dialog-cleanup/checklists/requirements.md +36 -0
  70. package/specs/040-dynamics-dialog-cleanup/contracts/dynamics-dialog.md +52 -0
  71. package/specs/040-dynamics-dialog-cleanup/data-model.md +48 -0
  72. package/specs/040-dynamics-dialog-cleanup/plan.md +89 -0
  73. package/specs/040-dynamics-dialog-cleanup/quickstart.md +69 -0
  74. package/specs/040-dynamics-dialog-cleanup/research.md +80 -0
  75. package/specs/040-dynamics-dialog-cleanup/spec.md +120 -0
  76. package/specs/040-dynamics-dialog-cleanup/tasks.md +114 -0
  77. package/specs/041-delete-special/checklists/requirements.md +35 -0
  78. package/specs/041-delete-special/contracts/delete-special-api.md +41 -0
  79. package/specs/041-delete-special/data-model.md +40 -0
  80. package/specs/041-delete-special/plan.md +93 -0
  81. package/specs/041-delete-special/quickstart.md +41 -0
  82. package/specs/041-delete-special/research.md +75 -0
  83. package/specs/041-delete-special/spec.md +136 -0
  84. package/specs/041-delete-special/tasks.md +106 -0
  85. package/src/application/application.ts +13 -2
  86. package/src/application/exports.ts +401 -395
  87. package/src/application/generateVexTests.ts +2 -2
  88. package/src/render/sui/NoteEntryCaret.ts +117 -16
  89. package/src/render/sui/autosave.ts +265 -0
  90. package/src/render/sui/formatter.ts +12 -2
  91. package/src/render/sui/scoreChangeState.ts +8 -3
  92. package/src/render/sui/scoreView.ts +47 -2
  93. package/src/render/sui/scoreViewOperations.ts +2706 -2555
  94. package/src/render/sui/staffLineGeometry.ts +82 -0
  95. package/src/render/vex/vxMeasure.ts +22 -8
  96. package/src/render/vex/vxNote.ts +2 -4
  97. package/src/smo/data/measure.ts +58 -1
  98. package/src/smo/data/measureModifiers.ts +81 -1
  99. package/src/smo/data/music.ts +169 -359
  100. package/src/smo/data/note.ts +1 -1
  101. package/src/smo/data/noteModifiers.ts +12 -0
  102. package/src/smo/data/score.ts +167 -2
  103. package/src/smo/data/scoreModifiers.ts +46 -1
  104. package/src/smo/data/systemStaff.ts +43 -0
  105. package/src/smo/xform/beamers.ts +80 -6
  106. package/src/smo/xform/copypaste.ts +207 -47
  107. package/src/smo/xform/measureFill.ts +89 -0
  108. package/src/smo/xform/operations.ts +403 -61
  109. package/src/smo/xform/selections.ts +7 -0
  110. package/src/ui/components/dialogs/beamGroups.vue +106 -0
  111. package/src/ui/components/dialogs/deleteSpecial.vue +38 -0
  112. package/src/ui/components/dialogs/dynamics.vue +16 -3
  113. package/src/ui/components/dialogs/fileUpload.vue +2 -2
  114. package/src/ui/components/dialogs/hairpin.vue +3 -3
  115. package/src/ui/components/dialogs/newPart.vue +148 -184
  116. package/src/ui/components/dialogs/pasteSpecial.vue +49 -0
  117. package/src/ui/components/dialogs/recovery.vue +66 -0
  118. package/src/ui/components/dialogs/save.vue +11 -4
  119. package/src/ui/components/dialogs/scorePreferences.vue +23 -1
  120. package/src/ui/components/dialogs/slur.vue +6 -6
  121. package/src/ui/components/dialogs/tempo.vue +11 -3
  122. package/src/ui/components/dialogs/timeSignature.vue +5 -0
  123. package/src/ui/components/dialogs/volta.vue +3 -3
  124. package/src/ui/dialogs/annotationVue.ts +1 -1
  125. package/src/ui/dialogs/beamGroupsVue.ts +69 -0
  126. package/src/ui/dialogs/createPartHelpers.ts +68 -0
  127. package/src/ui/dialogs/deleteSpecial.ts +57 -0
  128. package/src/ui/dialogs/dynamicsVue.ts +87 -60
  129. package/src/ui/dialogs/factory.ts +0 -2
  130. package/src/ui/dialogs/fileDialogs.ts +63 -22
  131. package/src/ui/dialogs/instrument.ts +37 -30
  132. package/src/ui/dialogs/pasteSpecial.ts +59 -0
  133. package/src/ui/dialogs/preferences.ts +41 -40
  134. package/src/ui/dialogs/recovery.ts +49 -0
  135. package/src/ui/fileio/fileInput.ts +12 -3
  136. package/src/ui/fileio/scoreArchive.ts +151 -0
  137. package/src/ui/fileio/xhrLoader.ts +26 -0
  138. package/src/ui/i18n/language.ts +1 -1
  139. package/src/ui/menus/beams.ts +181 -112
  140. package/src/ui/menus/edit.ts +44 -6
  141. package/src/ui/menus/file.ts +21 -1
  142. package/src/ui/menus/measure.ts +40 -1
  143. package/tests/autosave.ts +332 -0
  144. package/tests/beamDotted.ts +56 -0
  145. package/tests/beamGroups.ts +338 -0
  146. package/tests/createPart.ts +292 -0
  147. package/tests/deleteSpecial.ts +350 -0
  148. package/tests/dynamicsFontSize.ts +132 -0
  149. package/tests/keyDistanceTransposition.ts +351 -0
  150. package/tests/measureFill.ts +295 -0
  151. package/tests/movePart.ts +94 -0
  152. package/tests/multimeasureRest.ts +338 -0
  153. package/tests/pasteIntoMeasure.ts +103 -0
  154. package/tests/pasteSpecial.ts +541 -0
  155. package/tests/quickBeam.ts +126 -0
  156. package/tests/scoreArchive.ts +218 -0
  157. package/tests/scoreChangeState.ts +2 -0
  158. package/tests/staffLineGeometry.ts +179 -0
  159. package/tests/timeSignatureDisplay.ts +58 -0
  160. package/tools/smoosic-schema.json +37 -0
  161. package/typedoc.ts +1 -1
  162. package/types/src/application/exports.d.ts +453 -447
  163. package/types/src/common/promiseHelpers.d.ts +30 -30
  164. package/types/src/common/vex.d.ts +224 -224
  165. package/types/src/render/sui/NoteEntryCaret.d.ts +107 -107
  166. package/types/src/render/sui/autosave.d.ts +99 -0
  167. package/types/src/render/sui/scoreView.d.ts +23 -0
  168. package/types/src/render/sui/scoreViewOperations.d.ts +759 -639
  169. package/types/src/render/sui/svgHelpers.d.ts +88 -88
  170. package/types/src/render/sui/svgPageMap.d.ts +344 -344
  171. package/types/src/render/sui/textEdit.d.ts +96 -96
  172. package/types/src/render/sui/textRender.d.ts +199 -199
  173. package/types/src/render/vex/smoAdapter.d.ts +47 -47
  174. package/types/src/render/vex/vxMeasure.d.ts +97 -97
  175. package/types/src/render/vex/vxNote.d.ts +56 -56
  176. package/types/src/render/vex/vxSystem.d.ts +71 -71
  177. package/types/src/smo/data/measure.d.ts +30 -0
  178. package/types/src/smo/data/measureModifiers.d.ts +568 -513
  179. package/types/src/smo/data/music.d.ts +74 -32
  180. package/types/src/smo/data/note.d.ts +498 -498
  181. package/types/src/smo/data/noteModifiers.d.ts +615 -606
  182. package/types/src/smo/data/score.d.ts +476 -421
  183. package/types/src/smo/data/scoreModifiers.d.ts +30 -0
  184. package/types/src/smo/data/scoreText.d.ts +334 -334
  185. package/types/src/smo/data/systemStaff.d.ts +14 -0
  186. package/types/src/smo/xform/beamers.d.ts +23 -1
  187. package/types/src/smo/xform/copypaste.d.ts +138 -2
  188. package/types/src/smo/xform/measureFill.d.ts +21 -0
  189. package/types/src/smo/xform/operations.d.ts +245 -129
  190. package/types/src/smo/xform/selections.d.ts +154 -154
  191. package/types/src/smo/xform/undo.d.ts +142 -142
  192. package/types/src/ui/buttons/button.d.ts +100 -100
  193. package/types/src/ui/buttons/ribbon.d.ts +70 -70
  194. package/types/src/ui/common.d.ts +76 -76
  195. package/types/src/ui/components/dialogs/chordText.d.ts +28 -28
  196. package/types/src/ui/components/dialogs/textBlockAtomNode.d.ts +20 -20
  197. package/types/src/ui/components/dialogs/textGroupHtml.d.ts +29 -29
  198. package/types/src/ui/composable/draggable.d.ts +15 -15
  199. package/types/src/ui/dialogs/adapter.d.ts +120 -120
  200. package/types/src/ui/dialogs/annotationVue.d.ts +9 -9
  201. package/types/src/ui/dialogs/audioSettings.d.ts +32 -32
  202. package/types/src/ui/dialogs/beamGroupsVue.d.ts +20 -0
  203. package/types/src/ui/dialogs/chordChangeVue.d.ts +8 -8
  204. package/types/src/ui/dialogs/createPartHelpers.d.ts +29 -0
  205. package/types/src/ui/dialogs/customTupletsVue.d.ts +9 -9
  206. package/types/src/ui/dialogs/deleteSpecial.d.ts +8 -0
  207. package/types/src/ui/dialogs/dialog.d.ts +203 -203
  208. package/types/src/ui/dialogs/dynamicsVue.d.ts +10 -10
  209. package/types/src/ui/dialogs/factory.d.ts +15 -15
  210. package/types/src/ui/dialogs/fileDialogs.d.ts +4 -4
  211. package/types/src/ui/dialogs/hairpin.d.ts +35 -35
  212. package/types/src/ui/dialogs/hairpinVue.d.ts +7 -7
  213. package/types/src/ui/dialogs/instrument.d.ts +3 -3
  214. package/types/src/ui/dialogs/keySignatureVue.d.ts +7 -7
  215. package/types/src/ui/dialogs/libraryVue.d.ts +15 -15
  216. package/types/src/ui/dialogs/lyricVue.d.ts +8 -8
  217. package/types/src/ui/dialogs/pasteSpecial.d.ts +8 -0
  218. package/types/src/ui/dialogs/pedalMarkingVue.d.ts +7 -7
  219. package/types/src/ui/dialogs/recovery.d.ts +8 -0
  220. package/types/src/ui/dialogs/rehearsalMark.d.ts +25 -25
  221. package/types/src/ui/dialogs/rehearsalMarkVue.d.ts +7 -7
  222. package/types/src/ui/dialogs/slur.d.ts +57 -57
  223. package/types/src/ui/dialogs/slurVue.d.ts +7 -7
  224. package/types/src/ui/dialogs/tempo.d.ts +50 -50
  225. package/types/src/ui/dialogs/tempoVue.d.ts +10 -10
  226. package/types/src/ui/dialogs/textBlockVue.d.ts +8 -8
  227. package/types/src/ui/dialogs/textBracket.d.ts +35 -35
  228. package/types/src/ui/dialogs/textBracketVue.d.ts +7 -7
  229. package/types/src/ui/dialogs/tie.d.ts +41 -41
  230. package/types/src/ui/dialogs/volta.d.ts +40 -40
  231. package/types/src/ui/dialogs/voltaVue.d.ts +7 -7
  232. package/types/src/ui/fileio/fileInput.d.ts +7 -1
  233. package/types/src/ui/fileio/library.d.ts +40 -40
  234. package/types/src/ui/fileio/scoreArchive.d.ts +55 -0
  235. package/types/src/ui/fileio/xhrLoader.d.ts +7 -0
  236. package/types/src/ui/menus/file.d.ts +8 -8
  237. package/types/src/ui/menus/language.d.ts +7 -7
  238. package/types/src/ui/menus/manager.d.ts +58 -58
  239. package/types/src/ui/menus/menu.d.ts +159 -159
  240. package/types/src/ui/menus/partSelection.d.ts +14 -14
  241. package/types/src/ui/menus/score.d.ts +8 -8
  242. package/types/src/ui/menus/staffModifier.d.ts +17 -17
  243. package/types/typedoc.d.ts +147 -147
  244. package/src/ui/dialogs/dynamics.ts +0 -141
  245. package/types/src/ui/dialogs/dynamics.d.ts +0 -37
@@ -0,0 +1,351 @@
1
+ /**
2
+ * Headless checks for key-distance enharmonic spelling: SmoMusic.keyFifths, pitchFifths, fifthsToPitch,
3
+ * distanceFromKey, getEnharmonicInKey, getScaleTonesForKey, keyForOffset, transposePitchForKey and
4
+ * SmoOperation.transposeScore. See specs/033-key-distance-transposition/.
5
+ *
6
+ * Run with: npm run test:key-distance-transposition
7
+ */
8
+ import { SmoScore } from '../src/smo/data/score';
9
+ import { SmoNote } from '../src/smo/data/note';
10
+ import { SmoMusic } from '../src/smo/data/music';
11
+ import { Pitch, PitchLetter } from '../src/smo/data/common';
12
+ import { SmoOperation } from '../src/smo/xform/operations';
13
+ import { SmoGraceNote, SmoLyric, noteModifierDynamicCtorInit } from '../src/smo/data/noteModifiers';
14
+ import { measureModifierDynamicCtorInit } from '../src/smo/data/measureModifiers';
15
+ import { staffModifierDynamicCtorInit } from '../src/smo/data/staffModifiers';
16
+ import { scoreModifierDynamicCtorInit } from '../src/smo/data/scoreModifiers';
17
+
18
+ // The smo layer registers its deserialization factories at app start; do the same here
19
+ // without pulling in any UI.
20
+ noteModifierDynamicCtorInit();
21
+ measureModifierDynamicCtorInit();
22
+ staffModifierDynamicCtorInit();
23
+ scoreModifierDynamicCtorInit();
24
+
25
+ let failures = 0;
26
+ const check = (name: string, condition: boolean, detail?: string) => {
27
+ if (condition) {
28
+ console.log(`PASS ${name}`);
29
+ } else {
30
+ failures += 1;
31
+ console.log(`FAIL ${name}${detail ? ' -- ' + detail : ''}`);
32
+ }
33
+ };
34
+
35
+ const p = (letter: string, accidental: string, octave: number): Pitch =>
36
+ ({ letter: letter as PitchLetter, accidental, octave });
37
+ const name = (pitch: Pitch) => `${pitch.letter}${pitch.accidental === 'n' ? '' : pitch.accidental}${pitch.octave}`;
38
+ const spelled = (pitch: Pitch) => name(pitch).replace(/[0-9]+$/, '');
39
+
40
+ const KEYS = ['c', 'g', 'd', 'a', 'e', 'b', 'f#', 'c#', 'f', 'bb', 'eb', 'ab', 'db', 'gb', 'cb'];
41
+ const FLAT_FIFTHS: Record<string, number> = {
42
+ c: 0, g: 1, d: 2, a: 3, e: 4, b: 5, 'f#': 6, 'c#': 7, f: -1, bb: -2, eb: -3, ab: -4, db: -5, gb: -6, cb: -7
43
+ };
44
+
45
+ // ---- Foundational: key index, line-of-fifths positions, distance ----
46
+ {
47
+ KEYS.forEach((key) => {
48
+ check(`keyFifths ${key}`, SmoMusic.keyFifths(key) === FLAT_FIFTHS[key], `${SmoMusic.keyFifths(key)}`);
49
+ });
50
+ check('keyFifths is case insensitive', SmoMusic.keyFifths('Bb') === -2 && SmoMusic.keyFifths('F#') === 6);
51
+ check('keyFifths unknown key is C', SmoMusic.keyFifths('') === 0 && SmoMusic.keyFifths('x') === 0);
52
+ check('keySignatureLength has Gb', SmoMusic.keySignatureLength['Gb'] === 6 && SmoMusic.getFlatsInKeySignature('gb') === 6);
53
+
54
+ let roundTrip = true;
55
+ for (let f = SmoMusic.minFifths; f <= SmoMusic.maxFifths; ++f) {
56
+ const pitch = SmoMusic.fifthsToPitch(f, 4);
57
+ if (SmoMusic.pitchFifths(pitch) !== f) {
58
+ roundTrip = false;
59
+ console.log(` position ${f} -> ${name(pitch)} -> ${SmoMusic.pitchFifths(pitch)}`);
60
+ }
61
+ }
62
+ check('pitchFifths/fifthsToPitch round trip over the whole range', roundTrip);
63
+ check('fifthsToPitch examples', spelled(SmoMusic.fifthsToPitch(10, 4)) === 'a#' && spelled(SmoMusic.fifthsToPitch(-2, 4)) === 'bb'
64
+ && spelled(SmoMusic.fifthsToPitch(-1, 4)) === 'f' && spelled(SmoMusic.fifthsToPitch(19, 4)) === 'b##'
65
+ && spelled(SmoMusic.fifthsToPitch(-15, 4)) === 'fbb');
66
+
67
+ check('distance A# in C is 5 sharps', SmoMusic.distanceFromKey(p('a', '#', 4), 'c') === 5);
68
+ check('distance Bb in C is 1 flat', SmoMusic.distanceFromKey(p('b', 'b', 4), 'c') === -1);
69
+ check('distance C# in C is 2 sharps', SmoMusic.distanceFromKey(p('c', '#', 4), 'c') === 2);
70
+ check('distance E natural in Eb is 2 sharps', SmoMusic.distanceFromKey(p('e', 'n', 4), 'eb') === 2);
71
+ check('distance G# in G is 2 sharps', SmoMusic.distanceFromKey(p('g', '#', 4), 'g') === 2);
72
+ check('distance of in-key notes is 0', SmoMusic.distanceFromKey(p('f', '#', 4), 'g') === 0 &&
73
+ SmoMusic.distanceFromKey(p('b', 'b', 4), 'f') === 0 && SmoMusic.distanceFromKey(p('c', 'n', 4), 'c') === 0 &&
74
+ SmoMusic.distanceFromKey(p('b', 'b', 4), 'cb') === 0);
75
+ check('natural pitch with empty accidental counts as natural', SmoMusic.distanceFromKey(p('c', '', 4), 'c') === 0);
76
+
77
+ check('sounding value cb4 is b3', SmoMusic.soundingValue(p('c', 'b', 4)) === SmoMusic.soundingValue(p('b', 'n', 3)));
78
+ check('sounding value b#3 is c4', SmoMusic.soundingValue(p('b', '#', 3)) === SmoMusic.soundingValue(p('c', 'n', 4)));
79
+ check('sounding value b##3 is c#4', SmoMusic.soundingValue(p('b', '##', 3)) === SmoMusic.soundingValue(p('c', '#', 4)));
80
+ }
81
+
82
+ // ---- getScaleTonesForKey: same answers as the old table, for every key the table had ----
83
+ {
84
+ const sameTones = (a: Record<string, string>, b: Record<string, string>) =>
85
+ Object.keys(b).length === 7 && Object.keys(b).every((letter) => a[letter] === b[letter]);
86
+ const oldTable: Record<string, Record<string, string>> = {
87
+ c: { c: 'cn', d: 'dn', e: 'en', f: 'fn', g: 'gn', a: 'an', b: 'bn' },
88
+ g: { g: 'gn', a: 'an', b: 'bn', c: 'cn', d: 'dn', e: 'en', f: 'f#' },
89
+ d: { d: 'dn', e: 'en', f: 'f#', g: 'gn', a: 'an', b: 'bn', c: 'c#' },
90
+ a: { a: 'an', b: 'bn', c: 'c#', d: 'dn', e: 'en', f: 'f#', g: 'g#' },
91
+ e: { e: 'en', f: 'f#', g: 'g#', a: 'an', b: 'bn', c: 'c#', d: 'd#' },
92
+ b: { b: 'bn', c: 'c#', d: 'd#', e: 'en', f: 'f#', g: 'g#', a: 'a#' },
93
+ 'f#': { f: 'f#', g: 'g#', a: 'a#', b: 'bn', c: 'c#', d: 'd#', e: 'e#' },
94
+ 'c#': { c: 'c#', d: 'd#', e: 'e#', f: 'f#', g: 'g#', a: 'a#', b: 'b#' },
95
+ f: { f: 'fn', g: 'gn', a: 'an', b: 'bb', c: 'cn', d: 'dn', e: 'en' },
96
+ bb: { b: 'bb', c: 'cn', d: 'dn', e: 'eb', f: 'fn', g: 'gn', a: 'an' },
97
+ eb: { e: 'eb', f: 'fn', g: 'gn', a: 'ab', b: 'bb', c: 'cn', d: 'dn' },
98
+ ab: { a: 'ab', b: 'bb', c: 'cn', d: 'db', e: 'eb', f: 'fn', g: 'gn' },
99
+ db: { d: 'db', e: 'eb', f: 'fn', g: 'gb', a: 'ab', b: 'bb', c: 'cn' },
100
+ gb: { g: 'gb', a: 'ab', b: 'bb', c: 'cb', d: 'db', e: 'eb', f: 'fn' }
101
+ };
102
+ Object.keys(oldTable).forEach((key) => {
103
+ check(`getScaleTonesForKey ${key} matches the old table`,
104
+ sameTones(SmoMusic.getScaleTonesForKey(key), oldTable[key]), JSON.stringify(SmoMusic.getScaleTonesForKey(key)));
105
+ });
106
+ const cb = SmoMusic.getScaleTonesForKey('cb');
107
+ check('getScaleTonesForKey cb (not in the old table) has 7 flats', Object.values(cb).every((v) => v.length === 2 && v[1] === 'b'));
108
+ check('getScaleTonesForKey accepts uppercase', sameTones(SmoMusic.getScaleTonesForKey('Eb'), oldTable.eb));
109
+ }
110
+
111
+ // ---- Cross-key transposition keeps the distance from the key ----
112
+ {
113
+ const t = (pitch: Pitch, from: string, to: string, offset: number) =>
114
+ SmoMusic.transposePitchForKey(pitch, from, to, offset);
115
+
116
+ check('c# in C -> D is d#', name(t(p('c', '#', 4), 'c', 'd', 2)) === 'd#4', name(t(p('c', '#', 4), 'c', 'd', 2)));
117
+ check('a# in C -> D is b#', name(t(p('a', '#', 4), 'c', 'd', 2)) === 'b#4', name(t(p('a', '#', 4), 'c', 'd', 2)));
118
+ check('e natural in Eb -> G (+4) is g#', name(t(p('e', 'n', 4), 'eb', 'g', 4)) === 'g#4', name(t(p('e', 'n', 4), 'eb', 'g', 4)));
119
+ check('f in C -> G (+7) octave', name(t(p('f', 'n', 4), 'c', 'g', 7)) === 'c5', name(t(p('f', 'n', 4), 'c', 'g', 7)));
120
+ check('Bb in C -> D keeps flat direction (c natural)', name(t(p('b', 'b', 3), 'c', 'd', 2)) === 'c4',
121
+ name(t(p('b', 'b', 3), 'c', 'd', 2)));
122
+ check('cb4 in Gb -> G (+1) is c4 and sounds a half step higher', name(t(p('c', 'b', 4), 'gb', 'g', 1)) === 'c4',
123
+ name(t(p('c', 'b', 4), 'gb', 'g', 1)));
124
+ check('b#3 in C -> D (+2) sounds a whole step above c4',
125
+ SmoMusic.soundingValue(t(p('b', '#', 3), 'c', 'd', 2)) === SmoMusic.soundingValue(p('c', 'n', 4)) + 2);
126
+ check('same key, offset 0 keeps spelling', name(t(p('d', '#', 4), 'c', 'c', 0)) === 'd#4' &&
127
+ name(t(p('e', 'b', 4), 'g', 'g', 0)) === 'eb4' && name(t(p('b', '#', 3), 'f', 'f', 0)) === 'b#3');
128
+ check('same key, octave transposition keeps spelling', name(t(p('c', '#', 4), 'c', 'c', 12)) === 'c#5' &&
129
+ name(t(p('a', 'b', 4), 'c', 'c', -12)) === 'ab3');
130
+ // A transposing instrument: the keys differ by the offset
131
+ check('d# in D down a major second to C is c# (still 2 sharps from the key)',
132
+ name(t(p('d', '#', 5), 'd', 'c', -2)) === 'c#5', name(t(p('d', '#', 5), 'd', 'c', -2)));
133
+ check('c# in D (a key tone) down a major second to C is b natural',
134
+ name(t(p('c', '#', 5), 'd', 'c', -2)) === 'b4', name(t(p('c', '#', 5), 'd', 'c', -2)));
135
+ // keys and offset disagree: the sounding pitch still wins
136
+ const mismatch = t(p('c', 'n', 4), 'c', 'd', 3);
137
+ check('mismatched key and offset still sounds the offset',
138
+ SmoMusic.soundingValue(mismatch) === SmoMusic.soundingValue(p('c', 'n', 4)) + 3, name(mismatch));
139
+ // extreme: needs more than a double accidental, so falls back to the default spelling in the destination key
140
+ const extreme = t(p('b', '##', 4), 'c#', 'g', 7);
141
+ check('triple accidental is respelled to a double or less',
142
+ Math.abs(SmoMusic.accidentalCount(extreme.accidental)) <= 2 &&
143
+ SmoMusic.soundingValue(extreme) === SmoMusic.soundingValue(p('b', '##', 4)) + 7, name(extreme));
144
+
145
+ // all keys, all consistent offsets, every letter/accidental
146
+ let invariantFailures = 0;
147
+ let cases = 0;
148
+ const letters: PitchLetter[] = ['c', 'd', 'e', 'f', 'g', 'a', 'b'];
149
+ KEYS.forEach((from) => {
150
+ KEYS.forEach((to) => {
151
+ const shift = FLAT_FIFTHS[to] - FLAT_FIFTHS[from];
152
+ [((7 * shift) % 12 + 12) % 12, ((7 * shift) % 12 + 12) % 12 - 12].forEach((offset) => {
153
+ letters.forEach((letter) => {
154
+ ['b', 'n', '#'].forEach((accidental) => {
155
+ const input = p(letter, accidental, 4);
156
+ const position = SmoMusic.pitchFifths(input) + shift;
157
+ if (position < SmoMusic.minFifths || position > SmoMusic.maxFifths) {
158
+ return;
159
+ }
160
+ cases += 1;
161
+ const result = t(input, from, to, offset);
162
+ const ok = SmoMusic.soundingValue(result) === SmoMusic.soundingValue(input) + offset &&
163
+ SmoMusic.distanceFromKey(result, to) === SmoMusic.distanceFromKey(input, from) &&
164
+ name(t(result, to, from, -offset)) === name(input);
165
+ if (!ok) {
166
+ invariantFailures += 1;
167
+ if (invariantFailures < 6) {
168
+ console.log(` ${name(input)} ${from}->${to} (${offset}) = ${name(result)}`);
169
+ }
170
+ }
171
+ });
172
+ });
173
+ });
174
+ });
175
+ });
176
+ check(`sounding pitch, distance and round trip hold for ${cases} key/offset/pitch combinations`, invariantFailures === 0,
177
+ `${invariantFailures} failures`);
178
+
179
+ // in-key notes land on the destination scale
180
+ let scaleFailures = 0;
181
+ KEYS.forEach((from) => {
182
+ KEYS.forEach((to) => {
183
+ const shift = FLAT_FIFTHS[to] - FLAT_FIFTHS[from];
184
+ const offset = ((7 * shift) % 12 + 12) % 12;
185
+ const fromScale = SmoMusic.getScaleTonesForKey(from);
186
+ const toTones = Object.values(SmoMusic.getScaleTonesForKey(to));
187
+ Object.values(fromScale).forEach((tone) => {
188
+ const input = p(tone[0], tone[1], 4);
189
+ const result = t(input, from, to, offset);
190
+ if (SmoMusic.distanceFromKey(result, to) !== 0 || toTones.indexOf(spelled(result).length === 1 ? spelled(result) + 'n' : spelled(result)) < 0) {
191
+ scaleFailures += 1;
192
+ }
193
+ });
194
+ });
195
+ });
196
+ check('every note in the source key is a note of the destination key', scaleFailures === 0, `${scaleFailures} failures`);
197
+ }
198
+
199
+ // ---- Default spelling outside the key ----
200
+ {
201
+ const spell = (value: number, key: string) => spelled(SmoMusic.spellSoundingValue(value, key));
202
+ // 4 * 12 + n: c4 is 48
203
+ check('half step below b in C is bb, not a#', spell(58, 'c') === 'bb', spell(58, 'c'));
204
+ check('eb rather than d# in C', spell(51, 'c') === 'eb', spell(51, 'c'));
205
+ check('sharp and flat equally far goes to flat (ab in C)', spell(56, 'c') === 'ab', spell(56, 'c'));
206
+ check('c# in C (2 sharps vs 4 flats)', spell(49, 'c') === 'c#', spell(49, 'c'));
207
+ check('f# in C (1 sharp vs 5 flats)', spell(54, 'c') === 'f#', spell(54, 'c'));
208
+ check('f# is the key tone in G', spell(54, 'g') === 'f#', spell(54, 'g'));
209
+ check('bb is the key tone in F', spell(58, 'f') === 'bb', spell(58, 'f'));
210
+ check('a# is the key tone in B', spell(58, 'b') === 'a#', spell(58, 'b'));
211
+ check('db is the key tone in Ab', spell(49, 'ab') === 'db', spell(49, 'ab'));
212
+ check('in-key natural is natural', spell(48, 'c') === 'c' || spell(48, 'c') === 'cn');
213
+
214
+ // every pitch class in every key keeps its sounding value and never has more than a double accidental
215
+ let bad = 0;
216
+ KEYS.forEach((key) => {
217
+ for (let value = 36; value < 48; ++value) {
218
+ const pitch = SmoMusic.spellSoundingValue(value, key);
219
+ if (SmoMusic.soundingValue(pitch) !== value || Math.abs(SmoMusic.accidentalCount(pitch.accidental)) > 2) {
220
+ bad += 1;
221
+ }
222
+ }
223
+ });
224
+ check('default spelling keeps the sounding value and uses at most a double accidental', bad === 0, `${bad} failures`);
225
+
226
+ const viaEnharmonic = SmoMusic.getEnharmonicInKey(p('a', '#', 4), 'c');
227
+ check('getEnharmonicInKey a#4 in C is bb4', name(viaEnharmonic) === 'bb4', name(viaEnharmonic));
228
+ const boundary = SmoMusic.getEnharmonicInKey(p('b', '#', 3), 'c');
229
+ check('getEnharmonicInKey b#3 in C is c4 (octave moves)', name(boundary) === 'c4', name(boundary));
230
+ const boundary2 = SmoMusic.getEnharmonicInKey(p('c', 'b', 4), 'c');
231
+ check('getEnharmonicInKey cb4 in C is b3 (octave moves)', name(boundary2) === 'b3', name(boundary2));
232
+ const noOctave = SmoMusic.getEnharmonicInKey({ letter: 'a', accidental: '#' } as any, 'c');
233
+ check('getEnharmonicInKey with no octave defaults to octave 1', noOctave.octave === 1 && spelled(noOctave) === 'bb', name(noOctave));
234
+ }
235
+
236
+ // ---- Destination key for an offset ----
237
+ {
238
+ check('keyForOffset zero keeps the key (c#)', SmoMusic.keyForOffset('c#', 0) === 'c#');
239
+ check('keyForOffset zero keeps the key (Cb)', SmoMusic.keyForOffset('Cb', 0) === 'Cb');
240
+ check('keyForOffset eb + 4 is g', SmoMusic.keyForOffset('eb', 4) === 'g');
241
+ check('keyForOffset c + 2 is d', SmoMusic.keyForOffset('c', 2) === 'd');
242
+ check('keyForOffset c + 6 is gb (tie goes to flats)', SmoMusic.keyForOffset('c', 6) === 'gb');
243
+ check('keyForOffset c - 6 is gb (tie goes to flats)', SmoMusic.keyForOffset('c', -6) === 'gb');
244
+ check('keyForOffset c + 1 is db, not c#', SmoMusic.keyForOffset('c', 1) === 'db');
245
+ check('keyForOffset c + 11 is b', SmoMusic.keyForOffset('c', 11) === 'b');
246
+ check('keyForOffset c + 8 is ab, not g#', SmoMusic.keyForOffset('c', 8) === 'ab');
247
+ check('keyForOffset keeps upper case style', SmoMusic.keyForOffset('Eb', 4) === 'G' && SmoMusic.keyForOffset('C', 1) === 'Db');
248
+ check('keyForOffset octave is the same key', SmoMusic.keyForOffset('f#', 12) === 'f#' || SmoMusic.keyForOffset('f#', 12) === 'gb');
249
+ let inRange = true;
250
+ KEYS.forEach((key) => {
251
+ for (let offset = -24; offset <= 24; ++offset) {
252
+ const dest = SmoMusic.keyFifths(SmoMusic.keyForOffset(key, offset));
253
+ // the sounding tonic must move by exactly offset (mod 12)
254
+ const tonicShift = (((7 * dest - 7 * FLAT_FIFTHS[key]) % 12) + 12) % 12;
255
+ if (offset !== 0 && (Math.abs(dest) > 6 || tonicShift !== ((offset % 12) + 12) % 12)) {
256
+ inRange = false;
257
+ }
258
+ }
259
+ });
260
+ check('keyForOffset always has at most 6 accidentals and the right tonic', inRange);
261
+ }
262
+
263
+ // ---- Chord symbols ----
264
+ {
265
+ const chord = (text: string) => {
266
+ const lyric = new SmoLyric({ ...SmoLyric.defaults, text, parser: SmoLyric.parsers.chord } as any);
267
+ return lyric;
268
+ };
269
+ const out = SmoLyric.transposeChordToKey(chord('C#'), 2, 'c', 'd');
270
+ check('chord symbol C# in C -> D is D#', out.text === 'D#', out.text);
271
+ const out2 = SmoLyric.transposeChordToKey(chord('A#'), 2, 'c', 'd');
272
+ check('chord symbol A# in C -> D is B#', out2.text === 'B#', out2.text);
273
+ const out3 = SmoLyric.transposeChordToKey(chord('Em7'), 4, 'eb', 'g');
274
+ check('chord symbol E in Eb -> G (+4) is G#m7', out3.text === 'G#m7', out3.text);
275
+ }
276
+
277
+ // ---- Whole-score transposition ----
278
+ {
279
+ const buildScore = (key: string) => {
280
+ const score = SmoScore.getDefaultScore(SmoScore.defaults, null);
281
+ while (score.staves[0].measures.length < 2) {
282
+ score.addMeasure(score.staves[0].measures.length - 1);
283
+ }
284
+ score.staves[0].measures.forEach((measure) => {
285
+ measure.setKeySignature(key);
286
+ });
287
+ return score;
288
+ };
289
+ const setPitches = (score: SmoScore, measure: number, pitches: Pitch[]) => {
290
+ const notes = score.staves[0].measures[measure].voices[0].notes;
291
+ pitches.forEach((pitch, ix) => {
292
+ notes[ix].noteType = 'n';
293
+ notes[ix].pitches = [JSON.parse(JSON.stringify(pitch))];
294
+ });
295
+ };
296
+ const snapshot = (score: SmoScore) => score.staves[0].measures.map((measure) =>
297
+ measure.keySignature + ':' + measure.voices[0].notes.slice(0, 4).map((n) => name(n.pitches[0])).join(','));
298
+
299
+ const score = buildScore('eb');
300
+ const grace = new SmoGraceNote(SmoGraceNote.defaults);
301
+ grace.pitches = [p('e', 'n', 4)];
302
+ setPitches(score, 0, [p('e', 'n', 4), p('e', 'b', 4), p('b', 'b', 3), p('d', 'n', 4)]);
303
+ setPitches(score, 1, [p('g', 'n', 4), p('a', 'b', 4), p('c', 'n', 5), p('f', '#', 4)]);
304
+ score.staves[0].measures[0].voices[0].notes[0].addGraceNote(grace, 0);
305
+ const original = snapshot(score);
306
+
307
+ SmoOperation.transposeScore(score, 0);
308
+ check('transposeScore with no offset changes nothing', JSON.stringify(snapshot(score)) === JSON.stringify(original));
309
+
310
+ SmoOperation.transposeScore(score, 4);
311
+ const up = snapshot(score);
312
+ check('transposeScore +4 changes Eb measures to G', score.staves[0].measures.every((m) => m.keySignature === 'g'), JSON.stringify(up));
313
+ check('transposeScore +4 turns e natural into g#', name(score.staves[0].measures[0].voices[0].notes[0].pitches[0]) === 'g#4',
314
+ up[0]);
315
+ check('transposeScore +4 maps eb to g, bb to d (key tones)', up[0].startsWith('g:g#4,g4,d4,'), up[0]);
316
+ check('transposeScore +4 also transposes the notes\' key signature',
317
+ score.staves[0].measures[0].voices[0].notes.every((n) => n.keySignature === 'g'));
318
+ check('transposeScore +4 transposes grace notes', name(score.staves[0].measures[0].voices[0].notes[0].getGraceNotes()[0].pitches[0]) === 'g#4',
319
+ name(score.staves[0].measures[0].voices[0].notes[0].getGraceNotes()[0].pitches[0]));
320
+
321
+ SmoOperation.transposeScore(score, -4);
322
+ check('transposeScore -4 restores key, spelling and octave', JSON.stringify(snapshot(score)) === JSON.stringify(original),
323
+ JSON.stringify(snapshot(score)));
324
+ check('transposeScore -4 restores the grace note', name(score.staves[0].measures[0].voices[0].notes[0].getGraceNotes()[0].pitches[0]) === 'e4');
325
+
326
+ // step by step, as the dialog does, then cancel
327
+ for (let i = 0; i < 7; ++i) {
328
+ SmoOperation.transposeScore(score, 1);
329
+ }
330
+ const sevenUp = snapshot(score);
331
+ SmoOperation.transposeScore(score, -7);
332
+ check('transposeScore +1 seven times then -7 restores the score', JSON.stringify(snapshot(score)) === JSON.stringify(original),
333
+ sevenUp[0] + ' / ' + JSON.stringify(snapshot(score)));
334
+ const whole = buildScore('eb');
335
+ setPitches(whole, 0, [p('e', 'n', 4), p('e', 'b', 4), p('b', 'b', 3), p('d', 'n', 4)]);
336
+ SmoOperation.transposeScore(whole, 7);
337
+ check('transposeScore +7 in one step equals +1 seven times', whole.staves[0].measures[0].keySignature === 'bb' &&
338
+ JSON.stringify(snapshot(whole)[0]) === JSON.stringify(sevenUp[0]), snapshot(whole)[0] + ' vs ' + sevenUp[0]);
339
+
340
+ const down = buildScore('c');
341
+ setPitches(down, 0, [p('c', '#', 4), p('a', '#', 3), p('e', 'b', 4), p('g', 'n', 3)]);
342
+ SmoOperation.transposeScore(down, 2);
343
+ check('transposeScore C -> D: c# is d#, a# is b#', snapshot(down)[0] === 'd:d#4,b#3,f4,a3', snapshot(down)[0]);
344
+ }
345
+
346
+ if (failures > 0) {
347
+ console.log(`${failures} check(s) failed`);
348
+ process.exitCode = 1;
349
+ } else {
350
+ console.log('all checks passed');
351
+ }
@@ -0,0 +1,295 @@
1
+ /**
2
+ * Headless checks for filling measures with rests: the rule for which rests (fillRestDurations), the measure
3
+ * method, the operation and its cleanup, and serialization. Cases are listed in
4
+ * specs/039-measure-fill-submenu/contracts/measure-fill-api.md.
5
+ *
6
+ * Run with: npm run test:measure-fill
7
+ */
8
+ import { SmoScore } from '../src/smo/data/score';
9
+ import { SmoMeasure } from '../src/smo/data/measure';
10
+ import { SmoNote } from '../src/smo/data/note';
11
+ import { SmoSystemStaff } from '../src/smo/data/systemStaff';
12
+ import { SmoMusic } from '../src/smo/data/music';
13
+ import { SmoTimeSignature, SmoBarline, SmoRehearsalMark } from '../src/smo/data/measureModifiers';
14
+ import { SmoSlur, SmoStaffHairpin, SmoInstrument } from '../src/smo/data/staffModifiers';
15
+ import { SmoLyric, SmoDynamicText, noteModifierDynamicCtorInit } from '../src/smo/data/noteModifiers';
16
+ import { SmoTextGroup, SmoScoreText } from '../src/smo/data/scoreText';
17
+ import { measureModifierDynamicCtorInit } from '../src/smo/data/measureModifiers';
18
+ import { staffModifierDynamicCtorInit } from '../src/smo/data/staffModifiers';
19
+ import { scoreModifierDynamicCtorInit } from '../src/smo/data/scoreModifiers';
20
+ import { SmoSelector, SmoSelection } from '../src/smo/xform/selections';
21
+ import { SmoOperation } from '../src/smo/xform/operations';
22
+ import { fillRestDurations, MeasureFillType, MeasureFillTypes } from '../src/smo/xform/measureFill';
23
+
24
+ // The smo layer registers its deserialization factories at app start; do the same here
25
+ // without pulling in any UI.
26
+ noteModifierDynamicCtorInit();
27
+ measureModifierDynamicCtorInit();
28
+ staffModifierDynamicCtorInit();
29
+ scoreModifierDynamicCtorInit();
30
+
31
+ let failures = 0;
32
+ const check = (name: string, condition: boolean, detail?: string) => {
33
+ if (condition) {
34
+ console.log(`PASS ${name}`);
35
+ } else {
36
+ failures += 1;
37
+ console.log(`FAIL ${name}${detail ? ' -- ' + detail : ''}`);
38
+ }
39
+ };
40
+
41
+ const ts = (actualBeats: number, beatDuration: number): SmoTimeSignature => {
42
+ const params = SmoTimeSignature.defaults;
43
+ params.times = [{ actualBeats, beatDuration }];
44
+ return new SmoTimeSignature(params);
45
+ };
46
+ /** n copies of a value */
47
+ const x = (n: number, value: number): number[] => new Array(n).fill(value);
48
+ const list = (values: number[]): string => values.join(',');
49
+ /** A voice of notes (not rests) of the given length each, adding up to the measure */
50
+ const fillNotes = (measure: SmoMeasure, noteTicks: number): SmoNote[] => {
51
+ const total = SmoMusic.timeSignatureToTicks(measure.timeSignature.actualBeats, measure.timeSignature.beatDuration);
52
+ const proto = measure.voices[0].notes[0];
53
+ return x(total / noteTicks, noteTicks).map((tt) => {
54
+ const note = SmoNote.cloneWithDuration(proto, tt);
55
+ note.noteType = 'n';
56
+ return note;
57
+ });
58
+ };
59
+ /**
60
+ * A score with one measure for each meter, each full of sixteenth notes. More than one staff gives each
61
+ * staff the same measures.
62
+ */
63
+ const buildScore = (meters: [number, number][], staves: number = 1): SmoScore => {
64
+ const score = SmoScore.getDefaultScore(SmoScore.defaults, null);
65
+ while (score.staves[0].measures.length < meters.length) {
66
+ score.addMeasure(score.staves[0].measures.length - 1);
67
+ }
68
+ for (let s = 1; s < staves; ++s) {
69
+ const params = SmoSystemStaff.defaults;
70
+ params.staffId = s;
71
+ params.measureInstrumentMap[0] = new SmoInstrument(SmoInstrument.defaults);
72
+ score.addStaff(params);
73
+ }
74
+ score.staves.forEach((staff) => {
75
+ staff.measures.forEach((measure, ix) => {
76
+ measure.timeSignature = ts(meters[ix][0], meters[ix][1]);
77
+ measure.voices[0].notes = fillNotes(measure, 1024);
78
+ });
79
+ });
80
+ return score;
81
+ };
82
+ const pick = (score: SmoScore, staff: number, measure: number): SmoSelection =>
83
+ SmoSelection.measureSelection(score, staff, measure)!;
84
+ const measureOf = (score: SmoScore, staff: number, measure: number): SmoMeasure => score.staves[staff].measures[measure];
85
+ /** The durations of the notes in voice 0 */
86
+ const durationsOf = (measure: SmoMeasure): number[] => measure.voices[0].notes.map((nn) => nn.tickCount);
87
+ const allRests = (measure: SmoMeasure): boolean => measure.voices.every((vv) => vv.notes.every((nn) => nn.noteType === 'r'));
88
+ const sum = (values: number[]): number => values.reduce((acc, vv) => acc + vv, 0);
89
+ const selector = (staff: number, measure: number): SmoSelector => {
90
+ const rv = SmoSelector.default;
91
+ rv.staff = staff;
92
+ rv.measure = measure;
93
+ return rv;
94
+ };
95
+
96
+ // --- the rule: every row of the table in data-model.md (D1 to D39) ---
97
+ const rows: { beats: number, den: number, whole: number[], beat: number[], eighth: number[] }[] = [
98
+ { beats: 4, den: 4, whole: [16384], beat: x(4, 4096), eighth: x(8, 2048) },
99
+ { beats: 2, den: 4, whole: [8192], beat: x(2, 4096), eighth: x(4, 2048) },
100
+ { beats: 3, den: 4, whole: [12288], beat: x(3, 4096), eighth: x(6, 2048) },
101
+ { beats: 6, den: 8, whole: [12288], beat: x(2, 6144), eighth: x(6, 2048) },
102
+ { beats: 9, den: 8, whole: [16384, 2048], beat: x(3, 6144), eighth: x(9, 2048) },
103
+ { beats: 12, den: 8, whole: [24576], beat: x(4, 6144), eighth: x(12, 2048) },
104
+ { beats: 5, den: 8, whole: [8192, 2048], beat: [4096, 4096, 2048], eighth: x(5, 2048) },
105
+ { beats: 7, den: 8, whole: [14336], beat: [4096, 4096, 4096, 2048], eighth: x(7, 2048) },
106
+ { beats: 3, den: 8, whole: [6144], beat: [6144], eighth: x(3, 2048) },
107
+ { beats: 4, den: 8, whole: [8192], beat: x(2, 4096), eighth: x(4, 2048) },
108
+ { beats: 2, den: 2, whole: [16384], beat: x(2, 8192), eighth: x(8, 2048) },
109
+ { beats: 6, den: 16, whole: [6144], beat: x(2, 3072), eighth: x(3, 2048) },
110
+ { beats: 5, den: 16, whole: [4096, 1024], beat: [2048, 2048, 1024], eighth: [2048, 2048, 1024] }
111
+ ];
112
+ const fillNames: [MeasureFillType, string][] = [['wholeRest', 'Whole Rest'], ['beats', 'Beats'], ['eighthRests', 'Eighth Rests']];
113
+ rows.forEach((row) => {
114
+ const expected: Record<MeasureFillType, number[]> = { wholeRest: row.whole, beats: row.beat, eighthRests: row.eighth };
115
+ fillNames.forEach(([fill, label]) => {
116
+ const got = fillRestDurations(ts(row.beats, row.den), fill, undefined);
117
+ const total = SmoMusic.timeSignatureToTicks(row.beats, row.den);
118
+ check(`D ${row.beats}/${row.den} ${label}`, list(got) === list(expected[fill]), `${list(got)} vs ${list(expected[fill])}`);
119
+ check(`D40 ${row.beats}/${row.den} ${label} adds up to the measure`, sum(got) === total, `${sum(got)} vs ${total}`);
120
+ check(`D41 ${row.beats}/${row.den} ${label} uses only valid durations`,
121
+ got.every((dd) => SmoMusic.validDurations[dd] !== undefined));
122
+ });
123
+ });
124
+ check('D42 pickup 4/4 Whole Rest', list(fillRestDurations(ts(4, 4), 'wholeRest', 4096)) === '4096');
125
+ check('D43 pickup 4/4 Beats', list(fillRestDurations(ts(4, 4), 'beats', 4096)) === '4096');
126
+ check('D44 pickup 4/4 Eighth Rests', list(fillRestDurations(ts(4, 4), 'eighthRests', 6144)) === '2048,2048,2048');
127
+ check('D45 pickup 6/8 Beats shorter than a beat', list(fillRestDurations(ts(6, 8), 'beats', 2048)) === '2048');
128
+ check('D46 zero length', fillRestDurations(ts(4, 4), 'wholeRest', 0).length === 0);
129
+ check('D47 there are three fill types', MeasureFillTypes.length === 3);
130
+
131
+ // --- SmoMeasure.fillWithRests ---
132
+ {
133
+ const score = buildScore([[4, 4]]);
134
+ const measure = measureOf(score, 0, 0);
135
+ measure.addRehearsalMark(SmoRehearsalMark.defaults);
136
+ measure.setBeamGroups([2, 2]);
137
+ measure.setBarline(new SmoBarline({ position: SmoBarline.positions.end, barline: SmoBarline.barlines.doubleBar }));
138
+ measure.voices.push({ notes: fillNotes(measure, 2048) });
139
+ measure.tupletTrees = [];
140
+ const before = { ts: measure.timeSignature.timeSignature, clef: measure.clef, key: measure.keySignature,
141
+ modifiers: measure.modifiers.length, groups: list(measure.getBeamGroups()) };
142
+ measure.fillWithRests([8192, 8192], 4096);
143
+ check('F1 one voice of rests', measure.voices.length === 1 && allRests(measure));
144
+ check('F2 the rests are the durations given', list(durationsOf(measure)) === '8192,8192');
145
+ check('F3 tuplets and beam groups are empty and the active voice is 0',
146
+ measure.tupletTrees.length === 0 && measure.beamGroups.length === 0 && measure.activeVoice === 0);
147
+ check('F4 the measure settings are kept', measure.timeSignature.timeSignature === before.ts && measure.clef === before.clef
148
+ && measure.keySignature === before.key && measure.modifiers.length === before.modifiers
149
+ && list(measure.getBeamGroups()) === before.groups);
150
+ check('F5 the rests have the measure clef and key', measure.voices[0].notes.every((nn) =>
151
+ nn.clef === measure.clef && nn.keySignature === measure.keySignature));
152
+ }
153
+
154
+ // --- SmoOperation.fillMeasures ---
155
+ {
156
+ // O1: scope: measures 1 and 2 of staff 1 only
157
+ const score = buildScore([[4, 4], [4, 4], [4, 4], [4, 4]], 2);
158
+ SmoOperation.fillMeasures(score, [pick(score, 1, 1), pick(score, 1, 2)], 'beats');
159
+ const changed = [1, 2].every((m) => allRests(measureOf(score, 1, m)) && list(durationsOf(measureOf(score, 1, m))) === list(x(4, 4096)));
160
+ const others = [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 3]].every(([s, m]) =>
161
+ durationsOf(measureOf(score, s, m)).length === 16 && !allRests(measureOf(score, s, m)));
162
+ check('O1 only the selected measures of the selected staff change', changed && others);
163
+ }
164
+ {
165
+ // O2: several meters in one selection
166
+ const score = buildScore([[4, 4], [6, 8], [5, 8]]);
167
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0), pick(score, 0, 1), pick(score, 0, 2)], 'beats');
168
+ check('O2 each measure is filled by its own meter',
169
+ list(durationsOf(measureOf(score, 0, 0))) === list(x(4, 4096)) && list(durationsOf(measureOf(score, 0, 1))) === list(x(2, 6144))
170
+ && list(durationsOf(measureOf(score, 0, 2))) === '4096,4096,2048');
171
+ }
172
+ {
173
+ // O3: a tuplet
174
+ const score = buildScore([[4, 4]]);
175
+ const measure = measureOf(score, 0, 0);
176
+ measure.voices[0].notes = fillNotes(measure, 4096);
177
+ SmoOperation.makeTuplet(SmoSelection.noteSelection(score, 0, 0, 0, 0)!,
178
+ { numNotes: 3, notesOccupied: 2, bracketed: true, ratioed: false });
179
+ const hadTuplet = measure.tupletTrees.length > 0;
180
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0)], 'wholeRest');
181
+ check('O3 a tuplet is removed', hadTuplet && measure.tupletTrees.length === 0 && list(durationsOf(measure)) === '16384');
182
+ }
183
+ {
184
+ // O4: lyrics and dynamics go with the notes
185
+ const score = buildScore([[4, 4]]);
186
+ const measure = measureOf(score, 0, 0);
187
+ measure.voices[0].notes[0].addLyric(new SmoLyric(SmoLyric.defaults));
188
+ measure.voices[0].notes[1].addDynamic(new SmoDynamicText(SmoDynamicText.defaults));
189
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0)], 'wholeRest');
190
+ const note = measure.voices[0].notes[0];
191
+ check('O4 attachments on the notes are gone', measure.voices[0].notes.length === 1
192
+ && note.getTrueLyrics().length === 0 && note.getModifiers('SmoDynamicText').length === 0);
193
+ }
194
+ {
195
+ // O5 to O8: staff modifiers
196
+ const score = buildScore([[4, 4], [4, 4], [4, 4], [4, 4]], 2);
197
+ const addSlur = (staff: number, from: number, to: number) => {
198
+ const slur = new SmoSlur(SmoSlur.defaults);
199
+ slur.startSelector = selector(staff, from);
200
+ slur.endSelector = selector(staff, to);
201
+ slur.associatedStaff = staff;
202
+ score.staves[staff].addStaffModifier(slur);
203
+ return slur;
204
+ };
205
+ const startsIn = addSlur(0, 1, 3);
206
+ const endsIn = addSlur(0, 0, 1);
207
+ const hairpin = new SmoStaffHairpin(SmoStaffHairpin.defaults);
208
+ hairpin.startSelector = selector(0, 0);
209
+ hairpin.endSelector = selector(0, 3);
210
+ hairpin.associatedStaff = 0;
211
+ score.staves[0].addStaffModifier(hairpin);
212
+ const otherStaff = addSlur(1, 1, 2);
213
+ SmoOperation.fillMeasures(score, [pick(score, 0, 1)], 'wholeRest');
214
+ const left = score.staves[0].modifiers.map((mm) => mm.attrs.id);
215
+ check('O5 a modifier that starts in the measure is removed', left.indexOf(startsIn.attrs.id) < 0);
216
+ check('O6 a modifier that ends in the measure is removed', left.indexOf(endsIn.attrs.id) < 0);
217
+ check('O7 a modifier that passes over the measure is kept', left.indexOf(hairpin.attrs.id) >= 0);
218
+ check('O8 a modifier in another staff is kept', score.staves[1].modifiers.map((mm) => mm.attrs.id).indexOf(otherStaff.attrs.id) >= 0);
219
+ }
220
+ {
221
+ // O9 and O10: text attached to notes
222
+ const score = buildScore([[4, 4], [4, 4]]);
223
+ const attached = (measure: number) => {
224
+ const params = SmoTextGroup.defaults;
225
+ params.textBlocks = [{ text: new SmoScoreText(SmoScoreText.defaults), position: SmoTextGroup.relativePositions.RIGHT, activeText: false }];
226
+ params.attachToSelector = true;
227
+ params.selector = selector(0, measure);
228
+ const group = new SmoTextGroup(params);
229
+ score.addTextGroup(group);
230
+ return group;
231
+ };
232
+ const inFilled = attached(0);
233
+ const elsewhere = attached(1);
234
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0)], 'wholeRest');
235
+ const ids = score.textGroups.map((tg) => tg.attrs.id);
236
+ check('O9 text attached to a note in a filled measure is removed', ids.indexOf(inFilled.attrs.id) < 0);
237
+ check('O10 text attached to another measure is kept', ids.indexOf(elsewhere.attrs.id) >= 0);
238
+ }
239
+ {
240
+ // O11: a pickup keeps its length
241
+ const score = buildScore([[4, 4]]);
242
+ const measure = measureOf(score, 0, 0);
243
+ measure.voices[0].notes = fillNotes(measure, 4096).slice(0, 1);
244
+ const wasPickup = measure.isPickup();
245
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0)], 'wholeRest');
246
+ check('O11 a pickup measure is filled to the length it had', wasPickup && list(durationsOf(measure)) === '4096' && measure.isPickup());
247
+ }
248
+ {
249
+ // O12 and O13: the same fill twice, and a measure that is already a whole rest
250
+ const score = buildScore([[4, 4], [4, 4]]);
251
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0)], 'eighthRests');
252
+ const once = list(durationsOf(measureOf(score, 0, 0)));
253
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0)], 'eighthRests');
254
+ check('O12 the same fill twice gives the same measure', list(durationsOf(measureOf(score, 0, 0))) === once && once === list(x(8, 2048)));
255
+ SmoOperation.fillMeasures(score, [pick(score, 0, 1)], 'wholeRest');
256
+ SmoOperation.fillMeasures(score, [pick(score, 0, 1)], 'wholeRest');
257
+ check('O13 Whole Rest on a whole rest', list(durationsOf(measureOf(score, 0, 1))) === '16384');
258
+ }
259
+ {
260
+ // O14 and O15: beam grouping is kept, and the rests get the beam length for the meter
261
+ const score = buildScore([[5, 8], [6, 8], [4, 4]]);
262
+ measureOf(score, 0, 0).setBeamGroups([3, 2]);
263
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0), pick(score, 0, 1), pick(score, 0, 2)], 'eighthRests');
264
+ check('O14 the beam grouping is kept', list(measureOf(score, 0, 0).getBeamGroups()) === '3,2');
265
+ check('O15 rests get the beam length for the meter',
266
+ measureOf(score, 0, 1).voices[0].notes.every((nn) => nn.beamBeats === score.preferences.defaultTripleDuration)
267
+ && measureOf(score, 0, 2).voices[0].notes.every((nn) => nn.beamBeats === score.preferences.defaultDupleDuration));
268
+ }
269
+
270
+ // --- serialization ---
271
+ {
272
+ const score = buildScore([[4, 4], [6, 8], [5, 8], [3, 4]]);
273
+ const pickup = measureOf(score, 0, 3);
274
+ pickup.voices[0].notes = fillNotes(pickup, 4096).slice(0, 1);
275
+ SmoOperation.fillMeasures(score, [pick(score, 0, 0)], 'wholeRest');
276
+ SmoOperation.fillMeasures(score, [pick(score, 0, 1)], 'beats');
277
+ SmoOperation.fillMeasures(score, [pick(score, 0, 2)], 'eighthRests');
278
+ SmoOperation.fillMeasures(score, [pick(score, 0, 3)], 'wholeRest');
279
+ const back = SmoScore.deserialize(JSON.stringify(score.serialize()));
280
+ const same = [0, 1, 2, 3].every((m) => list(durationsOf(measureOf(back, 0, m))) === list(durationsOf(measureOf(score, 0, m)))
281
+ && allRests(measureOf(back, 0, m)));
282
+ check('S1 filled measures survive a save and load', same);
283
+ const untouched = buildScore([[4, 4], [6, 8]]);
284
+ const text = JSON.stringify(untouched.serialize());
285
+ const again = JSON.stringify(SmoScore.deserialize(text).serialize());
286
+ check('S2 a score with no filled measure has the same content after a save and load',
287
+ JSON.stringify(SmoScore.deserialize(again).serialize()) === again);
288
+ }
289
+
290
+ if (failures > 0) {
291
+ console.log(`${failures} check(s) failed`);
292
+ process.exitCode = 1;
293
+ } else {
294
+ console.log('all checks passed');
295
+ }