blob: d80a255ced43da1788c1cddbf2c5e90cff3d6a88 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
12#define MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013
kwiberg88788ad2016-02-19 07:04:49 -080014#include <memory>
15
Per Åhgren0e3198e2019-11-18 08:52:22 +010016#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/agc/agc.h"
Hanna Silena0047152021-06-03 03:29:38 +020018#include "modules/audio_processing/agc/clipping_predictor.h"
Alessio Bazzica42dacda2021-06-17 17:18:46 +020019#include "modules/audio_processing/agc/clipping_predictor_evaluator.h"
Per Åhgren3daedb62019-11-22 12:11:40 +010020#include "modules/audio_processing/audio_buffer.h"
Alex Loikoc1676732018-07-02 12:05:28 +020021#include "modules/audio_processing/logging/apm_data_dumper.h"
Alex Loiko9489c3a2018-08-09 15:04:24 +020022#include "rtc_base/gtest_prod_util.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023
24namespace webrtc {
25
Per Åhgren3daedb62019-11-22 12:11:40 +010026class MonoAgc;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000027class GainControl;
28
pbos@webrtc.org788acd12014-12-15 09:41:24 +000029// Direct interface to use AGC to set volume and compression values.
30// AudioProcessing uses this interface directly to integrate the callback-less
Alejandro Luebsd094c042015-09-29 15:43:42 -070031// AGC.
pbos@webrtc.org788acd12014-12-15 09:41:24 +000032//
33// This class is not thread-safe.
Alejandro Luebsd094c042015-09-29 15:43:42 -070034class AgcManagerDirect final {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000035 public:
36 // AgcManagerDirect will configure GainControl internally. The user is
37 // responsible for processing the audio using it after the call to Process.
Bjorn Volckeradc46c42015-04-15 11:42:40 +020038 // The operating range of startup_min_level is [12, 255] and any input value
Hanna Silenb8dc7fa2021-05-20 17:37:56 +020039 // outside that range will be clamped. `clipped_level_step` is the amount
40 // the microphone level is lowered with every clipping event, limited to
41 // (0, 255]. `clipped_ratio_threshold` is the proportion of clipped
42 // samples required to declare a clipping event, limited to (0.f, 1.f).
43 // `clipped_wait_frames` is the time in frames to wait after a clipping event
44 // before checking again, limited to values higher than 0.
Alessio Bazzica42dacda2021-06-17 17:18:46 +020045 AgcManagerDirect(
46 int num_capture_channels,
47 int startup_min_level,
48 int clipped_level_min,
49 bool disable_digital_adaptive,
50 int sample_rate_hz,
51 int clipped_level_step,
52 float clipped_ratio_threshold,
53 int clipped_wait_frames,
54 const AudioProcessing::Config::GainController1::AnalogGainController::
55 ClippingPredictor& clipping_config);
Alex Loiko64cb83b2018-07-02 13:38:19 +020056
pbos@webrtc.org788acd12014-12-15 09:41:24 +000057 ~AgcManagerDirect();
Per Åhgren3daedb62019-11-22 12:11:40 +010058 AgcManagerDirect(const AgcManagerDirect&) = delete;
59 AgcManagerDirect& operator=(const AgcManagerDirect&) = delete;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000060
Per Åhgren0e3198e2019-11-18 08:52:22 +010061 void Initialize();
Per Åhgren3daedb62019-11-22 12:11:40 +010062 void SetupDigitalGainControl(GainControl* gain_control) const;
Per Åhgren0e3198e2019-11-18 08:52:22 +010063
Per Åhgren3daedb62019-11-22 12:11:40 +010064 void AnalyzePreProcess(const AudioBuffer* audio);
65 void Process(const AudioBuffer* audio);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000066
Per Åhgren0a144a72021-02-09 08:47:51 +010067 // Call when the capture stream output has been flagged to be used/not-used.
68 // If unused, the manager disregards all incoming audio.
69 void HandleCaptureOutputUsedChange(bool capture_output_used);
Per Åhgren3daedb62019-11-22 12:11:40 +010070 float voice_probability() const;
Alejandro Luebsd094c042015-09-29 15:43:42 -070071
Per Åhgren0e3198e2019-11-18 08:52:22 +010072 int stream_analog_level() const { return stream_analog_level_; }
Per Åhgren3daedb62019-11-22 12:11:40 +010073 void set_stream_analog_level(int level);
74 int num_channels() const { return num_capture_channels_; }
75 int sample_rate_hz() const { return sample_rate_hz_; }
76
77 // If available, returns a new compression gain for the digital gain control.
78 absl::optional<int> GetDigitalComressionGain();
Per Åhgren0e3198e2019-11-18 08:52:22 +010079
Alessio Bazzica42dacda2021-06-17 17:18:46 +020080 // Returns true if clipping prediction is enabled.
81 bool clipping_predictor_enabled() const { return !!clipping_predictor_; }
82
83 // Returns true if clipping prediction is used to adjust the analog gain.
84 bool use_clipping_predictor_step() const {
85 return use_clipping_predictor_step_;
86 }
87
Alejandro Luebsd094c042015-09-29 15:43:42 -070088 private:
Alex Loiko2ffafa82018-07-06 15:35:42 +020089 friend class AgcManagerDirectTest;
90
Alex Loiko9489c3a2018-08-09 15:04:24 +020091 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
92 DisableDigitalDisablesDigital);
henrikaebf45522019-11-04 13:59:21 +010093 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
94 AgcMinMicLevelExperiment);
Alessio Bazzica42eef862021-01-15 16:41:48 +010095 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
96 AgcMinMicLevelExperimentDisabled);
97 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
98 AgcMinMicLevelExperimentOutOfRangeAbove);
99 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
100 AgcMinMicLevelExperimentOutOfRangeBelow);
101 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
102 AgcMinMicLevelExperimentEnabled50);
103 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
104 AgcMinMicLevelExperimentEnabledAboveStartupLevel);
Hanna Silenb8dc7fa2021-05-20 17:37:56 +0200105 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
106 ClippingParametersVerified);
Hanna Silena0047152021-06-03 03:29:38 +0200107 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
108 DisableClippingPredictorDoesNotLowerVolume);
Alessio Bazzica42dacda2021-06-17 17:18:46 +0200109 FRIEND_TEST_ALL_PREFIXES(
110 AgcManagerDirectStandaloneTest,
111 EnableClippingPredictorWithUnusedPredictedStepDoesNotLowerVolume);
Hanna Silena0047152021-06-03 03:29:38 +0200112 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
113 EnableClippingPredictorLowersVolume);
Alex Loiko9489c3a2018-08-09 15:04:24 +0200114
Alex Loiko2ffafa82018-07-06 15:35:42 +0200115 // Dependency injection for testing. Don't delete |agc| as the memory is owned
116 // by the manager.
Alessio Bazzica42dacda2021-06-17 17:18:46 +0200117 AgcManagerDirect(
118 Agc* agc,
119 int startup_min_level,
120 int clipped_level_min,
121 int sample_rate_hz,
122 int clipped_level_step,
123 float clipped_ratio_threshold,
124 int clipped_wait_frames,
125 const AudioProcessing::Config::GainController1::AnalogGainController::
126 ClippingPredictor& clipping_config);
Alex Loiko2ffafa82018-07-06 15:35:42 +0200127
Per Åhgren3daedb62019-11-22 12:11:40 +0100128 void AnalyzePreProcess(const float* const* audio, size_t samples_per_channel);
129
130 void AggregateChannelLevels();
131
132 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren3daedb62019-11-22 12:11:40 +0100133 static int instance_counter_;
Per Åhgren26cc5e62019-11-26 22:58:53 +0100134 const bool use_min_channel_level_;
Per Åhgren3daedb62019-11-22 12:11:40 +0100135 const int sample_rate_hz_;
136 const int num_capture_channels_;
137 const bool disable_digital_adaptive_;
138
139 int frames_since_clipped_;
140 int stream_analog_level_ = 0;
Per Åhgren0a144a72021-02-09 08:47:51 +0100141 bool capture_output_used_;
Per Åhgren3daedb62019-11-22 12:11:40 +0100142 int channel_controlling_gain_ = 0;
143
Hanna Silenb8dc7fa2021-05-20 17:37:56 +0200144 const int clipped_level_step_;
145 const float clipped_ratio_threshold_;
146 const int clipped_wait_frames_;
147
Per Åhgren3daedb62019-11-22 12:11:40 +0100148 std::vector<std::unique_ptr<MonoAgc>> channel_agcs_;
149 std::vector<absl::optional<int>> new_compressions_to_set_;
Hanna Silena0047152021-06-03 03:29:38 +0200150
151 const std::unique_ptr<ClippingPredictor> clipping_predictor_;
Alessio Bazzica42dacda2021-06-17 17:18:46 +0200152 const bool use_clipping_predictor_step_;
153 ClippingPredictorEvaluator clipping_predictor_evaluator_;
154 int clipping_predictor_log_counter_;
Hanna Silene7e92922021-07-08 17:26:31 +0200155 float clipping_rate_log_;
156 int clipping_rate_log_counter_;
Per Åhgren3daedb62019-11-22 12:11:40 +0100157};
158
159class MonoAgc {
160 public:
161 MonoAgc(ApmDataDumper* data_dumper,
162 int startup_min_level,
163 int clipped_level_min,
Per Åhgren3daedb62019-11-22 12:11:40 +0100164 bool disable_digital_adaptive,
165 int min_mic_level);
166 ~MonoAgc();
167 MonoAgc(const MonoAgc&) = delete;
168 MonoAgc& operator=(const MonoAgc&) = delete;
169
170 void Initialize();
Per Åhgren0a144a72021-02-09 08:47:51 +0100171 void HandleCaptureOutputUsedChange(bool capture_output_used);
Per Åhgren3daedb62019-11-22 12:11:40 +0100172
Hanna Silenb8dc7fa2021-05-20 17:37:56 +0200173 void HandleClipping(int clipped_level_step);
Per Åhgren3daedb62019-11-22 12:11:40 +0100174
175 void Process(const int16_t* audio,
176 size_t samples_per_channel,
177 int sample_rate_hz);
178
179 void set_stream_analog_level(int level) { stream_analog_level_ = level; }
180 int stream_analog_level() const { return stream_analog_level_; }
181 float voice_probability() const { return agc_->voice_probability(); }
182 void ActivateLogging() { log_to_histograms_ = true; }
183 absl::optional<int> new_compression() const {
184 return new_compression_to_set_;
185 }
186
187 // Only used for testing.
188 void set_agc(Agc* agc) { agc_.reset(agc); }
henrikaebf45522019-11-04 13:59:21 +0100189 int min_mic_level() const { return min_mic_level_; }
190 int startup_min_level() const { return startup_min_level_; }
191
Per Åhgren3daedb62019-11-22 12:11:40 +0100192 private:
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000193 // Sets a new microphone level, after first checking that it hasn't been
194 // updated by the user, in which case no action is taken.
195 void SetLevel(int new_level);
196
197 // Set the maximum level the AGC is allowed to apply. Also updates the
198 // maximum compression gain to compensate. The level must be at least
199 // |kClippedLevelMin|.
200 void SetMaxLevel(int level);
201
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000202 int CheckVolumeAndReset();
203 void UpdateGain();
204 void UpdateCompressor();
205
Per Åhgren3daedb62019-11-22 12:11:40 +0100206 const int min_mic_level_;
207 const bool disable_digital_adaptive_;
kwiberg88788ad2016-02-19 07:04:49 -0800208 std::unique_ptr<Agc> agc_;
Per Åhgren3daedb62019-11-22 12:11:40 +0100209 int level_ = 0;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000210 int max_level_;
211 int max_compression_gain_;
212 int target_compression_;
213 int compression_;
214 float compression_accumulator_;
Per Åhgren0a144a72021-02-09 08:47:51 +0100215 bool capture_output_used_ = true;
Per Åhgren3daedb62019-11-22 12:11:40 +0100216 bool check_volume_on_next_process_ = true;
217 bool startup_ = true;
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200218 int startup_min_level_;
Alex Loikof3122e02018-08-10 14:43:51 +0200219 int calls_since_last_gain_log_ = 0;
Per Åhgren0e3198e2019-11-18 08:52:22 +0100220 int stream_analog_level_ = 0;
221 absl::optional<int> new_compression_to_set_;
Per Åhgren3daedb62019-11-22 12:11:40 +0100222 bool log_to_histograms_ = false;
223 const int clipped_level_min_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000224};
225
226} // namespace webrtc
227
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200228#endif // MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_