forked from dukesrg/logue-fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkompressor.cpp
99 lines (86 loc) · 2.96 KB
/
kompressor.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* File: kompressor.cpp
*
* Peak downward compressor/expander for Modulation FX, Delay FX and Reverb FX
*
* 2020 (c) Oleg Burdaev
* mailto: [email protected]
*
*/
#include <float.h>
#include "fxwrapper.h"
#define MIN_THRESHOLD 80 //dB
static float s_threshold = .5f;
static float s_gain = 1.f;
#ifndef FX_MODFX
static float s_wet = .5f;
#endif
FX_INIT
{
}
FX_PROCESS
{
#ifdef FX_MODFX
f32pair_t * __restrict y = (f32pair_t*)yn;
#endif
#ifdef FX_MODFX_SUB
f32pair_t * __restrict y_sub = (f32pair_t*)sub_yn;
for (f32pair_t * __restrict x = (f32pair_t*)xn, * __restrict x_sub = (f32pair_t*)sub_xn; frames--; x++, x_sub++) {
#else
for (f32pair_t * __restrict x = (f32pair_t*)xn; frames--; x++) {
#endif
f32pair_t valp = *x;
if ((s_gain > 1.f && valp.a > 0.f && valp.a < s_threshold) || (s_gain < 1.f && valp.a > s_threshold))
valp.a = clipminf(0.f, (valp.a - s_threshold) * s_gain + s_threshold);
else if ((s_gain > 1.f && valp.a < 0.f && valp.a > -s_threshold) || (s_gain < 1.f && valp.a < -s_threshold))
valp.a = clipmaxf(0.f, (valp.a + s_threshold) * s_gain - s_threshold);
if ((s_gain > 1.f && valp.b > 0.f && valp.b < s_threshold) || (s_gain < 1.f && valp.b > s_threshold))
valp.b = clipminf(0.f, (valp.b - s_threshold) * s_gain + s_threshold);
else if ((s_gain > 1.f && valp.b < 0.f && valp.b > -s_threshold) || (s_gain < 1.f && valp.b < -s_threshold))
valp.b = clipmaxf(0.f, (valp.b + s_threshold) * s_gain - s_threshold);
#ifdef FX_MODFX
*(y++) = valp;
#else
*x = f32pair_linint(s_wet, *x, valp);
#endif
#ifdef FX_MODFX_SUB
valp = *x_sub;
if ((s_gain > 1.f && valp.a > 0.f && valp.a < s_threshold) || (s_gain < 1.f && valp.a > s_threshold))
valp.a = clipminf(0.f, (valp.a - s_threshold) * s_gain + s_threshold);
else if ((s_gain > 1.f && valp.a < 0.f && valp.a > -s_threshold) || (s_gain < 1.f && valp.a < -s_threshold))
valp.a = clipmaxf(0.f, (valp.a + s_threshold) * s_gain - s_threshold);
if ((s_gain > 1.f && valp.b > 0.f && valp.b < s_threshold) || (s_gain < 1.f && valp.b > s_threshold))
valp.b = clipminf(0.f, (valp.b - s_threshold) * s_gain + s_threshold);
else if ((s_gain > 1.f && valp.b < 0.f && valp.b > -s_threshold) || (s_gain < 1.f && valp.b < -s_threshold))
valp.b = clipmaxf(0.f, (valp.b + s_threshold) * s_gain - s_threshold);
*(y_sub++) = valp;
#endif
}
}
FX_PARAM
{
float valf = q31_to_f32(value);
switch (index) {
case FX_PARAM_TIME: //threshold
s_threshold = fasterdbampf((valf - 1.f) * MIN_THRESHOLD);
break;
case FX_PARAM_DEPTH: //gain
s_gain = valf * 2.f;
if (s_gain == 0.f) //noise gate
s_gain = FLT_MAX;
else if (s_gain == 1.f) // dry
s_gain = 1.f;
else if (s_gain < 1.f) //expand - gain inf...1
s_gain = 1.f / s_gain;
else if (s_gain > 1.f) //compress - gain 1...0
s_gain = 2.f - s_gain;
break;
#ifndef FX_MODFX
case FX_PARAM_SHIFT_DEPTH:
s_wet = valf;
break;
#endif
default:
break;
}
}