Last active
July 26, 2016 09:05
-
-
Save NickEntin/7f0516a6c62ab0485720 to your computer and use it in GitHub Desktop.
Note frequency calculation in Swift
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
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