Line data Source code
1 : #pragma once
2 :
3 : #include "common.h"
4 : #include <cmath>
5 :
6 : namespace Amplitron {
7 :
8 : /**
9 : * Attack/release envelope follower.
10 : * Used by Compressor, NoiseGate, WahPedal, and AmpSimulator.
11 : */
12 198 : struct EnvelopeFollower {
13 132 : float envelope = 0.0f;
14 :
15 : /**
16 : * Compute attack/release coefficients from time constants.
17 : * @param ms Time in milliseconds.
18 : * @param sr Sample rate.
19 : * @return One-pole coefficient (pass to process()).
20 : */
21 685 : static float time_to_coeff(float ms, int sr) {
22 685 : return std::exp(-1.0f / (sr * ms * 0.001f));
23 : }
24 :
25 : /**
26 : * Process one sample and return the new envelope value.
27 : * Coefficients are in "smoothing" form: higher = slower.
28 : */
29 24000 : float process(float input, float attack_coeff, float release_coeff) {
30 24000 : float abs_in = std::fabs(input);
31 24000 : float coeff = (abs_in > envelope) ? attack_coeff : release_coeff;
32 24000 : envelope = coeff * envelope + (1.0f - coeff) * abs_in;
33 24000 : return envelope;
34 : }
35 :
36 : /**
37 : * Alternative form matching the Compressor's convention:
38 : * coeff = 1 - exp(...), applied as: envelope += coeff * (abs - envelope)
39 : */
40 238080 : float process_additive(float input, float attack_coeff, float release_coeff) {
41 238080 : float abs_in = std::fabs(input);
42 193024 : float coeff = (abs_in > envelope) ? (1.0f - attack_coeff) : (1.0f - release_coeff);
43 238080 : envelope += coeff * (abs_in - envelope);
44 238080 : return envelope;
45 : }
46 :
47 249 : void reset() { envelope = 0.0f; }
48 : };
49 :
50 : } // namespace Amplitron
|