Fix float-to-integer PCM conversion in to_wav (int8 crash, norm=False silence, truncation bias)#444
Open
gaoflow wants to merge 1 commit into
Open
Fix float-to-integer PCM conversion in to_wav (int8 crash, norm=False silence, truncation bias)#444gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
MicrophoneArray.to_wav wrote np.array(signal, dtype=bitdepth) with no quantization, so the float->integer-PCM path was broken three ways: - bitdepth=np.int8 raised ValueError (WAV 8-bit PCM is unsigned); it is now written as unsigned offset-binary, the inverse of what to_float32 reads back. - norm=False with any integer bitdepth truncated a [-1, 1] float signal to an all-zero file. Float signals are now scaled to full scale. - casts truncated toward zero, giving a sign-dependent ~1 LSB bias. Samples are rounded and clipped so the cast cannot overflow. to_16b rounds as well.
fakufaku
self-requested a review
July 18, 2026 00:18
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.
MicrophoneArray.to_wavconverts a float signal to the requestedbitdepthwith a barenp.array(signal, dtype=bitdepth), i.e. no float-to-PCM conversion at all. That breaks thewrite side of the same dtype handling #440 (read) and #443 (normalize) already fixed:
norm=False+ any integer bitdepth silently writes an all-zero file. Simulation signals arefloats in ~[-1, 1]; the bare cast truncates every sample to 0. No error, just silence on disk.
bitdepth=np.int8always raisesValueError: Unsupported data type 'int8'. WAV 8-bit PCM isunsigned; the documented
int8option can't work with a signed cast. This is the write side ofthe unsigned-8-bit format Fix to_float32 producing nan/inf for unsigned-integer (8-bit WAV) input #440 taught
to_float32to read.(mean -0.5 LSB for x>0, +0.5 for x<0). This hits the documented happy path too
(
examples/room_from_rt60.pyusesnorm=True, bitdepth=np.int16), and the exportedto_16b.#440 made an 8-bit WAV readable; this makes one writable. The conversion is now the exact inverse of
to_float32: scale by full scale2**(bits-1), round, clip so the cast can't overflow, and writeint8as unsigned offset-binary. Soto_wav->wavfile.read->to_float32round-trips.Round-trip residual (write -> read ->
to_float32, 1 kHz tone), before vs after:One judgment call:
norm=False+ a float signal + integer bitdepth goes from "all-zero garbage" to"input assumed full-scale [-1, 1], clipped" (the soundfile/librosa convention). Nothing sane consumes
the current all-zero output, but if you'd rather raise for out-of-range input than clip, happy to
switch that hunk.
Tests in
tests/test_to_wav.pycover every bitdepth x norm round-trip (including the previouslycrashing
int8and all-zeronorm=Falserows), the uint8-on-disk / silence->128 behavior,full-scale saturation without wraparound (int64 is the sharp case:
2**63-1isn't float64representable), rounding, multichannel, integer-input passthrough, and the
to_16bbias. Rantests/test_to_wav.pyplustest_to_float32,test_normalize,test_microphone_array,test_metrics,test_create_noisy_signallocally (59 passing); did not run the heavysimulation/DOA suites.