I'm working on a voice recording and playing app. I'm using a kAudioUnitType_Output audio with kAudioUnitSubType_VoiceProcessingIO. Sometimes it works fine but sometimes has a lot of clipping. I think this is because the ambient noise. I don't know if this clipping is happening as side effect of the noise with the AEC or I my audio unit setup is wrong:
Here is my setup functions:
struct CallbackData {
AudioUnit rioUnit;
BOOL* audioChainIsBeingReconstructed;
CallbackData(): rioUnit(NULL), audioChainIsBeingReconstructed(NULL){}
} cd;
static OSStatus performRender (void*inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
OSStatus err = noErr;
if (*cd.audioChainIsBeingReconstructed == NO)
{
err = AudioUnitRender(cd.rioUnit, ioActionFlags, inTimeStamp, 1, inNumberFrames, ioData);
float *inputFrames = (float*)(ioData->mBuffers->mData);
//engine_process_iOS(inputFrames, ioData->mBuffers->mNumberChannels * inNumberFrames);
}
return err;
}
- (void)setupAudioSession
{
try {
// Configure the audio session
AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];
NSError *error = nil;
[sessionInstance setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];
NSTimeInterval bufferDuration = .005;
[sessionInstance setPreferredIOBufferDuration:bufferDuration error:&error];
[sessionInstance setPreferredSampleRate:44100 error:&error];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleInterruption:)
name:AVAudioSessionInterruptionNotification
object:sessionInstance];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRouteChange:)
name:AVAudioSessionRouteChangeNotification
object:sessionInstance];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleMediaServerReset:)
name: AVAudioSessionMediaServicesWereResetNotification
object: sessionInstance];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
}
catch (NSException *e) {
NSLog(@"Error returned from setupAudioSession");
}
catch (...) {
NSLog(@"Unknown error returned from setupAudioSession");
}
return;
}
- (void)setupIOUnit
{
try {
// Create a new instance of AURemoteIO
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO ;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
AudioComponentInstanceNew(comp, &_rioUnit);
UInt32 one = 1;
AudioUnitSetProperty(_rioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &one, sizeof(one));
AudioUnitSetProperty(_rioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &one, sizeof(one));
CAStreamBasicDescription ioFormat = CAStreamBasicDescription(44100, 2, CAStreamBasicDescription::kPCMFormatFloat32, true);
AudioUnitSetProperty(_rioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &ioFormat, sizeof(ioFormat));
AudioUnitSetProperty(_rioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &ioFormat, sizeof(ioFormat));
UInt32 maxFramesPerSlice = 4096;
AudioUnitSetProperty(_rioUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxFramesPerSlice, sizeof(UInt32));
UInt32 propSize = sizeof(UInt32);
AudioUnitGetProperty(_rioUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxFramesPerSlice, &propSize);
cd.rioUnit = _rioUnit;
cd.audioChainIsBeingReconstructed = &_audioChainIsBeingReconstructed;
// Set the render callback on AURemoteIO
AURenderCallbackStruct renderCallback;
renderCallback.inputProc = performRender;
renderCallback.inputProcRefCon = NULL;
AudioUnitSetProperty(_rioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(renderCallback));
// Initialize the AURemoteIO instance
AudioUnitInitialize(_rioUnit);
//if (err) NSLog(@"couldn't start AURemoteIO: %d", (int)err);
}
catch (NSException *e) {
NSLog(@"Error returned from setupIOUnit");
}
catch (...) {
NSLog(@"Unknown error returned from setupIOUnit");
}
return;
}
What could be the reason of this clipping?