summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@spork.org>2025-03-09 21:23:29 -0400
committerBrian Cully <bjc@spork.org>2025-03-09 21:23:29 -0400
commitdc9f5c34c4b09afad79d768cc420566385467f82 (patch)
tree875fa133c4b2d7632ba7c9c1b95a611256995335
parent4a4ff55ae2793183a18a5cd6acbb2bc1aae675e5 (diff)
downloadchords-dc9f5c34c4b09afad79d768cc420566385467f82.tar.gz
chords-dc9f5c34c4b09afad79d768cc420566385467f82.zip
js: fix f♯maj scale display by using e♯ on the 7th.
-rw-r--r--scale.mjs23
1 files changed, 19 insertions, 4 deletions
diff --git a/scale.mjs b/scale.mjs
index b4f043d..5341413 100644
--- a/scale.mjs
+++ b/scale.mjs
@@ -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);
}