You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu开发AI语音功能遇ALSA无法打开slave错误求助

Ubuntu下SpeechRecognition调用Google Cloud语音识别的ALSA错误解决方案

问题描述

在Ubuntu系统开发AI语音功能时,运行基于SpeechRecognition库调用Google Cloud语音识别服务的Python代码,遭遇一系列ALSA错误,包括unable to open slave、未知PCM设备、无法打开/dev/dsp等。已执行sudo apt-get install alsa-utils但问题未解决,相同代码在Windows系统可正常运行。

代码示例

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.pause_threshold = 0.6
        print("Listening... ")
        audio = r.listen(source)
        try:
            print("Recognizing...")
            query = r.recognize_google_cloud(audio,language="en-US")

            print(f"User said: {query}")
            return query

错误信息

ALSA lib pcm_dsnoop.c:601:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave

解决方案

1. 检查并修复麦克风权限

  • 查看当前用户所属组:groups
  • 如果输出中没有audio组,执行以下命令添加用户到音频组,之后注销重新登录:
    sudo usermod -aG audio $USER
    

2. 手动指定麦克风设备

SpeechRecognition默认使用系统默认音频设备,若默认设备配置异常,可手动指定:

  • 执行以下命令列出所有可用麦克风设备及对应索引:
    python -c "import speech_recognition as sr; print(sr.Microphone.list_microphone_names())"
    
  • 修改代码,将device_index设置为你的麦克风对应的索引(比如0或1):
    def takeCommand():
        r = sr.Recognizer()
        # 替换为你的麦克风实际索引
        with sr.Microphone(device_index=0) as source:
            r.pause_threshold = 0.6
            print("Listening... ")
            audio = r.listen(source)
            try:
                print("Recognizing...")
                query = r.recognize_google_cloud(audio, language="en-US")
                print(f"User said: {query}")
                return query
            # 增加异常捕获,方便排查问题
            except Exception as e:
                print(f"识别错误: {e}")
                return None
    

3. 修复ALSA全局配置

  • 备份原ALSA配置文件:
    sudo cp /usr/share/alsa/alsa.conf /usr/share/alsa/alsa.conf.bak
    
  • 编辑配置文件:
    sudo nano /usr/share/alsa/alsa.conf
    
  • 找到以下默认配置行:
    defaults.pcm.card 0
    defaults.pcm.device 0
    defaults.ctl.card 0
    
  • 执行aplay -l查看系统声卡列表,将上述配置中的0替换为你的实际声卡索引(比如如果主声卡是1,就改成1)
  • 保存退出后重启ALSA服务:
    sudo alsa force-reload
    

4. 安装完整的音频依赖库

除alsa-utils外,还需安装PortAudio及Python绑定:

sudo apt-get install portaudio19-dev python3-pyaudio

之后更新SpeechRecognition库:

pip install --upgrade SpeechRecognition

5. 禁用OSS兼容层(解决/dev/dsp错误)

Ubuntu默认不再使用OSS音频系统,可通过配置禁用OSS兼容:

  • 创建或编辑/etc/asound.conf文件:
    sudo nano /etc/asound.conf
    
  • 添加以下内容(注意替换card 0为你的实际声卡索引):
    pcm.!default {
        type hw
        card 0
    }
    ctl.!default {
        type hw
        card 0
    }
    
  • 保存后重启系统生效。

内容的提问来源于stack exchange,提问作者Anas Faaiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:16:22