peah | 69221db | 2017-01-27 03:28:19 -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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "modules/audio_processing/aec3/echo_remover.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 11 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 12 | #include <math.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 15 | #include <algorithm> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <array> |
Mirko Bonadei | dbce090 | 2019-03-15 07:39:02 +0100 | [diff] [blame] | 17 | #include <cmath> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 18 | #include <memory> |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/array_view.h" |
| 21 | #include "modules/audio_processing/aec3/aec3_common.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/aec3/aec3_fft.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/aec_state.h" |
| 24 | #include "modules/audio_processing/aec3/comfort_noise_generator.h" |
| 25 | #include "modules/audio_processing/aec3/echo_path_variability.h" |
| 26 | #include "modules/audio_processing/aec3/echo_remover_metrics.h" |
| 27 | #include "modules/audio_processing/aec3/fft_data.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "modules/audio_processing/aec3/render_buffer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 29 | #include "modules/audio_processing/aec3/render_signal_analyzer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "modules/audio_processing/aec3/residual_echo_estimator.h" |
| 31 | #include "modules/audio_processing/aec3/subtractor.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 32 | #include "modules/audio_processing/aec3/subtractor_output.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "modules/audio_processing/aec3/suppression_filter.h" |
| 34 | #include "modules/audio_processing/aec3/suppression_gain.h" |
| 35 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "rtc_base/atomic_ops.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 37 | #include "rtc_base/checks.h" |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 38 | #include "rtc_base/logging.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 39 | |
| 40 | namespace webrtc { |
| 41 | |
| 42 | namespace { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 43 | |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 44 | // Maximum number of channels for which the capture channel data is stored on |
| 45 | // the stack. If the number of channels are larger than this, they are stored |
| 46 | // using scratch memory that is pre-allocated on the heap. The reason for this |
| 47 | // partitioning is not to waste heap space for handling the more common numbers |
| 48 | // of channels, while at the same time not limiting the support for higher |
| 49 | // numbers of channels by enforcing the capture channel data to be stored on the |
| 50 | // stack using a fixed maximum value. |
| 51 | constexpr size_t kMaxNumChannelsOnStack = 2; |
| 52 | |
| 53 | // Chooses the number of channels to store on the heap when that is required due |
| 54 | // to the number of capture channels being larger than the pre-defined number |
| 55 | // of channels to store on the stack. |
| 56 | size_t NumChannelsOnHeap(size_t num_capture_channels) { |
| 57 | return num_capture_channels > kMaxNumChannelsOnStack ? num_capture_channels |
| 58 | : 0; |
| 59 | } |
| 60 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 61 | void LinearEchoPower(const FftData& E, |
| 62 | const FftData& Y, |
| 63 | std::array<float, kFftLengthBy2Plus1>* S2) { |
| 64 | for (size_t k = 0; k < E.re.size(); ++k) { |
| 65 | (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) + |
| 66 | (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]); |
| 67 | } |
| 68 | } |
| 69 | |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 70 | // Fades between two input signals using a fix-sized transition. |
| 71 | void SignalTransition(rtc::ArrayView<const float> from, |
| 72 | rtc::ArrayView<const float> to, |
| 73 | rtc::ArrayView<float> out) { |
Gustaf Ullberg | 7911d37 | 2019-09-24 16:31:01 +0200 | [diff] [blame] | 74 | if (from == to) { |
| 75 | RTC_DCHECK_EQ(to.size(), out.size()); |
| 76 | std::copy(to.begin(), to.end(), out.begin()); |
| 77 | } else { |
| 78 | constexpr size_t kTransitionSize = 30; |
| 79 | constexpr float kOneByTransitionSizePlusOne = 1.f / (kTransitionSize + 1); |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 80 | |
Gustaf Ullberg | 7911d37 | 2019-09-24 16:31:01 +0200 | [diff] [blame] | 81 | RTC_DCHECK_EQ(from.size(), to.size()); |
| 82 | RTC_DCHECK_EQ(from.size(), out.size()); |
| 83 | RTC_DCHECK_LE(kTransitionSize, out.size()); |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 84 | |
Gustaf Ullberg | 7911d37 | 2019-09-24 16:31:01 +0200 | [diff] [blame] | 85 | for (size_t k = 0; k < kTransitionSize; ++k) { |
| 86 | float a = (k + 1) * kOneByTransitionSizePlusOne; |
| 87 | out[k] = a * to[k] + (1.f - a) * from[k]; |
| 88 | } |
| 89 | |
| 90 | std::copy(to.begin() + kTransitionSize, to.end(), |
| 91 | out.begin() + kTransitionSize); |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 92 | } |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 95 | // Computes a windowed (square root Hanning) padded FFT and updates the related |
| 96 | // memory. |
| 97 | void WindowedPaddedFft(const Aec3Fft& fft, |
| 98 | rtc::ArrayView<const float> v, |
| 99 | rtc::ArrayView<float> v_old, |
| 100 | FftData* V) { |
| 101 | fft.PaddedFft(v, v_old, Aec3Fft::Window::kSqrtHanning, V); |
| 102 | std::copy(v.begin(), v.end(), v_old.begin()); |
| 103 | } |
| 104 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 105 | // Class for removing the echo from the capture signal. |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 106 | class EchoRemoverImpl final : public EchoRemover { |
| 107 | public: |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 108 | EchoRemoverImpl(const EchoCanceller3Config& config, |
| 109 | int sample_rate_hz, |
| 110 | size_t num_render_channels, |
| 111 | size_t num_capture_channels); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 112 | ~EchoRemoverImpl() override; |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 113 | EchoRemoverImpl(const EchoRemoverImpl&) = delete; |
| 114 | EchoRemoverImpl& operator=(const EchoRemoverImpl&) = delete; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 115 | |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 116 | void GetMetrics(EchoControl::Metrics* metrics) const override; |
| 117 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 118 | // Removes the echo from a block of samples from the capture signal. The |
| 119 | // supplied render signal is assumed to be pre-aligned with the capture |
| 120 | // signal. |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 121 | void ProcessCapture( |
| 122 | EchoPathVariability echo_path_variability, |
| 123 | bool capture_signal_saturation, |
| 124 | const absl::optional<DelayEstimate>& external_delay, |
| 125 | RenderBuffer* render_buffer, |
| 126 | std::vector<std::vector<std::vector<float>>>* capture) override; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 127 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 128 | // Updates the status on whether echo leakage is detected in the output of the |
| 129 | // echo remover. |
| 130 | void UpdateEchoLeakageStatus(bool leakage_detected) override { |
| 131 | echo_leakage_detected_ = leakage_detected; |
| 132 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 133 | |
| 134 | private: |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 135 | // Selects which of the shadow and main linear filter outputs that is most |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 136 | // appropriate to pass to the suppressor and forms the linear filter output by |
| 137 | // smoothly transition between those. |
Gustaf Ullberg | 68d6d44 | 2019-01-29 10:08:15 +0100 | [diff] [blame] | 138 | void FormLinearFilterOutput(const SubtractorOutput& subtractor_output, |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 139 | rtc::ArrayView<float> output); |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 140 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 141 | static int instance_count_; |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 142 | const EchoCanceller3Config config_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 143 | const Aec3Fft fft_; |
| 144 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 145 | const Aec3Optimization optimization_; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 146 | const int sample_rate_hz_; |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 147 | const size_t num_render_channels_; |
| 148 | const size_t num_capture_channels_; |
Per Åhgren | 7802675 | 2018-08-01 16:24:08 +0200 | [diff] [blame] | 149 | const bool use_shadow_filter_output_; |
Per Åhgren | 7bdf073 | 2019-09-25 14:53:30 +0200 | [diff] [blame] | 150 | Subtractor subtractor_; |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 151 | std::vector<std::unique_ptr<SuppressionGain>> suppression_gains_; |
| 152 | std::vector<std::unique_ptr<ComfortNoiseGenerator>> cngs_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 153 | SuppressionFilter suppression_filter_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 154 | RenderSignalAnalyzer render_signal_analyzer_; |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 155 | std::vector<std::unique_ptr<ResidualEchoEstimator>> residual_echo_estimators_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 156 | bool echo_leakage_detected_ = false; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 157 | AecState aec_state_; |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 158 | EchoRemoverMetrics metrics_; |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 159 | std::vector<std::array<float, kFftLengthBy2>> e_old_; |
| 160 | std::vector<std::array<float, kFftLengthBy2>> y_old_; |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 161 | size_t block_counter_ = 0; |
| 162 | int gain_change_hangover_ = 0; |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 163 | bool main_filter_output_last_selected_ = true; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 164 | |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 165 | std::vector<std::array<float, kFftLengthBy2>> e_heap_; |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 166 | std::vector<std::array<float, kFftLengthBy2Plus1>> Y2_heap_; |
| 167 | std::vector<std::array<float, kFftLengthBy2Plus1>> E2_heap_; |
| 168 | std::vector<std::array<float, kFftLengthBy2Plus1>> R2_heap_; |
| 169 | std::vector<std::array<float, kFftLengthBy2Plus1>> S2_linear_heap_; |
| 170 | std::vector<FftData> Y_heap_; |
| 171 | std::vector<FftData> E_heap_; |
| 172 | std::vector<FftData> comfort_noise_heap_; |
| 173 | std::vector<FftData> high_band_comfort_noise_heap_; |
| 174 | std::vector<SubtractorOutput> subtractor_output_heap_; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 177 | int EchoRemoverImpl::instance_count_ = 0; |
| 178 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 179 | EchoRemoverImpl::EchoRemoverImpl(const EchoCanceller3Config& config, |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 180 | int sample_rate_hz, |
| 181 | size_t num_render_channels, |
| 182 | size_t num_capture_channels) |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 183 | : config_(config), |
| 184 | fft_(), |
aleloi | 88b82b5 | 2017-02-23 06:27:03 -0800 | [diff] [blame] | 185 | data_dumper_( |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 186 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 187 | optimization_(DetectOptimization()), |
| 188 | sample_rate_hz_(sample_rate_hz), |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 189 | num_render_channels_(num_render_channels), |
| 190 | num_capture_channels_(num_capture_channels), |
Per Åhgren | 2402154 | 2018-08-31 07:34:29 +0200 | [diff] [blame] | 191 | use_shadow_filter_output_( |
Per Åhgren | 2402154 | 2018-08-31 07:34:29 +0200 | [diff] [blame] | 192 | config_.filter.enable_shadow_filter_output_usage), |
Per Åhgren | 7bdf073 | 2019-09-25 14:53:30 +0200 | [diff] [blame] | 193 | subtractor_(config, |
| 194 | num_render_channels_, |
| 195 | num_capture_channels_, |
| 196 | data_dumper_.get(), |
| 197 | optimization_), |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 198 | suppression_gains_(num_capture_channels_), |
| 199 | cngs_(num_capture_channels_), |
Gustaf Ullberg | af3fdc0 | 2019-09-24 15:05:04 +0200 | [diff] [blame] | 200 | suppression_filter_(optimization_, |
| 201 | sample_rate_hz_, |
| 202 | num_capture_channels_), |
Per Åhgren | 971de07 | 2018-03-14 23:23:47 +0100 | [diff] [blame] | 203 | render_signal_analyzer_(config_), |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 204 | residual_echo_estimators_(num_capture_channels_), |
Sam Zackrisson | 8f736c0 | 2019-10-01 12:47:53 +0200 | [diff] [blame] | 205 | aec_state_(config_, num_capture_channels_), |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 206 | e_old_(num_capture_channels_), |
| 207 | y_old_(num_capture_channels_), |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 208 | e_heap_(NumChannelsOnHeap(num_capture_channels_)), |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 209 | Y2_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 210 | E2_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 211 | R2_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 212 | S2_linear_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 213 | Y_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 214 | E_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 215 | comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 216 | high_band_comfort_noise_heap_(NumChannelsOnHeap(num_capture_channels_)), |
| 217 | subtractor_output_heap_(NumChannelsOnHeap(num_capture_channels_)) { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 218 | RTC_DCHECK(ValidFullBandRate(sample_rate_hz)); |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 219 | for (auto& e_k : e_heap_) { |
| 220 | e_k.fill(0.f); |
| 221 | } |
| 222 | |
Sam Zackrisson | 32eae4c | 2019-09-30 09:58:09 +0200 | [diff] [blame] | 223 | uint32_t cng_seed = 42; |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 224 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 225 | residual_echo_estimators_[ch] = |
| 226 | std::make_unique<ResidualEchoEstimator>(config_); |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 227 | suppression_gains_[ch] = std::make_unique<SuppressionGain>( |
| 228 | config_, optimization_, sample_rate_hz); |
Sam Zackrisson | 32eae4c | 2019-09-30 09:58:09 +0200 | [diff] [blame] | 229 | cngs_[ch] = |
| 230 | std::make_unique<ComfortNoiseGenerator>(optimization_, cng_seed++); |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 231 | e_old_[ch].fill(0.f); |
| 232 | y_old_[ch].fill(0.f); |
| 233 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | EchoRemoverImpl::~EchoRemoverImpl() = default; |
| 237 | |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 238 | void EchoRemoverImpl::GetMetrics(EchoControl::Metrics* metrics) const { |
| 239 | // Echo return loss (ERL) is inverted to go from gain to attenuation. |
Mirko Bonadei | dbce090 | 2019-03-15 07:39:02 +0100 | [diff] [blame] | 240 | metrics->echo_return_loss = -10.0 * std::log10(aec_state_.ErlTimeDomain()); |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 241 | metrics->echo_return_loss_enhancement = |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 242 | Log2TodB(aec_state_.FullBandErleLog2()); |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 243 | } |
| 244 | |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 245 | void EchoRemoverImpl::ProcessCapture( |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 246 | EchoPathVariability echo_path_variability, |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 247 | bool capture_signal_saturation, |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 248 | const absl::optional<DelayEstimate>& external_delay, |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 249 | RenderBuffer* render_buffer, |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 250 | std::vector<std::vector<std::vector<float>>>* capture) { |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 251 | ++block_counter_; |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 252 | const std::vector<std::vector<std::vector<float>>>& x = |
| 253 | render_buffer->Block(0); |
| 254 | std::vector<std::vector<std::vector<float>>>* y = capture; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 255 | RTC_DCHECK(render_buffer); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 256 | RTC_DCHECK(y); |
| 257 | RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_)); |
| 258 | RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_)); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 259 | RTC_DCHECK_EQ(x[0].size(), num_render_channels_); |
| 260 | RTC_DCHECK_EQ((*y)[0].size(), num_capture_channels_); |
| 261 | RTC_DCHECK_EQ(x[0][0].size(), kBlockSize); |
| 262 | RTC_DCHECK_EQ((*y)[0][0].size(), kBlockSize); |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 263 | |
| 264 | // Stack allocated data to use when the number of channels is low. |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 265 | std::array<std::array<float, kFftLengthBy2>, kMaxNumChannelsOnStack> e_stack; |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 266 | std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 267 | Y2_stack; |
| 268 | std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 269 | E2_stack; |
| 270 | std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 271 | R2_stack; |
| 272 | std::array<std::array<float, kFftLengthBy2Plus1>, kMaxNumChannelsOnStack> |
| 273 | S2_linear_stack; |
| 274 | std::array<FftData, kMaxNumChannelsOnStack> Y_stack; |
| 275 | std::array<FftData, kMaxNumChannelsOnStack> E_stack; |
| 276 | std::array<FftData, kMaxNumChannelsOnStack> comfort_noise_stack; |
| 277 | std::array<FftData, kMaxNumChannelsOnStack> high_band_comfort_noise_stack; |
| 278 | std::array<SubtractorOutput, kMaxNumChannelsOnStack> subtractor_output_stack; |
| 279 | |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 280 | rtc::ArrayView<std::array<float, kFftLengthBy2>> e(e_stack.data(), |
| 281 | num_capture_channels_); |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 282 | rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> Y2( |
| 283 | Y2_stack.data(), num_capture_channels_); |
| 284 | rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> E2( |
| 285 | E2_stack.data(), num_capture_channels_); |
| 286 | rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> R2( |
| 287 | R2_stack.data(), num_capture_channels_); |
| 288 | rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>> S2_linear( |
| 289 | S2_linear_stack.data(), num_capture_channels_); |
| 290 | rtc::ArrayView<FftData> Y(Y_stack.data(), num_capture_channels_); |
| 291 | rtc::ArrayView<FftData> E(E_stack.data(), num_capture_channels_); |
| 292 | rtc::ArrayView<FftData> comfort_noise(comfort_noise_stack.data(), |
| 293 | num_capture_channels_); |
| 294 | rtc::ArrayView<FftData> high_band_comfort_noise( |
| 295 | high_band_comfort_noise_stack.data(), num_capture_channels_); |
| 296 | rtc::ArrayView<SubtractorOutput> subtractor_output( |
| 297 | subtractor_output_stack.data(), num_capture_channels_); |
| 298 | if (NumChannelsOnHeap(num_capture_channels_) > 0) { |
| 299 | // If the stack-allocated space is too small, use the heap for storing the |
| 300 | // microphone data. |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 301 | e = rtc::ArrayView<std::array<float, kFftLengthBy2>>(e_heap_.data(), |
| 302 | num_capture_channels_); |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 303 | Y2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 304 | Y2_heap_.data(), num_capture_channels_); |
| 305 | E2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 306 | E2_heap_.data(), num_capture_channels_); |
| 307 | R2 = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 308 | R2_heap_.data(), num_capture_channels_); |
| 309 | S2_linear = rtc::ArrayView<std::array<float, kFftLengthBy2Plus1>>( |
| 310 | S2_linear_heap_.data(), num_capture_channels_); |
| 311 | Y = rtc::ArrayView<FftData>(Y_heap_.data(), num_capture_channels_); |
| 312 | E = rtc::ArrayView<FftData>(E_heap_.data(), num_capture_channels_); |
| 313 | comfort_noise = rtc::ArrayView<FftData>(comfort_noise_heap_.data(), |
| 314 | num_capture_channels_); |
| 315 | high_band_comfort_noise = rtc::ArrayView<FftData>( |
| 316 | high_band_comfort_noise_heap_.data(), num_capture_channels_); |
| 317 | subtractor_output = rtc::ArrayView<SubtractorOutput>( |
| 318 | subtractor_output_heap_.data(), num_capture_channels_); |
| 319 | } |
| 320 | |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 321 | const std::vector<float>& x0 = x[0][0]; |
| 322 | std::vector<float>& y0 = (*y)[0][0]; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 323 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 324 | data_dumper_->DumpWav("aec3_echo_remover_capture_input", kBlockSize, &y0[0], |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 325 | 16000, 1); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 326 | data_dumper_->DumpWav("aec3_echo_remover_render_input", kBlockSize, &x0[0], |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 327 | 16000, 1); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 328 | data_dumper_->DumpRaw("aec3_echo_remover_capture_input", y0); |
| 329 | data_dumper_->DumpRaw("aec3_echo_remover_render_input", x0); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 330 | |
| 331 | aec_state_.UpdateCaptureSaturation(capture_signal_saturation); |
| 332 | |
| 333 | if (echo_path_variability.AudioPathChanged()) { |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 334 | // Ensure that the gain change is only acted on once per frame. |
| 335 | if (echo_path_variability.gain_change) { |
| 336 | if (gain_change_hangover_ == 0) { |
| 337 | constexpr int kMaxBlocksPerFrame = 3; |
| 338 | gain_change_hangover_ = kMaxBlocksPerFrame; |
Gustaf Ullberg | 940c2b5 | 2019-08-08 15:04:41 +0200 | [diff] [blame] | 339 | RTC_LOG(LS_INFO) << "Gain change detected at block " << block_counter_; |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 340 | } else { |
| 341 | echo_path_variability.gain_change = false; |
| 342 | } |
| 343 | } |
| 344 | |
Per Åhgren | 7bdf073 | 2019-09-25 14:53:30 +0200 | [diff] [blame] | 345 | subtractor_.HandleEchoPathChange(echo_path_variability); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 346 | aec_state_.HandleEchoPathChange(echo_path_variability); |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 347 | |
| 348 | if (echo_path_variability.delay_change != |
| 349 | EchoPathVariability::DelayAdjustment::kNone) { |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 350 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 351 | suppression_gains_[ch]->SetInitialState(true); |
| 352 | } |
Per Åhgren | 88cf050 | 2018-07-16 17:08:41 +0200 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | if (gain_change_hangover_ > 0) { |
| 356 | --gain_change_hangover_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 357 | } |
| 358 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 359 | // Analyze the render signal. |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 360 | render_signal_analyzer_.Update(*render_buffer, |
| 361 | aec_state_.FilterDelayBlocks()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 362 | |
Per Åhgren | 7bdf073 | 2019-09-25 14:53:30 +0200 | [diff] [blame] | 363 | // State transition. |
Jesús de Vicente Peña | 02e9e44 | 2018-08-29 13:34:07 +0200 | [diff] [blame] | 364 | if (aec_state_.TransitionTriggered()) { |
Per Åhgren | 7bdf073 | 2019-09-25 14:53:30 +0200 | [diff] [blame] | 365 | subtractor_.ExitInitialState(); |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 366 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 367 | suppression_gains_[ch]->SetInitialState(false); |
| 368 | } |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 369 | } |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 370 | |
Per Åhgren | 7bdf073 | 2019-09-25 14:53:30 +0200 | [diff] [blame] | 371 | // Perform linear echo cancellation. |
| 372 | subtractor_.Process(*render_buffer, (*y)[0], render_signal_analyzer_, |
| 373 | aec_state_, subtractor_output); |
| 374 | |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 375 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 376 | auto& y_low = (*y)[0][ch]; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 377 | |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 378 | // Compute spectra. |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 379 | FormLinearFilterOutput(subtractor_output[ch], e[ch]); |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 380 | WindowedPaddedFft(fft_, y_low, y_old_[ch], &Y[ch]); |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 381 | WindowedPaddedFft(fft_, e[ch], e_old_[ch], &E[ch]); |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 382 | LinearEchoPower(E[ch], Y[ch], &S2_linear[ch]); |
| 383 | Y[ch].Spectrum(optimization_, Y2[ch]); |
| 384 | E[ch].Spectrum(optimization_, E2[ch]); |
| 385 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 386 | |
| 387 | // Update the AEC state information. |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 388 | // TODO(bugs.webrtc.org/10913): Take all subtractors into account. |
Per Åhgren | b441acf | 2019-10-05 09:07:24 +0200 | [diff] [blame] | 389 | aec_state_.Update(external_delay, subtractor_.FilterFrequencyResponse()[0], |
| 390 | subtractor_.FilterImpulseResponse()[0], *render_buffer, |
| 391 | E2[0], Y2[0], subtractor_output); |
Per Åhgren | 169c7fd | 2018-04-27 12:04:03 +0200 | [diff] [blame] | 392 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 393 | // Choose the linear output. |
Gustaf Ullberg | af3fdc0 | 2019-09-24 15:05:04 +0200 | [diff] [blame] | 394 | const auto& Y_fft = aec_state_.UseLinearFilterOutput() ? E : Y; |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 395 | |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 396 | data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0], 16000, 1); |
Per Åhgren | 0e3b1ff | 2019-09-25 12:09:37 +0200 | [diff] [blame] | 397 | data_dumper_->DumpWav("aec3_output_linear2", kBlockSize, &e[0][0], 16000, 1); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 398 | |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 399 | float high_bands_gain = 1.f; |
| 400 | std::array<float, kFftLengthBy2Plus1> G; |
| 401 | G.fill(1.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 402 | |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 403 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 404 | // Estimate the residual echo power. |
| 405 | residual_echo_estimators_[ch]->Estimate(aec_state_, *render_buffer, |
| 406 | S2_linear[ch], Y2[ch], &R2[ch]); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 407 | |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 408 | // Estimate the comfort noise. |
| 409 | cngs_[ch]->Compute(aec_state_, Y2[ch], &comfort_noise[ch], |
| 410 | &high_band_comfort_noise[ch]); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 411 | |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 412 | // Suppressor echo estimate. |
| 413 | const auto& echo_spectrum = |
| 414 | aec_state_.UsableLinearEstimate() ? S2_linear[ch] : R2[ch]; |
| 415 | |
| 416 | // Suppressor nearend estimate. |
| 417 | std::array<float, kFftLengthBy2Plus1> nearend_spectrum_bounded; |
| 418 | if (aec_state_.UsableLinearEstimate()) { |
| 419 | std::transform(E2[ch].begin(), E2[ch].end(), Y2[ch].begin(), |
| 420 | nearend_spectrum_bounded.begin(), |
| 421 | [](float a, float b) { return std::min(a, b); }); |
| 422 | } |
| 423 | const auto& nearend_spectrum = |
| 424 | aec_state_.UsableLinearEstimate() ? nearend_spectrum_bounded : Y2[ch]; |
| 425 | |
| 426 | // Compute preferred gains for each channel. The minimum gain determines the |
| 427 | // final gain. |
| 428 | float high_bands_gain_channel; |
| 429 | std::array<float, kFftLengthBy2Plus1> G_channel; |
| 430 | suppression_gains_[ch]->GetGain(nearend_spectrum, echo_spectrum, R2[ch], |
| 431 | cngs_[ch]->NoiseSpectrum(), |
| 432 | render_signal_analyzer_, aec_state_, x, |
| 433 | &high_bands_gain_channel, &G_channel); |
| 434 | |
| 435 | high_bands_gain = std::min(high_bands_gain, high_bands_gain_channel); |
| 436 | std::transform(G.begin(), G.end(), G_channel.begin(), G.begin(), |
Gustaf Ullberg | 2bab5ad | 2019-04-15 17:15:37 +0200 | [diff] [blame] | 437 | [](float a, float b) { return std::min(a, b); }); |
| 438 | } |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 439 | |
Gustaf Ullberg | af3fdc0 | 2019-09-24 15:05:04 +0200 | [diff] [blame] | 440 | suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 441 | high_bands_gain, Y_fft, y); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 442 | |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 443 | // Update the metrics. |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 444 | metrics_.Update(aec_state_, cngs_[0]->NoiseSpectrum(), G); |
peah | e985b3f | 2017-02-28 22:08:53 -0800 | [diff] [blame] | 445 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 446 | // Debug outputs for the purpose of development and analysis. |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 447 | data_dumper_->DumpWav("aec3_echo_estimate", kBlockSize, |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 448 | &subtractor_output[0].s_main[0], 16000, 1); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 449 | data_dumper_->DumpRaw("aec3_output", y0); |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 450 | data_dumper_->DumpRaw("aec3_narrow_render", |
| 451 | render_signal_analyzer_.NarrowPeakBand() ? 1 : 0); |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 452 | data_dumper_->DumpRaw("aec3_N2", cngs_[0]->NoiseSpectrum()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 453 | data_dumper_->DumpRaw("aec3_suppressor_gain", G); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 454 | data_dumper_->DumpWav( |
| 455 | "aec3_output", rtc::ArrayView<const float>(&y0[0], kBlockSize), 16000, 1); |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 456 | data_dumper_->DumpRaw("aec3_using_subtractor_output[0]", |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 457 | aec_state_.UseLinearFilterOutput() ? 1 : 0); |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 458 | data_dumper_->DumpRaw("aec3_E2", E2[0]); |
| 459 | data_dumper_->DumpRaw("aec3_S2_linear", S2_linear[0]); |
| 460 | data_dumper_->DumpRaw("aec3_Y2", Y2[0]); |
Jesús de Vicente Peña | 7682c6e | 2018-03-22 14:53:23 +0100 | [diff] [blame] | 461 | data_dumper_->DumpRaw( |
Sam Zackrisson | a81c09d | 2019-09-05 09:35:10 +0200 | [diff] [blame] | 462 | "aec3_X2", |
| 463 | render_buffer->Spectrum(aec_state_.FilterDelayBlocks(), /*channel=*/0)); |
Per Åhgren | f6aa572 | 2019-09-10 18:05:17 +0200 | [diff] [blame] | 464 | data_dumper_->DumpRaw("aec3_R2", R2[0]); |
Jesús de Vicente Peña | 075cb2b | 2018-06-13 15:13:55 +0200 | [diff] [blame] | 465 | data_dumper_->DumpRaw("aec3_R2_reverb", |
Gustaf Ullberg | a99b89b | 2019-09-23 16:03:12 +0200 | [diff] [blame] | 466 | residual_echo_estimators_[0]->GetReverbPowerSpectrum()); |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 467 | data_dumper_->DumpRaw("aec3_filter_delay", aec_state_.FilterDelayBlocks()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 468 | data_dumper_->DumpRaw("aec3_capture_saturation", |
| 469 | aec_state_.SaturatedCapture() ? 1 : 0); |
| 470 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 471 | |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 472 | void EchoRemoverImpl::FormLinearFilterOutput( |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 473 | const SubtractorOutput& subtractor_output, |
| 474 | rtc::ArrayView<float> output) { |
| 475 | RTC_DCHECK_EQ(subtractor_output.e_main.size(), output.size()); |
| 476 | RTC_DCHECK_EQ(subtractor_output.e_shadow.size(), output.size()); |
| 477 | bool use_main_output = true; |
| 478 | if (use_shadow_filter_output_) { |
Jesús de Vicente Peña | 02e9e44 | 2018-08-29 13:34:07 +0200 | [diff] [blame] | 479 | // As the output of the main adaptive filter generally should be better |
| 480 | // than the shadow filter output, add a margin and threshold for when |
| 481 | // choosing the shadow filter output. |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 482 | if (subtractor_output.e2_shadow < 0.9f * subtractor_output.e2_main && |
| 483 | subtractor_output.y2 > 30.f * 30.f * kBlockSize && |
| 484 | (subtractor_output.s2_main > 60.f * 60.f * kBlockSize || |
| 485 | subtractor_output.s2_shadow > 60.f * 60.f * kBlockSize)) { |
| 486 | use_main_output = false; |
| 487 | } else { |
| 488 | // If the main filter is diverged, choose the filter output that has the |
| 489 | // lowest power. |
| 490 | if (subtractor_output.e2_shadow < subtractor_output.e2_main && |
| 491 | subtractor_output.y2 < subtractor_output.e2_main) { |
| 492 | use_main_output = false; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Gustaf Ullberg | 7911d37 | 2019-09-24 16:31:01 +0200 | [diff] [blame] | 497 | SignalTransition( |
| 498 | main_filter_output_last_selected_ ? subtractor_output.e_main |
| 499 | : subtractor_output.e_shadow, |
| 500 | use_main_output ? subtractor_output.e_main : subtractor_output.e_shadow, |
| 501 | output); |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 502 | main_filter_output_last_selected_ = use_main_output; |
| 503 | } |
| 504 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 505 | } // namespace |
| 506 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 507 | EchoRemover* EchoRemover::Create(const EchoCanceller3Config& config, |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 508 | int sample_rate_hz, |
| 509 | size_t num_render_channels, |
| 510 | size_t num_capture_channels) { |
| 511 | return new EchoRemoverImpl(config, sample_rate_hz, num_render_channels, |
| 512 | num_capture_channels); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | } // namespace webrtc |