Principle:78 Xiaozhi esp32 Opus Audio Codec
| Knowledge Sources | |
|---|---|
| Domains | Audio_Coding, domain:Compression |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
A low-latency, variable-bitrate audio codec designed for real-time voice communication over constrained network links, combining the SILK and CELT codecs to deliver intelligible speech at bitrates as low as 8 kbps with frame durations from 2.5 to 120 ms.
Description
Opus is the audio codec used by XiaoZhi for all voice communication between the device and server. It is selected for its:
- Low latency: Frame durations of 60ms (XiaoZhi default) enable near-real-time conversation.
- Variable bitrate (VBR): Silence and quiet segments use fewer bits;
enable_dtx=trueallows Discontinuous Transmission (no packets during silence). - Bandwidth efficiency: At 16kHz mono with auto bitrate, typical speech consumes 24–32 kbps.
- Quality at low bitrate: Opus outperforms legacy codecs (G.711, AMR) at equivalent bitrates.
The XiaoZhi firmware uses the ESP-Opus component (part of ESP-IDF) for both encoding and decoding. The encoder is configured at 16kHz mono with 60ms frames, auto bitrate, complexity 0 (fastest), and VBR+DTX enabled. The decoder handles server-sent audio at variable sample rates (typically 24kHz) with on-the-fly reconfiguration when the sample rate or frame duration changes.
Usage
Use this principle when:
- Real-time voice communication over WiFi or cellular networks is required
- Bandwidth is constrained (cellular modem links)
- Low-latency bidirectional audio is needed (voice assistant conversation)
- Silence suppression (DTX) saves bandwidth during pauses
Theoretical Basis
Opus uses two core coding modes:
SILK (6–40 kbps): Optimized for voice at narrowband/medium-band
CELT (48–128 kbps): Optimized for full-band audio at low latency
Opus combines both: Uses SILK for voice-band, CELT for full-band
Switches automatically based on bandwidth & bitrate
The encoder configuration in XiaoZhi:
sample_rate = 16000 Hz
channel = mono
bitrate = AUTO (codec selects optimal)
frame_duration = 60 ms
complexity = 0 (fastest encoding)
enable_dtx = true (Discontinuous Transmission)
enable_vbr = true (Variable Bitrate)
The decoder is stateless across frames (each frame is self-delimited) and can handle sample rate changes without restart, though XiaoZhi recreates the decoder handle when sample_rate or frame_duration changes for safety.
Practical Guide
When configuring Opus for embedded voice:
- Use 16kHz mono for capture: this is the standard for voice assistants and matches WakeNet/VAD input requirements.
- Enable DTX+VBR: reduces bandwidth during silence without quality loss for active speech.
- Set complexity to 0: fastest encoding, critical for real-time on ESP32.
- Handle sample rate changes: the server may send audio at a different sample rate (e.g., 24kHz); recreate the decoder when it changes.
- Resample to codec native rate: use
esp_ae_rate_cvtto match decoded audio to the speaker's native sample rate.