简体   繁体   English

录音脚本不适用于 WSL2 Ubuntu

[英]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.

相关问题 运行 Python 脚本时出现问题 - 无法在 windows 11 ubuntu wsl2 上运行脚本 - Problems Running Python Script - not being able to run script on windows 11 ubuntu wsl2 在 WSL2 Ubuntu 中使用 Python 的 Kafka 消费者/生产者 - Kafka consumer/producer with Python in WSL2 Ubuntu Jupyter Notebook 无法在 Ubuntu WSL2 中打开 - Jupyter Notebook cannot open in Ubuntu WSL2 在 Ubuntu(WSL1 和 WSL2)中显示 matplotlib 图(和其他 GUI) - Show matplotlib plots (and other GUI) in Ubuntu (WSL1 & WSL2) WSL2 上的 Python Ubuntu 启动。json 参数 - Python on WSL2 Ubuntu launch.json args 在 WSL2 上的 Ubuntu 中为 jupyter 实验室扩展安装 nodejs - Installing nodejs for jupyter lab extensions inside Ubuntu on WSL2 让 Kivy 在 Win10 上的 WSL2 上的 Ubuntu 中工作 - Getting Kivy to work in Ubuntu on WSL2 on Win10 WSL2 和 PySide6 - WSL2 and PySide6 相同的 Keras 脚本在 WSL (Ubuntu) 中有效但在 Conda 中无效? - Same Keras script works in WSL (Ubuntu) but not in Conda? (venv) 标识符未显示在 VS Code Insider 的集成终端(WSL2 Ubuntu)中,但显示在终端预览中 - (venv) identifier not showing in VS Code Insider's integrated terminal (WSL2 Ubuntu), but is showing in Terminal Preview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM