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 Wake Word Detection

From Leeroopedia
Knowledge Sources
Domains Speech_Recognition, Machine_Learning, Embedded_Systems
Last Updated 2026-10-17 00:00 GMT

Overview

An on-device keyword spotting mechanism that continuously monitors microphone input for predefined wake words using lightweight neural network models (WakeNet/MultiNet), triggering an event when a match is detected without requiring continuous network connectivity.

Description

Wake word detection is a form of always-on keyword spotting (KWS) that runs locally on the ESP32's processor. Unlike cloud-based voice activation, it requires no network round-trip, enabling sub-second response times and offline operation. The XiaoZhi firmware supports two detection strategies:

  • WakeNet (ESP-provided): A compact neural network optimized for ESP32 chips, trained on common wake words. Used by the AFE (Audio Front-End) engine on S3/P4 targets.
  • MultiNet (Custom): A command recognition model that supports custom wake words defined by the user. Used as a fallback or on chips without AFE support (C3/C6) via the Lite engine.

The detection runs in a background processing task that continuously feeds 10ms PCM frames (160 samples at 16kHz) into the model. When a wake word is detected, a callback fires, setting an event bit (MAIN_EVENT_WAKE_WORD_DETECTED) that the main event loop processes to initiate a voice conversation.

Key properties:

  • Continuous monitoring: The audio input task feeds frames to the engine regardless of device state, but detection is only active when the AS_EVENT_WAKE_WORD_RUNNING bit is set.
  • Pre-wake-word audio cache: A ring buffer (WakeWordAudioCache) stores ~1s of audio before the wake word, so the beginning of speech is not lost.
  • Detection suppression: During listening or speaking states, wake word detection is disabled to prevent self-triggering from TTS playback.

Usage

Use this principle when:

  • A voice assistant must respond to a wake word without a physical button
  • On-device processing is required (privacy, latency, offline capability)
  • The device must enter a low-power idle state between activations
  • Multiple wake words or custom commands are needed

Theoretical Basis

Keyword spotting models typically use a streaming neural network architecture:

Audio Stream (16kHz PCM, 10ms frames)
    ↓
[Feature Extraction] → MFCC / log-mel features
    ↓
[Acoustic Model] → WakeNet (CNN/RNN) or MultiNet
    ↓
[Posterior → Decision] → softmax threshold
    ↓
Wake Word Detected (callback fires)

The detection state machine within the audio service:

IDLE (wake word running)
  → wake word detected → clear AS_EVENT_WAKE_WORD_RUNNING
  → set MAIN_EVENT_WAKE_WORD_DETECTED
  → main loop: BeginWakeWordInvoke() → OpenAudioChannel()

LISTENING (wake word disabled)
  → server sends tts/stop → re-enable wake word

SPEAKING (wake word disabled)
  → playback drained → re-enable wake word

The pre-wake-word audio cache solves the cold start problem: by the time a wake word is detected and the audio channel opens, the first syllables of the user's command may already have been spoken. The cache retains a rolling buffer of PCM frames that are encoded and sent immediately after the wake word event.

Practical Guide

When implementing wake word detection:

  • Feed frames at the engine's expected rate: GetFeedSize() returns the number of samples per feed call (typically 160 for 10ms at 16kHz).
  • Disable detection during active conversation: call EnableWakeWordDetection(false) when entering listening/speaking states.
  • Use an audio cache: retain pre-wake-word audio to avoid losing the start of user speech.
  • Handle the detection on the main loop: use an event bit to signal the main task, not a direct callback that might run in the audio task context.

Related Pages

Implemented By

Uses Heuristic

Page Connections

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