peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/output_selector.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <numeric> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | |
| 21 | // Performs the transition between the signals in a smooth manner. |
| 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 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | OutputSelector::OutputSelector() = default; |
| 40 | |
| 41 | OutputSelector::~OutputSelector() = default; |
| 42 | |
| 43 | void OutputSelector::FormLinearOutput( |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 44 | bool use_subtractor_output, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 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 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 51 | if (use_subtractor_output != use_subtractor_output_) { |
| 52 | use_subtractor_output_ = use_subtractor_output; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 53 | SmoothFrameTransition(use_subtractor_output_, e_main, y); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 54 | } else if (use_subtractor_output_) { |
| 55 | std::copy(e_main.begin(), e_main.end(), y.begin()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } // namespace webrtc |