added _asNumber + interprete numbers as midi

This commit is contained in:
Felix Roos 2022-02-27 21:24:23 +01:00
parent 190729df73
commit 8a7e49780a
3 changed files with 44 additions and 6 deletions

11
util.mjs Normal file
View file

@ -0,0 +1,11 @@
export const isNote = (name) => /^[a-gA-G][#b]?[0-9]$/.test(name);
export const tokenizeNote = (note) => note.match(/^([a-gA-G])([#b])?([0-9])?$/).slice(1);
export const toMidi = (note) => {
const [pc, acc, oct] = tokenizeNote(note);
const chroma = { c: 0, d: 2, e: 4, f: 5, g: 7, a: 9, b: 11 }[pc];
const offset = acc?.split('').reduce((o, char) => o + { '#': 1, b: -1 }[char], 0) || 0;
return (Number(oct) + 1) * 12 + chroma + offset;
};
export const fromMidi = (n) => {
return Math.pow(2, (n - 69) / 12) * 440;
};