Source code for audiofile.core.io
-import os
+from __future__ import annotations
+
+import os
import tempfile
-import typing
import numpy as np
import soundfile
@@ -161,8 +162,8 @@ Source code for audiofile.core.io
[docs]def convert_to_wav(
infile: str,
outfile: str = None,
- offset: typing.Union[float, int, str, np.timedelta64] = None,
- duration: typing.Union[float, int, str, np.timedelta64] = None,
+ offset: float | int | str | np.timedelta64 = None,
+ duration: float | int | str | np.timedelta64 = None,
bit_depth: int = 16,
normalize: bool = False,
overwrite: bool = False,
@@ -265,12 +266,12 @@ Source code for audiofile.core.io
[docs]def read(
file: str,
- duration: typing.Union[float, int, str, np.timedelta64] = None,
- offset: typing.Union[float, int, str, np.timedelta64] = None,
+ duration: float | int | str | np.timedelta64 = None,
+ offset: float | int | str | np.timedelta64 = None,
always_2d: bool = False,
dtype: str = "float32",
**kwargs,
-) -> typing.Tuple[np.array, int]:
+) -> tuple[np.array, int]:
"""Read audio file.
It uses :func:`soundfile.read` for WAV, FLAC, MP3, and OGG files.
@@ -396,6 +397,7 @@ Source code for audiofile.core.io
""" # noqa: E501
file = audeer.safe_path(file)
+ sampling_rate = None
# Parse offset and duration values
if (
@@ -519,9 +521,6 @@ Source code for audiofile.core.io
# the returned magnitude
# (https://github.com/librosa/librosa/issues/811).
#
- # It might be the case that MP3 files will be supported by soundfile in
- # the future as well. For a discussion on MP3 support in the underlying
- # libsndfile see https://github.com/erikd/libsndfile/issues/258.
with tempfile.TemporaryDirectory(prefix="audiofile") as tmpdir:
tmpfile = os.path.join(tmpdir, "tmp.wav")
# offset and duration have to be given in seconds
@@ -529,7 +528,16 @@ Source code for audiofile.core.io
offset /= sampling_rate
if duration is not None and duration != 0:
duration /= sampling_rate
- convert(file, tmpfile, offset, duration)
+ if sampling_rate is None:
+ # Infer sampling rate using mediainfo before conversion,
+ # as ffmpeg does ignore the original sampling rate for opus files,
+ # see:
+ # * https://trac.ffmpeg.org/ticket/5240
+ # * https://github.com/audeering/audiofile/issues/157
+ from audiofile.core.info import sampling_rate as get_sampling_rate
+
+ sampling_rate = get_sampling_rate(file)
+ convert(file, tmpfile, offset, duration, sampling_rate)
signal, sampling_rate = soundfile.read(
tmpfile,
dtype=dtype,
@@ -658,7 +666,7 @@ Source code for audiofile.core.io
- Built with Sphinx on 2024/07/26 using the audEERING theme
+ Built with Sphinx on 2025/01/03 using the audEERING theme
@@ -666,7 +674,7 @@ Source code for audiofile.core.io
- © 2018-2024 audEERING GmbH
+ © 2018-2025 audEERING GmbH
diff --git a/_modules/index.html b/_modules/index.html
index 2fa5415..f8401f4 100644
--- a/_modules/index.html
+++ b/_modules/index.html
@@ -66,7 +66,7 @@
- v1.5.0
+ v1.5.1
@@ -157,7 +157,7 @@ All modules for which code is available
- Built with Sphinx on 2024/07/26 using the audEERING theme
+ Built with Sphinx on 2025/01/03 using the audEERING theme
@@ -165,7 +165,7 @@ All modules for which code is available
- © 2018-2024 audEERING GmbH
+ © 2018-2025 audEERING GmbH
diff --git a/api/audiofile.bit_depth.html b/api/audiofile.bit_depth.html
index aa29c48..f03eff9 100644
--- a/api/audiofile.bit_depth.html
+++ b/api/audiofile.bit_depth.html
@@ -68,7 +68,7 @@
- v1.5.0
+ v1.5.1
@@ -164,16 +164,16 @@ bit_depth()None is returned as they have a varying bit depth.
- Parameters
-file (str
) – file name of input audio file
+file (str
) – file name of input audio file
- Return type
--
+
-
- Returns
bit depth of audio file
- Raises
-RuntimeError – if file
is missing,
+
RuntimeError – if file
is missing,
broken or format is not supported
@@ -209,7 +209,7 @@ bit_depth()Sphinx on 2024/07/26 using the audEERING theme
+ Built with Sphinx on 2025/01/03 using the audEERING theme
@@ -217,7 +217,7 @@ bit_depth()
- © 2018-2024 audEERING GmbH
+ © 2018-2025 audEERING GmbH
diff --git a/api/audiofile.channels.html b/api/audiofile.channels.html
index e5282c3..6568ba4 100644
--- a/api/audiofile.channels.html
+++ b/api/audiofile.channels.html
@@ -68,7 +68,7 @@
- v1.5.0
+ v1.5.1
@@ -162,19 +162,19 @@ channels()
Parameters
-file (str
) – file name of input audio file
+file (str
) – file name of input audio file
Return type
-
+
Returns
number of channels in audio file
Raises
-FileNotFoundError – if mediainfo binary is needed,
+
FileNotFoundError – if mediainfo binary is needed,
but cannot be found
-RuntimeError – if file
is missing,
+
RuntimeError – if file
is missing,
broken or format is not supported
@@ -211,7 +211,7 @@ channels()Sphinx on 2024/07/26 using the audEERING theme
+ Built with Sphinx on 2025/01/03 using the audEERING theme
@@ -219,7 +219,7 @@ channels()
- © 2018-2024 audEERING GmbH
+ © 2018-2025 audEERING GmbH
diff --git a/api/audiofile.convert_to_wav.html b/api/audiofile.convert_to_wav.html
index 16001da..3d29903 100644
--- a/api/audiofile.convert_to_wav.html
+++ b/api/audiofile.convert_to_wav.html
@@ -68,7 +68,7 @@
- v1.5.0
+ v1.5.1
@@ -191,35 +191,35 @@ convert_to_wav()
Parameters
-infile (str
) – audio/video file name
-infile (str
) – audio/video file name
+outfile (Optional
[str
]) – WAV file name.
If None
same path as infile
but file extension is replaced by 'wav'
-duration (Union
[float
, int
, str
, timedelta64
, None
]) – return only a specified duration
-offset (Union
[float
, int
, str
, timedelta64
, None
]) – start reading at offset
-bit_depth (int
) – bit depth of written file in bit,
+
duration (Union
[float
, int
, str
, timedelta64
, None
]) – return only a specified duration
+offset (Union
[float
, int
, str
, timedelta64
, None
]) – start reading at offset
+bit_depth (int
) – bit depth of written file in bit,
can be 8, 16, 24
-normalize (bool
) – normalize audio data before writing
-overwrite (bool
) – force overwriting
+
normalize (bool
) – normalize audio data before writing
+overwrite (bool
) – force overwriting
if outfile
is identical to outfile
kwargs – pass on further arguments to soundfile.write()
Return type
-
+
Returns
absolute path to resulting WAV file
Raises
-FileNotFoundError – if ffmpeg binary is needed,
+
FileNotFoundError – if ffmpeg binary is needed,
but cannot be found
-RuntimeError – if file
is missing,
+
RuntimeError – if file
is missing,
broken or format is not supported
-RuntimeError – if infile
would need to be overwritten
+
RuntimeError – if infile
would need to be overwritten
and overwrite
is False
-ValueError – if duration
is a string
+
ValueError – if duration
is a string
that does not match a valid ‘<value><unit>’ pattern
or the provided unit is not supported
@@ -258,7 +258,7 @@ convert_to_wav()Sphinx on 2024/07/26 using the audEERING theme
+ Built with Sphinx on 2025/01/03 using the audEERING theme
@@ -266,7 +266,7 @@ convert_to_wav()
- © 2018-2024 audEERING GmbH
+ © 2018-2025 audEERING GmbH
diff --git a/api/audiofile.duration.html b/api/audiofile.duration.html
index 223787f..12d416d 100644
--- a/api/audiofile.duration.html
+++ b/api/audiofile.duration.html
@@ -68,7 +68,7 @@
- v1.5.0
+ v1.5.1
@@ -180,22 +180,22 @@ duration()
Parameters
Return type
-
+
Returns
duration in seconds of audio file
Raises
-FileNotFoundError – if ffmpeg or mediainfo binary is needed,
+
FileNotFoundError – if ffmpeg or mediainfo binary is needed,
but cannot be found
-RuntimeError – if file
is missing,
+
RuntimeError – if file
is missing,
broken or format is not supported
@@ -232,7 +232,7 @@ duration()Sphinx on 2024/07/26 using the audEERING theme
+ Built with Sphinx on 2025/01/03 using the audEERING theme
@@ -240,7 +240,7 @@ duration()
- © 2018-2024 audEERING GmbH
+ © 2018-2025 audEERING GmbH
has_video()
Parameters
-file (str
) – file name of input file
+file (str
) – file name of input file
Return type
-
+
Returns
True
if file contains video data
Raises
-FileNotFoundError – if mediainfo binary is needed,
+
FileNotFoundError – if mediainfo binary is needed,
but cannot be found
-RuntimeError – if file
is missing
+
RuntimeError – if file
is missing
and does not end with "wav"
, "flac"
, "mp3"
, "ogg"
@@ -214,7 +214,7 @@ has_video()Sphinx on 2024/07/26 using the audEERING theme
+ Built with Sphinx on 2025/01/03 using the audEERING theme
file (str
) – file name of input file
file (str
) – file name of input file
True
if file contains video data
-
-
FileNotFoundError – if mediainfo binary is needed, +
FileNotFoundError – if mediainfo binary is needed, but cannot be found
-RuntimeError – if
file
is missing +RuntimeError – if
file
is missing and does not end with"wav"
,"flac"
,"mp3"
,"ogg"