pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/agc/agc_manager_direct.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <algorithm> |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 14 | #include <cmath> |
| 15 | |
| 16 | #ifdef WEBRTC_AGC_DEBUG_DUMP |
| 17 | #include <cstdio> |
| 18 | #endif |
| 19 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 20 | #include "common_audio/include/audio_util.h" |
Sam Zackrisson | 41478c7 | 2019-10-15 10:10:26 +0200 | [diff] [blame] | 21 | #include "modules/audio_processing/agc/gain_control.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/agc/gain_map_internal.h" |
Alex Loiko | 2ffafa8 | 2018-07-06 15:35:42 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/checks.h" |
| 25 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 26 | #include "rtc_base/numerics/safe_minmax.h" |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 27 | #include "system_wrappers/include/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "system_wrappers/include/metrics.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 32 | int AgcManagerDirect::instance_counter_ = 0; |
| 33 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 36 | // Amount the microphone level is lowered with every clipping event. |
| 37 | const int kClippedLevelStep = 15; |
| 38 | // Proportion of clipped samples required to declare a clipping event. |
| 39 | const float kClippedRatioThreshold = 0.1f; |
| 40 | // Time in frames to wait after a clipping event before checking again. |
| 41 | const int kClippedWaitFrames = 300; |
| 42 | |
| 43 | // Amount of error we tolerate in the microphone level (presumably due to OS |
| 44 | // quantization) before we assume the user has manually adjusted the microphone. |
| 45 | const int kLevelQuantizationSlack = 25; |
| 46 | |
| 47 | const int kDefaultCompressionGain = 7; |
| 48 | const int kMaxCompressionGain = 12; |
| 49 | const int kMinCompressionGain = 2; |
| 50 | // Controls the rate of compression changes towards the target. |
| 51 | const float kCompressionGainStep = 0.05f; |
| 52 | |
| 53 | const int kMaxMicLevel = 255; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 54 | static_assert(kGainMapSize > kMaxMicLevel, "gain map too small"); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 55 | const int kMinMicLevel = 12; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 56 | |
| 57 | // Prevent very large microphone level changes. |
| 58 | const int kMaxResidualGainChange = 15; |
| 59 | |
| 60 | // Maximum additional gain allowed to compensate for microphone level |
| 61 | // restrictions from clipping events. |
| 62 | const int kSurplusCompressionGain = 6; |
| 63 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 64 | // Maximum number of channels and number of samples per channel supported. |
| 65 | constexpr size_t kMaxNumSamplesPerChannel = 1920; |
| 66 | constexpr size_t kMaxNumChannels = 4; |
| 67 | |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 68 | // Returns kMinMicLevel if no field trial exists or if it has been disabled. |
| 69 | // Returns a value between 0 and 255 depending on the field-trial string. |
| 70 | // Example: 'WebRTC-Audio-AgcMinMicLevelExperiment/Enabled-80' => returns 80. |
| 71 | int GetMinMicLevel() { |
| 72 | RTC_LOG(LS_INFO) << "[agc] GetMinMicLevel"; |
| 73 | constexpr char kMinMicLevelFieldTrial[] = |
| 74 | "WebRTC-Audio-AgcMinMicLevelExperiment"; |
| 75 | if (!webrtc::field_trial::IsEnabled(kMinMicLevelFieldTrial)) { |
| 76 | RTC_LOG(LS_INFO) << "[agc] Using default min mic level: " << kMinMicLevel; |
| 77 | return kMinMicLevel; |
| 78 | } |
| 79 | const auto field_trial_string = |
| 80 | webrtc::field_trial::FindFullName(kMinMicLevelFieldTrial); |
| 81 | int min_mic_level = -1; |
| 82 | sscanf(field_trial_string.c_str(), "Enabled-%d", &min_mic_level); |
| 83 | if (min_mic_level >= 0 && min_mic_level <= 255) { |
| 84 | RTC_LOG(LS_INFO) << "[agc] Experimental min mic level: " << min_mic_level; |
| 85 | return min_mic_level; |
| 86 | } else { |
| 87 | RTC_LOG(LS_WARNING) << "[agc] Invalid parameter for " |
| 88 | << kMinMicLevelFieldTrial << ", ignored."; |
| 89 | return kMinMicLevel; |
| 90 | } |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 91 | } |
| 92 | |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 93 | int ClampLevel(int mic_level, int min_mic_level) { |
| 94 | return rtc::SafeClamp(mic_level, min_mic_level, kMaxMicLevel); |
| 95 | } |
| 96 | |
| 97 | int LevelFromGainError(int gain_error, int level, int min_mic_level) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 98 | RTC_DCHECK_GE(level, 0); |
| 99 | RTC_DCHECK_LE(level, kMaxMicLevel); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 100 | if (gain_error == 0) { |
| 101 | return level; |
| 102 | } |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 103 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 104 | int new_level = level; |
| 105 | if (gain_error > 0) { |
| 106 | while (kGainMap[new_level] - kGainMap[level] < gain_error && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 107 | new_level < kMaxMicLevel) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 108 | ++new_level; |
| 109 | } |
| 110 | } else { |
| 111 | while (kGainMap[new_level] - kGainMap[level] > gain_error && |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 112 | new_level > min_mic_level) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 113 | --new_level; |
| 114 | } |
| 115 | } |
| 116 | return new_level; |
| 117 | } |
| 118 | |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 119 | int InitializeGainControl(GainControl* gain_control, |
| 120 | bool disable_digital_adaptive) { |
| 121 | if (gain_control->set_mode(GainControl::kFixedDigital) != 0) { |
| 122 | RTC_LOG(LS_ERROR) << "set_mode(GainControl::kFixedDigital) failed."; |
| 123 | return -1; |
| 124 | } |
| 125 | const int target_level_dbfs = disable_digital_adaptive ? 0 : 2; |
| 126 | if (gain_control->set_target_level_dbfs(target_level_dbfs) != 0) { |
| 127 | RTC_LOG(LS_ERROR) << "set_target_level_dbfs() failed."; |
| 128 | return -1; |
| 129 | } |
| 130 | const int compression_gain_db = |
| 131 | disable_digital_adaptive ? 0 : kDefaultCompressionGain; |
| 132 | if (gain_control->set_compression_gain_db(compression_gain_db) != 0) { |
| 133 | RTC_LOG(LS_ERROR) << "set_compression_gain_db() failed."; |
| 134 | return -1; |
| 135 | } |
| 136 | const bool enable_limiter = !disable_digital_adaptive; |
| 137 | if (gain_control->enable_limiter(enable_limiter) != 0) { |
| 138 | RTC_LOG(LS_ERROR) << "enable_limiter() failed."; |
| 139 | return -1; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 144 | // Returns the proportion of samples in the buffer which are at full-scale |
| 145 | // (and presumably clipped). |
| 146 | float ComputeClippedRatio(const float* const* audio, |
| 147 | size_t num_channels, |
| 148 | size_t samples_per_channel) { |
Per Åhgren | b49aec5 | 2019-11-07 08:41:20 +0100 | [diff] [blame] | 149 | RTC_DCHECK_GT(samples_per_channel, 0); |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 150 | int num_clipped = 0; |
| 151 | for (size_t ch = 0; ch < num_channels; ++ch) { |
Per Åhgren | b49aec5 | 2019-11-07 08:41:20 +0100 | [diff] [blame] | 152 | int num_clipped_in_ch = 0; |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 153 | for (size_t i = 0; i < samples_per_channel; ++i) { |
| 154 | RTC_DCHECK(audio[ch]); |
| 155 | if (audio[ch][i] >= 32767.f || audio[ch][i] <= -32768.f) { |
Per Åhgren | b49aec5 | 2019-11-07 08:41:20 +0100 | [diff] [blame] | 156 | ++num_clipped_in_ch; |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
Per Åhgren | b49aec5 | 2019-11-07 08:41:20 +0100 | [diff] [blame] | 159 | num_clipped = std::max(num_clipped, num_clipped_in_ch); |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 160 | } |
Per Åhgren | b49aec5 | 2019-11-07 08:41:20 +0100 | [diff] [blame] | 161 | return static_cast<float>(num_clipped) / (samples_per_channel); |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 162 | } |
| 163 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 164 | } // namespace |
| 165 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 166 | AgcManagerDirect::AgcManagerDirect(Agc* agc, |
| 167 | GainControl* gctrl, |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 168 | VolumeCallbacks* volume_callbacks, |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 169 | int startup_min_level, |
| 170 | int clipped_level_min) |
Per Åhgren | b8c1be5 | 2019-11-07 20:35:50 +0100 | [diff] [blame] | 171 | : AgcManagerDirect(gctrl, |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 172 | volume_callbacks, |
| 173 | startup_min_level, |
| 174 | clipped_level_min, |
| 175 | false, |
Alex Loiko | 99f1e0d | 2018-07-19 16:39:39 +0200 | [diff] [blame] | 176 | false) { |
| 177 | RTC_DCHECK(agc_); |
Per Åhgren | b8c1be5 | 2019-11-07 20:35:50 +0100 | [diff] [blame] | 178 | agc_.reset(agc); |
Alex Loiko | 99f1e0d | 2018-07-19 16:39:39 +0200 | [diff] [blame] | 179 | } |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 180 | |
Per Åhgren | b8c1be5 | 2019-11-07 20:35:50 +0100 | [diff] [blame] | 181 | AgcManagerDirect::AgcManagerDirect(GainControl* gctrl, |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 182 | VolumeCallbacks* volume_callbacks, |
| 183 | int startup_min_level, |
| 184 | int clipped_level_min, |
| 185 | bool use_agc2_level_estimation, |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 186 | bool disable_digital_adaptive) |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 187 | : data_dumper_(new ApmDataDumper(instance_counter_)), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 188 | gctrl_(gctrl), |
| 189 | volume_callbacks_(volume_callbacks), |
| 190 | frames_since_clipped_(kClippedWaitFrames), |
| 191 | level_(0), |
| 192 | max_level_(kMaxMicLevel), |
| 193 | max_compression_gain_(kMaxCompressionGain), |
| 194 | target_compression_(kDefaultCompressionGain), |
| 195 | compression_(target_compression_), |
| 196 | compression_accumulator_(compression_), |
| 197 | capture_muted_(false), |
| 198 | check_volume_on_next_process_(true), // Check at startup. |
| 199 | startup_(true), |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 200 | min_mic_level_(GetMinMicLevel()), |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 201 | disable_digital_adaptive_(disable_digital_adaptive), |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 202 | startup_min_level_(ClampLevel(startup_min_level, min_mic_level_)), |
Per Åhgren | 7c1fb41 | 2019-11-07 06:55:35 +0100 | [diff] [blame] | 203 | clipped_level_min_(clipped_level_min) { |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 204 | instance_counter_++; |
Per Åhgren | b8c1be5 | 2019-11-07 20:35:50 +0100 | [diff] [blame] | 205 | if (use_agc2_level_estimation) { |
| 206 | agc_ = std::make_unique<AdaptiveModeLevelEstimatorAgc>(data_dumper_.get()); |
Alex Loiko | 2ffafa8 | 2018-07-06 15:35:42 +0200 | [diff] [blame] | 207 | } else { |
Per Åhgren | b8c1be5 | 2019-11-07 20:35:50 +0100 | [diff] [blame] | 208 | agc_ = std::make_unique<Agc>(); |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 209 | } |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 210 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 211 | |
| 212 | AgcManagerDirect::~AgcManagerDirect() {} |
| 213 | |
| 214 | int AgcManagerDirect::Initialize() { |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 215 | RTC_DLOG(LS_INFO) << "AgcManagerDirect::Initialize"; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 216 | max_level_ = kMaxMicLevel; |
| 217 | max_compression_gain_ = kMaxCompressionGain; |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 218 | target_compression_ = disable_digital_adaptive_ ? 0 : kDefaultCompressionGain; |
| 219 | compression_ = disable_digital_adaptive_ ? 0 : target_compression_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 220 | compression_accumulator_ = compression_; |
| 221 | capture_muted_ = false; |
| 222 | check_volume_on_next_process_ = true; |
| 223 | // TODO(bjornv): Investigate if we need to reset |startup_| as well. For |
| 224 | // example, what happens when we change devices. |
| 225 | |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 226 | data_dumper_->InitiateNewSetOfRecordings(); |
| 227 | |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 228 | return InitializeGainControl(gctrl_, disable_digital_adaptive_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 231 | void AgcManagerDirect::AnalyzePreProcess(const float* const* audio, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 232 | int num_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 233 | size_t samples_per_channel) { |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 234 | RTC_DCHECK(audio); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 235 | if (capture_muted_) { |
| 236 | return; |
| 237 | } |
| 238 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 239 | if (frames_since_clipped_ < kClippedWaitFrames) { |
| 240 | ++frames_since_clipped_; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | // Check for clipped samples, as the AGC has difficulty detecting pitch |
| 245 | // under clipping distortion. We do this in the preprocessing phase in order |
| 246 | // to catch clipped echo as well. |
| 247 | // |
| 248 | // If we find a sufficiently clipped frame, drop the current microphone level |
| 249 | // and enforce a new maximum level, dropped the same amount from the current |
| 250 | // maximum. This harsh treatment is an effort to avoid repeated clipped echo |
| 251 | // events. As compensation for this restriction, the maximum compression |
| 252 | // gain is increased, through SetMaxLevel(). |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 253 | float clipped_ratio = |
| 254 | ComputeClippedRatio(audio, num_channels, samples_per_channel); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 255 | if (clipped_ratio > kClippedRatioThreshold) { |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 256 | RTC_DLOG(LS_INFO) << "[agc] Clipping detected. clipped_ratio=" |
| 257 | << clipped_ratio; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 258 | // Always decrease the maximum level, even if the current level is below |
| 259 | // threshold. |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 260 | SetMaxLevel(std::max(clipped_level_min_, max_level_ - kClippedLevelStep)); |
henrik.lundin | 30a12fb | 2016-11-22 07:02:44 -0800 | [diff] [blame] | 261 | RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.AgcClippingAdjustmentAllowed", |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 262 | level_ - kClippedLevelStep >= clipped_level_min_); |
| 263 | if (level_ > clipped_level_min_) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 264 | // Don't try to adjust the level if we're already below the limit. As |
| 265 | // a consequence, if the user has brought the level above the limit, we |
| 266 | // will still not react until the postproc updates the level. |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 267 | SetLevel(std::max(clipped_level_min_, level_ - kClippedLevelStep)); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 268 | // Reset the AGC since the level has changed. |
| 269 | agc_->Reset(); |
| 270 | } |
| 271 | frames_since_clipped_ = 0; |
| 272 | } |
| 273 | } |
| 274 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 275 | void AgcManagerDirect::Process(const float* audio, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 276 | size_t length, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 277 | int sample_rate_hz) { |
| 278 | if (capture_muted_) { |
| 279 | return; |
| 280 | } |
| 281 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 282 | std::array<int16_t, kMaxNumSamplesPerChannel * kMaxNumChannels> audio_data; |
| 283 | const int16_t* audio_fix; |
| 284 | size_t safe_length; |
| 285 | if (audio) { |
| 286 | audio_fix = audio_data.data(); |
| 287 | safe_length = std::min(audio_data.size(), length); |
| 288 | FloatS16ToS16(audio, length, audio_data.data()); |
| 289 | } else { |
| 290 | audio_fix = nullptr; |
| 291 | safe_length = length; |
| 292 | } |
| 293 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 294 | if (check_volume_on_next_process_) { |
| 295 | check_volume_on_next_process_ = false; |
| 296 | // We have to wait until the first process call to check the volume, |
| 297 | // because Chromium doesn't guarantee it to be valid any earlier. |
| 298 | CheckVolumeAndReset(); |
| 299 | } |
| 300 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 301 | agc_->Process(audio_fix, safe_length, sample_rate_hz); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 302 | |
| 303 | UpdateGain(); |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 304 | if (!disable_digital_adaptive_) { |
| 305 | UpdateCompressor(); |
| 306 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 307 | |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 308 | data_dumper_->DumpRaw("experimental_gain_control_compression_gain_db", 1, |
| 309 | &compression_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void AgcManagerDirect::SetLevel(int new_level) { |
| 313 | int voe_level = volume_callbacks_->GetMicVolume(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 314 | if (voe_level == 0) { |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 315 | RTC_DLOG(LS_INFO) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 316 | << "[agc] VolumeCallbacks returned level=0, taking no action."; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 317 | return; |
| 318 | } |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 319 | if (voe_level < 0 || voe_level > kMaxMicLevel) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 320 | RTC_LOG(LS_ERROR) << "VolumeCallbacks returned an invalid level=" |
| 321 | << voe_level; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | |
| 325 | if (voe_level > level_ + kLevelQuantizationSlack || |
| 326 | voe_level < level_ - kLevelQuantizationSlack) { |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 327 | RTC_DLOG(LS_INFO) << "[agc] Mic volume was manually adjusted. Updating " |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 328 | "stored level from " |
| 329 | << level_ << " to " << voe_level; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 330 | level_ = voe_level; |
| 331 | // Always allow the user to increase the volume. |
| 332 | if (level_ > max_level_) { |
| 333 | SetMaxLevel(level_); |
| 334 | } |
| 335 | // Take no action in this case, since we can't be sure when the volume |
| 336 | // was manually adjusted. The compressor will still provide some of the |
| 337 | // desired gain change. |
| 338 | agc_->Reset(); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | new_level = std::min(new_level, max_level_); |
| 343 | if (new_level == level_) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | volume_callbacks_->SetMicVolume(new_level); |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 348 | RTC_DLOG(LS_INFO) << "[agc] voe_level=" << voe_level << ", " |
| 349 | << "level_=" << level_ << ", " |
| 350 | << "new_level=" << new_level; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 351 | level_ = new_level; |
| 352 | } |
| 353 | |
| 354 | void AgcManagerDirect::SetMaxLevel(int level) { |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 355 | RTC_DCHECK_GE(level, clipped_level_min_); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 356 | max_level_ = level; |
| 357 | // Scale the |kSurplusCompressionGain| linearly across the restricted |
| 358 | // level range. |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 359 | max_compression_gain_ = |
| 360 | kMaxCompressionGain + std::floor((1.f * kMaxMicLevel - max_level_) / |
| 361 | (kMaxMicLevel - clipped_level_min_) * |
| 362 | kSurplusCompressionGain + |
| 363 | 0.5f); |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 364 | RTC_DLOG(LS_INFO) << "[agc] max_level_=" << max_level_ |
| 365 | << ", max_compression_gain_=" << max_compression_gain_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void AgcManagerDirect::SetCaptureMuted(bool muted) { |
| 369 | if (capture_muted_ == muted) { |
| 370 | return; |
| 371 | } |
| 372 | capture_muted_ = muted; |
| 373 | |
| 374 | if (!muted) { |
| 375 | // When we unmute, we should reset things to be safe. |
| 376 | check_volume_on_next_process_ = true; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | float AgcManagerDirect::voice_probability() { |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 381 | return agc_->voice_probability(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | int AgcManagerDirect::CheckVolumeAndReset() { |
| 385 | int level = volume_callbacks_->GetMicVolume(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 386 | // Reasons for taking action at startup: |
| 387 | // 1) A person starting a call is expected to be heard. |
| 388 | // 2) Independent of interpretation of |level| == 0 we should raise it so the |
| 389 | // AGC can do its job properly. |
| 390 | if (level == 0 && !startup_) { |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 391 | RTC_DLOG(LS_INFO) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 392 | << "[agc] VolumeCallbacks returned level=0, taking no action."; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 393 | return 0; |
| 394 | } |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 395 | if (level < 0 || level > kMaxMicLevel) { |
| 396 | RTC_LOG(LS_ERROR) << "[agc] VolumeCallbacks returned an invalid level=" |
| 397 | << level; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 398 | return -1; |
| 399 | } |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 400 | RTC_DLOG(LS_INFO) << "[agc] Initial GetMicVolume()=" << level; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 401 | |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 402 | int minLevel = startup_ ? startup_min_level_ : min_mic_level_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 403 | if (level < minLevel) { |
| 404 | level = minLevel; |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 405 | RTC_DLOG(LS_INFO) << "[agc] Initial volume too low, raising to " << level; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 406 | volume_callbacks_->SetMicVolume(level); |
| 407 | } |
| 408 | agc_->Reset(); |
| 409 | level_ = level; |
| 410 | startup_ = false; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | // Requests the RMS error from AGC and distributes the required gain change |
| 415 | // between the digital compression stage and volume slider. We use the |
| 416 | // compressor first, providing a slack region around the current slider |
| 417 | // position to reduce movement. |
| 418 | // |
| 419 | // If the slider needs to be moved, we check first if the user has adjusted |
| 420 | // it, in which case we take no action and cache the updated level. |
| 421 | void AgcManagerDirect::UpdateGain() { |
| 422 | int rms_error = 0; |
| 423 | if (!agc_->GetRmsErrorDb(&rms_error)) { |
| 424 | // No error update ready. |
| 425 | return; |
| 426 | } |
| 427 | // The compressor will always add at least kMinCompressionGain. In effect, |
| 428 | // this adjusts our target gain upward by the same amount and rms_error |
| 429 | // needs to reflect that. |
| 430 | rms_error += kMinCompressionGain; |
| 431 | |
| 432 | // Handle as much error as possible with the compressor first. |
kwiberg | 0703856 | 2017-06-12 11:40:47 -0700 | [diff] [blame] | 433 | int raw_compression = |
| 434 | rtc::SafeClamp(rms_error, kMinCompressionGain, max_compression_gain_); |
| 435 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 436 | // Deemphasize the compression gain error. Move halfway between the current |
| 437 | // target and the newly received target. This serves to soften perceptible |
| 438 | // intra-talkspurt adjustments, at the cost of some adaptation speed. |
| 439 | if ((raw_compression == max_compression_gain_ && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 440 | target_compression_ == max_compression_gain_ - 1) || |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 441 | (raw_compression == kMinCompressionGain && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 442 | target_compression_ == kMinCompressionGain + 1)) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 443 | // Special case to allow the target to reach the endpoints of the |
| 444 | // compression range. The deemphasis would otherwise halt it at 1 dB shy. |
| 445 | target_compression_ = raw_compression; |
| 446 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 447 | target_compression_ = |
| 448 | (raw_compression - target_compression_) / 2 + target_compression_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Residual error will be handled by adjusting the volume slider. Use the |
| 452 | // raw rather than deemphasized compression here as we would otherwise |
| 453 | // shrink the amount of slack the compressor provides. |
kwiberg | 0703856 | 2017-06-12 11:40:47 -0700 | [diff] [blame] | 454 | const int residual_gain = |
| 455 | rtc::SafeClamp(rms_error - raw_compression, -kMaxResidualGainChange, |
| 456 | kMaxResidualGainChange); |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 457 | RTC_DLOG(LS_INFO) << "[agc] rms_error=" << rms_error |
| 458 | << ", target_compression=" << target_compression_ |
| 459 | << ", residual_gain=" << residual_gain; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 460 | if (residual_gain == 0) |
| 461 | return; |
| 462 | |
henrik.lundin | 3edc7f0 | 2016-11-24 01:42:46 -0800 | [diff] [blame] | 463 | int old_level = level_; |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 464 | SetLevel(LevelFromGainError(residual_gain, level_, min_mic_level_)); |
henrik.lundin | 3edc7f0 | 2016-11-24 01:42:46 -0800 | [diff] [blame] | 465 | if (old_level != level_) { |
| 466 | // level_ was updated by SetLevel; log the new value. |
henrik.lundin | 45bb513 | 2016-12-06 04:28:04 -0800 | [diff] [blame] | 467 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.AgcSetLevel", level_, 1, |
| 468 | kMaxMicLevel, 50); |
Alex Loiko | 99f1e0d | 2018-07-19 16:39:39 +0200 | [diff] [blame] | 469 | // Reset the AGC since the level has changed. |
| 470 | agc_->Reset(); |
henrik.lundin | 3edc7f0 | 2016-11-24 01:42:46 -0800 | [diff] [blame] | 471 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void AgcManagerDirect::UpdateCompressor() { |
Alex Loiko | f3122e0 | 2018-08-10 14:43:51 +0200 | [diff] [blame] | 475 | calls_since_last_gain_log_++; |
| 476 | if (calls_since_last_gain_log_ == 100) { |
| 477 | calls_since_last_gain_log_ = 0; |
| 478 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc.DigitalGainApplied", |
| 479 | compression_, 0, kMaxCompressionGain, |
| 480 | kMaxCompressionGain + 1); |
| 481 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 482 | if (compression_ == target_compression_) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | // Adapt the compression gain slowly towards the target, in order to avoid |
| 487 | // highly perceptible changes. |
| 488 | if (target_compression_ > compression_) { |
| 489 | compression_accumulator_ += kCompressionGainStep; |
| 490 | } else { |
| 491 | compression_accumulator_ -= kCompressionGainStep; |
| 492 | } |
| 493 | |
| 494 | // The compressor accepts integer gains in dB. Adjust the gain when |
| 495 | // we've come within half a stepsize of the nearest integer. (We don't |
| 496 | // check for equality due to potential floating point imprecision). |
| 497 | int new_compression = compression_; |
| 498 | int nearest_neighbor = std::floor(compression_accumulator_ + 0.5); |
| 499 | if (std::fabs(compression_accumulator_ - nearest_neighbor) < |
| 500 | kCompressionGainStep / 2) { |
| 501 | new_compression = nearest_neighbor; |
| 502 | } |
| 503 | |
| 504 | // Set the new compression gain. |
| 505 | if (new_compression != compression_) { |
Alex Loiko | f3122e0 | 2018-08-10 14:43:51 +0200 | [diff] [blame] | 506 | RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc.DigitalGainUpdated", |
| 507 | new_compression, 0, kMaxCompressionGain, |
| 508 | kMaxCompressionGain + 1); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 509 | compression_ = new_compression; |
| 510 | compression_accumulator_ = new_compression; |
| 511 | if (gctrl_->set_compression_gain_db(compression_) != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 512 | RTC_LOG(LS_ERROR) << "set_compression_gain_db(" << compression_ |
| 513 | << ") failed."; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | } // namespace webrtc |