Skip to content

Commit

Permalink
[BC-Breaking] Default to PCM_16 for flac on soundfile backend (pytorc…
Browse files Browse the repository at this point in the history
…h#1604)

* [BC-Breaking] Default to PCM_16 for flac on soundfile backend

Resolving pytorch#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.
  • Loading branch information
yangarbiter authored Jul 12, 2021
1 parent d6ae55c commit 886c59b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions torchaudio/backend/soundfile_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 886c59b

Please sign in to comment.