From 7928e4a7d3311cc1325d413c53ff349e5ef816b5 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 10 Mar 2025 16:23:07 -0400 Subject: scale: calculate diatonic scales --- key-picker.mjs | 18 +++++++++++------- scale.mjs | 8 ++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/key-picker.mjs b/key-picker.mjs index fe344ae..6d2efbe 100644 --- a/key-picker.mjs +++ b/key-picker.mjs @@ -1,4 +1,4 @@ -import { MajorScale, MinorScale, Note, chromaticScale } from "./scale.mjs"; +import { AugmentedScale, DiminishedScale, MajorScale, MinorScale, Note, allDiatonicScales } from "./scale.mjs"; function scaleFrom(tonic, scale) { switch (scale) { @@ -70,14 +70,18 @@ function formChanged(form) { Array.from(form.getElementsByClassName(c)).forEach(elt => elt.innerText = scale[i]); }); - // todo: memoize this or put it in the scales module. - const allScales = chromaticScale.flatMap(tonic => { - return [MajorScale(tonic), MinorScale(tonic)]; - }) + function suffixFromIndex(i) { + switch (i % 2) { + case 0: + return ''; + case 1: + return 'm'; + } + } const availableScales = - allScales.reduce((acc, s, i) => { - const suffix = i % 2 == 0 ? '' : 'm'; + allDiatonicScales.reduce((acc, s, i) => { + const suffix = suffixFromIndex(i); if (scale.includes(s.tonic) && scale.includes(s.third) && scale.includes(s.fifth)) { return acc.concat(`${s.tonic}${suffix}`); } diff --git a/scale.mjs b/scale.mjs index 9bec03b..d33a5c3 100644 --- a/scale.mjs +++ b/scale.mjs @@ -182,3 +182,11 @@ export function MinorScale(tonic) { const intervals = [2, 1, 2, 2, 1, 2]; return new Scale(tonic, intervals); } + +// well, a lot of them. +export const allDiatonicScales = chromaticScale.flatMap(tonic => { + return [ + MajorScale(tonic), + MinorScale(tonic) + ]; +}) -- cgit v1.3