1 /*
2  *  Copyright (c) 2017 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/output_selector.h"
12 
13 #include <algorithm>
14 #include <numeric>
15 
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 namespace {
20 
21 // Performs the transition between the signals in a smooth manner.
SmoothFrameTransition(bool from_y_to_e,rtc::ArrayView<const float> e,rtc::ArrayView<float> y)22 void SmoothFrameTransition(bool from_y_to_e,
23                            rtc::ArrayView<const float> e,
24                            rtc::ArrayView<float> y) {
25   RTC_DCHECK_LT(0u, e.size());
26   RTC_DCHECK_EQ(y.size(), e.size());
27 
28   const float change_factor = (from_y_to_e ? 1.f : -1.f) / e.size();
29   float averaging = from_y_to_e ? 0.f : 1.f;
30   for (size_t k = 0; k < e.size(); ++k) {
31     y[k] += averaging * (e[k] - y[k]);
32     averaging += change_factor;
33   }
34   RTC_DCHECK_EQ(from_y_to_e ? 1.f : 0.f, averaging);
35 }
36 
37 }  // namespace
38 
39 OutputSelector::OutputSelector() = default;
40 
41 OutputSelector::~OutputSelector() = default;
42 
FormLinearOutput(bool use_subtractor_output,rtc::ArrayView<const float> subtractor_output,rtc::ArrayView<float> capture)43 void OutputSelector::FormLinearOutput(
44     bool use_subtractor_output,
45     rtc::ArrayView<const float> subtractor_output,
46     rtc::ArrayView<float> capture) {
47   RTC_DCHECK_EQ(subtractor_output.size(), capture.size());
48   rtc::ArrayView<const float>& e_main = subtractor_output;
49   rtc::ArrayView<float> y = capture;
50 
51   if (use_subtractor_output != use_subtractor_output_) {
52     use_subtractor_output_ = use_subtractor_output;
53     SmoothFrameTransition(use_subtractor_output_, e_main, y);
54   } else if (use_subtractor_output_) {
55     std::copy(e_main.begin(), e_main.end(), y.begin());
56   }
57 }
58 
59 }  // namespace webrtc
60