Last active
August 29, 2015 14:04
-
-
Save mildsunrise/4336d48213b5af7c4ca3 to your computer and use it in GitHub Desktop.
Playing with notes and frequencies...
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
# Get the frequency, in Hertz, from an octave number. | |
# `0` corresponds to A0, `1` to A1, etc. | |
frequencyOf = (octaves) -> | |
27.5 * Math.pow(2, octaves) | |
# Parse a note string into its corresponding octave number. | |
# Note: this cannot parse sharp (#) notes yet. | |
parseNote = (string) -> | |
point = string.charCodeAt(0) - 65 | |
offset = point * 2 | |
offset-- if point >= 2 | |
offset-- if point >= 5 | |
octaves = Number string.substr 1 | |
return octaves + offset/12 |
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
# Use them together to get the frequency of a note: | |
frequnecyOf parseNote("A4") #-> 440 | |
frequencyOf parseNote("G5") #-> 1568 | |
frequencyOf parseNote("D3") #-> 293.7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment