Skip to content

Instantly share code, notes, and snippets.

@etolstoy
Created February 17, 2015 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etolstoy/ded5c1b3f8f27873ee28 to your computer and use it in GitHub Desktop.
Save etolstoy/ded5c1b3f8f27873ee28 to your computer and use it in GitHub Desktop.
IDTMessaging Test
- (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