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§
sourcetype Channel<'this>: Channel<Sample = Self::Sample>
where
Self: 'this
type Channel<'this>: Channel<Sample = Self::Sample> where Self: 'this
The type of the channel container.
sourcetype IterChannels<'this>: Iterator<Item = Self::Channel<'this>>
where
Self: 'this
type IterChannels<'this>: Iterator<Item = Self::Channel<'this>> where Self: 'this
An iterator over available channels.
Required Methods§
sourcefn frames_hint(&self) -> Option<usize>
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]]));
sourcefn channels(&self) -> usize
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]]));
sourcefn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>>
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]]);
sourcefn iter_channels(&self) -> Self::IterChannels<'_>
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§
sourcefn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
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])
sourcefn tail(self, n: usize) -> Tail<Self>where
Self: Sized,
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]));
}
sourcefn limit(self, limit: usize) -> Limit<Self>where
Self: Sized,
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§
Implementations on Foreign Types§
source§impl<B> Buf for &B
impl<B> Buf for &B
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
fn frames_hint(&self) -> Option<usize>
fn channels(&self) -> usize
fn get_channel(&self, channel: usize) -> Option<<&B as Buf>::Channel<'_>>
fn iter_channels(&self) -> <&B as Buf>::IterChannels<'_>
source§impl<B> Buf for &mut B
impl<B> Buf for &mut B
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
fn frames_hint(&self) -> Option<usize>
fn channels(&self) -> usize
fn get_channel(&self, channel: usize) -> Option<<&mut B as Buf>::Channel<'_>>
fn iter_channels(&self) -> <&mut B as Buf>::IterChannels<'_>
Implementors§
source§impl<B> Buf for Skip<B>where
B: Buf,
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));