Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:78 Xiaozhi esp32 Audio Echo Cancellation

From Leeroopedia
Knowledge Sources
Domains Audio_Processing, Signal_Processing
Last Updated 2026-10-17 00:00 GMT

Overview

A signal processing technique that removes the device's own speaker output from the microphone input in real-time, enabling full-duplex voice conversation where the microphone captures user speech while the speaker plays TTS audio simultaneously.

Description

Acoustic Echo Cancellation (AEC) is essential for voice assistants that need to listen while speaking. Without AEC, the microphone picks up the TTS audio from the speaker, creating an echo that the server's speech-to-text engine would misinterpret as user speech.

XiaoZhi supports three AEC modes:

  • Off (kAecOff): No echo cancellation. The device mutes the microphone during TTS playback (half-duplex).
  • Device-side (kAecOnDeviceSide): The ESP-AFE pipeline performs AEC locally using a reference signal from the I2S output path. Requires CONFIG_USE_DEVICE_AEC and AFE-capable hardware (S3/P4).
  • Server-side (kAecOnServerSide): The microphone audio (including echo) is sent to the server, which performs AEC using its knowledge of the TTS audio. Requires CONFIG_USE_SERVER_AEC.

Device-side AEC uses the Audio Front-End (AFE) provided by ESP-SR, which takes both the microphone input and a reference copy of the speaker output signal, then subtracts the echo using adaptive filtering. The AFE configuration specifies the number of reference channels.

Server-side AEC relies on timestamps embedded in the binary audio protocol (BinaryProtocol2.timestamp) to align the microphone audio with the TTS audio that the server previously sent.

Usage

Use this principle when:

  • Full-duplex voice conversation is required (user can interrupt TTS by speaking)
  • The device has AFE-capable hardware (ESP32-S3, ESP32-P4) for device-side AEC
  • Low-latency echo removal is needed (device-side preferred)
  • The target chip lacks AFE support (use server-side or half-duplex)

Theoretical Basis

AEC uses adaptive filtering to model the acoustic path from speaker to microphone:

x(n) = microphone signal (speech + echo)
d(n) = reference signal (speaker output)
ê(n) = estimated echo = H(z) * d(n)   [adaptive filter]
y(n) = x(n) - ê(n)                     [cleaned signal]

The adaptive filter H(z) models the room's acoustic transfer function. The ESP-AFE implements this as part of its processing pipeline, configured via afe_config_t with aec_mode and reference channel count.

The server-side AEC alternative uses timestamp-based alignment:

Server sends TTS audio at time T
Device captures microphone audio at time T+Δ
Device sends mic audio with timestamp T+Δ
Server subtracts known TTS audio from mic signal using alignment

Practical Guide

When configuring AEC:

  • Never enable both modes simultaneously: CONFIG_USE_DEVICE_AEC and CONFIG_USE_SERVER_AEC are mutually exclusive.
  • Configure reference channels: if the codec has a reference mic input (input_reference_), the AFE uses it as the echo reference.
  • Reset AFE buffer on mode switch: toggling AEC requires a deferred buffer reset (reset_pending_ flag in AfeAudioEngine).
  • Use realtime listening mode: kListeningModeRealtime requires AEC support for simultaneous listen-and-speak.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment