// clair.js - module to output Clairnote sheets (https:clairnote.org) // // Copyright (C) 2019-2023 Jean-Francois Moine // // This file is part of abc2svg. // // abc2svg is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // abc2svg is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with abc2svg. If not, see . // // This module is loaded when "%%clairnote" appears in a ABC source. // // Parameters (none) // %%clairnote 1 if (typeof abc2svg == "undefined") var abc2svg = {} abc2svg.clair = { // table for helper lines (from C, to c'') // index = (note pitch + 16) // value = array of line numbers / null hl_tb: [ new Int8Array([-10,-8,-6,-4,-2]), // _B,, -18 new Int8Array([-10,-9,-8,-6,-4,-2]), // B,, -17 new Int8Array([-8,-6,-4,-2]), // C, -16 new Int8Array([-8,-6,-4,-2]), // ^C, -15 new Int8Array([-8,-7,-6,-4,-2]), // D, -14 new Int8Array([-6,-4,-2]), // ^D, -13 new Int8Array([-6,-4,-2]), // E, -12 new Int8Array([-6,-4,-2]), // F, -11 new Int8Array([-6,-5,-4,-2]), // ^F, -10 new Int8Array([-4,-2]), // G, -9 new Int8Array([-4,-2]), // ^G, -8 new Int8Array([-4,-2]), // A, -7 new Int8Array([-4,-3,-2]), // _B, -6 new Int8Array([-2]), // B, -5 new Int8Array([-2]), // C -4 new Int8Array([-2]), // ^C -3 new Int8Array([-2,-1]), // D -2 null, // ^D -1 null, // E 0 null, // F 1 new Int8Array([1]), // ^F 2 null, // G 3 null, // ^G 4 null, // A 5 new Int8Array([3,4]), // _B 6 new Int8Array([4]), // B 7 new Int8Array([4]), // c 8 new Int8Array([4]), // ^c 9 new Int8Array([4,5]), // d 10 null, // _e 11 null, // e 12 null, // f 13 new Int8Array([7]), // ^f 14 null, // g 15 null, // ^g 16 null, // a 17 new Int8Array([9,10]), // _b 18 new Int8Array([10]), // b 19 new Int8Array([10]), // c' 20 new Int8Array([10]), // ^c' 21 new Int8Array([10,11]), // d' 22 new Int8Array([10,11]), // _e' 23 new Int8Array([10,12]), // e' 24 new Int8Array([10,12]), // f' 25 new Int8Array([10,12,13]), // ^f' 26 new Int8Array([10,12,13]), // g' 27 new Int8Array([10,12,14]), // ^g' 28 new Int8Array([10,12,14]), // a' 29 new Int8Array([10,12,14,15]), // _b' 30 new Int8Array([10,12,14,15]), // b' 31 new Int8Array([10,12,14,16]) // c'' 32 ], // draw the helper lines draw_hl: function(of, s) { if (!s.p_v.clair) { of(s) return } var i, m, hl, dx = s.grace ? 4 : [4.7, 5, 6, 7.2, 7.5][s.head] * 1.4, p_st = this.get_staff_tb()[s.st] for (m = 0; m <= s.nhd; m++) { hl = abc2svg.clair.hl_tb[s.notes[m].pit] if (!hl) continue for (i = 0; i < hl.length; i++) this.set_hl(p_st, hl[i], s.x, -dx, dx) } }, // draw_hl() // draw the key signature draw_keysig: function(of, x, s) { if (!s.p_v.clair) { of(x, s) return } var i, staffb = this.get_staff_tb()[s.st].y, a_tb = new Int8Array(24), sc = [0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23], // major scale (2 octaves) bn_tb = [7, 2, -3, 4, -1, 6, 1, 8, 3, -2, 5, 0, 7, 2, -3], // #sf -> (major) base note bn = bn_tb[s.k_sf + 7] // base note if (!s.k_a_acc) { if (s.k_sf > 0) { for (i = 1; i <= s.k_sf; i++) { a_tb[bn_tb[i] + 3] = 1; // index = y + 3 a_tb[bn_tb[i] + 12 + 3] = 1 } if (bn + sc[s.k_mode] > 8) bn -= 12 for (i = 7; --i >= 0; ) { y = bn + sc[s.k_mode + i] this.xygl(x, staffb + y * 3, (y & 1) ? "wk" : "bk"); if (a_tb[y + 3]) this.xygl(x, staffb + y * 3, "sht"); switch (s.k_mode + i) { case 3: case 7: case 10: x += 4.5 break } } } else { for (i = s.k_sf; i < 0; i++) { a_tb[bn_tb[i + 6] + 3] = 1; // index = y + 3 a_tb[bn_tb[i + 6] + 12 + 3 ] = 1 } if (bn + sc[s.k_mode] > 8) bn -= 12 for (i = 0; i < 7; i++) { // down top y = bn + sc[s.k_mode + i] this.xygl(x, staffb + y * 3, (y & 1) ? "wk" : "bk"); if (a_tb[y + 3]) this.xygl(x, staffb + y * 3, "flt"); switch (s.k_mode + i) { case 2: case 6: case 9: x += 4.5 break } } } } else if (s.k_a_acc.length) { for (i = 0; i < s.k_a_acc.length; i++) { var acc = s.k_a_acc[i]; j = sc[(acc.pit + 3) % 7]; a_tb[j] = acc.acc; a_tb[j + 12] = acc.acc } // todo } }, // draw_keysig() // update a clef new_clef: function(s) { switch (s.clef_type) { case 't': s.clef_line = 3; s.clefpit = -2; s.clef_name = "cl4" break case 'c': s.clef_line = 3; s.clefpit = 6; s.clef_name = "cl3" break case 'b': s.clef_line = 3; s.clefpit = 14; s.clef_name = "cl2" break } }, // new_clef() // function called before SVG generation output_music: function(of) { var s, m, mp, p_v, v, cfmt = this.cfmt(), tsfirst = this.get_tsfirst(), voice_tb = this.get_voice_tb() // define the new clefs and key signatures // (clefs converted from clairnote.ly) this.do_begin_end("svg", null, '\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ ') // change the pitches of all notes // (pitch + accidental => offset) for (v = 0; v < voice_tb.length; v++) { p_v = voice_tb[v] if (!p_v.clair) continue p_v.scale = 1.3; for (s = p_v.sym; s; s = s.next) { if (!s.dur) continue for (m = 0; m <= s.nhd; m++) { mp = s.notes[m].midi if (mp) { mp -= 46; s.notes[m].pit = mp delete s.notes[m].acc } } } } of() }, // set a format parameter set_fmt: function(of, cmd, param) { if (cmd == "clairnote") { if (!this.get_bool(param)) return this.set_v_param("clair", true); this.set_v_param("stafflines", "|-|---|-|") this.set_v_param("staffscale", .8) return } of(cmd, param) }, // set the pitches according to the clefs set_pitch: function(of, last_s) { if (last_s) { of(last_s) return // not the 1st time } var p_v, s, v, voice_tb = this.get_voice_tb(), staff_tb = this.get_staff_tb() for (v = 0; v < voice_tb.length; v++) { p_v = voice_tb[v] if (!p_v.clair) continue abc2svg.clair.new_clef(staff_tb[p_v.st].clef) for (s = p_v.sym; s; s = s.next) { if (s.clef_type) abc2svg.clair.new_clef(s) } } of(last_s) }, // set the clairnote flag in the current voice set_vp: function(of, a) { var i, curvoice = this.get_curvoice() for (i = 0; i < a.length; i++) { if (a[i] == "clair=") { curvoice.clair = a[i + 1] break } } of(a) }, // set the width of the clairnote key signatures set_width: function(of, s) { if (s.k_sf && s.p_v && s.p_v.clair) { s.wl = 8; s.wr = 10 } else { of(s) } }, set_hooks: function(abc) { abc.draw_hl = abc2svg.clair.draw_hl.bind(abc, abc.draw_hl); abc.draw_keysig = abc2svg.clair.draw_keysig.bind(abc, abc.draw_keysig); abc.output_music = abc2svg.clair.output_music.bind(abc, abc.output_music); abc.set_format = abc2svg.clair.set_fmt.bind(abc, abc.set_format); abc.set_pitch = abc2svg.clair.set_pitch.bind(abc, abc.set_pitch); abc.set_vp = abc2svg.clair.set_vp.bind(abc, abc.set_vp); abc.set_width = abc2svg.clair.set_width.bind(abc, abc.set_width) } } // clair if (!abc2svg.mhooks) abc2svg.mhooks = {} abc2svg.mhooks.clair = abc2svg.clair.set_hooks