1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use *;
use wrap_pyfunction;
use *;
use crateWaveform;
use cratePyArraySamples;
/// Uses multithreading in Rust to decode many audio files in parallel.
///
///
/// Example:
/// **(Attempt to) decode three files.**
///
/// In this example, we succesfully decode two MP3 files with
/// the default decoding arguments. Then, we demonstrate
/// how to catch an error when decoding the third file.
///
/// >>> import babycat
/// >>> filenames = [
/// ... "audio-for-tests/andreas-theme/track.flac",
/// ... "audio-for-tests/blippy-trance/track.wav",
/// ... "does-not-exist",
/// ... ]
/// >>>
/// >>> batch = babycat.batch.waveforms_from_files(filenames)
///
/// The first two files are decoded as expected, with the
/// ``exception`` field being ``None`` and the ``waveform``
/// field containing a :py:class:`Waveform`.
///
/// >>> batch[0].name
/// 'audio-for-tests/andreas-theme/track.flac'
/// >>> print(batch[0].exception)
/// None
/// >>> batch[0].waveform
/// <babycat.Waveform: 9586415 frames, 2 channels, 44100 hz>
///
/// >>> batch[1].name
/// 'audio-for-tests/blippy-trance/track.wav'
/// >>> print(batch[1].exception)
/// None
/// >>> batch[1].waveform
/// <babycat.Waveform: 5292911 frames, 2 channels, 44100 hz>
///
/// For the third file, the ``waveform`` field is ``None`` and the
/// ``exception`` field contains a reference to a
/// :py:class:`FileNotFoundError`. The ``name`` field helps us
/// identify which file is missing.
///
/// >>> batch[2].name
/// 'does-not-exist'
/// >>> type(batch[2].exception)
/// <class 'FileNotFoundError'>
/// >>> print(batch[2].waveform)
/// None
/// >>>
///
/// .. admonition:: Remember to raise exceptions when needed
/// :class: danger
///
/// :py:meth:`~waveforms_from_files` will return
/// exceptions but **will not raise them** for you. It is your
/// responsibility to check every ``exception`` field for
/// a not-``None`` exception that you can raise or
/// intentionally ignore.
///
/// Args:
/// filenames(list[str]): A :py:class:`list` of filenames--each as
/// :py:class:`str`--to decode in parallel.
///
/// start_time_milliseconds(int, optional): We discard
/// any audio before this millisecond offset. By default, this
/// does nothing and the audio is decoded from the beginning.
/// Negative offsets are invalid.
///
/// end_time_milliseconds(int, optional): We discard
/// any audio after this millisecond offset. By default,
/// this does nothing and the audio is decoded all the way
/// to the end. If ``start_time_milliseconds`` is specified,
/// then ``end_time_milliseconds`` must be greater. The resulting
///
/// frame_rate_hz(int, optional): A destination frame rate to resample
/// the audio to. Do not specify this parameter if you wish
/// Babycat to preserve the audio's original frame rate.
/// This does nothing if ``frame_rate_hz`` is equal to the
/// audio's original frame rate.
///
/// num_channels(int, optional): Set this to a positive integer ``n``
/// to select the *first* ``n`` channels stored in the
/// audio file. By default, Babycat will return all of the channels
/// in the original audio. This will raise an exception
/// if you specify a ``num_channels`` greater than the actual
/// number of channels in the audio.
///
/// convert_to_mono(bool, optional): Set to ``True`` to average all channels
/// into a single monophonic (mono) channel. If
/// ``num_channels = n`` is also specified, then only the
/// first ``n`` channels will be averaged. Note that
/// ``convert_to_mono`` cannot be set to ``True`` while
/// also setting ``num_channels = 1``.
///
/// zero_pad_ending(bool, optional): If you set this to ``True``,
/// Babycat will zero-pad the ending of the decoded waveform
/// to ensure that the output waveform's duration is exactly
/// ``end_time_milliseconds - start_time_milliseconds``.
/// By default, ``zero_pad_ending = False``, in which case
/// the output waveform will be shorter than
/// ``end_time_milliseconds - start_time_milliseconds``
/// if the input audio is shorter than ``end_time_milliseconds``.
///
/// resample_mode(int, optional): If you set ``frame_rate_hz``
/// to resample the audio when decoding, you can also set
/// ``resample_mode`` to pick which resampling backend to use.
/// The :py:mod:`babycat.resample_mode` submodule contains
/// the various available resampling algorithms compiled into Babycat.
/// By default, Babycat resamples audio using
/// `libsamplerate <http://www.mega-nerd.com/SRC/>`_ at its
/// highest-quality setting.
///
/// decoding_backend(int, optional): Sets the audio decoding
/// backend to use. Defaults to the Symphonia backend.
///
/// num_workers(int, optional): The number of threads--*Rust threads*, not Python
/// threads--to use for parallel decoding of the audio files in
/// ``filenames``. By default, Babycat creates the same
/// number of threads as the number of logical CPU cores on
/// your machine.
///
/// Returns:
/// list[WaveformNamedResult]: A list of objects that contain
/// a :py:class:`~babycat.Waveform` for every successful encoding
/// and a Python exception for every failed encoding. Look at
/// the ``"Raises"`` section of :py:meth:`Waveform.decode_from_filename`
/// for a list of possible exceptions that can be returned by this method.
///
/// Uses multithreading in Rust to decode many audio files in parallel,
/// directly returning a list of :py:class:`NumPyNamedResult` objects
/// instead of :py:class:`Waveform` objects.
///
/// This method is not as ergonomic as :py:meth:`~waveforms_from_files`
/// for decoding audio files in parallel. However, this method avoids
/// unnecessary memory allocations and Python GIL contention.
///
/// .. admonition:: Remember to raise exceptions when needed
/// :class: danger
///
/// :py:meth:`~waveforms_from_files_into_numpy` will return
/// exceptions but **will not raise them** for you. It is your
/// responsibility to check every ``exception`` field for
/// a not-``None`` exception that you can raise or
/// intentionally ignore.
///
/// Args:
/// filenames(list[str]): A :py:class:`list` of filenames--each as
/// :py:class:`str`--to decode in parallel.
///
/// start_time_milliseconds(int, optional): We discard
/// any audio before this millisecond offset. By default, this
/// does nothing and the audio is decoded from the beginning.
/// Negative offsets are invalid.
///
/// end_time_milliseconds(int, optional): We discard
/// any audio after this millisecond offset. By default,
/// this does nothing and the audio is decoded all the way
/// to the end. If ``start_time_milliseconds`` is specified,
/// then ``end_time_milliseconds`` must be greater. The resulting
///
/// frame_rate_hz(int, optional): A destination frame rate to resample
/// the audio to. Do not specify this parameter if you wish
/// Babycat to preserve the audio's original frame rate.
/// This does nothing if ``frame_rate_hz`` is equal to the
/// audio's original frame rate.
///
/// num_channels(int, optional): Set this to a positive integer ``n``
/// to select the *first* ``n`` channels stored in the
/// audio file. By default, Babycat will return all of the channels
/// in the original audio. This will raise an exception
/// if you specify a ``num_channels`` greater than the actual
/// number of channels in the audio.
///
/// convert_to_mono(bool, optional): Set to ``True`` to average all channels
/// into a single monophonic (mono) channel. If
/// ``num_channels = n`` is also specified, then only the
/// first ``n`` channels will be averaged. Note that
/// ``convert_to_mono`` cannot be set to ``True`` while
/// also setting ``num_channels = 1``.
///
/// zero_pad_ending(bool, optional): If you set this to ``True``,
/// Babycat will zero-pad the ending of the decoded waveform
/// to ensure that the output waveform's duration is exactly
/// ``end_time_milliseconds - start_time_milliseconds``.
/// By default, ``zero_pad_ending = False``, in which case
/// the output waveform will be shorter than
/// ``end_time_milliseconds - start_time_milliseconds``
/// if the input audio is shorter than ``end_time_milliseconds``.
///
/// resample_mode(int, optional): If you set ``frame_rate_hz``
/// to resample the audio when decoding, you can also set
/// ``resample_mode`` to pick which resampling backend to use.
/// The :py:mod:`babycat.resample_mode` submodule contains
/// the various available resampling algorithms compiled into Babycat.
/// By default, Babycat resamples audio using
/// `libsamplerate <http://www.mega-nerd.com/SRC/>`_ at its
/// highest-quality setting.
///
/// decoding_backend(int, optional): Sets the audio decoding
/// backend to use. Defaults to the Symphonia backend.
///
/// num_workers(int, optional): The number of threads--*Rust threads*, not Python
/// threads--to use for parallel decoding of the audio files in
/// ``filenames``. By default, Babycat creates the same
/// number of threads as the number of logical CPU cores on
/// your machine.
///
/// Returns:
/// list[NumPyNamedResult]: A list of objects that contain
/// a :py:class:`~numpy.ndarray` for every successful encoding
/// and a Python exception for every failed encoding. Look at
/// the ``"Raises"`` section of :py:meth:`Waveform.decode_from_filename`
/// for a list of possible exceptions that can be returned by this method.
///
/// Uses multithreading in Rust to decode many audio files in parallel,
/// directly returning a list of :py:class:`NumPyNamedResult` objects
/// instead of :py:class:`Waveform` objects.
///
/// This method is not as ergonomic as :py:meth:`~waveforms_from_files`
/// for decoding audio files in parallel. However, this method avoids
/// unnecessary memory allocations and Python GIL contention.
///
/// This method is also better if you want to automatically raise
/// decoding errors as exceptions, and do not care to inspect
/// the name or type of exception. Currently this method
/// raises all exceptions as ``pyo3.PanicException``.
///
/// Args:
/// filenames(list[str]): A :py:class:`list` of filenames--each as
/// :py:class:`str`--to decode in parallel.
///
/// start_time_milliseconds(int, optional): We discard
/// any audio before this millisecond offset. By default, this
/// does nothing and the audio is decoded from the beginning.
/// Negative offsets are invalid.
///
/// end_time_milliseconds(int, optional): We discard
/// any audio after this millisecond offset. By default,
/// this does nothing and the audio is decoded all the way
/// to the end. If ``start_time_milliseconds`` is specified,
/// then ``end_time_milliseconds`` must be greater. The resulting
///
/// frame_rate_hz(int, optional): A destination frame rate to resample
/// the audio to. Do not specify this parameter if you wish
/// Babycat to preserve the audio's original frame rate.
/// This does nothing if ``frame_rate_hz`` is equal to the
/// audio's original frame rate.
///
/// num_channels(int, optional): Set this to a positive integer ``n``
/// to select the *first* ``n`` channels stored in the
/// audio file. By default, Babycat will return all of the channels
/// in the original audio. This will raise an exception
/// if you specify a ``num_channels`` greater than the actual
/// number of channels in the audio.
///
/// convert_to_mono(bool, optional): Set to ``True`` to average all channels
/// into a single monophonic (mono) channel. If
/// ``num_channels = n`` is also specified, then only the
/// first ``n`` channels will be averaged. Note that
/// ``convert_to_mono`` cannot be set to ``True`` while
/// also setting ``num_channels = 1``.
///
/// zero_pad_ending(bool, optional): If you set this to ``True``,
/// Babycat will zero-pad the ending of the decoded waveform
/// to ensure that the output waveform's duration is exactly
/// ``end_time_milliseconds - start_time_milliseconds``.
/// By default, ``zero_pad_ending = False``, in which case
/// the output waveform will be shorter than
/// ``end_time_milliseconds - start_time_milliseconds``
/// if the input audio is shorter than ``end_time_milliseconds``.
///
/// resample_mode(int, optional): If you set ``frame_rate_hz``
/// to resample the audio when decoding, you can also set
/// ``resample_mode`` to pick which resampling backend to use.
/// The :py:mod:`babycat.resample_mode` submodule contains
/// the various available resampling algorithms compiled into Babycat.
/// By default, Babycat resamples audio using
/// `libsamplerate <http://www.mega-nerd.com/SRC/>`_ at its
/// highest-quality setting.
///
/// decoding_backend(int, optional): Sets the audio decoding
/// backend to use. Defaults to the Symphonia backend.
///
/// num_workers(int, optional): The number of threads--*Rust threads*, not Python
/// threads--to use for parallel decoding of the audio files in
/// ``filenames``. By default, Babycat creates the same
/// number of threads as the number of logical CPU cores on
/// your machine.
///
/// Returns:
/// list[numpy.ndarray]: A list of NumPy arrays of all of the
/// successfully-decoded audio waveforms.
///
/// Raises:
/// Currently, this method raises ``pyo3.PanicException`` if it
/// faces an error when decoding from any files.
///