Add --flush option to reduce stdout buffering latency #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(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
--flushoption to reduce stdout buffering latencyPR Description
Summary
--flushCLI flag that flushes stdout after each audio chunk writeProblem
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:
Solution
Add
fflush(stdout)after eachFileHandle.standardOutput.write()call when the--flushflag is enabled.Changes
Sources/Output/Handlers/BinaryOutputHandler.swift: AddflushAfterWriteparameter to init, callfflush(stdout)when enabledSources/CLI/AudioTee.swift: Add--flushflag, pass toBinaryAudioOutputHandlerUsage
Tradeoffs
Testing
Tested by spawning AudioTee from Node.js and measuring inter-chunk timing:
--flush: 40-70ms gaps between chunk arrivals--flush: Consistent ~10ms gaps matching chunk duration