-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcompressor.ino
72 lines (60 loc) · 1.74 KB
/
compressor.ino
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// # compressor
//
// Author: shensley, AvAars
//
#include "compressor.h"
#ifndef max
#define max(a, b) ((a < b) ? b : a)
#endif
#ifndef min
#define min(a, b) ((a < b) ? a : b)
#endif
void Compressor::Init(float sample_rate)
{
sample_rate_ = min(192000, max(1, sample_rate));
sample_rate_inv_ = 1.0f / (float)sample_rate_;
sample_rate_inv2_ = 2.0f / (float)sample_rate_;
// Initializing the params in this order to avoid dividing by zero
SetRatio(12.0f);
SetAttack(0.05f);
SetRelease(0.3f);
SetThreshold(-20.0f);
AutoMakeup(true);
gain_rec_ = 0.1f;
slope_rec_ = 0.1f;
}
float Compressor::Process(float in)
{
float inAbs = fabsf(in);
float cur_slo = ((slope_rec_ > inAbs) ? rel_slo_ : atk_slo_);
slope_rec_ = ((slope_rec_ * cur_slo) + ((1.0f - cur_slo) * inAbs));
gain_rec_ = ((atk_slo2_ * gain_rec_)
+ (ratio_mul_
* fmax(((20.f * fastlog10f(slope_rec_)) - thresh_), 0.0f)));
gain_ = pow10f(0.05f * (gain_rec_ + makeup_gain_));
return gain_ * in;
}
void Compressor::ProcessBlock(float *in, float *out, float *key, size_t size)
{
for(size_t i = 0; i < size; i++)
{
Process(key[i]);
out[i] = Apply(in[i]);
}
}
// Multi-channel block processing
void Compressor::ProcessBlock(float **in,
float **out,
float * key,
size_t channels,
size_t size)
{
for(size_t i = 0; i < size; i++)
{
Process(key[i]);
for(size_t c = 0; c < channels; c++)
{
out[c][i] = Apply(in[c][i]);
}
}
}