Debian Codegraph @

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

raw symbols SCIP
1 // perc.js - module to handle %%percmap
2 //
3 // Copyright (C) 2018-2023 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 "%%percmap" appears in a ABC source.
21 //
22 // Parameters (from W. Vree)
23 // %%percmap ABC_note percussion [note_head]
24 // The percussion may be a number (MIDI percussion number range 35..81),
25 // a ABC note or a possibly abbreviated percussion name.
26 // See https://wim.vree.org/js2/tabDrumDoc.html for more information.
27
28 // Using this command creates a voicemap named "MIDIdrum".
29
30 if (typeof abc2svg == "undefined")
31 var abc2svg = {}
32
33 abc2svg.perc = {
34
35 // parse %%percmap
36 do_perc: function(parm) {
37 var pits = new Int8Array([0, 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6]),
38 accs = new Int8Array([3, 1, 3, -1, 3, 3, 1, 3, -1, 3, -1, 3])
39
40 // GM drum
41 // 35 B,,, Acoustic Bass Drum a-b-d
42 // 36 C,, Bass Drum 1 b-d-1
43 // 37 ^C,, Side Stick s-s
44 // 38 D,, Acoustic Snare a-s
45 // 39 ^D,, Hand Clap h-c
46 // 40 E,, Electric Snare e-s
47 // 41 F,, Low Floor Tom l-f-t
48 // 42 ^F,, Closed Hi Hat c-h-h
49 // 43 G,, High Floor Tom h-f-t
50 // 44 ^G,, Pedal Hi-Hat p-h-h
51 // 45 A,, Low Tom l-to
52 // 46 ^A,, Open Hi-Hat o-h-h
53 // 47 B,, Low-Mid Tom l-m-t
54 // 48 C, Hi Mid Tom h-m-t
55 // 49 ^C, Crash Cymbal 1 c-c-1
56 // 50 D, High Tom h-to
57 // 51 ^D, Ride Cymbal 1 r-c-1
58 // 52 E, Chinese Cymbal c-c
59 // 53 F, Ride Bell r-b
60 // 54 ^F, Tambourine t
61 // 55 G, Splash Cymbal s-c
62 // 56 ^G, Cowbell co
63 // 57 A, Crash Cymbal 2 c-c-2
64 // 58 ^A, Vibraslap v
65 // 59 B, Ride Cymbal 2 r-c-2
66 // 60 C Hi Bongo h-b
67 // 61 ^C Low Bongo l-b
68 // 62 D Mute Hi Conga m-h-c
69 // 63 ^D Open Hi Conga o-h-c
70 // 64 E Low Conga l-c
71 // 65 F High Timbale h-ti
72 // 66 ^F Low Timbale l-ti
73 // 67 G High Agogo h-a
74 // 68 ^G Low Agogo l-a
75 // 69 A Cabasa ca
76 // 70 ^A Maracas m
77 // 71 B Short Whistle s-w
78 // 72 c Long Whistle l-w
79 // 73 ^c Short Guiro s-g
80 // 74 d Long Guiro l-g
81 // 75 ^d Claves cl
82 // 76 e Hi Wood Block h-w-b
83 // 77 f Low Wood Block l-w-b
84 // 78 ^f Mute Cuica m-c
85 // 79 g Open Cuica o-c
86 // 80 ^g Mute Triangle m-t
87 // 81 a Open Triangle o-t
88
89 // percussion reduced names (alphabetic order)
90 var prn = {
91 "a-b-d": 35,
92 "a-s": 38,
93 "b-d-1": 36,
94 "ca": 69,
95 "cl": 75,
96 "co": 56,
97 "c-c": 52,
98 "c-c-1": 49,
99 "c-c-2": 57,
100 "c-h-h": 42,
101 "e-s": 40,
102 "h-a": 67,
103 "h-b": 60,
104 "h-c": 39,
105 "h-f-t": 43,
106 "h-m-t": 48,
107 "h-ti": 65,
108 "h-to": 50,
109 "h-w-b": 76,
110 "l-a": 68,
111 "l-b": 61,
112 "l-c": 64,
113 "l-f-t": 41,
114 "l-g": 74,
115 "l-m-t": 47,
116 "l-ti": 66,
117 "l-to": 45,
118 "l-w": 72,
119 "l-w-b": 77,
120 "m": 70,
121 "m-c": 78,
122 "m-h-c": 62,
123 "m-t": 80,
124 "o-c": 79,
125 "o-h-c": 63,
126 "o-h-h": 46,
127 "o-t": 81,
128 "p-h-h": 44,
129 "r-b": 53,
130 "r-c-1": 51,
131 "r-c-2": 59,
132 "s-c": 55,
133 "s-g": 73,
134 "s-s": 37,
135 "s-w": 71,
136 "t": 54,
137 "v": 58
138 }
139
140 // convert a drum instrument to a ABC note
141 function toabc(p) {
142 var i, j, s, pit
143
144 if (/^[_^]*[A-Ga-g][,']*$/.test(p)) // '
145 return p // ABC note
146
147 // if not a MIDI pitch, try a drum instrument name
148 pit = Number(p)
149 if (isNaN(pit)) {
150 p = p.toLowerCase(p);
151 s = p[0]; // get the 1st letters after '-'
152 i = 0
153 while (1) {
154 j = p.indexOf('-', i)
155 if (j < 0)
156 break
157 i = j + 1;
158 s += '-' + p[i]
159 }
160 pit = prn[s]
161
162 // solve some specific cases
163 if (!pit) {
164 switch (p[0]) {
165 case 'c':
166 switch (p[1]) {
167 case 'a': pit = prn.ca; break
168 case 'l': pit = prn.cl; break
169 case 'o': pit = prn.co; break
170 }
171 break
172 case 'h':
173 case 'l':
174 i = p.indexOf('-')
175 if (p[i + 1] != 't')
176 break
177 switch (p[i + 2]) {
178 case 'i':
179 case 'o':
180 pit = prn[s + p[i + 2]]
181 break
182 }
183 break
184 }
185 if (!pit)
186 return
187 }
188 }
189
190 p = ["C","^C","D","_E","E","F","^F","G","^G","A","_B","B"][pit % 12]
191 while (pit < 60) {
192 p += ','
193 pit += 12
194 }
195 while (pit >= 72) {
196 p += "'"
197 pit -= 12
198 }
199 return p
200 } // toabc()
201
202 // do_perc()
203 var a = parm.split(/\s+/),
204 p = a[1].replace(/[=_^]/, '')
205
206 this.do_pscom("map MIDIdrum " + a[1] +
207 " play=" + toabc(a[2]) +
208 " print=" + p +
209 (a[3] ? (" heads=" + a[3]) : ''))
210 this.set_v_param("perc", "MIDIdrum")
211 }, // do_perc()
212
213 // set the MIDI parameters in the current voice
214 set_perc: function(a) {
215 var i, item, s,
216 curvoice = this.get_curvoice()
217
218 for (i = 0; i < a.length; i++) {
219 switch (a[i]) {
220 case "perc=": // %%percmap
221 if (!curvoice.map)
222 curvoice.map = {}
223 curvoice.map = a[i + 1];
224 s = this.new_block("midiprog")
225 s.play = s.invis = 1 //true
226 curvoice.chn = s.chn = 9 // channel 10
227 break
228 }
229 }
230 }, // set_perc()
231
232 do_pscom: function(of, text) {
233 if (text.slice(0, 8) == "percmap ")
234 abc2svg.perc.do_perc.call(this, text)
235 else
236 of(text)
237 },
238
239 set_vp: function(of, a) {
240 abc2svg.perc.set_perc.call(this, a);
241 of(a)
242 },
243
244 set_hooks: function(abc) {
245 abc.do_pscom = abc2svg.perc.do_pscom.bind(abc, abc.do_pscom);
246 abc.set_vp = abc2svg.perc.set_vp.bind(abc, abc.set_vp)
247 }
248 } // perc
249
250 if (!abc2svg.mhooks)
251 abc2svg.mhooks = {}
252 abc2svg.mhooks.perc = abc2svg.perc.set_hooks