From 886c59b1384c8e380621ec279ceedc925adea905 Mon Sep 17 00:00:00 2001 From: yangarbiter Date: Mon, 12 Jul 2021 11:03:58 -0700 Subject: [PATCH] [BC-Breaking] Default to PCM_16 for flac on soundfile backend (#1604) * [BC-Breaking] Default to PCM_16 for flac on soundfile backend Resolving https://github.com/pytorch/audio/issues/1592 The test backend/soundfile/save_test.py::TestFileObject::test_fileobj_flac is failing due to the fact that when soundfile.write received subtype=None (for flac files), it would fall back to 'PCM_16'. But in the same time, torchaudio will use 'PCM_24', which would result in distorted signal. This commit fixed this issue by changing the default bit-per-sample of for flac files from 24-bit to 16-bit. --- torchaudio/backend/soundfile_backend.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/torchaudio/backend/soundfile_backend.py b/torchaudio/backend/soundfile_backend.py index afce945026..c5d6fa82a0 100644 --- a/torchaudio/backend/soundfile_backend.py +++ b/torchaudio/backend/soundfile_backend.py @@ -283,7 +283,7 @@ def _get_subtype( if encoding: raise ValueError("flac does not support encoding.") if not bits_per_sample: - return "PCM_24" + return "PCM_16" if bits_per_sample > 24: raise ValueError("flac does not support bits_per_sample > 24.") return "PCM_S8" if bits_per_sample == 8 else f"PCM_{bits_per_sample}" @@ -382,8 +382,8 @@ def save( ``"flac"`` - 8-bit - - 16-bit - - 24-bit (default) + - 16-bit (default) + - 24-bit ``"ogg"``, ``"vorbis"`` - Doesn't accept changing configuration. @@ -416,6 +416,9 @@ def save( if bits_per_sample not in (None, 8, 16, 24, 32, 64): raise ValueError("Invalid bits_per_sample.") + if bits_per_sample == 24: + warnings.warn("Saving audio with 24 bits per sample might warp samples near -1. " + "Using 16 bits per sample might be able to avoid this.") subtype = _get_subtype(src.dtype, ext, encoding, bits_per_sample) # sph is a extension used in TED-LIUM but soundfile does not recognize it as NIST format,