81 if (!(sampleRate > 0.0) || !std::isfinite(sampleRate))
return;
82 sampleRate_ = sampleRate;
89 void setDelay(T ms)
noexcept { setTime(delayMs_, ms); }
91 void setAttack(T ms)
noexcept { setTime(attackMs_, ms); }
93 void setHold(T ms)
noexcept { setTime(holdMs_, ms); }
95 void setDecay(T ms)
noexcept { setTime(decayMs_, ms); }
97 void setRelease(T ms)
noexcept { setTime(releaseMs_, ms); }
100 void setTimes(T delayMs, T attackMs, T holdMs, T decayMs, T releaseMs)
noexcept
116 if (!std::isfinite(level))
return;
117 const double s = std::clamp(
static_cast<double>(level), 0.0, 1.0);
118 if (s == sustain_)
return;
119 const double now = value_;
124 const double f = shape(x_, decayCurve_);
125 if (f < 1.0 - 1e-12) from_ = (now - sustain_ * f) / (1.0 - f);
134 decayScale_ = std::abs(sustain_ - now);
136 curveNow_ = decayCurve_;
149 if (!std::isfinite(attack) || !std::isfinite(decay) || !std::isfinite(release))
return;
150 const double a = std::clamp(
static_cast<double>(attack), -1.0, 1.0);
151 const double d = std::clamp(
static_cast<double>(decay), -1.0, 1.0);
152 const double r = std::clamp(
static_cast<double>(release), -1.0, 1.0);
153 const double* mine = curveOf(stage_, a, d, r);
154 if (mine !=
nullptr && *mine != curveNow_)
157 const double place = (to_ != from_) ? std::clamp((value_ - from_) / (to_ - from_), 0.0, 1.0) : 1.0;
158 x_ = inverse(place, *mine);
169 [[nodiscard]]
bool isBipolar() const noexcept {
return bipolar_; }
180 if (!std::isfinite(db))
return;
181 thresholdDb_ = std::clamp(
static_cast<double>(db), -120.0, 24.0);
182 recalculateTrigger();
188 if (!std::isfinite(db))
return;
189 hysteresisDb_ = std::clamp(
static_cast<double>(db), 0.0, 40.0);
190 recalculateTrigger();
196 if (!(ms >= T(0)) || !std::isfinite(ms))
return;
197 waitMs_ =
static_cast<double>(ms);
198 recalculateTrigger();
207 if (delaySteps_ <= 0.0) enterAttack();
230 [[nodiscard]] T
getValue() const noexcept {
return static_cast<T
>(value_); }
252 if (out ==
nullptr || numSamples <= 0)
return;
253 for (
int i = 0; i < numSamples; ++i)
254 out[i] =
process(key !=
nullptr ? key[i] : T(0));
258 void setTime(
double& slot, T ms)
noexcept
260 if (!(ms >= T(0)) || !std::isfinite(ms))
return;
261 slot =
static_cast<double>(ms);
265 [[nodiscard]]
double samplesFor(
double ms)
const noexcept {
return std::round(ms * 0.001 * sampleRate_); }
268 [[nodiscard]]
double stageSamples() const noexcept
273 case Stage::Decay:
return samplesFor(decayMs_ * decayScale_);
281 void aim(
double samples)
noexcept
283 left_ = std::max<int64_t>(1,
static_cast<int64_t
>(std::llround((1.0 - x_) * samples)));
284 moveStep_ = (1.0 - x_) /
static_cast<double>(left_);
287 void recalculate() noexcept
289 delaySteps_ = std::round(delayMs_ * 0.001 * sampleRate_);
290 holdSteps_ = std::round(holdMs_ * 0.001 * sampleRate_);
291 attackN_ = samplesFor(attackMs_);
292 releaseN_ = samplesFor(releaseMs_);
294 recalculateTrigger();
297 void recalculateTrigger() noexcept
299 threshold_ = std::pow(10.0, thresholdDb_ / 20.0);
300 lowLevel_ = std::pow(10.0, (thresholdDb_ - hysteresisDb_) / 20.0);
301 waitSamples_ =
static_cast<int64_t
>(std::llround(waitMs_ * 0.001 * sampleRate_));
302 levelFall_ = std::exp(-1.0 / (kDetectorReleaseSeconds * sampleRate_));
306 [[nodiscard]]
static double shape(
double x,
double curve)
noexcept
308 if (curve == 0.0)
return x;
309 const double k = 6.0 * curve;
310 return std::expm1(-k * x) / std::expm1(-k);
314 [[nodiscard]]
static double inverse(
double y,
double curve)
noexcept
316 if (curve == 0.0)
return y;
317 const double k = 6.0 * curve;
318 return std::clamp(-std::log1p(y * std::expm1(-k)) / k, 0.0, 1.0);
321 [[nodiscard]]
const double* curveOf(
Stage stage,
const double& a,
const double& d,
const double& r)
const noexcept
328 default:
return nullptr;
332 void enterAttack() noexcept
337 curveNow_ = attackCurve_;
338 x_ = inverse(std::clamp(held_, 0.0, 1.0), curveNow_);
339 if (x_ >= 1.0) { enterHold();
return; }
343 void enterHold() noexcept
348 if (holdSteps_ <= 0.0) enterDecay();
351 void enterDecay() noexcept
356 curveNow_ = decayCurve_;
362 void enterRelease() noexcept
367 curveNow_ = releaseCurve_;
368 x_ = inverse(1.0 - std::clamp(value_, 0.0, 1.0), curveNow_);
372 void advance() noexcept
381 if (++x_ >= delaySteps_) enterAttack();
385 if (++x_ >= holdSteps_) enterDecay();
404 value_ = from_ + (to_ - from_) * shape(x_, curveNow_);
408 void listen(
double key)
noexcept
410 const double a = std::isfinite(key) ? std::abs(key) : 0.0;
411 level_ = std::max(a, level_ * levelFall_);
414 if (level_ > threshold_)
422 if (level_ >= lowLevel_) { below_ = 0;
return; }
423 if (++below_ >= waitSamples_)
430 [[nodiscard]] T output() const noexcept
432 if (!bipolar_)
return static_cast<T
>(value_);
433 const double s = sustain_;
434 if (value_ >= s)
return static_cast<T
>(s < 1.0 ? (value_ - s) / (1.0 - s) : 0.0);
435 return static_cast<T
>(s > 0.0 ? (value_ - s) / s : 0.0);
438 static constexpr double kDetectorReleaseSeconds = 0.01;
440 double sampleRate_ = 48000.0;
441 double delayMs_ = 0.0, attackMs_ = 10.0, holdMs_ = 0.0, decayMs_ = 100.0, releaseMs_ = 200.0;
442 double sustain_ = 0.7;
443 double attackCurve_ = 0.0, decayCurve_ = 0.0, releaseCurve_ = 0.0;
444 bool bipolar_ =
false;
446 double delaySteps_ = 0.0, holdSteps_ = 0.0;
447 double attackN_ = 0.0, releaseN_ = 0.0;
452 double from_ = 0.0, to_ = 0.0;
453 double curveNow_ = 0.0;
454 double moveStep_ = 0.0;
456 double decayScale_ = 1.0;
460 double thresholdDb_ = -40.0, hysteresisDb_ = 6.0, waitMs_ = 50.0;
461 double threshold_ = 0.01, lowLevel_ = 0.005, levelFall_ = 0.99;
462 int64_t waitSamples_ = 2400;