Skip to content

Commit c06597c

Browse files
committed
synthio: replace the quietest releasing note when over-writing
1 parent 4f56b76 commit c06597c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

shared-module/synthio/__init__.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,21 @@ STATIC int find_channel_with_note(synthio_synth_t *synth, mp_obj_t note) {
352352
return i;
353353
}
354354
}
355+
int result = -1;
355356
if (note == SYNTHIO_SILENCE) {
356-
// we need a victim note that is releasing. simple algorithm: lowest numbered slot
357-
for (int i = 0; i < CIRCUITPY_SYNTHIO_MAX_CHANNELS; i++) {
358-
if (!SYNTHIO_NOTE_IS_PLAYING(synth, i)) {
359-
return i;
357+
// replace the releasing note with lowest volume level
358+
int level = 32768;
359+
for (int chan = 0; chan < CIRCUITPY_SYNTHIO_MAX_CHANNELS; chan++) {
360+
if (!SYNTHIO_NOTE_IS_PLAYING(synth, chan)) {
361+
synthio_envelope_state_t *state = &synth->envelope_state[chan];
362+
if (state->level < level) {
363+
result = chan;
364+
level = state->level;
365+
}
360366
}
361367
}
362368
}
363-
return -1;
369+
return result;
364370
}
365371

366372
bool synthio_span_change_note(synthio_synth_t *synth, mp_obj_t old_note, mp_obj_t new_note) {

0 commit comments

Comments
 (0)