Heuristic:78 Xiaozhi esp32 AEC Mode Selection
| Knowledge Sources | |
|---|---|
| Domains | Embedded, Audio |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Choose between device-side and server-side AEC based on hardware reference mic availability; both modes are mutually exclusive at compile time.
Description
The firmware supports three AEC (Acoustic Echo Cancellation) modes: `kAecOff`, `kAecOnDeviceSide`, and `kAecOnServerSide`. Device-side AEC uses the ESP-AFE's built-in AEC pipeline, which requires a hardware reference microphone channel. Server-side AEC sends audio with timestamps to the server, which performs echo cancellation remotely. The two active modes are mutually exclusive: `CONFIG_USE_DEVICE_AEC` and `CONFIG_USE_SERVER_AEC` cannot both be enabled — a `#error` directive enforces this at compile time.
Device-side AEC is preferred when the board has a reference mic (e.g., ESP32-S3-BOX-3 with dual ES7311 codecs). It enables full-duplex realtime conversation mode (`kListeningModeRealtime`). Server-side AEC is the fallback for boards without a reference mic but with adequate network bandwidth.
Usage
Use this heuristic when configuring a board's AEC capability. Set `CONFIG_USE_DEVICE_AEC=y` in `config.json` only if the board has a physical reference microphone input (`codec->input_reference()` returns true). Otherwise, leave AEC off or use server-side AEC. Changing the AEC mode at runtime closes and reopens the audio channel.
The Insight (Rule of Thumb)
- Action: In `config.json`, set exactly one of `CONFIG_USE_DEVICE_AEC=y` or `CONFIG_USE_SERVER_AEC=y`. Never both.
- Value: Device-side AEC uses AFE `AEC_MODE_FD_LOW_COST` with `AEC_NLP_LEVEL_VERYAGGR`. Server-side AEC sends timestamps via `BinaryProtocol2`.
- Trade-off: Device-side AEC uses more CPU/PSRAM but lower network bandwidth. Server-side AEC saves device resources but requires the server to perform echo cancellation and adds network latency.
- Compatibility: Device-side AEC requires ESP32-S3/P4/S31 with a reference mic. Server-side AEC works on any target with any transport.
Reasoning
Full-duplex voice interaction (realtime mode where the user can interrupt the assistant while it speaks) requires echo cancellation so the microphone doesn't pick up the speaker's output. The ESP-AFE can do this on-device if a reference channel captures the speaker output. Without a reference mic, the only option is to send timestamped audio to the server and let it remove the echo using the known TTS output. The compile-time `#error` prevents accidental configuration that would produce broken audio — running both AEC pipelines would double-cancel and corrupt the signal.
When AEC mode changes at runtime, the audio channel must be closed and reopened because the AFE pipeline configuration (AEC on/off) is baked into the AFE creation and cannot be hot-swapped.
Code Evidence
Compile-time mutual exclusion from `main/application.cc:28-33`:
#if CONFIG_USE_DEVICE_AEC && CONFIG_USE_SERVER_AEC
#error "CONFIG_USE_DEVICE_AEC and CONFIG_USE_SERVER_AEC cannot be enabled at the same time"
#elif CONFIG_USE_DEVICE_AEC
aec_mode_ = kAecOnDeviceSide;
#elif CONFIG_USE_SERVER_AEC
aec_mode_ = kAecOnServerSide;
#endif
AFE AEC configuration from `main/audio/engines/afe_audio_engine.cc:133-135`:
afe_config->aec_init = codec_->input_reference();
afe_config->aec_mode = AEC_MODE_FD_LOW_COST;
afe_config->aec_nlp_level = AEC_NLP_LEVEL_VERYAGGR;
Runtime mode switching from `main/application.cc:1414-1438`:
void Application::SetAecMode(AecMode mode) {
aec_mode_ = mode;
Schedule([this]() {
switch (aec_mode_) {
case kAecOff:
audio_service_.EnableDeviceAec(false);
break;
case kAecOnServerSide:
audio_service_.EnableDeviceAec(false);
break;
case kAecOnDeviceSide:
audio_service_.EnableDeviceAec(true);
break;
}
// If the AEC mode is changed, close the audio channel
if (protocol_ && protocol_->IsAudioChannelOpened()) {
protocol_->CloseAudioChannel();
}
});
}
Server-side AEC timestamp tracking from `main/audio/audio_service.cc:359-367`:
#if CONFIG_USE_SERVER_AEC
/* Record the timestamp for server AEC */
if (task.timestamp > 0) {
if (timestamp_queue_.full()) {
timestamp_queue_.pop_front();
}
timestamp_queue_.push_back(task.timestamp);
}
#endif