Trait audio::ChannelMut

source ·
pub trait ChannelMut: Channel {
    type ChannelMut<'this>: ChannelMut<Sample = Self::Sample>
       where Self: 'this;
    type IterMut<'this>: Iterator<Item = &'this mut Self::Sample>
       where Self: 'this;

    // Required methods
    fn as_channel_mut(&mut self) -> Self::ChannelMut<'_>;
    fn iter_mut(&mut self) -> Self::IterMut<'_>;
    fn get_mut(&mut self, n: usize) -> Option<&mut Self::Sample>;
    fn try_as_linear_mut(&mut self) -> Option<&mut [Self::Sample]>;

    // Provided method
    fn fill(&mut self, value: Self::Sample) { ... }
}
Expand description

One channel of audio samples, usually one of several channels in a multichannel buffer

This trait provides read and write access.

See BufMut::get_channel_mut.

Required Associated Types§

source

type ChannelMut<'this>: ChannelMut<Sample = Self::Sample> where Self: 'this

A reborrowed mutable channel.

source

type IterMut<'this>: Iterator<Item = &'this mut Self::Sample> where Self: 'this

A mutable iterator over a channel.

Required Methods§

source

fn as_channel_mut(&mut self) -> Self::ChannelMut<'_>

Reborrow the channel mutably.

source

fn iter_mut(&mut self) -> Self::IterMut<'_>

Construct a mutable iterator over the channel

source

fn get_mut(&mut self, n: usize) -> Option<&mut Self::Sample>

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_channels_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]);
source

fn try_as_linear_mut(&mut self) -> 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_channel_mut(0).unwrap().try_as_linear_mut() {
        linear[2] = 1.0;
        true
    } else {
        false
    };

    if is_linear {
        assert_eq!(buf.get_channel(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]);

Provided Methods§

source

fn fill(&mut self, value: Self::Sample)

Replace all samples in the channel with the specified value

Example
use audio::ChannelMut;

let mut buf = audio::sequential![[0; 2]; 2];
for mut channel in buf.iter_channels_mut() {
    channel.fill(1);
}
assert_eq!(buf.get_channel(0).unwrap().as_ref(), &[1, 1]);
assert_eq!(buf.get_channel(1).unwrap().as_ref(), &[1, 1]);

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'a, T> ChannelMut for InterleavedChannelMut<'a, T>
where T: Copy,

§

type ChannelMut<'this> = InterleavedChannelMut<'this, <InterleavedChannelMut<'a, T> as Channel>::Sample> where Self: 'this

§

type IterMut<'this> = IterMut<'this, <InterleavedChannelMut<'a, T> as Channel>::Sample> where Self: 'this

source§

impl<'a, T> ChannelMut for LinearChannelMut<'a, T>
where T: Copy,

§

type ChannelMut<'this> = LinearChannelMut<'this, T> where Self: 'this

§

type IterMut<'this> = IterMut<'this, T> where Self: 'this