Rebuilt from a behaviour spec by an implementer who never saw the removed code (batch K: rest of #2170).
(cherry picked from commit 1930a91fd797b33a94e99911fc9905a945362ad0)
Removes code from contributors without a CLA on file, leaving CLEAN-ROOM markers where the reimplementation goes.
(cherry picked from commit 03c8656ab22b5191cfea9647225c4e6f6e94c095)
The fallback divided every decode by 32768.0, which is only correct for
16-bit input. pydub reports 8-bit as sample_width 1 and widens 24-bit to a
full-range int32 (sample_width 4), so a reference clip came back 32768x too
loud for 24- and 32-bit sources and 256x too quiet for 8-bit ones. Nothing
downstream clamps, so the clip silently became noise.
Measured on x86_64 with pydub 0.25.1, peak of a 0.5-amplitude sine:
PCM_U8 0.00195 PCM_24 32768.0
PCM_16 0.5 PCM_32 32768.0
Catching ImportError made this reachable for everyone: before, the fallback
ran only for formats torchaudio could not open, but on torchaudio >= 2.9
without TorchCodec it is the only path, turning a loud failure into a silent
wrong result. Fixing it here rather than leaving it for the next reader.
pydub already exposes the right divisor as max_possible_amplitude. No 24-bit
special case is needed: its own comment claims 24-bit values are "not scaled
up to the 32 bit range", but the conversion writes the pad byte first, so it
lands in the LSB and the sample does occupy the full int32 range. Verified
empirically across all five subtypes above.
Also corrects this file's motivation: torch 2.8.0 from the pinned cu128
index does carry sm_120 (arch_list confirmed on an sm_120 device, matching
CU128_ARCHS in tests/test_cuda_arch_compat.py), so the earlier claim that
RTX 50-series owners must leave the pin was wrong.
Raised by CodeRabbit and Greptile on the PR.
Signed-off-by: Moep90 <volleyballlive@googlemail.com>
Signed-off-by: moep90 <volleyballlive@googlemail.com>
torchaudio >= 2.9 sends save()/load() through TorchCodec, which requires
FFmpeg shared libraries. Without them every generation returns 500 and every
reference-audio read (cloning, watermark, dub) raises ImportError.
Follow-up to #1931: that issue's fix guarded set_audio_backend() but left
load()/save() unprotected, so the RTX 50-series users it identified as having
no choice but to leave the torch 2.8.0 pin still hit a hard failure.
- _safe_torchaudio_save: fall back to the audited _safe_soundfile_write
- _safe_soundfile_write: optional format= passthrough (BytesIO needs it)
- load_audio: catch ImportError so its existing pydub fallback actually runs
backend="soundfile" does not avoid this; 2.9 accepts and ignores it.
Signed-off-by: Moep90 <volleyballlive@googlemail.com>