Skip to content

Instantly share code, notes, and snippets.

@mildsunrise
Last active August 29, 2015 14:04
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 mildsunrise/4336d48213b5af7c4ca3 to your computer and use it in GitHub Desktop.
Save mildsunrise/4336d48213b5af7c4ca3 to your computer and use it in GitHub Desktop.
Playing with notes and frequencies...
# 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
# 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