Skip to content

Commit

Permalink
Document the CPAL buffer size handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robbert-vdh committed May 5, 2024
1 parent a5abe89 commit 916dfc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/wrapper/standalone/backend/cpal.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 916dfc1

Please sign in to comment.