blob: 4f547d98d914cbf55461b3081b0c186529480e62 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/output_selector.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
14#include <numeric>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
peah522d71b2017-02-23 05:16:26 -080017
18namespace webrtc {
19namespace {
20
21// Performs the transition between the signals in a smooth manner.
22void 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
peah522d71b2017-02-23 05:16:26 -080037} // namespace
38
39OutputSelector::OutputSelector() = default;
40
41OutputSelector::~OutputSelector() = default;
42
43void OutputSelector::FormLinearOutput(
peah86afe9d2017-04-06 15:45:32 -070044 bool use_subtractor_output,
peah522d71b2017-02-23 05:16:26 -080045 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
peah86afe9d2017-04-06 15:45:32 -070051 if (use_subtractor_output != use_subtractor_output_) {
52 use_subtractor_output_ = use_subtractor_output;
peah522d71b2017-02-23 05:16:26 -080053 SmoothFrameTransition(use_subtractor_output_, e_main, y);
peah522d71b2017-02-23 05:16:26 -080054 } else if (use_subtractor_output_) {
55 std::copy(e_main.begin(), e_main.end(), y.begin());
56 }
57}
58
59} // namespace webrtc