简体   繁体   中英

Why is the audiobuffer not full in my iOS audio recording callback?

The audio recording works as it should: I can record and playback the recording. Only strange thing is that the Callback function is sometimes called with buffers that are not filled completely. I have logged the value of inNumPackets variable in my StreamCallback function, and a sequence could look like this:

2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 1568 , 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2010 , 2048, 2048,

The code processing the buffers currently relies on a fixed size, which means I get artifacts every time a buffer is not filled.

Can I do something to always get full buffers? - or is it expected behavior?

A few extracts from my code:

DataFormat.mFormatID := kAudioFormatLinearPCM;
DataFormat.mSampleRate := 44100;
DataFormat.mChannelsPerFrame := 1;
DataFormat.mBitsPerChannel := 16;
DataFormat.mBytesPerPacket := 2;
DataFormat.mBytesPerFrame := 2;
DataFormat.mFramesPerPacket := 1;
DataFormat.mFormatFlags := kLinearPCMFormatFlagIsSignedInteger OR kLinearPCMFormatFlagIsPacked;

lStatus := AudioQueueNewInput(@DataFormat,@StreamCallback,nil,nil,kCFRunLoopCommonModes,0,fAqDataQueue);
...
//allocate 3 buffers with a buffer size of 2048 packets with 2 bytes per packet
AudioQueueAllocateBuffer(fAqDataQueue,2*2048,fAqDataBuffers[li]);

You can not use processing that depends on iOS Core Audio buffers of a fixed size. This is because iOS is free to change the audio unit buffer size due to reasons outside of your app's control (such as for sample rate resampling required by other audio processes running on the device or due to the device's hardware capabilities, or for various power saving modes, etc.)

If you need to process buffers of a fixed size, use a lock-free circular FIFO/buffer for incoming audio unit samples, and poll that FIFO to see if it contains enough samples for your processing step. See this answer for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM