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

Ubuntu下PyAudio调用USB外置麦克风失败问题求助

Fixing "no default input device detected" with USB Microphone in PyAudio/SpeechRecognition (Ubuntu 16.04)

Hey there! Let's get your speech recognition code working with that USB microphone. Since it works fine in Audacity, the issue is just that your Python script isn't targeting the right audio device by default. Here's how to fix it:

Step 1: Identify Your USB Microphone's Device Index

First, we need to find exactly which index your USB mic is assigned to. Run this quick script to list all available audio devices:

import speech_recognition as sr
# Print all available microphone names and their indices
mic_list = sr.Microphone.list_microphone_names()
for index, name in enumerate(mic_list):
    print(f"Device Index: {index} - Name: {name}")

Alternatively, if you want more detailed info using PyAudio directly:

import pyaudio
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
    device_info = p.get_device_info_by_index(i)
    # Only show devices that support input
    if device_info["maxInputChannels"] > 0:
        print(f"Device {i}: {device_info['name']}")
p.terminate()

Look for the entry that matches your USB microphone's name (it usually has "USB" in it somewhere). Note down its index number (e.g., 1, 2, etc.).

Step 2: Modify Your Code to Use the USB Device

Now update your original script to explicitly use the USB microphone's index. Replace X with the index you found in Step 1:

import speech_recognition as sr
import pyaudio

r = sr.Recognizer()
# Explicitly specify your USB microphone's device index
with sr.Microphone(device_index=X) as source:
    print("Say something!")
    # Optional: Adjust for ambient noise to improve recognition
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)

try:
    print("You said: " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
    print("Could not understand audio")
except sr.RequestError as e:
    print(f"Error; {e}")

Why This Works

The sr.Microphone() call defaults to using the system's default input device, but sometimes PyAudio doesn't pick up the USB mic as the default even if it's set in pavucontrol. By specifying the device_index, you force the script to use the exact device you want.

Quick Troubleshooting Check

If you still run into issues, double-check that:

  • Your USB microphone is selected as the active Input Device in pavucontrol (you mentioned it works in Audacity, but it's worth confirming again)
  • You're using the correct index from the device list (indices can shift if you unplug/replug devices)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:06:40