Skip to content

Instantly share code, notes, and snippets.

@NickEntin
Last active July 26, 2016 09:05
Show Gist options
  • Save NickEntin/7f0516a6c62ab0485720 to your computer and use it in GitHub Desktop.
Save NickEntin/7f0516a6c62ab0485720 to your computer and use it in GitHub Desktop.
Note frequency calculation in Swift
enum Note: Int {
case C = 0
case D = 2
case E = 4
case F = 5
case G = 7
case A = 9
case B = 11
}
enum Modifier: Int {
case Sharp = 1
case Flat = -1
}
func frequencyForNote(note: Note, modifier: Modifier?, octave: Int) -> Double {
var halfStepsFromA4: Int = note.rawValue - Note.A.rawValue
halfStepsFromA4 += 12 * (octave - 4)
if let modifier = modifier {
halfStepsFromA4 += modifier.rawValue
}
let frequencyOfA4 = 440.0
let a = 1.059463094359
return frequencyOfA4 * pow(a, Double(halfStepsFromA4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment