Skip to content

Instantly share code, notes, and snippets.

@teyc
Last active August 29, 2015 14:27
Show Gist options
  • Save teyc/8eea5e9afc027bd2da6a to your computer and use it in GitHub Desktop.
Save teyc/8eea5e9afc027bd2da6a to your computer and use it in GitHub Desktop.
Converts from music notes to number of semitones
type Notes =
| A = 0
| ASharp = 1
| B = 2
| C = 3
| CSharp = 4
| D = 5
| DSharp = 6
| E = 7
| F = 8
| FSharp = 9
| G = 10
| GSharp = 11;;
// Incomplete http://www.seventhstring.com/resources/notefrequencies.html
let notesToFreq =
Map[
(Notes.A, 4), 440.0;
(Notes.ASharp, 4), 466.2;
(Notes.B, 4), 493.9;
(Notes.C, 5), 523.3;
(Notes.CSharp, 5), 554.4;
(Notes.D, 5), 587.3;
(Notes.DSharp, 5), 622.3;
(Notes.E, 5), 659.3;
(Notes.F, 5), 698.5;
(Notes.FSharp, 5), 740.0;
(Notes.G, 5), 784.0;
(Notes.GSharp, 5), 830.6];;
let semitoneInterval = 1.0595;;
let numberOfSemitones first second = log(Map.find second notesToFreq / Map.find first notesToFreq)/log(semitoneInterval) |> round |> int;;
// Test
numberOfSemitones (Notes.A,4) (Notes.GSharp, 5) = 11;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment