blob: 8ecda2db468f2be43f8f312584033caac28285ef [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
peah522d71b2017-02-23 05:16:26 -080014#include <algorithm>
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <array>
peah522d71b2017-02-23 05:16:26 -080016#include <vector>
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020017#include "math.h"
peah522d71b2017-02-23 05:16:26 -080018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/aec3/adaptive_fir_filter.h"
20#include "modules/audio_processing/aec3/aec3_common.h"
21#include "modules/audio_processing/aec3/aec3_fft.h"
22#include "modules/audio_processing/aec3/aec_state.h"
23#include "modules/audio_processing/aec3/echo_path_variability.h"
24#include "modules/audio_processing/aec3/main_filter_update_gain.h"
25#include "modules/audio_processing/aec3/render_buffer.h"
26#include "modules/audio_processing/aec3/shadow_filter_update_gain.h"
27#include "modules/audio_processing/aec3/subtractor_output.h"
28#include "modules/audio_processing/logging/apm_data_dumper.h"
29#include "modules/audio_processing/utility/ooura_fft.h"
30#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080031
32namespace webrtc {
33
34// Proves linear echo cancellation functionality
35class Subtractor {
36 public:
Per Åhgren09a718a2017-12-11 22:28:45 +010037 Subtractor(const EchoCanceller3Config& config,
38 ApmDataDumper* data_dumper,
39 Aec3Optimization optimization);
peah522d71b2017-02-23 05:16:26 -080040 ~Subtractor();
41
42 // Performs the echo subtraction.
peahcf02cf12017-04-05 14:18:07 -070043 void Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080044 const rtc::ArrayView<const float> capture,
45 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070046 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -080047 SubtractorOutput* output);
48
peah522d71b2017-02-23 05:16:26 -080049 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
50
Per Åhgrena98c8072018-01-15 19:17:16 +010051 // Exits the initial state.
52 void ExitInitialState();
53
peah29103572017-07-11 02:54:02 -070054 // Returns the block-wise frequency response for the main adaptive filter.
peah522d71b2017-02-23 05:16:26 -080055 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
56 FilterFrequencyResponse() const {
Per Åhgren5c532d32018-03-22 00:29:25 +010057 return main_filter_once_converged_ || (!shadow_filter_converged_)
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010058 ? main_filter_.FilterFrequencyResponse()
59 : shadow_filter_.FilterFrequencyResponse();
peah522d71b2017-02-23 05:16:26 -080060 }
61
peah29103572017-07-11 02:54:02 -070062 // Returns the estimate of the impulse response for the main adaptive filter.
Per Åhgren09a718a2017-12-11 22:28:45 +010063 const std::vector<float>& FilterImpulseResponse() const {
Per Åhgren5c532d32018-03-22 00:29:25 +010064 return main_filter_once_converged_ || (!shadow_filter_converged_)
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010065 ? main_filter_.FilterImpulseResponse()
66 : shadow_filter_.FilterImpulseResponse();
peah29103572017-07-11 02:54:02 -070067 }
68
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010069 bool ConvergedFilter() const {
70 return main_filter_converged_ || shadow_filter_converged_;
71 }
Per Åhgren1b4059e2017-10-15 20:19:21 +020072
Per Åhgren5c532d32018-03-22 00:29:25 +010073 bool DivergedFilter() const { return main_filter_diverged_; }
74
75 void DumpFilters() {
76 main_filter_.DumpFilter("aec3_subtractor_H_main", "aec3_subtractor_h_main");
77 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow",
78 "aec3_subtractor_h_shadow");
79 }
80
peah522d71b2017-02-23 05:16:26 -080081 private:
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020082 class FilterMisadjustmentEstimator {
83 public:
84 FilterMisadjustmentEstimator() = default;
85 ~FilterMisadjustmentEstimator() = default;
86 // Update the misadjustment estimator.
87 void Update(float e2, float y2);
88 // GetMisadjustment() Returns a recommended scale for the filter so the
89 // prediction error energy gets closer to the energy that is seen at the
90 // microphone input.
91 float GetMisadjustment() const {
92 RTC_DCHECK_GT(inv_misadjustment_, 0.0f);
93 // It is not aiming to adjust all the estimated mismatch. Instead,
94 // it adjusts half of that estimated mismatch.
95 return 2.f / sqrtf(inv_misadjustment_);
96 }
97 // Returns true if the prediciton error energy is significantly larger
98 // than the microphone signal energy and, therefore, an adjustment is
99 // recommended.
100 bool IsAdjustmentNeeded() const { return inv_misadjustment_ > 10.f; }
101 void Reset();
102 void Dump(ApmDataDumper* data_dumper) const;
103
104 private:
105 const int n_blocks_ = 4;
106 int n_blocks_acum_ = 0;
107 float e2_acum_ = 0.f;
108 float y2_acum_ = 0.f;
109 float inv_misadjustment_ = 0.f;
110 int overhang_ = 0.f;
111 };
112
peah522d71b2017-02-23 05:16:26 -0800113 const Aec3Fft fft_;
114 ApmDataDumper* data_dumper_;
115 const Aec3Optimization optimization_;
Per Åhgrena98c8072018-01-15 19:17:16 +0100116 const EchoCanceller3Config config_;
Per Åhgrenfc63c9e2018-06-28 13:23:23 +0200117 const bool adaptation_during_saturation_;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200118 const bool enable_misadjustment_estimator_;
peah522d71b2017-02-23 05:16:26 -0800119 AdaptiveFirFilter main_filter_;
120 AdaptiveFirFilter shadow_filter_;
121 MainFilterUpdateGain G_main_;
122 ShadowFilterUpdateGain G_shadow_;
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100123 bool main_filter_converged_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100124 bool main_filter_once_converged_ = false;
Per Åhgrenb5adc9e2018-01-15 13:20:20 +0100125 bool shadow_filter_converged_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +0100126 bool main_filter_diverged_ = false;
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200127 FilterMisadjustmentEstimator filter_misadjustment_estimator_;
peah522d71b2017-02-23 05:16:26 -0800128 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Subtractor);
129};
130
131} // namespace webrtc
132
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200133#endif // MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_