fix: OGG/Opus audio truncation — final page lost in write_chunk finalize#448
fix: OGG/Opus audio truncation — final page lost in write_chunk finalize#448will-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
|
Hit this in production via OpenClaw → Telegram voice-note delivery (gateway requests 1. Truncation (the bug this PR fixes): Telegram played "Hi Jim, Happy Sun" — last ~1.5s gone, classic missing-final-page symptom. 2. Possibly related — emoji input produces an empty Opus container (127 bytes, header only): Same text via Workaround we shipped: forced This PR has been clean since Feb — could a maintainer take a look? Happy to test against a custom build before merge if useful. |
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 frames.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)