8000 fix(codec): stream all streamable Blobs, not just files ≥500 MB by johannboehme · Pull Request #51 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content

fix(codec): stream all streamable Blobs, not just files ≥500 MB - #51

Merged
johannboehme merged 1 commit into
mainfrom
claude/vigorous-goodall-dbd7aa
Jun 6, 2026
Merged

johannboehme merged 1 commit into
mainfrom
claude/vigorous-goodall-dbd7aa

Conversation

@johannboehme
Copy link
Copy Markdown
Owner

What

Audio decode routing in codec/index.ts is now capability-based instead of size-based:

  • Any Blob/File that sniffs as mp3 / mp4 / wav → streaming decoder, regardless of file size.
  • ArrayBuffer sources and non-streamable containers (ogg/flac/opus) → whole-file path (decodeAudioData → ffmpeg.wasm), unchanged.
  • If streaming fails on a sub-ceiling file, it falls back to the whole-file path; above the ceiling it surfaces the streaming error directly (a whole-file retry would only OOM). The streaming reason is chained into the combined error.
  • STREAMING_THRESHOLD is repurposed from "stream above this" to "fallback ceiling."

Only one production file changed.

Why

A tester hit a hard failure at [decoding-studio-audio] with a 45.5 MB MP3:

Audio decode failed (ffmpeg: RangeError: Array buffer allocation failed;
                     webcodecs: Unable to decode audio data)

Root cause: the resolver only routed to the memory-safe streaming decoder above a 500 MB file-size gate. A long MP3 stays below that gate but expands ~10–25× when decoded — a ~40 min file becomes ~1 GB of PCM, OOMing both whole-file backends. Compressed file size is the wrong proxy; duration drives memory. Streaming reads in 4 MiB batches and keeps memory bounded.

Reviewer notes

  • Behavioural shift on the main sync path: normal-sized mp4/mp3 files now go through the streaming mp4/mp3 decoders (mp4box.js + WebCodecs AudioDecoder) instead of decodeAudioData. Those decoders were already exercised by the ≥500 MB path and have browser tests, but this is the main risk surface.
  • All callers already pass Blob/File (or a Blob|ArrayBuffer union the new logic handles); no caller changes needed.
  • runStreamingDecode still sniffs internally (trivial 16-byte re-read) to preserve the forceBackend: "streaming" "not streamable" error.

Testing

  • 36/36 codec browser tests green; tsc --noEmit clean.
  • New routing tests (TDD, red→green): a streamable Blob emits sub-progress (proves streaming ran); an ArrayBuffer source emits only 0/1 (proves whole-file path). Both rely on the fact streaming and whole-file both report backend: "webcodecs", so progress shape is the distinguishing signal.
  • Real repro: generated a 30-min / 27 MB MP3 and decoded it through the resolver → 1800.0s, 39,691,059 samples, no OOM (throwaway test + fixture removed before commit).

🤖 Generated with Claude Code

A 45.5 MB MP3 (a long recording) failed at [decoding-studio-audio] with
"Array buffer allocation failed" / "Unable to decode audio data". Root
cause: the resolver only used the memory-safe streaming decoder above a
500 MB *file-size* gate. A long MP3 stays below that gate but expands to
~1 GB of PCM, OOMing the whole-file decodeAudioData + ffmpeg.wasm paths.

Route by capability instead of size: any Blob that sniffs as mp3/mp4/wav
streams regardless of size; ArrayBuffer sources and non-streamable
containers (ogg/flac/opus) keep the whole-file path. Streaming failures
on sub-ceiling files fall back to whole-file; the streaming reason is
chained into the combined error. STREAMING_THRESHOLD is repurposed as the
fallback ceiling.

Verified: a 30-min MP3 decodes via streaming (1800s, 39.7M samples); new
routing tests assert streamable Blobs emit sub-progress (streaming) while
ArrayBuffer sources don't (whole-file). All 36 codec browser tests green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@johannboehme
johannboehme merged commit a8637f4 into main Jun 6, 2026
1 check passed
@johannboehme
johannboehme deleted the claude/vigorous-goodall-dbd7aa branch June 6, 2026 10:56
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

0