Heuristic:78 Xiaozhi esp32 DTX VBR Optimization
| Knowledge Sources | |
|---|---|
| Domains | Embedded, Audio, Optimization |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Enable Opus DTX (Discontinuous Transmission) and VBR (Variable Bitrate) to reduce uplink bandwidth during silence without degrading speech quality.
Description
The Opus encoder is configured with `enable_dtx = true` and `enable_vbr = true`. DTX stops transmitting during silence periods, sending only occasional comfort-noise frames. VBR dynamically adjusts the bitrate based on speech complexity — more bits for complex speech, fewer for simple sounds. Together, these dramatically reduce the average uplink bandwidth, which is critical for cellular (ML307/NT26) and constrained WiFi connections on ESP32.
The bitrate is set to `ESP_OPUS_BITRATE_AUTO`, letting the encoder choose the optimal rate. FEC (Forward Error Correction) is disabled (`enable_fec = false`) because the transport layer (WebSocket or MQTT/UDP) handles reliability. Complexity is set to 0 (fastest) to minimize CPU usage on the ESP32.
Usage
Use this heuristic whenever configuring the Opus encoder for voice streaming. DTX+VBR is the correct default for all boards — there is no scenario where disabling them improves the experience on ESP32 hardware.
The Insight (Rule of Thumb)
- Action: Set `enable_dtx = true` and `enable_vbr = true` in the Opus encoder config. Set `bitrate = ESP_OPUS_BITRATE_AUTO` and `complexity = 0`.
- Value: Frame duration 60ms. Bitrate auto. Complexity 0. DTX on. VBR on. FEC off.
- Trade-off: DTX may cause slight clipping at the start of speech after silence. VBR means peak bitrate may exceed average — ensure the transport buffer can handle bursts. Complexity 0 sacrifices quality for CPU savings, but on ESP32 the difference is inaudible at 16kHz mono.
- Compatibility: Works on all targets. The ESP-Opus component supports DTX and VBR on all ESP32 variants.
Reasoning
On ESP32 hardware, the uplink bandwidth is the bottleneck — especially on 4G modems (ML307, NT26) where data costs money and latency is high. A 16kHz mono Opus stream at constant bitrate would consume ~24-32 kbps continuously. With DTX, during silence (which is 40-60% of a typical conversation), bandwidth drops to near zero. VBR ensures that the bits spent during speech are used efficiently. Complexity 0 is the fastest setting, critical because the Opus encoder runs in a dedicated task with priority 2 and shares CPU with the audio I/O tasks.
FEC is disabled because retransmission or packet-level recovery is handled by the transport: WebSocket over TCP guarantees delivery, and the MQTT/UDP protocol has its own AES-CTR framing. Adding FEC would waste bandwidth duplicating data the transport already handles.
Code Evidence
Encoder configuration macro from `main/audio/audio_service.h:67-80`:
#define AS_OPUS_ENC_CONFIG() \
{ \
.sample_rate = ESP_AUDIO_SAMPLE_RATE_16K, \
.channel = ESP_AUDIO_MONO, \
.bits_per_sample = ESP_AUDIO_BIT16, \
.bitrate = ESP_OPUS_BITRATE_AUTO, \
.frame_duration = \
(esp_opus_enc_frame_duration_t)AS_OPUS_GET_FRAME_DRU_ENUM(OPUS_FRAME_DURATION_MS), \
.application_mode = ESP_OPUS_ENC_APPLICATION_AUDIO, \
.complexity = 0, \
.enable_fec = false, \
.enable_dtx = true, \
.enable_vbr = true, \
}
Frame duration constant from `main/audio/audio_service.h:40`:
#define OPUS_FRAME_DURATION_MS 60