pointillism 0.4.3

A compositional library for musical composition.
Documentation
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
//! Defines different types for audio buffers.
//!
//! You can load a buffer from a WAV file (if the `hound` feature is enabled), or you can create
//! your own buffer and write a signal into it, to then read it back. This can be useful if you want
//! to loop an expensive to compute signal. This is also used in [`eff::dly::Delay`].
//!
//! You can also use a buffer if you want to process large amounts of audio data before playing it
//! back. This is useful for certain algorithms, such as the
//! [FFT](https://en.wikipedia.org/wiki/Fast_Fourier_transform). Convenience methods such as
//! [`BufferMut::overwrite`] are provided for loading a buffer.
//!
//! We distinguish three different kinds of buffers: those that hold a reference to its data, those
//! that hold a mutable reference to its data, and those that own its data.

use crate::prelude::*;
use std::ops::{Index, IndexMut};

pub mod interpolate;
mod ring;
#[cfg(feature = "hound")]
pub mod wav;

pub use interpolate as int;
pub use ring::*;

/// A trait for readable buffers.
pub trait Buffer: AsRef<[Self::Item]> + std::ops::Index<usize, Output = Self::Item> {
    /// The type of sample stored in the buffer.
    type Item: Audio;

    /// Returns the length of the buffer.
    fn len(&self) -> usize {
        self.as_ref().len()
    }

    /// Returns whether the buffer is empty.
    fn is_empty(&self) -> bool {
        self.as_ref().is_empty()
    }

    /// Returns the time that takes to play this buffer.
    #[must_use]
    fn time(&self) -> unt::Time {
        unt::Time::new(crate::units::FracInt::new(self.len() as u64))
    }

    /// Returns the inner slice.    
    #[must_use]
    fn as_slice(&self) -> &[Self::Item] {
        self.as_ref()
    }

    /// Gets a sample at a given index.
    #[must_use]
    fn get(&self, index: usize) -> Option<Self::Item> {
        self.as_ref().get(index).copied()
    }

    // TODO: move these elsewhere?

    /// Returns the sample corresponding to peak amplitude on all channels.
    #[must_use]
    fn peak(&self) -> <Self::Item as smp::Array>::Array<unt::Vol> {
        /// Prevent code duplication.
        fn peak<A: Audio>(buf: &[A]) -> <A as smp::Array>::Array<unt::Vol> {
            let mut res = A::new_default();

            for sample in buf {
                A::for_each(|index| {
                    let peak = &mut res[index];
                    let new = sample[index].abs();

                    if *peak > new {
                        *peak = new;
                    }
                });
            }

            res.map_array(|&x| unt::Vol::new(x))
        }

        peak(self.as_ref())
    }

    /// Calculates the RMS on all channels.
    #[must_use]
    fn rms(&self) -> <Self::Item as smp::Array>::Array<unt::Vol> {
        /// Prevent code duplication.
        fn rms<A: Audio>(buf: &[A]) -> <A as smp::Array>::Array<unt::Vol> {
            let mut res: <A as Array>::Array<f64> = Array::new_default();

            for sample in buf {
                A::for_each(|index| {
                    let new = sample[index];
                    res[index] += new * new;
                });
            }

            // Precision loss should not occur in practice.
            #[allow(clippy::cast_precision_loss)]
            A::for_each(|index| {
                res[index] = (res[index] / buf.len() as f64).sqrt();
            });

            res.map_array(|&x| unt::Vol::new(x))
        }

        rms(self.as_ref())
    }
}

/// A trait for buffers that hold a mutable reference to its data.
pub trait BufferMut:
    Buffer + AsMut<[Self::Item]> + std::ops::IndexMut<usize, Output = Self::Item>
{
    /// Returns a mutable reference to the inner slice.
    fn as_mut_slice(&mut self) -> &mut [Self::Item] {
        self.as_mut()
    }

    /// Currently, `rust-analyzer` trips up sometimes that `as_mut` is called directly, not
    /// recognizing that it can be dereferenced. This hack bypasses this.
    fn _as_mut(&mut self) -> &mut [Self::Item] {
        self.as_mut()
    }

    /// Gets a mutable reference to a sample at a given index.
    fn get_mut(&mut self, index: usize) -> Option<&mut Self::Item> {
        self._as_mut().get_mut(index)
    }

    /// Overwrites a buffer with the output from a song.
    ///
    /// The passed `time` parameter is used for the function.
    fn overwrite_time<F: FnMut(unt::Time) -> Self::Item>(
        &mut self,
        time: &mut unt::Time,
        mut song: F,
    ) {
        for sample in self._as_mut() {
            *sample = song(*time);
            time.advance();
        }
    }

    /// Overwrites a buffer with the output from a song.
    ///
    /// The timer starts at zero. Use [`Self::overwrite_time`] to specify the time.
    fn overwrite<F: FnMut(unt::Time) -> Self::Item>(&mut self, song: F) {
        let mut time = unt::Time::ZERO;
        self.overwrite_time(&mut time, song);
    }

    /// Overwrites a buffer with the output from a signal. The signal is not consumed.
    fn overwrite_from_sgn<S: SignalMut<Sample = Self::Item>>(&mut self, sgn: &mut S) {
        self.overwrite(|_| sgn.next());
    }

    /// Clears a buffer, without changing its length.
    fn clear(&mut self) {
        self.overwrite(|_| smp::SampleBase::ZERO);
    }
}

/// A buffer that holds a reference to its data.
#[derive(Clone, Debug)]
pub struct Slice<'a, A: Audio> {
    pub data: &'a [A],
}

/// A buffer that holds a mutable reference to its data.
#[derive(Debug)]
pub struct SliceMut<'a, A: Audio> {
    pub data: &'a mut [A],
}

/// A statically allocated, owned sample buffer.
#[derive(Clone, Copy, Debug)]
pub struct Stc<A: Audio, const N: usize> {
    /// The data stored by the buffer.
    pub data: [A; N],
}

/// A dynamically allocated, owned sample buffer.
#[derive(Clone, Debug, Default)]
pub struct Dyn<A: Audio> {
    /// The data stored by the buffer.
    pub data: Vec<A>,
}

impl<'a, A: Audio> AsRef<[A]> for Slice<'a, A> {
    fn as_ref(&self) -> &[A] {
        self.data
    }
}

impl<'a, A: Audio> AsRef<[A]> for SliceMut<'a, A> {
    fn as_ref(&self) -> &[A] {
        self.data
    }
}

impl<A: Audio, const N: usize> AsRef<[A]> for Stc<A, N> {
    fn as_ref(&self) -> &[A] {
        &self.data
    }
}

impl<A: Audio> AsRef<[A]> for Dyn<A> {
    fn as_ref(&self) -> &[A] {
        &self.data
    }
}

impl<'a, A: Audio> AsMut<[A]> for SliceMut<'a, A> {
    fn as_mut(&mut self) -> &mut [A] {
        self.data
    }
}

impl<A: Audio, const N: usize> AsMut<[A]> for Stc<A, N> {
    fn as_mut(&mut self) -> &mut [A] {
        &mut self.data
    }
}

impl<A: Audio> AsMut<[A]> for Dyn<A> {
    fn as_mut(&mut self) -> &mut [A] {
        &mut self.data
    }
}

impl<'a, A: Audio> Index<usize> for Slice<'a, A> {
    type Output = A;

    fn index(&self, index: usize) -> &A {
        &self.as_ref()[index]
    }
}

impl<'a, A: Audio> Index<usize> for SliceMut<'a, A> {
    type Output = A;

    fn index(&self, index: usize) -> &A {
        &self.as_ref()[index]
    }
}

impl<A: Audio, const N: usize> Index<usize> for Stc<A, N> {
    type Output = A;

    fn index(&self, index: usize) -> &A {
        &self.as_ref()[index]
    }
}

impl<A: Audio> Index<usize> for Dyn<A> {
    type Output = A;

    fn index(&self, index: usize) -> &A {
        &self.as_ref()[index]
    }
}

impl<'a, A: Audio> IndexMut<usize> for SliceMut<'a, A> {
    fn index_mut(&mut self, index: usize) -> &mut A {
        &mut self.as_mut()[index]
    }
}

impl<A: Audio, const N: usize> IndexMut<usize> for Stc<A, N> {
    fn index_mut(&mut self, index: usize) -> &mut A {
        &mut self.as_mut()[index]
    }
}

impl<A: Audio> IndexMut<usize> for Dyn<A> {
    fn index_mut(&mut self, index: usize) -> &mut A {
        &mut self.as_mut()[index]
    }
}

impl<'a, A: Audio> Buffer for Slice<'a, A> {
    type Item = A;
}

impl<'a, A: Audio> Buffer for SliceMut<'a, A> {
    type Item = A;
}

impl<A: Audio, const N: usize> Buffer for Stc<A, N> {
    type Item = A;
}

impl<A: Audio> Buffer for Dyn<A> {
    type Item = A;
}

impl<'a, A: Audio> BufferMut for SliceMut<'a, A> {}
impl<A: Audio, const N: usize> BufferMut for Stc<A, N> {}
impl<A: Audio> BufferMut for Dyn<A> {}

impl<A: Audio, const N: usize> Default for Stc<A, N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a, A: Audio> Slice<'a, A> {
    /// Initializes a new [`buf::Slice`].
    pub const fn new(data: &'a [A]) -> Self {
        Self { data }
    }
}

impl<'a, A: Audio> SliceMut<'a, A> {
    /// Initializes a new [`buf::SliceMut`].
    pub fn new(data: &'a mut [A]) -> Self {
        Self { data }
    }

    /// Converts `self` into a `BufRef`.
    ///
    /// Notes that this consumes the buffer, as mutable aliasing is prohibited.
    #[must_use]
    pub const fn buf_ref(self) -> Slice<'a, A> {
        Slice::new(self.data)
    }
}

impl<A: Audio, const N: usize> Stc<A, N> {
    /// Initializes a new [`Stc`] from data.
    pub const fn from_data(data: [A; N]) -> Self {
        Self { data }
    }

    /// Initializes the zero [`Stc`].
    ///
    /// Use [`Self::from_data`] to initialize this with pre-existing data.
    #[must_use]
    pub const fn new() -> Self {
        Self::from_data([A::ZERO; N])
    }

    /// Converts `self` into a [`Slice`].
    #[must_use]
    pub fn slice(&self) -> Slice<A> {
        Slice::new(&self.data)
    }

    /// Converts `self` into a [`SliceMut`].
    #[must_use]
    pub fn slice_mut(&mut self) -> SliceMut<A> {
        SliceMut::new(&mut self.data)
    }
}

impl<A: Audio> Dyn<A> {
    /// Initializes a new [`Dyn`] from data.
    #[must_use]
    pub const fn from_data(data: Vec<A>) -> Self {
        Self { data }
    }

    /// Initializes an empty buffer with a given size. All samples are initialized to zero.
    ///
    /// Use [`Self::from_data`] to initialize this with pre-existing data.
    pub fn new(samples: usize) -> Self {
        Self::from_data(vec![A::ZERO; samples])
    }

    /// Initializes an empty buffer with a given length, rounded down to the nearest sample. All
    /// samples are initialized to zero.
    ///
    /// ## Panics
    ///
    /// On a 32-bit machine, panics if the buffer is too large.
    #[must_use]
    pub fn new_time(time: unt::Time) -> Self {
        Self::new(time.samples.int().try_into().expect("buffer too large"))
    }

    /// Initializes a buffer with zero initial capacity.
    #[must_use]
    pub const fn empty() -> Self {
        Self::from_data(Vec::new())
    }

    /// Converts `self` into a [`buf::Slice`].
    #[must_use]
    pub fn slice(&self) -> Slice<A> {
        Slice::new(&self.data)
    }

    /// Converts `self` into a [`buf::SliceMut`].
    #[must_use]
    pub fn slice_mut(&mut self) -> SliceMut<A> {
        SliceMut::new(&mut self.data)
    }

    /// Iterates over the slice.
    pub fn iter(&self) -> std::slice::Iter<A> {
        self.into_iter()
    }

    /// Mutably iterates over the slice.
    pub fn iter_mut(&mut self) -> std::slice::IterMut<A> {
        self.into_iter()
    }

    /// Creates a buffer from the output of a song.
    ///
    /// Compare to [`crate::create`].
    ///
    /// ## Panics
    ///
    /// Panics if a buffer of this size can't be created.
    pub fn create<F: FnMut(unt::Time) -> A>(length: unt::Time, mut song: F) -> Self {
        let length = length.samples.int();
        let mut data = Vec::with_capacity(usize::try_from(length).expect("buffer too large"));

        let mut time = unt::Time::ZERO;
        for _ in 0..length {
            data.push(song(time));
            time.advance();
        }

        Self::from_data(data)
    }

    /// Creates a buffer from the output of a signal. The signal is not consumed.
    ///
    /// Compare to [`crate::create_from_sgn`].
    ///
    /// ## Panics
    ///
    /// Panics if a buffer of this size can't be created.
    pub fn create_from_sgn<S: SignalMut<Sample = A>>(length: unt::Time, sgn: &mut S) -> Self {
        Self::create(length, |_| sgn.next())
    }
}

impl<A: Audio> From<Vec<A>> for Dyn<A> {
    fn from(data: Vec<A>) -> Self {
        Self::from_data(data)
    }
}

impl<A: Audio> FromIterator<A> for Dyn<A> {
    fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
        Self::from_data(FromIterator::from_iter(iter))
    }
}

impl<A: Audio> IntoIterator for Dyn<A> {
    type IntoIter = std::vec::IntoIter<A>;
    type Item = A;

    fn into_iter(self) -> Self::IntoIter {
        self.data.into_iter()
    }
}

impl<'a, A: Audio> IntoIterator for &'a Dyn<A> {
    type IntoIter = std::slice::Iter<'a, A>;
    type Item = &'a A;

    fn into_iter(self) -> Self::IntoIter {
        self.data.iter()
    }
}

impl<'a, A: Audio> IntoIterator for &'a mut Dyn<A> {
    type IntoIter = std::slice::IterMut<'a, A>;
    type Item = &'a mut A;

    fn into_iter(self) -> Self::IntoIter {
        self.data.iter_mut()
    }
}

/// An empty buffer.
pub type Empty<A> = Stc<A, 0>;