Line data Source code
1 : #include "audio/effects/dynamics/noise_gate.h"
2 :
3 : #include "audio/effects/core/effect_factory.h"
4 :
5 : namespace Amplitron {
6 :
7 2 : static EffectRegistrar<NoiseGate> reg("Noise Gate");
8 :
9 120 : NoiseGate::NoiseGate() {
10 270 : params_ = {
11 90 : {"Threshold", -55.0f, -80.0f, 0.0f, -55.0f, "dB",
12 30 : "Signal level below which the gate closes and mutes the audio. Set just above background "
13 : "noise level."},
14 30 : {"Attack", 0.5f, 0.1f, 10.0f, 0.5f, "ms",
15 30 : "How quickly the gate opens when the signal exceeds the threshold. Fast attack preserves "
16 : "pick transients."},
17 30 : {"Release", 50.0f, 5.0f, 500.0f, 50.0f, "ms",
18 30 : "How quickly the gate closes after the signal falls below threshold. Longer release "
19 : "preserves sustained notes."},
20 270 : };
21 180 : }
22 :
23 87 : void NoiseGate::process(float* buffer, int num_samples) {
24 87 : if (!enabled_) return;
25 :
26 87 : float threshold = db_to_linear(params_[0].value);
27 87 : float attack_coeff = EnvelopeFollower::time_to_coeff(params_[1].value, sample_rate_);
28 87 : float release_coeff = EnvelopeFollower::time_to_coeff(params_[2].value, sample_rate_);
29 :
30 21015 : for (int i = 0; i < num_samples; ++i) {
31 20928 : float envelope = env_.process(buffer[i], attack_coeff, release_coeff);
32 :
33 20928 : float target_gain = (envelope > threshold) ? 1.0f : 0.0f;
34 20928 : float gain_coeff = (target_gain > gain_) ? attack_coeff : release_coeff;
35 20928 : gain_ += (target_gain - gain_) * (1.0f - gain_coeff);
36 20928 : buffer[i] *= gain_;
37 6976 : }
38 29 : }
39 :
40 78 : void NoiseGate::reset() {
41 78 : env_.reset();
42 78 : gain_ = 0.0f;
43 78 : }
44 :
45 : } // namespace Amplitron
|