Created
August 24, 2015 01:02
-
-
Save NickEntin/32c37e3d31724b229696 to your computer and use it in GitHub Desktop.
Note frequency calculation in Objective-C
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
#import <Foundation/Foundation.h> | |
typedef enum : int { | |
C = 0, | |
D = 2, | |
E = 4, | |
F = 5, | |
G = 7, | |
A = 9, | |
B = 11, | |
} Note; | |
typedef enum : int { | |
None = 0, | |
Sharp = 1, | |
Flat = -1, | |
} Modifier; | |
@interface NoteUtility : NSObject | |
+ (double)frequencyForNote:(Note)note withModifier:(Modifier)modifier inOctave:(int)octave; | |
@end |
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
#import "NoteUtility.h" | |
@implementation NoteUtility | |
+ (double)frequencyForNote:(Note)note withModifier:(Modifier)modifier inOctave:(int)octave { | |
int halfStepsFromA4 = note - A; | |
halfStepsFromA4 += 12 * (octave - 4); | |
halfStepsFromA4 += modifier; | |
double frequencyOfA4 = 440.0; | |
double a = 1.059463094359; | |
return frequencyOfA4 * pow(a, halfStepsFromA4); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment