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 | */ |
| 10 | #include "webrtc/modules/audio_processing/aec3/echo_remover.h" |
| 11 | |
| 12 | #include <algorithm> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <numeric> |
| 15 | #include <string> |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 16 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 17 | #include "webrtc/base/array_view.h" |
| 18 | #include "webrtc/base/atomicops.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 19 | #include "webrtc/base/constructormagic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 21 | #include "webrtc/modules/audio_processing/aec3/aec_state.h" |
| 22 | #include "webrtc/modules/audio_processing/aec3/comfort_noise_generator.h" |
| 23 | #include "webrtc/modules/audio_processing/aec3/echo_path_variability.h" |
| 24 | #include "webrtc/modules/audio_processing/aec3/fft_buffer.h" |
| 25 | #include "webrtc/modules/audio_processing/aec3/fft_data.h" |
| 26 | #include "webrtc/modules/audio_processing/aec3/output_selector.h" |
| 27 | #include "webrtc/modules/audio_processing/aec3/power_echo_model.h" |
| 28 | #include "webrtc/modules/audio_processing/aec3/render_delay_buffer.h" |
| 29 | #include "webrtc/modules/audio_processing/aec3/residual_echo_estimator.h" |
| 30 | #include "webrtc/modules/audio_processing/aec3/subtractor.h" |
| 31 | #include "webrtc/modules/audio_processing/aec3/suppression_filter.h" |
| 32 | #include "webrtc/modules/audio_processing/aec3/suppression_gain.h" |
| 33 | #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 34 | |
| 35 | namespace webrtc { |
| 36 | |
| 37 | namespace { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 38 | |
| 39 | void LinearEchoPower(const FftData& E, |
| 40 | const FftData& Y, |
| 41 | std::array<float, kFftLengthBy2Plus1>* S2) { |
| 42 | for (size_t k = 0; k < E.re.size(); ++k) { |
| 43 | (*S2)[k] = (Y.re[k] - E.re[k]) * (Y.re[k] - E.re[k]) + |
| 44 | (Y.im[k] - E.im[k]) * (Y.im[k] - E.im[k]); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | float BlockPower(const std::array<float, kBlockSize> x) { |
| 49 | return std::accumulate(x.begin(), x.end(), 0.f, |
| 50 | [](float a, float b) -> float { return a + b * b; }); |
| 51 | } |
| 52 | |
| 53 | // Class for removing the echo from the capture signal. |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 54 | class EchoRemoverImpl final : public EchoRemover { |
| 55 | public: |
| 56 | explicit EchoRemoverImpl(int sample_rate_hz); |
| 57 | ~EchoRemoverImpl() override; |
| 58 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 59 | // Removes the echo from a block of samples from the capture signal. The |
| 60 | // supplied render signal is assumed to be pre-aligned with the capture |
| 61 | // signal. |
| 62 | void ProcessBlock( |
| 63 | const rtc::Optional<size_t>& external_echo_path_delay_estimate, |
| 64 | const EchoPathVariability& echo_path_variability, |
| 65 | bool capture_signal_saturation, |
| 66 | const std::vector<std::vector<float>>& render, |
| 67 | std::vector<std::vector<float>>* capture) override; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 68 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 69 | // Updates the status on whether echo leakage is detected in the output of the |
| 70 | // echo remover. |
| 71 | void UpdateEchoLeakageStatus(bool leakage_detected) override { |
| 72 | echo_leakage_detected_ = leakage_detected; |
| 73 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 74 | |
| 75 | private: |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 76 | static int instance_count_; |
| 77 | const Aec3Fft fft_; |
| 78 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 79 | const Aec3Optimization optimization_; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 80 | const int sample_rate_hz_; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 81 | Subtractor subtractor_; |
| 82 | SuppressionGain suppression_gain_; |
| 83 | ComfortNoiseGenerator cng_; |
| 84 | SuppressionFilter suppression_filter_; |
| 85 | PowerEchoModel power_echo_model_; |
| 86 | FftBuffer X_buffer_; |
| 87 | RenderSignalAnalyzer render_signal_analyzer_; |
| 88 | OutputSelector output_selector_; |
| 89 | ResidualEchoEstimator residual_echo_estimator_; |
| 90 | bool echo_leakage_detected_ = false; |
| 91 | std::array<float, kBlockSize> x_old_; |
| 92 | AecState aec_state_; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 93 | |
| 94 | RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverImpl); |
| 95 | }; |
| 96 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 97 | int EchoRemoverImpl::instance_count_ = 0; |
| 98 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 99 | EchoRemoverImpl::EchoRemoverImpl(int sample_rate_hz) |
aleloi | 88b82b5 | 2017-02-23 06:27:03 -0800 | [diff] [blame] | 100 | : fft_(), |
| 101 | data_dumper_( |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 102 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 103 | optimization_(DetectOptimization()), |
| 104 | sample_rate_hz_(sample_rate_hz), |
| 105 | subtractor_(data_dumper_.get(), optimization_), |
| 106 | suppression_gain_(optimization_), |
| 107 | cng_(optimization_), |
| 108 | suppression_filter_(sample_rate_hz_), |
| 109 | X_buffer_(optimization_, |
| 110 | std::max(subtractor_.MinFarendBufferLength(), |
| 111 | power_echo_model_.MinFarendBufferLength()), |
| 112 | subtractor_.NumBlocksInRenderSums()) { |
| 113 | RTC_DCHECK(ValidFullBandRate(sample_rate_hz)); |
| 114 | x_old_.fill(0.f); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | EchoRemoverImpl::~EchoRemoverImpl() = default; |
| 118 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 119 | void EchoRemoverImpl::ProcessBlock( |
| 120 | const rtc::Optional<size_t>& echo_path_delay_samples, |
| 121 | const EchoPathVariability& echo_path_variability, |
| 122 | bool capture_signal_saturation, |
| 123 | const std::vector<std::vector<float>>& render, |
| 124 | std::vector<std::vector<float>>* capture) { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 125 | const std::vector<std::vector<float>>& x = render; |
| 126 | std::vector<std::vector<float>>* y = capture; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 127 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 128 | RTC_DCHECK(y); |
| 129 | RTC_DCHECK_EQ(x.size(), NumBandsForRate(sample_rate_hz_)); |
| 130 | RTC_DCHECK_EQ(y->size(), NumBandsForRate(sample_rate_hz_)); |
| 131 | RTC_DCHECK_EQ(x[0].size(), kBlockSize); |
| 132 | RTC_DCHECK_EQ((*y)[0].size(), kBlockSize); |
| 133 | const std::vector<float>& x0 = x[0]; |
| 134 | std::vector<float>& y0 = (*y)[0]; |
| 135 | |
| 136 | data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize, &y0[0], |
| 137 | LowestBandRate(sample_rate_hz_), 1); |
| 138 | data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize, &x0[0], |
| 139 | LowestBandRate(sample_rate_hz_), 1); |
| 140 | |
| 141 | aec_state_.UpdateCaptureSaturation(capture_signal_saturation); |
| 142 | |
| 143 | if (echo_path_variability.AudioPathChanged()) { |
| 144 | subtractor_.HandleEchoPathChange(echo_path_variability); |
| 145 | power_echo_model_.HandleEchoPathChange(echo_path_variability); |
peah | ebe7778 | 2017-02-27 07:29:21 -0800 | [diff] [blame] | 146 | residual_echo_estimator_.HandleEchoPathChange(echo_path_variability); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | std::array<float, kFftLengthBy2Plus1> Y2; |
| 150 | std::array<float, kFftLengthBy2Plus1> S2_power; |
| 151 | std::array<float, kFftLengthBy2Plus1> R2; |
| 152 | std::array<float, kFftLengthBy2Plus1> S2_linear; |
| 153 | std::array<float, kFftLengthBy2Plus1> G; |
| 154 | FftData X; |
| 155 | FftData Y; |
| 156 | FftData comfort_noise; |
| 157 | FftData high_band_comfort_noise; |
| 158 | SubtractorOutput subtractor_output; |
| 159 | FftData& E_main = subtractor_output.E_main; |
| 160 | auto& E2_main = subtractor_output.E2_main; |
| 161 | auto& E2_shadow = subtractor_output.E2_shadow; |
| 162 | auto& e_main = subtractor_output.e_main; |
| 163 | auto& e_shadow = subtractor_output.e_shadow; |
| 164 | |
| 165 | // Update the render signal buffer. |
| 166 | fft_.PaddedFft(x0, x_old_, &X); |
| 167 | X_buffer_.Insert(X); |
| 168 | |
| 169 | // Analyze the render signal. |
| 170 | render_signal_analyzer_.Update(X_buffer_, aec_state_.FilterDelay()); |
| 171 | |
| 172 | // Perform linear echo cancellation. |
| 173 | subtractor_.Process(X_buffer_, y0, render_signal_analyzer_, |
| 174 | aec_state_.SaturatedCapture(), &subtractor_output); |
| 175 | |
| 176 | // Compute spectra. |
| 177 | fft_.ZeroPaddedFft(y0, &Y); |
| 178 | LinearEchoPower(E_main, Y, &S2_linear); |
| 179 | Y.Spectrum(optimization_, &Y2); |
| 180 | |
| 181 | // Update the AEC state information. |
| 182 | aec_state_.Update(subtractor_.FilterFrequencyResponse(), |
| 183 | echo_path_delay_samples, X_buffer_, E2_main, E2_shadow, Y2, |
| 184 | x0, echo_path_variability, echo_leakage_detected_); |
| 185 | |
| 186 | // Use the power model to estimate the echo. |
| 187 | power_echo_model_.EstimateEcho(X_buffer_, Y2, aec_state_, &S2_power); |
| 188 | |
| 189 | // Choose the linear output. |
| 190 | output_selector_.FormLinearOutput(e_main, y0); |
| 191 | data_dumper_->DumpWav("aec3_output_linear", kBlockSize, &y0[0], |
| 192 | LowestBandRate(sample_rate_hz_), 1); |
| 193 | const auto& E2 = output_selector_.UseSubtractorOutput() ? E2_main : Y2; |
| 194 | |
| 195 | // Estimate the residual echo power. |
| 196 | residual_echo_estimator_.Estimate( |
| 197 | output_selector_.UseSubtractorOutput(), aec_state_, X_buffer_, |
| 198 | subtractor_.FilterFrequencyResponse(), E2_main, E2_shadow, S2_linear, |
| 199 | S2_power, Y2, &R2); |
| 200 | |
| 201 | // Estimate the comfort noise. |
| 202 | cng_.Compute(aec_state_, Y2, &comfort_noise, &high_band_comfort_noise); |
| 203 | |
| 204 | // Detect basic doubletalk. |
| 205 | const bool doubletalk = BlockPower(e_shadow) < BlockPower(e_main); |
| 206 | |
| 207 | // A choose and apply echo suppression gain. |
| 208 | suppression_gain_.GetGain(E2, R2, cng_.NoiseSpectrum(), |
| 209 | doubletalk ? 0.001f : 0.0001f, &G); |
| 210 | suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G, y); |
| 211 | |
| 212 | // Debug outputs for the purpose of development and analysis. |
| 213 | data_dumper_->DumpRaw("aec3_N2", cng_.NoiseSpectrum()); |
| 214 | data_dumper_->DumpRaw("aec3_suppressor_gain", G); |
| 215 | data_dumper_->DumpWav("aec3_output", |
| 216 | rtc::ArrayView<const float>(&y0[0], kBlockSize), |
| 217 | LowestBandRate(sample_rate_hz_), 1); |
| 218 | data_dumper_->DumpRaw("aec3_using_subtractor_output", |
| 219 | output_selector_.UseSubtractorOutput() ? 1 : 0); |
| 220 | data_dumper_->DumpRaw("aec3_doubletalk", doubletalk ? 1 : 0); |
| 221 | data_dumper_->DumpRaw("aec3_E2", E2); |
| 222 | data_dumper_->DumpRaw("aec3_E2_main", E2_main); |
| 223 | data_dumper_->DumpRaw("aec3_E2_shadow", E2_shadow); |
| 224 | data_dumper_->DumpRaw("aec3_S2_linear", S2_linear); |
| 225 | data_dumper_->DumpRaw("aec3_S2_power", S2_power); |
| 226 | data_dumper_->DumpRaw("aec3_Y2", Y2); |
| 227 | data_dumper_->DumpRaw("aec3_R2", R2); |
| 228 | data_dumper_->DumpRaw("aec3_erle", aec_state_.Erle()); |
| 229 | data_dumper_->DumpRaw("aec3_erl", aec_state_.Erl()); |
| 230 | data_dumper_->DumpRaw("aec3_reliable_filter_bands", |
| 231 | aec_state_.BandsWithReliableFilter()); |
| 232 | data_dumper_->DumpRaw("aec3_active_render", aec_state_.ActiveRender()); |
| 233 | data_dumper_->DumpRaw("aec3_model_based_aec_feasible", |
| 234 | aec_state_.ModelBasedAecFeasible()); |
| 235 | data_dumper_->DumpRaw("aec3_usable_linear_estimate", |
| 236 | aec_state_.UsableLinearEstimate()); |
| 237 | data_dumper_->DumpRaw( |
| 238 | "aec3_filter_delay", |
| 239 | aec_state_.FilterDelay() ? *aec_state_.FilterDelay() : -1); |
| 240 | data_dumper_->DumpRaw( |
| 241 | "aec3_external_delay", |
| 242 | aec_state_.ExternalDelay() ? *aec_state_.ExternalDelay() : -1); |
| 243 | data_dumper_->DumpRaw("aec3_capture_saturation", |
| 244 | aec_state_.SaturatedCapture() ? 1 : 0); |
| 245 | } |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 246 | |
| 247 | } // namespace |
| 248 | |
| 249 | EchoRemover* EchoRemover::Create(int sample_rate_hz) { |
| 250 | return new EchoRemoverImpl(sample_rate_hz); |
| 251 | } |
| 252 | |
| 253 | } // namespace webrtc |