Trait audio::Channel

source ·
pub trait Channel {
    type Sample: Copy;
    type Channel<'this>: Channel<Sample = Self::Sample>
       where Self: 'this;
    type Iter<'this>: Iterator<Item = Self::Sample>
       where Self: 'this;

    // Required methods
    fn as_channel(&self) -> Self::Channel<'_>;
    fn len(&self) -> usize;
    fn get(&self, n: usize) -> Option<Self::Sample>;
    fn iter(&self) -> Self::Iter<'_>;
    fn try_as_linear(&self) -> Option<&[Self::Sample]>;
    fn skip(self, n: usize) -> Self;
    fn tail(self, n: usize) -> Self;
    fn limit(self, limit: usize) -> Self;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

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

This trait provides read-only access.

See Buf::get_channel.

Required Associated Types§

source

type Sample: Copy

The sample of a channel.

source

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

The type the channel assumes when coerced into a reference.

source

type Iter<'this>: Iterator<Item = Self::Sample> where Self: 'this

A borrowing iterator over the channel.

Required Methods§

source

fn as_channel(&self) -> Self::Channel<'_>

Reborrow the current channel as a reference.

source

fn len(&self) -> usize

Get the length which indicates number of frames in the current channel.

Examples
use audio::{Buf, Channel};

fn test(buf: impl Buf<Sample = f32>) {
    for chan in buf.iter_channels() {
        assert_eq!(chan.len(), 16);
    }
}

test(&audio::dynamic![[0.0; 16]; 2]);
test(&audio::sequential![[0.0; 16]; 2]);
test(&audio::interleaved![[0.0; 16]; 2]);
source

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

Get the frame at the given offset in the channel.

Examples
use audio::{Buf, Channel};

fn test(buf: impl Buf<Sample = f32>) {
    for chan in buf.iter_channels() {
        assert_eq!(chan.get(15), Some(0.0));
        assert_eq!(chan.get(16), None);
    }
}

test(&audio::dynamic![[0.0; 16]; 2]);
test(&audio::sequential![[0.0; 16]; 2]);
test(&audio::interleaved![[0.0; 16]; 2]);
source

fn iter(&self) -> Self::Iter<'_>

Construct an iterator over the channel.

Examples
use audio::{Buf, BufMut, Channel, ChannelMut};

let mut left = audio::interleaved![[0.0f32; 4]; 2];
let mut right = audio::dynamic![[0.0f32; 4]; 2];

if let (Some(mut left), Some(mut right)) = (left.get_mut(0), right.get_mut(0)) {
    for (l, r) in left.iter_mut().zip(right.iter_mut()) {
        *l = 1.0;
        *r = 1.0;
    }
}

assert!(left.get_channel(0).unwrap().iter().eq(right.get_channel(0).unwrap().iter()));

assert_eq!(left.as_slice(), &[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]);
assert_eq!(&right[0], &[1.0, 1.0, 1.0, 1.0]);
assert_eq!(&right[1], &[0.0, 0.0, 0.0, 0.0]);
source

fn try_as_linear(&self) -> Option<&[Self::Sample]>

Try to access the current channel as a linear buffer.

This is available because it could permit for some optimizations.

Examples
use audio::{Buf, Channel};

fn test(buf: &impl Buf<Sample = f32>, expected: Option<&[f32]>) {
    assert_eq!(buf.get_channel(0).unwrap().try_as_linear(), expected);
}

test(&audio::dynamic![[1.0; 16]; 2], Some(&[1.0; 16]));
test(&audio::sequential![[1.0; 16]; 2], Some(&[1.0; 16]));
test(&audio::interleaved![[1.0; 16]; 2], None);
source

fn skip(self, n: usize) -> Self

Construct a channel buffer where the first n frames are skipped.

Skipping to the end of the buffer will result in an empty buffer.

use audio::{Buf, Channel};

let buf = audio::interleaved![[0; 4]; 2];

for chan in buf.iter_channels() {
    assert_eq!(chan.skip(1).len(), 3);
    assert_eq!(chan.skip(4).len(), 0);
}
Examples
use audio::{Buf, BufMut, Channel, ChannelMut};

let mut from = audio::interleaved![[0.0f32; 4]; 2];
*from.sample_mut(0, 2).unwrap() = 1.0;
*from.sample_mut(0, 3).unwrap() = 1.0;

let mut to = audio::interleaved![[0.0f32; 4]; 2];

if let (Some(from), Some(to)) = (from.get_channel(0), to.get_channel_mut(0)) {
    audio::channel::copy(from.skip(2), to);
}

assert_eq!(to.as_slice(), &[1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
source

fn tail(self, n: usize) -> Self

Construct a channel buffer where the last n frames are included.

Examples
use audio::{Buf, BufMut, Channel, ChannelMut};

let from = audio::interleaved![[1.0f32; 4]; 2];
let mut to = audio::interleaved![[0.0f32; 4]; 2];

if let (Some(from), Some(to)) = (from.get_channel(0), to.get_channel_mut(0)) {
    audio::channel::copy(from, to.tail(2));
}

assert_eq!(to.as_slice(), &[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0]);
source

fn limit(self, limit: usize) -> Self

Limit the channel bufferto limit number of frames.

Examples
use audio::{Buf, BufMut, Channel, ChannelMut};

let from = audio::interleaved![[1.0f32; 4]; 2];
let mut to = audio::interleaved![[0.0f32; 4]; 2];

if let (Some(from), Some(to)) = (from.get_channel(0), to.get_channel_mut(0)) {
    audio::channel::copy(from.limit(2), to);
}

assert_eq!(to.as_slice(), &[1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]);

Provided Methods§

source

fn is_empty(&self) -> bool

Test if the current channel is empty.

Examples
use audio::{Buf, Channel};

fn test(buf: impl Buf<Sample = f32>) {
    for chan in buf.iter_channels() {
        assert!(!chan.is_empty());
    }
}

test(&audio::dynamic![[0.0; 16]; 2]);
test(&audio::sequential![[0.0; 16]; 2]);
test(&audio::interleaved![[0.0; 16]; 2]);

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'a, T> Channel for InterleavedChannel<'a, T>
where T: Copy,

§

type Sample = T

§

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

§

type Iter<'this> = Iter<'this, <InterleavedChannel<'a, T> as Channel>::Sample> where Self: 'this

source§

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

§

type Sample = T

§

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

§

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

source§

impl<'a, T> Channel for LinearChannel<'a, T>
where T: Copy,

§

type Sample = T

§

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

§

type Iter<'this> = Iter<'this, <LinearChannel<'a, T> as Channel>::Sample> where Self: 'this

source§

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

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <LinearChannelMut<'a, T> as Channel>::Sample> where Self: 'this

§

type Iter<'this> = Iter<'this, <LinearChannelMut<'a, T> as Channel>::Sample> where Self: 'this