Skip to content

Commit

Permalink
Ensure that the current state is valid when calling ADSR::setParamete…
Browse files Browse the repository at this point in the history
…rs() after calling ADSR::noteOn() and recalculate the release rate if ADSR::noteOff() is called when not in the sustain stage
  • Loading branch information
ed95 committed Mar 11, 2019
1 parent 4c58efa commit 55bc08f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/juce_audio_basics/utilities/juce_ADSR.h
Expand Up @@ -71,6 +71,9 @@ class ADSR

sustainLevel = newParameters.sustain;
calculateRates (newParameters);

if (currentState != State::idle)
checkCurrentState();
}

/** Returns the parameters currently being used by an ADSR object.
Expand Down Expand Up @@ -115,9 +118,16 @@ class ADSR
if (currentState != State::idle)
{
if (releaseRate > 0.0f)
{
if (currentState != State::sustain)
releaseRate = static_cast<float> (envelopeVal / (currentParameters.release * sr));

currentState = State::release;
}
else
{
reset();
}
}
}

Expand Down Expand Up @@ -205,6 +215,13 @@ class ADSR
releaseRate = (parameters.release > 0.0f ? static_cast<float> (sustainLevel / (parameters.release * sr)) : -1.0f);
}

void checkCurrentState()
{
if (currentState == State::attack && attackRate <= 0.0f) currentState = decayRate > 0.0f ? State::decay : State::sustain;
else if (currentState == State::decay && decayRate <= 0.0f) currentState = State::sustain;
else if (currentState == State::release && releaseRate <= 0.0f) reset();
}

//==============================================================================
enum class State { idle, attack, decay, sustain, release };

Expand Down

0 comments on commit 55bc08f

Please sign in to comment.