From 7c52bff937562e39c4b77af122f1ca9255b02db8 Mon Sep 17 00:00:00 2001 From: whning Date: Fri, 19 Jun 2026 14:38:06 +0800 Subject: [PATCH] Fix #2867 --- soundfile_backend.py | 17 +++++++++++++++++ soundfile_backend_original.py | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 soundfile_backend.py create mode 100644 soundfile_backend_original.py diff --git a/soundfile_backend.py b/soundfile_backend.py new file mode 100644 index 0000000000..84667c488a --- /dev/null +++ b/soundfile_backend.py @@ -0,0 +1,17 @@ + """ + with soundfile.SoundFile(filepath, "r") as file_: + if file_.format != "WAV" or normalize: + # When reading non-WAV formats (e.g. MP3) with normalize=True, + # use the native floating-point dtype for the subtype if available, + # falling back to float32. This ensures correct reading of files + # that require float64 precision (e.g. certain MP3 files with + # DOUBLE subtype). For integer subtypes, float32 is used for + # normalization. + subtype_dtype = _SUBTYPE2DTYPE.get(file_.subtype) + if subtype_dtype in ("float64",): + dtype = "float64" + else: + dtype = "float32" + elif file_.subtype not in _SUBTYPE2DTYPE: + raise ValueError(f"Unsupported subtype: {file_.subtype}") + else: diff --git a/soundfile_backend_original.py b/soundfile_backend_original.py new file mode 100644 index 0000000000..84667c488a --- /dev/null +++ b/soundfile_backend_original.py @@ -0,0 +1,17 @@ + """ + with soundfile.SoundFile(filepath, "r") as file_: + if file_.format != "WAV" or normalize: + # When reading non-WAV formats (e.g. MP3) with normalize=True, + # use the native floating-point dtype for the subtype if available, + # falling back to float32. This ensures correct reading of files + # that require float64 precision (e.g. certain MP3 files with + # DOUBLE subtype). For integer subtypes, float32 is used for + # normalization. + subtype_dtype = _SUBTYPE2DTYPE.get(file_.subtype) + if subtype_dtype in ("float64",): + dtype = "float64" + else: + dtype = "float32" + elif file_.subtype not in _SUBTYPE2DTYPE: + raise ValueError(f"Unsupported subtype: {file_.subtype}") + else: