torchaudio 2.8.0__cp310-cp310-win_amd64.whl → 2.9.0__cp310-cp310-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of torchaudio might be problematic. Click here for more details.
- torchaudio/__init__.py +179 -39
- torchaudio/_extension/__init__.py +1 -14
- torchaudio/_extension/utils.py +0 -47
- torchaudio/_internal/module_utils.py +12 -3
- torchaudio/_torchcodec.py +73 -85
- torchaudio/datasets/cmuarctic.py +1 -1
- torchaudio/datasets/utils.py +1 -1
- torchaudio/functional/__init__.py +0 -2
- torchaudio/functional/_alignment.py +1 -1
- torchaudio/functional/filtering.py +70 -55
- torchaudio/functional/functional.py +26 -60
- torchaudio/lib/_torchaudio.pyd +0 -0
- torchaudio/lib/libtorchaudio.pyd +0 -0
- torchaudio/models/decoder/__init__.py +14 -2
- torchaudio/models/decoder/_ctc_decoder.py +6 -6
- torchaudio/models/decoder/_cuda_ctc_decoder.py +1 -1
- torchaudio/models/squim/objective.py +2 -2
- torchaudio/pipelines/_source_separation_pipeline.py +1 -1
- torchaudio/pipelines/_squim_pipeline.py +2 -2
- torchaudio/pipelines/_tts/utils.py +1 -1
- torchaudio/pipelines/rnnt_pipeline.py +4 -4
- torchaudio/transforms/__init__.py +1 -0
- torchaudio/transforms/_transforms.py +2 -2
- torchaudio/utils/__init__.py +2 -9
- torchaudio/utils/download.py +1 -3
- torchaudio/version.py +2 -2
- {torchaudio-2.8.0.dist-info → torchaudio-2.9.0.dist-info}/METADATA +8 -11
- torchaudio-2.9.0.dist-info/RECORD +85 -0
- {torchaudio-2.8.0.dist-info → torchaudio-2.9.0.dist-info}/top_level.txt +0 -1
- torchaudio/_backend/__init__.py +0 -61
- torchaudio/_backend/backend.py +0 -53
- torchaudio/_backend/common.py +0 -52
- torchaudio/_backend/ffmpeg.py +0 -334
- torchaudio/_backend/soundfile.py +0 -54
- torchaudio/_backend/soundfile_backend.py +0 -457
- torchaudio/_backend/sox.py +0 -91
- torchaudio/_backend/utils.py +0 -350
- torchaudio/backend/__init__.py +0 -8
- torchaudio/backend/_no_backend.py +0 -25
- torchaudio/backend/_sox_io_backend.py +0 -294
- torchaudio/backend/common.py +0 -13
- torchaudio/backend/no_backend.py +0 -14
- torchaudio/backend/soundfile_backend.py +0 -14
- torchaudio/backend/sox_io_backend.py +0 -14
- torchaudio/io/__init__.py +0 -20
- torchaudio/io/_effector.py +0 -347
- torchaudio/io/_playback.py +0 -72
- torchaudio/kaldi_io.py +0 -150
- torchaudio/prototype/__init__.py +0 -0
- torchaudio/prototype/datasets/__init__.py +0 -4
- torchaudio/prototype/datasets/musan.py +0 -68
- torchaudio/prototype/functional/__init__.py +0 -26
- torchaudio/prototype/functional/_dsp.py +0 -441
- torchaudio/prototype/functional/_rir.py +0 -382
- torchaudio/prototype/functional/functional.py +0 -193
- torchaudio/prototype/models/__init__.py +0 -39
- torchaudio/prototype/models/_conformer_wav2vec2.py +0 -801
- torchaudio/prototype/models/_emformer_hubert.py +0 -337
- torchaudio/prototype/models/conv_emformer.py +0 -529
- torchaudio/prototype/models/hifi_gan.py +0 -342
- torchaudio/prototype/models/rnnt.py +0 -717
- torchaudio/prototype/models/rnnt_decoder.py +0 -402
- torchaudio/prototype/pipelines/__init__.py +0 -21
- torchaudio/prototype/pipelines/_vggish/__init__.py +0 -7
- torchaudio/prototype/pipelines/_vggish/_vggish_impl.py +0 -236
- torchaudio/prototype/pipelines/_vggish/_vggish_pipeline.py +0 -83
- torchaudio/prototype/pipelines/hifigan_pipeline.py +0 -233
- torchaudio/prototype/pipelines/rnnt_pipeline.py +0 -58
- torchaudio/prototype/transforms/__init__.py +0 -9
- torchaudio/prototype/transforms/_transforms.py +0 -461
- torchaudio/sox_effects/__init__.py +0 -10
- torchaudio/sox_effects/sox_effects.py +0 -275
- torchaudio/utils/ffmpeg_utils.py +0 -11
- torchaudio/utils/sox_utils.py +0 -118
- torchaudio-2.8.0.dist-info/RECORD +0 -145
- torio/__init__.py +0 -8
- torio/_extension/__init__.py +0 -13
- torio/_extension/utils.py +0 -147
- torio/io/__init__.py +0 -9
- torio/io/_streaming_media_decoder.py +0 -977
- torio/io/_streaming_media_encoder.py +0 -502
- torio/lib/__init__.py +0 -0
- torio/lib/_torio_ffmpeg4.pyd +0 -0
- torio/lib/_torio_ffmpeg5.pyd +0 -0
- torio/lib/_torio_ffmpeg6.pyd +0 -0
- torio/lib/libtorio_ffmpeg4.pyd +0 -0
- torio/lib/libtorio_ffmpeg5.pyd +0 -0
- torio/lib/libtorio_ffmpeg6.pyd +0 -0
- torio/utils/__init__.py +0 -4
- torio/utils/ffmpeg_utils.py +0 -275
- {torchaudio-2.8.0.dist-info → torchaudio-2.9.0.dist-info}/WHEEL +0 -0
- {torchaudio-2.8.0.dist-info → torchaudio-2.9.0.dist-info}/licenses/LICENSE +0 -0
torchaudio/_backend/utils.py
DELETED
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
from functools import lru_cache
|
|
3
|
-
from typing import BinaryIO, Dict, Optional, Tuple, Type, Union
|
|
4
|
-
import warnings
|
|
5
|
-
|
|
6
|
-
import torch
|
|
7
|
-
|
|
8
|
-
from torchaudio._extension import lazy_import_sox_ext
|
|
9
|
-
from torchaudio.io import CodecConfig
|
|
10
|
-
from torio._extension import lazy_import_ffmpeg_ext
|
|
11
|
-
|
|
12
|
-
from . import soundfile_backend
|
|
13
|
-
|
|
14
|
-
from .backend import Backend
|
|
15
|
-
from .common import AudioMetaData
|
|
16
|
-
from .ffmpeg import FFmpegBackend
|
|
17
|
-
from .soundfile import SoundfileBackend
|
|
18
|
-
from .sox import SoXBackend
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
@lru_cache(None)
|
|
22
|
-
def get_available_backends() -> Dict[str, Type[Backend]]:
|
|
23
|
-
backend_specs: Dict[str, Type[Backend]] = {}
|
|
24
|
-
if lazy_import_ffmpeg_ext().is_available():
|
|
25
|
-
backend_specs["ffmpeg"] = FFmpegBackend
|
|
26
|
-
if lazy_import_sox_ext().is_available():
|
|
27
|
-
backend_specs["sox"] = SoXBackend
|
|
28
|
-
if soundfile_backend._IS_SOUNDFILE_AVAILABLE:
|
|
29
|
-
backend_specs["soundfile"] = SoundfileBackend
|
|
30
|
-
return backend_specs
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def get_backend(backend_name, backends) -> Backend:
|
|
34
|
-
if backend := backends.get(backend_name):
|
|
35
|
-
return backend
|
|
36
|
-
else:
|
|
37
|
-
raise ValueError(
|
|
38
|
-
f"Unsupported backend '{backend_name}' specified; ",
|
|
39
|
-
f"please select one of {list(backends.keys())} instead.",
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def get_info_func():
|
|
44
|
-
backends = get_available_backends()
|
|
45
|
-
|
|
46
|
-
def dispatcher(
|
|
47
|
-
uri: Union[BinaryIO, str, os.PathLike], format: Optional[str], backend_name: Optional[str]
|
|
48
|
-
) -> Backend:
|
|
49
|
-
if backend_name is not None:
|
|
50
|
-
return get_backend(backend_name, backends)
|
|
51
|
-
|
|
52
|
-
for backend in backends.values():
|
|
53
|
-
if backend.can_decode(uri, format):
|
|
54
|
-
return backend
|
|
55
|
-
raise RuntimeError(f"Couldn't find appropriate backend to handle uri {uri} and format {format}.")
|
|
56
|
-
|
|
57
|
-
def info(
|
|
58
|
-
uri: Union[BinaryIO, str, os.PathLike],
|
|
59
|
-
format: Optional[str] = None,
|
|
60
|
-
buffer_size: int = 4096,
|
|
61
|
-
backend: Optional[str] = None,
|
|
62
|
-
) -> AudioMetaData:
|
|
63
|
-
"""Get signal information of an audio file.
|
|
64
|
-
|
|
65
|
-
Note:
|
|
66
|
-
When the input type is file-like object, this function cannot
|
|
67
|
-
get the correct length (``num_samples``) for certain formats,
|
|
68
|
-
such as ``vorbis``.
|
|
69
|
-
In this case, the value of ``num_samples`` is ``0``.
|
|
70
|
-
|
|
71
|
-
Args:
|
|
72
|
-
uri (path-like object or file-like object):
|
|
73
|
-
Source of audio data. The following types are accepted:
|
|
74
|
-
|
|
75
|
-
* ``path-like``: File path or URL.
|
|
76
|
-
* ``file-like``: Object with ``read(size: int) -> bytes`` method,
|
|
77
|
-
which returns byte string of at most ``size`` length.
|
|
78
|
-
|
|
79
|
-
format (str or None, optional):
|
|
80
|
-
If not ``None``, interpreted as hint that may allow backend to override the detected format.
|
|
81
|
-
(Default: ``None``)
|
|
82
|
-
|
|
83
|
-
buffer_size (int, optional):
|
|
84
|
-
Size of buffer to use when processing file-like objects, in bytes. (Default: ``4096``)
|
|
85
|
-
|
|
86
|
-
backend (str or None, optional):
|
|
87
|
-
I/O backend to use.
|
|
88
|
-
If ``None``, function selects backend given input and available backends.
|
|
89
|
-
Otherwise, must be one of [``"ffmpeg"``, ``"sox"``, ``"soundfile"``],
|
|
90
|
-
with the corresponding backend available.
|
|
91
|
-
(Default: ``None``)
|
|
92
|
-
|
|
93
|
-
.. seealso::
|
|
94
|
-
:ref:`backend`
|
|
95
|
-
|
|
96
|
-
Returns:
|
|
97
|
-
AudioMetaData
|
|
98
|
-
"""
|
|
99
|
-
backend = dispatcher(uri, format, backend)
|
|
100
|
-
return backend.info(uri, format, buffer_size)
|
|
101
|
-
|
|
102
|
-
return info
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
def get_load_func():
|
|
106
|
-
backends = get_available_backends()
|
|
107
|
-
|
|
108
|
-
def dispatcher(
|
|
109
|
-
uri: Union[BinaryIO, str, os.PathLike], format: Optional[str], backend_name: Optional[str]
|
|
110
|
-
) -> Backend:
|
|
111
|
-
if backend_name is not None:
|
|
112
|
-
return get_backend(backend_name, backends)
|
|
113
|
-
|
|
114
|
-
for backend in backends.values():
|
|
115
|
-
if backend.can_decode(uri, format):
|
|
116
|
-
return backend
|
|
117
|
-
raise RuntimeError(f"Couldn't find appropriate backend to handle uri {uri} and format {format}.")
|
|
118
|
-
|
|
119
|
-
def load(
|
|
120
|
-
uri: Union[BinaryIO, str, os.PathLike],
|
|
121
|
-
frame_offset: int = 0,
|
|
122
|
-
num_frames: int = -1,
|
|
123
|
-
normalize: bool = True,
|
|
124
|
-
channels_first: bool = True,
|
|
125
|
-
format: Optional[str] = None,
|
|
126
|
-
buffer_size: int = 4096,
|
|
127
|
-
backend: Optional[str] = None,
|
|
128
|
-
) -> Tuple[torch.Tensor, int]:
|
|
129
|
-
"""Load audio data from source.
|
|
130
|
-
|
|
131
|
-
.. warning::
|
|
132
|
-
In 2.9, this function's implementation will be changed to use
|
|
133
|
-
:func:`~torchaudio.load_with_torchcodec` under the hood. Some
|
|
134
|
-
parameters like ``normalize``, ``format``, ``buffer_size``, and
|
|
135
|
-
``backend`` will be ignored. We recommend that you port your code to
|
|
136
|
-
rely directly on TorchCodec's decoder instead:
|
|
137
|
-
https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.decoders.AudioDecoder.html#torchcodec.decoders.AudioDecoder.
|
|
138
|
-
|
|
139
|
-
By default (``normalize=True``, ``channels_first=True``), this function returns Tensor with
|
|
140
|
-
``float32`` dtype, and the shape of `[channel, time]`.
|
|
141
|
-
|
|
142
|
-
Note:
|
|
143
|
-
The formats this function can handle depend on the availability of backends.
|
|
144
|
-
Please use the following functions to fetch the supported formats.
|
|
145
|
-
|
|
146
|
-
- FFmpeg: :py:func:`torchaudio.utils.ffmpeg_utils.get_audio_decoders`
|
|
147
|
-
- Sox: :py:func:`torchaudio.utils.sox_utils.list_read_formats`
|
|
148
|
-
- SoundFile: Refer to `the official document <https://pysoundfile.readthedocs.io/>`__.
|
|
149
|
-
|
|
150
|
-
.. warning::
|
|
151
|
-
|
|
152
|
-
``normalize`` argument does not perform volume normalization.
|
|
153
|
-
It only converts the sample type to `torch.float32` from the native sample
|
|
154
|
-
type.
|
|
155
|
-
|
|
156
|
-
When the input format is WAV with integer type, such as 32-bit signed integer, 16-bit
|
|
157
|
-
signed integer, 24-bit signed integer, and 8-bit unsigned integer, by providing ``normalize=False``,
|
|
158
|
-
this function can return integer Tensor, where the samples are expressed within the whole range
|
|
159
|
-
of the corresponding dtype, that is, ``int32`` tensor for 32-bit signed PCM,
|
|
160
|
-
``int16`` for 16-bit signed PCM and ``uint8`` for 8-bit unsigned PCM. Since torch does not
|
|
161
|
-
support ``int24`` dtype, 24-bit signed PCM are converted to ``int32`` tensors.
|
|
162
|
-
|
|
163
|
-
``normalize`` argument has no effect on 32-bit floating-point WAV and other formats, such as
|
|
164
|
-
``flac`` and ``mp3``.
|
|
165
|
-
|
|
166
|
-
For these formats, this function always returns ``float32`` Tensor with values.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
Args:
|
|
170
|
-
uri (path-like object or file-like object):
|
|
171
|
-
Source of audio data.
|
|
172
|
-
frame_offset (int, optional):
|
|
173
|
-
Number of frames to skip before start reading data.
|
|
174
|
-
num_frames (int, optional):
|
|
175
|
-
Maximum number of frames to read. ``-1`` reads all the remaining samples,
|
|
176
|
-
starting from ``frame_offset``.
|
|
177
|
-
This function may return the less number of frames if there is not enough
|
|
178
|
-
frames in the given file.
|
|
179
|
-
normalize (bool, optional):
|
|
180
|
-
When ``True``, this function converts the native sample type to ``float32``.
|
|
181
|
-
Default: ``True``.
|
|
182
|
-
|
|
183
|
-
If input file is integer WAV, giving ``False`` will change the resulting Tensor type to
|
|
184
|
-
integer type.
|
|
185
|
-
This argument has no effect for formats other than integer WAV type.
|
|
186
|
-
|
|
187
|
-
channels_first (bool, optional):
|
|
188
|
-
When True, the returned Tensor has dimension `[channel, time]`.
|
|
189
|
-
Otherwise, the returned Tensor's dimension is `[time, channel]`.
|
|
190
|
-
|
|
191
|
-
format (str or None, optional):
|
|
192
|
-
If not ``None``, interpreted as hint that may allow backend to override the detected format.
|
|
193
|
-
(Default: ``None``)
|
|
194
|
-
|
|
195
|
-
buffer_size (int, optional):
|
|
196
|
-
Size of buffer to use when processing file-like objects, in bytes. (Default: ``4096``)
|
|
197
|
-
|
|
198
|
-
backend (str or None, optional):
|
|
199
|
-
I/O backend to use.
|
|
200
|
-
If ``None``, function selects backend given input and available backends.
|
|
201
|
-
Otherwise, must be one of [``"ffmpeg"``, ``"sox"``, ``"soundfile"``],
|
|
202
|
-
with the corresponding backend being available. (Default: ``None``)
|
|
203
|
-
|
|
204
|
-
.. seealso::
|
|
205
|
-
:ref:`backend`
|
|
206
|
-
|
|
207
|
-
Returns:
|
|
208
|
-
(torch.Tensor, int): Resulting Tensor and sample rate.
|
|
209
|
-
If the input file has integer wav format and normalization is off, then it has
|
|
210
|
-
integer type, else ``float32`` type. If ``channels_first=True``, it has
|
|
211
|
-
`[channel, time]` else `[time, channel]`.
|
|
212
|
-
"""
|
|
213
|
-
warnings.warn(
|
|
214
|
-
"In 2.9, this function's implementation will be changed to use "
|
|
215
|
-
"torchaudio.load_with_torchcodec` under the hood. Some "
|
|
216
|
-
"parameters like ``normalize``, ``format``, ``buffer_size``, and "
|
|
217
|
-
"``backend`` will be ignored. We recommend that you port your code to "
|
|
218
|
-
"rely directly on TorchCodec's decoder instead: "
|
|
219
|
-
"https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.decoders.AudioDecoder.html#torchcodec.decoders.AudioDecoder."
|
|
220
|
-
)
|
|
221
|
-
backend = dispatcher(uri, format, backend)
|
|
222
|
-
return backend.load(uri, frame_offset, num_frames, normalize, channels_first, format, buffer_size)
|
|
223
|
-
|
|
224
|
-
return load
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
def get_save_func():
|
|
228
|
-
backends = get_available_backends()
|
|
229
|
-
|
|
230
|
-
def dispatcher(
|
|
231
|
-
uri: Union[BinaryIO, str, os.PathLike], format: Optional[str], backend_name: Optional[str]
|
|
232
|
-
) -> Backend:
|
|
233
|
-
if backend_name is not None:
|
|
234
|
-
return get_backend(backend_name, backends)
|
|
235
|
-
|
|
236
|
-
for backend in backends.values():
|
|
237
|
-
if backend.can_encode(uri, format):
|
|
238
|
-
return backend
|
|
239
|
-
raise RuntimeError(f"Couldn't find appropriate backend to handle uri {uri} and format {format}.")
|
|
240
|
-
|
|
241
|
-
def save(
|
|
242
|
-
uri: Union[BinaryIO, str, os.PathLike],
|
|
243
|
-
src: torch.Tensor,
|
|
244
|
-
sample_rate: int,
|
|
245
|
-
channels_first: bool = True,
|
|
246
|
-
format: Optional[str] = None,
|
|
247
|
-
encoding: Optional[str] = None,
|
|
248
|
-
bits_per_sample: Optional[int] = None,
|
|
249
|
-
buffer_size: int = 4096,
|
|
250
|
-
backend: Optional[str] = None,
|
|
251
|
-
compression: Optional[Union[CodecConfig, float, int]] = None,
|
|
252
|
-
):
|
|
253
|
-
"""Save audio data to file.
|
|
254
|
-
|
|
255
|
-
.. warning::
|
|
256
|
-
In 2.9, this function's implementation will be changed to use
|
|
257
|
-
:func:`~torchaudio.save_with_torchcodec` under the hood. Some
|
|
258
|
-
parameters like format, encoding, bits_per_sample, buffer_size, and
|
|
259
|
-
``backend`` will be ignored. We recommend that you port your code to
|
|
260
|
-
rely directly on TorchCodec's decoder instead:
|
|
261
|
-
https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.encoders.AudioEncoder
|
|
262
|
-
|
|
263
|
-
Note:
|
|
264
|
-
The formats this function can handle depend on the availability of backends.
|
|
265
|
-
Please use the following functions to fetch the supported formats.
|
|
266
|
-
|
|
267
|
-
- FFmpeg: :py:func:`torchaudio.utils.ffmpeg_utils.get_audio_encoders`
|
|
268
|
-
- Sox: :py:func:`torchaudio.utils.sox_utils.list_write_formats`
|
|
269
|
-
- SoundFile: Refer to `the official document <https://pysoundfile.readthedocs.io/>`__.
|
|
270
|
-
|
|
271
|
-
Args:
|
|
272
|
-
uri (str or pathlib.Path): Path to audio file.
|
|
273
|
-
src (torch.Tensor): Audio data to save. must be 2D tensor.
|
|
274
|
-
sample_rate (int): sampling rate
|
|
275
|
-
channels_first (bool, optional): If ``True``, the given tensor is interpreted as `[channel, time]`,
|
|
276
|
-
otherwise `[time, channel]`.
|
|
277
|
-
format (str or None, optional): Override the audio format.
|
|
278
|
-
When ``uri`` argument is path-like object, audio format is
|
|
279
|
-
inferred from file extension. If the file extension is missing or
|
|
280
|
-
different, you can specify the correct format with this argument.
|
|
281
|
-
|
|
282
|
-
When ``uri`` argument is file-like object,
|
|
283
|
-
this argument is required.
|
|
284
|
-
|
|
285
|
-
Valid values are ``"wav"``, ``"ogg"``, and ``"flac"``.
|
|
286
|
-
encoding (str or None, optional): Changes the encoding for supported formats.
|
|
287
|
-
This argument is effective only for supported formats, i.e.
|
|
288
|
-
``"wav"`` and ``""flac"```. Valid values are
|
|
289
|
-
|
|
290
|
-
- ``"PCM_S"`` (signed integer Linear PCM)
|
|
291
|
-
- ``"PCM_U"`` (unsigned integer Linear PCM)
|
|
292
|
-
- ``"PCM_F"`` (floating point PCM)
|
|
293
|
-
- ``"ULAW"`` (mu-law)
|
|
294
|
-
- ``"ALAW"`` (a-law)
|
|
295
|
-
|
|
296
|
-
bits_per_sample (int or None, optional): Changes the bit depth for the
|
|
297
|
-
supported formats.
|
|
298
|
-
When ``format`` is one of ``"wav"`` and ``"flac"``,
|
|
299
|
-
you can change the bit depth.
|
|
300
|
-
Valid values are ``8``, ``16``, ``24``, ``32`` and ``64``.
|
|
301
|
-
|
|
302
|
-
buffer_size (int, optional):
|
|
303
|
-
Size of buffer to use when processing file-like objects, in bytes. (Default: ``4096``)
|
|
304
|
-
|
|
305
|
-
backend (str or None, optional):
|
|
306
|
-
I/O backend to use.
|
|
307
|
-
If ``None``, function selects backend given input and available backends.
|
|
308
|
-
Otherwise, must be one of [``"ffmpeg"``, ``"sox"``, ``"soundfile"``],
|
|
309
|
-
with the corresponding backend being available.
|
|
310
|
-
(Default: ``None``)
|
|
311
|
-
|
|
312
|
-
.. seealso::
|
|
313
|
-
:ref:`backend`
|
|
314
|
-
|
|
315
|
-
compression (CodecConfig, float, int, or None, optional):
|
|
316
|
-
Compression configuration to apply.
|
|
317
|
-
|
|
318
|
-
If the selected backend is FFmpeg, an instance of :py:class:`CodecConfig` must be provided.
|
|
319
|
-
|
|
320
|
-
Otherwise, if the selected backend is SoX, a float or int value corresponding to option ``-C`` of the
|
|
321
|
-
``sox`` command line interface must be provided. For instance:
|
|
322
|
-
|
|
323
|
-
``"mp3"``
|
|
324
|
-
Either bitrate (in ``kbps``) with quality factor, such as ``128.2``, or
|
|
325
|
-
VBR encoding with quality factor such as ``-4.2``. Default: ``-4.5``.
|
|
326
|
-
|
|
327
|
-
``"flac"``
|
|
328
|
-
Whole number from ``0`` to ``8``. ``8`` is default and highest compression.
|
|
329
|
-
|
|
330
|
-
``"ogg"``, ``"vorbis"``
|
|
331
|
-
Number from ``-1`` to ``10``; ``-1`` is the highest compression
|
|
332
|
-
and lowest quality. Default: ``3``.
|
|
333
|
-
|
|
334
|
-
Refer to http://sox.sourceforge.net/soxformat.html for more details.
|
|
335
|
-
|
|
336
|
-
"""
|
|
337
|
-
warnings.warn(
|
|
338
|
-
"In 2.9, this function's implementation will be changed to use "
|
|
339
|
-
"torchaudio.save_with_torchcodec` under the hood. Some "
|
|
340
|
-
"parameters like format, encoding, bits_per_sample, buffer_size, and "
|
|
341
|
-
"``backend`` will be ignored. We recommend that you port your code to "
|
|
342
|
-
"rely directly on TorchCodec's encoder instead: "
|
|
343
|
-
"https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.encoders.AudioEncoder"
|
|
344
|
-
)
|
|
345
|
-
backend = dispatcher(uri, format, backend)
|
|
346
|
-
return backend.save(
|
|
347
|
-
uri, src, sample_rate, channels_first, format, encoding, bits_per_sample, buffer_size, compression
|
|
348
|
-
)
|
|
349
|
-
|
|
350
|
-
return save
|
torchaudio/backend/__init__.py
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
# NOTE:
|
|
2
|
-
# The entire `torchaudio.backend` module is deprecated.
|
|
3
|
-
# New things should be added to `torchaudio._backend`.
|
|
4
|
-
# Only things related to backward compatibility should be placed here.
|
|
5
|
-
|
|
6
|
-
from . import common, no_backend, soundfile_backend, sox_io_backend # noqa
|
|
7
|
-
|
|
8
|
-
__all__ = []
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
from pathlib import Path
|
|
2
|
-
from typing import Callable, Optional, Tuple, Union
|
|
3
|
-
|
|
4
|
-
from torch import Tensor
|
|
5
|
-
from torchaudio import AudioMetaData
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def load(
|
|
9
|
-
filepath: Union[str, Path],
|
|
10
|
-
out: Optional[Tensor] = None,
|
|
11
|
-
normalization: Union[bool, float, Callable] = True,
|
|
12
|
-
channels_first: bool = True,
|
|
13
|
-
num_frames: int = 0,
|
|
14
|
-
offset: int = 0,
|
|
15
|
-
filetype: Optional[str] = None,
|
|
16
|
-
) -> Tuple[Tensor, int]:
|
|
17
|
-
raise RuntimeError("No audio I/O backend is available.")
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, channels_first: bool = True) -> None:
|
|
21
|
-
raise RuntimeError("No audio I/O backend is available.")
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def info(filepath: str) -> AudioMetaData:
|
|
25
|
-
raise RuntimeError("No audio I/O backend is available.")
|
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
from typing import Optional, Tuple
|
|
3
|
-
|
|
4
|
-
import torch
|
|
5
|
-
import torchaudio
|
|
6
|
-
from torchaudio import AudioMetaData
|
|
7
|
-
|
|
8
|
-
sox_ext = torchaudio._extension.lazy_import_sox_ext()
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def info(
|
|
12
|
-
filepath: str,
|
|
13
|
-
format: Optional[str] = None,
|
|
14
|
-
) -> AudioMetaData:
|
|
15
|
-
"""Get signal information of an audio file.
|
|
16
|
-
|
|
17
|
-
Args:
|
|
18
|
-
filepath (str):
|
|
19
|
-
Source of audio data.
|
|
20
|
-
|
|
21
|
-
format (str or None, optional):
|
|
22
|
-
Override the format detection with the given format.
|
|
23
|
-
Providing the argument might help when libsox can not infer the format
|
|
24
|
-
from header or extension.
|
|
25
|
-
|
|
26
|
-
Returns:
|
|
27
|
-
AudioMetaData: Metadata of the given audio.
|
|
28
|
-
"""
|
|
29
|
-
if not torch.jit.is_scripting():
|
|
30
|
-
if hasattr(filepath, "read"):
|
|
31
|
-
raise RuntimeError("sox_io backend does not support file-like object.")
|
|
32
|
-
filepath = os.fspath(filepath)
|
|
33
|
-
sinfo = sox_ext.get_info(filepath, format)
|
|
34
|
-
return AudioMetaData(*sinfo)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def load(
|
|
38
|
-
filepath: str,
|
|
39
|
-
frame_offset: int = 0,
|
|
40
|
-
num_frames: int = -1,
|
|
41
|
-
normalize: bool = True,
|
|
42
|
-
channels_first: bool = True,
|
|
43
|
-
format: Optional[str] = None,
|
|
44
|
-
) -> Tuple[torch.Tensor, int]:
|
|
45
|
-
"""Load audio data from file.
|
|
46
|
-
|
|
47
|
-
Note:
|
|
48
|
-
This function can handle all the codecs that underlying libsox can handle,
|
|
49
|
-
however it is tested on the following formats;
|
|
50
|
-
|
|
51
|
-
* WAV, AMB
|
|
52
|
-
|
|
53
|
-
* 32-bit floating-point
|
|
54
|
-
* 32-bit signed integer
|
|
55
|
-
* 24-bit signed integer
|
|
56
|
-
* 16-bit signed integer
|
|
57
|
-
* 8-bit unsigned integer (WAV only)
|
|
58
|
-
|
|
59
|
-
* MP3
|
|
60
|
-
* FLAC
|
|
61
|
-
* OGG/VORBIS
|
|
62
|
-
* OPUS
|
|
63
|
-
* SPHERE
|
|
64
|
-
* AMR-NB
|
|
65
|
-
|
|
66
|
-
To load ``MP3``, ``FLAC``, ``OGG/VORBIS``, ``OPUS`` and other codecs ``libsox`` does not
|
|
67
|
-
handle natively, your installation of ``torchaudio`` has to be linked to ``libsox``
|
|
68
|
-
and corresponding codec libraries such as ``libmad`` or ``libmp3lame`` etc.
|
|
69
|
-
|
|
70
|
-
By default (``normalize=True``, ``channels_first=True``), this function returns Tensor with
|
|
71
|
-
``float32`` dtype, and the shape of `[channel, time]`.
|
|
72
|
-
|
|
73
|
-
.. warning::
|
|
74
|
-
|
|
75
|
-
``normalize`` argument does not perform volume normalization.
|
|
76
|
-
It only converts the sample type to `torch.float32` from the native sample
|
|
77
|
-
type.
|
|
78
|
-
|
|
79
|
-
When the input format is WAV with integer type, such as 32-bit signed integer, 16-bit
|
|
80
|
-
signed integer, 24-bit signed integer, and 8-bit unsigned integer, by providing ``normalize=False``,
|
|
81
|
-
this function can return integer Tensor, where the samples are expressed within the whole range
|
|
82
|
-
of the corresponding dtype, that is, ``int32`` tensor for 32-bit signed PCM,
|
|
83
|
-
``int16`` for 16-bit signed PCM and ``uint8`` for 8-bit unsigned PCM. Since torch does not
|
|
84
|
-
support ``int24`` dtype, 24-bit signed PCM are converted to ``int32`` tensors.
|
|
85
|
-
|
|
86
|
-
``normalize`` argument has no effect on 32-bit floating-point WAV and other formats, such as
|
|
87
|
-
``flac`` and ``mp3``.
|
|
88
|
-
|
|
89
|
-
For these formats, this function always returns ``float32`` Tensor with values.
|
|
90
|
-
|
|
91
|
-
Args:
|
|
92
|
-
filepath (path-like object): Source of audio data.
|
|
93
|
-
frame_offset (int):
|
|
94
|
-
Number of frames to skip before start reading data.
|
|
95
|
-
num_frames (int, optional):
|
|
96
|
-
Maximum number of frames to read. ``-1`` reads all the remaining samples,
|
|
97
|
-
starting from ``frame_offset``.
|
|
98
|
-
This function may return the less number of frames if there is not enough
|
|
99
|
-
frames in the given file.
|
|
100
|
-
normalize (bool, optional):
|
|
101
|
-
When ``True``, this function converts the native sample type to ``float32``.
|
|
102
|
-
Default: ``True``.
|
|
103
|
-
|
|
104
|
-
If input file is integer WAV, giving ``False`` will change the resulting Tensor type to
|
|
105
|
-
integer type.
|
|
106
|
-
This argument has no effect for formats other than integer WAV type.
|
|
107
|
-
|
|
108
|
-
channels_first (bool, optional):
|
|
109
|
-
When True, the returned Tensor has dimension `[channel, time]`.
|
|
110
|
-
Otherwise, the returned Tensor's dimension is `[time, channel]`.
|
|
111
|
-
format (str or None, optional):
|
|
112
|
-
Override the format detection with the given format.
|
|
113
|
-
Providing the argument might help when libsox can not infer the format
|
|
114
|
-
from header or extension.
|
|
115
|
-
|
|
116
|
-
Returns:
|
|
117
|
-
(torch.Tensor, int): Resulting Tensor and sample rate.
|
|
118
|
-
If the input file has integer wav format and ``normalize=False``, then it has
|
|
119
|
-
integer type, else ``float32`` type. If ``channels_first=True``, it has
|
|
120
|
-
`[channel, time]` else `[time, channel]`.
|
|
121
|
-
"""
|
|
122
|
-
if not torch.jit.is_scripting():
|
|
123
|
-
if hasattr(filepath, "read"):
|
|
124
|
-
raise RuntimeError("sox_io backend does not support file-like object.")
|
|
125
|
-
filepath = os.fspath(filepath)
|
|
126
|
-
return sox_ext.load_audio_file(filepath, frame_offset, num_frames, normalize, channels_first, format)
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
def save(
|
|
130
|
-
filepath: str,
|
|
131
|
-
src: torch.Tensor,
|
|
132
|
-
sample_rate: int,
|
|
133
|
-
channels_first: bool = True,
|
|
134
|
-
compression: Optional[float] = None,
|
|
135
|
-
format: Optional[str] = None,
|
|
136
|
-
encoding: Optional[str] = None,
|
|
137
|
-
bits_per_sample: Optional[int] = None,
|
|
138
|
-
):
|
|
139
|
-
"""Save audio data to file.
|
|
140
|
-
|
|
141
|
-
Args:
|
|
142
|
-
filepath (path-like object): Path to save file.
|
|
143
|
-
src (torch.Tensor): Audio data to save. must be 2D tensor.
|
|
144
|
-
sample_rate (int): sampling rate
|
|
145
|
-
channels_first (bool, optional): If ``True``, the given tensor is interpreted as `[channel, time]`,
|
|
146
|
-
otherwise `[time, channel]`.
|
|
147
|
-
compression (float or None, optional): Used for formats other than WAV.
|
|
148
|
-
This corresponds to ``-C`` option of ``sox`` command.
|
|
149
|
-
|
|
150
|
-
``"mp3"``
|
|
151
|
-
Either bitrate (in ``kbps``) with quality factor, such as ``128.2``, or
|
|
152
|
-
VBR encoding with quality factor such as ``-4.2``. Default: ``-4.5``.
|
|
153
|
-
|
|
154
|
-
``"flac"``
|
|
155
|
-
Whole number from ``0`` to ``8``. ``8`` is default and highest compression.
|
|
156
|
-
|
|
157
|
-
``"ogg"``, ``"vorbis"``
|
|
158
|
-
Number from ``-1`` to ``10``; ``-1`` is the highest compression
|
|
159
|
-
and lowest quality. Default: ``3``.
|
|
160
|
-
|
|
161
|
-
See the detail at http://sox.sourceforge.net/soxformat.html.
|
|
162
|
-
format (str or None, optional): Override the audio format.
|
|
163
|
-
When ``filepath`` argument is path-like object, audio format is infered from
|
|
164
|
-
file extension. If file extension is missing or different, you can specify the
|
|
165
|
-
correct format with this argument.
|
|
166
|
-
|
|
167
|
-
When ``filepath`` argument is file-like object, this argument is required.
|
|
168
|
-
|
|
169
|
-
Valid values are ``"wav"``, ``"mp3"``, ``"ogg"``, ``"vorbis"``, ``"amr-nb"``,
|
|
170
|
-
``"amb"``, ``"flac"``, ``"sph"``, ``"gsm"``, and ``"htk"``.
|
|
171
|
-
|
|
172
|
-
encoding (str or None, optional): Changes the encoding for the supported formats.
|
|
173
|
-
This argument is effective only for supported formats, such as ``"wav"``, ``""amb"``
|
|
174
|
-
and ``"sph"``. Valid values are;
|
|
175
|
-
|
|
176
|
-
- ``"PCM_S"`` (signed integer Linear PCM)
|
|
177
|
-
- ``"PCM_U"`` (unsigned integer Linear PCM)
|
|
178
|
-
- ``"PCM_F"`` (floating point PCM)
|
|
179
|
-
- ``"ULAW"`` (mu-law)
|
|
180
|
-
- ``"ALAW"`` (a-law)
|
|
181
|
-
|
|
182
|
-
Default values
|
|
183
|
-
If not provided, the default value is picked based on ``format`` and ``bits_per_sample``.
|
|
184
|
-
|
|
185
|
-
``"wav"``, ``"amb"``
|
|
186
|
-
- | If both ``encoding`` and ``bits_per_sample`` are not provided, the ``dtype`` of the
|
|
187
|
-
| Tensor is used to determine the default value.
|
|
188
|
-
|
|
189
|
-
- ``"PCM_U"`` if dtype is ``uint8``
|
|
190
|
-
- ``"PCM_S"`` if dtype is ``int16`` or ``int32``
|
|
191
|
-
- ``"PCM_F"`` if dtype is ``float32``
|
|
192
|
-
|
|
193
|
-
- ``"PCM_U"`` if ``bits_per_sample=8``
|
|
194
|
-
- ``"PCM_S"`` otherwise
|
|
195
|
-
|
|
196
|
-
``"sph"`` format;
|
|
197
|
-
- the default value is ``"PCM_S"``
|
|
198
|
-
|
|
199
|
-
bits_per_sample (int or None, optional): Changes the bit depth for the supported formats.
|
|
200
|
-
When ``format`` is one of ``"wav"``, ``"flac"``, ``"sph"``, or ``"amb"``, you can change the
|
|
201
|
-
bit depth. Valid values are ``8``, ``16``, ``32`` and ``64``.
|
|
202
|
-
|
|
203
|
-
Default Value;
|
|
204
|
-
If not provided, the default values are picked based on ``format`` and ``"encoding"``;
|
|
205
|
-
|
|
206
|
-
``"wav"``, ``"amb"``;
|
|
207
|
-
- | If both ``encoding`` and ``bits_per_sample`` are not provided, the ``dtype`` of the
|
|
208
|
-
| Tensor is used.
|
|
209
|
-
|
|
210
|
-
- ``8`` if dtype is ``uint8``
|
|
211
|
-
- ``16`` if dtype is ``int16``
|
|
212
|
-
- ``32`` if dtype is ``int32`` or ``float32``
|
|
213
|
-
|
|
214
|
-
- ``8`` if ``encoding`` is ``"PCM_U"``, ``"ULAW"`` or ``"ALAW"``
|
|
215
|
-
- ``16`` if ``encoding`` is ``"PCM_S"``
|
|
216
|
-
- ``32`` if ``encoding`` is ``"PCM_F"``
|
|
217
|
-
|
|
218
|
-
``"flac"`` format;
|
|
219
|
-
- the default value is ``24``
|
|
220
|
-
|
|
221
|
-
``"sph"`` format;
|
|
222
|
-
- ``16`` if ``encoding`` is ``"PCM_U"``, ``"PCM_S"``, ``"PCM_F"`` or not provided.
|
|
223
|
-
- ``8`` if ``encoding`` is ``"ULAW"`` or ``"ALAW"``
|
|
224
|
-
|
|
225
|
-
``"amb"`` format;
|
|
226
|
-
- ``8`` if ``encoding`` is ``"PCM_U"``, ``"ULAW"`` or ``"ALAW"``
|
|
227
|
-
- ``16`` if ``encoding`` is ``"PCM_S"`` or not provided.
|
|
228
|
-
- ``32`` if ``encoding`` is ``"PCM_F"``
|
|
229
|
-
|
|
230
|
-
Supported formats/encodings/bit depth/compression are;
|
|
231
|
-
|
|
232
|
-
``"wav"``, ``"amb"``
|
|
233
|
-
- 32-bit floating-point PCM
|
|
234
|
-
- 32-bit signed integer PCM
|
|
235
|
-
- 24-bit signed integer PCM
|
|
236
|
-
- 16-bit signed integer PCM
|
|
237
|
-
- 8-bit unsigned integer PCM
|
|
238
|
-
- 8-bit mu-law
|
|
239
|
-
- 8-bit a-law
|
|
240
|
-
|
|
241
|
-
Note: Default encoding/bit depth is determined by the dtype of the input Tensor.
|
|
242
|
-
|
|
243
|
-
``"mp3"``
|
|
244
|
-
Fixed bit rate (such as 128kHz) and variable bit rate compression.
|
|
245
|
-
Default: VBR with high quality.
|
|
246
|
-
|
|
247
|
-
``"flac"``
|
|
248
|
-
- 8-bit
|
|
249
|
-
- 16-bit
|
|
250
|
-
- 24-bit (default)
|
|
251
|
-
|
|
252
|
-
``"ogg"``, ``"vorbis"``
|
|
253
|
-
- Different quality level. Default: approx. 112kbps
|
|
254
|
-
|
|
255
|
-
``"sph"``
|
|
256
|
-
- 8-bit signed integer PCM
|
|
257
|
-
- 16-bit signed integer PCM
|
|
258
|
-
- 24-bit signed integer PCM
|
|
259
|
-
- 32-bit signed integer PCM (default)
|
|
260
|
-
- 8-bit mu-law
|
|
261
|
-
- 8-bit a-law
|
|
262
|
-
- 16-bit a-law
|
|
263
|
-
- 24-bit a-law
|
|
264
|
-
- 32-bit a-law
|
|
265
|
-
|
|
266
|
-
``"amr-nb"``
|
|
267
|
-
Bitrate ranging from 4.75 kbit/s to 12.2 kbit/s. Default: 4.75 kbit/s
|
|
268
|
-
|
|
269
|
-
``"gsm"``
|
|
270
|
-
Lossy Speech Compression, CPU intensive.
|
|
271
|
-
|
|
272
|
-
``"htk"``
|
|
273
|
-
Uses a default single-channel 16-bit PCM format.
|
|
274
|
-
|
|
275
|
-
Note:
|
|
276
|
-
To save into formats that ``libsox`` does not handle natively, (such as ``"mp3"``,
|
|
277
|
-
``"flac"``, ``"ogg"`` and ``"vorbis"``), your installation of ``torchaudio`` has
|
|
278
|
-
to be linked to ``libsox`` and corresponding codec libraries such as ``libmad``
|
|
279
|
-
or ``libmp3lame`` etc.
|
|
280
|
-
"""
|
|
281
|
-
if not torch.jit.is_scripting():
|
|
282
|
-
if hasattr(filepath, "write"):
|
|
283
|
-
raise RuntimeError("sox_io backend does not handle file-like object.")
|
|
284
|
-
filepath = os.fspath(filepath)
|
|
285
|
-
sox_ext.save_audio_file(
|
|
286
|
-
filepath,
|
|
287
|
-
src,
|
|
288
|
-
sample_rate,
|
|
289
|
-
channels_first,
|
|
290
|
-
compression,
|
|
291
|
-
format,
|
|
292
|
-
encoding,
|
|
293
|
-
bits_per_sample,
|
|
294
|
-
)
|
torchaudio/backend/common.py
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
def __getattr__(name: str):
|
|
2
|
-
if name == "AudioMetaData":
|
|
3
|
-
import warnings
|
|
4
|
-
|
|
5
|
-
warnings.warn(
|
|
6
|
-
"`torchaudio.backend.common.AudioMetaData` has been moved to "
|
|
7
|
-
"`torchaudio.AudioMetaData`. Please update the import path.",
|
|
8
|
-
stacklevel=2,
|
|
9
|
-
)
|
|
10
|
-
from torchaudio import AudioMetaData
|
|
11
|
-
|
|
12
|
-
return AudioMetaData
|
|
13
|
-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|