我在 WSL 上,当尝试使用声音设备录制音频时,出现错误,提示未找到设备:
sounddevice.PortAudioError: Error querying device -1
。
我按照 sounddevice 的说明安装了 libportaudio2、CFFI 和 numpy。
我也按照PortAudio的指示编译了PortAudio。
由于我的设备是 USB 麦克风,我还按照 this 将设备连接到 WSL,显然成功,因为当我在 Windows Powershell 中运行
usbipd wsl list
时,我得到:
BUSID VID:PID DEVICE STATE
1-2 239d:f218 USB Input Device Not attached
1-3 216e:2462 Camera Not attached
1-6 046d:0a38 Logi USB Headset H340, USB Input Device Attached - WSL
1-10 12s1:8543 Bluetooth Adapter Not attached
当我在 WSL bash 中运行
sudo usbip port
时,我得到:
Port 00: <Port in Use> at Full Speed(12Mbps)
Logitech, Inc. : Headset H340 (046d:0a38)
1-1 -> usbip://172.25.96.1:3240/1-6
-> remote bus/dev 001/006
但是,当我的 python 脚本调用
sounddevice.rec
时,我得到以下信息:
Traceback (most recent call last):
File "/home/user/project/scripts/recorder.py", line 223, in <module>
main()
File "/home/user/project/scripts/recorder.py", line 203, in main
recording = record(args.sample_rate, args.duration)
File "/home/user/project/scripts/recorder.py", line 54, in record
recording = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=2)
File "/home/user/project/venv/lib/python3.9/site-packages/sounddevice.py", line 276, in rec
ctx.start_stream(InputStream, samplerate, ctx.input_channels,
File "/home/user/project/venv/lib/python3.9/site-packages/sounddevice.py", line 2582, in start_stream
self.stream = StreamClass(samplerate=samplerate,
File "/home/user/project/venv/lib/python3.9/site-packages/sounddevice.py", line 1421, in __init__
_StreamBase.__init__(self, kind='input', wrap_callback='array',
File "/home/user/project/venv/lib/python3.9/site-packages/sounddevice.py", line 817, in __init__
_get_stream_parameters(kind, device, channels, dtype, latency,
File "/home/user/project/venv/lib/python3.9/site-packages/sounddevice.py", line 2660, in _get_stream_parameters
info = query_devices(device)
File "/home/user/project/venv/lib/python3.9/site-packages/sounddevice.py", line 569, in query_devices
raise PortAudioError(f'Error querying device {device}')
sounddevice.PortAudioError: Error querying device -1
此外,在终端中运行
python3 -m sounddevice
或在控制台中运行 sd.query_devices()
不会返回任何内容。
我在这里缺少什么?有办法让它发挥作用吗?
我使用底部的代码。
如果您遇到
pyaudio
安装问题,请尝试修复它 错误:无法为 pyaudio 构建轮子,这是安装基于 pyproject.toml 的项目所必需的。
def record_audio_py() -> str:
chunk = 1024 # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16 # 16 bits per sample
channels = 1
fs = 44100 # Record at 44100 samples per second
seconds = 5
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
# Store data in chunks for 5 seconds
for i in range(0, int(fs / chunk * seconds)):
data = stream.read(chunk)
frames.append(data)
# Stop and close the stream
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()
print('Finished recording')
# Save the recorded data as a WAV file
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()
return filename
并播放文件:
def play_file(filename):
# Set chunk size of 1024 samples per data frame
chunk = 1024
# Open the sound file
wf = wave.open(filename, 'rb')
# Create an interface to PortAudio
p = pyaudio.PyAudio()
# Open a .Stream object to write the WAV file to
# 'output = True' indicates that the sound will be played rather than recorded
stream = p.open(format = p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# Read data in chunks
data = wf.readframes(chunk)
# Play the sound by writing the audio data to the stream
while data != '':
stream.write(data)
data = wf.readframes(chunk)
# Close and terminate the stream
stream.close()
p.terminate()