Friday, April 27, 2018

Real time Pitch Change while recording audio with AVAudioRecorder

Leave a Comment

I am trying to achieve functionality in which I can record a video and apply the effect over it in Real-time like an alien. So that when I play it, will sound like the alien.

I have already achieved that I can change the pitch of the audio after recording the audio but now want to do it while recording the audio.

Here is code for Recording audio with its settings.

NSString *docsDir;  dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = dirPaths[0];  NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];  NSDictionary *recordSettings = [NSDictionary                                 dictionaryWithObjectsAndKeys:                                 [NSNumber numberWithInt:AVAudioQualityMin],                                 AVEncoderAudioQualityKey,                                 [NSNumber numberWithInt:16],                                 AVEncoderBitRateKey,                                 [NSNumber numberWithInt: 2],                                 AVNumberOfChannelsKey,                                 [NSNumber numberWithFloat:44100.0],                                 AVSampleRateKey,                                 nil]; NSError *error = nil;  AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];  _audioRecorder = [[AVAudioRecorder alloc]                   initWithURL:soundFileURL                   settings:recordSettings                   error:&error];  _audioRecorder.delegate = self; _audioRecorder.meteringEnabled = YES;   if (error) {     NSLog(@"error: %@", [error localizedDescription]); } else {     [_audioRecorder prepareToRecord]; } 

1 Answers

Answers 1

Use AVCaptureAudioDataOutput instead of AVAudioRecorder, it allows you to handle audio data while it's being recorded:

_captureAudioDataOutput = [[AVCaptureAudioDataOutput alloc] init]; [_captureAudioDataOutput setAudioSettings:recordSettings]; [_captureAudioDataOutput setSampleBufferDelegate:self queue:_dispatchQueue]; 

You need a AVCaptureSession and self must provide the function captureOutput.

In captureOutput you can use the code you already have to change the audio pitch.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment