Chromium Code Reviews| Index: webrtc/modules/audio_processing/level_controller/gain_selector.cc |
| diff --git a/webrtc/modules/audio_processing/level_controller/gain_selector.cc b/webrtc/modules/audio_processing/level_controller/gain_selector.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..de942708c34e89fa510289df4f327259e0587e34 |
| --- /dev/null |
| +++ b/webrtc/modules/audio_processing/level_controller/gain_selector.cc |
| @@ -0,0 +1,61 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/audio_processing/level_controller/gain_selector.h" |
| + |
| +#include <math.h> |
| +#include <algorithm> |
| + |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/modules/audio_processing/include/audio_processing.h" |
| +#include "webrtc/modules/audio_processing/level_controller/lc_constants.h" |
| + |
| +namespace webrtc { |
| + |
| +GainSelector::GainSelector() { |
| + Initialize(AudioProcessing::kSampleRate48kHz); |
| +} |
| + |
| +void GainSelector::Initialize(int sample_rate_hz) { |
| + gain_ = 1.f; |
| + frame_length_ = sample_rate_hz / 100; |
|
hlundin-webrtc
2016/06/27 11:21:14
rtc::CheckedDivExact
peah-webrtc
2016/06/27 22:51:47
Great!
Done.
|
| +} |
| + |
| +/* Chooses the gain to apply by the level controller such that |
|
hlundin-webrtc
2016/06/27 11:21:14
Use // style comments.
peah-webrtc
2016/06/27 22:51:48
Done.
|
| + 1) The level of the stationary noise does not exceed |
| + a predefined threshold. |
| + 2) The gain does not exceed the that has been deemed |
|
hlundin-webrtc
2016/06/27 11:21:14
deemed to -> found to
hlundin-webrtc
2016/06/27 11:21:14
"the that" ?
peah-webrtc
2016/06/27 22:51:47
Done.
peah-webrtc
2016/06/27 22:51:47
Done.
|
| + to saturate the signal. |
| + 3) Such that the peak level achieves the target peak level. |
|
hlundin-webrtc
2016/06/27 11:21:14
Skip "Such that"; you already have that before the
peah-webrtc
2016/06/27 22:51:47
Done.
|
| + 4) The gain is not below 1. |
| + 5) The gain is not above the maximum gain. */ |
| +float GainSelector::GetNewGain(float peak_level, |
| + float noise_energy, |
| + float saturating_gain) { |
| + float desired_gain = gain_; |
| + |
| + RTC_DCHECK_LT(0.f, peak_level); |
|
hlundin-webrtc
2016/06/27 11:21:14
This will fail if peak_level == 0.f. Is this inten
peah-webrtc
2016/06/27 22:51:47
Yes, the peak level detector should ensure a minim
hlundin-webrtc
2016/06/28 11:29:00
Acknowledged.
peah-webrtc
2016/06/28 22:19:37
Acknowledged.
|
| + desired_gain = kTargetLcPeakLevel / peak_level; |
| + |
| + float max_noise_energy = kMaxLcNoisePower * frame_length_; |
| + if (noise_energy * desired_gain * desired_gain > max_noise_energy) { |
| + RTC_DCHECK_LE(0.f, noise_energy); |
| + desired_gain = sqrtf(max_noise_energy / noise_energy); |
| + } |
| + |
| + gain_ += 0.2f * (desired_gain - gain_); |
| + gain_ = std::min(gain_, saturating_gain); |
| + gain_ = std::min(gain_, kMaxLcGain); |
| + gain_ = std::max(gain_, 1.f); |
| + |
| + return gain_; |
| +} |
| + |
| +} // namespace webrtc |