1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 //#define LOG_NDEBUG 0
17
18 #include <cmath>
19
20 #include "common/core/math.h"
21 #include "common/core/types.h"
22 #include "dsp/core/basic.h"
23 #include "dsp/core/interpolation.h"
24 #include "dsp/core/dynamic_range_compression.h"
25
26 #include <android/log.h>
27
28 namespace le_fx {
29
30 // Definitions for static const class members declared in
31 // dynamic_range_compression.h.
32 const float AdaptiveDynamicRangeCompression::kMinAbsValue = 0.000001f;
33 const float AdaptiveDynamicRangeCompression::kMinLogAbsValue =
34 0.032766999999999997517097227728299912996590137481689453125f;
35 const float AdaptiveDynamicRangeCompression::kFixedPointLimit = 32767.0f;
36 const float AdaptiveDynamicRangeCompression::kInverseFixedPointLimit =
37 1.0f / AdaptiveDynamicRangeCompression::kFixedPointLimit;
38 const float AdaptiveDynamicRangeCompression::kDefaultKneeThresholdInDecibel =
39 -8.0f;
40 const float AdaptiveDynamicRangeCompression::kCompressionRatio = 7.0f;
41 const float AdaptiveDynamicRangeCompression::kTauAttack = 0.001f;
42 const float AdaptiveDynamicRangeCompression::kTauRelease = 0.015f;
43
AdaptiveDynamicRangeCompression()44 AdaptiveDynamicRangeCompression::AdaptiveDynamicRangeCompression() {
45 static const float kTargetGain[] = {
46 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
47 static const float kKneeThreshold[] = {
48 -8.0f, -8.0f, -8.5f, -9.0f, -10.0f };
49 target_gain_to_knee_threshold_.Initialize(
50 &kTargetGain[0], &kKneeThreshold[0],
51 sizeof(kTargetGain) / sizeof(kTargetGain[0]));
52 }
53
Initialize(float target_gain,float sampling_rate)54 bool AdaptiveDynamicRangeCompression::Initialize(
55 float target_gain, float sampling_rate) {
56 set_knee_threshold_via_target_gain(target_gain);
57 sampling_rate_ = sampling_rate;
58 state_ = 0.0f;
59 compressor_gain_ = 1.0f;
60 if (kTauAttack > 0.0f) {
61 const float taufs = kTauAttack * sampling_rate_;
62 alpha_attack_ = std::exp(-1.0f / taufs);
63 } else {
64 alpha_attack_ = 0.0f;
65 }
66 if (kTauRelease > 0.0f) {
67 const float taufs = kTauRelease * sampling_rate_;
68 alpha_release_ = std::exp(-1.0f / taufs);
69 } else {
70 alpha_release_ = 0.0f;
71 }
72 // Feed-forward topology
73 slope_ = 1.0f / kCompressionRatio - 1.0f;
74 return true;
75 }
76
Compress(float x)77 float AdaptiveDynamicRangeCompression::Compress(float x) {
78 const float max_abs_x = std::max(std::fabs(x), kMinLogAbsValue);
79 const float max_abs_x_dB = math::fast_log(max_abs_x);
80 // Subtract Threshold from log-encoded input to get the amount of overshoot
81 const float overshoot = max_abs_x_dB - knee_threshold_;
82 // Hard half-wave rectifier
83 const float rect = std::max(overshoot, 0.0f);
84 // Multiply rectified overshoot with slope
85 const float cv = rect * slope_;
86 const float prev_state = state_;
87 if (cv <= state_) {
88 state_ = alpha_attack_ * state_ + (1.0f - alpha_attack_) * cv;
89 } else {
90 state_ = alpha_release_ * state_ + (1.0f - alpha_release_) * cv;
91 }
92 compressor_gain_ *= expf(state_ - prev_state);
93 x *= compressor_gain_;
94 if (x > kFixedPointLimit) {
95 return kFixedPointLimit;
96 }
97 if (x < -kFixedPointLimit) {
98 return -kFixedPointLimit;
99 }
100 return x;
101 }
102
Compress(float * x1,float * x2)103 void AdaptiveDynamicRangeCompression::Compress(float *x1, float *x2) {
104 // Taking the maximum amplitude of both channels
105 const float max_abs_x = std::max(std::fabs(*x1),
106 std::max(std::fabs(*x2), kMinLogAbsValue));
107 const float max_abs_x_dB = math::fast_log(max_abs_x);
108 // Subtract Threshold from log-encoded input to get the amount of overshoot
109 const float overshoot = max_abs_x_dB - knee_threshold_;
110 // Hard half-wave rectifier
111 const float rect = std::max(overshoot, 0.0f);
112 // Multiply rectified overshoot with slope
113 const float cv = rect * slope_;
114 const float prev_state = state_;
115 if (cv <= state_) {
116 state_ = alpha_attack_ * state_ + (1.0f - alpha_attack_) * cv;
117 } else {
118 state_ = alpha_release_ * state_ + (1.0f - alpha_release_) * cv;
119 }
120 compressor_gain_ *= expf(state_ - prev_state);
121 *x1 *= compressor_gain_;
122 if (*x1 > kFixedPointLimit) {
123 *x1 = kFixedPointLimit;
124 }
125 if (*x1 < -kFixedPointLimit) {
126 *x1 = -kFixedPointLimit;
127 }
128 *x2 *= compressor_gain_;
129 if (*x2 > kFixedPointLimit) {
130 *x2 = kFixedPointLimit;
131 }
132 if (*x2 < -kFixedPointLimit) {
133 *x2 = -kFixedPointLimit;
134 }
135 }
136
137 } // namespace le_fx
138
139