使用 pyaudio 在 linux/ubuntu 上运行烧瓶应用程序时出现 ALSA 错误
Posted
技术标签:
【中文标题】使用 pyaudio 在 linux/ubuntu 上运行烧瓶应用程序时出现 ALSA 错误【英文标题】:ALSA Error running a flask application on linux/ubuntu using pyaudio 【发布时间】:2020-09-14 08:00:53 【问题描述】:该应用程序在 Windows 10 上按预期工作,但在 linux 中崩溃。我正在尝试使用 pyaudio(Python 3)在烧瓶应用程序中通过麦克风录制音频。我正在 Ubuntu 20.04 中尝试它。 错误如下:
ALSA lib pcm_dsnoop.c:641:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_dsnoop.c:641:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
这是我用来录制音频的代码,逻辑没有错,所有变量都正确分配,这只是代码的sn-p,所以有些变量可能看起来很模糊。
p = pyaudio.PyAudio()
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)):
if( fee =="T"):
data = stream.read(chunk)
frames.append(data)
else:
break
# 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()
【问题讨论】:
【参考方案1】:默认麦克风设备可能不是您认为的那样。为了找到正确的麦克风设备(如果有的话),您可以使用:
def get_device_index(p):
device_index = None
for i in range(p.get_device_count()):
devinfo = p.get_device_info_by_index(i)
for keyword in ["mic","input"]:
if keyword in devinfo["name"].lower():
print( "Found an input: device %d - %s"%(i, devinfo["name"]) )
device_index = i
return device_index
if device_index is None:
print( "No preferred input found; using default input device." )
return device_index
那么为了使用这个设备索引:
device_index = get_device_index(p)
stream = p.open(format = sample_format,
channels = channels,
rate = fs,
input = True,
input_device_index = device_index,
frames_per_buffer = chunk)
取自here。
【讨论】:
这并没有解决它,使用您的建议时会弹出相同的问题。以上是关于使用 pyaudio 在 linux/ubuntu 上运行烧瓶应用程序时出现 ALSA 错误的主要内容,如果未能解决你的问题,请参考以下文章