简体   繁体   English

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

[英]python-sounddevice - Playing back audio from microphone

I can get the audio to play back from my microphone but it's extremely muffled and it honestly sounds like the program is going to crash. 我可以从麦克风上播放音频,但声音非常沉闷,听起来好像程序会崩溃。

I tried using an InputStream but the sound is just horrible when I play it back, any idea what I'm doing wrong? 我尝试使用InputStream播放,但播放时声音太恐怖了,知道我做错了什么吗?

10 is my Microphone while 13 is my output device (speakers) 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()

I have a feeling I need to add it to a queue and play it from the queue? 我有需要将其添加到队列并从队列中播放的感觉吗?

The high-level convenience functions sd.play() , sd.rec() and sd.playrec() simply play and/or record whole NumPy arrays of arbitrary (but fixed) length (as long as they fit into memory). 高级便利函数sd.play()sd.rec()sd.playrec()只是播放和/或记录任意(但固定)长度(只要它们适合内存)的整个NumPy数组。 They are supposed to be simple and convenient, but their use cases are quite limited. 它们应该简单方便,但是用例却非常有限。

If you need more control (eg continuous recording, realtime processing, ...), you can use the lower-level "stream" classes (eg sd.Stream , sd.InputStream , sd.RawInputStream ) either with the "non-blocking" callback interface or with the "blocking" read() and write() methods. 如果需要更多控制(例如,连续记录,实时处理等),则可以将较低级别的“流”类(例如sd.Streamsd.InputStreamsd.RawInputStream )与“非阻塞”结合使用。回调接口或带有“阻塞”的read()write()方法。

The high-level functions internally already use the "stream" classes, therefore you are not supposed to mix them! 内部的高级函数已经使用了“ stream”类,因此您不应该将它们混用! If you use sd.play() in the callback function of a stream, it creates yet another stream within the callback function. 如果在流的回调函数中使用sd.play() ,它将在回调函数中创建另一个流。 This is bound to fail! 这注定会失败!

Long story short, you should use either the high-level or the low-level interface, but not both at the same time. 长话短说,你应该同时使用两种高级别低级别的接口,但不能同时使用。

If you want to play back the microphone input immediately, you should use an sd.Stream (including both input and output) with a callback function, just as it's shown in the documentation and in the example application wire.py . 如果要立即播放麦克风输入,则应使用具有回调函数的sd.Stream (包括输入和输出),就像文档和示例应用程序wire.py中所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM