Ubuntu 24.04 LTS下基于LiveKit构建语音AI代理时遭遇sounddevice.PortAudioError(无效采样率PaErrorCode -9997)问题求助
That PortAudioError: Invalid sample rate (-9997) is super common when LiveKit's default audio settings don't play nice with your system's audio hardware. Let's walk through the fixes step by step:
1. First, find out what sample rates your audio device supports
Open up a terminal and run these commands to get your device details:
- List all audio input devices:
You'll see output likearecord -lcard 0: XXXX [Device Name], device 0: XXXX [XXX]— note the card and device numbers (e.g.,0,0). - Check supported sample rates for that device:
Look for lines starting witharecord --dump-hw-params -D plughw:<card>,<device>Sample rates:— this will tell you which rates your hardware accepts (common ones are 16000, 44100, 48000).
2. Force LiveKit to use a compatible sample rate
Modify your RoomInputOptions in the code to explicitly set a sample rate that your device supports. For example, if your device works with 48000:
room_input_options=RoomInputOptions( noise_cancellation=noise_cancellation.BVC(), sample_rate=48000, # Replace with your device's supported rate )
3. Reinstall PortAudio dependencies (if the above doesn't work)
Sometimes Ubuntu's PortAudio packages can get messed up. Reinstall them with:
sudo apt update && sudo apt install --reinstall libportaudio2 portaudio19-dev
Then restart your virtual environment and try running the agent again.
4. For headless/server environments (no physical audio device)
If you're running this on a server without a microphone, you'll need a virtual audio device. Install PulseAudio and set up a null sink:
sudo apt install pulseaudio pulseaudio-utils pactl load-module module-null-sink sink_name=VirtualSink pactl set-default-source VirtualSink.monitor
This creates a dummy audio input that LiveKit can use without issues.
Once you've tried these steps, re-run uv run agent.py — it should connect without the sample rate error.
内容的提问来源于stack exchange,提问作者sh1nata

