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..e0bc715c401383621d5f1e7757f8fb4a9216f75b |
| --- /dev/null |
| +++ b/webrtc/modules/audio_processing/level_controller/gain_selector.cc |
| @@ -0,0 +1,77 @@ |
| +/* |
| + * 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_ = rtc::CheckedDivExact(sample_rate_hz, 100); |
| + highly_nonstationary_signal_hold_counter_ = 0; |
| +} |
| + |
| +// Chooses the gain to apply by the level controller such that |
| +// 1) The level of the stationary noise does not exceed |
| +// a predefined threshold. |
| +// 2) The gain does not exceed the gain that has been found |
| +// to saturate the signal. |
| +// 3) The peak level achieves the target peak level. |
| +// 4) The gain is not below 1. |
| +// 4) The gain is 1 if the signal has been classified as stationary |
| +// for a long time. |
| +// 5) The gain is not above the maximum gain. |
| +float GainSelector::GetNewGain(float peak_level, |
| + float noise_energy, |
| + float saturating_gain, |
| + SignalClassifier::SignalType signal_type) { |
| + RTC_DCHECK_LT(0.f, peak_level); |
| + |
| + if (signal_type == SignalClassifier::SignalType::kHighlyNonStationary) { |
| + highly_nonstationary_signal_hold_counter_ = 10000; |
| + } else { |
| + highly_nonstationary_signal_hold_counter_ = |
| + std::max(0, highly_nonstationary_signal_hold_counter_ - 1); |
| + } |
| + |
| + float desired_gain; |
| + if (highly_nonstationary_signal_hold_counter_ > 0) { |
|
hlundin-webrtc
2016/06/28 11:29:01
Please, explain this logic in a comment.
peah-webrtc
2016/06/28 22:19:37
Done.
|
| + 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); |
| + } |
| + } else { |
| + desired_gain = 1.0f; |
| + } |
| + |
| + 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 |