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
use rotary_core::{Buf, BufMut, ExactSizeBuf};
use rotary_core::{Channel, ChannelMut};
pub struct Sequential<T> {
value: T,
frames: usize,
}
impl<T> Sequential<T> {
pub(super) fn new(value: T, frames: usize) -> Self {
Self { value, frames }
}
}
macro_rules! impl_buf {
([$($p:tt)*] , $ty:ty $(, $len:ident)?) => {
impl<$($p)*> ExactSizeBuf for Sequential<$ty> {
fn frames(&self) -> usize {
self.frames
}
}
impl<$($p)*> Buf<T> for Sequential<$ty> {
fn frames_hint(&self) -> Option<usize> {
Some(self.frames)
}
fn channels(&self) -> usize {
impl_buf!(@frames self, $($len)*) / self.frames
}
fn channel(&self, channel: usize) -> Channel<'_, T> {
let value = &self.value[channel * self.frames..];
let value = &value[..self.frames];
Channel::linear(value)
}
}
};
(@frames $s:ident,) => { $s.value.len() };
(@frames $_:ident, $n:ident) => { $n };
}
impl_buf!([T], &'_ [T]);
impl_buf!([T], &'_ mut [T]);
impl_buf!([T, const N: usize], [T; N], N);
impl_buf!([T, const N: usize], &'_ [T; N], N);
impl_buf!([T, const N: usize], &'_ mut [T; N], N);
macro_rules! impl_buf_mut {
([$($p:tt)*], $ty:ty) => {
impl<$($p)*> BufMut<T> for Sequential<$ty> {
fn channel_mut(&mut self, channel: usize) -> ChannelMut<'_, T> {
let value = &mut self.value[channel * self.frames..];
let value = &mut value[..self.frames];
ChannelMut::linear(value)
}
}
};
}
impl_buf_mut!([T], &'_ mut [T]);
impl_buf_mut!([T, const N: usize], &'_ mut [T; N]);