如何在python中使用sounddevice打开音频输出流?

2024-06-16 12:20:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使我的音频接口连续播放循环中的相同音频。有人建议在sounddevice库中使用“OutputStream”函数。这是我为此编写的代码:

sounddevice.default.device = "Focusrite USB ASIO, ASIO"
sounddevice.default.samplerate = 48000
stream = sounddevice.OutputStream()
stream.start()
stream.write(data)

最后一行代码显示错误:

sounddevice.PortAudioError: Wait timed out [PaErrorCode -9987]

我做错了什么


Tags: 函数代码defaultstreamdevice音频start建议
1条回答
网友
1楼 · 发布于 2024-06-16 12:20:57

我让它工作的方法是使用:

with sounddevice.OutputStream(device="Focusrite USB ASIO, ASIO", channels=8, callback=callback, samplerate=samplesPerSecond)

这会重复调用回调函数。在回调函数中,我设置了输出:

def callback(outdata, frames, time, status):
     for i in range(8):
          channels[i] = audioData
     multiChannel = np.column_stack((channels[0], channels[1], channels[2], channels[3], channels[4], channels[5], channels[6], channels[7]))
     outdata[:] = multiChannel

相关问题 更多 >