audio_core/
buf_mut.rs

1use crate::{Buf, ChannelMut};
2
3/// A trait describing a mutable audio buffer.
4pub trait BufMut: Buf {
5    /// The type of the mutable channel container.
6    type ChannelMut<'this>: ChannelMut<Sample = Self::Sample>
7    where
8        Self: 'this;
9
10    /// A mutable iterator over available channels.
11    type IterChannelsMut<'this>: Iterator<Item = Self::ChannelMut<'this>>
12    where
13        Self: 'this;
14
15    /// Construct a mutable iterator over available channels.
16    ///
17    /// # Examples
18    ///
19    /// ```
20    /// use audio::{BufMut, ChannelMut};
21    ///
22    /// fn test(mut buf: impl BufMut<Sample = i32>) {
23    ///     for (n, mut chan) in buf.iter_channels_mut().enumerate() {
24    ///         for f in chan.iter_mut() {
25    ///             *f += n as i32 + 1;
26    ///         }
27    ///     }
28    /// }
29    ///
30    /// let mut buf = audio::dynamic![[0; 4]; 2];
31    /// test(&mut buf);
32    /// assert_eq!(
33    ///     buf.iter_channels().collect::<Vec<_>>(),
34    ///     vec![[1, 1, 1, 1], [2, 2, 2, 2]],
35    /// );
36    ///
37    /// let mut buf = audio::interleaved![[0; 4]; 2];
38    /// test(&mut buf);
39    /// assert_eq!(
40    ///     buf.iter_channels().collect::<Vec<_>>(),
41    ///     vec![[1, 1, 1, 1], [2, 2, 2, 2]],
42    /// );
43    /// ```
44    fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_>;
45
46    /// Return a mutable handler to the buffer associated with the channel.
47    ///
48    /// # Examples
49    ///
50    /// ```
51    /// use audio::{BufMut, ChannelMut};
52    ///
53    /// fn test(mut buf: impl BufMut<Sample = i32>) {
54    ///     if let Some(mut chan) = buf.get_channel_mut(1) {
55    ///         for f in chan.iter_mut() {
56    ///             *f += 1;
57    ///         }
58    ///     }
59    /// }
60    ///
61    /// let mut buf = audio::dynamic![[0; 4]; 2];
62    /// test(&mut buf);
63    /// assert_eq!(
64    ///     buf.iter_channels().collect::<Vec<_>>(),
65    ///     vec![[0, 0, 0, 0], [1, 1, 1, 1]],
66    /// );
67    /// ```
68    fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>>;
69
70    /// Copy one channel into another.
71    ///
72    /// If the channels have different sizes, the minimul difference between
73    /// them will be copied.
74    ///
75    /// # Panics
76    ///
77    /// Panics if one of the channels being tried to copy from or to is out of
78    /// bounds as reported by [Buf::channels].
79    ///
80    /// # Examples
81    ///
82    /// ```
83    /// use audio::{Buf, BufMut};
84    ///
85    /// let mut buf = audio::dynamic![[1, 2, 3, 4], [0, 0, 0, 0]];
86    /// buf.copy_channel(0, 1);
87    /// assert_eq!(buf.get_channel(1), buf.get_channel(0));
88    /// ```
89    fn copy_channel(&mut self, from: usize, to: usize)
90    where
91        Self::Sample: Copy;
92
93    /// Fill the entire buffer with the specified value
94    /// # Example
95    ///
96    /// ```
97    /// use audio::BufMut;
98    ///
99    /// let mut buf = audio::sequential![[0; 2]; 2];
100    /// buf.fill(1);
101    /// assert_eq!(buf.as_slice(), &[1, 1, 1, 1]);
102    /// ```
103    fn fill(&mut self, value: Self::Sample)
104    where
105        Self::Sample: Copy,
106    {
107        for mut channel in self.iter_channels_mut() {
108            channel.fill(value);
109        }
110    }
111}
112
113impl<B> BufMut for &mut B
114where
115    B: ?Sized + BufMut,
116{
117    type ChannelMut<'this>
118        = B::ChannelMut<'this>
119    where
120        Self: 'this;
121
122    type IterChannelsMut<'this>
123        = B::IterChannelsMut<'this>
124    where
125        Self: 'this;
126
127    #[inline]
128    fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>> {
129        (**self).get_channel_mut(channel)
130    }
131
132    #[inline]
133    fn copy_channel(&mut self, from: usize, to: usize)
134    where
135        Self::Sample: Copy,
136    {
137        (**self).copy_channel(from, to);
138    }
139
140    #[inline]
141    fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_> {
142        (**self).iter_channels_mut()
143    }
144}