Skip to content

Commit

Permalink
feat: add NotationParser
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasmafra committed Jan 31, 2024
1 parent 771af92 commit 1a62e3b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions theory/NotationParser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const notes = [ 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B' ];

export default {
chromaToText(chroma) {
return notes[chroma];
},
textToChroma(text) {
const letter = text.substring(0, 1);
let chroma = notes.indexOf(letter);
if (text.includes('#')) chroma++;
if (text.includes('b')) chroma--;
return (chroma + 12) % 12;
},
pitchToText(midiNumber) {
const chroma = midiNumber % 12;
const octave = Math.floor(midiNumber / 12) - 1;

return this.chromaToText(chroma) + octave;
},
textToPitch(text) {
const chroma = this.textToChroma(text);
text = text.substring(1);
if (text.startsWith('#') || text.startsWith('b')) text = text.substring(1);
const octave = parseInt(text);
return 12 * (octave + 1) + chroma;
},
};

0 comments on commit 1a62e3b

Please sign in to comment.