diff options
author | Brian Cully <bjc@spork.org> | 2025-03-09 21:23:29 -0400 |
---|---|---|
committer | Brian Cully <bjc@spork.org> | 2025-03-09 21:23:29 -0400 |
commit | dc9f5c34c4b09afad79d768cc420566385467f82 (patch) | |
tree | 875fa133c4b2d7632ba7c9c1b95a611256995335 | |
parent | 4a4ff55ae2793183a18a5cd6acbb2bc1aae675e5 (diff) | |
download | chords-dc9f5c34c4b09afad79d768cc420566385467f82.tar.gz chords-dc9f5c34c4b09afad79d768cc420566385467f82.zip |
js: fix f♯maj scale display by using e♯ on the 7th.
-rw-r--r-- | scale.mjs | 23 |
1 files changed, 19 insertions, 4 deletions
@@ -8,6 +8,16 @@ export const chromaticScale = new Proxy( ['A', 'A♯', 'B', 'C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯'], ringHandler); +function notesBetween(a, b) { + const [tonicA, tonicB] = [a[0], b[0]] + const abs = tonicB.charCodeAt() - tonicA.charCodeAt(); + if (abs < 0) { + return abs + 7; + } else { + return abs; + } +} + function scaleFromIntervals(tonic, intervals) { const scaleIndex = chromaticScale.indexOf(tonic); if (scaleIndex < 0) { @@ -23,14 +33,19 @@ function scaleFromIntervals(tonic, intervals) { const note = chromaticScale[scaleIndex + steps]; // don't display two base notes in a row by changing // accidentals. - if (note[0] === lastBase) { + const delta = notesBetween(lastBase, note); + if (delta === 0) { const nextBase = chromaticScale[scaleIndex + steps + 1][0]; scale.push(`${nextBase}♭`) - lastBase = nextBase[0]; + } else if (delta === 1) { + scale.push(`${note}`) } else { - scale.push(note); - lastBase = note[0]; + // we can only be two away, so take the sharp of the + // previous note. + const nextBase = chromaticScale[scaleIndex + steps - 1][0]; + scale.push(`${nextBase}♯`) } + lastBase = scale[scale.length-1]; } return new Proxy(scale, ringHandler); } |