blob: a6389f4c2da6a170d970b5993677d48a7696ab21 [file] [log] [blame]
Alex Loikoe36e8bb2018-02-16 11:54:07 +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_AGC2_COMMON_H_
12#define MODULES_AUDIO_PROCESSING_AGC2_AGC2_COMMON_H_
13
Niels Möller65ec0fc2018-05-21 11:46:20 +020014#include <stddef.h>
15
Alex Loikoe36e8bb2018-02-16 11:54:07 +010016namespace webrtc {
17
Alex Loikoa05ee822018-02-20 15:58:36 +010018constexpr float kMinFloatS16Value = -32768.f;
19constexpr float kMaxFloatS16Value = 32767.f;
Alex Loiko9917c4a2018-04-04 14:16:10 +020020constexpr float kMaxAbsFloatS16Value = 32768.0f;
Alex Loikoe36e8bb2018-02-16 11:54:07 +010021
Alex Loiko153f11e2018-02-16 12:39:00 +010022constexpr size_t kFrameDurationMs = 10;
23constexpr size_t kSubFramesInFrame = 20;
Alex Loikoa05ee822018-02-20 15:58:36 +010024constexpr size_t kMaximalNumberOfSamplesPerChannel = 480;
Alex Loiko153f11e2018-02-16 12:39:00 +010025
26constexpr float kAttackFilterConstant = 0.f;
27
Alex Loikocab48c32018-04-04 17:43:31 +020028// Adaptive digital gain applier settings below.
29constexpr float kMaxGainChangePerSecondDb = 3.f;
30constexpr float kMaxGainChangePerFrameDb =
31 kMaxGainChangePerSecondDb * kFrameDurationMs / 1000.f;
32constexpr float kHeadroomDbfs = 1.f;
33constexpr float kMaxGainDb = 30.f;
Alex Loiko95141d92018-04-20 15:28:45 +020034constexpr float kInitialAdaptiveDigitalGainDb = 8.f;
Alex Loiko93e57502018-10-01 16:28:47 +020035// At what limiter levels should we start decreasing the adaptive digital gain.
36constexpr float kLimiterThresholdForAgcGainDbfs = -kHeadroomDbfs;
Alex Loikocab48c32018-04-04 17:43:31 +020037
38// This parameter must be tuned together with the noise estimator.
39constexpr float kMaxNoiseLevelDbfs = -50.f;
40
Alex Loiko2ffafa82018-07-06 15:35:42 +020041// 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.
Alex Loiko57011622018-12-10 15:15:59 +010044constexpr float kVadConfidenceThreshold = 0.9f;
Alex Loiko1e48e802018-03-28 09:45:29 +020045
46// The amount of 'memory' of the Level Estimator. Decides leak factors.
Alex Loiko57011622018-12-10 15:15:59 +010047constexpr size_t kFullBufferSizeMs = 1200;
Alex Loiko1e48e802018-03-28 09:45:29 +020048constexpr float kFullBufferLeakFactor = 1.f - 1.f / kFullBufferSizeMs;
49
50constexpr float kInitialSpeechLevelEstimateDbfs = -30.f;
51
Alex Loiko9917c4a2018-04-04 14:16:10 +020052// Saturation Protector settings.
Alex Loiko4bb1e4a2018-10-05 11:06:14 +020053float GetInitialSaturationMarginDb();
54float GetExtraSaturationMarginOffsetDb();
Alex Loiko1e48e802018-03-28 09:45:29 +020055
Alex Loikodb6af362018-06-20 14:14:18 +020056constexpr size_t kPeakEnveloperSuperFrameLengthMs = 400;
57static_assert(kFullBufferSizeMs % kPeakEnveloperSuperFrameLengthMs == 0,
58 "Full buffer size should be a multiple of super frame length for "
59 "optimal Saturation Protector performance.");
Alex Loiko9917c4a2018-04-04 14:16:10 +020060
61constexpr 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.
66constexpr 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.
70constexpr float kSaturationProtectorDecayConstant = 0.9997697679981565f;
71
Alex Loiko153f11e2018-02-16 12:39:00 +010072// 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
76constexpr float kDecayFilterConstant = 0.9998848773724686f;
77
Alex Loikoa05ee822018-02-20 15:58:36 +010078// 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.
81constexpr size_t kInterpolatedGainCurveKneePoints = 22;
82constexpr size_t kInterpolatedGainCurveBeyondKneePoints = 10;
83constexpr size_t kInterpolatedGainCurveTotalPoints =
84 kInterpolatedGainCurveKneePoints + kInterpolatedGainCurveBeyondKneePoints;
85
Alex Loikoe36e8bb2018-02-16 11:54:07 +010086} // namespace webrtc
87
88#endif // MODULES_AUDIO_PROCESSING_AGC2_AGC2_COMMON_H_