ios - play - Real time Pitch Change while recording audio with AVAudioRecorder



record audio in swift 4 (1)

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];
}

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.





objective-c