From b85fef5c166ccd8ae81a32c00b8573a445b610d0 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sat, 5 Feb 2022 05:01:31 -0800 Subject: [PATCH] MIDI Input: Fix seeking code This fixes a possible crash with seeking operations, especially with Audio Unit plugins. Fix implemented in foo_midi and imported here. Signed-off-by: Christopher Snowhill --- Plugins/MIDI/MIDI/MIDIPlayer.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Plugins/MIDI/MIDI/MIDIPlayer.cpp b/Plugins/MIDI/MIDI/MIDIPlayer.cpp index a0e1fda47..3fa467d3e 100644 --- a/Plugins/MIDI/MIDI/MIDIPlayer.cpp +++ b/Plugins/MIDI/MIDI/MIDIPlayer.cpp @@ -347,13 +347,22 @@ void MIDIPlayer::Seek(unsigned long sample) temp = (float *) malloc(needs_time * 2 * sizeof(float)); if (temp) { + render(temp, needs_time); // flush events unsigned int render_junk = 0; + bool timestamp_set = false; + unsigned last_timestamp = 0; for (i = 0; i < stream_start; i++) { if (me[i].m_event) { send_event_time_filtered(me[i].m_event, render_junk); - render_junk += 16; + if (timestamp_set) { + if (me[i].m_timestamp != last_timestamp) { + render_junk += 16; + } + } + last_timestamp = me[i].m_timestamp; + timestamp_set = true; if (render_junk >= needs_time) { render(temp, needs_time); @@ -361,7 +370,7 @@ void MIDIPlayer::Seek(unsigned long sample) } } } - uSamplesRemaining = render_junk; + render(temp, needs_time); free(temp); } } @@ -370,13 +379,24 @@ void MIDIPlayer::Seek(unsigned long sample) temp = (float *) malloc(16 * 2 * sizeof(float)); if (temp) { + render(temp, 16); + bool timestamp_set = false; + unsigned last_timestamp = 0; for (i = 0; i < stream_start; i++) { if (me[i].m_event) { + if (timestamp_set) { + if (me[i].m_timestamp != last_timestamp) { + render(temp, 16); + } + } + last_timestamp = me[i].m_timestamp; + timestamp_set = true; send_event_filtered(me[i].m_event); } } + render(temp, 16); free(temp); } }