@mcqj/vexflow 0.0.1-alpha.1

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 (300) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +208 -0
  3. package/build/cjs/vexflow-bravura.js +2 -0
  4. package/build/cjs/vexflow-bravura.js.map +1 -0
  5. package/build/cjs/vexflow-core.js +2 -0
  6. package/build/cjs/vexflow-core.js.map +1 -0
  7. package/build/cjs/vexflow-debug-with-tests.js +43475 -0
  8. package/build/cjs/vexflow-debug-with-tests.js.map +1 -0
  9. package/build/cjs/vexflow-debug.js +19194 -0
  10. package/build/cjs/vexflow-debug.js.map +1 -0
  11. package/build/cjs/vexflow.js +2 -0
  12. package/build/cjs/vexflow.js.map +1 -0
  13. package/build/esm/entry/vexflow-bravura.js +17 -0
  14. package/build/esm/entry/vexflow-core.js +4 -0
  15. package/build/esm/entry/vexflow-debug-with-tests.js +38 -0
  16. package/build/esm/entry/vexflow-debug.js +31 -0
  17. package/build/esm/entry/vexflow.js +31 -0
  18. package/build/esm/package.json +3 -0
  19. package/build/esm/src/accidental.js +330 -0
  20. package/build/esm/src/annotation.js +231 -0
  21. package/build/esm/src/articulation.js +281 -0
  22. package/build/esm/src/barnote.js +56 -0
  23. package/build/esm/src/beam.js +644 -0
  24. package/build/esm/src/bend.js +186 -0
  25. package/build/esm/src/boundingbox.js +59 -0
  26. package/build/esm/src/canvascontext.js +216 -0
  27. package/build/esm/src/chordsymbol.js +325 -0
  28. package/build/esm/src/clef.js +120 -0
  29. package/build/esm/src/clefnote.js +35 -0
  30. package/build/esm/src/crescendo.js +78 -0
  31. package/build/esm/src/curve.js +126 -0
  32. package/build/esm/src/dot.js +129 -0
  33. package/build/esm/src/easyscore.js +374 -0
  34. package/build/esm/src/element.js +409 -0
  35. package/build/esm/src/factory.js +463 -0
  36. package/build/esm/src/flag.js +22 -0
  37. package/build/esm/src/font.js +237 -0
  38. package/build/esm/src/fonts/academico.js +1 -0
  39. package/build/esm/src/fonts/academicobold.js +1 -0
  40. package/build/esm/src/fonts/bravura.js +1 -0
  41. package/build/esm/src/fonts/gonville.js +1 -0
  42. package/build/esm/src/fonts/petaluma.js +1 -0
  43. package/build/esm/src/fonts/petalumascript.js +1 -0
  44. package/build/esm/src/formatter.js +610 -0
  45. package/build/esm/src/fraction.js +170 -0
  46. package/build/esm/src/frethandfinger.js +141 -0
  47. package/build/esm/src/ghostnote.js +50 -0
  48. package/build/esm/src/glyphnote.js +44 -0
  49. package/build/esm/src/glyphs.js +2936 -0
  50. package/build/esm/src/gracenote.js +106 -0
  51. package/build/esm/src/gracenotegroup.js +138 -0
  52. package/build/esm/src/gracetabnote.js +11 -0
  53. package/build/esm/src/index.js +86 -0
  54. package/build/esm/src/keymanager.js +99 -0
  55. package/build/esm/src/keysignature.js +220 -0
  56. package/build/esm/src/keysignote.js +28 -0
  57. package/build/esm/src/metrics.js +237 -0
  58. package/build/esm/src/modifier.js +121 -0
  59. package/build/esm/src/modifiercontext.js +110 -0
  60. package/build/esm/src/multimeasurerest.js +127 -0
  61. package/build/esm/src/music.js +322 -0
  62. package/build/esm/src/note.js +383 -0
  63. package/build/esm/src/notehead.js +89 -0
  64. package/build/esm/src/notesubgroup.js +59 -0
  65. package/build/esm/src/ornament.js +192 -0
  66. package/build/esm/src/parenthesis.js +61 -0
  67. package/build/esm/src/parser.js +164 -0
  68. package/build/esm/src/pedalmarking.js +168 -0
  69. package/build/esm/src/registry.js +81 -0
  70. package/build/esm/src/rendercontext.js +20 -0
  71. package/build/esm/src/renderer.js +112 -0
  72. package/build/esm/src/repeatnote.js +17 -0
  73. package/build/esm/src/stave.js +579 -0
  74. package/build/esm/src/stavebarline.js +237 -0
  75. package/build/esm/src/staveconnector.js +180 -0
  76. package/build/esm/src/stavehairpin.js +115 -0
  77. package/build/esm/src/staveline.js +199 -0
  78. package/build/esm/src/stavemodifier.js +56 -0
  79. package/build/esm/src/stavenote.js +861 -0
  80. package/build/esm/src/staverepetition.js +120 -0
  81. package/build/esm/src/stavesection.js +36 -0
  82. package/build/esm/src/stavetempo.js +110 -0
  83. package/build/esm/src/stavetext.js +55 -0
  84. package/build/esm/src/stavetie.js +186 -0
  85. package/build/esm/src/stavevolta.js +53 -0
  86. package/build/esm/src/stem.js +133 -0
  87. package/build/esm/src/stemmablenote.js +193 -0
  88. package/build/esm/src/stringnumber.js +228 -0
  89. package/build/esm/src/strokes.js +164 -0
  90. package/build/esm/src/svgcontext.js +402 -0
  91. package/build/esm/src/system.js +166 -0
  92. package/build/esm/src/tables.js +696 -0
  93. package/build/esm/src/tabnote.js +278 -0
  94. package/build/esm/src/tabslide.js +68 -0
  95. package/build/esm/src/tabstave.js +17 -0
  96. package/build/esm/src/tabtie.js +19 -0
  97. package/build/esm/src/textbracket.js +128 -0
  98. package/build/esm/src/textdynamics.js +53 -0
  99. package/build/esm/src/textnote.js +116 -0
  100. package/build/esm/src/tickable.js +224 -0
  101. package/build/esm/src/tickcontext.js +189 -0
  102. package/build/esm/src/timesignature.js +169 -0
  103. package/build/esm/src/timesignote.js +30 -0
  104. package/build/esm/src/tremolo.js +30 -0
  105. package/build/esm/src/tuning.js +55 -0
  106. package/build/esm/src/tuplet.js +258 -0
  107. package/build/esm/src/typeguard.js +30 -0
  108. package/build/esm/src/util.js +67 -0
  109. package/build/esm/src/version.js +3 -0
  110. package/build/esm/src/vexflow.js +309 -0
  111. package/build/esm/src/vibrato.js +68 -0
  112. package/build/esm/src/vibratobracket.js +44 -0
  113. package/build/esm/src/voice.js +178 -0
  114. package/build/esm/src/web.js +15 -0
  115. package/build/esm/tests/accidental_tests.js +1055 -0
  116. package/build/esm/tests/annotation_tests.js +381 -0
  117. package/build/esm/tests/articulation_tests.js +432 -0
  118. package/build/esm/tests/auto_beam_formatting_tests.js +551 -0
  119. package/build/esm/tests/barline_tests.js +52 -0
  120. package/build/esm/tests/beam_tests.js +669 -0
  121. package/build/esm/tests/bend_tests.js +223 -0
  122. package/build/esm/tests/boundingbox_tests.js +53 -0
  123. package/build/esm/tests/browser_test_runner.js +128 -0
  124. package/build/esm/tests/chordsymbol_tests.js +405 -0
  125. package/build/esm/tests/clef_tests.js +141 -0
  126. package/build/esm/tests/crossbeam_tests.js +306 -0
  127. package/build/esm/tests/curve_tests.js +152 -0
  128. package/build/esm/tests/demo_petzold_tests.js +335 -0
  129. package/build/esm/tests/dot_tests.js +119 -0
  130. package/build/esm/tests/easyscore_tests.js +488 -0
  131. package/build/esm/tests/factory_tests.js +44 -0
  132. package/build/esm/tests/font_tests.js +134 -0
  133. package/build/esm/tests/formatter/framestack.js +61 -0
  134. package/build/esm/tests/formatter/tests.js +251 -0
  135. package/build/esm/tests/formatter_tests.js +759 -0
  136. package/build/esm/tests/fraction_tests.js +72 -0
  137. package/build/esm/tests/ghostnote_tests.js +77 -0
  138. package/build/esm/tests/glyphnote_tests.js +119 -0
  139. package/build/esm/tests/gracenote_tests.js +414 -0
  140. package/build/esm/tests/gracetabnote_tests.js +79 -0
  141. package/build/esm/tests/index.js +68 -0
  142. package/build/esm/tests/key_clef_tests.js +106 -0
  143. package/build/esm/tests/keymanager_tests.js +68 -0
  144. package/build/esm/tests/keysignature_tests.js +322 -0
  145. package/build/esm/tests/mocks.js +70 -0
  146. package/build/esm/tests/modifier_tests.js +24 -0
  147. package/build/esm/tests/multimeasurerest_tests.js +141 -0
  148. package/build/esm/tests/music_tests.js +161 -0
  149. package/build/esm/tests/node_test_runner.js +31 -0
  150. package/build/esm/tests/notehead_tests.js +224 -0
  151. package/build/esm/tests/notesubgroup_tests.js +149 -0
  152. package/build/esm/tests/offscreencanvas_tests.js +47 -0
  153. package/build/esm/tests/ornament_tests.js +333 -0
  154. package/build/esm/tests/parser_tests.js +122 -0
  155. package/build/esm/tests/pedalmarking_tests.js +91 -0
  156. package/build/esm/tests/percussion_tests.js +184 -0
  157. package/build/esm/tests/playwright/flow.spec.js +127 -0
  158. package/build/esm/tests/playwright/visual_helpers.js +30 -0
  159. package/build/esm/tests/registry_tests.js +64 -0
  160. package/build/esm/tests/renderer_tests.js +113 -0
  161. package/build/esm/tests/rests_tests.js +266 -0
  162. package/build/esm/tests/rhythm_tests.js +357 -0
  163. package/build/esm/tests/stave_tests.js +570 -0
  164. package/build/esm/tests/staveconnector_tests.js +402 -0
  165. package/build/esm/tests/stavehairpin_tests.js +90 -0
  166. package/build/esm/tests/staveline_tests.js +123 -0
  167. package/build/esm/tests/stavemodifier_tests.js +51 -0
  168. package/build/esm/tests/stavenote_tests.js +936 -0
  169. package/build/esm/tests/stavetie_tests.js +137 -0
  170. package/build/esm/tests/stringnumber_tests.js +283 -0
  171. package/build/esm/tests/strokes_tests.js +289 -0
  172. package/build/esm/tests/style_tests.js +96 -0
  173. package/build/esm/tests/tabnote_tests.js +462 -0
  174. package/build/esm/tests/tabslide_tests.js +133 -0
  175. package/build/esm/tests/tabstave_tests.js +24 -0
  176. package/build/esm/tests/tabtie_tests.js +171 -0
  177. package/build/esm/tests/test_runner.js +23 -0
  178. package/build/esm/tests/textbracket_tests.js +100 -0
  179. package/build/esm/tests/textnote_tests.js +216 -0
  180. package/build/esm/tests/threevoice_tests.js +132 -0
  181. package/build/esm/tests/tickcontext_tests.js +36 -0
  182. package/build/esm/tests/timesignature_tests.js +173 -0
  183. package/build/esm/tests/tremolo_tests.js +34 -0
  184. package/build/esm/tests/tuning_tests.js +56 -0
  185. package/build/esm/tests/tuplet_tests.js +711 -0
  186. package/build/esm/tests/typeguard_tests.js +82 -0
  187. package/build/esm/tests/unison_tests.js +74 -0
  188. package/build/esm/tests/vexflow_test_helpers.js +318 -0
  189. package/build/esm/tests/vf_prefix_tests.js +101 -0
  190. package/build/esm/tests/vibrato_tests.js +93 -0
  191. package/build/esm/tests/vibratobracket_tests.js +45 -0
  192. package/build/esm/tests/vitest/job-0.test.js +2 -0
  193. package/build/esm/tests/vitest/job-1.test.js +2 -0
  194. package/build/esm/tests/vitest/job-2.test.js +2 -0
  195. package/build/esm/tests/vitest/job-3.test.js +2 -0
  196. package/build/esm/tests/vitest/run.js +5 -0
  197. package/build/esm/tests/vitest.setup.js +44 -0
  198. package/build/esm/tests/voice_tests.js +69 -0
  199. package/build/types/entry/vexflow-bravura.d.ts +3 -0
  200. package/build/types/entry/vexflow-core.d.ts +3 -0
  201. package/build/types/entry/vexflow-debug.d.ts +3 -0
  202. package/build/types/entry/vexflow.d.ts +3 -0
  203. package/build/types/src/accidental.d.ts +60 -0
  204. package/build/types/src/annotation.d.ts +58 -0
  205. package/build/types/src/articulation.d.ts +78 -0
  206. package/build/types/src/barnote.d.ts +29 -0
  207. package/build/types/src/beam.d.ts +158 -0
  208. package/build/types/src/bend.d.ts +67 -0
  209. package/build/types/src/boundingbox.d.ts +42 -0
  210. package/build/types/src/canvascontext.d.ts +93 -0
  211. package/build/types/src/chordsymbol.d.ts +109 -0
  212. package/build/types/src/clef.d.ts +38 -0
  213. package/build/types/src/clefnote.d.ts +17 -0
  214. package/build/types/src/crescendo.d.ts +26 -0
  215. package/build/types/src/curve.d.ts +42 -0
  216. package/build/types/src/dot.d.ts +19 -0
  217. package/build/types/src/easyscore.d.ts +152 -0
  218. package/build/types/src/element.d.ts +278 -0
  219. package/build/types/src/factory.d.ts +273 -0
  220. package/build/types/src/flag.d.ts +12 -0
  221. package/build/types/src/font.d.ts +96 -0
  222. package/build/types/src/fonts/academico.d.ts +1 -0
  223. package/build/types/src/fonts/academicobold.d.ts +1 -0
  224. package/build/types/src/fonts/bravura.d.ts +1 -0
  225. package/build/types/src/fonts/gonville.d.ts +1 -0
  226. package/build/types/src/fonts/petaluma.d.ts +1 -0
  227. package/build/types/src/fonts/petalumascript.d.ts +1 -0
  228. package/build/types/src/formatter.d.ts +226 -0
  229. package/build/types/src/fraction.d.ts +65 -0
  230. package/build/types/src/frethandfinger.d.ts +19 -0
  231. package/build/types/src/ghostnote.d.ts +16 -0
  232. package/build/types/src/glyphnote.d.ts +14 -0
  233. package/build/types/src/glyphs.d.ts +2935 -0
  234. package/build/types/src/gracenote.d.ts +17 -0
  235. package/build/types/src/gracenotegroup.d.ts +33 -0
  236. package/build/types/src/gracetabnote.d.ts +5 -0
  237. package/build/types/src/index.d.ts +86 -0
  238. package/build/types/src/keymanager.d.ts +25 -0
  239. package/build/types/src/keysignature.d.ts +50 -0
  240. package/build/types/src/keysignote.d.ts +11 -0
  241. package/build/types/src/metrics.d.ts +25 -0
  242. package/build/types/src/modifier.d.ts +90 -0
  243. package/build/types/src/modifiercontext.d.ts +48 -0
  244. package/build/types/src/multimeasurerest.d.ts +61 -0
  245. package/build/types/src/music.d.ts +91 -0
  246. package/build/types/src/note.d.ts +232 -0
  247. package/build/types/src/notehead.d.ts +50 -0
  248. package/build/types/src/notesubgroup.d.ts +18 -0
  249. package/build/types/src/ornament.d.ts +71 -0
  250. package/build/types/src/parenthesis.d.ts +21 -0
  251. package/build/types/src/parser.d.ts +45 -0
  252. package/build/types/src/pedalmarking.d.ts +63 -0
  253. package/build/types/src/registry.d.ts +40 -0
  254. package/build/types/src/rendercontext.d.ts +59 -0
  255. package/build/types/src/renderer.d.ts +35 -0
  256. package/build/types/src/repeatnote.d.ts +6 -0
  257. package/build/types/src/stave.d.ts +181 -0
  258. package/build/types/src/stavebarline.d.ts +38 -0
  259. package/build/types/src/staveconnector.d.ts +53 -0
  260. package/build/types/src/stavehairpin.d.ts +56 -0
  261. package/build/types/src/staveline.d.ts +49 -0
  262. package/build/types/src/stavemodifier.d.ts +35 -0
  263. package/build/types/src/stavenote.d.ts +150 -0
  264. package/build/types/src/staverepetition.d.ts +30 -0
  265. package/build/types/src/stavesection.d.ts +8 -0
  266. package/build/types/src/stavetempo.d.ts +43 -0
  267. package/build/types/src/stavetext.d.ts +11 -0
  268. package/build/types/src/stavetie.d.ts +104 -0
  269. package/build/types/src/stavevolta.d.ts +15 -0
  270. package/build/types/src/stem.d.ts +58 -0
  271. package/build/types/src/stemmablenote.d.ts +39 -0
  272. package/build/types/src/stringnumber.d.ts +26 -0
  273. package/build/types/src/strokes.d.ts +35 -0
  274. package/build/types/src/svgcontext.d.ts +135 -0
  275. package/build/types/src/system.d.ts +121 -0
  276. package/build/types/src/tables.d.ts +86 -0
  277. package/build/types/src/tabnote.d.ts +45 -0
  278. package/build/types/src/tabslide.d.ts +36 -0
  279. package/build/types/src/tabstave.d.ts +7 -0
  280. package/build/types/src/tabtie.d.ts +18 -0
  281. package/build/types/src/textbracket.d.ts +45 -0
  282. package/build/types/src/textdynamics.d.ts +34 -0
  283. package/build/types/src/textnote.d.ts +48 -0
  284. package/build/types/src/tickable.d.ts +149 -0
  285. package/build/types/src/tickcontext.d.ts +112 -0
  286. package/build/types/src/timesignature.d.ts +75 -0
  287. package/build/types/src/timesignote.d.ts +11 -0
  288. package/build/types/src/tremolo.d.ts +12 -0
  289. package/build/types/src/tuning.d.ts +23 -0
  290. package/build/types/src/tuplet.d.ts +98 -0
  291. package/build/types/src/typeguard.d.ts +100 -0
  292. package/build/types/src/util.d.ts +42 -0
  293. package/build/types/src/version.d.ts +3 -0
  294. package/build/types/src/vexflow.d.ts +261 -0
  295. package/build/types/src/vibrato.d.ts +20 -0
  296. package/build/types/src/vibratobracket.d.ts +28 -0
  297. package/build/types/src/voice.d.ts +109 -0
  298. package/build/types/src/web.d.ts +2 -0
  299. package/package.json +145 -0
  300. package/tests/flow.html +256 -0
@@ -0,0 +1,644 @@
1
+ import { Element } from './element.js';
2
+ import { Fraction } from './fraction.js';
3
+ import { Stem } from './stem.js';
4
+ import { Tables } from './tables.js';
5
+ import { isStaveNote, isTabNote } from './typeguard.js';
6
+ import { RuntimeError } from './util.js';
7
+ function calculateStemDirection(notes) {
8
+ let lineSum = 0;
9
+ notes.forEach((note) => {
10
+ if (note.keyProps) {
11
+ note.keyProps.forEach((keyProp) => {
12
+ lineSum += keyProp.line - 3;
13
+ });
14
+ }
15
+ });
16
+ if (lineSum >= 0) {
17
+ return Stem.DOWN;
18
+ }
19
+ return Stem.UP;
20
+ }
21
+ function getStemSlope(firstNote, lastNote) {
22
+ const firstStemTipY = firstNote.getStemExtents().topY;
23
+ const firstStemX = firstNote.getStemX();
24
+ const lastStemTipY = lastNote.getStemExtents().topY;
25
+ const lastStemX = lastNote.getStemX();
26
+ return (lastStemTipY - firstStemTipY) / (lastStemX - firstStemX);
27
+ }
28
+ export const BEAM_LEFT = 'L';
29
+ export const BEAM_RIGHT = 'R';
30
+ export const BEAM_BOTH = 'B';
31
+ export class Beam extends Element {
32
+ static get CATEGORY() {
33
+ return "Beam";
34
+ }
35
+ getStemDirection() {
36
+ return this._stemDirection;
37
+ }
38
+ static getDefaultBeamGroups(timeSig) {
39
+ if (!timeSig || timeSig === 'c') {
40
+ timeSig = '4/4';
41
+ }
42
+ const defaults = {
43
+ '1/2': ['1/2'],
44
+ '2/2': ['1/2'],
45
+ '3/2': ['1/2'],
46
+ '4/2': ['1/2'],
47
+ '1/4': ['1/4'],
48
+ '2/4': ['1/4'],
49
+ '3/4': ['1/4'],
50
+ '4/4': ['1/4'],
51
+ '1/8': ['1/8'],
52
+ '2/8': ['2/8'],
53
+ '3/8': ['3/8'],
54
+ '4/8': ['2/8'],
55
+ '1/16': ['1/16'],
56
+ '2/16': ['2/16'],
57
+ '3/16': ['3/16'],
58
+ '4/16': ['2/16'],
59
+ };
60
+ const groups = defaults[timeSig];
61
+ if (groups === undefined) {
62
+ const beatTotal = parseInt(timeSig.split('/')[0], 10);
63
+ const beatValue = parseInt(timeSig.split('/')[1], 10);
64
+ const tripleMeter = beatTotal % 3 === 0;
65
+ if (tripleMeter) {
66
+ return [new Fraction(3, beatValue)];
67
+ }
68
+ else if (beatValue > 4) {
69
+ return [new Fraction(2, beatValue)];
70
+ }
71
+ else if (beatValue <= 4) {
72
+ return [new Fraction(1, beatValue)];
73
+ }
74
+ }
75
+ else {
76
+ return groups.map((group) => new Fraction().parse(group));
77
+ }
78
+ return [new Fraction(1, 4)];
79
+ }
80
+ static applyAndGetBeams(voice, stemDirection, groups) {
81
+ return Beam.generateBeams(voice.getTickables(), { groups, stemDirection });
82
+ }
83
+ static generateBeams(notes, config = {}) {
84
+ if (!config.groups || !config.groups.length) {
85
+ config.groups = [new Fraction(2, 8)];
86
+ }
87
+ const tickGroups = config.groups.map((group) => {
88
+ if (!group.multiply) {
89
+ throw new RuntimeError('InvalidBeamGroups', 'The beam groups must be an array of VexFlow.Fractions');
90
+ }
91
+ return group.clone().multiply(Tables.RESOLUTION, 1);
92
+ });
93
+ const unprocessedNotes = notes;
94
+ let currentTickGroup = 0;
95
+ let noteGroups = [];
96
+ let currentGroup = [];
97
+ function getTotalTicks(notes) {
98
+ return notes.reduce((memo, note) => note.getTicks().clone().add(memo), new Fraction(0, 1));
99
+ }
100
+ function nextTickGroup() {
101
+ if (tickGroups.length - 1 > currentTickGroup) {
102
+ currentTickGroup += 1;
103
+ }
104
+ else {
105
+ currentTickGroup = 0;
106
+ }
107
+ }
108
+ function createGroups() {
109
+ let nextGroup = [];
110
+ let currentGroupTotalTicks = new Fraction(0, 1);
111
+ unprocessedNotes.forEach((unprocessedNote) => {
112
+ nextGroup = [];
113
+ if (unprocessedNote.shouldIgnoreTicks()) {
114
+ noteGroups.push(currentGroup);
115
+ currentGroup = nextGroup;
116
+ return;
117
+ }
118
+ currentGroup.push(unprocessedNote);
119
+ const ticksPerGroup = tickGroups[currentTickGroup].clone();
120
+ const totalTicks = getTotalTicks(currentGroup).add(currentGroupTotalTicks);
121
+ const unbeamable = Tables.durationToNumber(unprocessedNote.getDuration()) < 8;
122
+ if (unbeamable && unprocessedNote.getTuplet()) {
123
+ ticksPerGroup.numerator *= 2;
124
+ }
125
+ if (totalTicks.greaterThan(ticksPerGroup)) {
126
+ if (!unbeamable) {
127
+ const note = currentGroup.pop();
128
+ if (note)
129
+ nextGroup.push(note);
130
+ }
131
+ noteGroups.push(currentGroup);
132
+ do {
133
+ currentGroupTotalTicks = totalTicks.subtract(tickGroups[currentTickGroup]);
134
+ nextTickGroup();
135
+ } while (currentGroupTotalTicks.greaterThanEquals(tickGroups[currentTickGroup]));
136
+ currentGroup = nextGroup;
137
+ }
138
+ else if (totalTicks.equals(ticksPerGroup)) {
139
+ noteGroups.push(currentGroup);
140
+ currentGroupTotalTicks = new Fraction(0, 1);
141
+ currentGroup = nextGroup;
142
+ nextTickGroup();
143
+ }
144
+ });
145
+ if (currentGroup.length > 0) {
146
+ noteGroups.push(currentGroup);
147
+ }
148
+ }
149
+ function getBeamGroups() {
150
+ return noteGroups.filter((group) => {
151
+ if (group.length > 1) {
152
+ let beamable = true;
153
+ group.forEach((note) => {
154
+ if (note.getIntrinsicTicks() >= Tables.durationToTicks('4')) {
155
+ beamable = false;
156
+ }
157
+ });
158
+ return beamable;
159
+ }
160
+ return false;
161
+ });
162
+ }
163
+ function sanitizeGroups() {
164
+ const sanitizedGroups = [];
165
+ noteGroups.forEach((group) => {
166
+ let tempGroup = [];
167
+ group.forEach((note, index, group) => {
168
+ const isFirstOrLast = index === 0 || index === group.length - 1;
169
+ const prevNote = group[index - 1];
170
+ const breaksOnEachRest = !config.beamRests && note.isRest();
171
+ const breaksOnFirstOrLastRest = config.beamRests && config.beamMiddleOnly && note.isRest() && isFirstOrLast;
172
+ let breakOnStemChange = false;
173
+ if (config.maintainStemDirections && prevNote && !note.isRest() && !prevNote.isRest()) {
174
+ const prevDirection = prevNote.getStemDirection();
175
+ const currentDirection = note.getStemDirection();
176
+ breakOnStemChange = currentDirection !== prevDirection;
177
+ }
178
+ const isUnbeamableDuration = parseInt(note.getDuration(), 10) < 8;
179
+ const shouldBreak = breaksOnEachRest || breaksOnFirstOrLastRest || breakOnStemChange || isUnbeamableDuration;
180
+ if (shouldBreak) {
181
+ if (tempGroup.length > 0) {
182
+ sanitizedGroups.push(tempGroup);
183
+ }
184
+ tempGroup = breakOnStemChange ? [note] : [];
185
+ }
186
+ else {
187
+ tempGroup.push(note);
188
+ }
189
+ });
190
+ if (tempGroup.length > 0) {
191
+ sanitizedGroups.push(tempGroup);
192
+ }
193
+ });
194
+ noteGroups = sanitizedGroups;
195
+ }
196
+ function formatStems() {
197
+ noteGroups.forEach((group) => {
198
+ let stemDirection;
199
+ if (config.maintainStemDirections) {
200
+ const note = findFirstNote(group);
201
+ stemDirection = note ? note.getStemDirection() : Stem.UP;
202
+ }
203
+ else {
204
+ if (config.stemDirection) {
205
+ stemDirection = config.stemDirection;
206
+ }
207
+ else {
208
+ stemDirection = calculateStemDirection(group);
209
+ }
210
+ }
211
+ applyStemDirection(group, stemDirection);
212
+ });
213
+ }
214
+ function findFirstNote(group) {
215
+ for (let i = 0; i < group.length; i++) {
216
+ const note = group[i];
217
+ if (!note.isRest()) {
218
+ return note;
219
+ }
220
+ }
221
+ return false;
222
+ }
223
+ function applyStemDirection(group, direction) {
224
+ group.forEach((note) => {
225
+ note.setStemDirection(direction);
226
+ });
227
+ }
228
+ function getTuplets() {
229
+ const uniqueTuplets = [];
230
+ noteGroups.forEach((group) => {
231
+ let tuplet;
232
+ group.forEach((note) => {
233
+ const noteTuplet = note.getTuplet();
234
+ if (noteTuplet && tuplet !== noteTuplet) {
235
+ tuplet = noteTuplet;
236
+ uniqueTuplets.push(tuplet);
237
+ }
238
+ });
239
+ });
240
+ return uniqueTuplets;
241
+ }
242
+ createGroups();
243
+ sanitizeGroups();
244
+ formatStems();
245
+ const beamedNoteGroups = getBeamGroups();
246
+ const allTuplets = getTuplets();
247
+ const beams = [];
248
+ beamedNoteGroups.forEach((group) => {
249
+ const beam = new Beam(group);
250
+ if (config.showStemlets) {
251
+ beam.renderOptions.showStemlets = true;
252
+ }
253
+ if (config.secondaryBreaks) {
254
+ beam.renderOptions.secondaryBreakTicks = Tables.durationToTicks(config.secondaryBreaks);
255
+ }
256
+ if (config.flatBeams === true) {
257
+ beam.renderOptions.flatBeams = true;
258
+ beam.renderOptions.flatBeamOffset = config.flatBeamOffset;
259
+ }
260
+ beams.push(beam);
261
+ });
262
+ allTuplets.forEach((tuplet) => {
263
+ const direction = tuplet.notes[0].stemDirection === Stem.DOWN ? -1 : 1;
264
+ tuplet.setTupletLocation(direction);
265
+ let bracketed = false;
266
+ for (let i = 0; i < tuplet.notes.length; i++) {
267
+ const note = tuplet.notes[i];
268
+ if (!note.hasBeam()) {
269
+ bracketed = true;
270
+ break;
271
+ }
272
+ }
273
+ tuplet.setBracketed(bracketed);
274
+ });
275
+ return beams;
276
+ }
277
+ constructor(notes, autoStem = false) {
278
+ super();
279
+ this.slope = 0;
280
+ this.yShift = 0;
281
+ this.forcedPartialDirections = {};
282
+ if (!notes || notes.length === 0) {
283
+ throw new RuntimeError('BadArguments', 'No notes provided for beam.');
284
+ }
285
+ if (notes.length === 1) {
286
+ throw new RuntimeError('BadArguments', 'Too few notes for beam.');
287
+ }
288
+ this._ticks = notes[0].getIntrinsicTicks();
289
+ if (this._ticks >= Tables.durationToTicks('4')) {
290
+ throw new RuntimeError('BadArguments', 'Beams can only be applied to notes shorter than a quarter note.');
291
+ }
292
+ let i;
293
+ let note;
294
+ this._stemDirection = notes[0].getStemDirection();
295
+ let stemDirection = this._stemDirection;
296
+ if (autoStem && isStaveNote(notes[0])) {
297
+ stemDirection = calculateStemDirection(notes);
298
+ }
299
+ else if (autoStem && isTabNote(notes[0])) {
300
+ const stemWeight = notes.reduce((memo, note) => memo + note.getStemDirection(), 0);
301
+ stemDirection = stemWeight > -1 ? Stem.UP : Stem.DOWN;
302
+ }
303
+ for (i = 0; i < notes.length; ++i) {
304
+ note = notes[i];
305
+ if (autoStem) {
306
+ note.setStemDirection(stemDirection);
307
+ this._stemDirection = stemDirection;
308
+ }
309
+ note.setBeam(this);
310
+ }
311
+ this.postFormatted = false;
312
+ this.notes = notes;
313
+ this._beamCount = this.getBeamCount();
314
+ this.breakOnIndexes = [];
315
+ this.renderOptions = {
316
+ beamWidth: 5,
317
+ maxSlope: 0.25,
318
+ minSlope: -0.25,
319
+ slopeIterations: 20,
320
+ slopeCost: 100,
321
+ showStemlets: false,
322
+ stemletExtension: 7,
323
+ partialBeamLength: 10,
324
+ flatBeams: false,
325
+ minFlatBeamOffset: 15,
326
+ };
327
+ }
328
+ getNotes() {
329
+ return this.notes;
330
+ }
331
+ getBeamCount() {
332
+ const beamCounts = this.notes.map((note) => note.getGlyphProps().beamCount);
333
+ const maxBeamCount = beamCounts.reduce((max, beamCount) => (beamCount > max ? beamCount : max));
334
+ return maxBeamCount;
335
+ }
336
+ breakSecondaryAt(indexes) {
337
+ this.breakOnIndexes = indexes;
338
+ return this;
339
+ }
340
+ setPartialBeamSideAt(noteIndex, side) {
341
+ this.forcedPartialDirections[noteIndex] = side;
342
+ return this;
343
+ }
344
+ unsetPartialBeamSideAt(noteIndex) {
345
+ delete this.forcedPartialDirections[noteIndex];
346
+ return this;
347
+ }
348
+ getSlopeY(x, firstX, firstY, slope) {
349
+ return firstY + (x - firstX) * slope;
350
+ }
351
+ calculateSlope() {
352
+ const { notes, renderOptions: { maxSlope, minSlope, slopeIterations, slopeCost }, } = this;
353
+ const stemDirection = this._stemDirection;
354
+ const firstNote = notes[0];
355
+ const initialSlope = getStemSlope(firstNote, notes[notes.length - 1]);
356
+ const increment = (maxSlope - minSlope) / slopeIterations;
357
+ let minCost = Number.MAX_VALUE;
358
+ let bestSlope = 0;
359
+ let yShift = 0;
360
+ for (let slope = minSlope; slope <= maxSlope; slope += increment) {
361
+ let totalStemExtension = 0;
362
+ let yShiftTemp = 0;
363
+ for (let i = 1; i < notes.length; ++i) {
364
+ const note = notes[i];
365
+ if (note.hasStem() || note.isRest()) {
366
+ const adjustedStemTipY = this.getSlopeY(note.getStemX(), firstNote.getStemX(), firstNote.getStemExtents().topY, slope) + yShiftTemp;
367
+ const stemTipY = note.getStemExtents().topY;
368
+ if (stemTipY * stemDirection < adjustedStemTipY * stemDirection) {
369
+ const diff = Math.abs(stemTipY - adjustedStemTipY);
370
+ yShiftTemp += diff * -stemDirection;
371
+ totalStemExtension += diff * i;
372
+ }
373
+ else {
374
+ totalStemExtension += (stemTipY - adjustedStemTipY) * stemDirection;
375
+ }
376
+ }
377
+ }
378
+ const idealSlope = initialSlope / 2;
379
+ const distanceFromIdeal = Math.abs(idealSlope - slope);
380
+ const cost = slopeCost * distanceFromIdeal + Math.abs(totalStemExtension);
381
+ if (cost < minCost) {
382
+ minCost = cost;
383
+ bestSlope = slope;
384
+ yShift = yShiftTemp;
385
+ }
386
+ }
387
+ this.slope = bestSlope;
388
+ this.yShift = yShift;
389
+ }
390
+ calculateFlatSlope() {
391
+ const { notes, renderOptions: { beamWidth, minFlatBeamOffset, flatBeamOffset }, } = this;
392
+ const stemDirection = this._stemDirection;
393
+ let total = 0;
394
+ let extremeY = 0;
395
+ let extremeBeamCount = 0;
396
+ let currentExtreme = 0;
397
+ for (let i = 0; i < notes.length; i++) {
398
+ const note = notes[i];
399
+ const stemTipY = note.getStemExtents().topY;
400
+ total += stemTipY;
401
+ if (stemDirection === Stem.DOWN && currentExtreme < stemTipY) {
402
+ currentExtreme = stemTipY;
403
+ extremeY = Math.max(...note.getYs());
404
+ extremeBeamCount = note.getBeamCount();
405
+ }
406
+ else if (stemDirection === Stem.UP && (currentExtreme === 0 || currentExtreme > stemTipY)) {
407
+ currentExtreme = stemTipY;
408
+ extremeY = Math.min(...note.getYs());
409
+ extremeBeamCount = note.getBeamCount();
410
+ }
411
+ }
412
+ let offset = total / notes.length;
413
+ const extremeTest = minFlatBeamOffset + extremeBeamCount * beamWidth * 1.5;
414
+ const newOffset = extremeY + extremeTest * -stemDirection;
415
+ if (stemDirection === Stem.DOWN && offset < newOffset) {
416
+ offset = extremeY + extremeTest;
417
+ }
418
+ else if (stemDirection === Stem.UP && offset > newOffset) {
419
+ offset = extremeY - extremeTest;
420
+ }
421
+ if (!flatBeamOffset) {
422
+ this.renderOptions.flatBeamOffset = offset;
423
+ }
424
+ else if (stemDirection === Stem.DOWN && offset > flatBeamOffset) {
425
+ this.renderOptions.flatBeamOffset = offset;
426
+ }
427
+ else if (stemDirection === Stem.UP && offset < flatBeamOffset) {
428
+ this.renderOptions.flatBeamOffset = offset;
429
+ }
430
+ this.slope = 0;
431
+ this.yShift = 0;
432
+ }
433
+ getBeamYToDraw() {
434
+ const firstNote = this.notes[0];
435
+ const firstStemTipY = firstNote.getStemExtents().topY;
436
+ let beamY = firstStemTipY;
437
+ if (this.renderOptions.flatBeams && this.renderOptions.flatBeamOffset) {
438
+ beamY = this.renderOptions.flatBeamOffset;
439
+ }
440
+ return beamY;
441
+ }
442
+ applyStemExtensions() {
443
+ const { notes, slope, renderOptions: { showStemlets, stemletExtension, beamWidth }, } = this;
444
+ const yShift = this.yShift;
445
+ const beamCount = this._beamCount;
446
+ const firstNote = notes[0];
447
+ const firstStemTipY = this.getBeamYToDraw();
448
+ const firstStemX = firstNote.getStemX();
449
+ for (let i = 0; i < notes.length; ++i) {
450
+ const note = notes[i];
451
+ const stem = note.getStem();
452
+ if (stem) {
453
+ const stemX = note.getStemX();
454
+ const { topY: stemTipY } = note.getStemExtents();
455
+ const beamedStemTipY = this.getSlopeY(stemX, firstStemX, firstStemTipY, slope) + yShift;
456
+ const preBeamExtension = stem.getExtension();
457
+ const beamExtension = note.getStemDirection() === Stem.UP ? stemTipY - beamedStemTipY : beamedStemTipY - stemTipY;
458
+ let crossStemExtension = 0;
459
+ if (note.getStemDirection() !== this._stemDirection) {
460
+ const beamCount = note.getGlyphProps().beamCount;
461
+ crossStemExtension = (1 + (beamCount - 1) * 1.5) * this.renderOptions.beamWidth;
462
+ }
463
+ stem.setExtension(preBeamExtension + beamExtension + crossStemExtension);
464
+ stem.adjustHeightForBeam();
465
+ if (note.isRest() && showStemlets) {
466
+ const totalBeamWidth = (beamCount - 1) * beamWidth * 1.5 + beamWidth;
467
+ stem.setVisibility(true).setStemlet(true, totalBeamWidth + stemletExtension);
468
+ }
469
+ }
470
+ }
471
+ }
472
+ lookupBeamDirection(duration, prevTick, tick, nextTick, noteIndex) {
473
+ if (duration === '4') {
474
+ return BEAM_LEFT;
475
+ }
476
+ const forcedBeamDirection = this.forcedPartialDirections[noteIndex];
477
+ if (forcedBeamDirection)
478
+ return forcedBeamDirection;
479
+ const lookupDuration = `${Tables.durationToNumber(duration) / 2}`;
480
+ const prevNoteGetsBeam = prevTick < Tables.durationToTicks(lookupDuration);
481
+ const nextNoteGetsBeam = nextTick < Tables.durationToTicks(lookupDuration);
482
+ const noteGetsBeam = tick < Tables.durationToTicks(lookupDuration);
483
+ if (prevNoteGetsBeam && nextNoteGetsBeam && noteGetsBeam) {
484
+ return BEAM_BOTH;
485
+ }
486
+ else if (prevNoteGetsBeam && !nextNoteGetsBeam && noteGetsBeam) {
487
+ return BEAM_LEFT;
488
+ }
489
+ else if (!prevNoteGetsBeam && nextNoteGetsBeam && noteGetsBeam) {
490
+ return BEAM_RIGHT;
491
+ }
492
+ return this.lookupBeamDirection(lookupDuration, prevTick, tick, nextTick, noteIndex);
493
+ }
494
+ getBeamLines(duration) {
495
+ const tickOfDuration = Tables.durationToTicks(duration);
496
+ let beamStarted = false;
497
+ const beamLines = [];
498
+ let currentBeam = undefined;
499
+ const partialBeamLength = this.renderOptions.partialBeamLength;
500
+ let previousShouldBreak = false;
501
+ let tickTally = 0;
502
+ for (let i = 0; i < this.notes.length; ++i) {
503
+ const note = this.notes[i];
504
+ const ticks = note.getTicks().value();
505
+ tickTally += ticks;
506
+ let shouldBreak = false;
507
+ if (parseInt(duration, 10) >= 8) {
508
+ shouldBreak = this.breakOnIndexes.indexOf(i) !== -1;
509
+ if (this.renderOptions.secondaryBreakTicks && tickTally >= this.renderOptions.secondaryBreakTicks) {
510
+ tickTally = 0;
511
+ shouldBreak = true;
512
+ }
513
+ }
514
+ const noteGetsBeam = note.getIntrinsicTicks() < tickOfDuration;
515
+ const stemX = note.getStemX() - Stem.WIDTH / 2;
516
+ const prevNote = this.notes[i - 1];
517
+ const nextNote = this.notes[i + 1];
518
+ const nextNoteGetsBeam = nextNote && nextNote.getIntrinsicTicks() < tickOfDuration;
519
+ const prevNoteGetsBeam = prevNote && prevNote.getIntrinsicTicks() < tickOfDuration;
520
+ const beamAlone = prevNote && nextNote && noteGetsBeam && !prevNoteGetsBeam && !nextNoteGetsBeam;
521
+ if (noteGetsBeam) {
522
+ if (beamStarted) {
523
+ currentBeam = beamLines[beamLines.length - 1];
524
+ currentBeam.end = stemX;
525
+ if (shouldBreak) {
526
+ beamStarted = false;
527
+ if (nextNote && !nextNoteGetsBeam && currentBeam.end === undefined) {
528
+ currentBeam.end = currentBeam.start - partialBeamLength;
529
+ }
530
+ }
531
+ }
532
+ else {
533
+ currentBeam = { start: stemX, end: undefined };
534
+ beamStarted = true;
535
+ if (beamAlone) {
536
+ const prevTick = prevNote.getIntrinsicTicks();
537
+ const nextTick = nextNote.getIntrinsicTicks();
538
+ const tick = note.getIntrinsicTicks();
539
+ const beamDirection = this.lookupBeamDirection(duration, prevTick, tick, nextTick, i);
540
+ if ([BEAM_LEFT, BEAM_BOTH].includes(beamDirection)) {
541
+ currentBeam.end = currentBeam.start - partialBeamLength;
542
+ }
543
+ else {
544
+ currentBeam.end = currentBeam.start + partialBeamLength;
545
+ }
546
+ }
547
+ else if (!nextNoteGetsBeam) {
548
+ if ((previousShouldBreak || i === 0) && nextNote) {
549
+ currentBeam.end = currentBeam.start + partialBeamLength;
550
+ }
551
+ else {
552
+ currentBeam.end = currentBeam.start - partialBeamLength;
553
+ }
554
+ }
555
+ else if (shouldBreak) {
556
+ currentBeam.end = currentBeam.start - partialBeamLength;
557
+ beamStarted = false;
558
+ }
559
+ beamLines.push(currentBeam);
560
+ }
561
+ }
562
+ else {
563
+ beamStarted = false;
564
+ }
565
+ previousShouldBreak = shouldBreak;
566
+ }
567
+ const lastBeam = beamLines[beamLines.length - 1];
568
+ if (lastBeam && lastBeam.end === undefined) {
569
+ lastBeam.end = lastBeam.start - partialBeamLength;
570
+ }
571
+ return beamLines;
572
+ }
573
+ drawStems(ctx) {
574
+ this.notes.forEach((note) => {
575
+ const stem = note.getStem();
576
+ if (stem) {
577
+ const stemX = note.getStemX();
578
+ stem.setNoteHeadXBounds(stemX, stemX);
579
+ stem.setContext(ctx).drawWithStyle();
580
+ }
581
+ }, this);
582
+ }
583
+ drawBeamLines(ctx) {
584
+ const validBeamDurations = ['4', '8', '16', '32', '64', '128', '256', '512', '1024'];
585
+ const firstNote = this.notes[0];
586
+ let beamY = this.getBeamYToDraw();
587
+ const firstStemX = firstNote.getStemX();
588
+ const beamThickness = this.renderOptions.beamWidth * this._stemDirection;
589
+ for (let i = 0; i < validBeamDurations.length; ++i) {
590
+ const duration = validBeamDurations[i];
591
+ const beamLines = this.getBeamLines(duration);
592
+ for (let j = 0; j < beamLines.length; ++j) {
593
+ const beamLine = beamLines[j];
594
+ const startBeamX = beamLine.start;
595
+ const startBeamY = this.getSlopeY(startBeamX, firstStemX, beamY, this.slope);
596
+ const lastBeamX = beamLine.end;
597
+ if (lastBeamX) {
598
+ const lastBeamY = this.getSlopeY(lastBeamX, firstStemX, beamY, this.slope);
599
+ ctx.beginPath();
600
+ ctx.moveTo(startBeamX, startBeamY);
601
+ ctx.lineTo(startBeamX, startBeamY + beamThickness);
602
+ ctx.lineTo(lastBeamX + 1, lastBeamY + beamThickness);
603
+ ctx.lineTo(lastBeamX + 1, lastBeamY);
604
+ ctx.closePath();
605
+ ctx.fill();
606
+ }
607
+ else {
608
+ throw new RuntimeError('NoLastBeamX', 'lastBeamX undefined.');
609
+ }
610
+ }
611
+ beamY += beamThickness * 1.5;
612
+ }
613
+ }
614
+ preFormat() {
615
+ return this;
616
+ }
617
+ postFormat() {
618
+ if (this.postFormatted)
619
+ return;
620
+ if (isTabNote(this.notes[0]) || this.renderOptions.flatBeams) {
621
+ this.calculateFlatSlope();
622
+ }
623
+ else {
624
+ this.calculateSlope();
625
+ }
626
+ this.applyStemExtensions();
627
+ this.postFormatted = true;
628
+ }
629
+ draw() {
630
+ const ctx = this.checkContext();
631
+ this.setRendered();
632
+ if (this.unbeamable)
633
+ return;
634
+ if (!this.postFormatted) {
635
+ this.postFormat();
636
+ }
637
+ const clsAttribute = this.getAttribute('class');
638
+ ctx.openGroup('beam' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
639
+ this.drawStems(ctx);
640
+ this.drawBeamLines(ctx);
641
+ this.drawPointerRect();
642
+ ctx.closeGroup();
643
+ }
644
+ }