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 #ifndef MODULES_AUDIO_PROCESSING_AGC2_LIMITER_DB_GAIN_CURVE_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_LIMITER_DB_GAIN_CURVE_H_
13 
14 #include <array>
15 
16 #include "modules/audio_processing/agc2/agc2_testing_common.h"
17 
18 namespace webrtc {
19 
20 // A class for computing a limiter gain curve (in dB scale) given a set of
21 // hard-coded parameters (namely, kLimiterDbGainCurveMaxInputLevelDbFs,
22 // kLimiterDbGainCurveKneeSmoothnessDb, and
23 // kLimiterDbGainCurveCompressionRatio). The generated curve consists of four
24 // regions: identity (linear), knee (quadratic polynomial), compression
25 // (linear), saturation (linear). The aforementioned constants are used to shape
26 // the different regions.
27 class LimiterDbGainCurve {
28  public:
29   LimiterDbGainCurve();
30 
max_input_level_db()31   double max_input_level_db() const { return max_input_level_db_; }
max_input_level_linear()32   double max_input_level_linear() const { return max_input_level_linear_; }
knee_start_linear()33   double knee_start_linear() const { return knee_start_linear_; }
limiter_start_linear()34   double limiter_start_linear() const { return limiter_start_linear_; }
35 
36   // These methods can be marked 'constexpr' in C++ 14.
37   double GetOutputLevelDbfs(double input_level_dbfs) const;
38   double GetGainLinear(double input_level_linear) const;
39   double GetGainFirstDerivativeLinear(double x) const;
40   double GetGainIntegralLinear(double x0, double x1) const;
41 
42  private:
43   double GetKneeRegionOutputLevelDbfs(double input_level_dbfs) const;
44   double GetCompressorRegionOutputLevelDbfs(double input_level_dbfs) const;
45 
46   static constexpr double max_input_level_db_ = test::kLimiterMaxInputLevelDbFs;
47   static constexpr double knee_smoothness_db_ = test::kLimiterKneeSmoothnessDb;
48   static constexpr double compression_ratio_ = test::kLimiterCompressionRatio;
49 
50   const double max_input_level_linear_;
51 
52   // Do not modify signal with level <= knee_start_dbfs_.
53   const double knee_start_dbfs_;
54   const double knee_start_linear_;
55 
56   // The upper end of the knee region, which is between knee_start_dbfs_ and
57   // limiter_start_dbfs_.
58   const double limiter_start_dbfs_;
59   const double limiter_start_linear_;
60 
61   // Coefficients {a, b, c} of the knee region polynomial
62   // ax^2 + bx + c in the DB scale.
63   const std::array<double, 3> knee_region_polynomial_;
64 
65   // Parameters for the computation of the first derivative of GetGainLinear().
66   const double gain_curve_limiter_d1_;
67   const double gain_curve_limiter_d2_;
68 
69   // Parameters for the computation of the integral of GetGainLinear().
70   const double gain_curve_limiter_i1_;
71   const double gain_curve_limiter_i2_;
72 };
73 
74 }  // namespace webrtc
75 
76 #endif  // MODULES_AUDIO_PROCESSING_AGC2_LIMITER_DB_GAIN_CURVE_H_
77