iOS, Audio Queue: Buffer size is not contant
I use Audio Queue Services in my application. When allocating the buffer, I set t开发者_运维百科he buffer size to 30000 samples:
AudioQueueAllocateBuffer(mQueue, 30000, &mBuffers[i]);
But the subsequent calls of the callback are made with the following inNumberPacketDescriptions:
30000
30000
30000
26928
30000
30000
They aren't always equal to 30000. Why?
Record format configuration (using CAStreamBasicDescription):
mRecordFormat.mSampleRate = kSampleRate;
mRecordFormat.mChannelsPerFrame = 1;
mRecordFormat.mFormatID = kAudioFormatLinearPCM;
mRecordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
mRecordFormat.mBitsPerChannel = 16;
mRecordFormat.mBytesPerPacket = mRecordFormat.mBytesPerFrame = (mRecordFormat.mBitsPerChannel / 8) * mRecordFormat.mChannelsPerFrame;
mRecordFormat.mFramesPerPacket = 1;
3 buffers are used.
Edit: I've seen iOS freak out and spontaneously change buffer sizes when presented with a non-power-of-two audio buffer. (Another SO question references this) Anyway,
30000 is
(a) a HUGE buffer size, and
(b) a weird number to use for a buffer. Usually they're in powers of 2— i.e. *=2
from 64, i.e. 64, 128, 256, 512, 1024, 2048, 4096. I've never seen one higher than 4096, and I do a lot of audio work.
If you have a specialized reason to use unusually-large buffers, you could use a nextPowerOfTwo
convenience function or just hard-code the math yourself.
精彩评论