-
-
Save jochemstoel/4c8bd7621a0a2eae4fcdc2c7bd448088 to your computer and use it in GitHub Desktop.
Convert note to frequency
This file contains hidden or 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
| // Takes string of Note + Octave | |
| // Example: | |
| // var frequency = getFrequency('C3'); | |
| var getFrequency = function (note) { | |
| var notes = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'], | |
| octave, | |
| keyNumber; | |
| if (note.length === 3) { | |
| octave = note.charAt(2); | |
| } else { | |
| octave = note.charAt(1); | |
| } | |
| keyNumber = notes.indexOf(note.slice(0, -1)); | |
| if (keyNumber < 3) { | |
| keyNumber = keyNumber + 12 + ((octave - 1) * 12) + 1; | |
| } else { | |
| keyNumber = keyNumber + ((octave - 1) * 12) + 1; | |
| } | |
| // Return frequency of note | |
| return 440 * Math.pow(2, (keyNumber- 49) / 12); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment