small improvement for limitSampleDynamics

support the case when only attackTime XOR decayTime is defined
This commit is contained in:
Frank 2022-08-14 14:38:27 +02:00
parent 72770e5809
commit 8acb44b202

View File

@ -687,17 +687,20 @@ class AudioReactive : public Usermod {
static unsigned long last_time = 0;
static float last_volumeSmth = 0.0f;
if ((attackTime > 0) && (decayTime > 0)) { // only change volume if user has defined attack>0 and decay>0
long delta_time = millis() - last_time;
delta_time = constrain(delta_time , 1, 1000); // below 1ms -> 1ms; above 1sec -> sily lil hick-up
float maxAttack = bigChange * float(delta_time) / float(attackTime);
float maxDecay = - bigChange * float(delta_time) / float(decayTime);
float deltaSample = volumeSmth - last_volumeSmth;
if (attackTime > 0) { // user has defined attack time > 0
float maxAttack = bigChange * float(delta_time) / float(attackTime);
if (deltaSample > maxAttack) deltaSample = maxAttack;
if (deltaSample < maxDecay) deltaSample = maxDecay;
volumeSmth = last_volumeSmth + deltaSample;
}
if (decayTime > 0) { // user has defined decay time > 0
float maxDecay = - bigChange * float(delta_time) / float(decayTime);
if (deltaSample < maxDecay) deltaSample = maxDecay;
}
volumeSmth = last_volumeSmth + deltaSample;
last_volumeSmth = volumeSmth;
last_time = millis();