Created
December 23, 2015 06:54
-
-
Save srikumarks/6b338307b2ba67741a0f to your computer and use it in GitHub Desktop.
Pitch class calculator
This file contains 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
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