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