Trait audio::Buf [−][src]
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]]);
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]);
Implementations on Foreign Types
impl<'_, B> Buf for &'_ mut B where
B: Buf + ?Sized,
[src]
B: Buf + ?Sized,
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...B: Buf + ?Sized,
Implementors
impl<B> Buf for Read<B> where
B: Buf,
[src]
B: Buf,
impl<B> Buf for ReadWrite<B> where
B: Buf,
[src]
B: Buf,
impl<B> Buf for Write<B> where
B: Buf,
[src]
B: Buf,