Fix: 8-bit WAV bias overflowed numpy int8 waveforms (regression from #14) - #15
Merged
Merged
Conversation
encode_wav_bytes(np.int8 array, width_bytes=1) worked before #14 and raised OverflowError after it (numpy 2) because the +128 bias was applied to np.int8 scalars. Samples are now widened with operator.index before the shift, which also keeps non-integer samples failing loudly instead of truncating. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Post-merge refute review of #14.
Defect:
encode_wav_bytes(np.array([...], dtype=np.int8), sr, width_bytes=1)encoded fine before #14. After it,_shift_samplesadds a Python128to eachnp.int8sample, which numpy 2 refuses (OverflowError: Python integer 128 out of bounds for int8) and numpy 1 silently wraps. int8 is the natural dtype for 8-bit audio, so this is the common numpy case, mono and multichannel.Fix: widen each sample with
operator.indexbefore the shift. Non-integer samples still raise (the struct codec always refused them) rather than being truncated byint().Test:
test_8_bit_encode_accepts_a_numpy_int8_waveform(mono + stereo; fails on master with the OverflowError, passes here; skips if numpy is absent, since numpy is not a recode dependency). Full suite + doctests: 173 passed locally on Python 3.10.Also checked and found sound: EXTENSIBLE fallback on 3.10 (3-channel and 8-bit),
extract_wav_header_from_filewith a LIST chunk larger thanread_sizebeforefmt, and consumers (hum, know) only use 16-bit WAV, so the unsigned-8-bit default flip does not affect them.Self-reviewed only (the dispatching run disallowed sub-agents).
🤖 Generated with Claude Code