12

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];
}
Vijay S
  • 282
  • 2
  • 7
  • 15
Deep Kohli
  • 128
  • 8

1 Answers1

8

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.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • @ChandniSanghani Comments are not for questions. If you have a new question, post it as new question. – Max Vollmer Nov 12 '21 at 12:29
  • I already added new questions but no one is answering for the same. My new question https://stackoverflow.com/questions/68602131/error-in-processing-audio-file-with-proper-format-and-data – Chandni Sanghani Dec 01 '21 at 07:12