torchaudio 2.8.0__cp313-cp313-win_amd64.whl → 2.9.0__cp313-cp313-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.

Files changed (92) hide show
  1. torchaudio/__init__.py +179 -39
  2. torchaudio/_extension/__init__.py +1 -14
  3. torchaudio/_extension/utils.py +0 -47
  4. torchaudio/_internal/module_utils.py +12 -3
  5. torchaudio/_torchcodec.py +73 -85
  6. torchaudio/datasets/cmuarctic.py +1 -1
  7. torchaudio/datasets/utils.py +1 -1
  8. torchaudio/functional/__init__.py +0 -2
  9. torchaudio/functional/_alignment.py +1 -1
  10. torchaudio/functional/filtering.py +70 -55
  11. torchaudio/functional/functional.py +26 -60
  12. torchaudio/lib/_torchaudio.pyd +0 -0
  13. torchaudio/lib/libtorchaudio.pyd +0 -0
  14. torchaudio/models/decoder/__init__.py +14 -2
  15. torchaudio/models/decoder/_ctc_decoder.py +6 -6
  16. torchaudio/models/decoder/_cuda_ctc_decoder.py +1 -1
  17. torchaudio/models/squim/objective.py +2 -2
  18. torchaudio/pipelines/_source_separation_pipeline.py +1 -1
  19. torchaudio/pipelines/_squim_pipeline.py +2 -2
  20. torchaudio/pipelines/_tts/utils.py +1 -1
  21. torchaudio/pipelines/rnnt_pipeline.py +4 -4
  22. torchaudio/transforms/__init__.py +1 -0
  23. torchaudio/transforms/_transforms.py +2 -2
  24. torchaudio/utils/__init__.py +2 -9
  25. torchaudio/utils/download.py +1 -3
  26. torchaudio/version.py +2 -2
  27. {torchaudio-2.8.0.dist-info → torchaudio-2.9.0.dist-info}/METADATA +8 -11
  28. torchaudio-2.9.0.dist-info/RECORD +85 -0
  29. {torchaudio-2.8.0.dist-info → torchaudio-2.9.0.dist-info}/top_level.txt +0 -1
  30. torchaudio/_backend/__init__.py +0 -61
  31. torchaudio/_backend/backend.py +0 -53
  32. torchaudio/_backend/common.py +0 -52
  33. torchaudio/_backend/ffmpeg.py +0 -334
  34. torchaudio/_backend/soundfile.py +0 -54
  35. torchaudio/_backend/soundfile_backend.py +0 -457
  36. torchaudio/_backend/sox.py +0 -91
  37. torchaudio/_backend/utils.py +0 -350
  38. torchaudio/backend/__init__.py +0 -8
  39. torchaudio/backend/_no_backend.py +0 -25
  40. torchaudio/backend/_sox_io_backend.py +0 -294
  41. torchaudio/backend/common.py +0 -13
  42. torchaudio/backend/no_backend.py +0 -14
  43. torchaudio/backend/soundfile_backend.py +0 -14
  44. torchaudio/backend/sox_io_backend.py +0 -14
  45. torchaudio/io/__init__.py +0 -20
  46. torchaudio/io/_effector.py +0 -347
  47. torchaudio/io/_playback.py +0 -72
  48. torchaudio/kaldi_io.py +0 -150
  49. torchaudio/prototype/__init__.py +0 -0
  50. torchaudio/prototype/datasets/__init__.py +0 -4
  51. torchaudio/prototype/datasets/musan.py +0 -68
  52. torchaudio/prototype/functional/__init__.py +0 -26
  53. torchaudio/prototype/functional/_dsp.py +0 -441
  54. torchaudio/prototype/functional/_rir.py +0 -382
  55. torchaudio/prototype/functional/functional.py +0 -193
  56. torchaudio/prototype/models/__init__.py +0 -39
  57. torchaudio/prototype/models/_conformer_wav2vec2.py +0 -801
  58. torchaudio/prototype/models/_emformer_hubert.py +0 -337
  59. torchaudio/prototype/models/conv_emformer.py +0 -529
  60. torchaudio/prototype/models/hifi_gan.py +0 -342
  61. torchaudio/prototype/models/rnnt.py +0 -717
  62. torchaudio/prototype/models/rnnt_decoder.py +0 -402
  63. torchaudio/prototype/pipelines/__init__.py +0 -21
  64. torchaudio/prototype/pipelines/_vggish/__init__.py +0 -7
  65. torchaudio/prototype/pipelines/_vggish/_vggish_impl.py +0 -236
  66. torchaudio/prototype/pipelines/_vggish/_vggish_pipeline.py +0 -83
  67. torchaudio/prototype/pipelines/hifigan_pipeline.py +0 -233
  68. torchaudio/prototype/pipelines/rnnt_pipeline.py +0 -58
  69. torchaudio/prototype/transforms/__init__.py +0 -9
  70. torchaudio/prototype/transforms/_transforms.py +0 -461
  71. torchaudio/sox_effects/__init__.py +0 -10
  72. torchaudio/sox_effects/sox_effects.py +0 -275
  73. torchaudio/utils/ffmpeg_utils.py +0 -11
  74. torchaudio/utils/sox_utils.py +0 -118
  75. torchaudio-2.8.0.dist-info/RECORD +0 -145
  76. torio/__init__.py +0 -8
  77. torio/_extension/__init__.py +0 -13
  78. torio/_extension/utils.py +0 -147
  79. torio/io/__init__.py +0 -9
  80. torio/io/_streaming_media_decoder.py +0 -977
  81. torio/io/_streaming_media_encoder.py +0 -502
  82. torio/lib/__init__.py +0 -0
  83. torio/lib/_torio_ffmpeg4.pyd +0 -0
  84. torio/lib/_torio_ffmpeg5.pyd +0 -0
  85. torio/lib/_torio_ffmpeg6.pyd +0 -0
  86. torio/lib/libtorio_ffmpeg4.pyd +0 -0
  87. torio/lib/libtorio_ffmpeg5.pyd +0 -0
  88. torio/lib/libtorio_ffmpeg6.pyd +0 -0
  89. torio/utils/__init__.py +0 -4
  90. torio/utils/ffmpeg_utils.py +0 -275
  91. {torchaudio-2.8.0.dist-info → torchaudio-2.9.0.dist-info}/WHEEL +0 -0
  92. {torchaudio-2.8.0.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 per-call backend 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 underlying implementation directly.",
9
- stacklevel=2,
10
- )
11
-
12
- from . import _no_backend
13
-
14
- return getattr(_no_backend, name)
@@ -1,14 +0,0 @@
1
- def __getattr__(name: str):
2
- import warnings
3
-
4
- warnings.warn(
5
- "Torchaudio's I/O functions now support per-call backend 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 underlying implementation directly.",
9
- stacklevel=2,
10
- )
11
-
12
- from torchaudio._backend import soundfile_backend
13
-
14
- return getattr(soundfile_backend, name)
@@ -1,14 +0,0 @@
1
- def __getattr__(name: str):
2
- import warnings
3
-
4
- warnings.warn(
5
- "Torchaudio's I/O functions now support per-call backend 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 underlying 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,20 +0,0 @@
1
- from torio.io import CodecConfig as _CodecConfig, StreamingMediaDecoder as _StreamReader, StreamingMediaEncoder as _StreamWriter
2
- from torchaudio._internal.module_utils import dropping_class_io_support, dropping_class_support, dropping_io_support
3
-
4
- from ._effector import AudioEffector as _AudioEffector
5
- from ._playback import play_audio as _play_audio
6
-
7
- CodecConfig = dropping_class_io_support(_CodecConfig)
8
- StreamReader = dropping_class_io_support(_StreamReader)
9
- StreamWriter = dropping_class_io_support(_StreamWriter)
10
- AudioEffector = dropping_class_support(_AudioEffector)
11
- play_audio = dropping_io_support(_play_audio)
12
-
13
-
14
- __all__ = [
15
- "AudioEffector",
16
- "StreamReader",
17
- "StreamWriter",
18
- "CodecConfig",
19
- "play_audio",
20
- ]
@@ -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)
@@ -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,150 +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
- from torchaudio._internal.module_utils import dropping_support
10
-
11
- if _mod_utils.is_module_available("numpy"):
12
- import numpy as np
13
-
14
-
15
- __all__ = [
16
- "read_vec_int_ark",
17
- "read_vec_flt_scp",
18
- "read_vec_flt_ark",
19
- "read_mat_scp",
20
- "read_mat_ark",
21
- ]
22
-
23
-
24
- def _convert_method_output_to_tensor(
25
- file_or_fd: Any, fn: Callable, convert_contiguous: bool = False
26
- ) -> Iterable[Tuple[str, Tensor]]:
27
- r"""Takes a method invokes it. The output is converted to a tensor.
28
-
29
- Args:
30
- file_or_fd (str/FileDescriptor): File name or file descriptor
31
- fn (Callable): Function that has the signature (file name/descriptor) and converts it to
32
- Iterable[Tuple[str, Tensor]].
33
- convert_contiguous (bool, optional): Determines whether the array should be converted into a
34
- contiguous layout. (Default: ``False``)
35
-
36
- Returns:
37
- Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is vec/mat
38
- """
39
- for key, np_arr in fn(file_or_fd):
40
- if convert_contiguous:
41
- np_arr = np.ascontiguousarray(np_arr)
42
- yield key, torch.from_numpy(np_arr)
43
-
44
-
45
- @dropping_support
46
- @_mod_utils.requires_module("kaldi_io", "numpy")
47
- def read_vec_int_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
48
- r"""Create generator of (key,vector<int>) tuples, which reads from the ark file/stream.
49
-
50
- Args:
51
- file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor
52
-
53
- Returns:
54
- Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file
55
-
56
- Example
57
- >>> # read ark to a 'dictionary'
58
- >>> d = { u:d for u,d in torchaudio.kaldi_io.read_vec_int_ark(file) }
59
- """
60
-
61
- import kaldi_io
62
-
63
- # Requires convert_contiguous to be True because elements from int32 vector are
64
- # sorted in tuples: (sizeof(int32), value) so strides are (5,) instead of (4,) which will throw an error
65
- # in from_numpy as it expects strides to be a multiple of 4 (int32).
66
- return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_int_ark, convert_contiguous=True)
67
-
68
-
69
- @dropping_support
70
- @_mod_utils.requires_module("kaldi_io", "numpy")
71
- def read_vec_flt_scp(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
72
- r"""Create generator of (key,vector<float32/float64>) tuples, read according to Kaldi scp.
73
-
74
- Args:
75
- file_or_fd (str/FileDescriptor): scp, gzipped scp, pipe or opened file descriptor
76
-
77
- Returns:
78
- Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file
79
-
80
- Example
81
- >>> # read scp to a 'dictionary'
82
- >>> # d = { u:d for u,d in torchaudio.kaldi_io.read_vec_flt_scp(file) }
83
- """
84
-
85
- import kaldi_io
86
-
87
- return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_flt_scp)
88
-
89
-
90
- @dropping_support
91
- @_mod_utils.requires_module("kaldi_io", "numpy")
92
- def read_vec_flt_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
93
- r"""Create generator of (key,vector<float32/float64>) tuples, which reads from the ark file/stream.
94
-
95
- Args:
96
- file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor
97
-
98
- Returns:
99
- Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file
100
-
101
- Example
102
- >>> # read ark to a 'dictionary'
103
- >>> d = { u:d for u,d in torchaudio.kaldi_io.read_vec_flt_ark(file) }
104
- """
105
-
106
- import kaldi_io
107
-
108
- return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_flt_ark)
109
-
110
-
111
- @dropping_support
112
- @_mod_utils.requires_module("kaldi_io", "numpy")
113
- def read_mat_scp(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
114
- r"""Create generator of (key,matrix<float32/float64>) tuples, read according to Kaldi scp.
115
-
116
- Args:
117
- file_or_fd (str/FileDescriptor): scp, gzipped scp, pipe or opened file descriptor
118
-
119
- Returns:
120
- Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the matrix read from file
121
-
122
- Example
123
- >>> # read scp to a 'dictionary'
124
- >>> d = { u:d for u,d in torchaudio.kaldi_io.read_mat_scp(file) }
125
- """
126
-
127
- import kaldi_io
128
-
129
- return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_mat_scp)
130
-
131
-
132
- @dropping_support
133
- @_mod_utils.requires_module("kaldi_io", "numpy")
134
- def read_mat_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
135
- r"""Create generator of (key,matrix<float32/float64>) tuples, which reads from the ark file/stream.
136
-
137
- Args:
138
- file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor
139
-
140
- Returns:
141
- Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the matrix read from file
142
-
143
- Example
144
- >>> # read ark to a 'dictionary'
145
- >>> d = { u:d for u,d in torchaudio.kaldi_io.read_mat_ark(file) }
146
- """
147
-
148
- import kaldi_io
149
-
150
- return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_mat_ark)
File without changes
@@ -1,4 +0,0 @@
1
- from .musan import Musan
2
-
3
-
4
- __all__ = ["Musan"]