From 693b618bc5b3b904097fbaaefa3932e024d1f6bf Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 12 Feb 2022 19:24:41 +0100 Subject: [PATCH] Add accessors for channel data This is needed when you want to modify the same sample multiple times within an outer loop. --- src/buffer.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/buffer.rs b/src/buffer.rs index d008f05d..dac80e21 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -123,3 +123,25 @@ impl<'outer, 'inner> Iterator for Channels<'outer, 'inner> { } impl<'outer, 'inner> ExactSizeIterator for Channels<'outer, 'inner> {} + +impl<'outer, 'inner> Channels<'outer, 'inner> { + /// Access a sample by index. Useful when you would otehrwise iterate over this 'Channels' + /// iterator multiple times. + #[inline] + pub fn get_mut(&mut self, index: usize) -> Option<&mut f32> { + // SAFETY: The channel bound has already been checked + unsafe { + self.buffers + .get_unchecked_mut(self.current_channel) + .get_mut(index) + } + } + + /// The same as [Self::get_mut], but without any bounds checking. + #[inline] + pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut f32 { + self.buffers + .get_unchecked_mut(self.current_channel) + .get_unchecked_mut(index) + } +}