57 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0))
return;
60 dipDown_ = coeff(400.0);
62 gateDown_ = coeff(50.0);
63 refFall_ = coeff(10000.0);
72 dipEnv_ = gateEnv_ = dipRef_ = gateRef_ = 0.0;
80 if (std::isfinite(dB)) dipDb_.store(std::clamp(dB, T(0), T(48)), std::memory_order_relaxed);
89 if (std::isfinite(thresholdDb)) gateDb_.store(std::clamp(thresholdDb, T(-200), T(0)), std::memory_order_relaxed);
95 if (std::isfinite(ms)) holdMs_.store(std::clamp(ms, T(0), T(4000)), std::memory_order_relaxed);
101 if (std::isfinite(ms)) releaseMs_.store(std::clamp(ms, T(5), T(4000)), std::memory_order_relaxed);
104 [[nodiscard]] T
getDryDip() const noexcept {
return dipDb_.load(std::memory_order_relaxed); }
105 [[nodiscard]] T
getGate() const noexcept {
return gateDb_.load(std::memory_order_relaxed); }
106 [[nodiscard]] T
getGateHold() const noexcept {
return holdMs_.load(std::memory_order_relaxed); }
107 [[nodiscard]] T
getGateRelease() const noexcept {
return releaseMs_.load(std::memory_order_relaxed); }
112 dip_ =
static_cast<double>(dipDb_.load(std::memory_order_relaxed)) * (2.302585092994046 / 20.0);
113 const double gateDb =
static_cast<double>(gateDb_.load(std::memory_order_relaxed));
114 gateOn_ = gateDb >= -80.0;
115 gateLin_ = std::pow(10.0, gateDb / 20.0);
116 holdSamples_ =
static_cast<int>(
static_cast<double>(holdMs_.load(std::memory_order_relaxed)) * 0.001 * fs_);
117 release_ = coeff(
static_cast<double>(releaseMs_.load(std::memory_order_relaxed)));
121 [[nodiscard]]
bool isActive() const noexcept {
return dip_ > 0.0 || gateOn_ || g2_ != 1.0; }
128 [[nodiscard]] T
next(T dryLevel)
noexcept
130 const double x = std::isfinite(dryLevel) ? std::abs(
static_cast<double>(dryLevel)) : 0.0;
131 dipEnv_ = x + (x > dipEnv_ ? dipUp_ : dipDown_) * (dipEnv_ - x);
132 gateEnv_ = x + (x > gateEnv_ ? gateUp_ : gateDown_) * (gateEnv_ - x);
134 dipRef_ = dipEnv_ > dipRef_ ? dipEnv_ : dipRef_ * refFall_;
135 gateRef_ = gateEnv_ > gateRef_ ? gateEnv_ : gateRef_ * refFall_;
136 if (dipRef_ < 1e-12) dipRef_ = 0.0;
137 if (gateRef_ < 1e-12) gateRef_ = 0.0;
139 if (dip_ > 0.0 && dipRef_ > 0.0)
140 gain = std::exp(-dip_ * std::min(1.0, dipEnv_ / dipRef_));
144 if (gateRef_ > 0.0 && gateEnv_ >= gateRef_ * gateLin_) hold_ = holdSamples_ + 1;
145 if (hold_ > 0) --hold_;
148 if (g1_ != target || g2_ != target)
150 const double a = target > g1_ ? open_ : release_;
151 g1_ = target + a * (g1_ - target);
152 g2_ = g1_ + a * (g2_ - g1_);
153 if (std::abs(g2_ - target) < 1e-9 && std::abs(g1_ - target) < 1e-9) g1_ = g2_ = target;
155 return static_cast<T
>(gain * g2_);
159 [[nodiscard]]
double coeff(
double ms)
const noexcept {
return std::exp(-1.0 / (ms * 0.001 * fs_)); }
161 std::atomic<T> dipDb_ { T(0) }, gateDb_ { T(-200) }, holdMs_ { T(100) }, releaseMs_ { T(150) };
163 double fs_ = 48000.0;
164 double dipUp_ = 0.0, dipDown_ = 0.0, gateUp_ = 0.0, gateDown_ = 0.0, refFall_ = 0.0, open_ = 0.0, release_ = 0.0;
165 double dip_ = 0.0, gateLin_ = 0.0;
166 bool gateOn_ =
false;
167 int holdSamples_ = 0, hold_ = 0;
168 double dipEnv_ = 0.0, gateEnv_ = 0.0, dipRef_ = 0.0, gateRef_ = 0.0, g1_ = 1.0, g2_ = 1.0;
A dip and a relative gate driven by the dry signal.
bool isActive() const noexcept
Whether next() can return anything but 1 (as of the last sync()).
void setGateRelease(T ms) noexcept
Time constant of the gate's closing, 5 to 4000 ms (default 150).
void sync() noexcept
Takes the settings for the next block (audio thread).
void setGateHold(T ms) noexcept
Time the gate stays open after the last crossing, 0 to 4000 ms (default 100).
void setGate(T thresholdDb) noexcept
Gate threshold in dB under the dry signal's recent peak, -80 to 0; below -80 the gate is off (the def...
void prepare(double sampleRate) noexcept
Prepares for a sample rate (an invalid one is ignored) and clears.
T getDryDip() const noexcept
T next(T dryLevel) noexcept
One sample: the gain for the wet signal.
void reset() noexcept
Clears the followers; the gate starts open and the dip released.
T getGate() const noexcept
void setDryDip(T dB) noexcept
Depth of the dip in dB, 0 to 48 (0, the default, is off).
T getGateHold() const noexcept
T getGateRelease() const noexcept
Main namespace for the DSPark framework.