python-sounddevice - 从麦克风播放音频

python-sounddevice - Playing back audio from microphone

我可以让我的麦克风播放音频,但声音非常低沉,老实说,这听起来像是程序要崩溃了。

我尝试使用 InputStream,但播放时声音很糟糕,知道我做错了什么吗?

10 是我的麦克风,而 13 是我的输出设备(扬声器)

import sounddevice as sd

device_info = sd.query_devices(10, 'input')
samplerate = int(device_info['default_samplerate'])

sd.default.samplerate = samplerate
sd.default.channels = 2
devices = sd.query_devices()
print(devices)

def callback(indata, frames, time, status):
    #print(indata)
    sd.play(indata, device=13, blocking=True)

with sd.InputStream(device = 10, samplerate=44100, dtype='float32', callback=callback):
    print('#' * 80)
    print('press Return to quit')
    print('#' * 80)
    input()

我觉得我需要将其添加到队列中并从队列中播放?

高级便利函数 sd.play()sd.rec()sd.playrec() 简单地播放 and/or 记录任意(但固定)长度的整个 NumPy 数组(只要它们适合内存)。 它们应该简单方便,但它们的用例非常有限。

如果你需要更多的控制(例如连续记录,实时处理,......),你可以使用较低级别的"stream" 类(例如sd.Streamsd.InputStream, sd.RawInputStream) 使用 "non-blocking" 回调接口或使用 "blocking" read()write() 方法。

高级函数内部已经使用了"stream"类,所以你不应该混用它们! 如果您在流的回调函数中使用 sd.play(),它会在回调函数中创建另一个流。这注定要失败!

长话短说,您应该使用 高级 低级接口,但不能同时使用两者时间。

如果你想立即回放麦克风输入,你应该使用带有回调函数的sd.Stream(包括输入和输出),如图in the documentation and in the example application wire.py