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