aboutsummaryrefslogtreecommitdiff
path: root/public
diff options
context:
space:
mode:
authorJoris2024-01-03 10:49:33 +0100
committerJoris2024-01-03 10:49:33 +0100
commitf76737fdf157dffce055bdfa915252b41f3e73eb (patch)
treec4a3492f3ae9f2459ca03ab4de1d582a4ad834db /public
parentb5292689534c00918675223d1f36174bbf44406f (diff)
Consider bemols and sharps
Diffstat (limited to 'public')
-rw-r--r--public/main.js18
1 files 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}`) {