From 916dfc16a28024c9b42075ceb332f7e74905fb54 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 5 May 2024 22:36:21 +0200 Subject: [PATCH] Document the CPAL buffer size handling --- CHANGELOG.md | 8 ++++++++ src/wrapper/standalone/backend/cpal.rs | 14 +++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7dbb0d5..502b172b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,14 @@ Since there is no stable release yet, the changes are organized per day in reverse chronological order. The main purpose of this document in its current state is to list breaking changes. +## [2024-05-05] + +### Fixed + +- The CPAL backend now correctly handles situations where it receives fewer + samples than configured. +- Fixed the handling of multichannel audio in the CPAL backend. + ## [2024-05-04] ### Fixed diff --git a/src/wrapper/standalone/backend/cpal.rs b/src/wrapper/standalone/backend/cpal.rs index 054dfe43..f45adb2c 100644 --- a/src/wrapper/standalone/backend/cpal.rs +++ b/src/wrapper/standalone/backend/cpal.rs @@ -702,6 +702,8 @@ impl CpalMidir { .main_input_channels .map(NonZeroU32::get) .unwrap_or(0) as usize; + // This may contain excess unused space at the end if we get fewer samples than configured + // from CPAL let mut main_io_storage = vec![vec![0.0f32; buffer_size]; num_output_channels]; // This backend does not support auxiliary inputs and outputs, so in order to have the same @@ -824,10 +826,16 @@ impl CpalMidir { } { - let sample_count = data.len() / num_output_channels; - assert!(sample_count <= buffer_size); + // Even though we told CPAL that we wanted `buffer_size` samples, it may still give + // us fewer. If we receive more than what we configured, then this will panic. + let actual_sample_count = data.len() / num_output_channels; + assert!( + actual_sample_count <= buffer_size, + "Received {actual_sample_count} samples, while the configured buffer size is \ + {buffer_size}" + ); let buffers = unsafe { - buffer_manager.create_buffers(0, sample_count, |buffer_sources| { + buffer_manager.create_buffers(0, actual_sample_count, |buffer_sources| { *buffer_sources.main_output_channel_pointers = Some(ChannelPointers { ptrs: NonNull::new(main_io_channel_pointers.get().as_mut_ptr()) .unwrap(),