DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
TailShaper.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
34#include "DspMath.h"
35
36#include <algorithm>
37#include <atomic>
38#include <cmath>
39
40namespace dspark {
41
48template <FloatType T>
50{
51public:
52 TailShaper() noexcept { prepare(48000.0); }
53
55 void prepare(double sampleRate) noexcept
56 {
57 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0)) return;
58 fs_ = sampleRate;
59 dipUp_ = coeff(30.0);
60 dipDown_ = coeff(400.0);
61 gateUp_ = coeff(1.0);
62 gateDown_ = coeff(50.0);
63 refFall_ = coeff(10000.0);
64 open_ = coeff(2.0);
65 reset();
66 sync();
67 }
68
70 void reset() noexcept
71 {
72 dipEnv_ = gateEnv_ = dipRef_ = gateRef_ = 0.0;
73 hold_ = 0;
74 g1_ = g2_ = 1.0;
75 }
76
78 void setDryDip(T dB) noexcept
79 {
80 if (std::isfinite(dB)) dipDb_.store(std::clamp(dB, T(0), T(48)), std::memory_order_relaxed);
81 }
82
87 void setGate(T thresholdDb) noexcept
88 {
89 if (std::isfinite(thresholdDb)) gateDb_.store(std::clamp(thresholdDb, T(-200), T(0)), std::memory_order_relaxed);
90 }
91
93 void setGateHold(T ms) noexcept
94 {
95 if (std::isfinite(ms)) holdMs_.store(std::clamp(ms, T(0), T(4000)), std::memory_order_relaxed);
96 }
97
99 void setGateRelease(T ms) noexcept
100 {
101 if (std::isfinite(ms)) releaseMs_.store(std::clamp(ms, T(5), T(4000)), std::memory_order_relaxed);
102 }
103
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); }
108
110 void sync() noexcept
111 {
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)));
118 }
119
121 [[nodiscard]] bool isActive() const noexcept { return dip_ > 0.0 || gateOn_ || g2_ != 1.0; }
122
128 [[nodiscard]] T next(T dryLevel) noexcept
129 {
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);
133 // Each follower against its own recent peak.
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;
138 double gain = 1.0;
139 if (dip_ > 0.0 && dipRef_ > 0.0)
140 gain = std::exp(-dip_ * std::min(1.0, dipEnv_ / dipRef_));
141 double target = 1.0;
142 if (gateOn_)
143 {
144 if (gateRef_ > 0.0 && gateEnv_ >= gateRef_ * gateLin_) hold_ = holdSamples_ + 1;
145 if (hold_ > 0) --hold_;
146 else target = 0.0;
147 }
148 if (g1_ != target || g2_ != target)
149 {
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;
154 }
155 return static_cast<T>(gain * g2_);
156 }
157
158private:
159 [[nodiscard]] double coeff(double ms) const noexcept { return std::exp(-1.0 / (ms * 0.001 * fs_)); }
160
161 std::atomic<T> dipDb_ { T(0) }, gateDb_ { T(-200) }, holdMs_ { T(100) }, releaseMs_ { T(150) };
162
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;
169};
170
171} // namespace dspark
A dip and a relative gate driven by the dry signal.
Definition TailShaper.h:50
bool isActive() const noexcept
Whether next() can return anything but 1 (as of the last sync()).
Definition TailShaper.h:121
void setGateRelease(T ms) noexcept
Time constant of the gate's closing, 5 to 4000 ms (default 150).
Definition TailShaper.h:99
void sync() noexcept
Takes the settings for the next block (audio thread).
Definition TailShaper.h:110
void setGateHold(T ms) noexcept
Time the gate stays open after the last crossing, 0 to 4000 ms (default 100).
Definition TailShaper.h:93
TailShaper() noexcept
Definition TailShaper.h:52
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...
Definition TailShaper.h:87
void prepare(double sampleRate) noexcept
Prepares for a sample rate (an invalid one is ignored) and clears.
Definition TailShaper.h:55
T getDryDip() const noexcept
Definition TailShaper.h:104
T next(T dryLevel) noexcept
One sample: the gain for the wet signal.
Definition TailShaper.h:128
void reset() noexcept
Clears the followers; the gate starts open and the dip released.
Definition TailShaper.h:70
T getGate() const noexcept
Definition TailShaper.h:105
void setDryDip(T dB) noexcept
Depth of the dip in dB, 0 to 48 (0, the default, is off).
Definition TailShaper.h:78
T getGateHold() const noexcept
Definition TailShaper.h:106
T getGateRelease() const noexcept
Definition TailShaper.h:107
Main namespace for the DSPark framework.