Principle:78 Xiaozhi esp32 Audio Resampling
| Knowledge Sources | |
|---|---|
| Domains | Signal_Processing, Audio_Processing |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
The process of converting a digital audio signal from one sampling rate to another using interpolation or decimation, necessary when the audio source (microphone, server, speaker) operates at a different native sample rate than the processing or output stage requires.
Description
Audio resampling bridges sample rate mismatches between components in the audio pipeline:
- Input path: The microphone codec may capture at 16kHz, 48kHz, or 96kHz. The audio engine (WakeNet, VAD, AEC) and Opus encoder require 16kHz. An input resampler converts from the codec's native rate to 16kHz.
- Output path: The Opus decoder outputs at the server's sample rate (typically 24kHz). The speaker codec may operate at 16kHz, 32kHz, or 48kHz. An output resampler converts from the decoded rate to the speaker's native rate.
The XiaoZhi firmware uses the ESP-AE rate conversion component (esp_ae_rate_cvt), configured for 16-bit mono PCM with complexity 2 and speed-optimized performance type.
Resamplers are created once during initialization (input) or when the decode sample rate changes (output), and are protected by mutexes for thread safety. The SetDecodeSampleRate() method recreates the output resampler when the server-sent audio format changes.
Usage
Use this principle when:
- The audio capture codec's native sample rate differs from the processing engine's required rate
- The server sends decoded audio at a different rate than the speaker codec's native rate
- Sample rate may change dynamically (server switches from 16kHz to 24kHz)
Theoretical Basis
Sample rate conversion involves interpolation (upsampling) or decimation (downsampling):
Upsampling (e.g., 16kHz → 48kHz):
1. Insert L-1 zeros between each sample (L = 3)
2. Low-pass filter to remove mirror images
3. Output at 3× the input rate
Downsampling (e.g., 48kHz → 16kHz):
1. Low-pass filter to prevent aliasing (cutoff = fs/2M)
2. Keep every M-th sample (M = 3)
3. Output at 1/3 the input rate
The ESP-AE rate converter handles both cases:
esp_ae_rate_cvt_cfg_t cfg = {
.src_rate = 16000, // input rate
.dest_rate = 48000, // output rate
.channel = ESP_AUDIO_MONO,
.bits_per_sample = ESP_AUDIO_BIT16,
.complexity = 2,
.perf_type = ESP_AE_RATE_CVT_PERF_TYPE_SPEED
};
esp_ae_rate_cvt_open(&cfg, &handle);
esp_ae_rate_cvt_process(handle, in, in_num, out, &out_num);
Practical Guide
When implementing audio resampling:
- Create resamplers once: input resampler at
Initialize(), output resampler when decode rate changes. - Protect with mutex:
input_resampler_mutex_guards the input resampler; the output resampler is only accessed byOpusCodecTask. - Check max output size: call
esp_ae_rate_cvt_get_max_out_sample_num()to size the output buffer. - Recreate on rate change: close and reopen the resampler when the source/dest rate changes.
- Only create when rates differ: if input is already 16kHz, skip the input resampler.