audio_core/
channel_mut.rs

1use crate::Channel;
2
3/// One channel of audio samples, usually one of several channels in a
4/// multichannel buffer
5///
6/// This trait provides read and write access.
7///
8/// See [BufMut::get_channel_mut][crate::BufMut::get_channel_mut].
9pub trait ChannelMut: Channel {
10    /// A reborrowed mutable channel.
11    type ChannelMut<'this>: ChannelMut<Sample = Self::Sample>
12    where
13        Self: 'this;
14
15    /// A mutable iterator over a channel.
16    type IterMut<'this>: Iterator<Item = &'this mut Self::Sample>
17    where
18        Self: 'this;
19
20    /// Reborrow the channel mutably.
21    fn as_channel_mut(&mut self) -> Self::ChannelMut<'_>;
22
23    /// Construct a mutable iterator over the channel
24    fn iter_mut(&mut self) -> Self::IterMut<'_>;
25
26    /// Get the frame at the given offset in the channel.
27    ///
28    /// # Examples
29    ///
30    /// ```
31    /// use audio::{BufMut, ChannelMut};
32    ///
33    /// fn test(mut buf: impl BufMut<Sample = i16>) {
34    ///     for mut chan in buf.iter_channels_mut() {
35    ///         if let Some(f) = chan.get_mut(2) {
36    ///             *f = 1;
37    ///         }
38    ///     }
39    /// }
40    ///
41    /// test(&mut audio::dynamic![[0; 16]; 2]);
42    /// test(&mut audio::sequential![[0; 16]; 2]);
43    /// test(&mut audio::interleaved![[0; 16]; 2]);
44    /// ```
45    fn get_mut(&mut self, n: usize) -> Option<&mut Self::Sample>;
46
47    /// Try to access the current channel as a mutable linear buffer.
48    ///
49    /// This is available because it could permit for some optimizations.
50    ///
51    /// # Examples
52    ///
53    /// ```
54    /// use audio::{BufMut, Channel, ChannelMut};
55    ///
56    /// fn test(buf: &mut impl BufMut<Sample = f32>) {
57    ///     let is_linear = if let Some(linear) = buf.get_channel_mut(0).unwrap().try_as_linear_mut() {
58    ///         linear[2] = 1.0;
59    ///         true
60    ///     } else {
61    ///         false
62    ///     };
63    ///
64    ///     if is_linear {
65    ///         assert_eq!(buf.get_channel(0).and_then(|c| c.get(2)), Some(1.0));
66    ///     }
67    /// }
68    ///
69    /// test(&mut audio::dynamic![[0.0; 8]; 2]);
70    /// test(&mut audio::sequential![[0.0; 8]; 2]);
71    /// test(&mut audio::interleaved![[0.0; 8]; 2]);
72    /// ```
73    fn try_as_linear_mut(&mut self) -> Option<&mut [Self::Sample]>;
74
75    /// Replace all samples in the channel with the specified value
76    ///
77    /// # Example
78    ///
79    /// ```
80    /// use audio::ChannelMut;
81    ///
82    /// let mut buf = audio::sequential![[0; 2]; 2];
83    /// for mut channel in buf.iter_channels_mut() {
84    ///     channel.fill(1);
85    /// }
86    /// assert_eq!(buf.get_channel(0).unwrap().as_ref(), &[1, 1]);
87    /// assert_eq!(buf.get_channel(1).unwrap().as_ref(), &[1, 1]);
88    /// ```
89    fn fill(&mut self, value: Self::Sample) {
90        for sample in self.iter_mut() {
91            *sample = value;
92        }
93    }
94}