From 10513f80a94bde633922862db9ca71337889f0e5 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 9 Feb 2023 20:37:06 +0100 Subject: Fix round robin voice stealing --- src/app.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/app.c') diff --git a/src/app.c b/src/app.c index a035022..21f376a 100644 --- a/src/app.c +++ b/src/app.c @@ -435,13 +435,13 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) { } // Find next free voice slot. - for (u8 i = state.ch_min; i <= state.ch_max; i++) { - Voice *voice = &voices[i]; + for (u8 i = state.ch_min; i <= state.ch_max; i++, next_voice++) { + Voice *voice = &voices[(next_voice % max_voices)]; // Register voice. if (!voice->active) { voice->active = 1; voice->port = port; - voice->channel = state.ch_min + (next_voice++ % max_voices) ; + voice->channel = (next_voice % max_voices); voice->d1 = d1; voice->d2 = d2; hal_send_midi(voice->port, @@ -449,17 +449,20 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) { voice->d1, voice->d2); voice->age = note_counter++; + next_voice++; break; } } - n_voices++; } else if ((status & 0xF0) == NOTEOFF) { for (u8 i = state.ch_min; i <= state.ch_max; i++) { Voice *voice = &voices[i]; if (voice->active && voice->d1 == d1) { voice->active = 0; - hal_send_midi(port, NOTEOFF | voice->channel, d1, d2); + hal_send_midi(voice->port, + NOTEOFF | voice->channel, + voice->d1, + voice->d2); n_voices--; break; } -- cgit v1.2.1