Debian Codegraph @

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

raw symbols SCIP
1 // tropt.js - module to optimize the notes after transposition
2 //
3 // Copyright (C) 2022-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 "%%tropt" appears in a ABC source.
21
22 if (typeof abc2svg == "undefined")
23 var abc2svg = {}
24
25 abc2svg.tropt = {
26
27 // function called before start of the generation
28 voice_adj: function(of, last_s) {
29 if (last_s) { // if not first time
30 of(last_s)
31 return
32 }
33 var v, p_v, s, m, nt, p, a, np, na,
34 C = abc2svg.C,
35 vo_tb = this.get_voice_tb(),
36 nv = vo_tb.length
37
38 // check if some next note has the same pitch without any accidental
39 function ok(s, p) {
40 var nt, m
41
42 while (s) {
43 if (s.bar_type)
44 return 1 //true
45 if (s.type == C.NOTE) {
46 for (m = 0; m <= s.nhd; m++) {
47 nt = s.notes[m]
48 if (nt.pit == p)
49 return nt.acc
50 }
51 }
52 s = s.next
53 }
54 return 1 //true
55 } // ok()
56
57 for (v = 0; v < nv; v++) {
58 p_v = vo_tb[v]
59 if (!p_v.tropt || !p_v.ckey.k_none || !p_v.tr_sco)
60 continue
61 for (s = p_v.sym; s; s = s.next) {
62 if (s.type != C.NOTE)
63 continue
64 for (m = 0; m <= s.nhd; m++) {
65 nt = s.notes[m]
66 if (nt.tie_s) { // if end of tie
67 nt.pit = nt.tie_s.pit
68 continue
69 }
70 p = nt.pit % 7 // A..G
71 a = nt.acc
72 na = 3
73 switch (a) {
74 case -1:
75 switch (p) {
76 case 2: // C
77 case 5: // F
78 break
79 default:
80 continue
81 }
82 np = nt.pit - 1
83 break
84 case -2:
85 switch (p) {
86 case 2: // C
87 case 5: // F
88 na = -1
89 break
90 }
91 np = nt.pit - 1
92 break
93 case 1:
94 switch (p) {
95 case 1: // B
96 case 4: // E
97 break
98 default:
99 continue
100 }
101 np = nt.pit + 1
102 break
103 case 2:
104 switch (p) {
105 case 1: // B
106 case 4: // E
107 na = 1
108 break
109 }
110 np = nt.pit + 1
111 break
112 default:
113 continue
114 }
115 if (ok(s, np)) {
116 nt.pit = np
117 nt.acc = na
118 if (p_v.map)
119 this.set_map(p_v, nt, na)
120 }
121 }
122 }
123 }
124 of(last_s)
125 }, // voice_adj()
126
127 // set the tropt parameter
128 do_pscom: function(of, text) {
129 if (text.indexOf("tropt ") == 0)
130 this.set_v_param("tropt", text.split(/[ \t]/)[1])
131 else
132 of(text)
133 }, // do_pscom()
134
135 // set the tropt parameter in the current voice
136 set_vp: function(of, a) {
137 var i,
138 curvoice = this.get_curvoice()
139
140 for (i = 0; i < a.length; i++) {
141 if (a[i] == "tropt=") {
142 curvoice.tropt = this.get_bool(a[i + 1])
143 break
144 }
145 }
146 of(a)
147 }, // set_vp()
148
149 set_hooks: function(abc) {
150 abc.do_pscom = abc2svg.tropt.do_pscom.bind(abc, abc.do_pscom)
151 abc.voice_adj = abc2svg.tropt.voice_adj.bind(abc, abc.voice_adj)
152 abc.set_vp = abc2svg.tropt.set_vp.bind(abc, abc.set_vp)
153 }
154 } // tropt
155
156 if (!abc2svg.mhooks)
157 abc2svg.mhooks = {}
158 abc2svg.mhooks.tropt = abc2svg.tropt.set_hooks