Skip to content

Fix: 8-bit WAV bias overflowed numpy int8 waveforms (regression from #14) - #15

Merged
thorwhalen merged 1 commit into
masterfrom
fix/uint8-numpy-overflow
Sep 22, 2026
Merged

thorwhalen merged 1 commit into
masterfrom
fix/uint8-numpy-overflow

Conversation

@thorwhalen

Copy link
Copy Markdown
Member

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_samples adds a Python 128 to each np.int8 sample, 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.index before the shift. Non-integer samples still raise (the struct codec always refused them) rather than being truncated by int().

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_file with a LIST chunk larger than read_size before fmt , 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

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>
@thorwhalen
thorwhalen merged commit 3b92016 into master Sep 22, 2026
6 checks passed
@thorwhalen
thorwhalen deleted the fix/uint8-numpy-overflow branch September 22, 2026 14:09
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