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

How do I open an audio output stream using sounddevice in 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]

我做错了什么?

我的工作方式是使用:

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