blob: 910be18a124e1c854028bfef94b12804e589af19 [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#ifndef MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_
peah522d71b2017-02-23 05:16:26 -080013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <math.h>
15#include <stddef.h>
Yves Gerey665174f2018-06-19 15:03:05 +020016#include <array>
peah522d71b2017-02-23 05:16:26 -080017#include <vector>
18
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "api/array_view.h"
20#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/aec3/adaptive_fir_filter.h"
22#include "modules/audio_processing/aec3/aec3_common.h"
23#include "modules/audio_processing/aec3/aec3_fft.h"
24#include "modules/audio_processing/aec3/aec_state.h"
25#include "modules/audio_processing/aec3/echo_path_variability.h"
26#include "modules/audio_processing/aec3/main_filter_update_gain.h"
27#include "modules/audio_processing/aec3/render_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020028#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/aec3/shadow_filter_update_gain.h"
30#include "modules/audio_processing/aec3/subtractor_output.h"
31#include "modules/audio_processing/logging/apm_data_dumper.h"
Yves Gerey988cc082018-10-23 12:03:01 +020032#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/constructor_magic.h"
peah522d71b2017-02-23 05:16:26 -080034
35namespace webrtc {
36
37// Proves linear echo cancellation functionality
38class Subtractor {
39 public:
Per Åhgren09a718a2017-12-11 22:28:45 +010040 Subtractor(const EchoCanceller3Config& config,
41 ApmDataDumper* data_dumper,
42 Aec3Optimization optimization);
peah522d71b2017-02-23 05:16:26 -080043 ~Subtractor();
44
45 // Performs the echo subtraction.
peahcf02cf12017-04-05 14:18:07 -070046 void Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080047 const rtc::ArrayView<const float> capture,
48 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070049 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -080050 SubtractorOutput* output);
51
peah522d71b2017-02-23 05:16:26 -080052 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
53
Per Åhgrena98c8072018-01-15 19:17:16 +010054 // Exits the initial state.
55 void ExitInitialState();
56
peah29103572017-07-11 02:54:02 -070057 // Returns the block-wise frequency response for the main adaptive filter.
peah522d71b2017-02-23 05:16:26 -080058 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
59 FilterFrequencyResponse() const {
Per Åhgrenb20b9372018-07-13 00:22:54 +020060 return main_filter_.FilterFrequencyResponse();
peah522d71b2017-02-23 05:16:26 -080061 }
62
peah29103572017-07-11 02:54:02 -070063 // Returns the estimate of the impulse response for the main adaptive filter.
Per Åhgren09a718a2017-12-11 22:28:45 +010064 const std::vector<float>& FilterImpulseResponse() const {
Per Åhgrenb20b9372018-07-13 00:22:54 +020065 return main_filter_.FilterImpulseResponse();
peah29103572017-07-11 02:54:02 -070066 }
67
Per Åhgren5c532d32018-03-22 00:29:25 +010068 void DumpFilters() {
69 main_filter_.DumpFilter("aec3_subtractor_H_main", "aec3_subtractor_h_main");
70 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow",
71 "aec3_subtractor_h_shadow");
72 }
73
peah522d71b2017-02-23 05:16:26 -080074 private:
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020075 class FilterMisadjustmentEstimator {
76 public:
77 FilterMisadjustmentEstimator() = default;
78 ~FilterMisadjustmentEstimator() = default;
79 // Update the misadjustment estimator.
Per Åhgrene4db6a12018-07-26 15:32:24 +020080 void Update(const SubtractorOutput& output);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020081 // GetMisadjustment() Returns a recommended scale for the filter so the
82 // prediction error energy gets closer to the energy that is seen at the
83 // microphone input.
84 float GetMisadjustment() const {
85 RTC_DCHECK_GT(inv_misadjustment_, 0.0f);
86 // It is not aiming to adjust all the estimated mismatch. Instead,
87 // it adjusts half of that estimated mismatch.
88 return 2.f / sqrtf(inv_misadjustment_);
89 }
90 // Returns true if the prediciton error energy is significantly larger
91 // than the microphone signal energy and, therefore, an adjustment is
92 // recommended.
93 bool IsAdjustmentNeeded() const { return inv_misadjustment_ > 10.f; }
94 void Reset();
95 void Dump(ApmDataDumper* data_dumper) const;
96
97 private:
98 const int n_blocks_ = 4;
99 int n_blocks_acum_ = 0;
100 float e2_acum_ = 0.f;
101 float y2_acum_ = 0.f;
102 float inv_misadjustment_ = 0.f;
103 int overhang_ = 0.f;
104 };
105
peah522d71b2017-02-23 05:16:26 -0800106 const Aec3Fft fft_;
107 ApmDataDumper* data_dumper_;
108 const Aec3Optimization optimization_;
Per Åhgrena98c8072018-01-15 19:17:16 +0100109 const EchoCanceller3Config config_;
Per Åhgren22754392018-08-10 18:37:38 +0200110
peah522d71b2017-02-23 05:16:26 -0800111 AdaptiveFirFilter main_filter_;
112 AdaptiveFirFilter shadow_filter_;
113 MainFilterUpdateGain G_main_;
114 ShadowFilterUpdateGain G_shadow_;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200115 FilterMisadjustmentEstimator filter_misadjustment_estimator_;
Per Åhgrene4db6a12018-07-26 15:32:24 +0200116 size_t poor_shadow_filter_counter_ = 0;
peah522d71b2017-02-23 05:16:26 -0800117 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Subtractor);
118};
119
120} // namespace webrtc
121
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200122#endif // MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_