Working version with note names

This commit is contained in:
Aria 2025-08-22 11:25:13 -05:00
parent 938c92fda2
commit b08890bd9e
3 changed files with 75 additions and 46 deletions

View file

@ -101,11 +101,11 @@ export function nearestNumberIndex(target, numbers, preferHigher) {
let scaleSteps = {}; // [scaleName]: semitones[]
export function stepInNamedScale(step, scale, anchor, preferHigher) {
let [root, scaleName] = Scale.tokenize(scale);
const [root, scaleName] = Scale.tokenize(scale);
const rootMidi = x2midi(root);
const rootChroma = midi2chroma(rootMidi);
if (!scaleSteps[scaleName]) {
let { intervals } = Scale.get(`C ${scaleName}`);
const { intervals } = Scale.get(`C ${scaleName}`);
// cache result
scaleSteps[scaleName] = intervals.map(step2semitones);
}
@ -236,16 +236,3 @@ export function transpose(note, step) {
const offsetAccidentals = accidentalString(Step.accidentals(step) + Note.accidentals(note) + stepIndex - indexOffset); // "we need to add a # to to the G to make it a major third from E"
return [targetNote, offsetAccidentals].join('');
}
// Converts a `scaleName` into a corresponding list of chromas between 0 and 12
export function scaleToChromas(scaleName) {
if (Array.isArray(scaleName)) {
scaleName = scaleName.flat().join(' ');
}
const [tonic, name] = Scale.tokenize(scaleName);
const rootMidi = noteToMidi(tonic);
const chroma = rootMidi % 12;
const intervals = Scale.get(name).intervals;
const scaleSteps = intervals.map(Interval.semitones);
return scaleSteps.map((s) => (s + chroma) % 12);
}