简体   繁体   English

为什么我的iOS录音回调中的音频缓冲区未满?

[英]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. 唯一奇怪的是,有时会使用未完全填充的缓冲区来调用Callback函数。 I have logged the value of inNumPackets variable in my StreamCallback function, and a sequence could look like this: 我已经在StreamCallback函数中记录了inNumPackets变量的值,并且序列看起来像这样:

2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 1568 , 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2048, 2010 , 2048, 2048, 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. 您不能使用依赖于固定大小的iOS Core Audio缓冲区的处理。 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.) 这是因为由于应用程序无法控制的原因,iOS可以自由更改音频单元缓冲区的大小(例如,由于设备上运行的其他音频进程需要采样率重采样或由于设备的硬件功能,或者为了节省各种电量)模式等)

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. 如果您需要处理固定大小的缓冲区,请对输入的音频单元样本使用无锁循环FIFO /缓冲区,并轮询该FIFO以查看其是否包含足够的样本用于您的处理步骤。 See this answer for more details. 有关更多详细信息,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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