1 /*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/audio_processing/agc2/limiter_db_gain_curve.h"
12
13 #include <cmath>
14
15 #include "common_audio/include/audio_util.h"
16 #include "modules/audio_processing/agc2/agc2_common.h"
17 #include "rtc_base/checks.h"
18
19 namespace webrtc {
20 namespace {
21
ComputeKneeStart(double max_input_level_db,double knee_smoothness_db,double compression_ratio)22 double ComputeKneeStart(double max_input_level_db,
23 double knee_smoothness_db,
24 double compression_ratio) {
25 RTC_CHECK_LT((compression_ratio - 1.0) * knee_smoothness_db /
26 (2.0 * compression_ratio),
27 max_input_level_db);
28 return -knee_smoothness_db / 2.0 -
29 max_input_level_db / (compression_ratio - 1.0);
30 }
31
ComputeKneeRegionPolynomial(double knee_start_dbfs,double knee_smoothness_db,double compression_ratio)32 std::array<double, 3> ComputeKneeRegionPolynomial(double knee_start_dbfs,
33 double knee_smoothness_db,
34 double compression_ratio) {
35 const double a = (1.0 - compression_ratio) /
36 (2.0 * knee_smoothness_db * compression_ratio);
37 const double b = 1.0 - 2.0 * a * knee_start_dbfs;
38 const double c = a * knee_start_dbfs * knee_start_dbfs;
39 return {{a, b, c}};
40 }
41
ComputeLimiterD1(double max_input_level_db,double compression_ratio)42 double ComputeLimiterD1(double max_input_level_db, double compression_ratio) {
43 return (std::pow(10.0, -max_input_level_db / (20.0 * compression_ratio)) *
44 (1.0 - compression_ratio) / compression_ratio) /
45 kMaxAbsFloatS16Value;
46 }
47
ComputeLimiterD2(double compression_ratio)48 constexpr double ComputeLimiterD2(double compression_ratio) {
49 return (1.0 - 2.0 * compression_ratio) / compression_ratio;
50 }
51
ComputeLimiterI2(double max_input_level_db,double compression_ratio,double gain_curve_limiter_i1)52 double ComputeLimiterI2(double max_input_level_db,
53 double compression_ratio,
54 double gain_curve_limiter_i1) {
55 RTC_CHECK_NE(gain_curve_limiter_i1, 0.f);
56 return std::pow(10.0, -max_input_level_db / (20.0 * compression_ratio)) /
57 gain_curve_limiter_i1 /
58 std::pow(kMaxAbsFloatS16Value, gain_curve_limiter_i1 - 1);
59 }
60
61 } // namespace
62
LimiterDbGainCurve()63 LimiterDbGainCurve::LimiterDbGainCurve()
64 : max_input_level_linear_(DbfsToFloatS16(max_input_level_db_)),
65 knee_start_dbfs_(ComputeKneeStart(max_input_level_db_,
66 knee_smoothness_db_,
67 compression_ratio_)),
68 knee_start_linear_(DbfsToFloatS16(knee_start_dbfs_)),
69 limiter_start_dbfs_(knee_start_dbfs_ + knee_smoothness_db_),
70 limiter_start_linear_(DbfsToFloatS16(limiter_start_dbfs_)),
71 knee_region_polynomial_(ComputeKneeRegionPolynomial(knee_start_dbfs_,
72 knee_smoothness_db_,
73 compression_ratio_)),
74 gain_curve_limiter_d1_(
75 ComputeLimiterD1(max_input_level_db_, compression_ratio_)),
76 gain_curve_limiter_d2_(ComputeLimiterD2(compression_ratio_)),
77 gain_curve_limiter_i1_(1.0 / compression_ratio_),
78 gain_curve_limiter_i2_(ComputeLimiterI2(max_input_level_db_,
79 compression_ratio_,
80 gain_curve_limiter_i1_)) {
81 static_assert(knee_smoothness_db_ > 0.0f, "");
82 static_assert(compression_ratio_ > 1.0f, "");
83 RTC_CHECK_GE(max_input_level_db_, knee_start_dbfs_ + knee_smoothness_db_);
84 }
85
86 constexpr double LimiterDbGainCurve::max_input_level_db_;
87 constexpr double LimiterDbGainCurve::knee_smoothness_db_;
88 constexpr double LimiterDbGainCurve::compression_ratio_;
89
GetOutputLevelDbfs(double input_level_dbfs) const90 double LimiterDbGainCurve::GetOutputLevelDbfs(double input_level_dbfs) const {
91 if (input_level_dbfs < knee_start_dbfs_) {
92 return input_level_dbfs;
93 } else if (input_level_dbfs < limiter_start_dbfs_) {
94 return GetKneeRegionOutputLevelDbfs(input_level_dbfs);
95 }
96 return GetCompressorRegionOutputLevelDbfs(input_level_dbfs);
97 }
98
GetGainLinear(double input_level_linear) const99 double LimiterDbGainCurve::GetGainLinear(double input_level_linear) const {
100 if (input_level_linear < knee_start_linear_) {
101 return 1.0;
102 }
103 return DbfsToFloatS16(
104 GetOutputLevelDbfs(FloatS16ToDbfs(input_level_linear))) /
105 input_level_linear;
106 }
107
108 // Computes the first derivative of GetGainLinear() in |x|.
GetGainFirstDerivativeLinear(double x) const109 double LimiterDbGainCurve::GetGainFirstDerivativeLinear(double x) const {
110 // Beyond-knee region only.
111 RTC_CHECK_GE(x, limiter_start_linear_ - 1e-7 * kMaxAbsFloatS16Value);
112 return gain_curve_limiter_d1_ *
113 std::pow(x / kMaxAbsFloatS16Value, gain_curve_limiter_d2_);
114 }
115
116 // Computes the integral of GetGainLinear() in the range [x0, x1].
GetGainIntegralLinear(double x0,double x1) const117 double LimiterDbGainCurve::GetGainIntegralLinear(double x0, double x1) const {
118 RTC_CHECK_LE(x0, x1); // Valid interval.
119 RTC_CHECK_GE(x0, limiter_start_linear_); // Beyond-knee region only.
120 auto limiter_integral = [this](const double& x) {
121 return gain_curve_limiter_i2_ * std::pow(x, gain_curve_limiter_i1_);
122 };
123 return limiter_integral(x1) - limiter_integral(x0);
124 }
125
GetKneeRegionOutputLevelDbfs(double input_level_dbfs) const126 double LimiterDbGainCurve::GetKneeRegionOutputLevelDbfs(
127 double input_level_dbfs) const {
128 return knee_region_polynomial_[0] * input_level_dbfs * input_level_dbfs +
129 knee_region_polynomial_[1] * input_level_dbfs +
130 knee_region_polynomial_[2];
131 }
132
GetCompressorRegionOutputLevelDbfs(double input_level_dbfs) const133 double LimiterDbGainCurve::GetCompressorRegionOutputLevelDbfs(
134 double input_level_dbfs) const {
135 return (input_level_dbfs - max_input_level_db_) / compression_ratio_;
136 }
137
138 } // namespace webrtc
139