Struct rotary::dynamic::Dynamic [−][src]
A dynamically sized, multi-channel audio buffer.
An audio buffer is constrained to only support sample-apt types. 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 frequently.
Implementations
impl<T> Dynamic<T> where
T: Sample,
[src]
T: Sample,
pub fn new() -> Self
[src]
Construct a new empty audio buffer.
Examples
let mut buffer = rotary::Dynamic::<f32>::new(); assert_eq!(buffer.frames(), 0);
pub fn with_topology(channels: usize, frames: usize) -> Self
[src]
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 mut buffer = rotary::Dynamic::<f32>::with_topology(4, 256); assert_eq!(buffer.frames(), 256); assert_eq!(buffer.channels(), 4);
pub fn from_array<const F: usize, const C: usize>(channels: [[T; F]; C]) -> Self
[src]
Allocate an audio buffer from a fixed-size array.
See dynamic!.
Examples
use rotary::BitSet; let mut buffer = rotary::Dynamic::<f32>::from_array([[2.0; 256]; 4]); assert_eq!(buffer.frames(), 256); assert_eq!(buffer.channels(), 4); for chan in &buffer { assert_eq!(chan, vec![2.0; 256]); }
pub fn frames(&self) -> usize
[src]
Get the number of frames in the channels of an audio buffer.
Examples
let mut buffer = rotary::Dynamic::<f32>::new(); assert_eq!(buffer.frames(), 0); buffer.resize(256); assert_eq!(buffer.frames(), 256);
pub fn channels(&self) -> usize
[src]
Check how many channels there are in the buffer.
Examples
let mut buffer = rotary::Dynamic::<f32>::new(); assert_eq!(buffer.channels(), 0); buffer.resize_channels(2); assert_eq!(buffer.channels(), 2);
pub fn iter(&self) -> Iter<'_, T>ⓘ
[src]
Construct a mutable iterator over all available channels.
Examples
use rand::Rng as _; let mut buffer = rotary::Dynamic::<f32>::with_topology(4, 256); let all_zeros = vec![0.0; 256]; for chan in buffer.iter() { assert_eq!(chan, &all_zeros[..]); }
pub fn iter_mut(&mut self) -> IterMut<'_, T>ⓘ
[src]
Construct a mutable iterator over all available channels.
Examples
use rand::Rng as _; let mut buffer = rotary::Dynamic::<f32>::with_topology(4, 256); let mut rng = rand::thread_rng(); for chan in buffer.iter_mut() { rng.fill(chan); }
pub fn resize(&mut self, frames: usize)
[src]
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 buffer = rotary::Dynamic::<f32>::new(); assert_eq!(buffer.channels(), 0); assert_eq!(buffer.frames(), 0); buffer.resize_channels(4); buffer.resize(256); assert_eq!(buffer[1][128], 0.0); buffer[1][128] = 42.0; assert_eq!(buffer.channels(), 4); assert_eq!(buffer.frames(), 256);
Decreasing and increasing the size will not touch a buffer that has already been allocated.
assert_eq!(buffer[1][128], 0.0); buffer[1][128] = 42.0; buffer.resize(64); assert!(buffer[1].get(128).is_none()); buffer.resize(256); assert_eq!(buffer[1][128], 42.0);
pub fn resize_channels(&mut self, channels: usize)
[src]
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 buffer = rotary::Dynamic::<f32>::new(); assert_eq!(buffer.channels(), 0); assert_eq!(buffer.frames(), 0); buffer.resize_channels(4); buffer.resize(256); assert_eq!(buffer.channels(), 4); assert_eq!(buffer.frames(), 256);
pub fn get(&self, channel: usize) -> Option<&[T]>
[src]
Get a reference to the buffer of the given channel.
Examples
let mut buffer = rotary::Dynamic::<f32>::new(); buffer.resize_channels(4); buffer.resize(256); let expected = vec![0.0; 256]; assert_eq!(Some(&expected[..]), buffer.get(0)); assert_eq!(Some(&expected[..]), buffer.get(1)); assert_eq!(Some(&expected[..]), buffer.get(2)); assert_eq!(Some(&expected[..]), buffer.get(3)); assert_eq!(None, buffer.get(4));
pub fn get_or_default(&mut self, channel: usize) -> &[T]
[src]
Get the given channel or initialize the buffer with the default value.
Examples
let mut buffer = rotary::Dynamic::<f32>::new(); buffer.resize(256); let expected = vec![0f32; 256]; assert_eq!(buffer.get_or_default(0), &expected[..]); assert_eq!(buffer.get_or_default(1), &expected[..]); assert_eq!(buffer.channels(), 2);
pub fn get_mut(&mut self, channel: usize) -> Option<&mut [T]>
[src]
Get a mutable reference to the buffer of the given channel.
Examples
use rand::Rng as _; let mut buffer = rotary::Dynamic::<f32>::new(); buffer.resize_channels(2); buffer.resize(256); let mut rng = rand::thread_rng(); if let Some(left) = buffer.get_mut(0) { rng.fill(left); } if let Some(right) = buffer.get_mut(1) { rng.fill(right); }
pub fn get_or_default_mut(&mut self, channel: usize) -> &mut [T]
[src]
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 as _; let mut buffer = rotary::Dynamic::<f32>::new(); buffer.resize(256); let mut rng = rand::thread_rng(); rng.fill(buffer.get_or_default_mut(0)); rng.fill(buffer.get_or_default_mut(1)); assert_eq!(buffer.channels(), 2);
pub fn into_vectors(self) -> Vec<Vec<T>>
[src]
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 buffer = rotary::Dynamic::<f32>::new(); buffer.resize_channels(4); buffer.resize(512); let expected = vec![0.0; 512]; let buffers = buffer.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[..]);
pub fn into_vectors_if(
self,
condition: impl FnMut(usize) -> bool
) -> Vec<Vec<T>>
[src]
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 buffer = rotary::Dynamic::<f32>::new(); buffer.resize_channels(4); buffer.resize(512); let expected = vec![0.0; 512]; let buffers = buffer.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
impl<T> Buf<T> for Dynamic<T> where
T: Sample,
[src]
T: Sample,
fn frames(&self) -> usize
[src]
fn channels(&self) -> usize
[src]
fn channel(&self, channel: usize) -> BufChannel<'_, T>
[src]
fn offset(self, offset: usize) -> Offset<Self> where
Self: Sized,
[src]
Self: Sized,
fn limit(self, limit: usize) -> Limit<Self> where
Self: Sized,
[src]
Self: Sized,
impl<T> BufMut<T> for Dynamic<T> where
T: Sample,
[src]
T: Sample,
fn channel_mut(&mut self, channel: usize) -> BufChannelMut<'_, T>
[src]
fn resize(&mut self, frames: usize)
[src]
fn resize_topology(&mut self, channels: usize, frames: usize)
[src]
impl<T> Debug for Dynamic<T> where
T: Sample + Debug,
[src]
T: Sample + Debug,
impl<T> Drop for Dynamic<T> where
T: Sample,
[src]
T: Sample,
impl<T> Eq for Dynamic<T> where
T: Sample + Eq,
[src]
T: Sample + Eq,
impl<T, const F: usize, const C: usize> From<[[T; F]; C]> for Dynamic<T> where
T: Sample,
[src]
T: Sample,
Allocate an audio buffer from a fixed-size array.
See dynamic!.
impl<T> Hash for Dynamic<T> where
T: Sample + Hash,
[src]
T: Sample + Hash,
fn hash<H: Hasher>(&self, state: &mut H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T> Index<usize> for Dynamic<T> where
T: Sample,
[src]
T: Sample,
type Output = [T]
The returned type after indexing.
fn index(&self, index: usize) -> &Self::Output
[src]
impl<T> IndexMut<usize> for Dynamic<T> where
T: Sample,
[src]
T: Sample,
impl<'a, T> IntoIterator for &'a Dynamic<T> where
T: Sample,
[src]
T: Sample,
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
type Item = <Self::IntoIter as Iterator>::Item
The type of the elements being iterated over.
fn into_iter(self) -> Self::IntoIter
[src]
impl<'a, T> IntoIterator for &'a mut Dynamic<T> where
T: Sample,
[src]
T: Sample,
type IntoIter = IterMut<'a, T>
Which kind of iterator are we turning this into?
type Item = <Self::IntoIter as Iterator>::Item
The type of the elements being iterated over.
fn into_iter(self) -> Self::IntoIter
[src]
impl<T> Ord for Dynamic<T> where
T: Sample + Ord,
[src]
T: Sample + Ord,
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<T> PartialEq<Dynamic<T>> for Dynamic<T> where
T: Sample + PartialEq,
[src]
T: Sample + PartialEq,
impl<T> PartialOrd<Dynamic<T>> for Dynamic<T> where
T: Sample + PartialOrd,
[src]
T: Sample + PartialOrd,
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T> Send for Dynamic<T> where
T: Sample + Send,
[src]
T: Sample + Send,
impl<T> Sync for Dynamic<T> where
T: Sample + Sync,
[src]
T: Sample + Sync,
Auto Trait Implementations
impl<T> RefUnwindSafe for Dynamic<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Unpin for Dynamic<T>
impl<T> UnwindSafe for Dynamic<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,