Unverified Commit 886c59b1 authored by yangarbiter's avatar yangarbiter Committed by GitHub
Browse files

[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.
parent d6ae55c6
......@@ -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,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment