OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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/level_controller/gain_selector.h" | 11 #include "webrtc/modules/audio_processing/level_controller/gain_selector.h" |
12 | 12 |
13 #include <math.h> | 13 #include <math.h> |
14 #include <algorithm> | 14 #include <algorithm> |
15 | 15 |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 #include "webrtc/modules/audio_processing/include/audio_processing.h" | 17 #include "webrtc/modules/audio_processing/include/audio_processing.h" |
18 #include "webrtc/modules/audio_processing/level_controller/lc_constants.h" | 18 #include "webrtc/modules/audio_processing/level_controller/level_controller_cons
tants.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace webrtc { |
21 | 21 |
22 GainSelector::GainSelector() { | 22 GainSelector::GainSelector() { |
23 Initialize(AudioProcessing::kSampleRate48kHz); | 23 Initialize(AudioProcessing::kSampleRate48kHz); |
24 } | 24 } |
25 | 25 |
26 void GainSelector::Initialize(int sample_rate_hz) { | 26 void GainSelector::Initialize(int sample_rate_hz) { |
27 gain_ = 1.f; | 27 gain_ = 1.f; |
28 frame_length_ = rtc::CheckedDivExact(sample_rate_hz, 100); | 28 frame_length_ = rtc::CheckedDivExact(sample_rate_hz, 100); |
29 highly_nonstationary_signal_hold_counter_ = 0; | 29 highly_nonstationary_signal_hold_counter_ = 0; |
30 } | 30 } |
31 | 31 |
32 // Chooses the gain to apply by the level controller such that | 32 // Chooses the gain to apply by the level controller such that |
33 // 1) The level of the stationary noise does not exceed | 33 // 1) The level of the stationary noise does not exceed |
34 // a predefined threshold. | 34 // a predefined threshold. |
35 // 2) The gain does not exceed the gain that has been found | 35 // 2) The gain does not exceed the gain that has been found |
36 // to saturate the signal. | 36 // to saturate the signal. |
37 // 3) The peak level achieves the target peak level. | 37 // 3) The peak level achieves the target peak level. |
38 // 4) The gain is not below 1. | 38 // 4) The gain is not below 1. |
39 // 4) The gain is 1 if the signal has been classified as stationary | 39 // 4) The gain is 1 if the signal has been classified as stationary |
40 // for a long time. | 40 // for a long time. |
41 // 5) The gain is not above the maximum gain. | 41 // 5) The gain is not above the maximum gain. |
42 float GainSelector::GetNewGain(float peak_level, | 42 float GainSelector::GetNewGain(float peak_level, |
43 float noise_energy, | 43 float noise_energy, |
44 float saturating_gain, | 44 float saturating_gain, |
| 45 bool gain_jumpstart, |
45 SignalClassifier::SignalType signal_type) { | 46 SignalClassifier::SignalType signal_type) { |
46 RTC_DCHECK_LT(0.f, peak_level); | 47 RTC_DCHECK_LT(0.f, peak_level); |
47 | 48 |
48 if (signal_type == SignalClassifier::SignalType::kHighlyNonStationary) { | 49 if (signal_type == SignalClassifier::SignalType::kHighlyNonStationary || |
| 50 gain_jumpstart) { |
49 highly_nonstationary_signal_hold_counter_ = 100; | 51 highly_nonstationary_signal_hold_counter_ = 100; |
50 } else { | 52 } else { |
51 highly_nonstationary_signal_hold_counter_ = | 53 highly_nonstationary_signal_hold_counter_ = |
52 std::max(0, highly_nonstationary_signal_hold_counter_ - 1); | 54 std::max(0, highly_nonstationary_signal_hold_counter_ - 1); |
53 } | 55 } |
54 | 56 |
55 float desired_gain; | 57 float desired_gain; |
56 if (highly_nonstationary_signal_hold_counter_ > 0) { | 58 if (highly_nonstationary_signal_hold_counter_ > 0) { |
57 // Compute a desired gain that ensures that the peak level is amplified to | 59 // Compute a desired gain that ensures that the peak level is amplified to |
58 // the target level. | 60 // the target level. |
(...skipping 17 matching lines...) Expand all Loading... |
76 // Limit the gain to not exceed the maximum and the saturating gains, and to | 78 // Limit the gain to not exceed the maximum and the saturating gains, and to |
77 // ensure that the lowest possible gain is 1. | 79 // ensure that the lowest possible gain is 1. |
78 gain_ = std::min(gain_, saturating_gain); | 80 gain_ = std::min(gain_, saturating_gain); |
79 gain_ = std::min(gain_, kMaxLcGain); | 81 gain_ = std::min(gain_, kMaxLcGain); |
80 gain_ = std::max(gain_, 1.f); | 82 gain_ = std::max(gain_, 1.f); |
81 | 83 |
82 return gain_; | 84 return gain_; |
83 } | 85 } |
84 | 86 |
85 } // namespace webrtc | 87 } // namespace webrtc |
OLD | NEW |