-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtorch_audio_backend.py
More file actions
111 lines (88 loc) · 3.62 KB
/
torch_audio_backend.py
File metadata and controls
111 lines (88 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Library for checking the torchaudio backend.
Authors
-------
* Mirco Ravanelli 2021
* Adel Moumen 2025
"""
import platform
from typing import Optional
import torchaudio
from speechbrain.utils.logger import get_logger
logger = get_logger(__name__)
def try_parse_torchaudio_major_version() -> Optional[tuple]:
"""Tries parsing the torchaudio major and minor version.
Returns
-------
tuple or None
A (major, minor) tuple, otherwise ``None``.
"""
if not hasattr(torchaudio, "__version__"):
return None
version_split = torchaudio.__version__.split(".")
# expect in format x.y.z whatever; we care only about x
if len(version_split) <= 2:
# not sure how to parse this
return None
try:
major_version = int(version_split[0])
minor_version = int(version_split[1])
except Exception:
return None
return major_version, minor_version
def check_torchaudio_backend():
"""Checks the torchaudio backend and sets it to soundfile if
windows is detected.
"""
result = try_parse_torchaudio_major_version()
if result is None:
logger.warning(
"Failed to detect torchaudio major version; unsure how to check your setup. We recommend that you keep torchaudio up-to-date."
)
return
torchaudio_major, torchaudio_minor = result
if torchaudio_major >= 2 and torchaudio_minor >= 1:
# list_audio_backends() was removed in torchaudio 2.9+
# In 2.9+, audio loading is handled by torchcodec
if hasattr(torchaudio, "list_audio_backends"):
available_backends = torchaudio.list_audio_backends()
if len(available_backends) == 0:
logger.warning(
"SpeechBrain could not find any working torchaudio backend. Audio files may fail to load. Follow this link for instructions and troubleshooting: https://speechbrain.readthedocs.io/en/latest/audioloading.html"
)
else:
# torchaudio 2.9+ - list_audio_backends() removed, audio loading handled by torchcodec
logger.debug(
"torchaudio 2.9+ detected - audio backend checking skipped (handled by torchcodec)"
)
else:
logger.warning(
"This version of torchaudio is old. SpeechBrain no longer tries using the torchaudio global backend mechanism in recipes, so if you encounter issues, update torchaudio to >=2.1.0."
)
current_system = platform.system()
if current_system == "Windows":
logger.warning(
'Switched audio backend to "soundfile" because you are running Windows and you are running an old torchaudio version.'
)
torchaudio.set_audio_backend("soundfile")
def validate_backend(backend):
"""
Validates the specified audio backend.
Parameters
----------
backend : str or None
The name of the backend to validate. Must be one of [None, 'ffmpeg', 'sox', 'soundfile'].
Raises
------
ValueError
If the `backend` is not one of the allowed values.
"""
allowed_backends = [None, "ffmpeg", "sox", "soundfile"]
if backend not in allowed_backends:
# Check if list_audio_backends() exists (removed in torchaudio 2.9+)
if hasattr(torchaudio, "list_audio_backends"):
available_backends_msg = f"Available backends on your system: {torchaudio.list_audio_backends()}"
else:
available_backends_msg = "Using torchaudio 2.9+ with torchcodec"
raise ValueError(
f"backend must be one of {allowed_backends}. {available_backends_msg}"
)