AudioUnit inputCallback with AudioUnitRender -> mismatch between audioBufferList.mBuffers[0].mDataByteSize != inNumberFrames

一个人想着一个人 提交于 2020-03-05 04:11:25

问题


We are using the AudioUnits input callback to process the incoming buffer. The audio unit setup is taken mostly from

https://github.com/robovm/apple-ios-samples/blob/master/aurioTouch/Classes/AudioController.mm

I have added some sanity check in the audio callback. It looks like this

/// The audio input callback
static OSStatus audioInputCallback(void __unused *inRefCon,
                                   AudioUnitRenderActionFlags *ioActionFlags,
                                   const AudioTimeStamp *inTimeStamp,
                                   UInt32  __unused inBusNumber,
                                   UInt32 inNumberFrames,
                                   AudioBufferList __unused *ioData)
{
    OSStatus err = noErr;
    if(!*callbackData.audioChainIsBeingReconstructed)
    {
        // we are calling AudioUnitRender on the input bus of AURemoteIO
        // this will store the audio data captured by the microphone in cd.audioBufferList
        err = AudioUnitRender(callbackData.audioUnit, ioActionFlags, inTimeStamp, kInputBus, inNumberFrames, &callbackData.audioBufferList);

        // check if the sample count is set correctly
        assert(callbackData.audioBufferList.mBuffers[0].mDataByteSize == inNumberFrames * sizeof(float));
        // Assert that we only received one buffer
        assert(callbackData.audioBufferList.mNumberBuffers == 1);

        // Copy buffer
        TPCircularBufferCopyAudioBufferList(callbackData.buffer, &callbackData.audioBufferList, inTimeStamp, kTPCircularBufferCopyAll, NULL);

    }
    return err;
}

Now sometimes the statement assert(callbackData.audioBufferList.mBuffers[0].mDataByteSize == inNumberFrames * sizeof(float)); fails as the buffers are not the same.

Does anybody have an explanation to this phenomena?


回答1:


This is normal behavior on iOS. An iOS Audio Unit callback can change the number of frames provided in the buffer size, to be different from the original buffer size. This can happen when the OS state changes, or when the audio hardware state changes, or when the hardware format available doesn't exactly match your audio format request.

So all Audio Unit callbacks must be written to handle a variable number of inNumberFrames from the requested value, or the previous callback value.



来源:https://stackoverflow.com/questions/57852372/audiounit-inputcallback-with-audiounitrender-mismatch-between-audiobufferlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!