blob: f350baeef105e3b9acff472bf947872f80eb1db8 [file] [log] [blame]
Alex Loikoa05ee822018-02-20 15:58:36 +01001/*
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_H_
12#define MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_
13
14#include <array>
15
16#include "modules/audio_processing/agc2/agc2_testing_common.h"
17
18namespace webrtc {
19
20// A class for computing gain curve parameters. The gain curve is
21// defined by constants kLimiterMaxInputLevelDbFs, kLimiterKneeSmoothnessDb,
22// kLimiterCompressionRatio. The curve consints of one linear part,
23// one quadratic polynomial part and another linear part. The
24// constants define the parameters of the parts.
25class Limiter {
26 public:
27 Limiter();
28
29 double max_input_level_db() const { return max_input_level_db_; }
30 double max_input_level_linear() const { return max_input_level_linear_; }
31 double knee_start_linear() const { return knee_start_linear_; }
32 double limiter_start_linear() const { return limiter_start_linear_; }
33
34 // These methods can be marked 'constexpr' in C++ 14.
35 double GetOutputLevelDbfs(double input_level_dbfs) const;
36 double GetGainLinear(double input_level_linear) const;
37 double GetGainFirstDerivativeLinear(double x) const;
38 double GetGainIntegralLinear(double x0, double x1) const;
39
40 private:
41 double GetKneeRegionOutputLevelDbfs(double input_level_dbfs) const;
42 double GetCompressorRegionOutputLevelDbfs(double input_level_dbfs) const;
43
44 static constexpr double max_input_level_db_ = test::kLimiterMaxInputLevelDbFs;
45 static constexpr double knee_smoothness_db_ = test::kLimiterKneeSmoothnessDb;
46 static constexpr double compression_ratio_ = test::kLimiterCompressionRatio;
47
48 const double max_input_level_linear_;
49
50 // Do not modify signal with level <= knee_start_dbfs_.
51 const double knee_start_dbfs_;
52 const double knee_start_linear_;
53
54 // The upper end of the knee region, which is between knee_start_dbfs_ and
55 // limiter_start_dbfs_.
56 const double limiter_start_dbfs_;
57 const double limiter_start_linear_;
58
59 // Coefficients {a, b, c} of the knee region polynomial
60 // ax^2 + bx + c in the DB scale.
61 const std::array<double, 3> knee_region_polynomial_;
62
63 // Parameters for the computation of the first derivative of GetGainLinear().
64 const double gain_curve_limiter_d1_;
65 const double gain_curve_limiter_d2_;
66
67 // Parameters for the computation of the integral of GetGainLinear().
68 const double gain_curve_limiter_i1_;
69 const double gain_curve_limiter_i2_;
70};
71
72} // namespace webrtc
73
74#endif // MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_