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