Returns an array of 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`.
(
&self,
instance_index: usize,
channels: usize,
frames: usize,
)
| 585 | /// |
| 586 | /// Returns `None` if there is no instance at `instance_index`. |
| 587 | pub fn instance<const MAX_CHANNELS: usize>( |
| 588 | &self, |
| 589 | instance_index: usize, |
| 590 | channels: usize, |
| 591 | frames: usize, |
| 592 | ) -> Option<ArrayVec<&[T], MAX_CHANNELS>> { |
| 593 | if instance_index >= self.num_instances { |
| 594 | return None; |
| 595 | } |
| 596 | |
| 597 | let frames = frames.min(self.num_frames); |
| 598 | let channels = channels.min(self.num_channels.get()).min(MAX_CHANNELS); |
| 599 | |
| 600 | let start_frame = instance_index * self.num_frames * self.num_channels.get(); |
| 601 | |
| 602 | let mut res = ArrayVec::new(); |
| 603 | |
| 604 | // SAFETY: |
| 605 | // |
| 606 | // * The constructor has set the size of the buffer to |
| 607 | // `self.frames * self.channels * self.num_instances`, and we have constrained |
| 608 | // `instance_index`, `channels` and `frames` above, so this is always within range. |
| 609 | unsafe { |
| 610 | for ch_i in 0..channels { |
| 611 | res.push_unchecked(core::slice::from_raw_parts( |
| 612 | self.buffer |
| 613 | .as_ptr() |
| 614 | .add(start_frame + (ch_i * self.num_frames)), |
| 615 | frames, |
| 616 | )); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | Some(res) |
| 621 | } |
| 622 | |
| 623 | /// Returns an array of mutable slices to the first `frames` frames of the backing buffers |
| 624 | /// for the first `num_channels` channels present of the instance at `instance_index`. |