sounddevice.play()不工作,并且不发送任何错误

2024-06-15 23:58:28 发布

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

我正在尝试录制一些Synth并用sounddevice模块播放它们,它似乎录制得很好,因为print(my_recording)返回一个数组,数组中填充了典型的疯狂音频数字,但函数sd.play()似乎没有任何效果,但没有弹出错误

我正在使用focusrite scarlett 18i8 gen 1音频接口,它似乎连接正确,因为我为sd.default.device提供了正确的ID,并且收到了与它同步的通知,虽然我注意到sd.default.channels变量有些奇怪,但它不接受sd.query_devices()明确规定的可用输出数量(对于我的设备是“14 Focusrite USB ASIO,ASIO(18英寸,8英寸)”。程序运行良好,直到它到达sd.play()(我可以从打印的字符串看出)。以下是当我尝试使用sd.default.channels=(18,8)运行程序时弹出的错误:

sounddevice.PortAudioError: Error opening OutputStream: Invalid number
of channels [PaErrorCode -9998]

例如,当我将它调整为(2,2)时,它不会运行任何错误,但仍然没有真正播放的音频。老实说,我不明白为什么该函数采用“输入/输出通道的数量”。正如文档所述,而不是与乐器/扬声器连接的确切通道。所以我试着用更自然的方式来处理它,通过提供准确的通道ID(根据我的音频接口手册),Ins是元组中的第一个值,Outs是第二个值,但毫不奇怪它也不起作用

这是我的密码:

''' capturing the sound from the synth '''
sd.default.samplerate = 44100
sd.default.device = 14
sd.default.channels = (18, 8)
duration = 10

print('records')
my_recording = sd.rec(int(44100 * duration))
sd.wait()
print('done recording')
time.sleep(3)
print('playing back')
sd.play(my_recording)
sd.wait()

Tags: 函数iddefaultplaymydevice错误数组