MCPcopy Create free account
hub / github.com/BillyDM/Firewheel / instance_mut

Method instance_mut

crates/firewheel-core/src/dsp/buffer.rs:629–665  ·  view source on GitHub ↗

Returns an array of mutable slices to the first `frames` frames of the backing buffers for the first `num_channels` channels present of the instance at `instance_index`. Clamps `num_channels` and `frames` to the available data. Returns `None` if there is no instance at `instance_index`.

(
        &mut self,
        instance_index: usize,
        channels: usize,
        frames: usize,
    )

Source from the content-addressed store, hash-verified

627 ///
628 /// Returns `None` if there is no instance at `instance_index`.
629 pub fn instance_mut<const MAX_CHANNELS: usize>(
630 &mut self,
631 instance_index: usize,
632 channels: usize,
633 frames: usize,
634 ) -> Option<ArrayVec<&mut [T], MAX_CHANNELS>> {
635 if instance_index >= self.num_instances {
636 return None;
637 }
638
639 let frames = frames.min(self.num_frames);
640 let channels = channels.min(self.num_channels.get()).min(MAX_CHANNELS);
641
642 let start_frame = instance_index * self.num_frames * self.num_channels.get();
643
644 let mut res = ArrayVec::new();
645
646 // SAFETY:
647 //
648 // * The constructor has set the size of the buffer to
649 // `self.frames * self.channels * self.num_instances`, and we have constrained
650 // `instance_index`, `channels` and `frames` above, so this is always within range.
651 // * None of these slices overlap, and `self` is borrowed mutably in this method,
652 // so all mutability rules are being upheld.
653 unsafe {
654 for ch_i in 0..channels {
655 res.push_unchecked(core::slice::from_raw_parts_mut(
656 self.buffer
657 .as_mut_ptr()
658 .add(start_frame + (ch_i * self.num_frames)),
659 frames,
660 ));
661 }
662 }
663
664 Some(res)
665 }
666}
667
668impl<T: Clone + Copy + Default> Clone for InstanceBuffer<T> {

Callers

nothing calls this directly

Calls 3

minMethod · 0.80
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected