Implementation:78 Xiaozhi esp32 AudioEngine
| Knowledge Sources | |
|---|---|
| Domains | Audio_Processing, Speech_Recognition, Embedded_Systems |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for audio front-end processing provided by the XiaoZhi firmware, defining the AudioEngine virtual interface and the AfeAudioEngine implementation that runs ESP-AFE for AEC, VAD, and wake word detection.
Description
AudioEngine is the abstract interface for audio processing (wake word detection, voice processing, AEC). It declares methods for feeding PCM data, enabling/disabling features, and registering callbacks for wake word detection, VAD state changes, and processed audio output.
AfeAudioEngine is the primary implementation for ESP32-S3 and ESP32-P4, using the ESP-AFE (Audio Front-End) library. It:
- Creates an AFE pipeline with configured AEC mode, VAD, and WakeNet models
- Runs a
ProcessingTaskthat feeds PCM to the AFE and fetches results - Dispatches results to
HandleWakeWordResult()orHandleVoiceResult()based on the active mode - Manages a pre-wake-word audio cache (
WakeWordAudioCache) for capturing audio before the wake word event - Supports custom wake words via
CustomWakeWord(MultiNet)
A LiteAudioEngine implementation exists for chips without AFE support (ESP32-C3, ESP32-C6), using standalone WakeNet.
Usage
Reference this implementation when:
- Understanding how wake word detection integrates with ESP-AFE
- Configuring AEC, VAD, or custom wake words
- Tracing the audio processing task lifecycle
- Adding a new audio engine implementation
Code Reference
Source Location
- Repository: XiaoZhi ESP32
- Files:
main/audio/audio_engine.h,main/audio/engines/afe_audio_engine.h,main/audio/engines/afe_audio_engine.cc,main/audio/engines/lite_audio_engine.cc
Signature
// Abstract interface
class AudioEngine {
public:
virtual ~AudioEngine() = default;
virtual bool Initialize(AudioCodec* codec, int frame_duration_ms,
srmodel_list_t* models_list) = 0;
virtual void Feed(std::vector<int16_t>&& data) = 0;
virtual void EnableWakeWordDetection(bool enable) = 0;
virtual void EnableVoiceProcessing(bool enable) = 0;
virtual void EnableDeviceAec(bool enable) = 0;
virtual bool HasWakeWord() const = 0;
virtual bool IsWakeWordDetectionEnabled() const = 0;
virtual bool IsVoiceProcessingEnabled() const = 0;
virtual bool IsAfeWakeWord() const = 0;
virtual std::vector<std::string> GetWakeWords() const = 0;
virtual size_t GetFeedSize() const = 0;
virtual void OnWakeWordDetected(std::function<void(const std::string&)> callback) = 0;
virtual void OnOutput(std::function<void(std::vector<int16_t>&&)> callback) = 0;
virtual void OnVadStateChange(std::function<void(bool)> callback) = 0;
virtual void EncodeWakeWordData() = 0;
virtual bool GetWakeWordOpus(std::vector<uint8_t>& opus) = 0;
virtual const std::string& GetLastDetectedWakeWord() const = 0;
};
// AFE implementation (partial — see afe_audio_engine.h for full)
class AfeAudioEngine : public AudioEngine {
public:
AfeAudioEngine();
~AfeAudioEngine() override;
bool Initialize(AudioCodec* codec, int frame_duration_ms,
srmodel_list_t* models_list) override;
void Feed(std::vector<int16_t>&& data) override;
void EnableWakeWordDetection(bool enable) override;
void EnableVoiceProcessing(bool enable) override;
void EnableDeviceAec(bool enable) override;
// ... (all virtual methods overridden)
private:
void ProcessingTask();
void HandleWakeWordResult(const afe_fetch_result_t* result);
void HandleVoiceResult(const afe_fetch_result_t* result);
// ...
};
Import
#include "audio_engine.h"
// For the AFE implementation:
#include "engines/afe_audio_engine.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| codec | AudioCodec* | Audio codec for hardware config (channels, reference mic) | |
| frame_duration_ms | int | Frame duration for AFE pipeline (typically 60) | |
| models_list | srmodel_list_t* | Speech recognition models from assets | |
| data (Feed) | std::vector<int16_t>&& | 10ms PCM frames at 16kHz mono | |
| enable | bool | Enable/disable wake word or voice processing |
Outputs
| Name | Type | Description |
|---|---|---|
| OnOutput callback | Processed PCM (AEC-applied, VAD-gated) for encoding | |
| OnWakeWordDetected callback | Wake word string when detected | |
| OnVadStateChange callback | True when speech detected, false when silence | |
| GetWakeWordOpus() | Opus-encoded pre-wake-word audio cache |
Usage Examples
Initializing the Audio Engine
// In AudioService::InitializeAudioEngine()
audio_engine_ = std::make_unique<AfeAudioEngine>();
audio_engine_->Initialize(codec_, OPUS_FRAME_DURATION_MS, models_list_);
// Register callbacks
audio_engine_->OnWakeWordDetected([this](const std::string& word) {
callbacks_.on_wake_word_detected(word);
});
audio_engine_->OnOutput([this](std::vector<int16_t>&& data) {
PushTaskToEncodeQueue(kAudioTaskTypeEncodeToSendQueue, std::move(data));
});
audio_engine_->OnVadStateChange([this](bool speaking) {
callbacks_.on_vad_change(speaking);
});
Feeding Audio and Toggling Modes
// In AudioInputTask — feed 10ms frames to the engine
std::vector<int16_t> data;
ReadAudioData(data, 16000, 160);
audio_engine_->Feed(std::move(data));
// Enable wake word detection (idle state)
audio_engine_->EnableWakeWordDetection(true);
// Switch to voice processing (listening state)
audio_engine_->EnableWakeWordDetection(false);
audio_engine_->EnableVoiceProcessing(true);
Related Pages
Implements Principle
- Principle:78_Xiaozhi_esp32_Wake_Word_Detection
- Principle:78_Xiaozhi_esp32_Audio_Echo_Cancellation
- Principle:78_Xiaozhi_esp32_Voice_Activity_Detection
- Principle:78_Xiaozhi_esp32_Audio_Pipeline_Architecture