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_AGC2_COMMON_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_AGC2_COMMON_H_
13 
14 #include <stddef.h>
15 
16 namespace webrtc {
17 
18 constexpr float kMinFloatS16Value = -32768.f;
19 constexpr float kMaxFloatS16Value = 32767.f;
20 constexpr float kMaxAbsFloatS16Value = 32768.0f;
21 
22 constexpr size_t kFrameDurationMs = 10;
23 constexpr size_t kSubFramesInFrame = 20;
24 constexpr size_t kMaximalNumberOfSamplesPerChannel = 480;
25 
26 constexpr float kAttackFilterConstant = 0.f;
27 
28 // Adaptive digital gain applier settings below.
29 constexpr float kMaxGainChangePerSecondDb = 3.f;
30 constexpr float kMaxGainChangePerFrameDb =
31     kMaxGainChangePerSecondDb * kFrameDurationMs / 1000.f;
32 constexpr float kHeadroomDbfs = 1.f;
33 constexpr float kMaxGainDb = 30.f;
34 constexpr float kInitialAdaptiveDigitalGainDb = 8.f;
35 // At what limiter levels should we start decreasing the adaptive digital gain.
36 constexpr float kLimiterThresholdForAgcGainDbfs = -kHeadroomDbfs;
37 
38 // This parameter must be tuned together with the noise estimator.
39 constexpr float kMaxNoiseLevelDbfs = -50.f;
40 
41 // This is the threshold for speech. Speech frames are used for updating the
42 // speech level, measuring the amount of speech, and decide when to allow target
43 // gain reduction.
44 constexpr float kVadConfidenceThreshold = 0.9f;
45 
46 // The amount of 'memory' of the Level Estimator. Decides leak factors.
47 constexpr size_t kFullBufferSizeMs = 1200;
48 constexpr float kFullBufferLeakFactor = 1.f - 1.f / kFullBufferSizeMs;
49 
50 constexpr float kInitialSpeechLevelEstimateDbfs = -30.f;
51 
52 // Saturation Protector settings.
53 float GetInitialSaturationMarginDb();
54 float GetExtraSaturationMarginOffsetDb();
55 
56 constexpr size_t kPeakEnveloperSuperFrameLengthMs = 400;
57 static_assert(kFullBufferSizeMs % kPeakEnveloperSuperFrameLengthMs == 0,
58               "Full buffer size should be a multiple of super frame length for "
59               "optimal Saturation Protector performance.");
60 
61 constexpr size_t kPeakEnveloperBufferSize =
62     kFullBufferSizeMs / kPeakEnveloperSuperFrameLengthMs + 1;
63 
64 // This value is 10 ** (-1/20 * frame_size_ms / satproc_attack_ms),
65 // where satproc_attack_ms is 5000.
66 constexpr float kSaturationProtectorAttackConstant = 0.9988493699365052f;
67 
68 // This value is 10 ** (-1/20 * frame_size_ms / satproc_decay_ms),
69 // where satproc_decay_ms is 1000.
70 constexpr float kSaturationProtectorDecayConstant = 0.9997697679981565f;
71 
72 // This is computed from kDecayMs by
73 // 10 ** (-1/20 * subframe_duration / kDecayMs).
74 // |subframe_duration| is |kFrameDurationMs / kSubFramesInFrame|.
75 // kDecayMs is defined in agc2_testing_common.h
76 constexpr float kDecayFilterConstant = 0.9998848773724686f;
77 
78 // Number of interpolation points for each region of the limiter.
79 // These values have been tuned to limit the interpolated gain curve error given
80 // the limiter parameters and allowing a maximum error of +/- 32768^-1.
81 constexpr size_t kInterpolatedGainCurveKneePoints = 22;
82 constexpr size_t kInterpolatedGainCurveBeyondKneePoints = 10;
83 constexpr size_t kInterpolatedGainCurveTotalPoints =
84     kInterpolatedGainCurveKneePoints + kInterpolatedGainCurveBeyondKneePoints;
85 
86 }  // namespace webrtc
87 
88 #endif  // MODULES_AUDIO_PROCESSING_AGC2_AGC2_COMMON_H_
89