Debian Codegraph @

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

raw symbols SCIP
1 // mdnn.js - module to output Modernised Diatonic Numerical Notation
2 // (https://medium.com/@info_70544/the-case-for-numerical-music-notation-part-1-introduction-and-history-5f1543ca8a95)
3 //
4 // Copyright (C) 2020-2023 Jean-Francois Moine
5 //
6 // This file is part of abc2svg.
7 //
8 // abc2svg is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // abc2svg is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
20 //
21 // This module is loaded when "%%mdnn" appears in a ABC source.
22 //
23 // Parameters
24 // (none)
25 //
26 //fixme:nsk
27 // - Non Standard Key signature - other way to display the accidentals on key change
28
29 // polyfill
30 if (typeof Object.assign !== 'function') {
31 // Must be writable: true, enumerable: false, configurable: true
32 Object.defineProperty(Object, "assign", {
33 value: function assign(target, varArgs) { // .length of function is 2
34 'use strict';
35 if (target === null || target === undefined) {
36 throw new TypeError('Cannot convert undefined or null to object');
37 }
38
39 var to = Object(target);
40
41 for (var index = 1; index < arguments.length; index++) {
42 var nextSource = arguments[index];
43
44 if (nextSource !== null && nextSource !== undefined) {
45 for (var nextKey in nextSource) {
46 // Avoid bugs when hasOwnProperty is shadowed
47 if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
48 to[nextKey] = nextSource[nextKey];
49 }
50 }
51 }
52 }
53 return to;
54 },
55 writable: true,
56 configurable: true
57 });
58 }
59
60 if (typeof abc2svg == "undefined")
61 var abc2svg = {}
62
63 abc2svg.mdnn = {
64
65 cde2fcg: new Int8Array([0, 2, 4, -1, 1, 3, 5]),
66 cgd2cde: new Int8Array([0, -4, -1, -5, -2, -6, -3,
67 0, -4, -1, -5, -2, -6, -3, 0]),
68 acc_tb: ["aff", "af", "n", "s", "ss"],
69
70 glyphs: {
71 n1: '<text id="n1" class="bn" x="-1.5" y="16">1</text>',
72 n2: '<text id="n2" class="bn" x="-1.5" y="16">2</text>',
73 n3: '<text id="n3" class="bn" x="-1.5" y="16">3</text>',
74 n4: '<text id="n4" class="bn" x="-1.5" y="16">4</text>',
75 n5: '<text id="n5" class="bn" x="-1.5" y="16">5</text>',
76 n6: '<text id="n6" class="bn" x="-1.5" y="16">6</text>',
77 n7: '<text id="n7" class="bn" x="-1.5" y="16">7</text>',
78 nq: '<text id="nq" class="bn" x="8" y="16">\'</text>',
79 nc: '<text id="nc" class="bn" x="8" y="16">,</text>',
80 nh: '<path id="nh" class="stroke" d="m3.5 0l-7.2 0 0 -6.2 7.2 0"/>',
81 nw: '<path id="nw" class="stroke" d="m6 -5l-7.2 0 0 -6.2 7.2 0 0 6.2"/>',
82 aff: '<text id="aff" class="music" x="-8" y="11">&#xe264;</text>',
83 af: '<text id="af" class="music" x="-8" y="11">&#xe260;</text>',
84 nn: '<text id="nn" class="music" x="-8" y="11">&#xe261;</text>',
85 ns: '<text id="ns" class="music" x="-8" y="11">&#xe262;</text>',
86 nss: '<text id="nss" class="music" x="-8" y="11">&#xe263;</text>'
87 }, //glyphs
88
89 decos: {
90 n1: "9 n1 0 0 0",
91 n2: "9 n2 0 0 0",
92 n3: "9 n3 0 0 0",
93 n4: "9 n4 0 0 0",
94 n5: "9 n5 0 0 0",
95 n6: "9 n6 0 0 0",
96 n7: "9 n7 0 0 0",
97 q: "0 nq 0 0 0",
98 c: "0 nc 0 0 0",
99 h: "0 nh 0 0 0",
100 w: "0 nw 0 0 0",
101 aff: "0 aff 0 0 0",
102 af: "0 af 0 0 0",
103 n: "0 nn 0 0 0",
104 s: "0 ns 0 0 0",
105 ss: "0 nss 0 0 0"
106 }, // decos
107
108 // change the voices into MDNN
109 //fixme: actually, one voice only
110 output_music: function(of) {
111 var C = abc2svg.C,
112 abc = this,
113 cfmt = abc.cfmt(),
114 cur_sy = abc.get_cur_sy(),
115 voice_tb = abc.get_voice_tb(),
116 sf = voice_tb[0].key.k_sf,
117 delta = abc2svg.mdnn.cgd2cde[sf + 7] - 2
118 var s, s2, note, pit, nn, p, a, i,
119 prev_oct = -10
120
121 if (!cfmt.mdnn) {
122 of()
123 return
124 }
125
126 delete voice_tb[0].key.k_a_acc // no accidental
127 voice_tb[0].clef.invis = true // no (visible) clef
128 cur_sy.staves[0].stafflines = "..." // empty staff
129
130 // scan the first voice of the tune
131 for (s = voice_tb[0].sym; s; s = s.next) {
132 switch (s.type) {
133 case C.CLEF:
134 s.invis = true
135 continue
136 case C.KEY:
137 //fixme:nsk
138 // delete s.k_a_acc // don't display the accidentals
139 sf = s.k_sf
140 delta = abc2svg.mdnn.cgd2cde[sf + 7] - 2
141 nn = sf - s.k_old_sf // delta accidentals
142 //fixme:nsk
143 // if (!nn) {
144 // s.k_old_sf = s.k_sf = 0 // don't display the accidentals
145 // continue
146 // }
147
148 //fixme:comment if nsk
149 // display normal accidentals
150 s.k_old_sf = 0
151 s.k_sf = nn
152 continue
153
154 //fixme:nsk
155 // // display '[' <accidentals ']'
156 // s2 = s
157 //// other solution
158 //// s2 = {
159 //// type: C.SPACE,
160 //// v: s.v,
161 //// p_v: s.p_v,
162 //// st: s.st,
163 //// dur: 0,
164 //// seqst: true,
165 //// invis: true,
166 //// time: s.time,
167 //// prev: s,
168 //// ts_prev: s,
169 //// next: s.next,
170 //// ts_next: s.ts_next
171 //// }
172 //// s.next = s2
173 //// s.ts_next = s2
174 //// if (s2.next)
175 //// s2.next.prev = s2
176 //// if (s2.ts_next)
177 //// s2.ts_next.ts_prev = s2
178 // abc.set_font("annotation")
179 // s2.a_gch = []
180 // s2.a_gch[0] = {
181 // type: '@',
182 // font: abc.get_font("annotation"),
183 // wh: [10,10],
184 // y: -6,
185 //// text: '[' + (nn > 0 ?
186 //// "\u266f\u266f\u266f\u266f\u266f\u266f".slice(0, nn) :
187 //// "\u266d\u266d\u266d\u266d\u266d\u266d".slice(0, -nn)) +
188 //// ']'
189 // text: '[' + (nn > 0 ?
190 // nn + '\u266f' : (-nn) + '\u266d') +
191 // ']'
192 // }
193 // s2.width = abc.strwh(s2.a_gch[0].text + ' ')[0]
194 // s2.a_gch[0].x = -s2.width / 2
195 // s = s2
196 // continue
197 default:
198 continue
199 case C.NOTE: // change the notes
200 break
201 }
202 note = s.notes[0]
203
204 // note head
205 p = note.pit
206 pit = p + delta
207 nn = ((pit + 77) % 7) + 1 // note number
208 abc.dh_put('n' + nn, s, note)
209
210 // display the note as C5 with stem up
211 note.pit = 23 // 'c'
212 s.stem = 1
213
214 // octave
215 nn = (pit / 7) | 0
216 if (nn > prev_oct) {
217 if (prev_oct != -10)
218 abc.dh_put('q', s, note)
219 prev_oct = nn
220 } else if (nn < prev_oct) {
221 abc.dh_put('c', s, note)
222 prev_oct = nn
223 }
224
225 // half and whole notes
226 if (s.dur >= C.BLEN / 2)
227 abc.dh_put(s.dur >= C.BLEN ? 'w' : 'h', s, note)
228
229 // accidentals
230 a = note.acc
231 if (a) {
232 note.acc = 0
233 nn = abc2svg.mdnn.cde2fcg[(p + 5 + 16 * 7) % 7] - sf
234 if (a != 3)
235 nn += a * 7
236 nn = ((((nn + 1 + 21) / 7) | 0) + 2 - 3 + 32 * 5) % 5
237 abc.dh_put(abc2svg.mdnn.acc_tb[nn], s, note)
238 }
239
240 // set the slurs and ties up
241 if (s.sls) {
242 for (i = 0; i < s.sls.length; i++)
243 s.sls[i].ty = C.SL_ABOVE
244 }
245 if (note.sls) {
246 for (i = 0; i < note.sls.length; i++)
247 note.sls[i].ty = C.SL_ABOVE
248 }
249 if (note.tie_ty != undefined)
250 note.tie_ty = C.SL_ABOVE
251 }
252
253 of()
254 }, // output_music()
255
256 set_fmt: function(of, cmd, param) {
257 if (cmd == "mdnn")
258 this.cfmt().mdnn = param
259 else
260 of(cmd, param)
261 }, // set_fmt()
262
263 // don't display the key signature at start of staff
264 set_pitch: function(of, last_s) {
265 of(last_s)
266 if (!last_s // first time
267 || !this.cfmt().mdnn)
268 return
269
270 var C = abc2svg.C,
271 s = this.get_tsfirst()
272
273 if (s && s.next && s.next.type == C.KEY)
274 //fixme:nsk
275 delete s.next.k_a_acc
276 // s.next.a_gch = null
277 }, // set_pitch()
278
279 set_hooks: function(abc) {
280 abc.output_music = abc2svg.mdnn.output_music.bind(abc, abc.output_music)
281 abc.set_format = abc2svg.mdnn.set_fmt.bind(abc, abc.set_format)
282 abc.set_pitch = abc2svg.mdnn.set_pitch.bind(abc, abc.set_pitch)
283
284 Object.assign(abc.get_glyphs(), abc2svg.mdnn.glyphs)
285 Object.assign(abc.get_decos(), abc2svg.mdnn.decos)
286 abc.add_style("\n.bn {font-family:sans-serif; font-size:15px}")
287 }
288 } // mdnn
289
290 if (!abc2svg.mhooks)
291 abc2svg.mhooks = {}
292 abc2svg.mhooks.mdnn = abc2svg.mdnn.set_hooks