torchaudio 2.7.1__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 +184 -33
- torchaudio/_extension/__init__.py +1 -14
- torchaudio/_extension/utils.py +0 -47
- torchaudio/_internal/module_utils.py +68 -10
- torchaudio/_torchcodec.py +340 -0
- torchaudio/datasets/cmuarctic.py +1 -1
- torchaudio/datasets/utils.py +1 -1
- torchaudio/functional/__init__.py +6 -3
- torchaudio/functional/_alignment.py +1 -1
- torchaudio/functional/filtering.py +70 -55
- torchaudio/functional/functional.py +31 -61
- torchaudio/lib/_torchaudio.pyd +0 -0
- torchaudio/lib/libtorchaudio.pyd +0 -0
- torchaudio/models/decoder/__init__.py +19 -1
- 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 +3 -1
- torchaudio/pipelines/rnnt_pipeline.py +4 -4
- torchaudio/transforms/__init__.py +4 -1
- torchaudio/transforms/_transforms.py +4 -3
- torchaudio/utils/__init__.py +2 -9
- torchaudio/utils/download.py +1 -1
- torchaudio/version.py +2 -2
- {torchaudio-2.7.1.dist-info → torchaudio-2.9.0.dist-info}/METADATA +15 -7
- torchaudio-2.9.0.dist-info/RECORD +85 -0
- {torchaudio-2.7.1.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 -317
- 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 -13
- torchaudio/io/_effector.py +0 -347
- torchaudio/io/_playback.py +0 -72
- torchaudio/kaldi_io.py +0 -144
- torchaudio/prototype/__init__.py +0 -0
- torchaudio/prototype/datasets/__init__.py +0 -4
- torchaudio/prototype/datasets/musan.py +0 -67
- torchaudio/prototype/functional/__init__.py +0 -26
- torchaudio/prototype/functional/_dsp.py +0 -433
- torchaudio/prototype/functional/_rir.py +0 -379
- torchaudio/prototype/functional/functional.py +0 -190
- torchaudio/prototype/models/__init__.py +0 -36
- torchaudio/prototype/models/_conformer_wav2vec2.py +0 -794
- torchaudio/prototype/models/_emformer_hubert.py +0 -333
- torchaudio/prototype/models/conv_emformer.py +0 -525
- torchaudio/prototype/models/hifi_gan.py +0 -336
- torchaudio/prototype/models/rnnt.py +0 -711
- torchaudio/prototype/models/rnnt_decoder.py +0 -399
- torchaudio/prototype/pipelines/__init__.py +0 -12
- torchaudio/prototype/pipelines/_vggish/__init__.py +0 -3
- torchaudio/prototype/pipelines/_vggish/_vggish_impl.py +0 -233
- torchaudio/prototype/pipelines/_vggish/_vggish_pipeline.py +0 -82
- torchaudio/prototype/pipelines/hifigan_pipeline.py +0 -228
- torchaudio/prototype/pipelines/rnnt_pipeline.py +0 -58
- torchaudio/prototype/transforms/__init__.py +0 -9
- torchaudio/prototype/transforms/_transforms.py +0 -456
- torchaudio/sox_effects/__init__.py +0 -10
- torchaudio/sox_effects/sox_effects.py +0 -272
- torchaudio/utils/ffmpeg_utils.py +0 -11
- torchaudio/utils/sox_utils.py +0 -99
- torchaudio-2.7.1.dist-info/RECORD +0 -144
- 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 -978
- 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 -247
- {torchaudio-2.7.1.dist-info → torchaudio-2.9.0.dist-info}/WHEEL +0 -0
- {torchaudio-2.7.1.dist-info → torchaudio-2.9.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
def __getattr__(name: str):
|
|
2
|
-
import warnings
|
|
3
|
-
|
|
4
|
-
warnings.warn(
|
|
5
|
-
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
|
|
6
|
-
"Importing backend implementation directly is no longer guaranteed to work. "
|
|
7
|
-
"Please use `backend` keyword with load/save/info function, instead of "
|
|
8
|
-
"calling the udnerlying implementation directly.",
|
|
9
|
-
stacklevel=2,
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
from . import _sox_io_backend
|
|
13
|
-
|
|
14
|
-
return getattr(_sox_io_backend, name)
|
torchaudio/io/__init__.py
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
from torio.io import CodecConfig, StreamingMediaDecoder as StreamReader, StreamingMediaEncoder as StreamWriter
|
|
2
|
-
|
|
3
|
-
from ._effector import AudioEffector
|
|
4
|
-
from ._playback import play_audio
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
__all__ = [
|
|
8
|
-
"AudioEffector",
|
|
9
|
-
"StreamReader",
|
|
10
|
-
"StreamWriter",
|
|
11
|
-
"CodecConfig",
|
|
12
|
-
"play_audio",
|
|
13
|
-
]
|
torchaudio/io/_effector.py
DELETED
|
@@ -1,347 +0,0 @@
|
|
|
1
|
-
import io
|
|
2
|
-
from typing import Iterator, List, Optional
|
|
3
|
-
|
|
4
|
-
import torch
|
|
5
|
-
from torch import Tensor
|
|
6
|
-
|
|
7
|
-
from torio.io._streaming_media_decoder import _get_afilter_desc, StreamingMediaDecoder as StreamReader
|
|
8
|
-
from torio.io._streaming_media_encoder import CodecConfig, StreamingMediaEncoder as 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)
|
torchaudio/io/_playback.py
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import warnings
|
|
2
|
-
from sys import platform
|
|
3
|
-
from typing import Optional
|
|
4
|
-
|
|
5
|
-
import torch
|
|
6
|
-
import torchaudio
|
|
7
|
-
|
|
8
|
-
dict_format = {
|
|
9
|
-
torch.uint8: "u8",
|
|
10
|
-
torch.int16: "s16",
|
|
11
|
-
torch.int32: "s32",
|
|
12
|
-
torch.int64: "s64",
|
|
13
|
-
torch.float32: "flt",
|
|
14
|
-
torch.float64: "dbl",
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def play_audio(
|
|
19
|
-
waveform: torch.Tensor,
|
|
20
|
-
sample_rate: Optional[float],
|
|
21
|
-
device: Optional[str] = None,
|
|
22
|
-
) -> None:
|
|
23
|
-
"""Plays audio through specified or available output device.
|
|
24
|
-
|
|
25
|
-
.. warning::
|
|
26
|
-
This function is currently only supported on MacOS, and requires
|
|
27
|
-
libavdevice (FFmpeg) with ``audiotoolbox`` output device.
|
|
28
|
-
|
|
29
|
-
.. note::
|
|
30
|
-
This function can play up to two audio channels.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
waveform: Tensor containing the audio to play.
|
|
34
|
-
Expected shape: `(time, num_channels)`.
|
|
35
|
-
sample_rate: Sample rate of the audio to play.
|
|
36
|
-
device: Output device to use. If None, the default device is used.
|
|
37
|
-
"""
|
|
38
|
-
|
|
39
|
-
if platform == "darwin":
|
|
40
|
-
device = device or "audiotoolbox"
|
|
41
|
-
path = "-"
|
|
42
|
-
else:
|
|
43
|
-
raise ValueError(f"This function only supports MacOS, but current OS is {platform}")
|
|
44
|
-
|
|
45
|
-
available_devices = list(torchaudio.utils.ffmpeg_utils.get_output_devices().keys())
|
|
46
|
-
if device not in available_devices:
|
|
47
|
-
raise ValueError(f"Device {device} is not available. Available devices are: {available_devices}")
|
|
48
|
-
|
|
49
|
-
if waveform.dtype not in dict_format:
|
|
50
|
-
raise ValueError(f"Unsupported type {waveform.dtype}. The list of supported types is: {dict_format.keys()}")
|
|
51
|
-
format = dict_format[waveform.dtype]
|
|
52
|
-
|
|
53
|
-
if waveform.ndim != 2:
|
|
54
|
-
raise ValueError(f"Expected 2D tensor with shape `(time, num_channels)`, got {waveform.ndim}D tensor instead")
|
|
55
|
-
|
|
56
|
-
time, num_channels = waveform.size()
|
|
57
|
-
if num_channels > 2:
|
|
58
|
-
warnings.warn(
|
|
59
|
-
f"Expected up to 2 channels, got {num_channels} channels instead. "
|
|
60
|
-
"Only the first 2 channels will be played.",
|
|
61
|
-
stacklevel=2,
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
# Write to speaker device
|
|
65
|
-
s = torchaudio.io.StreamWriter(dst=path, format=device)
|
|
66
|
-
s.add_audio_stream(sample_rate, num_channels, format=format)
|
|
67
|
-
|
|
68
|
-
# write audio to the device
|
|
69
|
-
block_size = 256
|
|
70
|
-
with s.open():
|
|
71
|
-
for i in range(0, time, block_size):
|
|
72
|
-
s.write_audio_chunk(0, waveform[i : i + block_size, :])
|
torchaudio/kaldi_io.py
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
# To use this file, the dependency (https://github.com/vesis84/kaldi-io-for-python)
|
|
2
|
-
# needs to be installed. This is a light wrapper around kaldi_io that returns
|
|
3
|
-
# torch.Tensors.
|
|
4
|
-
from typing import Any, Callable, Iterable, Tuple
|
|
5
|
-
|
|
6
|
-
import torch
|
|
7
|
-
from torch import Tensor
|
|
8
|
-
from torchaudio._internal import module_utils as _mod_utils
|
|
9
|
-
|
|
10
|
-
if _mod_utils.is_module_available("numpy"):
|
|
11
|
-
import numpy as np
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
__all__ = [
|
|
15
|
-
"read_vec_int_ark",
|
|
16
|
-
"read_vec_flt_scp",
|
|
17
|
-
"read_vec_flt_ark",
|
|
18
|
-
"read_mat_scp",
|
|
19
|
-
"read_mat_ark",
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def _convert_method_output_to_tensor(
|
|
24
|
-
file_or_fd: Any, fn: Callable, convert_contiguous: bool = False
|
|
25
|
-
) -> Iterable[Tuple[str, Tensor]]:
|
|
26
|
-
r"""Takes a method invokes it. The output is converted to a tensor.
|
|
27
|
-
|
|
28
|
-
Args:
|
|
29
|
-
file_or_fd (str/FileDescriptor): File name or file descriptor
|
|
30
|
-
fn (Callable): Function that has the signature (file name/descriptor) and converts it to
|
|
31
|
-
Iterable[Tuple[str, Tensor]].
|
|
32
|
-
convert_contiguous (bool, optional): Determines whether the array should be converted into a
|
|
33
|
-
contiguous layout. (Default: ``False``)
|
|
34
|
-
|
|
35
|
-
Returns:
|
|
36
|
-
Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is vec/mat
|
|
37
|
-
"""
|
|
38
|
-
for key, np_arr in fn(file_or_fd):
|
|
39
|
-
if convert_contiguous:
|
|
40
|
-
np_arr = np.ascontiguousarray(np_arr)
|
|
41
|
-
yield key, torch.from_numpy(np_arr)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
@_mod_utils.requires_module("kaldi_io", "numpy")
|
|
45
|
-
def read_vec_int_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
|
|
46
|
-
r"""Create generator of (key,vector<int>) tuples, which reads from the ark file/stream.
|
|
47
|
-
|
|
48
|
-
Args:
|
|
49
|
-
file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor
|
|
50
|
-
|
|
51
|
-
Returns:
|
|
52
|
-
Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file
|
|
53
|
-
|
|
54
|
-
Example
|
|
55
|
-
>>> # read ark to a 'dictionary'
|
|
56
|
-
>>> d = { u:d for u,d in torchaudio.kaldi_io.read_vec_int_ark(file) }
|
|
57
|
-
"""
|
|
58
|
-
|
|
59
|
-
import kaldi_io
|
|
60
|
-
|
|
61
|
-
# Requires convert_contiguous to be True because elements from int32 vector are
|
|
62
|
-
# sorted in tuples: (sizeof(int32), value) so strides are (5,) instead of (4,) which will throw an error
|
|
63
|
-
# in from_numpy as it expects strides to be a multiple of 4 (int32).
|
|
64
|
-
return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_int_ark, convert_contiguous=True)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
@_mod_utils.requires_module("kaldi_io", "numpy")
|
|
68
|
-
def read_vec_flt_scp(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
|
|
69
|
-
r"""Create generator of (key,vector<float32/float64>) tuples, read according to Kaldi scp.
|
|
70
|
-
|
|
71
|
-
Args:
|
|
72
|
-
file_or_fd (str/FileDescriptor): scp, gzipped scp, pipe or opened file descriptor
|
|
73
|
-
|
|
74
|
-
Returns:
|
|
75
|
-
Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file
|
|
76
|
-
|
|
77
|
-
Example
|
|
78
|
-
>>> # read scp to a 'dictionary'
|
|
79
|
-
>>> # d = { u:d for u,d in torchaudio.kaldi_io.read_vec_flt_scp(file) }
|
|
80
|
-
"""
|
|
81
|
-
|
|
82
|
-
import kaldi_io
|
|
83
|
-
|
|
84
|
-
return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_flt_scp)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
@_mod_utils.requires_module("kaldi_io", "numpy")
|
|
88
|
-
def read_vec_flt_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
|
|
89
|
-
r"""Create generator of (key,vector<float32/float64>) tuples, which reads from the ark file/stream.
|
|
90
|
-
|
|
91
|
-
Args:
|
|
92
|
-
file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor
|
|
93
|
-
|
|
94
|
-
Returns:
|
|
95
|
-
Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file
|
|
96
|
-
|
|
97
|
-
Example
|
|
98
|
-
>>> # read ark to a 'dictionary'
|
|
99
|
-
>>> d = { u:d for u,d in torchaudio.kaldi_io.read_vec_flt_ark(file) }
|
|
100
|
-
"""
|
|
101
|
-
|
|
102
|
-
import kaldi_io
|
|
103
|
-
|
|
104
|
-
return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_flt_ark)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
@_mod_utils.requires_module("kaldi_io", "numpy")
|
|
108
|
-
def read_mat_scp(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
|
|
109
|
-
r"""Create generator of (key,matrix<float32/float64>) tuples, read according to Kaldi scp.
|
|
110
|
-
|
|
111
|
-
Args:
|
|
112
|
-
file_or_fd (str/FileDescriptor): scp, gzipped scp, pipe or opened file descriptor
|
|
113
|
-
|
|
114
|
-
Returns:
|
|
115
|
-
Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the matrix read from file
|
|
116
|
-
|
|
117
|
-
Example
|
|
118
|
-
>>> # read scp to a 'dictionary'
|
|
119
|
-
>>> d = { u:d for u,d in torchaudio.kaldi_io.read_mat_scp(file) }
|
|
120
|
-
"""
|
|
121
|
-
|
|
122
|
-
import kaldi_io
|
|
123
|
-
|
|
124
|
-
return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_mat_scp)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
@_mod_utils.requires_module("kaldi_io", "numpy")
|
|
128
|
-
def read_mat_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
|
|
129
|
-
r"""Create generator of (key,matrix<float32/float64>) tuples, which reads from the ark file/stream.
|
|
130
|
-
|
|
131
|
-
Args:
|
|
132
|
-
file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor
|
|
133
|
-
|
|
134
|
-
Returns:
|
|
135
|
-
Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the matrix read from file
|
|
136
|
-
|
|
137
|
-
Example
|
|
138
|
-
>>> # read ark to a 'dictionary'
|
|
139
|
-
>>> d = { u:d for u,d in torchaudio.kaldi_io.read_mat_ark(file) }
|
|
140
|
-
"""
|
|
141
|
-
|
|
142
|
-
import kaldi_io
|
|
143
|
-
|
|
144
|
-
return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_mat_ark)
|
torchaudio/prototype/__init__.py
DELETED
|
File without changes
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
from pathlib import Path
|
|
2
|
-
from typing import Tuple, Union
|
|
3
|
-
|
|
4
|
-
import torch
|
|
5
|
-
from torch.utils.data import Dataset
|
|
6
|
-
from torchaudio.datasets.utils import _load_waveform
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
_SUBSETS = ["music", "noise", "speech"]
|
|
10
|
-
_SAMPLE_RATE = 16_000
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class Musan(Dataset):
|
|
14
|
-
r"""*MUSAN* :cite:`musan2015` dataset.
|
|
15
|
-
|
|
16
|
-
Args:
|
|
17
|
-
root (str or Path): Root directory where the dataset's top-level directory exists.
|
|
18
|
-
subset (str): Subset of the dataset to use. Options: [``"music"``, ``"noise"``, ``"speech"``].
|
|
19
|
-
"""
|
|
20
|
-
|
|
21
|
-
def __init__(self, root: Union[str, Path], subset: str):
|
|
22
|
-
if subset not in _SUBSETS:
|
|
23
|
-
raise ValueError(f"Invalid subset '{subset}' given. Please provide one of {_SUBSETS}")
|
|
24
|
-
|
|
25
|
-
subset_path = Path(root) / subset
|
|
26
|
-
self._walker = [str(p) for p in subset_path.glob("*/*.*")]
|
|
27
|
-
|
|
28
|
-
def get_metadata(self, n: int) -> Tuple[str, int, str]:
|
|
29
|
-
r"""Get metadata for the n-th sample in the dataset. Returns filepath instead of waveform,
|
|
30
|
-
but otherwise returns the same fields as :py:func:`__getitem__`.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
n (int): Index of sample to be loaded.
|
|
34
|
-
|
|
35
|
-
Returns:
|
|
36
|
-
(str, int, str):
|
|
37
|
-
str
|
|
38
|
-
Path to audio.
|
|
39
|
-
int
|
|
40
|
-
Sample rate.
|
|
41
|
-
str
|
|
42
|
-
File name.
|
|
43
|
-
"""
|
|
44
|
-
audio_path = self._walker[n]
|
|
45
|
-
return audio_path, _SAMPLE_RATE, Path(audio_path).name
|
|
46
|
-
|
|
47
|
-
def __getitem__(self, n: int) -> Tuple[torch.Tensor, int, str]:
|
|
48
|
-
r"""Return the n-th sample in the dataset.
|
|
49
|
-
|
|
50
|
-
Args:
|
|
51
|
-
n (int): Index of sample to be loaded.
|
|
52
|
-
|
|
53
|
-
Returns:
|
|
54
|
-
(torch.Tensor, int, str):
|
|
55
|
-
torch.Tensor
|
|
56
|
-
Waveform.
|
|
57
|
-
int
|
|
58
|
-
Sample rate.
|
|
59
|
-
str
|
|
60
|
-
File name.
|
|
61
|
-
"""
|
|
62
|
-
audio_path, sample_rate, filename = self.get_metadata(n)
|
|
63
|
-
path = Path(audio_path)
|
|
64
|
-
return _load_waveform(path.parent, path.name, sample_rate), sample_rate, filename
|
|
65
|
-
|
|
66
|
-
def __len__(self) -> int:
|
|
67
|
-
return len(self._walker)
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
from ._dsp import (
|
|
2
|
-
adsr_envelope,
|
|
3
|
-
exp_sigmoid,
|
|
4
|
-
extend_pitch,
|
|
5
|
-
filter_waveform,
|
|
6
|
-
frequency_impulse_response,
|
|
7
|
-
oscillator_bank,
|
|
8
|
-
sinc_impulse_response,
|
|
9
|
-
)
|
|
10
|
-
from ._rir import ray_tracing, simulate_rir_ism
|
|
11
|
-
from .functional import barkscale_fbanks, chroma_filterbank
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
__all__ = [
|
|
15
|
-
"adsr_envelope",
|
|
16
|
-
"exp_sigmoid",
|
|
17
|
-
"barkscale_fbanks",
|
|
18
|
-
"chroma_filterbank",
|
|
19
|
-
"extend_pitch",
|
|
20
|
-
"filter_waveform",
|
|
21
|
-
"frequency_impulse_response",
|
|
22
|
-
"oscillator_bank",
|
|
23
|
-
"ray_tracing",
|
|
24
|
-
"sinc_impulse_response",
|
|
25
|
-
"simulate_rir_ism",
|
|
26
|
-
]
|