OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/modules/audio_processing/level_controller/noise_level_estimator .h" | |
12 | |
13 #include <algorithm> | |
14 | |
15 #include "webrtc/modules/audio_processing/audio_buffer.h" | |
16 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" | |
17 | |
18 namespace webrtc { | |
19 | |
20 NoiseLevelEstimator::NoiseLevelEstimator() { | |
21 Initialize(AudioProcessing::kSampleRate48kHz); | |
22 } | |
23 | |
24 NoiseLevelEstimator::~NoiseLevelEstimator() {} | |
25 | |
26 void NoiseLevelEstimator::Initialize(int sample_rate_hz) { | |
27 noise_energy_ = 1.f; | |
28 first_update_ = true; | |
29 min_noise_energy_ = sample_rate_hz * 2.f * 2.f / 100.f; | |
30 noise_energy_hold_counter_ = 0; | |
31 } | |
32 | |
33 float NoiseLevelEstimator::Analyze(SignalClassifier::SignalType signal_type, | |
34 float frame_energy) { | |
35 if (frame_energy > 0) { | |
hlundin-webrtc
2016/06/27 11:21:16
You can save yourself one level of indentation on
peah-webrtc
2016/06/27 22:51:49
Good point!
Done.
| |
36 if (first_update_) { | |
37 // Initialize the noise energy to the frame energy. | |
38 noise_energy_ = frame_energy; | |
hlundin-webrtc
2016/06/27 11:21:16
first_update_ = false;
return noise_energy_ = std:
peah-webrtc
2016/06/27 22:51:49
Nice!
Done.
| |
39 } else { | |
40 // Update the noise estimate in a minimum statistics-type manner. | |
41 if (signal_type == SignalClassifier::SignalType::kStationary) { | |
42 if (frame_energy > noise_energy_) { | |
43 // Leak the estimate upwards towards the frame energy if no recent | |
44 // downward update. | |
45 (noise_energy_hold_counter_) = | |
hlundin-webrtc
2016/06/27 11:21:16
Why the parentheses around the lhs?
peah-webrtc
2016/06/27 22:51:49
That is a leftover from a previous code construct.
| |
46 std::max(noise_energy_hold_counter_ - 1, 0); | |
47 | |
48 if (noise_energy_hold_counter_ == 0) { | |
49 noise_energy_ = std::min(noise_energy_ * 1.01f, frame_energy); | |
50 } | |
51 } else { | |
52 // Update smothly downwards with a limited maximum update magnitude. | |
hlundin-webrtc
2016/06/27 11:21:16
smothly -> smoothly
peah-webrtc
2016/06/27 22:51:49
Done.
| |
53 noise_energy_ = | |
54 std::max(noise_energy_ * 0.9f, | |
55 noise_energy_ + 0.05f * (frame_energy - noise_energy_)); | |
56 noise_energy_hold_counter_ = 1000; | |
57 } | |
58 } else { | |
59 // For a non-stationary signal, leak the estimate downwards in order to | |
60 // avoid estimate locking due to incorrect signal classification. | |
61 noise_energy_ = noise_energy_ * 0.99f; | |
62 } | |
63 } | |
64 | |
65 // Ensure a minimum of the estimate. | |
66 noise_energy_ = std::max(noise_energy_, min_noise_energy_); | |
67 | |
68 first_update_ = false; | |
hlundin-webrtc
2016/06/27 11:21:16
Delete.
peah-webrtc
2016/06/27 22:51:49
Done.
| |
69 } | |
70 | |
71 return noise_energy_; | |
72 } | |
73 | |
74 } // namespace webrtc | |
OLD | NEW |