1 /*
2 * Copyright (c) 2019 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 "modules/audio_processing/aec3/dominant_nearend_detector.h"
12
13 #include <numeric>
14
15 namespace webrtc {
DominantNearendDetector(const EchoCanceller3Config::Suppressor::DominantNearendDetection & config,size_t num_capture_channels)16 DominantNearendDetector::DominantNearendDetector(
17 const EchoCanceller3Config::Suppressor::DominantNearendDetection& config,
18 size_t num_capture_channels)
19 : enr_threshold_(config.enr_threshold),
20 enr_exit_threshold_(config.enr_exit_threshold),
21 snr_threshold_(config.snr_threshold),
22 hold_duration_(config.hold_duration),
23 trigger_threshold_(config.trigger_threshold),
24 use_during_initial_phase_(config.use_during_initial_phase),
25 num_capture_channels_(num_capture_channels),
26 trigger_counters_(num_capture_channels_),
27 hold_counters_(num_capture_channels_) {}
28
Update(rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> nearend_spectrum,rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> residual_echo_spectrum,rtc::ArrayView<const std::array<float,kFftLengthBy2Plus1>> comfort_noise_spectrum,bool initial_state)29 void DominantNearendDetector::Update(
30 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
31 nearend_spectrum,
32 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
33 residual_echo_spectrum,
34 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
35 comfort_noise_spectrum,
36 bool initial_state) {
37 nearend_state_ = false;
38
39 auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) {
40 RTC_DCHECK_LE(16, spectrum.size());
41 return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f);
42 };
43
44 for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
45 const float ne_sum = low_frequency_energy(nearend_spectrum[ch]);
46 const float echo_sum = low_frequency_energy(residual_echo_spectrum[ch]);
47 const float noise_sum = low_frequency_energy(comfort_noise_spectrum[ch]);
48
49 // Detect strong active nearend if the nearend is sufficiently stronger than
50 // the echo and the nearend noise.
51 if ((!initial_state || use_during_initial_phase_) &&
52 echo_sum < enr_threshold_ * ne_sum &&
53 ne_sum > snr_threshold_ * noise_sum) {
54 if (++trigger_counters_[ch] >= trigger_threshold_) {
55 // After a period of strong active nearend activity, flag nearend mode.
56 hold_counters_[ch] = hold_duration_;
57 trigger_counters_[ch] = trigger_threshold_;
58 }
59 } else {
60 // Forget previously detected strong active nearend activity.
61 trigger_counters_[ch] = std::max(0, trigger_counters_[ch] - 1);
62 }
63
64 // Exit nearend-state early at strong echo.
65 if (echo_sum > enr_exit_threshold_ * ne_sum &&
66 echo_sum > snr_threshold_ * noise_sum) {
67 hold_counters_[ch] = 0;
68 }
69
70 // Remain in any nearend mode for a certain duration.
71 hold_counters_[ch] = std::max(0, hold_counters_[ch] - 1);
72 nearend_state_ = nearend_state_ || hold_counters_[ch] > 0;
73 }
74 }
75 } // namespace webrtc
76