From eac88c4477b7ce5c824c79fa19f920e7dd0fdfc1 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Wed, 1 Feb 2023 17:18:06 +0100 Subject: [PATCH] Clamp event times for JACK standalones --- src/wrapper/standalone/backend/jack.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/wrapper/standalone/backend/jack.rs b/src/wrapper/standalone/backend/jack.rs index eb47f2fc..80a0f71f 100644 --- a/src/wrapper/standalone/backend/jack.rs +++ b/src/wrapper/standalone/backend/jack.rs @@ -121,11 +121,15 @@ impl Backend

for Jack { input_events.clear(); if let Some(midi_input) = &midi_input { - input_events.extend( - midi_input - .iter(ps) - .filter_map(|midi| NoteEvent::from_midi(midi.time, midi.bytes).ok()), - ); + input_events.extend(midi_input.iter(ps).filter_map(|midi| { + nih_debug_assert!( + midi.time < num_frames, + "Input event is out of bounds, will be clamped to the buffer's size" + ); + let timing = midi.time.min(num_frames - 1); + + NoteEvent::from_midi(timing, midi.bytes).ok() + })); } output_events.clear(); @@ -134,7 +138,12 @@ impl Backend

for Jack { let mut midi_output = midi_output.lock(); let mut midi_writer = midi_output.writer(ps); for event in output_events.drain(..) { - let timing = event.timing(); + // Out of bounds events are clamped to the buffer's size + nih_debug_assert!( + event.timing() < num_frames, + "Output event is out of bounds, will be clamped to the buffer's size" + ); + let timing = event.timing().min(num_frames - 1); match event.as_midi() { Some(MidiResult::Basic(midi_data)) => {