我根本无法弄清楚这个问题。下面代码中的音频播放异常快速且断断续续。 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线程中处理Audio Queue回调。如果阻塞该线程的时间太长(通过花费太多时间进行综合,UI或drawRects等操作),AQ回调将无法及时调用。因此,音频输出将下溢且声音断断续续。

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

10-12 13:49