fix: respect TypedArray byteOffset and byteLength in RawAudio.toBlob()#1714
Open
jahanzaib-iqbal-dev wants to merge 1 commit into
Open
Conversation
When audio chunks are TypedArray subarrays (non-zero byteOffset), using chunk.buffer returns the entire backing ArrayBuffer, causing WAV header and payload length mismatch. Fix uses new Uint8Array(c.buffer, c.byteOffset, c.byteLength) to correctly slice only the relevant portion of the backing buffer. Fixes huggingface#1704
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
Fixes
RawAudio.toBlob()producing malformed WAV files when audio chunks are TypedArray subarrays with non-zerobyteOffset.Root Cause
The
encodeWAVmethod useschunk.bufferto get the raw binary data from each audio chunk. However, for TypedArray subarrays (created via.subarray()or.slice()in streaming contexts),.bufferreturns the entire backingArrayBufferregardless of the view's offset and length. This causes the WAV blob to contain extra bytes from outside the actual audio data, creating a length mismatch with the WAV header.Fix
Replace
chunk.bufferwithnew Uint8Array(c.buffer, c.byteOffset, c.byteLength)to correctly extract only the relevant portion of the backing buffer, respecting the TypedArray view's offset and length.Before:
```js
return new Blob([buffer, ...chunks.map((chunk) => chunk.buffer)], { type: 'audio/wav' });
```
After:
```js
return new Blob([buffer, ...chunks.map((c) => new Uint8Array(c.buffer, c.byteOffset, c.byteLength))], { type: 'audio/wav' });
```
Impact
Fixes silent audio corruption in TTS pipelines that use streaming output, where audio chunks are subarrays of a larger buffer. The WAV header length fields are computed correctly from
chunk.length(number of elements), but the oldchunk.buffercould include padding bytes outside the view's range, causing the actual data payload to mismatch the declared lengths.Fixes #1704