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 Voice Activity Detection

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

Overview

A signal processing technique that determines whether human speech is present in an audio stream, used to automatically start and stop audio capture during voice conversations and to manage power states based on audio activity.

Description

Voice Activity Detection (VAD) is a binary classifier that operates on short audio frames (typically 10–30ms) to determine whether speech is present. In the XiaoZhi firmware, VAD runs as part of the ESP-AFE pipeline and is exposed through the AudioEngine interface via the OnVadStateChange callback.

VAD serves two critical functions:

  • Endpoint detection: In auto-stop listening mode, the server uses VAD state changes (speaking → not-speaking) to detect when the user has finished speaking and should stop capturing audio.
  • Power management: The audio service monitors input/output activity timestamps and transitions to a low-power state after AUDIO_POWER_TIMEOUT_MS (15s) of inactivity, shutting down audio tasks to conserve battery.

The VAD callback fires with a boolean speaking parameter, which sets the MAIN_EVENT_VAD_CHANGE event bit. The main loop reads IsVoiceDetected() to determine the current state.

Usage

Use this principle when:

  • Auto-stop listening mode is enabled (server needs VAD to detect end of speech)
  • Power management should reduce consumption during silence
  • The UI should show speaking/listening indicators based on voice activity

Theoretical Basis

VAD typically uses energy-based features combined with spectral analysis:

For each frame x(n):
  1. Compute short-time energy: E = Σ|x(n)|²
  2. Compute spectral features (MFCC, spectral centroid)
  3. Classify: speech vs. silence using trained model or threshold
  4. Apply hangover scheme (smooth transitions, avoid flicker)

The ESP-AFE integrates VAD into its processing pipeline. The result is delivered as a boolean state change callback:

AFE ProcessingTask:
  result = afe_iface_->fetch()
  if result.vad_state != previous_vad_state:
    vad_state_change_callback_(result.vad_state == SPEECH)

Practical Guide

When using VAD:

  • Handle the callback on the main loop: the on_vad_change callback runs in the audio task context; use an event bit to signal the main loop.
  • Use VAD for auto-stop: kListeningModeAutoStop relies on the server receiving VAD information to trigger SendStopListening.
  • Combine with power management: monitor last_input_time_ and last_output_time_ to shut down audio tasks after inactivity.

Related Pages

Implemented By

Page Connections

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