Skip to content

[Fix] RawAudio.toBlob() ignores TypedArray byteOffset/length (#1704)#1712

Open
yushuosun wants to merge 1 commit into
huggingface:mainfrom
yushuosun:fix/rawaudio-toblob-byteoffset
Open

[Fix] RawAudio.toBlob() ignores TypedArray byteOffset/length (#1704)#1712
yushuosun wants to merge 1 commit into
huggingface:mainfrom
yushuosun:fix/rawaudio-toblob-byteoffset

Conversation

@yushuosun

Copy link
Copy Markdown

Motivation

RawAudio.toBlob() produces a WAV whose audio is wrong (e.g. leading silence / only the first N samples) when the audio is a TypedArray view with a non-zero byteOffset or a length shorter than its backing buffer — anything coming from Float32Array.subarray(...), for example (#1704).

Root cause

In packages/transformers/src/utils/audio.js, encodeWAV() writes the WAV data-chunk size from each view's length (chunk.length), but builds the Blob payload from chunk.buffer — the entire underlying ArrayBuffer, ignoring the view's byteOffset/byteLength. Header and payload then disagree, and decoders honor the header, reading the wrong window of samples.

Modifications

packages/transformers/src/utils/audio.js (encodeWAV): build the payload from Uint8Array views that respect each chunk's byteOffset/byteLength, instead of passing the whole chunk.buffer.

Duplicate-check

encodeWAV() sized the WAV data chunk from each view's length but built the
Blob payload from chunk.buffer, the entire underlying ArrayBuffer. For views
with a non-zero byteOffset or a length shorter than the buffer (e.g. the
output of Float32Array.subarray()), the header and payload disagreed and
decoders played the wrong window. Emit Uint8Array views that honor each
chunk's byteOffset/byteLength.
Copilot AI review requested due to automatic review settings June 28, 2026 21:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect WAV payload generation in RawAudio.toBlob()/encodeWAV() when audio chunks are TypedArray views (e.g., Float32Array.subarray(...)) with non-zero byteOffset or shortened byteLength, ensuring the WAV header and data payload refer to the same sample window.

Changes:

  • Update encodeWAV() to append per-chunk byte-accurate views (Uint8Array over buffer + byteOffset/byteLength) instead of the full backing ArrayBuffer.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

view.setUint32(40, totalLength * 4, true);

return new Blob([buffer, ...chunks.map((chunk) => /** @type {ArrayBuffer} */ (chunk.buffer))], {
return new Blob([buffer, ...chunks.map((chunk) => new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength))], {
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.

2 participants