From 2a9b6ef0be04b5b5cacc47922e19c05d190d6ade Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 26 Jul 2025 14:55:53 -0400 Subject: first stab at fretboard player. need to handle actual chord positions so everything isn't in 4, but otherwise works? --- scale.mjs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'scale.mjs') diff --git a/scale.mjs b/scale.mjs index 5b04831..fbd3617 100644 --- a/scale.mjs +++ b/scale.mjs @@ -8,12 +8,22 @@ function notesBetween(a, b) { } } +export function toCents([aNote, aChord], [bNote, bChord]) { + console.debug('- a', [aNote, aChord]); + console.debug('- b', [bNote, bChord]); + const offset = (aNote.distanceTo(bNote)) * 100; + console.debug('- offset', offset); + const scale = (bChord - aChord) * 1200; + console.debug('- scale', scale); + return scale + offset; +} + export class Note { static natural = '♮'; static sharp = '♯'; static flat = '♭'; - static range = ['A', 'B', 'C', 'D', 'E', 'F', 'G']; + static range = ['C', 'D', 'E', 'F', 'G', 'A', 'B']; static get first() { return this.range.at(0); } @@ -26,6 +36,16 @@ export class Note { return this.noSharps.map(n => String.fromCharCode(n.charCodeAt()+1)) } + static get noteRange() { + return this.range.flatMap(n => { + if (this.noSharps.includes(n)) { + return new Note(n); + } else { + return [new Note(n, false), new Note(n, true)]; + } + }) + } + static fromString(string) { const [root, accidental] = [string[0], string[1]]; switch (accidental) { @@ -62,6 +82,14 @@ export class Note { this.isSharp = isSharp; } + distanceTo(dest) { + const srcFromC = Note.noteRange.findIndex(t => + t.toString() === this.toString()); + const destFromC = Note.noteRange.findIndex(t => + t.toString() === dest.toString()); + return destFromC - srcFromC; + } + toString() { return this.root + (this.isSharp ? Note.sharp : ''); } -- cgit v1.3