ADSR goes crazy if you use times of 0s

#1

ADSR goes crazy if you use times of 0s. If you use a time of 0 the rate is set to -1.

    attackRate  = (parameters.attack  > 0.0f ? static_cast<float> (1.0f                  / (parameters.attack * sr))  : -1.0f);
    decayRate   = (parameters.decay   > 0.0f ? static_cast<float> ((1.0f - sustainLevel) / (parameters.decay * sr))   : -1.0f);
    releaseRate = (parameters.release > 0.0f ? static_cast<float> (sustainLevel          / (parameters.release * sr)) : -1.0f);

But in getNextSample it is assumed the rate is set properly.

    if (currentState == State::attack)
    {
        envelopeVal += attackRate;

        if (envelopeVal >= 1.0f)
        {
            envelopeVal = 1.0f;

            if (decayRate > 0.0f)
                currentState = State::decay;
            else
                currentState = State::sustain;
        }
    }

So my envelope value goes flying off to -∞!

0 Likes

#2

From looking at the code, this should only happen if you set the attack time to zero during the attack-phase. But that’s possible, so it should be fixed.

Looking at the noteOn() method, if attackRate is zero or negative, it will skipped and the state will set to State::decay, however without setting the envelopeVal to 1.0f. That means, the decay phase is also skipped automatically.

Edit: as -infinity is very loud and can be dangerous: @ed95 would you take a look at this? :slight_smile:

0 Likes

#3

Yep, I’ve pushed a fix to develop here and I’ll cherry-pick it onto the master branch too. Thanks for reporting!

2 Likes