synthio: replace the quietest releasing note when over-writing

This commit is contained in:
Jeff Epler 2023-05-02 14:00:31 -05:00
parent 4f56b7646e
commit c06597c07a
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE

View File

@ -352,15 +352,21 @@ STATIC int find_channel_with_note(synthio_synth_t *synth, mp_obj_t note) {
return i; return i;
} }
} }
int result = -1;
if (note == SYNTHIO_SILENCE) { if (note == SYNTHIO_SILENCE) {
// we need a victim note that is releasing. simple algorithm: lowest numbered slot // replace the releasing note with lowest volume level
for (int i = 0; i < CIRCUITPY_SYNTHIO_MAX_CHANNELS; i++) { int level = 32768;
if (!SYNTHIO_NOTE_IS_PLAYING(synth, i)) { for (int chan = 0; chan < CIRCUITPY_SYNTHIO_MAX_CHANNELS; chan++) {
return i; if (!SYNTHIO_NOTE_IS_PLAYING(synth, chan)) {
synthio_envelope_state_t *state = &synth->envelope_state[chan];
if (state->level < level) {
result = chan;
level = state->level;
}
} }
} }
} }
return -1; return result;
} }
bool synthio_span_change_note(synthio_synth_t *synth, mp_obj_t old_note, mp_obj_t new_note) { bool synthio_span_change_note(synthio_synth_t *synth, mp_obj_t old_note, mp_obj_t new_note) {