Skip to content

Instantly share code, notes, and snippets.

@Paul-Browne
Created April 1, 2025 07:25
Show Gist options
  • Save Paul-Browne/da8be77e0d01f5171798309f59abba95 to your computer and use it in GitHub Desktop.
Save Paul-Browne/da8be77e0d01f5171798309f59abba95 to your computer and use it in GitHub Desktop.
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