audio-core 0.2.0-alpha.4

The core audio traits
Documentation
use crate::Channel;

/// The mutable buffer of a single channel.
///
/// This doesn't provide direct access to the underlying buffer, but rather
/// allows us to copy data usinga  number of utility functions.
///
/// See [BufMut::get_mut][crate::BufMut::get_mut].
pub trait ChannelMut: Channel {
    /// A reborrowed mutable channel.
    type ChannelMut<'this>: ChannelMut<Sample = Self::Sample>
    where
        Self: 'this;

    /// A mutable iterator over a channel.
    type IterMut<'this>: Iterator<Item = &'this mut Self::Sample>
    where
        Self: 'this;

    /// Reborrow the channel mutably.
    fn as_channel_mut(&mut self) -> Self::ChannelMut<'_>;

    /// Construct a mutable iterator over the channel
    fn iter_mut(&mut self) -> Self::IterMut<'_>;

    /// Get the frame at the given offset in the channel.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::{BufMut, ChannelMut};
    ///
    /// fn test(mut buf: impl BufMut<Sample = i16>) {
    ///     for mut chan in buf.iter_mut() {
    ///         if let Some(f) = chan.get_mut(2) {
    ///             *f = 1;
    ///         }
    ///     }
    /// }
    ///
    /// test(&mut audio::dynamic![[0; 16]; 2]);
    /// test(&mut audio::sequential![[0; 16]; 2]);
    /// test(&mut audio::interleaved![[0; 16]; 2]);
    /// ```
    fn get_mut(&mut self, n: usize) -> Option<&mut Self::Sample>;

    /// Try to access the current channel as a mutable linear buffer.
    ///
    /// This is available because it could permit for some optimizations.
    ///
    /// # Examples
    ///
    /// ```
    /// use audio::{BufMut, Channel, ChannelMut};
    ///
    /// fn test(buf: &mut impl BufMut<Sample = f32>) {
    ///     let is_linear = if let Some(linear) = buf.get_mut(0).unwrap().try_as_linear_mut() {
    ///         linear[2] = 1.0;
    ///         true
    ///     } else {
    ///         false
    ///     };
    ///
    ///     if is_linear {
    ///         assert_eq!(buf.get(0).and_then(|c| c.get(2)), Some(1.0));
    ///     }
    /// }
    ///
    /// test(&mut audio::dynamic![[0.0; 8]; 2]);
    /// test(&mut audio::sequential![[0.0; 8]; 2]);
    /// test(&mut audio::interleaved![[0.0; 8]; 2]);
    /// ```
    fn try_as_linear_mut(&mut self) -> Option<&mut [Self::Sample]>;
}