Trait audio::Buf[][src]

pub trait Buf {
    pub fn frames_hint(&self) -> Option<usize>;
pub fn channels(&self) -> usize; pub fn skip(self, n: usize) -> Skip<Self> { ... }
pub fn tail(self, n: usize) -> Tail<Self> { ... }
pub fn limit(self, limit: usize) -> Limit<Self> { ... }
pub fn chunk(self, n: usize, len: usize) -> Chunk<Self> { ... } }

The base trait available to all audio buffers.

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

use audio::Buf as _;

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

assert_eq!(buffer.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 as _, ExactSizeBuf as _};

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

assert_eq!(buffer.channels(), 2);
assert_eq!(buffer.frames(), 4);
assert_eq!(buffer.limit(2).frames(), 2);

Required methods

pub fn frames_hint(&self) -> Option<usize>[src]

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 Channels 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(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::Channels;

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

    assert_eq!(buf.channel(0).frames(), 4);
    assert_eq!(buf.channel(1).frames(), 2);
}

test(vec![vec![1, 2, 3, 4], vec![5, 6]]);

pub fn channels(&self) -> usize[src]

The number of channels in the buffer.

Examples

use audio::Channels;

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

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

    assert_eq! {
        buf.channel(1).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(vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]]);
Loading content...

Provided methods

pub fn skip(self, n: usize) -> Skip<Self>[src]

Construct a new buffer where n frames are skipped.

Examples

use audio::Buf as _;
use audio::buf;

let from = audio::interleaved![[0, 0, 1, 1], [0; 4]];
let mut to = audio::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 as _, ChannelsMut as _};
use audio::{buf, wrap};

let from = wrap::interleaved(&[1, 1, 1, 1, 1, 1, 1, 1], 2);
let mut to = audio::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])

pub fn tail(self, n: usize) -> Tail<Self>[src]

Construct a new buffer where n frames are skipped.

Examples

use audio::Buf as _;
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]);

pub fn limit(self, limit: usize) -> Limit<Self>[src]

Limit the channel buffer to limit number of frames.

Examples

use audio::Buf as _;
use audio::buf;

let from = audio::interleaved![[1; 4]; 2];
let mut to = audio::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]);

pub fn chunk(self, n: usize, len: usize) -> Chunk<Self>[src]

Construct a range of frames corresponds to the chunk with len and position n.

Which is the range n * len .. n * len + len.

Examples

use audio::Buf as _;
use audio::buf;

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

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

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

Implementations on Foreign Types

impl<'_, B> Buf for &'_ mut B where
    B: Buf + ?Sized
[src]

impl<T> Buf for [Vec<T, Global>][src]

impl<T> Buf for Vec<Vec<T, Global>, Global>[src]

impl<'_, B> Buf for &'_ B where
    B: Buf + ?Sized
[src]

Loading content...

Implementors

impl<B> Buf for Read<B> where
    B: Buf
[src]

impl<B> Buf for ReadWrite<B> where
    B: Buf
[src]

impl<B> Buf for Write<B> where
    B: Buf
[src]

impl<T> Buf for Dynamic<T>[src]

impl<T> Buf for audio::interleaved::Interleaved<T>[src]

impl<T> Buf for audio::sequential::Sequential<T>[src]

impl<T> Buf for audio::wrap::Interleaved<&[T]>[src]

impl<T> Buf for audio::wrap::Interleaved<&mut [T]>[src]

impl<T> Buf for audio::wrap::Sequential<&[T]>[src]

impl<T> Buf for audio::wrap::Sequential<&mut [T]>[src]

impl<T, const N: usize> Buf for audio::wrap::Interleaved<&[T; N]>[src]

impl<T, const N: usize> Buf for audio::wrap::Interleaved<&mut [T; N]>[src]

impl<T, const N: usize> Buf for audio::wrap::Interleaved<[T; N]>[src]

impl<T, const N: usize> Buf for audio::wrap::Sequential<&[T; N]>[src]

impl<T, const N: usize> Buf for audio::wrap::Sequential<&mut [T; N]>[src]

impl<T, const N: usize> Buf for audio::wrap::Sequential<[T; N]>[src]

Loading content...