Heuristic:78 Xiaozhi esp32 Resampler Lifecycle Management
| Knowledge Sources | |
|---|---|
| Domains | Embedded, Audio, Optimization |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Input and output resamplers are created once at initialization and only recreated when the decoder's sample rate changes, avoiding per-frame allocation overhead.
Description
The firmware uses two ESP-AE rate converters:
- Input resampler: Created once during `AudioService::Initialize()` if the codec's input sample rate differs from 16kHz (the Opus encoder's fixed rate). Never recreated — the codec input rate is fixed by hardware. Protected by `input_resampler_mutex_` because it is accessed from the audio input task.
- Output resampler: Created during initialization if the codec's output sample rate differs from the decoder's initial sample rate. Recreated whenever `SetDecodeSampleRate()` detects a rate change (e.g., server switches from 24kHz to 48kHz audio). The old resampler is closed before the new one is opened to avoid resource leaks.
Both resamplers are closed in the destructor. The input resampler is reset (not recreated) when wake word detection or voice processing is toggled, to clear any internal state.
Usage
Use this heuristic when managing rate converter handles. Create the input resampler once and never recreate it. For the output resampler, detect rate changes and recreate only when necessary — always close the old handle first. Reset (not recreate) when audio modes change.
The Insight (Rule of Thumb)
- Action: Input resampler: create once in `Initialize()`, reset on mode changes, close in destructor. Output resampler: create in `Initialize()`, recreate in `SetDecodeSampleRate()` when rate changes, close in destructor.
- Value: `RATE_CVT_CFG` uses `complexity = 2` and `ESP_AE_RATE_CVT_PERF_TYPE_SPEED` for the performance/speed trade-off.
- Trade-off: Recreating the output resampler on rate change has a one-time cost but avoids resampling artifacts from using the wrong ratio. Resetting the input resampler (vs. recreating) is cheaper because it only clears internal state without reallocating buffers.
- Compatibility: ESP-AE rate conversion is available on all ESP-IDF targets. The handles are lightweight C structs managed by the `esp_ae_rate_cvt_*` API.
Reasoning
The Opus encoder runs at a fixed 16kHz, but hardware codecs have various input rates (8kHz, 16kHz, 48kHz). The input resampler bridges this gap and its ratio never changes — the hardware sample rate is fixed. Recreating it would waste CPU and memory.
The decoder, however, can change its sample rate when the server sends a `hello` message with a different `sample_rate` field. The output resampler must match the new decoder rate to the codec's output rate. If the ratio changes, the old resampler's internal state (filter coefficients, buffer) is invalid and must be recreated. The `SetDecodeSampleRate()` method first closes the old handle, then opens a new one — this is the only place where output resampler recreation happens.
The `esp_ae_rate_cvt_reset()` call on the input resampler when toggling wake word/voice processing clears the filter state (residual samples from a previous processing mode) without the cost of deallocation and reallocation.
Code Evidence
Input resampler creation from `main/audio/audio_service.cc:73-79`:
if (codec->input_sample_rate() != 16000) {
esp_ae_rate_cvt_cfg_t input_resampler_cfg = RATE_CVT_CFG(
codec->input_sample_rate(), ESP_AUDIO_SAMPLE_RATE_16K, codec->input_channels());
auto resampler_ret = esp_ae_rate_cvt_open(&input_resampler_cfg, &input_resampler_);
if (input_resampler_ == nullptr) {
ESP_LOGE(TAG, "Failed to create input resampler, error code: %d", resampler_ret);
}
}
Output resampler recreation on rate change from `main/audio/audio_service.cc:532-566`:
void AudioService::SetDecodeSampleRate(int sample_rate, int frame_duration) {
if (decoder_sample_rate_ == sample_rate && decoder_duration_ms_ == frame_duration) {
return; // No change needed
}
// ... close and reopen decoder ...
if (decoder_sample_rate_ != codec_->output_sample_rate()) {
if (output_resampler_ != nullptr) {
esp_ae_rate_cvt_close(output_resampler_);
output_resampler_ = nullptr;
}
esp_ae_rate_cvt_cfg_t output_resampler_cfg =
RATE_CVT_CFG(decoder_sample_rate_, codec->output_sample_rate(), ESP_AUDIO_MONO);
auto resampler_ret = esp_ae_rate_cvt_open(&output_resampler_cfg, &output_resampler_);
}
}
Input resampler reset (not recreate) from `main/audio/audio_service.cc:684-687`:
std::lock_guard<std::mutex> lock(input_resampler_mutex_);
if (input_resampler_ != nullptr) {
esp_ae_rate_cvt_reset(input_resampler_);
}
Rate converter config from `main/audio/audio_service.cc:5-10`:
#define RATE_CVT_CFG(_src_rate, _dest_rate, _channel) \
(esp_ae_rate_cvt_cfg_t) { \
.src_rate = (uint32_t)(_src_rate), .dest_rate = (uint32_t)(_dest_rate), \
.channel = (uint8_t)(_channel), .bits_per_sample = ESP_AUDIO_BIT16, .complexity = 2, \
.perf_type = ESP_AE_RATE_CVT_PERF_TYPE_SPEED, \
}