16template <
typename SampleType =
float>
21 SampleType
ratio = SampleType{10.0f};
30 params_.threshold_db =
31 std::isfinite(p.threshold_db) ? p.threshold_db : SampleType{-40.0f};
32 params_.ratio = std::isfinite(p.ratio)
33 ? std::max(SampleType{1.0f}, p.ratio)
36 std::isfinite(p.attack_ms)
37 ? std::max(SampleType{0.0f}, p.attack_ms)
40 std::isfinite(p.release_ms)
41 ? std::max(SampleType{0.0f}, p.release_ms)
44 std::isfinite(p.range_db)
45 ? std::min(SampleType{0.0f}, p.range_db)
51 sample_rate_ = (std::isfinite(sr) && sr > SampleType{0.0f})
53 : SampleType{44100.0f};
58 SampleType abs_in = std::abs(input);
59 if (abs_in < SampleType{1e-10f}) abs_in = SampleType{1e-10f};
60 SampleType input_db = SampleType{20.0f} * std::log10(abs_in);
62 SampleType gain_db = SampleType{0.0f};
63 if (input_db < params_.threshold_db) {
65 SampleType below = params_.threshold_db - input_db;
66 gain_db = -below * (params_.ratio - SampleType{1.0f});
67 gain_db = std::max(gain_db, params_.range_db);
73 SampleType coeff = (gain_db < envelope_db_) ? attack_coeff_ : release_coeff_;
74 envelope_db_ += coeff * (gain_db - envelope_db_);
77 envelope_db_ = std::max(envelope_db_, params_.range_db);
83 SampleType gain_linear =
84 std::pow(SampleType{10.0f}, envelope_db_ / SampleType{20.0f});
85 return input * gain_linear;
89 if (
buffer ==
nullptr || num_samples <= 0)
92 for (
int i = 0;
i < num_samples; ++
i)
101 void reset() { envelope_db_ = SampleType{0.0f}; }
107 void recompute_coeffs() {
108 attack_coeff_ = params_.
attack_ms > SampleType{0.0f}
110 std::exp(SampleType{-1.0f} /
111 (params_.attack_ms * SampleType{0.001f} * sample_rate_))
113 release_coeff_ = params_.release_ms > SampleType{0.0f}
115 std::exp(SampleType{-1.0f} /
116 (params_.release_ms * SampleType{0.001f} * sample_rate_))
121 SampleType sample_rate_ = SampleType{44100.0f};
122 SampleType envelope_db_ = SampleType{0.0f};
123 SampleType attack_coeff_ = SampleType{1.0f};
124 SampleType release_coeff_ = SampleType{1.0f};
Canonical gain-reduction meter value.
Definition dynamics_contract.hpp:27
static GainReduction from_signed_db(double signed_db) noexcept
Adapt a signed gain value, where attenuation is negative dB.
Definition dynamics_contract.hpp:32
Definition noise_gate.hpp:17
void process(SampleType *buffer, int num_samples)
Definition noise_gate.hpp:88
SampleType process(SampleType input)
Definition noise_gate.hpp:57
NoiseGateT()
Definition noise_gate.hpp:27
void reset()
Definition noise_gate.hpp:101
void set_sample_rate(SampleType sr)
Definition noise_gate.hpp:50
GainReduction gain_reduction() const noexcept
Canonical non-negative attenuation magnitude for shared meters.
Definition noise_gate.hpp:97
void set_params(const Params &p)
Definition noise_gate.hpp:29
Shared envelope and gain-reduction vocabulary for dynamics processors.
Definition additive_bank.hpp:218
@ buffer
A borrowed static buffer with a traversable playhead.
Definition granular.hpp:110
NoiseGateT< double > NoiseGate64
Definition noise_gate.hpp:128
NoiseGateT< float > NoiseGate
Definition noise_gate.hpp:127
T snap_to_zero(T x)
Returns 0 if |x| < snap_threshold, else x.
Definition denormal.hpp:45
@ i
Definition formant_filter_bank.hpp:20
Definition noise_gate.hpp:19
SampleType release_ms
Definition noise_gate.hpp:23
SampleType threshold_db
Definition noise_gate.hpp:20
SampleType range_db
Definition noise_gate.hpp:24
SampleType attack_ms
Definition noise_gate.hpp:22
SampleType ratio
Definition noise_gate.hpp:21