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 <cassert> | |
14 #include <cmath> | 13 #include <cmath> |
15 | 14 |
16 #ifdef WEBRTC_AGC_DEBUG_DUMP | 15 #ifdef WEBRTC_AGC_DEBUG_DUMP |
17 #include <cstdio> | 16 #include <cstdio> |
18 #endif | 17 #endif |
19 | 18 |
| 19 #include "webrtc/base/checks.h" |
20 #include "webrtc/modules/audio_processing/agc/gain_map_internal.h" | 20 #include "webrtc/modules/audio_processing/agc/gain_map_internal.h" |
21 #include "webrtc/modules/audio_processing/gain_control_impl.h" | 21 #include "webrtc/modules/audio_processing/gain_control_impl.h" |
22 #include "webrtc/modules/include/module_common_types.h" | 22 #include "webrtc/modules/include/module_common_types.h" |
23 #include "webrtc/system_wrappers/include/logging.h" | 23 #include "webrtc/system_wrappers/include/logging.h" |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Lowest the microphone level can be lowered due to clipping. | 29 // Lowest the microphone level can be lowered due to clipping. |
(...skipping 24 matching lines...) Expand all Loading... |
54 | 54 |
55 // Maximum additional gain allowed to compensate for microphone level | 55 // Maximum additional gain allowed to compensate for microphone level |
56 // restrictions from clipping events. | 56 // restrictions from clipping events. |
57 const int kSurplusCompressionGain = 6; | 57 const int kSurplusCompressionGain = 6; |
58 | 58 |
59 int ClampLevel(int mic_level) { | 59 int ClampLevel(int mic_level) { |
60 return std::min(std::max(kMinMicLevel, mic_level), kMaxMicLevel); | 60 return std::min(std::max(kMinMicLevel, mic_level), kMaxMicLevel); |
61 } | 61 } |
62 | 62 |
63 int LevelFromGainError(int gain_error, int level) { | 63 int LevelFromGainError(int gain_error, int level) { |
64 assert(level >= 0 && level <= kMaxMicLevel); | 64 RTC_DCHECK_GE(level, 0); |
| 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; |
70 if (gain_error > 0) { | 71 if (gain_error > 0) { |
71 while (kGainMap[new_level] - kGainMap[level] < gain_error && | 72 while (kGainMap[new_level] - kGainMap[level] < gain_error && |
72 new_level < kMaxMicLevel) { | 73 new_level < kMaxMicLevel) { |
73 ++new_level; | 74 ++new_level; |
74 } | 75 } |
75 } else { | 76 } else { |
76 while (kGainMap[new_level] - kGainMap[level] > gain_error && | 77 while (kGainMap[new_level] - kGainMap[level] > gain_error && |
77 new_level > kMinMicLevel) { | 78 new_level > kMinMicLevel) { |
78 --new_level; | 79 --new_level; |
79 } | 80 } |
80 } | 81 } |
81 return new_level; | 82 return new_level; |
82 } | 83 } |
83 | 84 |
84 } // namespace | 85 } // namespace |
85 | 86 |
86 // Facility for dumping debug audio files. All methods are no-ops in the | 87 // Facility for dumping debug audio files. All methods are no-ops in the |
87 // default case where WEBRTC_AGC_DEBUG_DUMP is undefined. | 88 // default case where WEBRTC_AGC_DEBUG_DUMP is undefined. |
88 class DebugFile { | 89 class DebugFile { |
89 #ifdef WEBRTC_AGC_DEBUG_DUMP | 90 #ifdef WEBRTC_AGC_DEBUG_DUMP |
90 public: | 91 public: |
91 explicit DebugFile(const char* filename) | 92 explicit DebugFile(const char* filename) |
92 : file_(fopen(filename, "wb")) { | 93 : file_(fopen(filename, "wb")) { |
93 assert(file_); | 94 RTC_DCHECK(file_); |
94 } | 95 } |
95 ~DebugFile() { | 96 ~DebugFile() { |
96 fclose(file_); | 97 fclose(file_); |
97 } | 98 } |
98 void Write(const int16_t* data, size_t length_samples) { | 99 void Write(const int16_t* data, size_t length_samples) { |
99 fwrite(data, 1, length_samples * sizeof(int16_t), file_); | 100 fwrite(data, 1, length_samples * sizeof(int16_t), file_); |
100 } | 101 } |
101 private: | 102 private: |
102 FILE* file_; | 103 FILE* file_; |
103 #else | 104 #else |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 239 |
239 if (check_volume_on_next_process_) { | 240 if (check_volume_on_next_process_) { |
240 check_volume_on_next_process_ = false; | 241 check_volume_on_next_process_ = false; |
241 // We have to wait until the first process call to check the volume, | 242 // We have to wait until the first process call to check the volume, |
242 // because Chromium doesn't guarantee it to be valid any earlier. | 243 // because Chromium doesn't guarantee it to be valid any earlier. |
243 CheckVolumeAndReset(); | 244 CheckVolumeAndReset(); |
244 } | 245 } |
245 | 246 |
246 if (agc_->Process(audio, length, sample_rate_hz) != 0) { | 247 if (agc_->Process(audio, length, sample_rate_hz) != 0) { |
247 LOG(LS_ERROR) << "Agc::Process failed"; | 248 LOG(LS_ERROR) << "Agc::Process failed"; |
248 assert(false); | 249 RTC_NOTREACHED(); |
249 } | 250 } |
250 | 251 |
251 UpdateGain(); | 252 UpdateGain(); |
252 UpdateCompressor(); | 253 UpdateCompressor(); |
253 | 254 |
254 file_postproc_->Write(audio, length); | 255 file_postproc_->Write(audio, length); |
255 } | 256 } |
256 | 257 |
257 void AgcManagerDirect::SetLevel(int new_level) { | 258 void AgcManagerDirect::SetLevel(int new_level) { |
258 int voe_level = volume_callbacks_->GetMicVolume(); | 259 int voe_level = volume_callbacks_->GetMicVolume(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 } | 291 } |
291 | 292 |
292 volume_callbacks_->SetMicVolume(new_level); | 293 volume_callbacks_->SetMicVolume(new_level); |
293 LOG(LS_INFO) << "[agc] voe_level=" << voe_level << ", " | 294 LOG(LS_INFO) << "[agc] voe_level=" << voe_level << ", " |
294 << "level_=" << level_ << ", " | 295 << "level_=" << level_ << ", " |
295 << "new_level=" << new_level; | 296 << "new_level=" << new_level; |
296 level_ = new_level; | 297 level_ = new_level; |
297 } | 298 } |
298 | 299 |
299 void AgcManagerDirect::SetMaxLevel(int level) { | 300 void AgcManagerDirect::SetMaxLevel(int level) { |
300 assert(level >= kClippedLevelMin); | 301 RTC_DCHECK_GE(level, kClippedLevelMin); |
301 max_level_ = level; | 302 max_level_ = level; |
302 // Scale the |kSurplusCompressionGain| linearly across the restricted | 303 // Scale the |kSurplusCompressionGain| linearly across the restricted |
303 // level range. | 304 // level range. |
304 max_compression_gain_ = kMaxCompressionGain + std::floor( | 305 max_compression_gain_ = kMaxCompressionGain + std::floor( |
305 (1.f * kMaxMicLevel - max_level_) / (kMaxMicLevel - kClippedLevelMin) * | 306 (1.f * kMaxMicLevel - max_level_) / (kMaxMicLevel - kClippedLevelMin) * |
306 kSurplusCompressionGain + 0.5f); | 307 kSurplusCompressionGain + 0.5f); |
307 LOG(LS_INFO) << "[agc] max_level_=" << max_level_ | 308 LOG(LS_INFO) << "[agc] max_level_=" << max_level_ |
308 << ", max_compression_gain_=" << max_compression_gain_; | 309 << ", max_compression_gain_=" << max_compression_gain_; |
309 } | 310 } |
310 | 311 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 compression_ = new_compression; | 435 compression_ = new_compression; |
435 compression_accumulator_ = new_compression; | 436 compression_accumulator_ = new_compression; |
436 if (gctrl_->set_compression_gain_db(compression_) != 0) { | 437 if (gctrl_->set_compression_gain_db(compression_) != 0) { |
437 LOG(LS_ERROR) << "set_compression_gain_db(" << compression_ | 438 LOG(LS_ERROR) << "set_compression_gain_db(" << compression_ |
438 << ") failed."; | 439 << ") failed."; |
439 } | 440 } |
440 } | 441 } |
441 } | 442 } |
442 | 443 |
443 } // namespace webrtc | 444 } // namespace webrtc |
OLD | NEW |