OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/agc/agc_manager_direct.h" | 11 #include "webrtc/modules/audio_processing/agc/agc_manager_direct.h" |
12 | 12 |
13 #include <cmath> | 13 #include <cmath> |
14 | 14 |
15 #ifdef WEBRTC_AGC_DEBUG_DUMP | 15 #ifdef WEBRTC_AGC_DEBUG_DUMP |
16 #include <cstdio> | 16 #include <cstdio> |
17 #endif | 17 #endif |
18 | 18 |
19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 21 #include "webrtc/base/safe_minmax.h" |
21 #include "webrtc/modules/audio_processing/agc/gain_map_internal.h" | 22 #include "webrtc/modules/audio_processing/agc/gain_map_internal.h" |
22 #include "webrtc/modules/audio_processing/gain_control_impl.h" | 23 #include "webrtc/modules/audio_processing/gain_control_impl.h" |
23 #include "webrtc/modules/include/module_common_types.h" | 24 #include "webrtc/modules/include/module_common_types.h" |
24 #include "webrtc/system_wrappers/include/metrics.h" | 25 #include "webrtc/system_wrappers/include/metrics.h" |
25 | 26 |
26 namespace webrtc { | 27 namespace webrtc { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // Amount the microphone level is lowered with every clipping event. | 31 // Amount the microphone level is lowered with every clipping event. |
(...skipping 18 matching lines...) Expand all Loading... |
49 const int kMinMicLevel = 12; | 50 const int kMinMicLevel = 12; |
50 | 51 |
51 // Prevent very large microphone level changes. | 52 // Prevent very large microphone level changes. |
52 const int kMaxResidualGainChange = 15; | 53 const int kMaxResidualGainChange = 15; |
53 | 54 |
54 // Maximum additional gain allowed to compensate for microphone level | 55 // Maximum additional gain allowed to compensate for microphone level |
55 // restrictions from clipping events. | 56 // restrictions from clipping events. |
56 const int kSurplusCompressionGain = 6; | 57 const int kSurplusCompressionGain = 6; |
57 | 58 |
58 int ClampLevel(int mic_level) { | 59 int ClampLevel(int mic_level) { |
59 return std::min(std::max(kMinMicLevel, mic_level), kMaxMicLevel); | 60 return rtc::SafeClamp(mic_level, kMinMicLevel, kMaxMicLevel); |
60 } | 61 } |
61 | 62 |
62 int LevelFromGainError(int gain_error, int level) { | 63 int LevelFromGainError(int gain_error, int level) { |
63 RTC_DCHECK_GE(level, 0); | 64 RTC_DCHECK_GE(level, 0); |
64 RTC_DCHECK_LE(level, kMaxMicLevel); | 65 RTC_DCHECK_LE(level, kMaxMicLevel); |
65 if (gain_error == 0) { | 66 if (gain_error == 0) { |
66 return level; | 67 return level; |
67 } | 68 } |
68 // TODO(ajm): Could be made more efficient with a binary search. | 69 // TODO(ajm): Could be made more efficient with a binary search. |
69 int new_level = level; | 70 int new_level = level; |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 if (!agc_->GetRmsErrorDb(&rms_error)) { | 374 if (!agc_->GetRmsErrorDb(&rms_error)) { |
374 // No error update ready. | 375 // No error update ready. |
375 return; | 376 return; |
376 } | 377 } |
377 // The compressor will always add at least kMinCompressionGain. In effect, | 378 // The compressor will always add at least kMinCompressionGain. In effect, |
378 // this adjusts our target gain upward by the same amount and rms_error | 379 // this adjusts our target gain upward by the same amount and rms_error |
379 // needs to reflect that. | 380 // needs to reflect that. |
380 rms_error += kMinCompressionGain; | 381 rms_error += kMinCompressionGain; |
381 | 382 |
382 // Handle as much error as possible with the compressor first. | 383 // Handle as much error as possible with the compressor first. |
383 int raw_compression = std::max(std::min(rms_error, max_compression_gain_), | 384 int raw_compression = |
384 kMinCompressionGain); | 385 rtc::SafeClamp(rms_error, kMinCompressionGain, max_compression_gain_); |
| 386 |
385 // Deemphasize the compression gain error. Move halfway between the current | 387 // Deemphasize the compression gain error. Move halfway between the current |
386 // target and the newly received target. This serves to soften perceptible | 388 // target and the newly received target. This serves to soften perceptible |
387 // intra-talkspurt adjustments, at the cost of some adaptation speed. | 389 // intra-talkspurt adjustments, at the cost of some adaptation speed. |
388 if ((raw_compression == max_compression_gain_ && | 390 if ((raw_compression == max_compression_gain_ && |
389 target_compression_ == max_compression_gain_ - 1) || | 391 target_compression_ == max_compression_gain_ - 1) || |
390 (raw_compression == kMinCompressionGain && | 392 (raw_compression == kMinCompressionGain && |
391 target_compression_ == kMinCompressionGain + 1)) { | 393 target_compression_ == kMinCompressionGain + 1)) { |
392 // Special case to allow the target to reach the endpoints of the | 394 // Special case to allow the target to reach the endpoints of the |
393 // compression range. The deemphasis would otherwise halt it at 1 dB shy. | 395 // compression range. The deemphasis would otherwise halt it at 1 dB shy. |
394 target_compression_ = raw_compression; | 396 target_compression_ = raw_compression; |
395 } else { | 397 } else { |
396 target_compression_ = (raw_compression - target_compression_) / 2 | 398 target_compression_ = (raw_compression - target_compression_) / 2 |
397 + target_compression_; | 399 + target_compression_; |
398 } | 400 } |
399 | 401 |
400 // Residual error will be handled by adjusting the volume slider. Use the | 402 // Residual error will be handled by adjusting the volume slider. Use the |
401 // raw rather than deemphasized compression here as we would otherwise | 403 // raw rather than deemphasized compression here as we would otherwise |
402 // shrink the amount of slack the compressor provides. | 404 // shrink the amount of slack the compressor provides. |
403 int residual_gain = rms_error - raw_compression; | 405 const int residual_gain = |
404 residual_gain = std::min(std::max(residual_gain, -kMaxResidualGainChange), | 406 rtc::SafeClamp(rms_error - raw_compression, -kMaxResidualGainChange, |
405 kMaxResidualGainChange); | 407 kMaxResidualGainChange); |
406 LOG(LS_INFO) << "[agc] rms_error=" << rms_error << ", " | 408 LOG(LS_INFO) << "[agc] rms_error=" << rms_error << ", " |
407 << "target_compression=" << target_compression_ << ", " | 409 << "target_compression=" << target_compression_ << ", " |
408 << "residual_gain=" << residual_gain; | 410 << "residual_gain=" << residual_gain; |
409 if (residual_gain == 0) | 411 if (residual_gain == 0) |
410 return; | 412 return; |
411 | 413 |
412 int old_level = level_; | 414 int old_level = level_; |
413 SetLevel(LevelFromGainError(residual_gain, level_)); | 415 SetLevel(LevelFromGainError(residual_gain, level_)); |
414 if (old_level != level_) { | 416 if (old_level != level_) { |
415 // level_ was updated by SetLevel; log the new value. | 417 // level_ was updated by SetLevel; log the new value. |
(...skipping 30 matching lines...) Expand all Loading... |
446 compression_ = new_compression; | 448 compression_ = new_compression; |
447 compression_accumulator_ = new_compression; | 449 compression_accumulator_ = new_compression; |
448 if (gctrl_->set_compression_gain_db(compression_) != 0) { | 450 if (gctrl_->set_compression_gain_db(compression_) != 0) { |
449 LOG(LS_ERROR) << "set_compression_gain_db(" << compression_ | 451 LOG(LS_ERROR) << "set_compression_gain_db(" << compression_ |
450 << ") failed."; | 452 << ") failed."; |
451 } | 453 } |
452 } | 454 } |
453 } | 455 } |
454 | 456 |
455 } // namespace webrtc | 457 } // namespace webrtc |
OLD | NEW |