44#include "../Core/AudioBuffer.h"
45#include "../Core/AudioSpec.h"
46#include "../Core/DspMath.h"
47#include "../Core/Hilbert.h"
64template <FloatType T,
int MaxChannels = 16>
84 if (!spec.isValid() || !std::isfinite(spec.sampleRate))
return;
85 sampleRate_.store(spec.sampleRate, std::memory_order_relaxed);
86 numChannels_.store(std::clamp(spec.numChannels, 1, MaxChannels),
87 std::memory_order_relaxed);
101 for (
auto& s : state_) s = 0.0;
102 for (
auto& e : published_) e.store(T(0), std::memory_order_relaxed);
111 const double v =
static_cast<double>(ms);
112 if (!std::isfinite(v))
return;
113 attackMs_.store(std::max(v, 0.01), std::memory_order_relaxed);
121 const double v =
static_cast<double>(ms);
122 if (!std::isfinite(v))
return;
123 releaseMs_.store(std::max(v, 0.01), std::memory_order_relaxed);
130 const int v = std::clamp(
static_cast<int>(m), 0, 1);
131 mode_.store(
static_cast<Mode>(v), std::memory_order_relaxed);
143 const int nCh = std::min(buffer.getNumChannels(),
144 numChannels_.load(std::memory_order_relaxed));
145 const int nS = buffer.getNumSamples();
146 const double aAtt = attackA_.load(std::memory_order_relaxed);
147 const double aRel = releaseA_.load(std::memory_order_relaxed);
148 const bool peak = mode_.load(std::memory_order_relaxed) ==
Mode::Peak;
150 for (
int ch = 0; ch < nCh; ++ch)
152 const T* d = buffer.getChannel(ch);
153 double env = state_[
static_cast<size_t>(ch)];
156 for (
int i = 0; i < nS; ++i)
158 const double x = std::abs(
static_cast<double>(d[i]));
159 const double a = (x > env) ? aAtt : aRel;
160 env = a * env + (1.0 - a) * x;
165 for (
int i = 0; i < nS; ++i)
167 const double x =
static_cast<double>(d[i]) * d[i];
168 const double a = (x > env) ? aAtt : aRel;
169 env = a * env + (1.0 - a) * x;
177 if (!std::isfinite(env) || env < 1e-100) env = 0.0;
178 state_[
static_cast<size_t>(ch)] = env;
179 published_[
static_cast<size_t>(ch)].store(readout(env, peak),
180 std::memory_order_relaxed);
194 const double aAtt = attackA_.load(std::memory_order_relaxed);
195 const double aRel = releaseA_.load(std::memory_order_relaxed);
196 const bool peak = mode_.load(std::memory_order_relaxed) ==
Mode::Peak;
197 double env = state_[0];
198 const double x = peak ? std::abs(
static_cast<double>(input))
199 :
static_cast<double>(input) * input;
200 const double a = (x > env) ? aAtt : aRel;
201 env = a * env + (1.0 - a) * x;
202 if (!std::isfinite(env) || env < 1e-100) env = 0.0;
204 const T out = readout(env, peak);
205 published_[0].store(out, std::memory_order_relaxed);
214 const int n = numChannels_.load(std::memory_order_relaxed);
215 channel = std::clamp(channel, 0, n - 1);
216 return published_[
static_cast<size_t>(channel)].load(std::memory_order_relaxed);
222 const int n = numChannels_.load(std::memory_order_relaxed);
224 for (
int ch = 0; ch < n; ++ch)
225 m = std::max(m, published_[
static_cast<size_t>(ch)].load(std::memory_order_relaxed));
232 const double e =
static_cast<double>(
getEnvelope(channel));
233 return static_cast<T
>(20.0 * std::log10(std::max(e, 1e-6)));
245 const double floor = std::isfinite(floorDb) ? std::clamp(
static_cast<double>(floorDb), -120.0, -1.0) : -60.0;
246 const double db =
static_cast<double>(
getEnvelopeDb(channel));
247 return static_cast<T
>(std::clamp((db - floor) / -floor, 0.0, 1.0));
253 return static_cast<T
>(attackMs_.load(std::memory_order_relaxed));
259 return static_cast<T
>(releaseMs_.load(std::memory_order_relaxed));
265 return mode_.load(std::memory_order_relaxed);
269 static_assert(MaxChannels >= 1,
"EnvelopeFollower needs at least one channel");
271 [[nodiscard]] T readout(
double env,
bool peak)
const noexcept
273 return static_cast<T
>(peak ? env : std::sqrt(std::max(env, 0.0)));
276 void updateCoeffs() noexcept
278 const double fs = sampleRate_.load(std::memory_order_relaxed);
279 attackA_.store(std::exp(-1.0 / (attackMs_.load(std::memory_order_relaxed) * 0.001 * fs)),
280 std::memory_order_relaxed);
281 releaseA_.store(std::exp(-1.0 / (releaseMs_.load(std::memory_order_relaxed) * 0.001 * fs)),
282 std::memory_order_relaxed);
285 std::atomic<double> sampleRate_ { 48000.0 };
286 std::atomic<int> numChannels_ { 1 };
287 std::atomic<double> attackMs_ { 10.0 };
288 std::atomic<double> releaseMs_ { 150.0 };
289 std::atomic<double> attackA_ { 0.99 };
290 std::atomic<double> releaseA_ { 0.999 };
293 std::array<double, MaxChannels> state_ {};
294 std::array<std::atomic<T>, MaxChannels> published_ {};
319template <FloatType T>
328 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0))
return;
330 const auto coeff = [
this](
double ms) {
return std::exp(-1.0 / (ms * 0.001 * fs_)); };
331 fastAttack_ = coeff(0.5);
332 slowAttack_ = coeff(30.0);
333 followRelease_ = coeff(100.0);
344 for (
auto& h : analytic_) h.reset();
345 value_.store(T(0), std::memory_order_relaxed);
351 if (std::isfinite(dB)) range_.store(std::clamp(dB, T(3), T(40)), std::memory_order_relaxed);
356 if (std::isfinite(ms)) holdMs_.store(std::clamp(ms, T(0), T(500)), std::memory_order_relaxed);
361 if (std::isfinite(ms)) releaseMs_.store(std::clamp(ms, T(1), T(2000)), std::memory_order_relaxed);
364 [[nodiscard]] T
getRange() const noexcept {
return range_.load(std::memory_order_relaxed); }
365 [[nodiscard]] T
getHold() const noexcept {
return holdMs_.load(std::memory_order_relaxed); }
366 [[nodiscard]] T
getRelease() const noexcept {
return releaseMs_.load(std::memory_order_relaxed); }
369 [[nodiscard]] T
getValue() const noexcept {
return value_.load(std::memory_order_relaxed); }
374 const Timing t = timing();
375 const double x =
static_cast<double>(input);
376 const T v = step(analytic_[0].magnitude(std::isfinite(x) ? x : 0.0), t);
377 value_.store(v, std::memory_order_relaxed);
389 const Timing t = timing();
391 T v = value_.load(std::memory_order_relaxed);
392 for (
int i = 0; i < nS; ++i)
395 for (
int ch = 0; ch < nCh; ++ch)
397 const double x =
static_cast<double>(buffer.
getChannel(ch)[i]);
398 top = std::max(top, analytic_[
static_cast<size_t>(ch)].magnitude(std::isfinite(x) ? x : 0.0));
403 value_.store(v, std::memory_order_relaxed);
414 [[nodiscard]] Timing timing() const noexcept
416 return {
static_cast<double>(range_.load(std::memory_order_relaxed)),
417 static_cast<int>(
static_cast<double>(holdMs_.load(std::memory_order_relaxed)) * 0.001 * fs_),
418 std::exp(-1.0 / (
static_cast<double>(releaseMs_.load(std::memory_order_relaxed)) * 0.001 * fs_)) };
421 [[nodiscard]] T step(
double x,
const Timing& t)
noexcept
423 const double level = std::isfinite(x) ? std::abs(x) : 0.0;
424 fast_ = level + ((level > fast_) ? fastAttack_ : followRelease_) * (fast_ - level);
425 slow_ = level + ((level > slow_) ? slowAttack_ : followRelease_) * (slow_ - level);
427 if (slow_ > 1e-6 && fast_ > 1e-6)
428 raw = std::clamp((20.0 * std::log10(fast_ / slow_) - kDeadZoneDb) / t.range, 0.0, 1.0);
429 else if (fast_ > 1e-6)
431 if (fast_ < 1e-12) fast_ = 0.0;
432 if (slow_ < 1e-12) slow_ = 0.0;
438 else if (holdLeft_ > 0)
444 out_ = raw + t.release * (out_ - raw);
446 return static_cast<T
>(out_);
449 static constexpr double kDeadZoneDb = 0.5;
450 static constexpr int kChannels = 16;
452 std::array<HilbertIIR<double>, kChannels> analytic_ {};
454 std::atomic<T> range_ { T(12) };
455 std::atomic<T> holdMs_ { T(20) };
456 std::atomic<T> releaseMs_ { T(80) };
457 std::atomic<T> value_ { T(0) };
459 double fs_ = 48000.0;
460 double fastAttack_ = 0.0, slowAttack_ = 0.0, followRelease_ = 0.0;
461 double fast_ = 0.0, slow_ = 0.0, out_ = 0.0;
Non-owning view over audio channel data.
int getNumSamples() const noexcept
Returns the number of samples per channel.
int getNumChannels() const noexcept
Returns the number of channels in this view.
T * getChannel(int ch) const noexcept
Returns a pointer to the sample data for the given channel.
Attack/release envelope detector (Peak or RMS).
T processSample(T input) noexcept
Single-sample path on channel 0 (for embedding in processors).
void setAttack(T ms) noexcept
Attack time in milliseconds (default 10, floor 0.01; non-finite values are ignored).
void setRelease(T ms) noexcept
Release time in milliseconds (default 150, floor 0.01; non-finite values are ignored).
T getAttack() const noexcept
EnvelopeFollower() noexcept
T getEnvelopeDb(int channel=0) const noexcept
T getRelease() const noexcept
void prepare(const AudioSpec &spec) noexcept
Prepares the follower. Allocation-free.
Mode getMode() const noexcept
T getEnvelopeNormalized(int channel=0, T floorDb=T(-60)) const noexcept
T getEnvelope(int channel=0) const noexcept
void processBlock(AudioBufferView< const T > buffer) noexcept
Analyzes a block (read-only) and updates per-channel envelopes.
T getEnvelopeMax() const noexcept
void setMode(Mode m) noexcept
Peak (default) or RMS detection. Out-of-range values clamp.
void reset() noexcept
Clears all envelopes.
How sudden the signal is, 0..1, whatever its level.
T getRelease() const noexcept
void prepare(double sampleRate) noexcept
Prepares for a sample rate (an invalid one is ignored) and clears.
void setRelease(T ms) noexcept
Time constant of the fall after the hold, 1 to 2000 ms (default 80).
T getHold() const noexcept
TransientFollower() noexcept
void setHold(T ms) noexcept
Time the value is held at a peak, 0 to 500 ms (default 20).
void setRange(T dB) noexcept
Range in dB that maps to the full value, 3 to 40 (default 12).
void processBlock(AudioBufferView< T > buffer, T *out=nullptr) noexcept
Follows a block (the loudest channel per frame); the audio is not touched.
T getRange() const noexcept
T getValue() const noexcept
The value after the last sample (any thread).
void reset() noexcept
Clears the followers and the output.
T processSample(T input) noexcept
One sample (already the loudest channel's, or mono); returns the value.
Main namespace for the DSPark framework.
Describes the audio environment for a DSP processor.