Skip to content

Conversation

@phritz
Copy link

@phritz phritz commented Jan 24, 2026

(AI-generated description, natch, but I think it covers it well. My use case is AEC and the ~50ms delay causes alignment headaches between reference audio from audiotee and what the mic is picking up)

PR Title

Add --flush option to reduce stdout buffering latency

PR Description

Summary

  • Adds a --flush CLI flag that flushes stdout after each audio chunk write
  • Fixes latency issues when piping AudioTee output to a parent process

Problem

When stdout is connected to a pipe (common when spawning AudioTee from Node.js, Python, etc.), the OS uses block buffering (typically 4KB-64KB). Since AudioTee writes 960-byte chunks (10ms at 48kHz s16le mono), 4-7 chunks accumulate before the buffer flushes, causing 40-70ms bursts instead of steady 10ms delivery.

This is problematic for real-time applications like:

  • AEC (Acoustic Echo Cancellation) that rely on consistent timing between reference and capture signals
  • Real-time ASR where latency spikes affect transcription timing
  • Audio visualization where smooth updates are expected

Solution

Add fflush(stdout) after each FileHandle.standardOutput.write() call when the --flush flag is enabled.

Changes

  • Sources/Output/Handlers/BinaryOutputHandler.swift: Add flushAfterWrite parameter to init, call fflush(stdout) when enabled
  • Sources/CLI/AudioTee.swift: Add --flush flag, pass to BinaryAudioOutputHandler

Usage

# Enable flushing for real-time applications
./audiotee --flush | your_audio_processor

# Combine with other options
./audiotee --sample-rate 48000 --flush | node audio-processor.js

Tradeoffs

  • (+) Enables real-time chunk delivery when piping
  • (+) Backward compatible - default behavior unchanged
  • (-) Slightly higher CPU overhead due to syscalls on each chunk (negligible for most use cases)

Testing

Tested by spawning AudioTee from Node.js and measuring inter-chunk timing:

  • Without --flush: 40-70ms gaps between chunk arrivals
  • With --flush: Consistent ~10ms gaps matching chunk duration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant