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
use crate::{Buf, ChannelMut};
/// A trait describing a mutable audio buffer.
pub trait BufMut: Buf {
/// The type of the mutable channel container.
type ChannelMut<'this>: ChannelMut<Sample = Self::Sample>
where
Self: 'this;
/// A mutable iterator over available channels.
type IterChannelsMut<'this>: Iterator<Item = Self::ChannelMut<'this>>
where
Self: 'this;
/// Construct a mutable iterator over available channels.
///
/// # Examples
///
/// ```
/// use audio::{BufMut, ChannelMut};
///
/// fn test(mut buf: impl BufMut<Sample = i32>) {
/// for (n, mut chan) in buf.iter_channels_mut().enumerate() {
/// for f in chan.iter_mut() {
/// *f += n as i32 + 1;
/// }
/// }
/// }
///
/// let mut buf = audio::dynamic![[0; 4]; 2];
/// test(&mut buf);
/// assert_eq!(
/// buf.iter_channels().collect::<Vec<_>>(),
/// vec![[1, 1, 1, 1], [2, 2, 2, 2]],
/// );
///
/// let mut buf = audio::interleaved![[0; 4]; 2];
/// test(&mut buf);
/// assert_eq!(
/// buf.iter_channels().collect::<Vec<_>>(),
/// vec![[1, 1, 1, 1], [2, 2, 2, 2]],
/// );
/// ```
fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_>;
/// Return a mutable handler to the buffer associated with the channel.
///
/// # Examples
///
/// ```
/// use audio::{BufMut, ChannelMut};
///
/// fn test(mut buf: impl BufMut<Sample = i32>) {
/// if let Some(mut chan) = buf.get_channel_mut(1) {
/// for f in chan.iter_mut() {
/// *f += 1;
/// }
/// }
/// }
///
/// let mut buf = audio::dynamic![[0; 4]; 2];
/// test(&mut buf);
/// assert_eq!(
/// buf.iter_channels().collect::<Vec<_>>(),
/// vec![[0, 0, 0, 0], [1, 1, 1, 1]],
/// );
/// ```
fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>>;
/// Copy one channel into another.
///
/// If the channels have different sizes, the minimul difference between
/// them will be copied.
///
/// # Panics
///
/// Panics if one of the channels being tried to copy from or to is out of
/// bounds as reported by [Buf::channels].
///
/// # Examples
///
/// ```
/// use audio::{Buf, BufMut};
///
/// let mut buf = audio::dynamic![[1, 2, 3, 4], [0, 0, 0, 0]];
/// buf.copy_channel(0, 1);
/// assert_eq!(buf.get_channel(1), buf.get_channel(0));
/// ```
fn copy_channel(&mut self, from: usize, to: usize)
where
Self::Sample: Copy;
/// Fill the entire buffer with the specified value
/// # Example
///
/// ```
/// use audio::BufMut;
///
/// let mut buf = audio::sequential![[0; 2]; 2];
/// buf.fill(1);
/// assert_eq!(buf.as_slice(), &[1, 1, 1, 1]);
/// ```
fn fill(&mut self, value: Self::Sample)
where
Self::Sample: Copy,
{
for mut channel in self.iter_channels_mut() {
channel.fill(value);
}
}
}
impl<B> BufMut for &mut B
where
B: ?Sized + BufMut,
{
type ChannelMut<'this>
= B::ChannelMut<'this>
where
Self: 'this;
type IterChannelsMut<'this>
= B::IterChannelsMut<'this>
where
Self: 'this;
#[inline]
fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>> {
(**self).get_channel_mut(channel)
}
#[inline]
fn copy_channel(&mut self, from: usize, to: usize)
where
Self::Sample: Copy,
{
(**self).copy_channel(from, to);
}
#[inline]
fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_> {
(**self).iter_channels_mut()
}
}