Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created December 23, 2015 06:54
Show Gist options
  • Save srikumarks/6b338307b2ba67741a0f to your computer and use it in GitHub Desktop.
Save srikumarks/6b338307b2ba67741a0f to your computer and use it in GitHub Desktop.
Pitch class calculator
var sharpLabels = ["A", "A♯", "B", "C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯"];
var flatLabels = ["A", "B♭", "B", "C", "D♭", "D", "E♭", "E", "F", "G♭", "G", "A♭"];
function pitchClass(freq_Hz, labels) {
var cents = Math.round(1200 * Math.log(freq_Hz/440) / Math.log(2));
var semitones = Math.round(cents / 100);
var pclass = (((semitones % 12) + 12) % 12);
var octave = 4 + Math.floor(semitones / 12);
var tuning = cents - semitones * 100;
return {
pitch_class: labels[pclass],
octave: octave,
tuning_cents: tuning
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment