Fix the value bounds in ADSR

This commit is contained in:
Jean Pierre Cimalando 2021-04-01 01:39:30 +02:00
parent 8f81f6327d
commit fb01fa7cfd
3 changed files with 16 additions and 13 deletions

View file

@ -13,14 +13,17 @@ namespace sfz {
using Float = ADSREnvelope::Float;
Float ADSREnvelope::secondsToSamples(Float timeInSeconds) const noexcept
int ADSREnvelope::secondsToSamples(Float timeInSeconds) const noexcept
{
if (timeInSeconds <= 0)
return Float(0);
return static_cast<int>(timeInSeconds * sampleRate);
};
Float ADSREnvelope::secondsToLinRate(Float timeInSeconds) const noexcept
{
if (timeInSeconds == 0)
if (timeInSeconds <= 0)
return Float(1);
return 1 / (sampleRate * timeInSeconds);
@ -28,7 +31,7 @@ Float ADSREnvelope::secondsToLinRate(Float timeInSeconds) const noexcept
Float ADSREnvelope::secondsToExpRate(Float timeInSeconds) const noexcept
{
if (timeInSeconds == 0)
if (timeInSeconds <= 0)
return Float(0.0);
timeInSeconds = std::max(Float(25e-3), timeInSeconds);
@ -44,8 +47,8 @@ void ADSREnvelope::reset(const EGDescription& desc, const Region& region, const
this->decayRate = secondsToExpRate(desc.getDecay(state, velocity));
this->releaseRate = secondsToExpRate(desc.getRelease(state, velocity));
this->hold = secondsToSamples(desc.getHold(state, velocity));
this->sustain = desc.getSustain(state, velocity);
this->start = desc.getStart(state, velocity);
this->sustain = clamp(desc.getSustain(state, velocity), 0.0f, 1.0f);
this->start = clamp(desc.getStart(state, velocity), 0.0f, 1.0f);
releaseDelay = 0;
sustainThreshold = this->sustain + config::virtuallyZero;

View file

@ -72,7 +72,7 @@ public:
private:
float sampleRate { config::defaultSampleRate };
Float secondsToSamples(Float timeInSeconds) const noexcept;
int secondsToSamples(Float timeInSeconds) const noexcept;
Float secondsToLinRate(Float timeInSeconds) const noexcept;
Float secondsToExpRate(Float timeInSeconds) const noexcept;

View file

@ -104,7 +104,7 @@ struct EGDescription {
for (auto& mod: ccAttack) {
returnedValue += state.getCCValue(mod.cc) * mod.data;
}
return Default::egTime.bounds.clamp(returnedValue);
return returnedValue;
}
/**
* @brief Get the decay with possibly a CC modifier and a velocity modifier
@ -120,7 +120,7 @@ struct EGDescription {
for (auto& mod: ccDecay) {
returnedValue += state.getCCValue(mod.cc) * mod.data;
}
return Default::egTime.bounds.clamp(returnedValue);
return returnedValue;
}
/**
* @brief Get the delay with possibly a CC modifier and a velocity modifier
@ -136,7 +136,7 @@ struct EGDescription {
for (auto& mod: ccDelay) {
returnedValue += state.getCCValue(mod.cc) * mod.data;
}
return Default::egTime.bounds.clamp(returnedValue);
return returnedValue;
}
/**
* @brief Get the holding duration with possibly a CC modifier and a velocity modifier
@ -152,7 +152,7 @@ struct EGDescription {
for (auto& mod: ccHold) {
returnedValue += state.getCCValue(mod.cc) * mod.data;
}
return Default::egTime.bounds.clamp(returnedValue);
return returnedValue;
}
/**
* @brief Get the release duration with possibly a CC modifier and a velocity modifier
@ -168,7 +168,7 @@ struct EGDescription {
for (auto& mod: ccRelease) {
returnedValue += state.getCCValue(mod.cc) * mod.data;
}
return Default::egTime.bounds.clamp(returnedValue);
return returnedValue;
}
/**
* @brief Get the starting level with possibly a CC modifier and a velocity modifier
@ -184,7 +184,7 @@ struct EGDescription {
for (auto& mod: ccStart) {
returnedValue += state.getCCValue(mod.cc) * mod.data;
}
return Default::egPercent.bounds.clamp(returnedValue);
return returnedValue;
}
/**
* @brief Get the sustain level with possibly a CC modifier and a velocity modifier
@ -200,7 +200,7 @@ struct EGDescription {
for (auto& mod: ccSustain) {
returnedValue += state.getCCValue(mod.cc) * mod.data;
}
return Default::egPercent.bounds.clamp(returnedValue);
return returnedValue;
}
LEAK_DETECTOR(EGDescription);
};