torchaudio 2.0.2__cp38-cp38-manylinux2014_aarch64.whl → 2.1.1__cp38-cp38-manylinux2014_aarch64.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 +22 -3
- torchaudio/_backend/__init__.py +55 -4
- torchaudio/_backend/backend.py +53 -0
- torchaudio/_backend/common.py +52 -0
- torchaudio/_backend/ffmpeg.py +373 -0
- torchaudio/_backend/soundfile.py +54 -0
- torchaudio/_backend/soundfile_backend.py +457 -0
- torchaudio/_backend/sox.py +91 -0
- torchaudio/_backend/utils.py +81 -323
- torchaudio/_extension/__init__.py +55 -36
- torchaudio/_extension/utils.py +109 -17
- torchaudio/_internal/__init__.py +4 -1
- torchaudio/_internal/module_utils.py +37 -6
- torchaudio/backend/__init__.py +7 -11
- torchaudio/backend/_no_backend.py +24 -0
- torchaudio/backend/_sox_io_backend.py +297 -0
- torchaudio/backend/common.py +12 -52
- torchaudio/backend/no_backend.py +11 -21
- torchaudio/backend/soundfile_backend.py +11 -448
- torchaudio/backend/sox_io_backend.py +11 -435
- torchaudio/backend/utils.py +9 -18
- torchaudio/datasets/__init__.py +2 -0
- torchaudio/datasets/cmuarctic.py +1 -1
- torchaudio/datasets/cmudict.py +61 -62
- torchaudio/datasets/dr_vctk.py +1 -1
- torchaudio/datasets/gtzan.py +1 -1
- torchaudio/datasets/librilight_limited.py +1 -1
- torchaudio/datasets/librispeech.py +1 -1
- torchaudio/datasets/librispeech_biasing.py +189 -0
- torchaudio/datasets/libritts.py +1 -1
- torchaudio/datasets/ljspeech.py +1 -1
- torchaudio/datasets/musdb_hq.py +1 -1
- torchaudio/datasets/quesst14.py +1 -1
- torchaudio/datasets/speechcommands.py +1 -1
- torchaudio/datasets/tedlium.py +1 -1
- torchaudio/datasets/vctk.py +1 -1
- torchaudio/datasets/voxceleb1.py +1 -1
- torchaudio/datasets/yesno.py +1 -1
- torchaudio/functional/__init__.py +6 -2
- torchaudio/functional/_alignment.py +128 -0
- torchaudio/functional/filtering.py +69 -92
- torchaudio/functional/functional.py +99 -148
- torchaudio/io/__init__.py +4 -1
- torchaudio/io/_effector.py +347 -0
- torchaudio/io/_stream_reader.py +158 -90
- torchaudio/io/_stream_writer.py +196 -10
- torchaudio/lib/_torchaudio.so +0 -0
- torchaudio/lib/_torchaudio_ffmpeg4.so +0 -0
- torchaudio/lib/_torchaudio_ffmpeg5.so +0 -0
- torchaudio/lib/_torchaudio_ffmpeg6.so +0 -0
- torchaudio/lib/_torchaudio_sox.so +0 -0
- torchaudio/lib/libtorchaudio.so +0 -0
- torchaudio/lib/libtorchaudio_ffmpeg4.so +0 -0
- torchaudio/lib/libtorchaudio_ffmpeg5.so +0 -0
- torchaudio/lib/libtorchaudio_ffmpeg6.so +0 -0
- torchaudio/lib/libtorchaudio_sox.so +0 -0
- torchaudio/models/__init__.py +14 -0
- torchaudio/models/decoder/__init__.py +22 -7
- torchaudio/models/decoder/_ctc_decoder.py +123 -69
- torchaudio/models/decoder/_cuda_ctc_decoder.py +187 -0
- torchaudio/models/rnnt_decoder.py +10 -14
- torchaudio/models/squim/__init__.py +11 -0
- torchaudio/models/squim/objective.py +326 -0
- torchaudio/models/squim/subjective.py +150 -0
- torchaudio/models/wav2vec2/components.py +6 -10
- torchaudio/pipelines/__init__.py +9 -0
- torchaudio/pipelines/_squim_pipeline.py +176 -0
- torchaudio/pipelines/_wav2vec2/aligner.py +87 -0
- torchaudio/pipelines/_wav2vec2/impl.py +198 -68
- torchaudio/pipelines/_wav2vec2/utils.py +120 -0
- torchaudio/sox_effects/sox_effects.py +7 -30
- torchaudio/transforms/__init__.py +2 -0
- torchaudio/transforms/_transforms.py +99 -54
- torchaudio/utils/download.py +2 -2
- torchaudio/utils/ffmpeg_utils.py +20 -15
- torchaudio/utils/sox_utils.py +8 -9
- torchaudio/version.py +2 -2
- torchaudio-2.1.1.dist-info/METADATA +113 -0
- torchaudio-2.1.1.dist-info/RECORD +117 -0
- {torchaudio-2.0.2.dist-info → torchaudio-2.1.1.dist-info}/WHEEL +1 -1
- torchaudio/io/_compat.py +0 -241
- torchaudio/lib/_torchaudio_ffmpeg.so +0 -0
- torchaudio/lib/flashlight_lib_text_decoder.so +0 -0
- torchaudio/lib/flashlight_lib_text_dictionary.so +0 -0
- torchaudio/lib/libflashlight-text.so +0 -0
- torchaudio/lib/libtorchaudio_ffmpeg.so +0 -0
- torchaudio-2.0.2.dist-info/METADATA +0 -26
- torchaudio-2.0.2.dist-info/RECORD +0 -100
- {torchaudio-2.0.2.dist-info → torchaudio-2.1.1.dist-info}/LICENSE +0 -0
- {torchaudio-2.0.2.dist-info → torchaudio-2.1.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import io
|
|
2
|
+
from typing import Iterator, List, Optional
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
from torch import Tensor
|
|
6
|
+
|
|
7
|
+
from ._stream_reader import _get_afilter_desc, StreamReader
|
|
8
|
+
from ._stream_writer import CodecConfig, StreamWriter
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class _StreamingIOBuffer:
|
|
12
|
+
"""Streaming Bytes IO buffer. Data are dropped when read."""
|
|
13
|
+
|
|
14
|
+
def __init__(self):
|
|
15
|
+
self._buffer: List(bytes) = []
|
|
16
|
+
|
|
17
|
+
def write(self, b: bytes):
|
|
18
|
+
if b:
|
|
19
|
+
self._buffer.append(b)
|
|
20
|
+
return len(b)
|
|
21
|
+
|
|
22
|
+
def pop(self, n):
|
|
23
|
+
"""Pop the oldest byte string. It does not necessary return the requested amount"""
|
|
24
|
+
if not self._buffer:
|
|
25
|
+
return b""
|
|
26
|
+
if len(self._buffer[0]) <= n:
|
|
27
|
+
return self._buffer.pop(0)
|
|
28
|
+
ret = self._buffer[0][:n]
|
|
29
|
+
self._buffer[0] = self._buffer[0][n:]
|
|
30
|
+
return ret
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _get_sample_fmt(dtype: torch.dtype):
|
|
34
|
+
types = {
|
|
35
|
+
torch.uint8: "u8",
|
|
36
|
+
torch.int16: "s16",
|
|
37
|
+
torch.int32: "s32",
|
|
38
|
+
torch.float32: "flt",
|
|
39
|
+
torch.float64: "dbl",
|
|
40
|
+
}
|
|
41
|
+
if dtype not in types:
|
|
42
|
+
raise ValueError(f"Unsupported dtype is provided {dtype}. Supported dtypes are: {types.keys()}")
|
|
43
|
+
return types[dtype]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class _AudioStreamingEncoder:
|
|
47
|
+
"""Given a waveform, encode on-demand and return bytes"""
|
|
48
|
+
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
src: Tensor,
|
|
52
|
+
sample_rate: int,
|
|
53
|
+
effect: str,
|
|
54
|
+
muxer: str,
|
|
55
|
+
encoder: Optional[str],
|
|
56
|
+
codec_config: Optional[CodecConfig],
|
|
57
|
+
frames_per_chunk: int,
|
|
58
|
+
):
|
|
59
|
+
self.src = src
|
|
60
|
+
self.buffer = _StreamingIOBuffer()
|
|
61
|
+
self.writer = StreamWriter(self.buffer, format=muxer)
|
|
62
|
+
self.writer.add_audio_stream(
|
|
63
|
+
num_channels=src.size(1),
|
|
64
|
+
sample_rate=sample_rate,
|
|
65
|
+
format=_get_sample_fmt(src.dtype),
|
|
66
|
+
encoder=encoder,
|
|
67
|
+
filter_desc=effect,
|
|
68
|
+
codec_config=codec_config,
|
|
69
|
+
)
|
|
70
|
+
self.writer.open()
|
|
71
|
+
self.fpc = frames_per_chunk
|
|
72
|
+
|
|
73
|
+
# index on the input tensor (along time-axis)
|
|
74
|
+
# we use -1 to indicate that we finished iterating the tensor and
|
|
75
|
+
# the writer is closed.
|
|
76
|
+
self.i_iter = 0
|
|
77
|
+
|
|
78
|
+
def read(self, n):
|
|
79
|
+
while not self.buffer._buffer and self.i_iter >= 0:
|
|
80
|
+
self.writer.write_audio_chunk(0, self.src[self.i_iter : self.i_iter + self.fpc])
|
|
81
|
+
self.i_iter += self.fpc
|
|
82
|
+
if self.i_iter >= self.src.size(0):
|
|
83
|
+
self.writer.flush()
|
|
84
|
+
self.writer.close()
|
|
85
|
+
self.i_iter = -1
|
|
86
|
+
return self.buffer.pop(n)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _encode(
|
|
90
|
+
src: Tensor,
|
|
91
|
+
sample_rate: int,
|
|
92
|
+
effect: str,
|
|
93
|
+
muxer: str,
|
|
94
|
+
encoder: Optional[str],
|
|
95
|
+
codec_config: Optional[CodecConfig],
|
|
96
|
+
):
|
|
97
|
+
buffer = io.BytesIO()
|
|
98
|
+
writer = StreamWriter(buffer, format=muxer)
|
|
99
|
+
writer.add_audio_stream(
|
|
100
|
+
num_channels=src.size(1),
|
|
101
|
+
sample_rate=sample_rate,
|
|
102
|
+
format=_get_sample_fmt(src.dtype),
|
|
103
|
+
encoder=encoder,
|
|
104
|
+
filter_desc=effect,
|
|
105
|
+
codec_config=codec_config,
|
|
106
|
+
)
|
|
107
|
+
with writer.open():
|
|
108
|
+
writer.write_audio_chunk(0, src)
|
|
109
|
+
buffer.seek(0)
|
|
110
|
+
return buffer
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _get_muxer(dtype: torch.dtype):
|
|
114
|
+
# TODO: check if this works in Windows.
|
|
115
|
+
types = {
|
|
116
|
+
torch.uint8: "u8",
|
|
117
|
+
torch.int16: "s16le",
|
|
118
|
+
torch.int32: "s32le",
|
|
119
|
+
torch.float32: "f32le",
|
|
120
|
+
torch.float64: "f64le",
|
|
121
|
+
}
|
|
122
|
+
if dtype not in types:
|
|
123
|
+
raise ValueError(f"Unsupported dtype is provided {dtype}. Supported dtypes are: {types.keys()}")
|
|
124
|
+
return types[dtype]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class AudioEffector:
|
|
128
|
+
"""Apply various filters and/or codecs to waveforms.
|
|
129
|
+
|
|
130
|
+
.. versionadded:: 2.1
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
effect (str or None, optional): Filter expressions or ``None`` to apply no filter.
|
|
134
|
+
See https://ffmpeg.org/ffmpeg-filters.html#Audio-Filters for the
|
|
135
|
+
details of filter syntax.
|
|
136
|
+
|
|
137
|
+
format (str or None, optional): When provided, encode the audio into the
|
|
138
|
+
corresponding format. Default: ``None``.
|
|
139
|
+
|
|
140
|
+
encoder (str or None, optional): When provided, override the encoder used
|
|
141
|
+
by the ``format``. Default: ``None``.
|
|
142
|
+
|
|
143
|
+
codec_config (CodecConfig or None, optional): When provided, configure the encoding codec.
|
|
144
|
+
Should be provided in conjunction with ``format`` option.
|
|
145
|
+
|
|
146
|
+
pad_end (bool, optional): When enabled, and if the waveform becomes shorter after applying
|
|
147
|
+
effects/codec, then pad the end with silence.
|
|
148
|
+
|
|
149
|
+
Example - Basic usage
|
|
150
|
+
To use ``AudioEffector``, first instantiate it with a set of
|
|
151
|
+
``effect`` and ``format``.
|
|
152
|
+
|
|
153
|
+
>>> # instantiate the effector
|
|
154
|
+
>>> effector = AudioEffector(effect=..., format=...)
|
|
155
|
+
|
|
156
|
+
Then, use :py:meth:`~AudioEffector.apply` or :py:meth:`~AudioEffector.stream`
|
|
157
|
+
method to apply them.
|
|
158
|
+
|
|
159
|
+
>>> # Apply the effect to the whole waveform
|
|
160
|
+
>>> applied = effector.apply(waveform, sample_rate)
|
|
161
|
+
|
|
162
|
+
>>> # Apply the effect chunk-by-chunk
|
|
163
|
+
>>> for chunk in effector.stream(waveform, sample_rate):
|
|
164
|
+
>>> ...
|
|
165
|
+
|
|
166
|
+
Example - Applying effects
|
|
167
|
+
Please refer to
|
|
168
|
+
https://ffmpeg.org/ffmpeg-filters.html#Filtergraph-description
|
|
169
|
+
for the overview of filter description, and
|
|
170
|
+
https://ffmpeg.org/ffmpeg-filters.html#toc-Audio-Filters
|
|
171
|
+
for the list of available filters.
|
|
172
|
+
|
|
173
|
+
Tempo - https://ffmpeg.org/ffmpeg-filters.html#atempo
|
|
174
|
+
|
|
175
|
+
>>> AudioEffector(effect="atempo=1.5")
|
|
176
|
+
|
|
177
|
+
Echo - https://ffmpeg.org/ffmpeg-filters.html#aecho
|
|
178
|
+
|
|
179
|
+
>>> AudioEffector(effect="aecho=0.8:0.88:60:0.4")
|
|
180
|
+
|
|
181
|
+
Flanger - https://ffmpeg.org/ffmpeg-filters.html#flanger
|
|
182
|
+
|
|
183
|
+
>>> AudioEffector(effect="aflanger")
|
|
184
|
+
|
|
185
|
+
Vibrato - https://ffmpeg.org/ffmpeg-filters.html#vibrato
|
|
186
|
+
|
|
187
|
+
>>> AudioEffector(effect="vibrato")
|
|
188
|
+
|
|
189
|
+
Tremolo - https://ffmpeg.org/ffmpeg-filters.html#tremolo
|
|
190
|
+
|
|
191
|
+
>>> AudioEffector(effect="vibrato")
|
|
192
|
+
|
|
193
|
+
You can also apply multiple effects at once.
|
|
194
|
+
|
|
195
|
+
>>> AudioEffector(effect="")
|
|
196
|
+
|
|
197
|
+
Example - Applying codec
|
|
198
|
+
One can apply codec using ``format`` argument. ``format`` can be
|
|
199
|
+
audio format or container format. If the container format supports
|
|
200
|
+
multiple encoders, you can specify it with ``encoder`` argument.
|
|
201
|
+
|
|
202
|
+
Wav format
|
|
203
|
+
(no compression is applied but samples are converted to
|
|
204
|
+
16-bit signed integer)
|
|
205
|
+
|
|
206
|
+
>>> AudioEffector(format="wav")
|
|
207
|
+
|
|
208
|
+
Ogg format with default encoder
|
|
209
|
+
|
|
210
|
+
>>> AudioEffector(format="ogg")
|
|
211
|
+
|
|
212
|
+
Ogg format with vorbis
|
|
213
|
+
|
|
214
|
+
>>> AudioEffector(format="ogg", encoder="vorbis")
|
|
215
|
+
|
|
216
|
+
Ogg format with opus
|
|
217
|
+
|
|
218
|
+
>>> AudioEffector(format="ogg", encoder="opus")
|
|
219
|
+
|
|
220
|
+
Webm format with opus
|
|
221
|
+
|
|
222
|
+
>>> AudioEffector(format="webm", encoder="opus")
|
|
223
|
+
|
|
224
|
+
Example - Applying codec with configuration
|
|
225
|
+
Reference: https://trac.ffmpeg.org/wiki/Encode/MP3
|
|
226
|
+
|
|
227
|
+
MP3 with default config
|
|
228
|
+
|
|
229
|
+
>>> AudioEffector(format="mp3")
|
|
230
|
+
|
|
231
|
+
MP3 with variable bitrate
|
|
232
|
+
|
|
233
|
+
>>> AudioEffector(format="mp3", codec_config=CodecConfig(qscale=5))
|
|
234
|
+
|
|
235
|
+
MP3 with constant bitrate
|
|
236
|
+
|
|
237
|
+
>>> AudioEffector(format="mp3", codec_config=CodecConfig(bit_rate=32_000))
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
def __init__(
|
|
241
|
+
self,
|
|
242
|
+
effect: Optional[str] = None,
|
|
243
|
+
format: Optional[str] = None,
|
|
244
|
+
*,
|
|
245
|
+
encoder: Optional[str] = None,
|
|
246
|
+
codec_config: Optional[CodecConfig] = None,
|
|
247
|
+
pad_end: bool = True,
|
|
248
|
+
):
|
|
249
|
+
if format is None:
|
|
250
|
+
if encoder is not None or codec_config is not None:
|
|
251
|
+
raise ValueError("`encoder` and/or `condec_config` opions are provided without `format` option.")
|
|
252
|
+
self.effect = effect
|
|
253
|
+
self.format = format
|
|
254
|
+
self.encoder = encoder
|
|
255
|
+
self.codec_config = codec_config
|
|
256
|
+
self.pad_end = pad_end
|
|
257
|
+
|
|
258
|
+
def _get_reader(self, waveform, sample_rate, output_sample_rate, frames_per_chunk=None):
|
|
259
|
+
num_frames, num_channels = waveform.shape
|
|
260
|
+
|
|
261
|
+
if self.format is not None:
|
|
262
|
+
muxer = self.format
|
|
263
|
+
encoder = self.encoder
|
|
264
|
+
option = {}
|
|
265
|
+
# Some formats are headerless, so need to provide these infomation.
|
|
266
|
+
if self.format == "mulaw":
|
|
267
|
+
option = {"sample_rate": f"{sample_rate}", "channels": f"{num_channels}"}
|
|
268
|
+
|
|
269
|
+
else: # PCM
|
|
270
|
+
muxer = _get_muxer(waveform.dtype)
|
|
271
|
+
encoder = None
|
|
272
|
+
option = {"sample_rate": f"{sample_rate}", "channels": f"{num_channels}"}
|
|
273
|
+
|
|
274
|
+
if frames_per_chunk is None:
|
|
275
|
+
src = _encode(waveform, sample_rate, self.effect, muxer, encoder, self.codec_config)
|
|
276
|
+
else:
|
|
277
|
+
src = _AudioStreamingEncoder(
|
|
278
|
+
waveform, sample_rate, self.effect, muxer, encoder, self.codec_config, frames_per_chunk
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
output_sr = sample_rate if output_sample_rate is None else output_sample_rate
|
|
282
|
+
filter_desc = _get_afilter_desc(output_sr, _get_sample_fmt(waveform.dtype), num_channels)
|
|
283
|
+
if self.pad_end:
|
|
284
|
+
filter_desc = f"{filter_desc},apad=whole_len={num_frames}"
|
|
285
|
+
|
|
286
|
+
reader = StreamReader(src, format=muxer, option=option)
|
|
287
|
+
reader.add_audio_stream(frames_per_chunk or -1, -1, filter_desc=filter_desc)
|
|
288
|
+
return reader
|
|
289
|
+
|
|
290
|
+
def apply(self, waveform: Tensor, sample_rate: int, output_sample_rate: Optional[int] = None) -> Tensor:
|
|
291
|
+
"""Apply the effect and/or codecs to the whole tensor.
|
|
292
|
+
|
|
293
|
+
Args:
|
|
294
|
+
waveform (Tensor): The input waveform. Shape: ``(time, channel)``
|
|
295
|
+
sample_rate (int): Sample rate of the input waveform.
|
|
296
|
+
output_sample_rate (int or None, optional): Output sample rate.
|
|
297
|
+
If provided, override the output sample rate.
|
|
298
|
+
Otherwise, the resulting tensor is resampled to have
|
|
299
|
+
the same sample rate as the input.
|
|
300
|
+
Default: ``None``.
|
|
301
|
+
|
|
302
|
+
Returns:
|
|
303
|
+
Tensor:
|
|
304
|
+
Resulting Tensor. Shape: ``(time, channel)``. The number of frames
|
|
305
|
+
could be different from that of the input.
|
|
306
|
+
"""
|
|
307
|
+
if waveform.ndim != 2:
|
|
308
|
+
raise ValueError(f"Expected the input waveform to be 2D. Found: {waveform.ndim}")
|
|
309
|
+
|
|
310
|
+
if waveform.numel() == 0:
|
|
311
|
+
return waveform
|
|
312
|
+
|
|
313
|
+
reader = self._get_reader(waveform, sample_rate, output_sample_rate)
|
|
314
|
+
reader.process_all_packets()
|
|
315
|
+
(applied,) = reader.pop_chunks()
|
|
316
|
+
return Tensor(applied)
|
|
317
|
+
|
|
318
|
+
def stream(
|
|
319
|
+
self, waveform: Tensor, sample_rate: int, frames_per_chunk: int, output_sample_rate: Optional[int] = None
|
|
320
|
+
) -> Iterator[Tensor]:
|
|
321
|
+
"""Apply the effect and/or codecs to the given tensor chunk by chunk.
|
|
322
|
+
|
|
323
|
+
Args:
|
|
324
|
+
waveform (Tensor): The input waveform. Shape: ``(time, channel)``
|
|
325
|
+
sample_rate (int): Sample rate of the waveform.
|
|
326
|
+
frames_per_chunk (int): The number of frames to return at a time.
|
|
327
|
+
output_sample_rate (int or None, optional): Output sample rate.
|
|
328
|
+
If provided, override the output sample rate.
|
|
329
|
+
Otherwise, the resulting tensor is resampled to have
|
|
330
|
+
the same sample rate as the input.
|
|
331
|
+
Default: ``None``.
|
|
332
|
+
|
|
333
|
+
Returns:
|
|
334
|
+
Iterator[Tensor]:
|
|
335
|
+
Series of processed chunks. Shape: ``(time, channel)``, where the
|
|
336
|
+
the number of frames matches ``frames_per_chunk`` except the
|
|
337
|
+
last chunk, which could be shorter.
|
|
338
|
+
"""
|
|
339
|
+
if waveform.ndim != 2:
|
|
340
|
+
raise ValueError(f"Expected the input waveform to be 2D. Found: {waveform.ndim}")
|
|
341
|
+
|
|
342
|
+
if waveform.numel() == 0:
|
|
343
|
+
return waveform
|
|
344
|
+
|
|
345
|
+
reader = self._get_reader(waveform, sample_rate, output_sample_rate, frames_per_chunk)
|
|
346
|
+
for (applied,) in reader.stream():
|
|
347
|
+
yield Tensor(applied)
|