From ed72de7cbf931132bd3ba0775372963356e61e5d Mon Sep 17 00:00:00 2001 From: Marcus Ramse Date: Tue, 16 Apr 2024 16:45:54 +0000 Subject: [PATCH] Fix CPAL buffer bug (thanks @jesnor) --- src/wrapper/standalone/backend/cpal.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/wrapper/standalone/backend/cpal.rs b/src/wrapper/standalone/backend/cpal.rs index 9ddf6d2d..054dfe43 100644 --- a/src/wrapper/standalone/backend/cpal.rs +++ b/src/wrapper/standalone/backend/cpal.rs @@ -824,8 +824,10 @@ impl CpalMidir { } { + let sample_count = data.len() / num_output_channels; + assert!(sample_count <= buffer_size); let buffers = unsafe { - buffer_manager.create_buffers(0, buffer_size, |buffer_sources| { + buffer_manager.create_buffers(0, sample_count, |buffer_sources| { *buffer_sources.main_output_channel_pointers = Some(ChannelPointers { ptrs: NonNull::new(main_io_channel_pointers.get().as_mut_ptr()) .unwrap(), @@ -893,11 +895,10 @@ impl CpalMidir { // The buffer's samples need to be written to `data` in an interlaced format // SAFETY: Dropping `buffers` allows us to borrow `main_io_storage` again - for (output_sample, buffer_sample) in data - .iter_mut() - .zip(main_io_storage.iter().flat_map(|channels| channels.iter())) - { - *output_sample = T::from_sample(*buffer_sample); + for (i, output_sample) in data.iter_mut().enumerate() { + let ch = i % num_output_channels; + let n = i / num_output_channels; + *output_sample = T::from_sample(main_io_storage[ch][n]); } if let Some(output_event_rb_producer) = &mut output_event_rb_producer {