Trait rotary_core::Buf[][src]

pub trait Buf {
    fn frames_hint(&self) -> Option<usize>;
fn channels(&self) -> usize; 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
, { ... }
fn chunk(self, n: usize, len: usize) -> Chunk<Self>
    where
        Self: Sized
, { ... } }

The base trait available to all audio buffers.

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

use rotary::Buf as _;

let buffer = rotary::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 rotary::{Buf as _, ExactSizeBuf as _};

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

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

Required methods

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 rotary::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 rotary::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]]);

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

The number of channels in the buffer.

Examples

use rotary::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(rotary::interleaved![[1, 2, 3, 4], [5, 6, 7, 8]]);
test(rotary::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

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

Construct a new buffer where n frames are skipped.

Examples

use rotary::Buf as _;
use rotary::buf;

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

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

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

Construct a new buffer where n frames are skipped.

Examples

use rotary::Buf as _;
use rotary::buf;

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

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

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

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

Limit the channel buffer to limit number of frames.

Examples

use rotary::Buf as _;
use rotary::buf;

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

fn chunk(self, n: usize, len: usize) -> Chunk<Self> where
    Self: Sized
[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 rotary::Buf as _;
use rotary::buf;

let from = rotary::interleaved![[1; 4]; 2];
let mut to = rotary::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: ?Sized> Buf for &B where
    B: Buf
[src]

impl<B: ?Sized> Buf for &mut B where
    B: Buf
[src]

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

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

Loading content...

Implementors

Loading content...