diff --git a/MicroDexed.ino b/MicroDexed.ino index 7a1b88a..a395c83 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -600,10 +600,22 @@ void loop() eeprom_update(); } - // check for value changes and unused voices + // check for value changes, unused voices and CPU overload for (uint8_t instance_id = 0; instance_id < NUM_DEXED; instance_id++) { active_voices[instance_id] = MicroDexed[instance_id]->getNumNotesPlaying(); +#ifdef CPU_OVERLOAD_THROTTLE + if (AudioProcessorUsageMax() > 99.9) + { + AudioProcessorUsageMaxReset(); + MicroDexed[instance_id]->keyup(-1); // kills the oldest note and decreases max_notes + Serial.print(F("!!!CPU overload!!! Automatic throttling max_notes to ")); + Serial.print(MicroDexed[instance_id]->getMaxNotes(), DEC); + Serial.print(F(" for instance ")); + Serial.print(instance_id, DEC); + Serial.println(F(".")); + } +#endif } } diff --git a/config.h b/config.h index c6b2a43..d8925c0 100644 --- a/config.h +++ b/config.h @@ -212,6 +212,7 @@ #define NUM_DEXED 1 #define MAX_DEXED 2 #define NORMALIZE_DX_VELOCITY 1 +#define CPU_OVERLOAD_THROTTLE 1 enum { DEXED, CHORUS, DELAY, REVERB}; diff --git a/dexed.cpp b/dexed.cpp index a7e371a..ec2f30d 100644 --- a/dexed.cpp +++ b/dexed.cpp @@ -251,10 +251,23 @@ void Dexed::keyup(int16_t pitch) { pitch += data[144] - TRANSPOSE_FIX; uint8_t note; - for (note = 0; note < max_notes; note++) { - if ( voices[note].midi_note == pitch && voices[note].keydown ) { - voices[note].keydown = false; - break; + if (pitch < 0) // for disabling the oldest note when cpu overload is detected + { + note = abs((currentNote + 1) % max_notes); + voices[note].keydown = false; + if (max_notes == note) + currentNote = 0; + else + currentNote = note; + max_notes--; + } + else + { + for (note = 0; note < max_notes; note++) { + if ( voices[note].midi_note == pitch && voices[note].keydown ) { + voices[note].keydown = false; + break; + } } }