blob: 32c42cccc15c8655c6596624befd33f5c90bd6ea [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Yves Gerey665174f2018-06-19 15:03:05 +020017#include <array>
peah522d71b2017-02-23 05:16:26 -080018#include <vector>
19
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "api/array_view.h"
21#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/aec3/adaptive_fir_filter.h"
23#include "modules/audio_processing/aec3/aec3_common.h"
24#include "modules/audio_processing/aec3/aec3_fft.h"
25#include "modules/audio_processing/aec3/aec_state.h"
26#include "modules/audio_processing/aec3/echo_path_variability.h"
27#include "modules/audio_processing/aec3/main_filter_update_gain.h"
28#include "modules/audio_processing/aec3/render_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020029#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_processing/aec3/shadow_filter_update_gain.h"
31#include "modules/audio_processing/aec3/subtractor_output.h"
32#include "modules/audio_processing/logging/apm_data_dumper.h"
Yves Gerey988cc082018-10-23 12:03:01 +020033#include "rtc_base/checks.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,
Per Åhgrena33dc012019-09-03 23:59:52 +020041 size_t num_render_channels,
42 size_t num_capture_channels,
Per Åhgren09a718a2017-12-11 22:28:45 +010043 ApmDataDumper* data_dumper,
44 Aec3Optimization optimization);
peah522d71b2017-02-23 05:16:26 -080045 ~Subtractor();
Per Åhgrend4e69042019-09-05 15:55:58 +020046 Subtractor(const Subtractor&) = delete;
47 Subtractor& operator=(const Subtractor&) = delete;
peah522d71b2017-02-23 05:16:26 -080048
49 // Performs the echo subtraction.
peahcf02cf12017-04-05 14:18:07 -070050 void Process(const RenderBuffer& render_buffer,
Per Åhgren7bdf0732019-09-25 14:53:30 +020051 const std::vector<std::vector<float>>& capture,
peah522d71b2017-02-23 05:16:26 -080052 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070053 const AecState& aec_state,
Per Åhgren7bdf0732019-09-25 14:53:30 +020054 rtc::ArrayView<SubtractorOutput> outputs);
peah522d71b2017-02-23 05:16:26 -080055
peah522d71b2017-02-23 05:16:26 -080056 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
57
Per Åhgrena98c8072018-01-15 19:17:16 +010058 // Exits the initial state.
59 void ExitInitialState();
60
Per Åhgren7bdf0732019-09-25 14:53:30 +020061 // Returns the block-wise frequency responses for the main adaptive filters.
Per Åhgrenb441acf2019-10-05 09:07:24 +020062 const std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>&
Per Åhgren119e2192019-10-18 08:50:50 +020063 FilterFrequencyResponses() const {
64 return main_frequency_responses_;
peah522d71b2017-02-23 05:16:26 -080065 }
66
Per Åhgren7bdf0732019-09-25 14:53:30 +020067 // Returns the estimates of the impulse responses for the main adaptive
68 // filters.
Per Åhgren119e2192019-10-18 08:50:50 +020069 const std::vector<std::vector<float>>& FilterImpulseResponses() const {
70 return main_impulse_responses_;
peah29103572017-07-11 02:54:02 -070071 }
72
Per Åhgren5c532d32018-03-22 00:29:25 +010073 void DumpFilters() {
Per Åhgrenb441acf2019-10-05 09:07:24 +020074 data_dumper_->DumpRaw(
75 "aec3_subtractor_h_main",
76 rtc::ArrayView<const float>(
Per Åhgren119e2192019-10-18 08:50:50 +020077 main_impulse_responses_[0].data(),
Per Åhgrenb441acf2019-10-05 09:07:24 +020078 GetTimeDomainLength(
Per Åhgren119e2192019-10-18 08:50:50 +020079 main_filters_[0]->max_filter_size_partitions())));
Per Åhgrend4e69042019-09-05 15:55:58 +020080
Per Åhgren119e2192019-10-18 08:50:50 +020081 main_filters_[0]->DumpFilter("aec3_subtractor_H_main");
Per Åhgren7bdf0732019-09-25 14:53:30 +020082 shadow_filter_[0]->DumpFilter("aec3_subtractor_H_shadow");
Per Åhgren5c532d32018-03-22 00:29:25 +010083 }
84
peah522d71b2017-02-23 05:16:26 -080085 private:
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020086 class FilterMisadjustmentEstimator {
87 public:
88 FilterMisadjustmentEstimator() = default;
89 ~FilterMisadjustmentEstimator() = default;
90 // Update the misadjustment estimator.
Per Åhgrene4db6a12018-07-26 15:32:24 +020091 void Update(const SubtractorOutput& output);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020092 // GetMisadjustment() Returns a recommended scale for the filter so the
93 // prediction error energy gets closer to the energy that is seen at the
94 // microphone input.
95 float GetMisadjustment() const {
96 RTC_DCHECK_GT(inv_misadjustment_, 0.0f);
97 // It is not aiming to adjust all the estimated mismatch. Instead,
98 // it adjusts half of that estimated mismatch.
99 return 2.f / sqrtf(inv_misadjustment_);
100 }
101 // Returns true if the prediciton error energy is significantly larger
102 // than the microphone signal energy and, therefore, an adjustment is
103 // recommended.
104 bool IsAdjustmentNeeded() const { return inv_misadjustment_ > 10.f; }
105 void Reset();
106 void Dump(ApmDataDumper* data_dumper) const;
107
108 private:
109 const int n_blocks_ = 4;
110 int n_blocks_acum_ = 0;
111 float e2_acum_ = 0.f;
112 float y2_acum_ = 0.f;
113 float inv_misadjustment_ = 0.f;
114 int overhang_ = 0.f;
115 };
116
peah522d71b2017-02-23 05:16:26 -0800117 const Aec3Fft fft_;
118 ApmDataDumper* data_dumper_;
119 const Aec3Optimization optimization_;
Per Åhgrena98c8072018-01-15 19:17:16 +0100120 const EchoCanceller3Config config_;
Per Åhgren7bdf0732019-09-25 14:53:30 +0200121 const size_t num_capture_channels_;
Per Åhgren22754392018-08-10 18:37:38 +0200122
Per Åhgren119e2192019-10-18 08:50:50 +0200123 std::vector<std::unique_ptr<AdaptiveFirFilter>> main_filters_;
Per Åhgren7bdf0732019-09-25 14:53:30 +0200124 std::vector<std::unique_ptr<AdaptiveFirFilter>> shadow_filter_;
Per Åhgren119e2192019-10-18 08:50:50 +0200125 std::vector<std::unique_ptr<MainFilterUpdateGain>> main_gains_;
126 std::vector<std::unique_ptr<ShadowFilterUpdateGain>> shadow_gains_;
127 std::vector<FilterMisadjustmentEstimator> filter_misadjustment_estimators_;
128 std::vector<size_t> poor_shadow_filter_counters_;
Per Åhgren7bdf0732019-09-25 14:53:30 +0200129 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
Per Åhgren119e2192019-10-18 08:50:50 +0200130 main_frequency_responses_;
131 std::vector<std::vector<float>> main_impulse_responses_;
peah522d71b2017-02-23 05:16:26 -0800132};
133
134} // namespace webrtc
135
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200136#endif // MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_