-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLimiter.cpp
42 lines (36 loc) · 883 Bytes
/
Limiter.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
#include "Limiter.h"
EnvelopeFollower::EnvelopeFollower()
{
envelope = 1;
}
void EnvelopeFollower::Setup(double attackMs, double releaseMs, double sampleRate)
{
a = pow(0.01, 1.0 / (attackMs * sampleRate * 0.001));
r = pow(0.01, 1.0 / (releaseMs * sampleRate * 0.001));
}
void EnvelopeFollower::Process(const IQSample &src)
{
double v = sqrt(std::real(src * std::conj(src)));
if (v > envelope)
envelope = a * (envelope - v) + v;
else
envelope = r * (envelope - v) + v;
}
//----------
Limiter::Limiter(double attackMs, double releaseMs, double sampleRate)
{
e.Setup(attackMs, releaseMs, sampleRate);
}
void Limiter::Setup(double attackMs, double releaseMs, double sampleRate)
{
e.Setup(attackMs, releaseMs, sampleRate);
}
void Limiter::Process(IQSampleVector &dest)
{
for (auto &con : dest)
{
e.Process(con);
if (e.envelope > 1)
con = con / e.envelope;
}
}