c++ - 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
我做错了什么,还是我对回调发生了什么的理解错了?
解决方案
好吧,现在我通过使用阻塞 API 解决了这个问题,抓取流并将其写入另一个 std::thread 中运行的 while 循环中的缓冲区,这有点好。
推荐阅读
- java - 使用 Java 8 从嵌套列表中获取多个映射值
- raku - 我可以一次绑定多个变量吗?
- python - matplotlib show() 在第二次调用中不显示()
- excel - VBA 循环通过可见单元格
- javascript - 使用坐标对象对 2 个数组进行排序,以获得彼此最接近的列表
- python-3.x - 方法 __int__ 的签名与类中基方法的签名不匹配 此检查检测覆盖方法签名中的不一致
- numpy - 意外的输入数据类型。实际:(tensor(double)),预期:(tensor(float))
- c# - 将 Elsa 工作流程与防伪令牌一起使用
- python - 逐行比较二维数组
- javascript - 模板文字的默认“标签”函数是什么?