Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real time Pitch Change while recording audio with AVAudioRecorder

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];
}
like image 859
Deep Kohli Avatar asked Apr 18 '18 08:04

Deep Kohli


1 Answers

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.

like image 78
Max Vollmer Avatar answered Nov 14 '22 07:11

Max Vollmer