我根本无法弄清楚这个问题。下面代码中的音频播放速度非常快且不连贯。 SO上的另一个问题是类似的,但它是针对可变比特率而不适用的。
其他网站上的一些可能答案(直接设置每个数据包的帧数)不再有效,并提供aq -50错误代码。
这是来自合成器代码库,源音频是8:24线性PCM,4400 Hz,非交错。将帧(4096)设置为采样率(44100Hz)似乎可以修复波动,但是音频生成后端无法跟上,因此它会使每个数据包停顿。
任何解决方案?对于我的生活,我无法弄清楚是什么导致它如此快速地运行。
static const int kNumBuffers = 3;
typedef struct CoreAudio_audiodriver
{
A2_audiodriver ad;
AudioQueueRef queue;
AudioQueueBufferRef buffer[kNumBuffers];
AudioStreamBasicDescription desc;
} CoreAudio_audiodriver;
/* CoreAudio render thread callback */
static void coreaudio_process(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
{
A2_audiodriver *driver = (A2_audiodriver *)inUserData;
CoreAudio_audiodriver * cad = (CoreAudio_audiodriver *)inUserData;
A2_config * config = driver->driver.config;
int c, i;
int frames = 4096 /* samples */;
if( driver->Process )
{
driver->Process(driver, 4096 /* samples */); // loads 4096 samples into internal buffers
}
/* copy and interleave internal buffers to queue */
for ( i = 0; i < frames; i++ )
{
for ( c = 0; c < 2 /* channels */; c++ )
{
((int32_t *)inBuffer->mAudioData)[i + c] = driver->buffers[c][i];
}
}
inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
AudioQueueEnqueueBuffer(cad->queue, inBuffer, 0, NULL);
}
static int coreaudiod_Open(A2_driver *driver)
{
CoreAudio_audiodriver * drv = (CoreAudio_audiodriver *)driver;
A2_config * config = drv->ad.driver.config;
AudioStreamBasicDescription * desc = &drv->desc;
OSStatus err;
int c, i;
/* set up stream description */
desc->mSampleRate = (Float64)config->samplerate;
desc->mFormatID = kAudioFormatLinearPCM;
desc->mFormatFlags = kAudioFormatFlagIsSignedInteger;
desc->mFramesPerPacket = 1;
desc->mChannelsPerFrame = 2 /* channels */;
/* packet -> frame -> channel -> data */
desc->mBytesPerFrame = desc->mChannelsPerFrame * sizeof(int32_t);
desc->mBytesPerPacket = desc->mBytesPerFrame * desc->mFramesPerPacket;
desc->mBitsPerChannel = 24; /* 8:24 PCM */
/* set up queue */
err = AudioQueueNewOutput(
desc, // data format
coreaudio_process, // callback
driver, // data passed to callback
NULL, // internal run loop
kCFRunLoopCommonModes, // kCFRunLoopCommonMode
0, // reserved by Apple
&drv->queue // queue output
);
for ( i = 0; i < kNumBuffers; i++ )
{
/* internal buffer */
err = AudioQueueAllocateBuffer(
drv->queue,
desc->mBytesPerPacket * 4096 /* samples */,
&drv->buffer[i]
);
/* start callback polling */
drv->buffer[i]->mAudioDataByteSize = drv->buffer[i]->mAudioDataBytesCapacity;
err = AudioQueueEnqueueBuffer(drv->queue, drv->buffer[i], 0, NULL);
}
err = AudioQueueStart(drv->queue, NULL);
return 1;
}
答案 0 :(得分:1)
您可能正在应用程序的主UI线程中处理音频队列回调。如果你阻止这个线程太长时间(通过花费太多做综合,UI或drawRects等),AQ回调将不会被及时调用。因此音频输出将下溢并且声音不稳定。