-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvelopeFollower.cpp
48 lines (40 loc) · 1.82 KB
/
EnvelopeFollower.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// EnvelopeFollower.cpp
#include "EnvelopeFollower.h"
EnvelopeFollower::EnvelopeFollower(float attack, float release, float smoothing, float sr)
: attackTime(attack), releaseTime(release), smoothingTime(smoothing), sampleRate(sr)
{
calculateGainFactors(); // calculate the attack and release gain factors
envelope = 0.0; // initialize the envelope to 0
smoothedEnvelope = 0.0; // initialize the smoothed envelope to 0
}
void EnvelopeFollower::setAttackTime(float attack) // set the attack time
{
attackTime = attack;
calculateGainFactors();
}
void EnvelopeFollower::setReleaseTime(float release) // set the release time
{
releaseTime = release;
calculateGainFactors();
}
void EnvelopeFollower::setSmoothingTime(float smoothing) // set the smoothing time
{
smoothingTime = smoothing;
calculateGainFactors();
}
void EnvelopeFollower::calculateGainFactors() // calculate the attack and release gain factors
{
attackGain = 1.0 - std::exp(-1.0 / (sampleRate * attackTime * 0.001));
releaseGain = 1.0 - std::exp(-1.0 / (sampleRate * releaseTime * 0.001));
smoothingGain = 1.0 - std::exp(-1.0 / (sampleRate * smoothingTime * 0.001));
}
float EnvelopeFollower::process(float input) // process the input sample
{
float absInput = std::fabs(input); // get the absolute value of the input
if (absInput > envelope) // if the absolute value of the input is greater than the envelope
envelope = attackGain * (absInput - envelope) + envelope; // attack phase
else
envelope = releaseGain * (absInput - envelope) + envelope; // release phase
smoothedEnvelope = smoothingGain * (envelope - smoothedEnvelope) + smoothedEnvelope; // smooth the envelope
return smoothedEnvelope; // return the smoothed envelope
}