From f76737fdf157dffce055bdfa915252b41f3e73eb Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 3 Jan 2024 10:49:33 +0100 Subject: Consider bemols and sharps --- public/main.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/public/main.js b/public/main.js index a5ede52..97f11f4 100644 --- a/public/main.js +++ b/public/main.js @@ -12,8 +12,8 @@ function appendText(parentElement, text) { return element } -// Only suggest sharps for the moment -tonalities = ['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B'] +tonalities_b = ['C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭', 'A', 'B♭', 'B'] +tonalities_s = ['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B'] function getRoot(c) { if (c.indexOf('♯') > -1 || c.indexOf('♭') > -1) { @@ -24,13 +24,14 @@ function getRoot(c) { } function getShift(t1, t2) { - return tonalities.indexOf(getRoot(t2)) - tonalities.indexOf(getRoot(t1)) + return tonalities_b.indexOf(getRoot(t2)) - tonalities_b.indexOf(getRoot(t1)) } -function applyShift(t1, s) { +function applyShift(tonalities, t1, s) { let root = getRoot(t1) - let mode = t1.substring(root.length) - root = tonalities[(tonalities.indexOf(root) + s + tonalities.length) % tonalities.length] + const mode = t1.substring(root.length) + const index = tonalities_b.indexOf(root) > -1 ? tonalities_b.indexOf(root) : tonalities_s.indexOf(root) + root = tonalities[(index + s + tonalities.length) % tonalities.length] return `${root}${mode}` } @@ -44,9 +45,10 @@ label['className'] = 'g-Chords__Tonality' label.onchange = (e) => { const shift = getShift(tonality, e.target.value) tonality = e.target.value + tonalities = tonality === 'F' || tonality.indexOf('♭') > -1 ? tonalities_b : tonalities_s document.querySelectorAll('.g-Chords__Chord').forEach(chord => { if (chord.innerText !== '%') { - chord.innerText = applyShift(chord.innerText, shift) + chord.innerText = applyShift(tonalities, chord.innerText, shift) } }) } @@ -55,7 +57,7 @@ appendText(label, 'Tonalité :') select = appendElement(label, 'select') select['name'] = 'tonality' -tonalities.forEach(t => { +tonalities_b.forEach(t => { option = appendElement(select, 'option') option['value'] = t if (tonality == `${t}${mode}`) { -- cgit v1.2.3