[Bugfix] Fix TorchCodec audio IO correctness (#58364)
Signed-off-by: Isotr0py <Isotr0py@outlook.com>
This commit is contained in:
@@ -185,9 +185,15 @@ def test_load_audio_backend_matches_default(backend, dummy_audio_bytes):
|
||||
ref_audio, ref_sr = load_audio(BytesIO(dummy_audio_bytes), sr=None)
|
||||
audio, sr = load_audio(BytesIO(dummy_audio_bytes), sr=None, backend=backend)
|
||||
assert sr == ref_sr
|
||||
# Decoders disagree only on codec encoder-delay/padding, so torchcodec may
|
||||
# emit a few extra trailing samples (e.g. ~192 for Ogg Vorbis). Compare the
|
||||
# overlapping region, which must agree to float32 precision.
|
||||
if backend == "torchcodec":
|
||||
# Vorbis encoder padding is trimmed, so torchcodec must match the
|
||||
# soundfile reference exactly, including the waveform length.
|
||||
assert audio.shape == ref_audio.shape
|
||||
np.testing.assert_allclose(ref_audio, audio, atol=1e-4)
|
||||
return
|
||||
# Decoders disagree only on codec encoder-delay/padding, so PyAV may
|
||||
# emit a few extra trailing samples (e.g. ~192 for Ogg Vorbis). Compare
|
||||
# the overlapping region, which must agree to float32 precision.
|
||||
n = min(ref_audio.shape[-1], audio.shape[-1])
|
||||
assert n > 0
|
||||
np.testing.assert_allclose(ref_audio[:n], audio[:n], atol=1e-4)
|
||||
@@ -201,6 +207,17 @@ def test_load_audio_default_preserves_vorbis_length(dummy_audio_bytes):
|
||||
assert audio.shape == expected.shape
|
||||
|
||||
|
||||
def test_load_audio_torchcodec_preserves_vorbis_length(dummy_audio_bytes):
|
||||
"""Torchcodec must trim Vorbis trailing padding even on FFmpeg < 5.0,
|
||||
matching the soundfile reference exactly."""
|
||||
pytest.importorskip("torchcodec")
|
||||
expected, expected_sr = load_audio_soundfile(BytesIO(dummy_audio_bytes), sr=None)
|
||||
audio, sr = load_audio_torchcodec(BytesIO(dummy_audio_bytes), sr=None)
|
||||
assert sr == expected_sr
|
||||
assert audio.shape == expected.shape
|
||||
np.testing.assert_allclose(audio, expected, atol=1e-4)
|
||||
|
||||
|
||||
def test_load_audio_unknown_backend_rejected(dummy_audio_bytes):
|
||||
"""An unknown backend must fail loudly instead of silently degrading."""
|
||||
with pytest.raises(ValueError, match="Unknown audio backend"):
|
||||
|
||||
Reference in New Issue
Block a user