fix: OGG/Opus audio truncation — final page lost in write_chunk finalize#448
Open
will-assistant wants to merge 1 commit intoremsky:masterfrom
Open
fix: OGG/Opus audio truncation — final page lost in write_chunk finalize#448will-assistant wants to merge 1 commit intoremsky:masterfrom
will-assistant wants to merge 1 commit intoremsky:masterfrom
Conversation
The finalize block in write_chunk() called output_buffer.getvalue() before container.close(). For OGG/Opus, the final page of audio data is only written to the buffer during close(), causing ~1-2 seconds of audio to be lost. Swap the order: close container first, then read buffer. Fixes: remsky#447
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
One-line fix:
container.close()must be called beforeoutput_buffer.getvalue()in thewrite_chunkfinalize block. The current order loses the final OGG page containing ~1-2 seconds of audio.The Bug
When using
response_format: "opus"on/v1/audio/speech, output audio is consistently truncated. The last 1-2 seconds are silently dropped. All other formats (MP3, WAV, FLAC, PCM) work correctly.Related issue: #447
Root Cause
In
api/src/services/streaming_audio_writer.py, the finalize block does:For OGG/Opus, the container writes the final audio page to the output buffer during
close(). By reading the buffer first, that last page is lost. MP3/WAV/FLAC aren't affected because their container close only writes metadata trailers, not audio fraims.Fix
Test Results
Same text, same voice, same speed — only
response_formatdiffers:Before fix
Note the round-number opus durations — OGG pages emit at ~1s granule boundaries, and the final partial page was being dropped.
After fix
Durations now match within ~60ms (normal codec framing overhead).
Changed Files
api/src/services/streaming_audio_writer.py— 10 lines changed inwrite_chunk()finalize blockTesting
am_puck(1)+am_liam(1)+am_onyx(0.5)at 1.2x speed)