From 68c082bebaac3b0412b2f373b27d8b8894f07467 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 9 Feb 2023 19:08:06 +0100 Subject: Add round-robin to channel selection --- src/app.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/app.c') diff --git a/src/app.c b/src/app.c index aacc68f..a035022 100644 --- a/src/app.c +++ b/src/app.c @@ -209,6 +209,7 @@ State state = {0}; u32 note_counter = 0; u8 midi_in_connected = 0; u8 midi_out_connected = 0; +u8 next_voice = 0; // store ADC frame pointer static const u16 *g_ADC = 0; @@ -365,13 +366,14 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) { return; } u8 channel = status & 0x0F; + u8 max_voices = (state.ch_max - state.ch_min + 1); // If the channel isn't set to listen, just passthrough the message. if (!state.ch_listen[channel]) { hal_send_midi(port, status, d1, d2); return; } if ((status & 0xF0) == NOTEON) { - if (n_voices == (state.ch_max - state.ch_min + 1)) { + if (n_voices == max_voices) { u8 selected = 0; switch (state.stealing) { case POLY_STEAL_OFF: { @@ -439,7 +441,7 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) { if (!voice->active) { voice->active = 1; voice->port = port; - voice->channel = i; + voice->channel = state.ch_min + (next_voice++ % max_voices) ; voice->d1 = d1; voice->d2 = d2; hal_send_midi(voice->port, @@ -455,10 +457,9 @@ app_midi_event(u8 port, u8 status, u8 d1, u8 d2) { } else if ((status & 0xF0) == NOTEOFF) { for (u8 i = state.ch_min; i <= state.ch_max; i++) { Voice *voice = &voices[i]; - // Register voice. if (voice->active && voice->d1 == d1) { voice->active = 0; - hal_send_midi(port, NOTEOFF | i, d1, d2); + hal_send_midi(port, NOTEOFF | voice->channel, d1, d2); n_voices--; break; } -- cgit v1.2.1