Skip to content

Commit

Permalink
improve logging for wrong sized output channel and let it play anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Sep 21, 2024
1 parent e3da605 commit 3ec0f03
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/audio/midi_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,31 @@ fn new_output_stream(
}
}
// Split buffer for this run between left and right
let channel_len = output.len() / 2;

if left.len() < channel_len || right.len() < channel_len {
log::info!("Buffer too small, skipping audio rendering");
return;
let mut output_channel_len = output.len() / 2;

if left.len() < output_channel_len || right.len() < output_channel_len {
log::info!(
"Output buffer larger than expected channel size {} > {}",
output_channel_len,
left.len()
);
output_channel_len = left.len()
}

// Render the waveform.
synthesizer_guard.render(&mut left[..channel_len], &mut right[..channel_len]);
synthesizer_guard.render(
&mut left[..output_channel_len],
&mut right[..output_channel_len],
);

// Drop locks
drop(sequencer_guard);
drop(synthesizer_guard);
drop(player_params_guard);

// Interleave the left and right channels into the output buffer.
for (i, (l, r)) in left.iter().zip(right.iter()).take(channel_len).enumerate() {
let stereo_interleaved = left.iter().zip(right.iter());
for (i, (l, r)) in stereo_interleaved.take(output_channel_len).enumerate() {
output[i * 2] = *l;
output[i * 2 + 1] = *r;
}
Expand Down

0 comments on commit 3ec0f03

Please sign in to comment.