Debian Codegraph @

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

raw symbols SCIP
1 // swing.js - module to set a swing feel
2 //
3 // Copyright (C) 2025 Jean-François 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 "%%playswing" appears in a ABC source.
21 //
22 // Parameters
23 // %%playswing duration_1 restart_time duration_2
24 // with the durations and time in percent of the duration of the two notes.
25 //
26 // This command may appear globally or in a voice.
27
28 "use strict"
29 if (typeof abc2svg == "undefined")
30 var abc2svg = {}
31
32 abc2svg.swing = {
33 // this function is called from sndgen
34 swing: function(first, voice_tb, cfmt) {
35 var v, p_v, sw, s, s2, d, m, beat, anac,
36 C = abc2svg.C,
37 a_dur = [],
38 nv = voice_tb.length
39
40 // set the times of the notes subject to swing adjustment
41 function set_dur(s) {
42 if (!s.a_meter[0] || s.a_meter[0].top[0] == 'C'
43 || !s.a_meter[0].bot)
44 beat = C.BLEN / 4 // quarter note
45 else if (s.a_meter[0].bot[0] == 8
46 && s.a_meter[0].top[0] % 3 == 0)
47 return 1 // no swing
48 else
49 beat = C.BLEN / s.a_meter[0].bot[0] |0
50 a_dur[0] = beat / 2 // x/n/
51 a_dur[1] = beat / 4 // x3//n//
52
53 // check if there is an anacrusis
54 if (!s.time) {
55 anac = 0
56 var wm = s.wmeasure
57 while (s && s.time < wm) {
58 if (s.bar_type) {
59 anac = wm - s.time
60 break
61 }
62 s = s.next
63 }
64 }
65 } // set_dur()
66
67 for (v = 0; v < nv; v++) {
68 p_v = voice_tb[v]
69 sw = cfmt.swing || p_v.swing
70 if (!sw || !p_v.sym)
71 continue
72 if (set_dur(p_v.meter))
73 continue // no swing
74 for (s = p_v.sym; s.next; s = s.next) {
75 if (s.subtype == "swing")
76 sw = s.sw
77 if (!sw
78 || !s.dur) {
79 if (s.a_meter)
80 set_dur(s)
81 continue
82 }
83 if ((s.time + anac - a_dur[0]) % beat
84 && (s.time + anac - a_dur[1]) % beat) {
85 s2 = s
86 continue
87 }
88 if (s2 && s2.time + s2.dur == s.time) {
89 d = s2.dur - (s2.time + s2.dur) % beat + sw[0] * beat
90 s2.dur = d
91 for (m = 0; m < s.nhd; m++)
92 s2.notes[m].dur = d
93 }
94 d = s.time + s.dur
95 s.time = ((s.time / beat | 0) + sw[0] + sw[1]) * beat
96 if (s.dur > beat / 2 || s.ti1)
97 d -= s.time
98 else
99 d = sw[2] * beat
100 s.dur = d
101 for (m = 0; m < s.nhd; m++)
102 s.notes[m].dur = d
103 s2 = s
104 }
105 }
106 }, // swing()
107
108 set_fmt: function(of, cmd, parm) {
109 var parse, sw, curvoice, i, s
110
111 if (cmd == "playswing") {
112 parse = this.get_parse(),
113 curvoice = this.get_curvoice()
114 sw = /(\d+)\s+(\d+)\s+(\d+)/.exec(parm)
115
116 if (sw) {
117 sw = sw.splice(1)
118 if (+sw[0] + +sw[1] + +sw[2] > 100)
119 return abc.syntax(1, "playswing greater than 100%")
120 for (i = 0; i < 3; i++)
121 sw[i] = +sw[i] / 100
122 }
123 if (parse.state >= 2) {
124 s = this.new_block("swing")
125 s.play = s.invis = 1 //true
126 s.sw = sw
127 if (sw)
128 curvoice.swing = 1 //true
129 } else {
130 this.cfmt().swing = sw
131 }
132 return
133 }
134 of(cmd, parm)
135 }, // set_fmt()
136
137 set_hooks: function(abc) {
138 abc.set_format = abc2svg.swing.set_fmt.bind(abc, abc.set_format)
139 }
140 } // swing
141
142 if (!abc2svg.mhooks)
143 abc2svg.mhooks = {}
144 abc2svg.mhooks.swing = abc2svg.swing.set_hooks