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
//! Trait for dealing with abstract channel buffers.

use crate::Channel;

#[macro_use]
mod macros;

mod skip;
pub use self::skip::Skip;

mod limit;
pub use self::limit::Limit;

mod tail;
pub use self::tail::Tail;

/// The base trait available to all audio buffers.
///
/// This provides information which is available to all buffers, such as the
/// number of channels.
///
/// ```
/// let buf = audio::interleaved![[0; 4]; 2];
/// assert_eq!(buf.channels(), 2);
/// ```
///
/// It also carries a number of slicing combinators, wuch as [skip][Buf::skip]
/// and [limit][Buf::limit] which allows an audio buffer to be sliced as needed.
///
///
/// ```
/// use audio::{Buf, ExactSizeBuf};
///
/// let buf = audio::interleaved![[0; 4]; 2];
///
/// assert_eq!(buf.channels(), 2);
/// assert_eq!(buf.limit(2).frames(), 2);
/// ```
pub trait Buf {
    /// The type of a single sample.
    type Sample;

    /// The type of the channel container.
    type Channel<'this>: Channel<Sample = Self::Sample>
    where
        Self: 'this;

    /// An iterator over available channels.
    type IterChannels<'this>: Iterator<Item = Self::Channel<'this>>
    where
        Self: 'this;

    /// A typical number of frames for each channel in the buffer, if known.
    ///
    /// If you only want to support buffers which have exact sizes use
    /// [ExactSizeBuf][crate::ExactSizeBuf].
    ///
    /// This is only a best effort hint. We can't require any [Buf] to know the
    /// exact number of frames, because we want to be able to implement it for
    /// types which does not keep track of the exact number of frames it expects
    /// each channel to have such as `Vec<Vec<i16>>`.
    ///
    /// ```
    /// use audio::Buf;
    ///
    /// fn test(buf: impl Buf) {
    ///     assert_eq!(buf.channels(), 2);
    ///     assert_eq!(buf.frames_hint(), Some(4));
    /// }
    ///
    /// test(audio::wrap::dynamic(vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]]));
    /// ```
    ///
    /// But it should be clear that such a buffer supports a variable number of
    /// frames in each channel.
    ///
    /// ```
    /// use audio::{Buf, Channel};
    ///
    /// fn test(buf: impl Buf<Sample = i16>) {
    ///     assert_eq!(buf.channels(), 2);
    ///     assert_eq!(buf.frames_hint(), Some(4));
    ///
    ///     assert_eq!(buf.get_channel(0).map(|c| c.len()), Some(4));
    ///     assert_eq!(buf.get_channel(1).map(|c| c.len()), Some(2));
    /// }
    ///
    /// test(audio::wrap::dynamic(vec![vec![1, 2, 3, 4], vec![5, 6]]));
    /// ```
    fn frames_hint(&self) -> Option<usize>;

    /// The number of channels in the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::{Buf, Channel};
    ///
    /// fn test(buf: impl Buf<Sample = i16>) {
    ///     assert_eq!(buf.channels(), 2);
    ///
    ///     assert_eq! {
    ///         buf.get_channel(0).unwrap().iter().collect::<Vec<_>>(),
    ///         &[1, 2, 3, 4],
    ///     }
    ///
    ///     assert_eq! {
    ///         buf.get_channel(1).unwrap().iter().collect::<Vec<_>>(),
    ///         &[5, 6, 7, 8],
    ///     }
    /// }
    ///
    /// test(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
    /// test(audio::wrap::interleaved(&[1, 5, 2, 6, 3, 7, 4, 8], 2));
    /// test(audio::wrap::dynamic(vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]]));
    /// ```
    fn channels(&self) -> usize;

    /// Return a handler to the buffer associated with the channel.
    ///
    /// Note that we don't access the buffer for the underlying channel directly
    /// as a linear buffer like `&[T]`, because the underlying representation
    /// might be different.
    ///
    /// We must instead make use of the various utility functions found on
    /// [Channel] to copy data out of the channel.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::{Buf, Channel};
    ///
    /// fn test(buf: impl Buf<Sample = i16>) {
    ///     let chan = buf.get_channel(1).unwrap();
    ///     chan.iter().eq([5, 6, 7, 8]);
    /// }
    ///
    /// test(audio::dynamic![[1, 2, 3, 4], [5, 6, 7, 8]]);
    /// test(audio::sequential![[1, 2, 3, 4], [5, 6, 7, 8]]);
    /// test(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
    /// ```
    fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>>;

    /// Construct an iterator over all the channels in the audio buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::{Buf, Channel};
    ///
    /// fn test(buf: impl Buf<Sample = i16>) {
    ///     let chan = buf.iter_channels().nth(1).unwrap();
    ///     chan.iter().eq([5, 6, 7, 8]);
    /// }
    ///
    /// test(audio::dynamic![[1, 2, 3, 4], [5, 6, 7, 8]]);
    /// test(audio::sequential![[1, 2, 3, 4], [5, 6, 7, 8]]);
    /// test(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
    /// ```
    fn iter_channels(&self) -> Self::IterChannels<'_>;

    /// Construct a wrapper around this buffer that skips the first `n` frames.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::Buf;
    /// use audio::buf;
    ///
    /// let from = audio::interleaved![[0, 0, 1, 1], [0; 4]];
    /// let mut to = audio::buf::Interleaved::with_topology(2, 4);
    ///
    /// buf::copy(from.skip(2), &mut to);
    ///
    /// assert_eq!(to.as_slice(), &[1, 0, 1, 0, 0, 0, 0, 0]);
    /// ```
    ///
    /// With a mutable buffer.
    ///
    /// ```
    /// use audio::Buf;
    /// use audio::{buf, wrap};
    ///
    /// let from = wrap::interleaved(&[1, 1, 1, 1, 1, 1, 1, 1], 2);
    /// let mut to = audio::buf::Interleaved::with_topology(2, 4);
    ///
    /// buf::copy(from, (&mut to).skip(2));
    ///
    /// assert_eq!(to.as_slice(), &[0, 0, 0, 0, 1, 1, 1, 1])
    /// ```
    fn skip(self, n: usize) -> Skip<Self>
    where
        Self: Sized,
    {
        Skip::new(self, n)
    }

    /// Construct a wrapper around this buffer that skips to the last `n` frames.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::Buf;
    /// use audio::buf;
    ///
    /// let from = audio::interleaved![[1; 4]; 2];
    /// let mut to = audio::interleaved![[0; 4]; 2];
    ///
    /// buf::copy(from, (&mut to).tail(2));
    ///
    /// assert_eq!(to.as_slice(), &[0, 0, 0, 0, 1, 1, 1, 1]);
    /// ```
    ///
    /// The [tail][Buf::tail] of a buffer adjusts all functions associated with
    /// the [Buf]:
    ///
    /// ```
    /// use audio::{Buf, ExactSizeBuf};
    ///
    /// let buf = audio::interleaved![[1, 2, 3, 4]; 2];
    ///
    /// assert_eq!((&buf).tail(0).channels(), 2);
    /// assert_eq!((&buf).tail(0).frames_hint(), Some(0));
    ///
    /// assert_eq!((&buf).tail(1).channels(), 2);
    /// assert_eq!((&buf).tail(1).frames_hint(), Some(1));
    ///
    /// assert_eq!((&buf).tail(5).channels(), 2);
    /// assert_eq!((&buf).tail(5).frames_hint(), Some(4));
    ///
    /// for chan in buf.tail(2).iter_channels() {
    ///     assert!(chan.iter().eq([3, 4]));
    /// }
    /// ```
    fn tail(self, n: usize) -> Tail<Self>
    where
        Self: Sized,
    {
        Tail::new(self, n)
    }

    /// Construct a wrapper around this buffer which stops after `limit` frames.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::Buf;
    /// use audio::buf;
    ///
    /// let from = audio::interleaved![[1; 4]; 2];
    /// let mut to = audio::buf::Interleaved::with_topology(2, 4);
    ///
    /// buf::copy(from, (&mut to).limit(2));
    ///
    /// assert_eq!(to.as_slice(), &[1, 1, 1, 1, 0, 0, 0, 0]);
    /// ```
    ///
    /// The [limit][Buf::limit] of a buffer adjusts all functions associated
    /// with the [Buf]:
    ///
    /// ```
    /// use audio::Buf;
    ///
    /// let buf = audio::interleaved![[1, 2, 3, 4]; 2];
    ///
    /// assert_eq!((&buf).limit(0).channels(), 2);
    /// assert_eq!((&buf).limit(0).frames_hint(), Some(0));
    ///
    /// assert_eq!((&buf).limit(1).channels(), 2);
    /// assert_eq!((&buf).limit(1).frames_hint(), Some(1));
    ///
    /// assert_eq!((&buf).limit(5).channels(), 2);
    /// assert_eq!((&buf).limit(5).frames_hint(), Some(4));
    ///
    /// for chan in buf.limit(2).iter_channels() {
    ///     assert!(chan.iter().eq([1, 2]));
    /// }
    /// ```
    fn limit(self, limit: usize) -> Limit<Self>
    where
        Self: Sized,
    {
        Limit::new(self, limit)
    }
}

impl<B> Buf for &B
where
    B: ?Sized + Buf,
{
    type Sample = B::Sample;

    type Channel<'a> = B::Channel<'a>
    where
        Self: 'a;

    type IterChannels<'a> = B::IterChannels<'a>
    where
        Self: 'a;

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

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

    #[inline]
    fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>> {
        (**self).get_channel(channel)
    }

    #[inline]
    fn iter_channels(&self) -> Self::IterChannels<'_> {
        (**self).iter_channels()
    }
}

impl<B> Buf for &mut B
where
    B: ?Sized + Buf,
{
    type Sample = B::Sample;

    type Channel<'this> = B::Channel<'this>
    where
        Self: 'this;

    type IterChannels<'this> = B::IterChannels<'this>
    where
        Self: 'this;

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

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

    #[inline]
    fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>> {
        (**self).get_channel(channel)
    }

    #[inline]
    fn iter_channels(&self) -> Self::IterChannels<'_> {
        (**self).iter_channels()
    }
}