Pa_GetStreamTime 返回 0 次

Pa_GetStreamTime is returning 0 time

我正在使用 PortAudio 读取音频 wav 文件。回调函数工作正常,文件在 Ubunto 中正常播放。问题是当我尝试使用 Pa_GetStreamTime 获取时间时 returns 0,我一直在阅读 portaudio 文档和示例,但我找不到解决问题的方法或至少找不到使用该功能的示例.我浏览了 API 文档 here 但还没有任何提示。如果有人可以提供提示,将不胜感激。下面是回调函数发生的实现部分,我使用 Qt,因为我的最终目标是显示任何 wav 文件的 FTT。提前致谢。

int playAudio::patestCallback(const void *inputBuffer, void *outputBuffer,
                       unsigned long framesPerBuffer,
                       const PaStreamCallbackTimeInfo* timeInfo,
                       PaStreamCallbackFlags statusFlags,
                       void *userData)
{

    /* Cast data passed through stream to our structure. */
    //  data = (WAV*)userData;
    float *out = (float*)outputBuffer;
    (void) inputBuffer; /* Prevent unused variable warning. */

    /*terminates the stream flows and reset cursor.*/
    if (cursor == playAudio::SubChunk2Size / 4)
    {
        cursor = 0;
        return paComplete;
    }

    for (int i = 0; i < framesPerBuffer; i++)
    {
        if (cursor == playAudio::SubChunk2Size / 4) break; // breaks if samples reached last.
        *(out++) = pLeftChannel[cursor];
        *(out++) = pRightChannel[cursor];
        cursor++;

    }
    //  qDebug()<<cursor;
    playAudio::audioTime = Pa_GetStreamTime(stream);
    qDebug() << playAudio::audioTime;
    return paContinue;
}

为 PortAudio 音频回调引用 the documentation

Before we begin, it's important to realize that the callback is a delicate place. This is because some systems perform the callback in a special thread, or interrupt handler, and it is rarely treated the same as the rest of your code. For most modern systems, you won't be able to cause crashes by making disallowed calls in the callback, but if you want your code to produce glitch-free audio, you will have to make sure you avoid function calls that may take an unbounded amount of time to execute. Exactly what these are depend on your platform but almost certainly include the following: memory allocation/deallocation, I/O (including file I/O as well as console I/O, such as printf()), context switching (such as exec() or yield()), mutex operations, or anything else that might rely on the OS. If you think short critical sections are safe please go read about priority inversion. Windows amd Mac OS schedulers have no real-time safe priority inversion prevention. Other platforms require special mutex flags. In addition, it is not safe to call any PortAudio API functions in the callback except as explicitly permitted in the documentation.

强调我的。

换句话说,从回调中调用 Pa_GetStreamTime() 是未定义的行为。但是您不需要首先调用该函数。为什么?因为回调的第四个参数是一个 PaStreamCallbackTimeInfo 结构,它包含与您尝试访问的时间信息完全相同的信息。

此外,qDebug 调用可能会破坏您的音频播放。写入 stdout 需要很长时间才能在实时音频回调中完成。