forked from electro-smith/DaisySP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter.cpp
34 lines (28 loc) · 812 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
#include "limiter.h"
#include <math.h>
#define SLOPE(out, in, positive, negative) \
{ \
float error = (in)-out; \
out += (error > 0 ? positive : negative) * error; \
}
using namespace daisysp;
static float softlimit(float x);
void Limiter::Init()
{
peak_ = 0.5f;
}
void Limiter::ProcessBlock(float *in, size_t size, float pre_gain)
{
while(size--)
{
float pre = *in * pre_gain;
float peak = fabsf(pre);
SLOPE(peak_, peak, 0.05f, 0.00002f);
float gain = (peak_ <= 1.0f ? 1.0f : 1.0f / peak_);
*in++ = softlimit(pre * gain * 0.7f);
}
}
static float softlimit(float x)
{
return x * (27.0f + x * x) / (27.0f + 9.0f * x * x);
}