[英]Audio Recording Script not working on WSL2 Ubuntu
I have the following code on WSL2 with Ubuntu:我在 WSL2 上有以下代码和 Ubuntu:
import pyaudio,wave
chunk = 1024 # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16 # 16 bits per sample
channels = 2
fs = 44100 # Record at 44100 samples per second
seconds = 3
filename = "output.wav"
p = pyaudio.PyAudio() # Create an interface to PortAudio
print('Recording')
stream = p.open(format=sample_format,
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
frames = [] # Initialize array to store frames
for i in range(0, int(fs / chunk * seconds)):
data = stream.read(chunk)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
print('Finished recording')
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
However, when I run the code, I get bunch of ALSA errors and it seems like WSL2 is not recognizing my microphone:但是,当我运行代码时,出现了一堆 ALSA 错误,而且 WSL2 似乎无法识别我的麦克风:
I am unsure if WSL2 has built in audio support as there aren't many examples of recording audio from a script.我不确定 WSL2 是否内置了音频支持,因为从脚本录制音频的示例并不多。 How can I fix these errors and make WSL2 record audio from my python script?
如何修复这些错误并让 WSL2 从我的 python 脚本中录制音频?
WSL2 doesn't have direct access to most hardware devices, so it's no surprise that mic access is failing. WSL2 无法直接访问大多数硬件设备,因此麦克风访问失败也就不足为奇了。
That said, there are some ways to access certain hardware devices through WSL2.也就是说,有一些方法可以通过 WSL2 访问某些硬件设备。 One of these is through USB/IP, to share a Windows USB device through to WSL2/Linux.
其中之一是通过 USB/IP,通过 WSL2/Linux 共享一个 Windows USB 设备。
I've used USB/IP, but never for this particular purpose, so I can't say for sure that it will work, but I expect it to.我使用过 USB/IP,但从未用于此特定目的,所以我不能肯定地说它会起作用,但我希望它能起作用。 In theory, you will need a USB microphone installed in Windows and to then share that USB device via USB/IP .
理论上,您需要在Windows中安装一个 USB 麦克风,然后通过USB/IP共享该 USB 设备。
You should be able to access the device through PyAudio with that in place.您应该能够通过 PyAudio 访问该设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.