Created
April 1, 2025 07:25
-
-
Save Paul-Browne/da8be77e0d01f5171798309f59abba95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NOTES = [ | |
"C", | |
"C♯/D♭", | |
"D", | |
"D♯/E♭", | |
"E", | |
"F", | |
"F♯/G♭", | |
"G", | |
"G♯/A♭", | |
"A", | |
"A♯/B♭", | |
"B" | |
] | |
const frequencyToNote = frequency => { | |
const midi = Math.round(69 + 12 * Math.log2(frequency / 440)) % 12; | |
return NOTES[midi]; | |
} | |
const useFlatsOrSharps = notes => notes.reduce((acc, current) => { | |
let usedNote = current | |
if(current.includes("/")){ | |
const [sharp, flat] = current.split("/") | |
usedNote = notes.includes(sharp[0]) ? flat : sharp | |
} | |
return [ | |
...acc, | |
usedNote | |
] | |
}, []) | |
const getMusicalNotes = notesAsFrequencies => useFlatsOrSharps(notesAsFrequencies.map(freq => frequencyToNote(freq))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment