Browse Source

ADSR: Fix a bug in noteOn after setting new parameters

pull/22/head
Tom Poole 3 years ago
parent
commit
d9c25ec17d
1 changed files with 50 additions and 26 deletions
  1. +50
    -26
      modules/juce_audio_basics/utilities/juce_ADSR.h

+ 50
- 26
modules/juce_audio_basics/utilities/juce_ADSR.h View File

@@ -115,6 +115,7 @@ public:
{
if (attackRate > 0.0f)
{
envelopeVal = 0.0f;
state = State::attack;
}
else if (decayRate > 0.0f)
@@ -153,39 +154,54 @@ public:
*/
float getNextSample() noexcept
{
if (state == State::idle)
return 0.0f;
if (state == State::attack)
switch (state)
{
envelopeVal += attackRate;
case State::idle:
{
return 0.0f;
}
if (envelopeVal >= 1.0f)
case State::attack:
{
envelopeVal = 1.0f;
goToNextState();
envelopeVal += attackRate;
if (envelopeVal >= 1.0f)
{
envelopeVal = 1.0f;
goToNextState();
}
break;
}
}
else if (state == State::decay)
{
envelopeVal -= decayRate;
if (envelopeVal <= parameters.sustain)
case State::decay:
{
envelopeVal -= decayRate;
if (envelopeVal <= parameters.sustain)
{
envelopeVal = parameters.sustain;
goToNextState();
}
break;
}
case State::sustain:
{
envelopeVal = parameters.sustain;
goToNextState();
break;
}
}
else if (state == State::sustain)
{
envelopeVal = parameters.sustain;
}
else if (state == State::release)
{
envelopeVal -= releaseRate;
if (envelopeVal <= 0.0f)
goToNextState();
case State::release:
{
envelopeVal -= releaseRate;
if (envelopeVal <= 0.0f)
goToNextState();
break;
}
}
return envelopeVal;
@@ -250,10 +266,18 @@ private:
void goToNextState() noexcept
{
if (state == State::attack)
{
state = (decayRate > 0.0f ? State::decay : State::sustain);
else if (state == State::decay)
return;
}
if (state == State::decay)
{
state = State::sustain;
else if (state == State::release)
return;
}
if (state == State::release)
reset();
}


Loading…
Cancel
Save