Implementation:78 Xiaozhi esp32 Opus Encoder
| Knowledge Sources | |
|---|---|
| Domains | Audio_Coding, Compression |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for encoding PCM audio to Opus format provided by the ESP-Opus component, wrapped by the XiaoZhi firmware's AudioService for real-time voice capture at 16kHz mono with VBR and DTX enabled.
Description
The Opus encoder is an external library API (ESP-Opus, part of ESP-IDF's audio components) used by XiaoZhi's AudioService::OpusCodecTask() to compress captured PCM audio into Opus packets for network transmission. This is a Wrapper Doc — it documents how the repo uses the external ESP-Opus API.
The encoder is configured via the AS_OPUS_ENC_CONFIG() macro:
- Sample rate: 16kHz (standard for voice)
- Channels: Mono
- Bit depth: 16-bit
- Bitrate: Auto (codec selects optimal)
- Frame duration: 60ms (
OPUS_FRAME_DURATION_MS) - Application mode: Audio (full-band)
- Complexity: 0 (fastest, critical for real-time on ESP32)
- FEC: Disabled
- DTX: Enabled (Discontinuous Transmission — no packets during silence)
- VBR: Enabled (Variable Bitrate)
The encoder is created once during AudioService::Initialize() via esp_opus_enc_open(). The OpusCodecTask reads AudioTask items from the encode queue, calls esp_opus_enc_process() to produce Opus packets, wraps them in AudioStreamPacket, and pushes to the send queue.
Usage
Reference this implementation when:
- Understanding Opus encoder configuration for voice assistants
- Tracing the encode path in the audio pipeline
- Configuring frame duration, bitrate, or complexity
- Debugging audio quality or bandwidth issues
Code Reference
Source Location
- Repository: XiaoZhi ESP32
- Files:
main/audio/audio_service.h(config macros),main/audio/audio_service.cc(encoder init and encode loop)
Signature
// ESP-Opus external API (from esp_opus_enc.h)
esp_err_t esp_opus_enc_open(esp_opus_enc_config_t* cfg, int cfg_size, void** handle);
esp_err_t esp_opus_enc_process(void* handle,
esp_audio_enc_in_frame_t* in,
esp_audio_enc_out_frame_t* out);
esp_err_t esp_opus_enc_get_frame_size(void* handle, int* frame_size, int* out_buf_size);
void esp_opus_enc_close(void* handle);
// XiaoZhi configuration macro
#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(60), \
.application_mode = ESP_OPUS_ENC_APPLICATION_AUDIO, \
.complexity = 0, \
.enable_fec = false, \
.enable_dtx = true, \
.enable_vbr = true, \
}
Import
#include "esp_opus_enc.h"
#include "esp_audio_enc.h"
// Used internally by audio_service.h
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cfg | esp_opus_enc_config_t* | Encoder configuration (sample rate, bitrate, frame duration, etc.) | |
| in (process) | esp_audio_enc_in_frame_t* | Input PCM frame: {buffer, size} = encoder_frame_size_ samples × 2 bytes | |
| pcm (AudioTask) | std::vector<int16_t> | 16kHz mono PCM from the encode queue |
Outputs
| Name | Type | Description |
|---|---|---|
| handle | void** | Encoder handle for subsequent process calls |
| out (process) | esp_audio_enc_out_frame_t* | Output Opus frame: {buffer, size, consumed} |
| AudioStreamPacket | Encoded packet pushed to send queue with sample_rate=16000, frame_duration=60 |
Usage Examples
Encoder Initialization
// In AudioService::Initialize()
esp_opus_enc_config_t enc_cfg = AS_OPUS_ENC_CONFIG();
esp_opus_enc_open(&enc_cfg, sizeof(enc_cfg), &opus_encoder_);
esp_opus_enc_get_frame_size(opus_encoder_, &encoder_frame_size_, &encoder_outbuf_size_);
// encoder_frame_size_ = 16000 * 60 / 1000 = 960 samples
Encoding Loop
// In AudioService::OpusCodecTask() — encode section
while (true) {
// Pop AudioTask from encode queue
auto task = audio_encode_queue_.Pop(); // blocks until available
// Prepare input frame
esp_audio_enc_in_frame_t in_frame = {
.buffer = task.pcm.data(),
.size = (int)task.pcm.size() * sizeof(int16_t)
};
// Prepare output buffer
std::vector<uint8_t> out_buf(encoder_outbuf_size_);
esp_audio_enc_out_frame_t out_frame = {
.buffer = out_buf.data(),
.size = (int)out_buf.size()
};
// Encode
esp_opus_enc_process(opus_encoder_, &in_frame, &out_frame);
// Create AudioStreamPacket and push to send queue
auto packet = std::make_unique<AudioStreamPacket>();
packet->sample_rate = 16000;
packet->frame_duration = 60;
packet->timestamp = task.timestamp;
packet->payload.assign(out_buf.data(), out_buf.data() + out_frame.encoded_size);
audio_send_queue_.Push(std::move(packet));
}