Skip to main content

SampleResourceF32Mut

Trait SampleResourceF32Mut 

Source
pub trait SampleResourceF32Mut: SampleResourceF32 {
    // Required methods
    fn channel_mut(&mut self, i: usize) -> Option<&mut [f32]>;
    fn channel_mut_2(&mut self, indices: [usize; 2]) -> Option<[&mut [f32]; 2]>;
}
Expand description

A mutable resource of audio samples which has its data stored in de-interleaved f32 format, and which has all of its data loaded into memory.

§Implementation example

struct MyResource {
    data: Vec<Vec<f32>>,
    frames: usize,
    sample_rate: Option<f64>,
}

impl SampleResourceF32 for MyResource {
    fn info(&self) -> SampleResourceInfo {
        SampleResourceInfo {
            num_channels: self.data.len(),
            len_frames: self.frames as u64,
            sample_rate: self.sample_rate,
        }
    }
    fn channel(&self, i: usize) -> Option<&[f32]> {
        self.data.get(i).map(|ch| ch.as_slice())
    }
}

impl SampleResourceF32Mut for MyResource {
    fn channel_mut(&mut self, i: usize) -> Option<&mut [f32]> {
        self.data.get_mut(i).map(|ch| ch.as_mut_slice())
    }

    fn channel_mut_2(&mut self, indices: [usize; 2]) -> Option<[&mut [f32]; 2]> {
        self.data
            .get_disjoint_mut(indices)
            .map(|ch| ch.map(|v| v.as_mut_slice()))
            .ok()
    }
}

§Realtime Safety

3rd party libraries that implement this trait should expect that all methods in this trait may be called in a realtime thread. For more information, see: http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing

Required Methods§

Source

fn channel_mut(&mut self, i: usize) -> Option<&mut [f32]>

Return a mutable slice to the entire channel at index i.

Returns None if i is out-of-bounds.

Source

fn channel_mut_2(&mut self, indices: [usize; 2]) -> Option<[&mut [f32]; 2]>

Return a mutable slice to two channels at once.

Returns None if any index is out-of-bounds or if the indices overlap.

Implementors§