Debian Codegraph @

lib/ChordPro/res/abc/abc2svg/MIDI-1.js

raw symbols SCIP
1 // MIDI.js - module to handle the %%MIDI parameters
2 //
3 // Copyright (C) 2019-2025 Jean-Francois Moine
4 //
5 // This file is part of abc2svg.
6 //
7 // abc2svg is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // abc2svg is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // This module is loaded when "%%MIDI" appears in a ABC source.
21 //
22 // Parameters (see abcMIDI - https://abcmidi.sourceforge.io/ - for details)
23 // %%MIDI channel n
24 // %%MIDI program [channel] n
25 // %%MIDI control k v
26 // %%MIDI drummap ABC_note MIDI_pitch
27 // %%MIDI temperamentequal nedo
28 // %%MIDI gchordbars n
29 // %%MIDI chordname <chord_type> <list of MIDI pitches>
30 // %%MIDI chordprog <#MIDI program> [octave=<n>]
31 // %%MIDI chordvol <volume>
32 // %%MIDI gchord <string>
33 // %%MIDI gchordon
34 // %%MIDI gchordoff
35
36 // Using %%MIDI drummap creates a voicemap named "MIDIdrum".
37 // This name must be used if some print map is required:
38 // %%MIDI drummap g 42
39 // %%map MIDIdrum g heads=x
40 // A same effect may be done by
41 // %%percmap g 42 x
42 // but this is not abcMIDI compatible!
43
44 if (typeof abc2svg == "undefined")
45 var abc2svg = {}
46
47 abc2svg.MIDI = {
48
49 // parse %%MIDI commands
50 do_midi: function(parm) {
51
52 // build the equal temperament as a b40 float array
53 function tb40(qs) {
54 // b40 C D E F G A B
55 // [ 2, 8, 14, 19, 25, 31, 37 ]
56 var i,
57 // C G D A E B ^F ^C ^G ^D ^A ^E ^B^^F^^C^^G^^D^^A^^E^^B
58 n1 = [2,25, 8,31,14,37,20, 3,26, 9,32,15,38,21, 4,27,10,33,16,39],
59 // C F _B _E _A _D _G _C _F__B__E__A__D__G__C__F
60 n2 = [0,19,36,13,30, 7,24, 1,18,35,12,29, 6,23, 0,17],
61 da = 21 - 3 * qs, // 21 = 12 (octave) + 9 (A)
62 b = new Float32Array(40)
63
64 for (i = 0; i < n1.length; i++)
65 b[n1[i]] = (qs * i + da) % 12
66 for (i = 1; i <= n2.length; i++)
67 b[n2[i]] = 12 - (qs * i - da) % 12
68 return b
69 } // tb40()
70
71 // do_midi()
72 var n, v, s, maps,
73 o, q, n, qs,
74 a = parm.split(/\s+/),
75 abc = this,
76 cfmt = abc.cfmt(),
77 curvoice = abc.get_curvoice(),
78 parse = abc.get_parse()
79
80 if (curvoice) {
81 if (curvoice.ignore)
82 return
83 if (curvoice.chn == undefined)
84 curvoice.chn = curvoice.v < 9 ?
85 curvoice.v :
86 curvoice.v + 1
87 }
88 switch (a[1]) {
89 // case "bassprog": // %%MIDI bassprog <#MIDI program> [octave=<n>]
90 // break
91 // case "bassvol": // %%MIDI bassvol <volume>
92 // break
93 // case "beatstring": // %%MIDI beatstring <string of fmp>
94 // break
95 case "chordname": // %%MIDI chordname <list of MIDI pitches>
96 // example: %%MIDI chordname m 0 3 7
97 if (!cfmt.chord)
98 cfmt.chord = {}
99 if (!cfmt.chord.names)
100 cfmt.chord.names = {}
101 cfmt.chord.names[a[2]] = a.slice(3)
102 break
103 case "chordprog": // %%MIDI chordprog <#MIDI program> [octave=<n>]
104 if (!cfmt.chord)
105 cfmt.chord = {}
106 cfmt.chord.prog = a[2]
107 if (a[3] && a[3].slice(0, 7) == "octave=")
108 cfmt.chord.trans = Number(a[3].slice(7))
109 break
110 case "chordvol": // %%MIDI chordvol <volume>
111 v = Number(a[2])
112 if (isNaN(v) || v < 0 || v > 127) {
113 abc.syntax(1, abc.errs.bad_val, "%%MIDI chordvol")
114 break
115 }
116 if (!cfmt.chord)
117 cfmt.chord = {}
118 cfmt.chord.vol = v
119 break
120 // case "drone": // %%MIDI drone <#prog> <pit_1> <pit_2> <vol_1> <vol_2>
121 // // default: 70 45 33 80 80
122 // break
123 // case "droneon": // %%MIDI droneon
124 // break
125 // case "droneoff": // %%MIDI droneoff
126 // break
127 case "gchord": // %%MIDI gchord <list of letters and repeat numbers>
128 // // z rest
129 // // c chord
130 // // f fundamental
131 // // b fundamental + chord
132 // // G/H/I/J/K individual notes starting
133 // // from the lowest note of the chord
134 // // g/h/i/j/k an octave above these
135 // // defaults:
136 // // M:2/4 or 4/4 fzczfzcz
137 // // M:3/4 fzczcz
138 // // M:6/8 fzcfzc
139 // // M:9/8 fzcfzcfzc
140 // fall thru
141 case "gchordbars": // %%MIDI gchordbars n
142 case "gchordon": // %%MIDI gchordon
143 case "gchordoff": // %%MIDI gchordoff
144 if (!cfmt.chord)
145 cfmt.chord = {}
146 if (parse.state >= 2
147 && curvoice) {
148 s = abc.new_block("midigch")
149 s.play = s.invis = 1 //true
150 if (a[1][6] == 'o')
151 s.on = a[1][7] == 'n'
152 else if (a[1][6] == 'b')
153 s.gchnb = +a[2]
154 else
155 s.rhy = a[2] // chord rhythm
156 } else if (a[1][6] == 'o') {
157 cfmt.chord.gchon = a[1][7] == 'n'
158 } else if (a[1][6] == 'b') {
159 cfmt.chord.gchnb = +a[2]
160 } else {
161 cfmt.chord.rhy = a[2]
162 }
163 break
164 case "channel":
165 v = parseInt(a[2])
166 if (isNaN(v) || v <= 0 || v > 16) {
167 abc.syntax(1, abc.errs.bad_val, "%%MIDI channel")
168 break
169 }
170 v-- // channel range 1..16 => 0..15
171 if (parse.state >= 2) {
172 s = abc.new_block("midiprog")
173 s.play = s.invis = 1 //true
174 s.chn = v
175 } else {
176 abc.set_v_param("channel", v)
177 }
178 break
179 case "drummap":
180 //fixme: should have a 'MIDIdrum' per voice?
181 v = Number(a[3])
182 if (isNaN(v)) {
183 abc.syntax(1, abc.errs.bad_val, "%%MIDI drummap")
184 break
185 }
186 n = ["C","^C","D","_E","E","F","^F","G","^G","A","_B","B"][v % 12]
187 while (v < 60) {
188 n += ','
189 v += 12
190 }
191 while (v > 72) {
192 n += "'"
193 v -= 12
194 }
195 this.do_pscom("map MIDIdrum " + a[2] + " play=" + n)
196 abc.set_v_param("mididrum", "MIDIdrum")
197 break
198 case "program":
199 a.shift()
200 v = []
201 if (a[2]) {
202 v[0] = +a[2] // program
203 v[1] = +a[1] // channel
204 } else {
205 v[0] = +a[1]
206 v[1] = 0
207 }
208 if (isNaN(v[0]) || v[0] < 0 || v[0] > 127
209 || (v[1]
210 && (isNaN(v[1]) || v[1] <= 0 || v[1] > 16))) {
211 abc.syntax(1, abc.errs.bad_val, "%%MIDI program")
212 break
213 }
214 if (parse.state >= 2) {
215 s = abc.new_block("midiprog");
216 s.play = s.invis = 1 //true
217 s.instr = v[0]
218 s.chn = v[1] > 0
219 ? (v[1] - 1)
220 : curvoice.v < 9 ? curvoice.v : curvoice.v + 1
221 } else {
222 abc.set_v_param("instr", a.slice(1).join(' '))
223 }
224 break
225 case "control":
226 n = parseInt(a[2])
227 if (isNaN(n) || n < 0 || n > 127) {
228 abc.syntax(1, "Bad controller number in %%MIDI")
229 break
230 }
231 v = parseInt(a[3])
232 if (isNaN(v) || v < 0 || v > 127) {
233 abc.syntax(1, "Bad controller value in %%MIDI")
234 break
235 }
236 if (parse.state >= 2) {
237 s = abc.new_block("midictl");
238 s.play = s.invis = 1 //true
239 s.ctrl = n;
240 s.val = v
241 } else {
242 abc.set_v_param("midictl", a[2] + ' ' + a[3])
243 }
244 break
245 case "temperamentequal":
246 n = parseInt(a[2])
247 if (isNaN(n) || n < 5 || n > 255) {
248 abc.syntax(1, abc.errs.bad_val, "%%MIDI " + a[1])
249 return
250 }
251
252 // define the Turkish accidentals (53-TET)
253 s = abc.get_glyphs()
254 if (n == 53
255 && !s.acc12_53) { // do not redefine the glyphs
256 // #1
257 s.acc12_53 = '<text id="acc12_53" x="-1">&#xe282;</text>'
258
259 // #2
260 s.acc24_53 = '<text id="acc24_53" x="-1">&#xe282;\
261 <tspan x="0" y="-9" style="font-size:9px">2</tspan></text>'
262
263 // #3
264 s.acc36_53 = '<text id="acc36_53" x="-1">&#xe262;\
265 <tspan x="0" y="-9" style="font-size:9px">3</tspan></text>'
266
267 // #4
268 s.acc48_53 = '<text id="acc48_53" x="-1">&#xe262;</text>'
269
270 // #5
271 s.acc60_53 = '<g id="acc60_53">\n\
272 <text style="font-size:1.1em" x="-1">&#xe282;</text>\n\
273 <path class="stroke" stroke-width="1.6" d="M-2 1l7 -2.2"/>\n\
274 </g>'
275
276 // #8
277 s.acc96_53 = '<g id="acc96_53">\n\
278 <text style="font-size:1.1em" x="-2">&#xe262;</text>\n\
279 <path class="stroke" stroke-width="1.6" d="M-2.8 1.4l8.5 -2.8"/>\n\
280 </g>'
281
282 // #9
283 s.acc108_53 = '<text id="acc108_53" x="-3">&#xe263;</text>'
284
285 // b9
286 s["acc-108_53"] = '<text id="acc-108_53" x="-3">&#xe264;</text>'
287
288 // b8
289 s["acc-96_53"] = '<g id="acc-96_53">\n\
290 <text x="-1">&#xe260;</text>\n\
291 <path class="stroke" stroke-width="1.3" d="M-3 -7l5 -2m0 3l-5 2"/>\n\
292 </g>'
293
294 // b5
295 s["acc-60_53"] = '<text id="acc-60_53" x="-1">&#xe260;</text>'
296
297 // b4
298 s["acc-48_53"] = '<g id="acc-48_53">\n\
299 <text x="-1">&#xe260;</text>\n\
300 <path class="stroke" stroke-width="1.3" d="M-3 -5.5l5 -2"/>\n\
301 </g>'
302
303 // b3
304 s["acc-36_53"] = '<g id="acc-36_53">\n\
305 <text x="-1">&#xe260;\
306 <tspan x="0" y="-12" style="font-size:9px">3</tspan></text>\n\
307 <path class="stroke" stroke-width="1.3" d="M-3 -5.5l5 -2"/>\n\
308 </g>'
309
310 // b2
311 s["acc-24_53"] = '<text id="acc-24_53" x="-2">&#xe280;\
312 <tspan x="0" y="-12" style="font-size:9px">2</tspan></text>'
313
314 // b1
315 s["acc-12_53"] = '<text id="acc-12_53" x="-2">&#xe280;</text>'
316 }
317
318 // define the detune values
319 q = 7.019550008653874, // Math.log(3/2)/Math.log(2) * 12
320 // = just intonation fifth
321 o = 12 // octave
322 cfmt.nedo = n // octave divider
323 qs = ((n * q / o + .5) | 0) * o / n // new fifth
324
325 // warn on bad fifth values
326 if (qs < 6.85 || qs > 7.2)
327 abc.syntax(0, abc.errs.bad_val, "%%MIDI " + a[1])
328
329 cfmt.temper = tb40(qs) // pitches / A in 100th of cents
330
331 break
332 }
333 }, // do_midi()
334
335 // set the MIDI parameters in the current voice
336 set_vp: function(of, a) {
337 var i, item, s,
338 abc = this,
339 curvoice = abc.get_curvoice()
340
341 // set the voice parameters before inserting any block
342 of(a.slice(0)) // (copy because the parameters are removed)
343
344 for (i = 0; i < a.length; i++) {
345 switch (a[i]) {
346 case "channel=": // %%MIDI channel
347 s = abc.new_block("midiprog")
348 s.play = s.invis = 1 //true
349 s.chn = +a[++i]
350 break
351 case "instr=": // %%MIDI program
352 s = abc.new_block("midiprog")
353 s.play = s.invis = 1 //true
354 s.instr = a[++i].split(' ')
355 if (s.instr[1])
356 s.chn = s.instr.shift() - 1
357 else
358 s.chn = curvoice.v < 9 ?
359 curvoice.v :
360 curvoice.v + 1
361 s.instr = +s.instr[0]
362 break
363 case "midictl=": // %%MIDI control
364 if (!curvoice.midictl)
365 curvoice.midictl = []
366 item = a[++i].split(' ');
367 curvoice.midictl[item[0]] = Number(item[1])
368 break
369 case "mididrum=": // %%MIDI drummap note midipitch
370 if (!curvoice.map)
371 curvoice.map = {}
372 curvoice.map = a[++i]
373 break
374 }
375 }
376 }, // set_vp()
377
378 do_pscom: function(of, text) {
379 if (text.slice(0, 5) == "MIDI ")
380 abc2svg.MIDI.do_midi.call(this, text)
381 else
382 of(text)
383 },
384
385 set_hooks: function(abc) {
386 abc.do_pscom = abc2svg.MIDI.do_pscom.bind(abc, abc.do_pscom);
387 abc.set_vp = abc2svg.MIDI.set_vp.bind(abc, abc.set_vp)
388 }
389 } // MIDI
390
391 if (!abc2svg.mhooks)
392 abc2svg.mhooks = {}
393 abc2svg.mhooks.MIDI = abc2svg.MIDI.set_hooks