Browse Source

Ensure that the current state is valid when calling ADSR::setParameters() after calling ADSR::noteOn() and recalculate the release rate if ADSR::noteOff() is called when not in the sustain stage

tags/2021-05-28
ed 6 years ago
parent
commit
efe197de39
1 changed files with 17 additions and 0 deletions
  1. +17
    -0
      modules/juce_audio_basics/utilities/juce_ADSR.h

+ 17
- 0
modules/juce_audio_basics/utilities/juce_ADSR.h View File

@@ -71,6 +71,9 @@ public:
sustainLevel = newParameters.sustain;
calculateRates (newParameters);
if (currentState != State::idle)
checkCurrentState();
}
/** Returns the parameters currently being used by an ADSR object.
@@ -115,9 +118,16 @@ public:
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();
}
}
}
@@ -205,6 +215,13 @@ private:
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 };


Loading…
Cancel
Save