pub struct Dynamic<T> { /* private fields */ }
Expand description
A dynamically sized, multi-channel audio buffer.
An audio buffer can only be resized if it contains a type which is sample-apt. For more information of what this means, see Sample.
This kind of buffer stores each channel in its own heap-allocated slice of memory, meaning they can be manipulated more cheaply independently of each other than say Interleaved or Sequential. These would have to re-organize every constituent channel when resizing, while Dynamic generally only requires growing and shrinking of a memory region.
This kind of buffer is a good choice if you need to resize_frames frequently.
Implementations§
source§impl<T> Dynamic<T>
impl<T> Dynamic<T>
sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new empty audio buffer.
Examples
let buf = audio::buf::Dynamic::<f32>::new();
assert_eq!(buf.frames(), 0);
sourcepub fn with_topology(channels: usize, frames: usize) -> Selfwhere
T: Sample,
pub fn with_topology(channels: usize, frames: usize) -> Selfwhere
T: Sample,
Allocate an audio buffer with the given topology. A “topology” is a
given number of channels
and the corresponding number of frames
in
their buffers.
Examples
let buf = audio::buf::Dynamic::<f32>::with_topology(4, 256);
assert_eq!(buf.frames(), 256);
assert_eq!(buf.channels(), 4);
sourcepub fn from_array<const F: usize, const C: usize>(channels: [[T; F]; C]) -> Selfwhere
T: Copy,
pub fn from_array<const F: usize, const C: usize>(channels: [[T; F]; C]) -> Selfwhere
T: Copy,
sourcepub fn frames(&self) -> usize
pub fn frames(&self) -> usize
Get how many frames there are in the buffer.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
assert_eq!(buf.frames(), 0);
buf.resize_frames(256);
assert_eq!(buf.frames(), 256);
sourcepub fn channels(&self) -> usize
pub fn channels(&self) -> usize
Get how many channels there are in the buffer.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
assert_eq!(buf.channels(), 0);
buf.resize_channels(2);
assert_eq!(buf.channels(), 2);
sourcepub fn iter_channels(&self) -> IterChannels<'_, T> ⓘ
pub fn iter_channels(&self) -> IterChannels<'_, T> ⓘ
Construct a mutable iterator over all available channels.
Examples
use rand::Rng;
let mut buf = audio::buf::Dynamic::<f32>::with_topology(4, 256);
let all_zeros = vec![0.0; 256];
for chan in buf.iter_channels() {
assert_eq!(chan.as_ref(), &all_zeros[..]);
}
sourcepub fn iter_channels_mut(&mut self) -> IterChannelsMut<'_, T> ⓘ
pub fn iter_channels_mut(&mut self) -> IterChannelsMut<'_, T> ⓘ
Construct a mutable iterator over all available channels.
Examples
use rand::Rng;
let mut buf = audio::buf::Dynamic::<f32>::with_topology(4, 256);
let mut rng = rand::thread_rng();
for mut chan in buf.iter_channels_mut() {
rng.fill(chan.as_mut());
}
sourcepub fn resize_frames(&mut self, frames: usize)where
T: Sample,
pub fn resize_frames(&mut self, frames: usize)where
T: Sample,
Set the size of the buffer. The size is the size of each channel’s buffer.
If the size of the buffer increases as a result, the new regions in the frames will be zeroed. If the size decreases, the region will be left untouched. So if followed by another increase, the data will be “dirty”.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
assert_eq!(buf.channels(), 0);
assert_eq!(buf.frames(), 0);
buf.resize_channels(4);
buf.resize_frames(256);
assert_eq!(buf[1][128], 0.0);
buf[1][128] = 42.0;
assert_eq!(buf.channels(), 4);
assert_eq!(buf.frames(), 256);
Decreasing and increasing the size will not touch a buffer that has already been allocated.
assert_eq!(buf[1][128], 0.0);
buf[1][128] = 42.0;
buf.resize_frames(64);
assert!(buf[1].get(128).is_none());
buf.resize_frames(256);
assert_eq!(buf[1][128], 42.0);
sourcepub fn resize_channels(&mut self, channels: usize)where
T: Sample,
pub fn resize_channels(&mut self, channels: usize)where
T: Sample,
Set the number of channels in use.
If the size of the buffer increases as a result, the new channels will be zeroed. If the size decreases, the channels that falls outside of the new size will be dropped.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
assert_eq!(buf.channels(), 0);
assert_eq!(buf.frames(), 0);
buf.resize_channels(4);
buf.resize_frames(256);
assert_eq!(buf.channels(), 4);
assert_eq!(buf.frames(), 256);
sourcepub fn get_channel(&self, channel: usize) -> Option<LinearChannel<'_, T>>
pub fn get_channel(&self, channel: usize) -> Option<LinearChannel<'_, T>>
Get a reference to the buffer of the given channel.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
buf.resize_channels(4);
buf.resize_frames(256);
let expected = vec![0.0; 256];
assert_eq!(buf.get_channel(0).unwrap(), &expected[..]);
assert_eq!(buf.get_channel(1).unwrap(), &expected[..]);
assert_eq!(buf.get_channel(2).unwrap(), &expected[..]);
assert_eq!(buf.get_channel(3).unwrap(), &expected[..]);
assert!(buf.get_channel(4).is_none());
sourcepub fn get_or_default(&mut self, channel: usize) -> &[T]where
T: Sample,
pub fn get_or_default(&mut self, channel: usize) -> &[T]where
T: Sample,
Get the given channel or initialize the buffer with the default value.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
buf.resize_frames(256);
let expected = vec![0f32; 256];
assert_eq!(buf.get_or_default(0), &expected[..]);
assert_eq!(buf.get_or_default(1), &expected[..]);
assert_eq!(buf.channels(), 2);
sourcepub fn get_mut(&mut self, channel: usize) -> Option<LinearChannelMut<'_, T>>
pub fn get_mut(&mut self, channel: usize) -> Option<LinearChannelMut<'_, T>>
Get a mutable reference to the buffer of the given channel.
Examples
use rand::Rng;
let mut buf = audio::buf::Dynamic::<f32>::new();
buf.resize_channels(2);
buf.resize_frames(256);
let mut rng = rand::thread_rng();
if let Some(mut left) = buf.get_mut(0) {
rng.fill(left.as_mut());
}
if let Some(mut right) = buf.get_mut(1) {
rng.fill(right.as_mut());
}
sourcepub fn get_or_default_mut(&mut self, channel: usize) -> &mut [T]where
T: Sample,
pub fn get_or_default_mut(&mut self, channel: usize) -> &mut [T]where
T: Sample,
Get the given channel or initialize the buffer with the default value.
If a channel that is out of bound is queried, the buffer will be empty.
Examples
use rand::Rng;
let mut buf = audio::buf::Dynamic::<f32>::new();
buf.resize_frames(256);
let mut rng = rand::thread_rng();
rng.fill(buf.get_or_default_mut(0));
rng.fill(buf.get_or_default_mut(1));
assert_eq!(buf.channels(), 2);
sourcepub fn into_vectors(self) -> Vec<Vec<T>>
pub fn into_vectors(self) -> Vec<Vec<T>>
Convert into a vector of vectors.
This is provided for the Dynamic type because it’s a very cheap oepration due to its memory topology. No copying of the underlying buffers is necessary.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
buf.resize_channels(4);
buf.resize_frames(512);
let expected = vec![0.0; 512];
let buffers = buf.into_vectors();
assert_eq!(buffers.len(), 4);
assert_eq!(buffers[0], &expected[..]);
assert_eq!(buffers[1], &expected[..]);
assert_eq!(buffers[2], &expected[..]);
assert_eq!(buffers[3], &expected[..]);
sourcepub fn into_vectors_if(
self,
condition: impl FnMut(usize) -> bool
) -> Vec<Vec<T>>
pub fn into_vectors_if( self, condition: impl FnMut(usize) -> bool ) -> Vec<Vec<T>>
Convert into a vector of vectors using a condition.
This is provided for the Dynamic type because it’s a very cheap oepration due to its memory topology. No copying of the underlying buffers is necessary.
Channels which does not match the condition will be filled with an empty vector.
Examples
let mut buf = audio::buf::Dynamic::<f32>::new();
buf.resize_channels(4);
buf.resize_frames(512);
let expected = vec![0.0; 512];
let buffers = buf.into_vectors_if(|n| n != 1);
assert_eq!(buffers.len(), 4);
assert_eq!(buffers[0], &expected[..]);
assert_eq!(buffers[1], &[][..]);
assert_eq!(buffers[2], &expected[..]);
assert_eq!(buffers[3], &expected[..]);
Trait Implementations§
source§impl<T> Buf for Dynamic<T>where
T: Copy,
impl<T> Buf for Dynamic<T>where
T: Copy,
§type Channel<'this> = LinearChannel<'this, <Dynamic<T> as Buf>::Sample>
where
Self::Sample: 'this
type Channel<'this> = LinearChannel<'this, <Dynamic<T> as Buf>::Sample> where Self::Sample: 'this
§type IterChannels<'this> = IterChannels<'this, T>
where
Self::Sample: 'this
type IterChannels<'this> = IterChannels<'this, T> where Self::Sample: 'this
source§fn frames_hint(&self) -> Option<usize>
fn frames_hint(&self) -> Option<usize>
source§fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>>
fn get_channel(&self, channel: usize) -> Option<Self::Channel<'_>>
source§fn iter_channels(&self) -> Self::IterChannels<'_>
fn iter_channels(&self) -> Self::IterChannels<'_>
source§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
frames. Read moresource§impl<T> BufMut for Dynamic<T>where
T: Copy,
impl<T> BufMut for Dynamic<T>where
T: Copy,
§type ChannelMut<'this> = LinearChannelMut<'this, <Dynamic<T> as Buf>::Sample>
where
Self: 'this
type ChannelMut<'this> = LinearChannelMut<'this, <Dynamic<T> as Buf>::Sample> where Self: 'this
§type IterChannelsMut<'this> = IterChannelsMut<'this, T>
where
Self: 'this
type IterChannelsMut<'this> = IterChannelsMut<'this, T> where Self: 'this
source§fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>>
fn get_channel_mut(&mut self, channel: usize) -> Option<Self::ChannelMut<'_>>
source§fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_>
fn iter_channels_mut(&mut self) -> Self::IterChannelsMut<'_>
source§impl<T> ExactSizeBuf for Dynamic<T>where
T: Copy,
impl<T> ExactSizeBuf for Dynamic<T>where
T: Copy,
source§impl<T, const F: usize, const C: usize> From<[[T; F]; C]> for Dynamic<T>where
T: Copy,
impl<T, const F: usize, const C: usize> From<[[T; F]; C]> for Dynamic<T>where
T: Copy,
Allocate an audio buffer from a fixed-size array.
See dynamic!.
source§impl<'a, T> IntoIterator for &'a Dynamic<T>
impl<'a, T> IntoIterator for &'a Dynamic<T>
source§impl<'a, T> IntoIterator for &'a mut Dynamic<T>
impl<'a, T> IntoIterator for &'a mut Dynamic<T>
source§impl<T> Ord for Dynamic<T>where
T: Ord,
impl<T> Ord for Dynamic<T>where
T: Ord,
source§impl<T> PartialEq for Dynamic<T>where
T: PartialEq,
impl<T> PartialEq for Dynamic<T>where
T: PartialEq,
source§impl<T> PartialOrd for Dynamic<T>where
T: PartialOrd,
impl<T> PartialOrd for Dynamic<T>where
T: PartialOrd,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more