Debian Codegraph @

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

raw symbols SCIP
1 // gamelan.js - module to output Gamelan (indonesian) music sheets
2 //
3 // Copyright (C) 2020-2024 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 "%%gamelan" appears in a ABC source.
21 //
22 // Parameters (none)
23 // %%gamelan 1
24 // scale:
25 // - sléndro - 5 equal tones
26 // 5-TET C D E G A
27 // detune 0 +40 +80 . +20 +60 .
28 // - pélog - 7 unequal tones
29 // D _E+ F- ^G+ A _B c+ d
30 // 1 2 3 4 5 6 7 1
31 // (first note = ding)
32 // Bali
33 // selisir ^C D E ^G A 12356
34 // tembung E F A B c 45612
35 // sunaren F G B c d (?) 56723
36 // Java
37 // bem D_E ^G A _B 12456
38 // barang _E F A _B c 23567
39 // numbers
40 // ?? E F G B c
41 // ?? C ^C _E G _A (or ^C D E ^G A)
42 // dot = continuation (not rest)
43
44 if (typeof abc2svg == "undefined")
45 var abc2svg = {}
46
47 abc2svg.gamelan = {
48
49 cde2fcg: new Int8Array([0, 2, 4, -1, 1, 3, 5]),
50 cgd2cde: new Int8Array([0, -4, -1, -5, -2, -6, -3,
51 0, -4, -1, -5, -2, -6, -3, 0]),
52 acc2: new Int8Array([-2, -1, 3, 1, 2]),
53
54 // change %%staves and %%score
55 do_pscom: function(of, p) {
56 switch (p.match(/\w+/)[0]) {
57 case 'staves':
58 case 'score':
59 p = p.replace(/\(|\)/g, '')
60 break
61 }
62 of(p)
63 },
64
65 // adjust some symbols before the generation
66 output_music: function(of) {
67 var v,
68 C = abc2svg.C,
69 abc = this,
70 cur_sy = abc.get_cur_sy(),
71 voice_tb = abc.get_voice_tb()
72
73 if (!abc.cfmt().gamelan) {
74 of()
75 return
76 }
77
78 // expand dots and long notes/rests
79 function slice(s) {
80 var m, n, s2, s3, d, d_orig
81
82 if (s.dur <= C.BLEN * 3 / 8) {
83 if ((s.dur_orig / 9 | 0) * 9 != s.dur_orig)
84 return
85 d = s.dur / 3
86 d_orig = s.dur_orig / 3
87 s.dur -= d
88 s.dur_orig -= d_orig
89 n = 1
90 } else {
91 if (s.dur >= C.BLEN)
92 n = 3
93 else if (s.dur == C.BLEN / 2)
94 n = 1
95 else
96 n = 2
97 d = d_orig = C.BLEN / 4
98 s.dur = s.dur_orig = C.BLEN / 4
99 }
100 for (m = 0; m <= s.nhd; m++)
101 s.notes[m].dur = s.dur
102 s.beam_on = true
103 while (--n >= 0) {
104 s2 = {
105 type: C.REST,
106 v: s.v,
107 p_v: s.p_v,
108 st: s.st,
109 fmt: s.fmt,
110 dur: d,
111 dur_orig: d_orig,
112 stem: 1,
113 multi: 0,
114 nhd: 0,
115 notes: [{
116 pit: s.notes[0].pit,
117 jn: 8
118 }],
119 xmx: 0,
120 beam_on: true,
121 noplay: true,
122 time: s.time + s.dur,
123 prev: s,
124 next: s.next
125 }
126 s.next = s2
127 if (s2.next)
128 s2.next.prev = s2
129
130 if (!s.ts_next) {
131 s.ts_next = s2
132 if (s.soln)
133 s.soln = false
134 s2.ts_prev = s
135 s2.seqst = true
136 } else {
137 for (s3 = s.ts_next; s3; s3 = s3.ts_next) {
138 if (s3.time < s2.time)
139 continue
140 if (s3.time > s2.time) {
141 s2.seqst = true
142 s3 = s3.ts_prev
143 }
144 s2.ts_next = s3.ts_next
145 s2.ts_prev = s3
146 if (s2.ts_next)
147 s2.ts_next.ts_prev = s2
148 s3.ts_next = s2
149 break
150 }
151 }
152 s = s2
153 }
154 } // slice()
155
156 // replace the tied notes by a '.'
157 function do_tie(s) {
158 var end_time = s.time + s.dur
159 while (1) {
160 s = s.ts_next
161 if (!s || s.time > end_time)
162 return // ?!
163 if (s.type == C.NOTE
164 && s.time == end_time)
165 break
166 }
167 s.notes[0].jn = 8
168 s.notes[0].jo = 2
169 } // do_tie()
170
171 function set_sym(p_v) {
172 var s, s2, note, pit, nn, p, a, m, i,
173 sf = p_v.key.k_sf,
174 delta = abc2svg.gamelan.cgd2cde[sf + 7] - 2
175
176 delete p_v.key.k_a_acc // no accidental
177
178 // no (visible) clef
179 p_v.clef.invis = true
180
181 // scan the voice
182 for (s = p_v.sym; s; s = s.next) {
183 s.st = p_v.st
184 switch (s.type) {
185 case C.CLEF:
186 s.invis = true
187 // continue
188 default:
189 continue
190 case C.KEY:
191 delta = abc2svg.gamelan.cgd2cde[s.k_sf + 7] - 2
192 continue
193 case C.REST:
194 if (s.notes[0].jn)
195 continue
196 s.notes[0].jn = 0
197 s.notes[0].pit = 21
198 slice(s)
199 continue
200 case C.NOTE: // change the notes
201 break
202 }
203
204 s.stem = 1
205 s.stemless = true
206
207 // set the slurs offset and direction
208 if (s.sls) {
209 for (i = 0; i < s.sls.length; i++)
210 s.sls[i].ty = C.SL_BELOW
211 }
212
213 for (m = 0; m <= s.nhd; m++) {
214 note = s.notes[m]
215
216 // note head
217 p = note.pit
218 pit = p + delta
219 if (note.jn == undefined) { // if not tied
220 note.jn = ((pit + 77) % 7) + 1 // note number
221 note.jo = (pit / 7) | 0 // octave number
222 }
223
224 // set a fixed offset to the note
225 // for the slurs and decorations
226 note.pit = 21 // "A"
227
228 // accidentals
229 a = note.acc
230 if (a) {
231 nn = abc2svg.gamelan.cde2fcg[(p + 5 + 16 * 7) % 7] - sf
232 if (a != 3)
233 nn += a * 7
234 nn = ((((nn + 1 + 21) / 7) | 0) + 2 - 3 + 32 * 5) % 5
235 note.acc = abc2svg.gamelan.acc2[nn]
236 }
237
238 if (note.tie_ty) {
239 do_tie(s)
240 delete note.tie_ty
241 }
242 }
243
244 // change the dots and the long notes
245 slice(s)
246
247 // replace the staccato dot
248 if (s.a_dd) {
249 for (i = 0; i < s.a_dd.length; i++) {
250 if (s.a_dd[i].glyph == "stc") {
251 abc.deco_put("gstc", s)
252 s.a_dd[i] = s.a_dd.pop()
253 }
254 }
255 }
256 }
257 } // set_sym()
258
259 // -- output_music --
260
261 for (v = 0; v < voice_tb.length; v++)
262 set_sym(voice_tb[v])
263
264 of()
265 }, // output_music()
266
267 draw_symbols: function(of, p_voice) {
268 var i, m, nl, note, s, s2, x, y,
269 C = abc2svg.C,
270 abc = this,
271 dot = "\ue1e7",
272 staff_tb = abc.get_staff_tb(),
273 out_svg = abc.out_svg,
274 out_sxsy = abc.out_sxsy,
275 xypath = abc.xypath
276
277 if (!abc.cfmt().gamelan) {
278 of(p_voice)
279 return
280 }
281
282 // draw the duration lines above the notes
283 function draw_dur(s1, y, s2, n, nl) {
284 var s, s3
285
286 xypath(s1.x - 3, y + 24)
287 out_svg('h' + (s2.x - s1.x + 8).toFixed(1) + '"/>\n') // "
288 y -= 2.5
289 while (++n <= nl) {
290 s = s1
291 while (1) {
292 if (s.nflags && s.nflags >= n) {
293 s3 = s
294 while (s != s2) {
295 if (s.next.beam_br1
296 || (s.next.beam_br2 && n > 2)
297 || (s.next.nflags
298 && s.next.nflags < n))
299 break
300 s = s.next
301 }
302 draw_dur(s3, y, s, n, nl)
303 }
304 if (s == s2)
305 break
306 s = s.next
307 }
308 }
309 } // draw_dur()
310
311 function out_mus(x, y, p) {
312 out_svg('<text x="')
313 out_sxsy(x, '" y="', y)
314 out_svg('">' + p + '</text>\n')
315 } // out_txt()
316
317 function out_txt(x, y, p) {
318 out_svg('<text class="bn" x="')
319 out_sxsy(x, '" y="', y)
320 out_svg('">' + p + '</text>\n')
321 } // out_txt()
322
323 function draw_hd(s, x, y) {
324 var m, note, ym
325
326 for (m = 0; m <= s.nhd; m++) {
327 note = s.notes[m]
328 out_txt(x - 3.5, y + 8, "01234567."[note.jn])
329 if (note.acc) {
330 out_svg('<path class="stroke" stroke-width="1.1" d="M')
331 if (note.acc > 0) {
332 out_sxsy(x - 6, ' ', y + 10)
333 out_svg("l12 -6")
334 } else {
335 out_sxsy(x - 6, ' ', y + 16)
336 out_svg("l12 6")
337 }
338 out_svg('"/>\n')
339 }
340 if (note.jo > 2) {
341 out_mus(x - 1, y + 23, dot)
342 if (note.jo > 3) {
343 y += 3
344 out_mus(x - 1, y + 23.4, dot)
345 }
346 } else if (note.jo < 2) {
347 ym = y + 4
348 out_mus(x - 1, ym, dot)
349 }
350 y += 20
351 }
352 } // draw_hd()
353
354 // -- draw_symbols --
355
356 for (s = p_voice.sym; s; s = s.next) {
357 if (s.invis)
358 continue
359 switch (s.type) {
360 case C.NOTE:
361 case C.REST:
362 x = s.x
363 y = staff_tb[s.st].y
364 draw_hd(s, x, y)
365 break
366 }
367 }
368
369 // draw the (pseudo) beams
370 for (s = p_voice.sym; s; s = s.next) {
371 if (s.invis)
372 continue
373 switch (s.type) {
374 case C.NOTE:
375 case C.REST:
376 nl = s.nflags
377 if (nl <= 0)
378 continue
379 y = staff_tb[s.st].y
380 s2 = s
381 while (s.next && s.next.nflags > 0) {
382 s = s.next
383 if (s.nflags > nl)
384 nl = s.nflags
385 if (s.beam_end)
386 break
387 }
388 draw_dur(s2, y + 7, s, 1, nl)
389 break
390 }
391 }
392 }, // draw_symbols()
393
394 // set some parameters
395 set_fmt: function(of, cmd, param) {
396 if (cmd == "gamelan") {
397 var cfmt = this.cfmt()
398
399 if (!this.get_bool(param))
400 return
401 cfmt.gamelan = true
402 cfmt.staffsep = 20
403 cfmt.sysstaffsep = 14
404 this.set_v_param("stafflines", "...")
405 cfmt.tuplets = [0, 1, 0, 1] // [auto, slur, number, above]
406 return
407 }
408 of(cmd, param)
409 }, // set_fmt()
410
411 // adjust some values
412 set_pitch: function(of, last_s) {
413 of(last_s)
414 if (!last_s
415 || !this.cfmt().gamelan)
416 return // first time
417
418 var C = abc2svg.C
419
420 for (var s = this.get_tsfirst(); s; s = s.ts_next) {
421 switch (s.type) {
422
423 // adjust the vertical spacing above the note heads
424 case C.NOTE:
425 s.ymx = 20 * s.nhd + (s.nflags > 0 ? 30 : 24)
426 if (s.notes[s.nhd].jo > 2) {
427 s.ymx += 3
428 if (s.notes[s.nhd].jo > 3)
429 s.ymx += 2
430 }
431 s.ys = s.ymx // (for tuplets)
432 break
433 }
434 }
435 }, // set_pitch()
436
437 // set the width of some symbols
438 set_width: function(of, s) {
439 of(s)
440 if (!this.cfmt().gamelan)
441 return
442
443 var w, m, note,
444 C = abc2svg.C
445
446 switch (s.type) {
447 case C.CLEF:
448 case C.KEY:
449 case C.METER:
450 s.wl = s.wr = .1 // (must not be null)
451 break
452 }
453 }, // set_width()
454
455 set_hooks: function(abc) {
456 abc.do_pscom = abc2svg.gamelan.do_pscom.bind(abc, abc.do_pscom)
457 abc.draw_symbols = abc2svg.gamelan.draw_symbols.bind(abc, abc.draw_symbols)
458 abc.output_music = abc2svg.gamelan.output_music.bind(abc, abc.output_music)
459 abc.set_format = abc2svg.gamelan.set_fmt.bind(abc, abc.set_format)
460 abc.set_pitch = abc2svg.gamelan.set_pitch.bind(abc, abc.set_pitch)
461 abc.set_width = abc2svg.gamelan.set_width.bind(abc, abc.set_width)
462
463 // big staccato dot
464 abc.get_glyphs().gstc = '<circle id="gstc" cx="0" cy="-3" r="2"/>'
465 abc.get_decos().gstc = "0 gstc 5 1 1"
466
467 abc.add_style("\n.bn {font-family:sans-serif; font-size:16px}")
468 } // set_hooks()
469 } // gamelan
470
471 if (!abc2svg.mhooks)
472 abc2svg.mhooks = {}
473 abc2svg.mhooks.gamelan = abc2svg.gamelan.set_hooks