Trait audio::Buf

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

    // Required methods
    fn frames_hint(&self) -> Option<usize>;
    fn channels(&self) -> usize;
    fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>>;
    fn iter_channels(&self) -> Self::IterChannels<'_>;

    // Provided methods
    fn skip(self, n: usize) -> Skip<Self>
       where Self: Sized { ... }
    fn tail(self, n: usize) -> Tail<Self>
       where Self: Sized { ... }
    fn limit(self, limit: usize) -> Limit<Self>
       where Self: Sized { ... }
}
Expand description

The base trait available to all audio buffers.

This provides information which is available to all buffers, such as the number of channels.

let buf = audio::interleaved![[0; 4]; 2];
assert_eq!(buf.channels(), 2);

It also carries a number of slicing combinators, wuch as skip and limit which allows an audio buffer to be sliced as needed.

use audio::{Buf, ExactSizeBuf};

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

assert_eq!(buf.channels(), 2);
assert_eq!(buf.limit(2).frames(), 2);

Required Associated Types§

source

type Sample

The type of a single sample.

source

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

The type of the channel container.

source

type IterChannels<'this>: Iterator<Item = Self::Channel<'this>> where Self: 'this

An iterator over available channels.

Required Methods§

source

fn frames_hint(&self) -> Option<usize>

A typical number of frames for each channel in the buffer, if known.

If you only want to support buffers which have exact sizes use ExactSizeBuf.

This is only a best effort hint. We can’t require any Buf to know the exact number of frames, because we want to be able to implement it for types which does not keep track of the exact number of frames it expects each channel to have such as Vec<Vec<i16>>.

use audio::Buf;

fn test(buf: impl Buf) {
    assert_eq!(buf.channels(), 2);
    assert_eq!(buf.frames_hint(), Some(4));
}

test(audio::wrap::dynamic(vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]]));

But it should be clear that such a buffer supports a variable number of frames in each channel.

use audio::{Buf, Channel};

fn test(buf: impl Buf<Sample = i16>) {
    assert_eq!(buf.channels(), 2);
    assert_eq!(buf.frames_hint(), Some(4));

    assert_eq!(buf.get_channel(0).map(|c| c.len()), Some(4));
    assert_eq!(buf.get_channel(1).map(|c| c.len()), Some(2));
}

test(audio::wrap::dynamic(vec![vec![1, 2, 3, 4], vec![5, 6]]));
source

fn channels(&self) -> usize

The number of channels in the buffer.

Examples
use audio::{Buf, Channel};

fn test(buf: impl Buf<Sample = i16>) {
    assert_eq!(buf.channels(), 2);

    assert_eq! {
        buf.get_channel(0).unwrap().iter().collect::<Vec<_>>(),
        &[1, 2, 3, 4],
    }

    assert_eq! {
        buf.get_channel(1).unwrap().iter().collect::<Vec<_>>(),
        &[5, 6, 7, 8],
    }
}

test(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
test(audio::wrap::interleaved(&[1, 5, 2, 6, 3, 7, 4, 8], 2));
test(audio::wrap::dynamic(vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]]));
source

fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>>

Return a handler to the buffer associated with the channel.

Note that we don’t access the buffer for the underlying channel directly as a linear buffer like &[T], because the underlying representation might be different.

We must instead make use of the various utility functions found on Channel to copy data out of the channel.

Examples
use audio::{Buf, Channel};

fn test(buf: impl Buf<Sample = i16>) {
    let chan = buf.get_channel(1).unwrap();
    chan.iter().eq([5, 6, 7, 8]);
}

test(audio::dynamic![[1, 2, 3, 4], [5, 6, 7, 8]]);
test(audio::sequential![[1, 2, 3, 4], [5, 6, 7, 8]]);
test(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
source

fn iter_channels(&self) -> Self::IterChannels<'_>

Construct an iterator over all the channels in the audio buffer.

Examples
use audio::{Buf, Channel};

fn test(buf: impl Buf<Sample = i16>) {
    let chan = buf.iter_channels().nth(1).unwrap();
    chan.iter().eq([5, 6, 7, 8]);
}

test(audio::dynamic![[1, 2, 3, 4], [5, 6, 7, 8]]);
test(audio::sequential![[1, 2, 3, 4], [5, 6, 7, 8]]);
test(audio::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);

Provided Methods§

source

fn skip(self, n: usize) -> Skip<Self>
where Self: Sized,

Construct a wrapper around this buffer that skips the first n frames.

Examples
use audio::Buf;
use audio::buf;

let from = audio::interleaved![[0, 0, 1, 1], [0; 4]];
let mut to = audio::buf::Interleaved::with_topology(2, 4);

buf::copy(from.skip(2), &mut to);

assert_eq!(to.as_slice(), &[1, 0, 1, 0, 0, 0, 0, 0]);

With a mutable buffer.

use audio::Buf;
use audio::{buf, wrap};

let from = wrap::interleaved(&[1, 1, 1, 1, 1, 1, 1, 1], 2);
let mut to = audio::buf::Interleaved::with_topology(2, 4);

buf::copy(from, (&mut to).skip(2));

assert_eq!(to.as_slice(), &[0, 0, 0, 0, 1, 1, 1, 1])
source

fn tail(self, n: usize) -> Tail<Self>
where Self: Sized,

Construct a wrapper around this buffer that skips to the last n frames.

Examples
use audio::Buf;
use audio::buf;

let from = audio::interleaved![[1; 4]; 2];
let mut to = audio::interleaved![[0; 4]; 2];

buf::copy(from, (&mut to).tail(2));

assert_eq!(to.as_slice(), &[0, 0, 0, 0, 1, 1, 1, 1]);

The tail of a buffer adjusts all functions associated with the Buf:

use audio::{Buf, ExactSizeBuf};

let buf = audio::interleaved![[1, 2, 3, 4]; 2];

assert_eq!((&buf).tail(0).channels(), 2);
assert_eq!((&buf).tail(0).frames_hint(), Some(0));

assert_eq!((&buf).tail(1).channels(), 2);
assert_eq!((&buf).tail(1).frames_hint(), Some(1));

assert_eq!((&buf).tail(5).channels(), 2);
assert_eq!((&buf).tail(5).frames_hint(), Some(4));

for chan in buf.tail(2).iter_channels() {
    assert!(chan.iter().eq([3, 4]));
}
source

fn limit(self, limit: usize) -> Limit<Self>
where Self: Sized,

Construct a wrapper around this buffer which stops after limit frames.

Examples
use audio::Buf;
use audio::buf;

let from = audio::interleaved![[1; 4]; 2];
let mut to = audio::buf::Interleaved::with_topology(2, 4);

buf::copy(from, (&mut to).limit(2));

assert_eq!(to.as_slice(), &[1, 1, 1, 1, 0, 0, 0, 0]);

The limit of a buffer adjusts all functions associated with the Buf:

use audio::Buf;

let buf = audio::interleaved![[1, 2, 3, 4]; 2];

assert_eq!((&buf).limit(0).channels(), 2);
assert_eq!((&buf).limit(0).frames_hint(), Some(0));

assert_eq!((&buf).limit(1).channels(), 2);
assert_eq!((&buf).limit(1).frames_hint(), Some(1));

assert_eq!((&buf).limit(5).channels(), 2);
assert_eq!((&buf).limit(5).frames_hint(), Some(4));

for chan in buf.limit(2).iter_channels() {
    assert!(chan.iter().eq([1, 2]));
}

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<B> Buf for &B
where B: Buf + ?Sized,

§

type Sample = <B as Buf>::Sample

§

type Channel<'a> = <B as Buf>::Channel<'a> where &B: 'a

§

type IterChannels<'a> = <B as Buf>::IterChannels<'a> where &B: 'a

source§

fn frames_hint(&self) -> Option<usize>

source§

fn channels(&self) -> usize

source§

fn get_channel(&self, channel: usize) -> Option<<&B as Buf>::Channel<'_>>

source§

fn iter_channels(&self) -> <&B as Buf>::IterChannels<'_>

source§

impl<B> Buf for &mut B
where B: Buf + ?Sized,

§

type Sample = <B as Buf>::Sample

§

type Channel<'this> = <B as Buf>::Channel<'this> where &mut B: 'this

§

type IterChannels<'this> = <B as Buf>::IterChannels<'this> where &mut B: 'this

source§

fn frames_hint(&self) -> Option<usize>

source§

fn channels(&self) -> usize

source§

fn get_channel(&self, channel: usize) -> Option<<&mut B as Buf>::Channel<'_>>

source§

fn iter_channels(&self) -> <&mut B as Buf>::IterChannels<'_>

Implementors§

source§

impl<B> Buf for Limit<B>
where B: Buf,

§

type Sample = <B as Buf>::Sample

§

type Channel<'this> = <B as Buf>::Channel<'this> where Limit<B>: 'this

§

type IterChannels<'this> = IterChannels<<B as Buf>::IterChannels<'this>> where Limit<B>: 'this

source§

impl<B> Buf for Skip<B>
where B: Buf,

Skip adjusts the implementation of Buf.

use audio::{Buf, ExactSizeBuf};

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

assert_eq!((&buf).skip(0).channels(), 2);
assert_eq!((&buf).skip(0).frames_hint(), Some(4));

assert_eq!((&buf).skip(1).channels(), 2);
assert_eq!((&buf).skip(1).frames_hint(), Some(3));

assert_eq!((&buf).skip(5).channels(), 2);
assert_eq!((&buf).skip(5).frames_hint(), Some(0));
§

type Sample = <B as Buf>::Sample

§

type Channel<'this> = <B as Buf>::Channel<'this> where Skip<B>: 'this

§

type IterChannels<'this> = IterChannels<<B as Buf>::IterChannels<'this>> where Skip<B>: 'this

source§

impl<B> Buf for Tail<B>
where B: Buf,

§

type Sample = <B as Buf>::Sample

§

type Channel<'this> = <B as Buf>::Channel<'this> where Tail<B>: 'this

§

type IterChannels<'this> = IterChannels<<B as Buf>::IterChannels<'this>> where Tail<B>: 'this

source§

impl<B> Buf for Read<B>
where B: Buf,

§

type Sample = <B as Buf>::Sample

§

type Channel<'this> = <B as Buf>::Channel<'this> where Self: 'this

§

type IterChannels<'this> = Iter<'this, B> where Self: 'this

source§

impl<B> Buf for ReadWrite<B>
where B: Buf,

§

type Sample = <B as Buf>::Sample

§

type Channel<'this> = <B as Buf>::Channel<'this> where Self: 'this

§

type IterChannels<'this> = Iter<'this, B> where Self: 'this

source§

impl<B> Buf for Write<B>
where B: Buf,

§

type Sample = <B as Buf>::Sample

§

type Channel<'this> = <B as Buf>::Channel<'this> where Self: 'this

§

type IterChannels<'this> = Iter<'this, B> where Self: 'this

source§

impl<T> Buf for audio::buf::dynamic::Dynamic<T>
where T: Copy,

§

type Sample = T

§

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

§

type IterChannels<'this> = IterChannels<'this, T> where Self::Sample: 'this

source§

impl<T> Buf for audio::buf::interleaved::Interleaved<T>
where T: Copy,

§

type Sample = T

§

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

§

type IterChannels<'this> = IterChannels<'this, <Interleaved<T> as Buf>::Sample> where Self::Sample: 'this

source§

impl<T> Buf for audio::buf::sequential::Sequential<T>
where T: Copy,

§

type Sample = T

§

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

§

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

source§

impl<T> Buf for audio::wrap::Dynamic<&Vec<Vec<T>>>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<&Vec<Vec<T>>> as Buf>::Sample> where Self: 'this

§

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

source§

impl<T> Buf for audio::wrap::Dynamic<&[Vec<T>]>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<&[Vec<T>]> as Buf>::Sample> where Self: 'this

§

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

source§

impl<T> Buf for audio::wrap::Dynamic<&mut Vec<Vec<T>>>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<&mut Vec<Vec<T>>> as Buf>::Sample> where Self: 'this

§

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

source§

impl<T> Buf for audio::wrap::Dynamic<&mut [Vec<T>]>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<&mut [Vec<T>]> as Buf>::Sample> where Self: 'this

§

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

source§

impl<T> Buf for audio::wrap::Dynamic<Vec<Vec<T>>>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<Vec<Vec<T>>> as Buf>::Sample> where Self: 'this

§

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

source§

impl<T> Buf for audio::wrap::Interleaved<T>
where T: Slice,

§

type Sample = <T as Slice>::Item

§

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

§

type IterChannels<'this> = IterChannels<'this, <Interleaved<T> as Buf>::Sample> where Self: 'this

source§

impl<T> Buf for audio::wrap::Sequential<T>
where T: Slice,

§

type Sample = <T as Slice>::Item

§

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

§

type IterChannels<'this> = IterChannels<'this, <Sequential<T> as Buf>::Sample> where Self: 'this

source§

impl<T, const N: usize> Buf for audio::wrap::Dynamic<&[Vec<T>; N]>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<&[Vec<T>; N]> as Buf>::Sample> where Self: 'this

§

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

source§

impl<T, const N: usize> Buf for audio::wrap::Dynamic<&mut [Vec<T>; N]>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<&mut [Vec<T>; N]> as Buf>::Sample> where Self: 'this

§

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

source§

impl<T, const N: usize> Buf for audio::wrap::Dynamic<[Vec<T>; N]>
where T: Copy,

§

type Sample = T

§

type Channel<'this> = LinearChannel<'this, <Dynamic<[Vec<T>; N]> as Buf>::Sample> where Self: 'this

§

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