• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/subband_nearend_detector.h"
12 
13 #include <numeric>
14 
15 namespace webrtc {
SubbandNearendDetector(const EchoCanceller3Config::Suppressor::SubbandNearendDetection & config,size_t num_capture_channels)16 SubbandNearendDetector::SubbandNearendDetector(
17     const EchoCanceller3Config::Suppressor::SubbandNearendDetection& config,
18     size_t num_capture_channels)
19     : config_(config),
20       num_capture_channels_(num_capture_channels),
21       nearend_smoothers_(num_capture_channels_,
22                          aec3::MovingAverage(kFftLengthBy2Plus1,
23                                              config_.nearend_average_blocks)),
24       one_over_subband_length1_(
25           1.f / (config_.subband1.high - config_.subband1.low + 1)),
26       one_over_subband_length2_(
27           1.f / (config_.subband2.high - config_.subband2.low + 1)) {}
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 SubbandNearendDetector::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   for (size_t ch = 0; ch < num_capture_channels_; ++ch) {
39     const std::array<float, kFftLengthBy2Plus1>& noise =
40         comfort_noise_spectrum[ch];
41     std::array<float, kFftLengthBy2Plus1> nearend;
42     nearend_smoothers_[ch].Average(nearend_spectrum[ch], nearend);
43 
44     // Noise power of the first region.
45     float noise_power =
46         std::accumulate(noise.begin() + config_.subband1.low,
47                         noise.begin() + config_.subband1.high + 1, 0.f) *
48         one_over_subband_length1_;
49 
50     // Nearend power of the first region.
51     float nearend_power_subband1 =
52         std::accumulate(nearend.begin() + config_.subband1.low,
53                         nearend.begin() + config_.subband1.high + 1, 0.f) *
54         one_over_subband_length1_;
55 
56     // Nearend power of the second region.
57     float nearend_power_subband2 =
58         std::accumulate(nearend.begin() + config_.subband2.low,
59                         nearend.begin() + config_.subband2.high + 1, 0.f) *
60         one_over_subband_length2_;
61 
62     // One channel is sufficient to trigger nearend state.
63     nearend_state_ =
64         nearend_state_ ||
65         (nearend_power_subband1 <
66              config_.nearend_threshold * nearend_power_subband2 &&
67          (nearend_power_subband1 > config_.snr_threshold * noise_power));
68   }
69 }
70 }  // namespace webrtc
71