gpt4 book ai didi

CoreAudio AudioQueue PCM 快速且不稳定

转载 作者:行者123 更新时间:2023-11-30 15:13:51 25 4
gpt4 key购买 nike

我根本无法弄清楚这个问题。下面代码中的音频播放速度非常快且断断续续。 SO 上还有一个类似的问题,但它是针对可变比特率的,不适用。

其他站点上的一些可能的答案(直接设置每个数据包的帧数)不再起作用并给出 aq -50 错误代码。

这来自合成器代码库,源音频为 44,100 Hz 的 8:24 线性 PCM,非交错。将帧 (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;
}

最佳答案

您可能正在应用的主 UI 线程中处理音频队列回调。如果您阻塞此线程太长时间(通过花费太多时间进行合成、UI 或绘制矩形等),AQ 回调将无法及时调用。因此,音频输出将会下溢并且听起来断断续续。

关于CoreAudio AudioQueue PCM 快速且不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com