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/peak_level_estimator. h" | 11 #include "webrtc/modules/audio_processing/level_controller/peak_level_estimator. h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "webrtc/modules/audio_processing/audio_buffer.h" | 15 #include "webrtc/modules/audio_processing/audio_buffer.h" |
16 #include "webrtc/modules/audio_processing/level_controller/lc_constants.h" | |
17 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" | 16 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
18 | 17 |
19 namespace webrtc { | 18 namespace webrtc { |
19 namespace { | |
20 | |
21 const float kMinLevel = 30.f; | |
22 const float kMaxLevel = 32768.f; | |
23 | |
24 } // namespace | |
20 | 25 |
21 PeakLevelEstimator::PeakLevelEstimator() { | 26 PeakLevelEstimator::PeakLevelEstimator() { |
22 Initialize(); | 27 Initialize(); |
23 } | 28 } |
24 | 29 |
25 PeakLevelEstimator::~PeakLevelEstimator() {} | 30 PeakLevelEstimator::~PeakLevelEstimator() {} |
26 | 31 |
27 void PeakLevelEstimator::Initialize() { | 32 void PeakLevelEstimator::Initialize() { |
28 peak_level_ = kTargetLcPeakLevel; | 33 peak_level_ = initial_peak_level_; |
29 hold_counter_ = 0; | 34 hold_counter_ = 0; |
30 initialization_phase_ = true; | 35 initialization_phase_ = true; |
31 } | 36 } |
32 | 37 |
38 void PeakLevelEstimator::SetInitialPeakLevel(float level) { | |
39 RTC_DCHECK_LE(0.f, level); | |
40 RTC_DCHECK_GE(32768.f, level); | |
the sun
2016/08/22 11:58:22
Why not use the constants here?
peah-webrtc
2016/08/22 12:09:04
That is a good question:
The thinking was like th
the sun
2016/08/22 14:18:39
Ok that makes sense, but when I think about it, 0
peah-webrtc
2016/08/23 05:33:44
You are right! Excellent point.
I changed the AP
the sun
2016/08/23 06:23:38
Nice, I would have gone for range 0-1.0 myself, bu
peah-webrtc
2016/08/26 10:47:13
That is correct, this is not something that will h
| |
41 | |
42 // Limit the supplied level to the level range used internally. | |
43 initial_peak_level_ = std::min(std::max(level, kMinLevel), kMaxLevel); | |
the sun
2016/08/22 11:58:22
Do you really need to DCHECK as well as clamp?
peah-webrtc
2016/08/22 12:09:04
That is a good question. Please see above response
the sun
2016/08/22 14:18:39
Right. I think generally APIs should aggressively
peah-webrtc
2016/08/23 05:33:44
True, I removed the std::min.
Done.
| |
44 Initialize(); | |
45 } | |
46 | |
33 float PeakLevelEstimator::Analyze(SignalClassifier::SignalType signal_type, | 47 float PeakLevelEstimator::Analyze(SignalClassifier::SignalType signal_type, |
34 float frame_peak_level) { | 48 float frame_peak_level) { |
35 if (frame_peak_level == 0) { | 49 if (frame_peak_level == 0) { |
36 RTC_DCHECK_LE(30.f, peak_level_); | 50 RTC_DCHECK_LE(30.f, peak_level_); |
the sun
2016/08/22 11:58:22
Should this be kMinLevel?
peah-webrtc
2016/08/23 05:33:44
Done.
| |
37 return peak_level_; | 51 return peak_level_; |
38 } | 52 } |
39 | 53 |
40 if (peak_level_ < frame_peak_level) { | 54 if (peak_level_ < frame_peak_level) { |
41 // Smoothly update the estimate upwards when the frame peak level is | 55 // Smoothly update the estimate upwards when the frame peak level is |
42 // higher than the estimate. | 56 // higher than the estimate. |
43 peak_level_ += 0.1f * (frame_peak_level - peak_level_); | 57 peak_level_ += 0.1f * (frame_peak_level - peak_level_); |
44 hold_counter_ = 100; | 58 hold_counter_ = 100; |
45 initialization_phase_ = false; | 59 initialization_phase_ = false; |
46 } else { | 60 } else { |
47 hold_counter_ = std::max(0, hold_counter_ - 1); | 61 hold_counter_ = std::max(0, hold_counter_ - 1); |
48 | 62 |
49 // When the signal is highly non-stationary, update the estimate slowly | 63 // When the signal is highly non-stationary, update the estimate slowly |
50 // downwards if the estimate is lower than the frame peak level. | 64 // downwards if the estimate is lower than the frame peak level. |
51 if ((signal_type == SignalClassifier::SignalType::kHighlyNonStationary && | 65 if ((signal_type == SignalClassifier::SignalType::kHighlyNonStationary && |
52 hold_counter_ == 0) || | 66 hold_counter_ == 0) || |
53 initialization_phase_) { | 67 initialization_phase_) { |
54 peak_level_ = | 68 peak_level_ = |
55 std::max(peak_level_ + 0.01f * (frame_peak_level - peak_level_), | 69 std::max(peak_level_ + 0.01f * (frame_peak_level - peak_level_), |
56 peak_level_ * 0.995f); | 70 peak_level_ * 0.995f); |
57 } | 71 } |
58 } | 72 } |
59 | 73 |
60 peak_level_ = std::max(peak_level_, 30.f); | 74 peak_level_ = std::max(peak_level_, 30.f); |
61 | 75 |
62 return peak_level_; | 76 return peak_level_; |
63 } | 77 } |
64 | 78 |
65 } // namespace webrtc | 79 } // namespace webrtc |
OLD | NEW |