Debian Codegraph @

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

raw symbols SCIP
1 // abc2svg - roman.js - convert the chord symbols to the RNN
2 // (Roman Numeral Notation)
3 //
4 // Copyright (C) 2021-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 by %%roman.
22 //
23 // Parameters
24 // %%roman int
25 // <int>
26 // = '1': use uppercase letters with 'm' for minor chords
27 // = '2': user lowercase letters for minor chords
28
29 if (typeof abc2svg == "undefined")
30 var abc2svg = {}
31
32 abc2svg.roman = {
33 note_nm: "CDEFGAB",
34
35 // chord names
36 nm_M: ["I", "♯I", "II", "♭III", "III", "IV", "♯IV",
37 "V", "♯V", "VI", "♭VII", "VII"],
38 nm_m: ["I", "♭II", "II", "III", "♯III", "IV", "♭V",
39 "V", "VI", "♯VI", "VII", "♯VII"],
40 // inversions: 1st: (upper)6, 2nd: (upper)6 (lower) 4
41
42 gch_build: function(of, s) {
43 var gch, ix, t,
44 ty = this.cfmt().roman
45
46 // transpose the chord back to "C"
47 function set_nm(p) {
48 var i, o, o2, a, n,
49 csa = []
50
51 i = p.indexOf('/') // get the bass if any
52 while (i > 0) {
53 if (p[i - 1] != '<') // if not </tag
54 break
55 i = p.indexOf('/', i + 1)
56 }
57 if (i < 0) {
58 csa.push(p)
59 } else {
60 csa.push(p.slice(0, i))
61 csa.push(p.slice(i + 1))
62 }
63
64 for (i = 0; i < csa.length; i++) { // main and optional bass
65 p = csa[i]
66
67 o = p.search(/[A-G]/) // get the base chord
68 if (o < 0)
69 continue // not a chord!
70
71 a = 0
72 o2 = o + 1
73 if (p[o2] == '#' || p[o2] == '♯') {
74 a++
75 o2++
76 } else if (p[o2] == 'b' || p[o2] == '♭') {
77 a--
78 o2++
79 }
80 n = [0, 2, 4, 5, 7, 9, 11]
81 [abc2svg.roman.note_nm.indexOf(p[o])]
82 + a
83 + abc2svg.roman.tr
84 n = abc2svg.roman.nm[n % 12]
85 if (ty == 2 && p[o2] == 'm') {
86 n = n.toLowerCase()
87 o2++
88 }
89 csa[i] = p.slice(0, o)
90 + n
91 + p.slice(o2)
92 }
93 return csa.join('/')
94 } // set_nm()
95
96 if (ty && s.a_gch) {
97 for (ix = 0; ix < s.a_gch.length; ix++) {
98 gch = s.a_gch[ix]
99 t = gch.text
100 if (gch.type == 'g' && t && t[0] != 'N') {
101 t = set_nm(t)
102 if (t)
103 gch.text = t
104 }
105 }
106 }
107 of(s)
108 }, // gch_build()
109
110 // hook before the generation
111 o_mus: function(of) {
112 var tsfirst = this.get_tsfirst()
113
114 if (this.cfmt().roman) {
115 abc2svg.roman.tr = (tsfirst.p_v.key.k_sf + 12) * 5
116 // transposition to "C"
117 if (tsfirst.p_v.key.k_mode) {
118 abc2svg.roman.tr += 3 // minor
119 abc2svg.roman.nm = abc2svg.roman.nm_m
120 } else {
121 abc2svg.roman.nm = abc2svg.roman.nm_M
122 }
123 }
124 of()
125 }, // o_mus()
126
127 set_fmt: function(of, cmd, parm) {
128 var ty,
129 cfmt = this.cfmt()
130
131 if (cmd == "roman") {
132 if (!parm)
133 parm = "1"
134 ty = Number(parm)
135 if (isNaN(ty))
136 this.syntax(1, this.errs.bad_val, "%%roman")
137 else
138 cfmt.roman = ty
139 return
140 }
141 of(cmd, parm)
142 }, // set_fmt()
143
144 set_hooks: function(abc) {
145 abc.gch_build = abc2svg.roman.gch_build.bind(abc, abc.gch_build)
146 abc.output_music = abc2svg.roman.o_mus.bind(abc, abc.output_music)
147 abc.set_format = abc2svg.roman.set_fmt.bind(abc, abc.set_format)
148 } // set_hooks()
149 } // roman
150
151 if (!abc2svg.mhooks)
152 abc2svg.mhooks = {}
153 abc2svg.mhooks.roman = abc2svg.roman.set_hooks