Created
February 17, 2015 20:15
-
-
Save etolstoy/ded5c1b3f8f27873ee28 to your computer and use it in GitHub Desktop.
IDTMessaging Test
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
- (void)prepareToRecordAudio { | |
self.lowpassResults = 0.0f; | |
NSError *error; | |
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; | |
// Setup default settings for the recorder | |
self.recorder = [[AVAudioRecorder alloc] initWithURL:[self applicationDocumentsDirectory] | |
settings:@{ | |
AVFormatIDKey : @(kAudioFormatMPEG4AAC), | |
AVSampleRateKey : @(44100.0f), | |
AVNumberOfChannelsKey : @(1), | |
AVEncoderAudioQualityKey : @(AVAudioQualityMax) | |
} | |
error:&error]; | |
[self.recorder prepareToRecord]; | |
[self.recorder setMeteringEnabled:YES]; | |
[self startRecordingAudio]; | |
} | |
- (void)startRecordingAudio { | |
[self.recorder record]; | |
self.recordingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateAverageFrequencyData) userInfo:nil repeats:YES]; | |
[[NSRunLoop currentRunLoop] addTimer:self.recordingTimer forMode:NSRunLoopCommonModes]; | |
} | |
- (void)updateAverageFrequencyData { | |
[self.recorder updateMeters]; | |
// Apply lowpass filter to current powerPeak data | |
const CGFloat kAlpha = 0.05; | |
CGFloat currentPeakPower = pow(10, (kAlpha * [self.recorder peakPowerForChannel:0])); | |
self.lowpassResults = kAlpha * currentPeakPower + (1.0 - kAlpha) * self.lowpassResults; | |
if (self.lowpassResults < 0.1) { | |
// Here goes the magic transformation to get Hz frequency. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment