Skip to content

Instantly share code, notes, and snippets.

@smallhadroncollider
Last active October 11, 2019 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smallhadroncollider/22b01340c88b0c6f7794617b72e7ada4 to your computer and use it in GitHub Desktop.
Save smallhadroncollider/22b01340c88b0c6f7794617b72e7ada4 to your computer and use it in GitHub Desktop.
Generates an object mapping notes to frequencies in equal temperament (e.g. "A4" = 440, "C#2" ~= 17.32)
// generic range function
const range = (start, stop) => Array(stop - start + 1).fill().map((_, i) => start + i);
// frequency of A4
const A = 440;
// the octave range - here 0 - 8
const octaveRange = range(0, 8).map(val => [val, val - 4]);
// semitone offset
const semitoneOffsets = [
["C", -9],
["C#", -8],
["Db", -8],
["D", -7],
["D#", -6],
["Eb", -6],
["E", -5],
["F", -4],
["F#", -3],
["Gb", -3],
["G", -2],
["G#", -1],
["Ab", -1],
["A", 0],
["A#", 1],
["Bb", 1],
["B", 2],
];
// map notes to frequencies
const notes = octaveRange.reduce((ob, [range, multiplier]) => semitoneOffsets.reduce((ob, [note, semitones]) => ({
...ob,
[note + range]: A * Math.pow(2, (semitones + (multiplier * 12)) / 12),
}), ob), {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment