I am working on an iOS app where audio recording and playback should happen simultaneously but at different sampling rates.
Recording will be done through a connected USB Audio device, and playback is done through the inbuilt speaker. I am using AudioUnits for both recording and playback. AVAudioSession category is set to AVAudioSessionCategoryPlayAndRecord.
Problem is that, recording sample rate should be 96kHz whereas playback sample rate should be 8kHz and, both should run simultaneously.
Currently, whenever I use AVAudioSessionCategoryPlayAndRecord and setPreferredSampleRate to 96kHz, ultimately sampleRate property of AVAudioSession remains at 48kHz and I am loosing half of the samples while recording.
If I use AVAudioSessionCategoryRecord recording happens just fine. But I can't run the audio playback simultaneously with this category. I even tried AVAudioSessionCategoryMultiRoute with no luck, here sampleRate remains at 44.1kHz
So, my question is in iOS how to use different sample rates for recording and playback and, still run them simultaneously? Any advice or references are greatly appreciated.
Please let me know if any other details are required.
I ended up using AVAudioSessionCategoryPlayAndRecord category.
Preferred sample rate is set to 48kHz.
To achieve higher sampling rate for recording, I am using setPreferredInputNumberOfChannels
. By increasing number of input channels(which affects only recording) you can have different sampling rate for recording.
In case the recording sampling rate is not a multiple of playback sample rate, we may need to add some interleaving/padding to input samples(Assuming you have control over how data is formatted by the USB device)