babycat 0.0.14

An audio decoding and manipulation library, with bindings for C, Python, and WebAssembly.
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
//! Iterators over audio.

use std::fmt::Debug;

mod append;
mod append_zeros;
mod convert_to_mono;
mod gain;
mod prepend_zeros;
mod scale;
mod select_channels;
mod skip_frames;
mod sum;
mod take_frames;
mod waveform_source;

pub use append::Append;
pub use append_zeros::AppendZeros;
pub use convert_to_mono::ConvertToMono;
pub use gain::Gain;
pub use prepend_zeros::PrependZeros;
pub use scale::Scale;
pub use select_channels::SelectChannels;
pub use skip_frames::SkipFrames;
pub use sum::Sum;
pub use take_frames::TakeFrames;
pub use waveform_source::WaveformSource;

use crate::backend::Error;
use crate::backend::Signal;
use crate::backend::Waveform;

/// A sample iterator created by an audio decoder.
pub trait Source: Signal + Iterator<Item = f32> + Debug {
    /// Append one [`Source`] after another [`Source`].
    ///
    /// Both Sources are required to have the same frame rate and number
    /// of channels.
    ///
    /// # Examples
    ///
    /// ```
    /// use babycat::assertions::assert_debug;
    /// use babycat::{decoder::SymphoniaDecoder, Source, Signal, Waveform};
    ///
    /// // Load the FIRST audio file as a source.
    /// let f1 = "audio-for-tests/circus-of-freaks/track.flac";
    /// let mut d1 = SymphoniaDecoder::from_file(f1).unwrap();
    /// assert_debug(
    ///     &d1,
    ///     "SymphoniaDecoder { 2491247 frames,  2 channels,  44100 hz,  56s 490ms }"
    /// );
    ///
    /// // Load the SECOND audio file as a source.
    /// let f2 = "audio-for-tests/andreas-theme/track.flac";
    /// let mut d2 = SymphoniaDecoder::from_file(f2).unwrap();
    /// assert_debug(
    ///     &d2,
    ///     "SymphoniaDecoder { 9586415 frames,  2 channels,  44100 hz,  3m 37s 379ms }"
    /// );
    ///
    /// // Append the second audio file AFTER the first audio file.
    /// // The new length (in frames) is the sum of the two audio files' lengths.
    /// let d1_d2 = d1.append(d2).unwrap();
    ///
    /// // Test that it works.
    /// assert_debug(
    ///     &d1_d2,
    ///     "Append { 2491247 + 9586415 = 12077662 frames,  2 channels,  44100 hz,  4m 33s 869ms }"
    /// );
    /// ```
    #[inline]
    fn append<S2: Source + Sized>(self, second: S2) -> Result<Append<Self, S2>, Error>
    where
        Self: Sized,
    {
        Append::new(self, second)
    }

    /// Pad the *beginning* of the [`Source`] with silence.
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, WaveformSource};
    ///
    /// // Set up a 3-frame, 2-channel `Source`.
    /// let frame_rate_hz = 44100;
    /// let num_channels = 2;
    /// let inp = vec![-1.0, -0.5, 0.1, 0.1, 0.5, 1.0];
    /// let source = WaveformSource::from_interleaved_samples(
    ///     frame_rate_hz, num_channels, &inp
    /// );
    ///
    /// // Prepend 3 frames (6 samples) of silent zero values.
    /// let out = source.prepend_zeros(3);
    ///
    /// // Test that it works.
    /// let out_samples = out.collect_interleaved_samples();
    /// assert_eq!(
    ///     &out_samples,
    ///     &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, -0.5, 0.1, 0.1, 0.5, 1.0],
    /// );
    /// ```
    #[inline]
    fn prepend_zeros(self, num_frames: usize) -> PrependZeros<Self>
    where
        Self: Sized,
    {
        PrependZeros::new(self, num_frames)
    }

    /// Pad the *end* of the [`Source`] with silence.
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, WaveformSource};
    ///
    /// // Set up a 3-frame, 2-channel `Source`.
    /// let frame_rate_hz = 44100;
    /// let num_channels = 2;
    /// let inp = vec![-1.0, -0.5, 0.1, 0.1, 0.5, 1.0];
    /// let source = WaveformSource::from_interleaved_samples(
    ///     frame_rate_hz, num_channels, &inp
    /// );
    ///
    /// // Append 3 frames (6 samples) of silent zero values.
    /// let out = source.append_zeros(3);
    ///
    /// // Test that it works.
    /// let out_samples = out.collect_interleaved_samples();
    /// assert_eq!(
    ///     &out_samples,
    ///     &[-1.0, -0.5, 0.1, 0.1, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    /// );
    /// ```
    #[inline]
    fn append_zeros(self, num_frames: usize) -> AppendZeros<Self>
    where
        Self: Sized,
    {
        AppendZeros::new(self, num_frames)
    }

    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, WaveformSource};
    ///
    /// // Set up a 3-frame, 2-channel `Source`.
    /// let frame_rate_hz = 44100;
    /// let num_channels = 2;
    /// let inp = vec![-1.0, -0.5, 0.0, 0.0, 0.5, 1.0];
    /// let source = WaveformSource::from_interleaved_samples(
    ///     frame_rate_hz, num_channels, &inp
    /// );
    ///
    /// // Alter the audio's gain by -10 dbFS.
    /// let out = source.gain_dbfs(-10.0);
    ///
    /// // Test that it works.
    /// let out_samples = out.collect_interleaved_samples();
    /// assert_eq!(
    ///     &out_samples,
    ///     &[-0.31622776, -0.15811388, 0.0, 0.0, 0.15811388, 0.31622776],
    /// );
    /// ```
    #[inline]
    fn gain_dbfs(self, dbfs: f32) -> Gain<Self>
    where
        Self: Sized,
    {
        Gain::new(self, dbfs)
    }

    /// Multiply each sample by a constant factor.
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, WaveformSource};
    ///
    /// // Set up a 3-frame, 2-channel `Source`.
    /// let frame_rate_hz = 44100;
    /// let num_channels = 2;
    /// let inp = vec![-1.0, -0.5, 0.0, 0.0, 0.5, 1.0];
    /// let source = WaveformSource::from_interleaved_samples(
    ///     frame_rate_hz, num_channels, &inp
    /// );
    ///
    /// // Multiply each sample by 0.25.
    /// let out = source.scale(0.25);
    ///
    /// // Test that it works.
    /// let out_samples = out.collect_interleaved_samples();
    /// assert_eq!(
    ///     out_samples,
    ///     &[-0.25, -0.125, 0.0, 0.0, 0.125, 0.25],
    /// );
    /// ```
    #[inline]
    fn scale(self, constant: f32) -> Scale<Self>
    where
        Self: Sized,
    {
        Scale::new(self, constant)
    }

    /// Skip the first `n` frames.
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, WaveformSource};
    ///
    /// let frame_rate_hz = 44100;
    /// let num_channels = 2;
    /// let inp = vec![-1.0, 1.0, -0.5, 0.5, -0.0, 0.0, -0.5, 0.5, -1.0, 1.0];
    /// let source = WaveformSource::from_interleaved_samples(
    ///     frame_rate_hz, num_channels, &inp
    /// );
    ///
    /// // Skip the first 2 frames in the `Source`.
    /// let out = source.skip_frames(2);
    ///
    /// // Test that it works.
    /// let out_samples = out.collect_interleaved_samples();
    /// assert_eq!(
    ///     out_samples,
    ///     &[-0.0, 0.0, -0.5, 0.5, -1.0, 1.0],
    /// );
    /// ```
    #[inline]
    fn skip_frames(self, num_frames: usize) -> SkipFrames<Self>
    where
        Self: Sized,
    {
        SkipFrames::new(self, num_frames)
    }

    #[inline]
    fn sum<S2: Source + Sized>(self, second: S2) -> Sum<Self, S2>
    where
        Self: Sized,
    {
        Sum::new(self, second, 0)
    }

    #[inline]
    fn sum_with_frame_offset<S2: Source + Sized>(
        self,
        second: S2,
        offset_frames: usize,
    ) -> Sum<Self, S2>
    where
        Self: Sized,
    {
        Sum::new(self, second, offset_frames)
    }

    /// Take the first `n` frames.
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, WaveformSource};
    ///
    /// let frame_rate_hz = 44100;
    /// let num_channels = 2;
    /// let inp = vec![-1.0, 1.0, -0.5, 0.5, -0.0, 0.0, -0.5, 0.5, -1.0, 1.0];
    /// let source = WaveformSource::from_interleaved_samples(
    ///     frame_rate_hz, num_channels, &inp
    /// );
    ///
    /// // Take the first 3 frames.
    /// let out = source.take_frames(3);
    /// let out_samples = out.collect_interleaved_samples();
    /// assert_eq!(
    ///     out_samples,
    ///     &[-1.0, 1.0, -0.5, 0.5, -0.0, 0.0],
    /// );
    /// ```
    #[inline]
    fn take_frames(self, num_frames: usize) -> TakeFrames<Self>
    where
        Self: Sized,
    {
        TakeFrames::new(self, num_frames)
    }

    /// Select an interval of frames between `[start_idx, end_idx)`
    /// (including `start_idx` and excluding `end_idx`).
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, WaveformSource};
    /// let inp = vec![-1.0, 1.0, -0.5, 0.5, -0.0, 0.0, -0.5, 0.5, -1.0, 1.0];
    ///
    ///
    /// // Select frames 1-3.
    /// let source = WaveformSource::from_interleaved_samples(44100, 2, &inp);
    /// let selected = source.select_frames(1, 3);
    /// let out = selected.collect_interleaved_samples();
    /// assert_eq!(out, &[-0.5, 0.5, -0.0, 0.0]);
    ///
    ///
    /// // Select the first 2 frames.
    /// let source = WaveformSource::from_interleaved_samples(44100, 2, &inp);
    /// let selected = source.select_frames(0, 2);
    /// let out = selected.collect_interleaved_samples();
    /// assert_eq!(out, &[-1.0, 1.0, -0.5, 0.5]);
    ///
    ///
    /// // Skip the first 2 frames and select the remainder.
    /// let source = WaveformSource::from_interleaved_samples(44100, 2, &inp);
    /// let selected = source.select_frames(2, 0);
    /// let out = selected.collect_interleaved_samples();
    /// assert_eq!(out, &[-0.0, 0.0, -0.5, 0.5, -1.0, 1.0]);
    ///
    ///
    /// // Select all of the frames.
    /// let source = WaveformSource::from_interleaved_samples(44100, 2, &inp);
    /// let selected = source.select_frames(0, 0);
    /// let out = selected.collect_interleaved_samples();
    /// assert_eq!(out, &[-1.0, 1.0, -0.5, 0.5, -0.0, 0.0, -0.5, 0.5, -1.0, 1.0]);
    /// ```
    #[inline]
    fn select_frames<'a>(self, start_frame_idx: usize, end_frame_idx: usize) -> Box<dyn Source + 'a>
    where
        Self: 'a + Sized,
    {
        match (start_frame_idx, end_frame_idx) {
            (0, 0) => Box::new(self),
            (_, 0) => Box::new(self.skip_frames(start_frame_idx)),
            (0, _) => Box::new(self.take_frames(end_frame_idx)),
            (_, _) => Box::new(
                self.skip_frames(start_frame_idx)
                    .take_frames(end_frame_idx.saturating_sub(start_frame_idx)),
            ),
        }
    }

    /// Select the first `n` channels.
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, Waveform};
    ///
    /// // Create a 4-frame, 3-channel `Source`.
    /// let frame_rate_hz: u32 = 44100;
    /// let num_channels: u16 = 3;
    /// let interleaved_samples = vec![-1.0, 1.0, 0.0, -0.5, 0.5, 0.0, -0.5, 0.5, 0.0, -1.0, 1.0, 0.0];
    /// let s = Waveform::new(frame_rate_hz, num_channels, interleaved_samples).into_source();
    ///
    /// // Select the first 2 channels out of a 3-channel `Source`.
    /// let output_samples = s.select_first_channels(2).collect_interleaved_samples();
    ///
    /// // Test that it works.
    /// assert_eq!(
    ///     output_samples,
    ///     &[-1.0, 1.0, -0.5, 0.5, -0.5, 0.5, -1.0, 1.0],
    /// );
    /// ```
    #[inline]
    fn select_first_channels(self, selected_num_channels: u16) -> SelectChannels<Self>
    where
        Self: Sized,
    {
        SelectChannels::new(self, selected_num_channels)
    }

    /// Average the samples in each channel to produce a 1-channel monophonic [`Source`].
    ///
    /// # Examples
    /// ```
    /// use babycat::{Source, Waveform};
    ///
    /// let interleaved_samples = vec![-1.0, 0.5, -0.5, 0.25, -0.25, 0.125];
    /// let s = Waveform::new(44100, 2, interleaved_samples).into_source();
    ///
    /// let output_samples = s.convert_to_mono().collect_interleaved_samples();
    ///
    /// assert_eq!(
    ///     output_samples,
    ///     &[-0.25, -0.125, -0.0625],
    /// );
    /// ```
    #[inline]
    fn convert_to_mono(self) -> ConvertToMono<Self>
    where
        Self: Sized,
    {
        ConvertToMono::new(self)
    }

    /// Return a [`Vec<f32>`](std::vec::Vec) of collected interleaved samples.
    #[inline]
    fn collect_interleaved_samples(self) -> Vec<f32>
    where
        Self: Sized,
    {
        self.collect()
    }

    /// Collect all samples into memory and return a [`Waveform`].
    ///
    /// # Examples
    /// ```
    /// use babycat::assertions::assert_debug;
    /// use babycat::{decoder::SymphoniaDecoder, Source, Signal};
    ///
    /// // Decode an audio file into a `SymphoniaDecoder` iterator.
    /// let mut decoder = SymphoniaDecoder::from_file("audio-for-tests/circus-of-freaks/track.flac").unwrap();
    /// assert_debug(
    ///     &decoder,
    ///     "SymphoniaDecoder { 2491247 frames,  2 channels,  44100 hz,  56s 490ms }"
    /// );
    ///
    /// // Convert the `SymphoniaDecoder` iterator into a `Waveform` struct,
    /// // which loads all of the audio samples into memory.
    /// let waveform = decoder.to_waveform();
    /// assert_debug(
    ///     &waveform,
    ///     "Waveform { 2491247 frames,  2 channels,  44100 hz,  56s 490ms }"
    /// );
    /// ```
    #[inline]
    fn to_waveform(self) -> Waveform
    where
        Self: Sized,
    {
        let frame_rate_hz = self.frame_rate_hz();
        let num_channels = self.num_channels();
        let interleaved_samples = self.collect_interleaved_samples();
        Waveform::new(frame_rate_hz, num_channels, interleaved_samples)
    }

    /// Collect all samples and return a [`WaveformSource`].
    ///
    /// # Examples
    /// ```
    /// use babycat::assertions::assert_debug;
    /// use babycat::{decoder::SymphoniaDecoder, Source, Signal};
    ///
    /// // Decode an audio file into a `SymphoniaDecoder` iterator.
    /// let mut decoder = SymphoniaDecoder::from_file("audio-for-tests/circus-of-freaks/track.flac").unwrap();
    /// assert_debug(
    ///     &decoder,
    ///     "SymphoniaDecoder { 2491247 frames,  2 channels,  44100 hz,  56s 490ms }"
    /// );
    ///
    /// // Convert the `SymphoniaDecoder` iterator into a `WaveformSource` iterator,
    /// // which loads all of the audio samples into memory.
    /// let waveform_source = decoder.to_waveform_source();
    /// assert_debug(
    ///     &waveform_source,
    ///     "WaveformSource { 2491247 frames,  2 channels,  44100 hz,  56s 490ms }"
    /// );
    ///
    #[inline]
    fn to_waveform_source(self) -> WaveformSource
    where
        Self: Sized,
    {
        let waveform = self.to_waveform();
        waveform.into_source()
    }
}

impl Source for Box<dyn Source + '_> {}

impl Signal for Box<dyn Source + '_> {
    #[inline]
    fn frame_rate_hz(&self) -> u32 {
        (&**self).frame_rate_hz()
    }

    #[inline]
    fn num_channels(&self) -> u16 {
        (&**self).num_channels()
    }

    #[inline]
    fn num_frames_estimate(&self) -> Option<usize> {
        (&**self).num_frames_estimate()
    }
}