1 // Copyright 2014 Olivier Gillet.
2 //
3 // Author: Olivier Gillet (ol.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Compressor.
28 
29 #include "streams/compressor.h"
30 
31 // #include <cmath>
32 
33 #include "stmlib/utils/dsp.h"
34 
35 namespace streams {
36 
37 using namespace stmlib;
38 
39 // 256 LSB <=> 1.55dB
40 const int32_t kGainConstant = 1 / (1.55 / 6.0 * 65536.0 / 256.0) * 65536;
41 
Init()42 void Compressor::Init() {
43   detector_ = 0;
44 }
45 
46 /* static */
Log2(int32_t value)47 int32_t Compressor::Log2(int32_t value) {
48   if (value <= 0) {
49     value = 1;
50   }
51   int32_t log_value = 0;
52   while (value >= 512) {
53     value >>= 1;
54     log_value += 65536;
55   }
56   while (value < 256) {
57     value <<= 1;
58     log_value -= 65536;
59   }
60   // Value is between 256 and 512, we can use the LUT.
61   return log_value + lut_log2[value - 256];
62 }
63 
64 /* static */
Exp2(int32_t value)65 int32_t Compressor::Exp2(int32_t value) {
66   int32_t num_shifts = 0;
67   while (value >= 65536) {
68     ++num_shifts;
69     value -= 65536;
70   }
71   while (value < 0) {
72     --num_shifts;
73     value += 65536;
74   }
75 
76   // Value is between 0 and 65535, we can use the LUT.
77   int32_t a = lut_exp2[value >> 8];
78   int32_t b = lut_exp2[(value >> 8) + 1];
79   int32_t mantissa = a + ((b - a) * (value & 0xff) >> 8);
80   return num_shifts >= 0 ? mantissa << num_shifts : mantissa >> -num_shifts;
81 }
82 
83 /* static */
Compress(int32_t squared_level,int32_t threshold,int32_t ratio,bool soft_knee)84 int32_t Compressor::Compress(
85     int32_t squared_level,
86     int32_t threshold,
87     int32_t ratio,
88     bool soft_knee) {
89   int32_t level = (Log2(squared_level) >> 1) - 15 * 65536;  // 15-bit peak
90   int32_t position = level - threshold;
91 
92   if (position < 0) {
93     return 0;
94   }
95 
96   int32_t attenuation = position - (position * ratio >> 8);
97   if (attenuation < 65535 && soft_knee) {
98     int32_t a = lut_soft_knee[attenuation >> 8];
99     int32_t b = lut_soft_knee[(attenuation >> 8) + 1];
100     int32_t soft_knee = a + ((b - a) * (attenuation & 0xff) >> 8);
101     attenuation += \
102         (soft_knee - attenuation) * ((65535 - attenuation) >> 1) >> 15;
103   }
104   return -attenuation;
105 }
106 
Process(int16_t audio,int16_t excite,uint16_t * gain,uint16_t * frequency)107 void Compressor::Process(
108     int16_t audio,
109     int16_t excite,
110     uint16_t* gain,
111     uint16_t* frequency) {
112   int32_t energy;
113   int64_t error;
114 
115   // Detect the RMS level on the EXCITE input.
116   energy = excite;
117   energy *= energy;
118   error = energy - sidechain_signal_detector_;
119   if (error > 0) {
120     sidechain_signal_detector_ += error;
121   } else {
122     // Decay time: 5s.
123     sidechain_signal_detector_ += error * 14174 >> 31;
124   }
125 
126   // If there is no signal on the "excite" input, disable sidechain and
127   // compress by metering input.
128   if (sidechain_signal_detector_ < (1024 * 1024)) {
129     energy = audio;
130     energy *= energy;
131   }
132 
133   // Detect the RMS level on the EXCITE or AUDIO input - whichever active.
134   error = energy - detector_;
135   if (error > 0) {
136     if (attack_coefficient_ == -1) {
137       detector_ += error;
138     } else {
139       detector_ += error * attack_coefficient_ >> 31;
140     }
141   } else {
142     detector_ += error * decay_coefficient_ >> 31;
143   }
144 
145   int32_t g = Compress(detector_, threshold_, ratio_, soft_knee_);
146   gain_reduction_ = g >> 3;
147   g = kUnityGain + ((g + makeup_gain_) * kGainConstant >> 16);
148   if (g > 65535) {
149     g = 65535;
150   }
151 
152   *gain = g;
153   // float ogain = powf(10.0f, 1.55f / 20.0f * (g - kUnityGain) / 256.0f);
154   // printf("%f %f\n", gain_reduction_ / 32768.0 * 24, 20 * logf(ogain) / logf(10.0f));
155   *frequency = 65535;
156 }
157 
158 }  // namespace streams
159