| 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" |
| 16 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" | 17 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| 17 | 18 |
| 18 namespace webrtc { | 19 namespace webrtc { |
| 19 | 20 |
| 20 PeakLevelEstimator::PeakLevelEstimator() { | 21 PeakLevelEstimator::PeakLevelEstimator() { |
| 21 Initialize(); | 22 Initialize(); |
| 22 } | 23 } |
| 23 | 24 |
| 24 PeakLevelEstimator::~PeakLevelEstimator() {} | 25 PeakLevelEstimator::~PeakLevelEstimator() {} |
| 25 | 26 |
| 26 void PeakLevelEstimator::Initialize() { | 27 void PeakLevelEstimator::Initialize() { |
| 27 peak_level_ = 1000.f; | 28 peak_level_ = kTargetLcPeakLevel; |
| 28 hold_counter_ = 0; | 29 hold_counter_ = 0; |
| 30 initialization_phase_ = true; |
| 29 } | 31 } |
| 30 | 32 |
| 31 float PeakLevelEstimator::Analyze(SignalClassifier::SignalType signal_type, | 33 float PeakLevelEstimator::Analyze(SignalClassifier::SignalType signal_type, |
| 32 float frame_peak_level) { | 34 float frame_peak_level) { |
| 33 if (frame_peak_level > 0) { | 35 if (frame_peak_level == 0) { |
| 34 if (peak_level_ < frame_peak_level) { | 36 RTC_DCHECK_LE(30.f, peak_level_); |
| 35 // Smoothly update the estimate upwards when the frame peak level is | 37 return peak_level_; |
| 36 // higher than the estimate. | 38 } |
| 37 peak_level_ += 0.1f * (frame_peak_level - peak_level_); | |
| 38 hold_counter_ = 100; | |
| 39 } else { | |
| 40 hold_counter_ = std::max(0, hold_counter_ - 1); | |
| 41 | 39 |
| 42 // When the signal is highly non-stationary, update the estimate slowly | 40 if (peak_level_ < frame_peak_level) { |
| 43 // downwards if the estimate is lower than the frame peak level. | 41 // Smoothly update the estimate upwards when the frame peak level is |
| 44 if (signal_type == SignalClassifier::SignalType::kHighlyNonStationary && | 42 // higher than the estimate. |
| 45 hold_counter_ == 0) { | 43 peak_level_ += 0.1f * (frame_peak_level - peak_level_); |
| 46 peak_level_ = | 44 hold_counter_ = 100; |
| 47 std::max(peak_level_ + 0.01f * (frame_peak_level - peak_level_), | 45 initialization_phase_ = false; |
| 48 peak_level_ * 0.995f); | 46 } else { |
| 49 } | 47 hold_counter_ = std::max(0, hold_counter_ - 1); |
| 48 |
| 49 // When the signal is highly non-stationary, update the estimate slowly |
| 50 // downwards if the estimate is lower than the frame peak level. |
| 51 if ((signal_type == SignalClassifier::SignalType::kHighlyNonStationary && |
| 52 hold_counter_ == 0) || |
| 53 initialization_phase_) { |
| 54 peak_level_ = |
| 55 std::max(peak_level_ + 0.01f * (frame_peak_level - peak_level_), |
| 56 peak_level_ * 0.995f); |
| 50 } | 57 } |
| 51 } | 58 } |
| 52 | 59 |
| 53 peak_level_ = std::max(peak_level_, 30.f); | 60 peak_level_ = std::max(peak_level_, 30.f); |
| 54 | 61 |
| 55 return peak_level_; | 62 return peak_level_; |
| 56 } | 63 } |
| 57 | 64 |
| 58 } // namespace webrtc | 65 } // namespace webrtc |
| OLD | NEW |