Repository navigation
Guard resample overshoot before 16-bit save (stop silent clipping) - #327
Merged
Merged
Conversation
The audio-copy sanitize path (downmix -> 16 kHz resample -> 16-bit save) let torchaudio silently hard-clamp any sample the resampler pushed past full-scale -- destructive, no error. On a clean (un-clipped) source this manufactured clipping (e.g. diadochokinesis source peak 0.998 -> resample 1.046 -> saved 1.000 with introduced clipping). Add _guard_resample_overshoot: when the resampled peak exceeds 1.0, rescale the waveform down to the input's peak so nothing exceeds [-1, 1] -- preserving gain and waveform shape, introducing no clipping -- and log a warning. No-op when the resampled peak is already in range, so non-overshooting audio is byte-identical. Measured on adult 07_01: resample overshoot fired on ~18% of recordings (all previously silently clamped); 14 had clean sources (definitely-wrong clamps). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Address review: the overshoot branch now preserves Audio.metadata (was dropped vs the no-op branch), and returns a no-op on a zero-sample waveform instead of raising on .max() of an empty tensor. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Author
|
Addressed the code review (pushed in
Kept by design:
Verified against the adult 07_01 build: overshoot fired on ~18% of recordings (all previously silently clamped); 14 had clean sources (the unambiguously-wrong clamps). |
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
The audio-copy sanitize path (
downmix → 16 kHz resample → 16-bit save) let torchaudio silently hard-clamp any sample the resampler pushed past full-scale. That's destructive (flattens peaks) and raises no error. On a source that wasn't already clipped it manufactures clipping — e.g.diadochokinesis-(v2)-tuh: source peak 0.998 → resample 1.046 → saved 1.000 with clipping introduced.Measured on the adult 07_01 build: resample overshoot fired on ~18% of recordings (all previously silently clamped); 14 of those had clean (un-clipped) sources, so those clamps are unambiguously wrong.
Fix
_guard_resample_overshoot(resampled_audio, in_peak)indataset.py: when the resampled peak exceeds 1.0, rescale the waveform down to the input peak so no sample exceeds [-1, 1] — preserving gain and waveform shape, introducing no clipping — and log a warning. No-op when the resampled peak is already in range, so non-overshooting audio is byte-identical to before. Wired into_copy_audio_files_parallelright beforesave_to_file.Rescale-to-input-peak (rather than clamp, or rescale-to-1.0) keeps every file at its own source level whether or not it overshot; overshoot only occurs on near-full-scale material, so the rescale factor is always small.
Verification
tests/test_resample_clip_guard.py(2 tests): overshoot → rescaled to input peak with a real 16-bit round-trip showing 0% clipping; in-range → no-op.diadochokinesis-(v2)-tuhold-path saved peak 1.000 with clipping, new-path saves 0.998 with 0% clipping.Independent of the language-aware work (#326); touches only the audio-copy path.
🤖 Generated with Claude Code