Audio Queue is playing too fast when the buffer size is small

僤鯓⒐⒋嵵緔 提交于 2019-12-05 09:10:40

When you allocate your queue for variable bit rate, instead of using XMAQDefaultBufSize, for variable bit rate, you need to calculate the packet size. I pulled a method from this tutorial from this book that shows how it's done.

void DeriveBufferSize (AudioQueueRef audioQueue, AudioStreamBasicDescription ASBDescription, Float64 seconds, UInt32 *outBufferSize)
{
    static const int maxBufferSize = 0x50000; // punting with 50k
    int maxPacketSize = ASBDescription.mBytesPerPacket; 
    if (maxPacketSize == 0) 
    {                           
        UInt32 maxVBRPacketSize = sizeof(maxPacketSize);
        AudioQueueGetProperty(audioQueue, kAudioConverterPropertyMaximumOutputPacketSize, &maxPacketSize, &maxVBRPacketSize);
    }

    Float64 numBytesForTime = ASBDescription.mSampleRate * maxPacketSize * seconds;
    *outBufferSize =  (UInt32)((numBytesForTime < maxBufferSize) ? numBytesForTime : maxBufferSize);
}

You would use it like this.

Float64 bufferDurSeconds = 0.54321;  
AudioStreamBasicDescription myAsbd = self.format; // or something

UInt32 bufferByteSize;   
DeriveBufferSize(recordState.queue, myAsbd, bufferDurSeconds, &bufferByteSize);

AudioQueueAllocateBuffer(queue, bufferByteSize, &buffers[i]);

Using kAudioConverterPropertyMaximumOutputPacketSize, you calculate the smallest buffer size you can safely use for the unpredictable variable bit rate file. If your file is too small, you just need to identify which samples are padding for the codec.

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