首页 > 解决方案 > PortAudio 回调不连续?

问题描述

我在使用 PortAudio 时遇到问题,我不确定我是否不太了解回调是如何工作的,或者我做错了什么。我的假设是回调应该连续“按时”触发,包含当前样本,但是当我打开并启动流时,我似乎只收到了几个回调,然后我再也不会收到回调了。我的代码:

PaStreamParameters inputP, outputP;
inputP.device = DeviceIndex;
inputP.channelCount = CardInfo->maxInputChannels;
inputP.sampleFormat = paFloat32;
inputP.suggestedLatency = CardInfo->defaultLowInputLatency;
inputP.hostApiSpecificStreamInfo = NULL;

outputP.device = DeviceIndex;
outputP.channelCount = CardInfo->maxOutputChannels;
outputP.sampleFormat = paFloat32;
outputP.suggestedLatency = CardInfo->defaultLowOutputLatency;
outputP.hostApiSpecificStreamInfo = NULL;

PaError err = Pa_OpenStream(
    &AudioStream,
    &inputP,
    &outputP,
    SAMPLE_RATE,
    FRAMES_PER_BUFFER,
    paClipOff,
    &CAudio::AudioCallback,
    this
);

err = Pa_StartStream(AudioStream);

我使用了以下常量

#define SAMPLE_RATE 44100
#define FRAMES_PER_BUFFER 64

回调:

int CAudio::AudioCallback(const void* pInputBuffer, void* pOutputBuffer, unsigned long iFramesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void* userData)
{
    CAudio* AudioInterface = (CAudio*)userData;

    const float* buffer = (const float*)pInputBuffer;

    printf("Callback: %d frames per buffer %d t %f\n", AudioInterface->CallbackIndex, iFramesPerBuffer, Pa_GetStreamTime(AudioInterface->AudioStream));
AudioInterface->CallbackIndex++;

    return paContinue;
}

现在,如果我打开并启动流会发生什么,我得到六个回调,以下日志输出

Opening audio stream on 'USB PnP Sound Device: Audio (hw:1,0)'
Opened audio stream, starting it...
Callback: 0 frames per buffer 64 t 21812.485122
Callback: 1 frames per buffer 64 t 21812.497681
Callback: 2 frames per buffer 64 t 21812.514483
Callback: 3 frames per buffer 64 t 21812.525110
Callback: 4 frames per buffer 64 t 21812.626489
Callback: 5 frames per buffer 64 t 21812.635590

我做错了什么,还是我对回调发生了什么的理解错了?

标签: c++audiocallbackportaudio

解决方案


好吧,现在我通过使用阻塞 API 解决了这个问题,抓取流并将其写入另一个 std::thread 中运行的 while 循环中的缓冲区,这有点好。


推荐阅读