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 Output Drain Detection

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

Overview

A synchronization mechanism that precisely detects when all queued audio data has been fully decoded and played through the speaker, enabling accurate state transitions from speaking to idle and preventing premature or delayed returns to listening mode.

Description

Audio output drain detection solves the problem of knowing when playback is truly complete in a multi-stage pipeline. A naive approach (check if the last queue is empty) is insufficient because:

  • The decoder may be mid-frame (decode in flight)
  • The output task may be writing to I2S (output in flight)
  • New packets may arrive between checks

The XiaoZhi firmware implements drain detection with four conditions that must all be true:

  • audio_decode_queue_ is empty (no pending Opus packets)
  • audio_playback_queue_ is empty (no pending PCM tasks)
  • !decode_in_flight_ (decoder is not processing a frame)
  • !output_in_flight_ (output task is not writing to I2S)

The detection is idempotent: MarkPlaybackDrainedLocked() returns true only on the first transition to drained, using playback_drained_notified_ to prevent duplicate notifications. A playback_generation_ counter invalidates stale drain checks when the decoder is reset or stopped.

When drain is detected, the on_playback_drained callback fires, setting MAIN_EVENT_PLAYBACK_DRAINED, which the main loop processes to transition from speaking to idle and re-enable wake word detection.

Usage

Use this principle when:

  • The device must know precisely when TTS playback is complete
  • State transitions depend on playback completion (speaking → idle)
  • Deferred listening start must wait for playback to finish (auto mode)
  • Notification playback completion must be reported to the UI

Theoretical Basis

The drain condition is a conjunction of invariants:

IsPlaybackDrained ≡ decode_queue.empty()
                  ∧ playback_queue.empty()
                  ∧ ¬decode_in_flight
                  ∧ ¬output_in_flight

The generation counter handles invalidation:

<syntaxhighlight lang="text} On ResetDecoder() / Stop():

 playback_generation_++
 → any in-flight decode/output that checks generation
   will see mismatch and skip drain notification

On each decode/output completion:

 if generation matches:
   check IsPlaybackDrainedLocked()
   if true ∧ ¬playback_drained_notified:
     playback_drained_notified_ = true
     on_playback_drained()   // fires once

</syntaxhighlight>

The check points are:

  • AudioOutputTask completion (after OutputData)
  • OpusCodecTask decode completion (after pushing to playback queue)
  • PushPacketToDecodeQueue (resets playback_drained_notified_ = false)

Practical Guide

When implementing drain detection:

  • Track in-flight work explicitly: use boolean flags set before and cleared after each stage's work.
  • Make notification idempotent: use a notified flag to prevent duplicate callbacks.
  • Use a generation counter: increment on reset/stop to invalidate stale checks.
  • Check at all completion points: both decode and output tasks must check drain after their work.
  • Reset notification flag on new input: PushPacketToDecodeQueue resets playback_drained_notified_.

Related Pages

Implemented By

Page Connections

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