diff --git a/packages/core/util.mjs b/packages/core/util.mjs index 3bad9199..c2b1fcdb 100644 --- a/packages/core/util.mjs +++ b/packages/core/util.mjs @@ -18,14 +18,17 @@ export const tokenizeNote = (note) => { return [pc, acc, oct ? Number(oct) : undefined]; }; +const chromas = { c: 0, d: 2, e: 4, f: 5, g: 7, a: 9, b: 11 }; +const accs = { '#': 1, b: -1, s: 1, f: -1 }; + // turns the given note into its midi number representation -export const noteToMidi = (note) => { - const [pc, acc, oct = 3] = tokenizeNote(note); +export const noteToMidi = (note, defaultOctave = 3) => { + const [pc, acc, oct = defaultOctave] = tokenizeNote(note); if (!pc) { throw new Error('not a note: "' + note + '"'); } - const chroma = { c: 0, d: 2, e: 4, f: 5, g: 7, a: 9, b: 11 }[pc.toLowerCase()]; - const offset = acc?.split('').reduce((o, char) => o + { '#': 1, b: -1, s: 1, f: -1 }[char], 0) || 0; + const chroma = chromas[pc.toLowerCase()]; + const offset = acc?.split('').reduce((o, char) => o + accs[char], 0) || 0; return (Number(oct) + 1) * 12 + chroma + offset; }; export const midiToFreq = (n) => {