]> git.sesse.net Git - nageru/blob - stereocompressor.cpp
Write 1.4.0 changelog.
[nageru] / stereocompressor.cpp
1 #include "stereocompressor.h"
2
3 #include <assert.h>
4 #include <algorithm>
5 #include <cmath>
6
7 using namespace std;
8
9 namespace {
10
11 // Implement a less accurate but faster pow(x, y). We use the standard identity
12 //
13 //    x^y = exp(y * ln(x))
14 //
15 // with the ranges:
16 //
17 //    x in 1..(1/threshold)
18 //    y in -1..0
19 //
20 // Assume threshold goes from 0 to -40 dB. That means 1/threshold = 100,
21 // so input to ln(x) can be 1..100. Worst case for end accuracy is y=-1.
22 // To get a good minimax approximation (not the least wrt. continuity
23 // at x=1), I had to make a piecewise linear function for the two ranges:
24 //
25 //   with(numapprox):
26 //   f1 := minimax(ln, 1..6, [3, 3], x -> 1/x, 'maxerror');
27 //   f2 := minimax(ln, 6..100, [3, 3], x -> 1/x, 'maxerror');
28 //   f := x -> piecewise(x < 6, f1(x), f2(x));
29 //
30 // (Continuity: Error is down to the 1e-6 range for x=1, difference between
31 // f1 and f2 range at the crossover point is in the 1e-5 range. The cutoff
32 // point at x=6 is chosen to get maxerror pretty close between f1 and f2.)
33 //
34 // Maximum output of ln(x) here is of course ln(100) ~= 4.605. So we can find
35 // an approximation for exp over the range -4.605..0, where we care mostly
36 // about the relative error:
37 //
38 //   g := minimax(exp, -ln(100)..0, [3, 3], x -> 1/exp(x), 'maxerror');
39 //
40 // We can find the worst-case error in dB from this through a simple plot:
41 //
42 //   dbdiff := (x, y) -> abs(20 * log10(x / y));
43 //   plot(dbdiff(g(-f(x)), 1/x), x=1..100);
44 //
45 // which readily shows the error never to be above ~0.001 dB or so
46 // (actually 0.00119 dB, for the case of x=100). y=-1 remains the worst case,
47 // it would seem.
48 //
49 // If we cared even more about speed, we could probably fuse y into
50 // the coefficients for ln_nom and postgain into the coefficients for ln_den.
51 // But if so, we should probably rather just SIMD the entire thing instead.
52 inline float fastpow(float x, float y)
53 {
54         float ln_nom, ln_den;
55         if (x < 6.0f) {
56                 ln_nom = -0.059237648f + (-0.0165117771f + (0.06818859075f + 0.007560968243f * x) * x) * x;
57                 ln_den = 0.0202509098f + (0.08419174188f + (0.03647189417f + 0.001642577975f * x) * x) * x;
58         } else {
59                 ln_nom = -0.005430534f + (0.00633589178f + (0.0006319155549f + 0.4789541675e-5f * x) * x) * x;
60                 ln_den = 0.0064785099f + (0.003219629109f + (0.0001531823694f + 0.6884656640e-6f * x) * x) * x;
61         }
62         float v = y * ln_nom / ln_den;
63         float exp_nom = 0.2195097621f + (0.08546059868f + (0.01208501759f + 0.0006173448113f * v) * v) * v;
64         float exp_den = 0.2194980791f + (-0.1343051968f + (0.03556072737f - 0.006174398513f * v) * v) * v;
65         return exp_nom / exp_den;
66 }
67
68 inline float compressor_knee(float x, float threshold, float inv_threshold, float inv_ratio_minus_one, float postgain)
69 {
70         assert(inv_ratio_minus_one <= 0.0f);
71         if (x > threshold) {
72                 return postgain * fastpow(x * inv_threshold, inv_ratio_minus_one);
73         } else {
74                 return postgain;
75         }
76 }
77
78 }  // namespace
79
80 void StereoCompressor::process(float *buf, size_t num_samples, float threshold, float ratio,
81             float attack_time, float release_time, float makeup_gain)
82 {
83         float attack_increment = float(pow(2.0f, 1.0f / (attack_time * sample_rate + 1)));
84         if (attack_time == 0.0f) attack_increment = 100000;  // For instant attack reaction.
85
86         const float release_increment = float(pow(2.0f, -1.0f / (release_time * sample_rate + 1)));
87         const float peak_increment = float(pow(2.0f, -1.0f / (0.003f * sample_rate + 1)));
88
89         float inv_ratio_minus_one = 1.0f / ratio - 1.0f;
90         if (ratio > 63) inv_ratio_minus_one = -1.0f;  // Infinite ratio.
91         float inv_threshold = 1.0f / threshold;
92
93         float *left_ptr = buf;
94         float *right_ptr = buf + 1;
95
96         if (inv_ratio_minus_one >= 0.0) {
97                 for (size_t i = 0; i < num_samples; ++i) {
98                         *left_ptr *= makeup_gain;
99                         left_ptr += 2;
100
101                         *right_ptr *= makeup_gain;
102                         right_ptr += 2;
103                 }
104                 return;
105         }
106
107         float peak_level = this->peak_level;
108         float compr_level = this->compr_level;
109
110         for (size_t i = 0; i < num_samples; ++i) {
111                 if (fabs(*left_ptr) > peak_level) peak_level = float(fabs(*left_ptr));
112                 if (fabs(*right_ptr) > peak_level) peak_level = float(fabs(*right_ptr));
113
114                 if (peak_level > compr_level) {
115                         compr_level = min(compr_level * attack_increment, peak_level);
116                 } else {
117                         compr_level = max(compr_level * release_increment, 0.0001f);
118                 }
119
120                 float scalefactor_with_gain = compressor_knee(compr_level, threshold, inv_threshold, inv_ratio_minus_one, makeup_gain);
121
122                 *left_ptr *= scalefactor_with_gain;
123                 left_ptr += 2;
124
125                 *right_ptr *= scalefactor_with_gain;
126                 right_ptr += 2;
127
128                 peak_level = max(peak_level * peak_increment, 0.0001f);
129         }
130
131         // Store attenuation level for debug/visualization.
132         scalefactor = compressor_knee(compr_level, threshold, inv_threshold, inv_ratio_minus_one, 1.0f);
133
134         this->peak_level = peak_level;
135         this->compr_level = compr_level;
136 }
137