abcjs 6.0.0-beta.7 → 6.0.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 (204) hide show
  1. package/.github/workflows/tests.yml +29 -0
  2. package/CODE_OF_CONDUCT.md +76 -0
  3. package/CONTRIBUTING.md +1 -0
  4. package/LICENSE.md +1 -1
  5. package/README.md +92 -3
  6. package/RELEASE.md +957 -1
  7. package/abcjs-audio.css +14 -5
  8. package/dist/.gitignore +1 -2
  9. package/dist/abcjs-basic-min.js +3 -0
  10. package/dist/abcjs-basic-min.js.LICENSE +23 -0
  11. package/dist/abcjs-basic.js +28231 -0
  12. package/dist/abcjs-basic.js.map +1 -0
  13. package/dist/abcjs-plugin-min.js +3 -0
  14. package/dist/abcjs-plugin-min.js.LICENSE +23 -0
  15. package/dist/report-basic.html +37 -0
  16. package/dist/report-before-glyph-compress.html +37 -0
  17. package/dist/report-brown-ts-target-es5.html +37 -0
  18. package/dist/report-dev-orig-no-babel.html +37 -0
  19. package/dist/report-synth.html +37 -0
  20. package/docker-build.sh +1 -0
  21. package/glyphs.json +1 -0
  22. package/index.js +27 -2
  23. package/{static-wrappers/license.js → license.js} +1 -1
  24. package/package.json +26 -29
  25. package/{src/plugin/abc_plugin.js → plugin.js} +31 -19
  26. package/src/api/abc_animation.js +1 -17
  27. package/src/api/abc_tablatures.js +144 -0
  28. package/src/api/abc_timing_callbacks.js +234 -116
  29. package/src/api/abc_tunebook.js +18 -67
  30. package/src/api/abc_tunebook_svg.js +38 -46
  31. package/src/data/abc_tune.js +232 -972
  32. package/src/data/deline-tune.js +199 -0
  33. package/src/edit/abc_editarea.js +112 -0
  34. package/src/edit/abc_editor.js +95 -221
  35. package/src/midi/abc_midi_create.js +48 -50
  36. package/src/parse/abc_common.js +0 -14
  37. package/src/parse/abc_parse.js +167 -1321
  38. package/src/parse/abc_parse_book.js +62 -0
  39. package/src/parse/abc_parse_directive.js +164 -41
  40. package/src/parse/abc_parse_header.js +116 -145
  41. package/src/parse/abc_parse_key_voice.js +26 -20
  42. package/src/parse/abc_parse_music.js +1337 -0
  43. package/src/parse/abc_tokenizer.js +21 -15
  44. package/src/parse/abc_transpose.js +3 -15
  45. package/src/parse/tune-builder.js +896 -0
  46. package/src/parse/wrap_lines.js +205 -453
  47. package/src/synth/abc_midi_flattener.js +1292 -0
  48. package/src/{midi → synth}/abc_midi_renderer.js +44 -17
  49. package/src/synth/abc_midi_sequencer.js +648 -0
  50. package/src/synth/active-audio-context.js +3 -14
  51. package/src/synth/cents-to-factor.js +10 -0
  52. package/src/synth/create-note-map.js +21 -32
  53. package/src/synth/create-synth-control.js +20 -103
  54. package/src/synth/create-synth.js +185 -77
  55. package/src/synth/download-buffer.js +7 -21
  56. package/src/synth/get-midi-file.js +13 -20
  57. package/src/synth/images/{loading.svg → loading.svg.js} +4 -0
  58. package/src/synth/images/loop.svg.js +65 -0
  59. package/src/synth/images/pause.svg.js +10 -0
  60. package/src/synth/images/play.svg.js +9 -0
  61. package/src/synth/images/{reset.svg → reset.svg.js} +5 -1
  62. package/src/synth/instrument-index-to-name.js +1 -16
  63. package/src/synth/load-note.js +37 -76
  64. package/src/synth/pitch-to-note-name.js +0 -15
  65. package/src/synth/pitches-to-perc.js +64 -0
  66. package/src/synth/place-note.js +78 -68
  67. package/src/synth/play-event.js +17 -18
  68. package/src/synth/register-audio-context.js +11 -23
  69. package/src/synth/sounds-cache.js +0 -15
  70. package/src/synth/supports-audio.js +9 -23
  71. package/src/synth/synth-controller.js +80 -49
  72. package/src/synth/synth-sequence.js +20 -34
  73. package/src/tablatures/instruments/guitar/guitar-fonts.js +19 -0
  74. package/src/tablatures/instruments/guitar/guitar-patterns.js +23 -0
  75. package/src/tablatures/instruments/guitar/tab-guitar.js +50 -0
  76. package/src/tablatures/instruments/string-patterns.js +277 -0
  77. package/src/tablatures/instruments/string-tablature.js +56 -0
  78. package/src/tablatures/instruments/tab-note.js +282 -0
  79. package/src/tablatures/instruments/tab-notes.js +41 -0
  80. package/src/tablatures/instruments/violin/tab-violin.js +47 -0
  81. package/src/tablatures/instruments/violin/violin-fonts.js +19 -0
  82. package/src/tablatures/instruments/violin/violin-patterns.js +23 -0
  83. package/src/tablatures/tab-absolute-elements.js +310 -0
  84. package/src/tablatures/tab-common.js +29 -0
  85. package/src/tablatures/tab-renderer.js +243 -0
  86. package/src/tablatures/transposer.js +110 -0
  87. package/src/test/abc_midi_lint.js +5 -22
  88. package/src/test/abc_midi_sequencer_lint.js +11 -14
  89. package/src/test/abc_parser_lint.js +136 -32
  90. package/src/test/abc_vertical_lint.js +94 -32
  91. package/src/test/rendering-lint.js +38 -5
  92. package/src/write/abc_absolute_element.js +112 -120
  93. package/src/write/abc_abstract_engraver.js +102 -253
  94. package/src/write/abc_beam_element.js +30 -290
  95. package/src/write/abc_brace_element.js +12 -121
  96. package/src/write/abc_create_clef.js +21 -32
  97. package/src/write/abc_create_key_signature.js +8 -26
  98. package/src/write/abc_create_note_head.js +107 -0
  99. package/src/write/abc_create_time_signature.js +2 -21
  100. package/src/write/abc_crescendo_element.js +3 -50
  101. package/src/write/abc_decoration.js +7 -30
  102. package/src/write/abc_dynamic_decoration.js +3 -37
  103. package/src/write/abc_ending_element.js +1 -57
  104. package/src/write/abc_engraver_controller.js +111 -234
  105. package/src/write/abc_glyphs.js +9 -19
  106. package/src/write/abc_relative_element.js +57 -97
  107. package/src/write/abc_renderer.js +10 -832
  108. package/src/write/abc_spacing.js +0 -15
  109. package/src/write/abc_staff_group_element.js +14 -349
  110. package/src/write/abc_tempo_element.js +9 -117
  111. package/src/write/abc_tie_element.js +5 -68
  112. package/src/write/abc_triplet_element.js +6 -124
  113. package/src/write/abc_voice_element.js +7 -222
  114. package/src/write/add-chord.js +103 -0
  115. package/src/write/add-text-if.js +33 -0
  116. package/src/write/bottom-text.js +79 -0
  117. package/src/write/calcHeight.js +17 -0
  118. package/src/write/classes.js +100 -0
  119. package/src/write/draw/absolute.js +68 -0
  120. package/src/write/draw/beam.js +56 -0
  121. package/src/write/draw/brace.js +106 -0
  122. package/src/write/draw/crescendo.js +38 -0
  123. package/src/write/draw/debug-box.js +8 -0
  124. package/src/write/draw/draw.js +56 -0
  125. package/src/write/draw/dynamics.js +20 -0
  126. package/src/write/draw/ending.js +46 -0
  127. package/src/write/draw/group-elements.js +66 -0
  128. package/src/write/draw/horizontal-line.js +25 -0
  129. package/src/write/draw/non-music.js +50 -0
  130. package/src/write/draw/print-line.js +24 -0
  131. package/src/write/draw/print-path.js +7 -0
  132. package/src/write/draw/print-stem.js +30 -0
  133. package/src/write/draw/print-symbol.js +59 -0
  134. package/src/write/draw/print-vertical-line.js +18 -0
  135. package/src/write/draw/relative.js +77 -0
  136. package/src/write/draw/round-number.js +5 -0
  137. package/src/write/draw/selectables.js +59 -0
  138. package/src/write/draw/separator.js +16 -0
  139. package/src/write/draw/set-paper-size.js +45 -0
  140. package/src/write/{sprintf.js → draw/sprintf.js} +0 -0
  141. package/src/write/draw/staff-group.js +226 -0
  142. package/src/write/draw/staff-line.js +9 -0
  143. package/src/write/draw/staff.js +33 -0
  144. package/src/write/draw/tab-line.js +40 -0
  145. package/src/write/draw/tempo.js +45 -0
  146. package/src/write/draw/text.js +71 -0
  147. package/src/write/draw/tie.js +97 -0
  148. package/src/write/draw/triplet.js +46 -0
  149. package/src/write/draw/voice.js +102 -0
  150. package/src/write/format-jazz-chord.js +15 -0
  151. package/src/write/free-text.js +41 -0
  152. package/src/write/get-font-and-attr.js +37 -0
  153. package/src/write/get-text-size.js +56 -0
  154. package/src/write/highlight.js +11 -0
  155. package/src/write/layout/VoiceElements.js +121 -0
  156. package/src/write/layout/beam.js +213 -0
  157. package/src/write/layout/get-left-edge-of-staff.js +56 -0
  158. package/src/write/layout/getBarYAt.js +6 -0
  159. package/src/write/layout/layout.js +94 -0
  160. package/src/write/layout/setUpperAndLowerElements.js +232 -0
  161. package/src/write/layout/staffGroup.js +146 -0
  162. package/src/write/layout/triplet.js +75 -0
  163. package/src/write/layout/voice.js +137 -0
  164. package/src/write/selection.js +188 -70
  165. package/src/write/separator.js +10 -0
  166. package/src/write/set-class.js +21 -0
  167. package/src/write/subtitle.js +12 -0
  168. package/src/write/svg.js +95 -43
  169. package/src/write/top-text.js +54 -0
  170. package/src/write/unhighlight.js +11 -0
  171. package/temp.txt +17 -0
  172. package/test.js +27 -64
  173. package/types/index.d.ts +1095 -0
  174. package/version.js +1 -1
  175. package/.babelrc +0 -5
  176. package/.eslintrc +0 -3
  177. package/.gitmodules +0 -3
  178. package/abcjs-midi.css +0 -166
  179. package/build-utils/loadPresets.js +0 -14
  180. package/build-utils/presets/webpack.analyze.js +0 -6
  181. package/build-utils/presets/webpack.optimize.js +0 -30
  182. package/build-utils/webpack.development.js +0 -14
  183. package/build-utils/webpack.production.js +0 -35
  184. package/deploy-docs.sh +0 -25
  185. package/docs/README.md +0 -33
  186. package/fix-versions.sh +0 -23
  187. package/mei.js +0 -43
  188. package/midi.js +0 -62
  189. package/src/api/abc_tunebook_midi.js +0 -116
  190. package/src/midi/abc_midi_controls.js +0 -701
  191. package/src/midi/abc_midi_flattener.js +0 -1119
  192. package/src/midi/abc_midi_js_preparer.js +0 -243
  193. package/src/midi/abc_midi_sequencer.js +0 -401
  194. package/src/midi/abc_midi_ui_generator.js +0 -86
  195. package/src/plugin/abc_plugin_midi.js +0 -220
  196. package/src/synth/images/loop.svg +0 -61
  197. package/src/synth/images/pause.svg +0 -6
  198. package/src/synth/images/play.svg +0 -5
  199. package/src/transform/abc2abc_write.js +0 -395
  200. package/static-wrappers/basic.js +0 -2
  201. package/static-wrappers/midi.js +0 -2
  202. package/static-wrappers/plugin-midi.js +0 -6
  203. package/static-wrappers/plugin.js +0 -6
  204. package/webpack.config.js +0 -29
@@ -0,0 +1,648 @@
1
+ // abc_midi_sequencer.js: Turn parsed abc into a linear series of events.
2
+
3
+ var sequence;
4
+ var parseCommon = require("../parse/abc_common");
5
+
6
+ (function() {
7
+ "use strict";
8
+
9
+ var measureLength;
10
+ // The abc is provided to us line by line. It might have repeats in it. We want to re arrange the elements to
11
+ // be an array of voices with all the repeats embedded, and no lines. Then it is trivial to go through the events
12
+ // one at a time and turn it into midi.
13
+
14
+ var PERCUSSION_PROGRAM = 128;
15
+
16
+ sequence = function(abctune, options) {
17
+ // Global options
18
+ options = options || {};
19
+ var qpm;
20
+ var program = options.program || 0; // The program if there isn't a program specified.
21
+ var transpose = options.midiTranspose || 0;
22
+ // If the tune has a visual transpose then that needs to be subtracted out because we are getting the visual object.
23
+ if (abctune.visualTranspose)
24
+ transpose -= abctune.visualTranspose;
25
+ var channel = options.channel || 0;
26
+ var channelExplicitlySet = false;
27
+ var drumPattern = options.drum || "";
28
+ var drumBars = options.drumBars || 1;
29
+ var drumIntro = options.drumIntro || 0;
30
+ var drumOn = drumPattern !== "";
31
+ var style = []; // The note head style for each voice.
32
+ var rhythmHeadThisBar = false; // Rhythm notation was detected.
33
+ var crescendoSize = 50; // how much to increase or decrease volume when crescendo/diminuendo is encountered.
34
+
35
+ // All of the above overrides need to be integers
36
+ program = parseInt(program, 10);
37
+ transpose = parseInt(transpose, 10);
38
+ channel = parseInt(channel, 10);
39
+ if (channel === 10)
40
+ program = PERCUSSION_PROGRAM;
41
+ drumPattern = drumPattern.split(" ");
42
+ drumBars = parseInt(drumBars, 10);
43
+ drumIntro = parseInt(drumIntro, 10);
44
+
45
+ var bagpipes = abctune.formatting.bagpipes; // If it is bagpipes, then the gracenotes are played on top of the main note.
46
+ if (bagpipes)
47
+ program = 71;
48
+
49
+ // %%MIDI fermatafixed
50
+ // %%MIDI fermataproportional
51
+ // %%MIDI deltaloudness n
52
+ // %%MIDI gracedivider b
53
+ // %%MIDI ratio n m
54
+ // %%MIDI beat a b c n
55
+ // %%MIDI grace a/b
56
+ // %%MIDI trim x/y
57
+
58
+ // %MIDI gchordon
59
+ // %MIDI gchordoff
60
+ // %%MIDI bassprog 45
61
+ // %%MIDI chordprog 24
62
+ // %%MIDI chordname name n1 n2 n3 n4 n5 n6
63
+
64
+ //%%MIDI beat ⟨int1⟩ ⟨int2⟩ ⟨int3⟩ ⟨int4⟩: controls the volumes of the notes in a measure. The first note in a bar has volume ⟨int1⟩; other ‘strong’ notes have volume ⟨int2⟩ and all the rest have volume ⟨int3⟩. These values must be in the range 0–127. The parameter ⟨int4⟩ determines which notes are ‘strong’. If the time signature is x/y, then each note is given a position number k = 0, 1, 2. . . x-1 within each bar. If k is a multiple of ⟨int4⟩, then the note is ‘strong’.
65
+
66
+ var startingMidi = [];
67
+ if (abctune.formatting.midi) {
68
+ //console.log("MIDI Formatting:", abctune.formatting.midi);
69
+ var globals = abctune.formatting.midi;
70
+ if (globals.program && globals.program.length > 0) {
71
+ program = globals.program[0];
72
+ if (globals.program.length > 1) {
73
+ program = globals.program[1];
74
+ channel = globals.program[0];
75
+ }
76
+ channelExplicitlySet = true;
77
+ }
78
+ if (globals.transpose)
79
+ transpose = globals.transpose[0];
80
+ if (globals.channel) {
81
+ channel = globals.channel[0];
82
+ channelExplicitlySet = true;
83
+ }
84
+ if (globals.drum)
85
+ drumPattern = globals.drum;
86
+ if (globals.drumbars)
87
+ drumBars = globals.drumbars[0];
88
+ if (globals.drumon)
89
+ drumOn = true;
90
+ if (channel === 10)
91
+ program = PERCUSSION_PROGRAM;
92
+ if (globals.beat)
93
+ startingMidi.push({ el_type: 'beat', beats: globals.beat })
94
+ if (globals.nobeataccents)
95
+ startingMidi.push({ el_type: 'beataccents', value: false });
96
+
97
+ }
98
+
99
+ // Specified options in abc string.
100
+
101
+ // If the tempo was passed in, use that.
102
+ // If the tempo is specified, use that.
103
+ // If there is a default, use that.
104
+ // Otherwise, use the default.
105
+ if (options.qpm)
106
+ qpm = parseInt(options.qpm, 10);
107
+ else if (abctune.metaText.tempo)
108
+ qpm = interpretTempo(abctune.metaText.tempo, abctune.getBeatLength());
109
+ else if (options.defaultQpm)
110
+ qpm = options.defaultQpm;
111
+ else
112
+ qpm = 180; // The tempo if there isn't a tempo specified.
113
+
114
+ var startVoice = [];
115
+ if (bagpipes)
116
+ startVoice.push({ el_type: 'bagpipes' });
117
+ startVoice.push({ el_type: 'instrument', program: program });
118
+ if (channel)
119
+ startVoice.push({ el_type: 'channel', channel: channel });
120
+ if (transpose)
121
+ startVoice.push({ el_type: 'transpose', transpose: transpose });
122
+ startVoice.push({ el_type: 'tempo', qpm: qpm });
123
+ for (var ss = 0; ss < startingMidi.length;ss++)
124
+ startVoice.push(startingMidi[ss]);
125
+
126
+ // the relevant part of the input structure is:
127
+ // abctune
128
+ // array lines
129
+ // array staff
130
+ // object key
131
+ // object meter
132
+ // array voices
133
+ // array abcelem
134
+
135
+ // visit each voice completely in turn
136
+ var voices = [];
137
+ var inCrescendo = [];
138
+ var inDiminuendo = [];
139
+ var durationCounter = [0];
140
+ var tempoChanges = {};
141
+ tempoChanges["0"] = { el_type: 'tempo', qpm: qpm, timing: 0 };
142
+ var currentVolume;
143
+ var startRepeatPlaceholder = []; // There is a place holder for each voice.
144
+ var skipEndingPlaceholder = []; // This is the place where the first ending starts.
145
+ var startingDrumSet = false;
146
+ var lines = abctune.lines; //abctune.deline(); TODO-PER: can switch to this, then simplify the loops below.
147
+ for (var i = 0; i < lines.length; i++) {
148
+ // For each group of staff lines in the tune.
149
+ var line = lines[i];
150
+ if (line.staff) {
151
+ var staves = line.staff;
152
+ var voiceNumber = 0;
153
+ for (var j = 0; j < staves.length; j++) {
154
+ var staff = staves[j];
155
+ if (staff.clef && staff.clef.type === "TAB")
156
+ continue;
157
+
158
+ // For each staff line
159
+ for (var k = 0; k < staff.voices.length; k++) {
160
+ // For each voice in a staff line
161
+ var voice = staff.voices[k];
162
+ if (!voices[voiceNumber]) {
163
+ voices[voiceNumber] = [].concat(JSON.parse(JSON.stringify(startVoice)));
164
+ var voiceName = getTrackTitle(line.staff, voiceNumber);
165
+ if (voiceName)
166
+ voices[voiceNumber].unshift({el_type: "name", trackName: voiceName});
167
+ }
168
+ // Negate any transposition for the percussion staff.
169
+ if (transpose && staff.clef.type === "perc")
170
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: 0 });
171
+
172
+ if (staff.clef && staff.clef.type === 'perc' && !channelExplicitlySet) {
173
+ for (var cl = 0; cl < voices[voiceNumber].length; cl++) {
174
+ if (voices[voiceNumber][cl].el_type === 'instrument')
175
+ voices[voiceNumber][cl].program = PERCUSSION_PROGRAM;
176
+ }
177
+ } else if (staff.key) {
178
+ addKey(voices[voiceNumber], staff.key);
179
+ }
180
+ if (staff.meter) {
181
+ addMeter(voices[voiceNumber], staff.meter);
182
+ }
183
+ if (!startingDrumSet && drumOn) { // drum information is only needed once, so use the first line and track 0.
184
+ voices[voiceNumber].push({el_type: 'drum', params: {pattern: drumPattern, bars: drumBars, on: drumOn, intro: drumIntro}});
185
+ startingDrumSet = true;
186
+ }
187
+ if (staff.clef && staff.clef.type !== "perc" && staff.clef.transpose) {
188
+ staff.clef.el_type = 'clef';
189
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: staff.clef.transpose });
190
+ }
191
+ if (staff.clef && staff.clef.type) {
192
+ if (staff.clef.type.indexOf("-8") >= 0)
193
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: -12 });
194
+ else if (staff.clef.type.indexOf("+8") >= 0)
195
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: 12 });
196
+ }
197
+
198
+ if (abctune.formatting.midi && abctune.formatting.midi.drumoff) {
199
+ // If there is a drum off command right at the beginning it is put in the metaText instead of the stream,
200
+ // so we will just insert it here.
201
+ voices[voiceNumber].push({ el_type: 'bar' });
202
+ voices[voiceNumber].push({el_type: 'drum', params: {pattern: "", on: false }});
203
+ }
204
+ var noteEventsInBar = 0;
205
+ var tripletMultiplier = 0;
206
+ var tripletDurationTotal = 0; // try to mitigate the js rounding problems.
207
+ var tripletDurationCount = 0;
208
+ currentVolume = [105, 95, 85, 1];
209
+
210
+ for (var v = 0; v < voice.length; v++) {
211
+ // For each element in a voice
212
+ var elem = voice[v];
213
+ switch (elem.el_type) {
214
+ case "note":
215
+ if (inCrescendo[k]) {
216
+ currentVolume[0] += inCrescendo[k];
217
+ currentVolume[1] += inCrescendo[k];
218
+ currentVolume[2] += inCrescendo[k];
219
+ voices[voiceNumber].push({ el_type: 'beat', beats: currentVolume.slice(0) });
220
+ }
221
+
222
+ if (inDiminuendo[k]) {
223
+ currentVolume[0] += inDiminuendo[k];
224
+ currentVolume[1] += inDiminuendo[k];
225
+ currentVolume[2] += inDiminuendo[k];
226
+ voices[voiceNumber].push({ el_type: 'beat', beats: currentVolume.slice(0) });
227
+ }
228
+ setDynamics(elem);
229
+
230
+ // regular items are just pushed.
231
+ if (!elem.rest || elem.rest.type !== 'spacer') {
232
+ var noteElem = { elem: elem, el_type: "note", timing: durationCounter[voiceNumber] }; // Make a copy so that modifications aren't kept except for adding the midiPitches
233
+ if (elem.style)
234
+ noteElem.style = elem.style;
235
+ else if (style[voiceNumber])
236
+ noteElem.style = style[voiceNumber];
237
+ noteElem.duration = (elem.duration === 0) ? 0.25 : elem.duration;
238
+ if (elem.startTriplet) {
239
+ tripletMultiplier = elem.tripletMultiplier;
240
+ tripletDurationTotal = elem.startTriplet * tripletMultiplier * elem.duration;
241
+ if (elem.startTriplet !== elem.tripletR) { // most commonly (3:2:2
242
+ if (v + elem.tripletR <= voice.length) {
243
+ var durationTotal = 0;
244
+ for (var w = v; w < v + elem.tripletR; w++) {
245
+ durationTotal += voice[w].duration;
246
+ }
247
+ tripletDurationTotal = tripletMultiplier * durationTotal;
248
+ }
249
+ }
250
+ noteElem.duration = noteElem.duration * tripletMultiplier;
251
+ noteElem.duration = Math.round(noteElem.duration*1000000)/1000000;
252
+ tripletDurationCount = noteElem.duration;
253
+ } else if (tripletMultiplier) {
254
+ if (elem.endTriplet) {
255
+ tripletMultiplier = 0;
256
+ noteElem.duration = Math.round((tripletDurationTotal - tripletDurationCount)*1000000)/1000000;
257
+ } else {
258
+ noteElem.duration = noteElem.duration * tripletMultiplier;
259
+ noteElem.duration = Math.round(noteElem.duration*1000000)/1000000;
260
+ tripletDurationCount += noteElem.duration;
261
+ }
262
+ }
263
+ if (elem.rest) noteElem.rest = elem.rest;
264
+ if (elem.decoration) noteElem.decoration = elem.decoration.slice(0);
265
+ if (elem.pitches) noteElem.pitches = parseCommon.cloneArray(elem.pitches);
266
+ if (elem.gracenotes) noteElem.gracenotes = parseCommon.cloneArray(elem.gracenotes);
267
+ if (elem.chord) noteElem.chord = parseCommon.cloneArray(elem.chord);
268
+
269
+ voices[voiceNumber].push(noteElem);
270
+ if (elem.style === "rhythm") {
271
+ rhythmHeadThisBar = true;
272
+ chordVoiceOffThisBar(voices)
273
+ }
274
+ noteEventsInBar++;
275
+ durationCounter[voiceNumber] += noteElem.duration;
276
+ }
277
+ break;
278
+ case "key":
279
+ case "keySignature":
280
+ addKey(voices[voiceNumber], elem);
281
+ break;
282
+ case "meter":
283
+ addMeter(voices[voiceNumber], elem);
284
+ break;
285
+ case "clef": // need to keep this to catch the "transpose" element.
286
+ if (elem.transpose)
287
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: elem.transpose });
288
+ if (elem.type) {
289
+ if (elem.type.indexOf("-8") >= 0)
290
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: -12 });
291
+ else if (elem.type.indexOf("+8") >= 0)
292
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: 12 });
293
+ }
294
+ break;
295
+ case "tempo":
296
+ qpm = interpretTempo(elem, abctune.getBeatLength());
297
+ voices[voiceNumber].push({ el_type: 'tempo', qpm: qpm, timing: durationCounter[voiceNumber] });
298
+ tempoChanges[''+durationCounter[voiceNumber]] = { el_type: 'tempo', qpm: qpm, timing: durationCounter[voiceNumber] };
299
+ break;
300
+ case "bar":
301
+ if (noteEventsInBar > 0) // don't add two bars in a row.
302
+ voices[voiceNumber].push({ el_type: 'bar' }); // We need the bar marking to reset the accidentals.
303
+ setDynamics(elem);
304
+ noteEventsInBar = 0;
305
+ // figure out repeats and endings --
306
+ // The important part is where there is a start repeat, and end repeat, or a first ending.
307
+ var endRepeat = (elem.type === "bar_right_repeat" || elem.type === "bar_dbl_repeat");
308
+ var startEnding = (elem.startEnding === '1');
309
+ var startRepeat = (elem.type === "bar_left_repeat" || elem.type === "bar_dbl_repeat" || elem.type === "bar_right_repeat");
310
+ if (endRepeat) {
311
+ var s = startRepeatPlaceholder[voiceNumber];
312
+ if (!s) s = 0; // If there wasn't a left repeat, then we repeat from the beginning.
313
+ var e = skipEndingPlaceholder[voiceNumber];
314
+ if (!e) e = voices[voiceNumber].length; // If there wasn't a first ending marker, then we copy everything.
315
+ // duplicate each of the elements - this has to be a deep copy.
316
+ for (var z = s; z < e; z++) {
317
+ var item = parseCommon.clone(voices[voiceNumber][z]);
318
+ if (item.pitches)
319
+ item.pitches = parseCommon.cloneArray(item.pitches);
320
+ voices[voiceNumber].push(item);
321
+ }
322
+ // reset these in case there is a second repeat later on.
323
+ skipEndingPlaceholder[voiceNumber] = undefined;
324
+ startRepeatPlaceholder[voiceNumber] = undefined;
325
+ }
326
+ if (startEnding)
327
+ skipEndingPlaceholder[voiceNumber] = voices[voiceNumber].length;
328
+ if (startRepeat)
329
+ startRepeatPlaceholder[voiceNumber] = voices[voiceNumber].length;
330
+ rhythmHeadThisBar = false;
331
+ break;
332
+ case 'style':
333
+ style[voiceNumber] = elem.head;
334
+ break;
335
+ case 'timeSignature':
336
+ voices[voiceNumber].push(interpretMeter(elem));
337
+ break;
338
+ case 'part':
339
+ // TODO-PER: If there is a part section in the header, then this should probably affect the repeats.
340
+ break;
341
+ case 'stem':
342
+ case 'scale':
343
+ case 'break':
344
+ case 'font':
345
+ // These elements don't affect sound
346
+ break;
347
+ case 'midi':
348
+ //console.log("MIDI inline", elem); // TODO-PER: for debugging. Remove this.
349
+ var drumChange = false;
350
+ switch (elem.cmd) {
351
+ case "drumon": drumOn = true; drumChange = true; break;
352
+ case "drumoff": drumOn = false; drumChange = true; break;
353
+ case "drum": drumPattern = elem.params; drumChange = true; break;
354
+ case "drumbars": drumBars = elem.params[0]; drumChange = true; break;
355
+ case "drummap":
356
+ // This is handled before getting here so it can be ignored.
357
+ break;
358
+ case "channel":
359
+ // There's not much needed for the channel except to look out for the percussion channel
360
+ if (elem.params[0] === 10)
361
+ voices[voiceNumber].push({ el_type: 'instrument', program: PERCUSSION_PROGRAM });
362
+ break;
363
+ case "program":
364
+ addIfDifferent(voices[voiceNumber], { el_type: 'instrument', program: elem.params[0] });
365
+ channelExplicitlySet = true;
366
+ break;
367
+ case "transpose":
368
+ voices[voiceNumber].push({ el_type: 'transpose', transpose: elem.params[0] });
369
+ break;
370
+ case "gchordoff":
371
+ voices[voiceNumber].push({ el_type: 'gchord', tacet: true });
372
+ break;
373
+ case "gchordon":
374
+ voices[voiceNumber].push({ el_type: 'gchord', tacet: false });
375
+ break;
376
+ case "beat":
377
+ voices[voiceNumber].push({ el_type: 'beat', beats: elem.params });
378
+ break;
379
+ case "nobeataccents":
380
+ voices[voiceNumber].push({ el_type: 'beataccents', value: false });
381
+ break;
382
+ case "beataccents":
383
+ voices[voiceNumber].push({ el_type: 'beataccents', value: true });
384
+ break;
385
+ case "vol":
386
+ voices[voiceNumber].push({ el_type: 'vol', volume: elem.params[0] });
387
+ break;
388
+ case "volinc":
389
+ voices[voiceNumber].push({ el_type: 'volinc', volume: elem.params[0] });
390
+ break;
391
+ default:
392
+ console.log("MIDI seq: midi cmd not handled: ", elem.cmd, elem);
393
+ }
394
+ if (drumChange) {
395
+ voices[0].push({el_type: 'drum', params: { pattern: drumPattern, bars: drumBars, intro: drumIntro, on: drumOn}});
396
+ startingDrumSet = true;
397
+ }
398
+ break;
399
+ default:
400
+ console.log("MIDI: element type " + elem.el_type + " not handled.");
401
+ }
402
+ }
403
+ voiceNumber++;
404
+ if (!durationCounter[voiceNumber])
405
+ durationCounter[voiceNumber] = 0;
406
+ }
407
+ }
408
+
409
+ function setDynamics(elem) {
410
+ var volumes = {
411
+ 'pppp': [15, 10, 5, 1],
412
+ 'ppp': [30, 20, 10, 1],
413
+ 'pp': [45, 35, 20, 1],
414
+ 'p': [60, 50, 35, 1],
415
+ 'mp': [75, 65, 50, 1],
416
+ 'mf': [90, 80, 65, 1],
417
+ 'f': [105, 95, 80, 1],
418
+ 'ff': [120, 110, 95, 1],
419
+ 'fff': [127, 125, 110, 1],
420
+ 'ffff': [127, 125, 110, 1]
421
+ };
422
+
423
+ var dynamicType;
424
+ if (elem.decoration) {
425
+ if (elem.decoration.indexOf('pppp') >= 0)
426
+ dynamicType = 'pppp';
427
+ else if (elem.decoration.indexOf('ppp') >= 0)
428
+ dynamicType = 'ppp';
429
+ else if (elem.decoration.indexOf('pp') >= 0)
430
+ dynamicType = 'pp';
431
+ else if (elem.decoration.indexOf('p') >= 0)
432
+ dynamicType = 'p';
433
+ else if (elem.decoration.indexOf('mp') >= 0)
434
+ dynamicType = 'mp';
435
+ else if (elem.decoration.indexOf('mf') >= 0)
436
+ dynamicType = 'mf';
437
+ else if (elem.decoration.indexOf('f') >= 0)
438
+ dynamicType = 'f';
439
+ else if (elem.decoration.indexOf('ff') >= 0)
440
+ dynamicType = 'ff';
441
+ else if (elem.decoration.indexOf('fff') >= 0)
442
+ dynamicType = 'fff';
443
+ else if (elem.decoration.indexOf('ffff') >= 0)
444
+ dynamicType = 'ffff';
445
+
446
+ if (dynamicType) {
447
+ currentVolume = volumes[dynamicType].slice(0);
448
+ voices[voiceNumber].push({ el_type: 'beat', beats: currentVolume.slice(0) });
449
+ inCrescendo[k] = false;
450
+ inDiminuendo[k] = false;
451
+ }
452
+
453
+ if (elem.decoration.indexOf("crescendo(") >= 0) {
454
+ var n = numNotesToDecoration(voice, v, "crescendo)");
455
+ var top = Math.min(127, currentVolume[0] + crescendoSize);
456
+ var endDec = endingVolume(voice, v+n+1, Object.keys(volumes));
457
+ if (endDec)
458
+ top = volumes[endDec][0];
459
+ if (n > 0)
460
+ inCrescendo[k] = Math.floor((top - currentVolume[0]) / n);
461
+ else
462
+ inCrescendo[k] = false;
463
+ inDiminuendo[k] = false;
464
+ } else if (elem.decoration.indexOf("crescendo)") >= 0) {
465
+ inCrescendo[k] = false;
466
+ } else if (elem.decoration.indexOf("diminuendo(") >= 0) {
467
+ var n2 = numNotesToDecoration(voice, v, "diminuendo)");
468
+ var bottom = Math.max(15, currentVolume[0] - crescendoSize);
469
+ var endDec2 = endingVolume(voice, v+n2+1, Object.keys(volumes));
470
+ if (endDec2)
471
+ bottom = volumes[endDec2][0];
472
+ inCrescendo[k] = false;
473
+ if (n2 > 0)
474
+ inDiminuendo[k] = Math.floor((bottom - currentVolume[0]) / n2);
475
+ else
476
+ inDiminuendo[k] = false;
477
+ } else if (elem.decoration.indexOf("diminuendo)") >= 0) {
478
+ inDiminuendo[k] = false;
479
+ }
480
+ }
481
+ }
482
+ }
483
+ }
484
+ // If there are tempo changes, make sure they are in all the voices. This must be done post process because all the elements in all the voices need to be created first.
485
+ insertTempoChanges(voices, tempoChanges);
486
+
487
+ if (drumIntro) {
488
+ var pickups = abctune.getPickupLength();
489
+ // add some measures of rests to the start of each track.
490
+ for (var vv = 0; vv < voices.length; vv++) {
491
+ var insertPoint = 0;
492
+ while (voices[vv][insertPoint].el_type !== "note" && voices[vv].length > insertPoint)
493
+ insertPoint++;
494
+ if (voices[vv].length > insertPoint) {
495
+ for (var w = 0; w < drumIntro; w++) {
496
+ // If it is the last measure of intro, subtract the pickups.
497
+ if (pickups === 0 || w < drumIntro-1)
498
+ voices[vv].splice(insertPoint, 0, {el_type: "note", rest: {type: "rest"}, duration: measureLength},
499
+ { el_type: "bar" });
500
+ else {
501
+ voices[vv].splice(insertPoint, 0, {el_type: "note", rest: {type: "rest"}, duration: measureLength-pickups});
502
+ }
503
+ }
504
+ }
505
+ }
506
+ }
507
+ if (voices.length > 0 && voices[0].length > 0) {
508
+ voices[0][0].pickupLength = abctune.getPickupLength();
509
+ }
510
+ return voices;
511
+ };
512
+
513
+ function numNotesToDecoration(voice, start, decoration) {
514
+ var counter = 0;
515
+ for (var i = start+1; i < voice.length; i++) {
516
+ if (voice[i].el_type === "note")
517
+ counter++;
518
+ if (voice[i].decoration && voice[i].decoration.indexOf(decoration) >= 0)
519
+ return counter;
520
+ }
521
+ return counter;
522
+ }
523
+ function endingVolume(voice, start, volumeDecorations) {
524
+ var end = Math.min(voice.length, start + 3); // If we have a volume within a couple notes of the end then assume that is the destination.
525
+ for (var i = start; i < end; i++) {
526
+ if (voice[i].el_type === "note") {
527
+ if (voice[i].decoration) {
528
+ for (var j = 0; j < voice[i].decoration.length; j++) {
529
+ if (volumeDecorations.indexOf(voice[i].decoration[j]) >= 0)
530
+ return voice[i].decoration[j];
531
+ }
532
+ }
533
+ }
534
+ }
535
+ return null;
536
+ }
537
+
538
+ function insertTempoChanges(voices, tempoChanges) {
539
+ if (!tempoChanges || tempoChanges.length === 0)
540
+ return;
541
+ var changePositions = Object.keys(tempoChanges);
542
+ for (var i = 0; i < voices.length; i++) {
543
+ var voice = voices[i];
544
+ var lastTempo = tempoChanges['0'] ? tempoChanges['0'].qpm : 0; // Don't insert redundant changes. This happens normally when repeating from the beginning, but could happen anywhere that there is a tempo marking that is the same as the last one.
545
+ for (var j = 0; j < voice.length; j++) {
546
+ var el = voice[j];
547
+ if (el.el_type === "tempo")
548
+ lastTempo = el.qpm;
549
+ if (changePositions.indexOf(''+el.timing) >= 0 && lastTempo !== tempoChanges[''+el.timing].qpm) {
550
+ lastTempo = tempoChanges[''+el.timing].qpm;
551
+ if (el.el_type === "tempo") {
552
+ el.qpm = tempoChanges[''+el.timing].qpm;
553
+ j++; // when there is a tempo element the next element has the same timing and we don't want it to match the second time.
554
+ } else {
555
+ //console.log("tempo position", i, j, el);
556
+ voices[i].splice(j, 0, {el_type: "tempo", qpm: tempoChanges[''+el.timing].qpm, timing: el.timing});
557
+ j +=2; // skip the element we just inserted.
558
+ }
559
+ }
560
+ }
561
+ }
562
+ }
563
+
564
+ function chordVoiceOffThisBar(voices) {
565
+ for (var i = 0; i < voices.length; i++) {
566
+ var voice = voices[i];
567
+ var j = voice.length-1;
568
+ while (j >= 0 && voice[j].el_type !== 'bar') {
569
+ voice[j].noChordVoice = true;
570
+ j--;
571
+ }
572
+ }
573
+ }
574
+
575
+ function getTrackTitle(staff, voiceNumber) {
576
+ if (!staff || staff.length <= voiceNumber || !staff[voiceNumber].title)
577
+ return undefined;
578
+ return staff[voiceNumber].title.join(" ");
579
+ }
580
+
581
+ function interpretTempo(element, beatLength) {
582
+ var duration = 1/4;
583
+ if (element.duration) {
584
+ duration = element.duration[0];
585
+ }
586
+ var bpm = 60;
587
+ if (element.bpm) {
588
+ bpm = element.bpm;
589
+ }
590
+ // The tempo is defined with a beat length of "duration". If that isn't the natural beat length then there is a translation.
591
+ return duration * bpm / beatLength;
592
+ }
593
+
594
+ function interpretMeter(element) {
595
+ var meter;
596
+ switch (element.type) {
597
+ case "common_time":
598
+ meter = { el_type: 'meter', num: 4, den: 4 };
599
+ break;
600
+ case "cut_time":
601
+ meter = { el_type: 'meter', num: 2, den: 2 };
602
+ break;
603
+ case "specified":
604
+ // TODO-PER: only taking the first meter, so the complex meters are not handled.
605
+ meter = { el_type: 'meter', num: element.value[0].num, den: element.value[0].den };
606
+ break;
607
+ default:
608
+ // This should never happen.
609
+ meter = { el_type: 'meter' };
610
+ }
611
+ measureLength = meter.num/meter.den;
612
+ return meter;
613
+ }
614
+
615
+ function removeNaturals(accidentals) {
616
+ var acc = [];
617
+ for (var i = 0; i < accidentals.length; i++) {
618
+ if (accidentals[i].acc !== "natural")
619
+ acc.push(accidentals[i])
620
+ }
621
+ return acc;
622
+ }
623
+ function addKey(arr, key) {
624
+ var newKey;
625
+ if (key.root === 'HP')
626
+ newKey = {el_type: 'key', accidentals: [{acc: 'natural', note: 'g'}, {acc: 'sharp', note: 'f'}, {acc: 'sharp', note: 'c'}]};
627
+ else
628
+ newKey = {el_type: 'key', accidentals: removeNaturals(key.accidentals) };
629
+ addIfDifferent(arr, newKey);
630
+ }
631
+ function addMeter(arr, meter) {
632
+ var newMeter = interpretMeter(meter);
633
+ addIfDifferent(arr, newMeter);
634
+ }
635
+ function addIfDifferent(arr, item) {
636
+ for (var i = arr.length-1; i >= 0; i--) {
637
+ if (arr[i].el_type === item.el_type) {
638
+ if (JSON.stringify(arr[i]) !== JSON.stringify(item))
639
+ arr.push(item);
640
+ return;
641
+ }
642
+ }
643
+ arr.push(item);
644
+ }
645
+
646
+ })();
647
+
648
+ module.exports = sequence;
@@ -1,19 +1,8 @@
1
- // Copyright (C) 2019-2020 Paul Rosen (paul at paulrosen dot net)
2
- //
3
- // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
- // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
5
- // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
6
- // to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
- //
8
- // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
- //
10
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
11
- // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
13
- // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ var registerAudioContext = require('./register-audio-context.js');
15
2
 
16
3
  function activeAudioContext() {
4
+ if (!window.abcjsAudioContext)
5
+ registerAudioContext();
17
6
  return window.abcjsAudioContext;
18
7
  }
19
8
 
@@ -0,0 +1,10 @@
1
+ // This turns the number of cents to detune into a value that is convenient to use in pitch calculations
2
+ // A cent is 1/100 of a musical half step and is calculated exponentially over the course of an octave.
3
+ // The equation is:
4
+ // Two to the power of cents divided by 1200
5
+
6
+ function centsToFactor(cents) {
7
+ return Math.pow(2, cents/1200);
8
+ }
9
+
10
+ module.exports = centsToFactor;