blob: 767e4aad4687f151d84ea2122bd6eb75c967f75e [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"
Per Åhgren9d661982020-03-20 11:26:48 +010026#include "modules/audio_processing/aec3/coarse_filter_update_gain.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/aec3/echo_path_variability.h"
Per Åhgrenff045112020-03-20 11:20:39 +010028#include "modules/audio_processing/aec3/refined_filter_update_gain.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/aec3/render_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020030#include "modules/audio_processing/aec3/render_signal_analyzer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#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 Åhgrenff045112020-03-20 11:20:39 +010061 // Returns the block-wise frequency responses for the refined adaptive
62 // filters.
Per Åhgrenb441acf2019-10-05 09:07:24 +020063 const std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>&
Per Åhgren119e2192019-10-18 08:50:50 +020064 FilterFrequencyResponses() const {
Per Åhgrenff045112020-03-20 11:20:39 +010065 return refined_frequency_responses_;
peah522d71b2017-02-23 05:16:26 -080066 }
67
Per Åhgrenff045112020-03-20 11:20:39 +010068 // Returns the estimates of the impulse responses for the refined adaptive
Per Åhgren7bdf0732019-09-25 14:53:30 +020069 // filters.
Per Åhgren119e2192019-10-18 08:50:50 +020070 const std::vector<std::vector<float>>& FilterImpulseResponses() const {
Per Åhgrenff045112020-03-20 11:20:39 +010071 return refined_impulse_responses_;
peah29103572017-07-11 02:54:02 -070072 }
73
Per Åhgren5c532d32018-03-22 00:29:25 +010074 void DumpFilters() {
Per Åhgrenb441acf2019-10-05 09:07:24 +020075 data_dumper_->DumpRaw(
Per Åhgrenff045112020-03-20 11:20:39 +010076 "aec3_subtractor_h_refined",
Per Åhgrenb441acf2019-10-05 09:07:24 +020077 rtc::ArrayView<const float>(
Per Åhgrenff045112020-03-20 11:20:39 +010078 refined_impulse_responses_[0].data(),
Per Åhgrenb441acf2019-10-05 09:07:24 +020079 GetTimeDomainLength(
Per Åhgrenff045112020-03-20 11:20:39 +010080 refined_filters_[0]->max_filter_size_partitions())));
Per Åhgren91a892f2021-05-07 23:28:42 +000081 if (ApmDataDumper::IsAvailable()) {
82 RTC_DCHECK_GT(coarse_impulse_responses_.size(), 0);
83 data_dumper_->DumpRaw(
84 "aec3_subtractor_h_coarse",
85 rtc::ArrayView<const float>(
86 coarse_impulse_responses_[0].data(),
87 GetTimeDomainLength(
88 coarse_filter_[0]->max_filter_size_partitions())));
89 }
Per Åhgrend4e69042019-09-05 15:55:58 +020090
Per Åhgrenff045112020-03-20 11:20:39 +010091 refined_filters_[0]->DumpFilter("aec3_subtractor_H_refined");
Per Åhgren9d661982020-03-20 11:26:48 +010092 coarse_filter_[0]->DumpFilter("aec3_subtractor_H_coarse");
Per Åhgren5c532d32018-03-22 00:29:25 +010093 }
94
peah522d71b2017-02-23 05:16:26 -080095 private:
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +020096 class FilterMisadjustmentEstimator {
97 public:
98 FilterMisadjustmentEstimator() = default;
99 ~FilterMisadjustmentEstimator() = default;
100 // Update the misadjustment estimator.
Per Åhgrene4db6a12018-07-26 15:32:24 +0200101 void Update(const SubtractorOutput& output);
Jesús de Vicente Peña2e79d2b2018-06-29 16:35:08 +0200102 // GetMisadjustment() Returns a recommended scale for the filter so the
103 // prediction error energy gets closer to the energy that is seen at the
104 // microphone input.
105 float GetMisadjustment() const {
106 RTC_DCHECK_GT(inv_misadjustment_, 0.0f);
107 // It is not aiming to adjust all the estimated mismatch. Instead,
108 // it adjusts half of that estimated mismatch.
109 return 2.f / sqrtf(inv_misadjustment_);
110 }
111 // Returns true if the prediciton error energy is significantly larger
112 // than the microphone signal energy and, therefore, an adjustment is
113 // recommended.
114 bool IsAdjustmentNeeded() const { return inv_misadjustment_ > 10.f; }
115 void Reset();
116 void Dump(ApmDataDumper* data_dumper) const;
117
118 private:
119 const int n_blocks_ = 4;
120 int n_blocks_acum_ = 0;
121 float e2_acum_ = 0.f;
122 float y2_acum_ = 0.f;
123 float inv_misadjustment_ = 0.f;
124 int overhang_ = 0.f;
125 };
126
peah522d71b2017-02-23 05:16:26 -0800127 const Aec3Fft fft_;
128 ApmDataDumper* data_dumper_;
129 const Aec3Optimization optimization_;
Per Åhgrena98c8072018-01-15 19:17:16 +0100130 const EchoCanceller3Config config_;
Per Åhgren7bdf0732019-09-25 14:53:30 +0200131 const size_t num_capture_channels_;
Gustaf Ullberg992a96f2020-12-08 13:03:55 +0100132 const bool use_coarse_filter_reset_hangover_;
Per Åhgren22754392018-08-10 18:37:38 +0200133
Per Åhgrenff045112020-03-20 11:20:39 +0100134 std::vector<std::unique_ptr<AdaptiveFirFilter>> refined_filters_;
Per Åhgren9d661982020-03-20 11:26:48 +0100135 std::vector<std::unique_ptr<AdaptiveFirFilter>> coarse_filter_;
Per Åhgrenff045112020-03-20 11:20:39 +0100136 std::vector<std::unique_ptr<RefinedFilterUpdateGain>> refined_gains_;
Per Åhgren9d661982020-03-20 11:26:48 +0100137 std::vector<std::unique_ptr<CoarseFilterUpdateGain>> coarse_gains_;
Per Åhgren119e2192019-10-18 08:50:50 +0200138 std::vector<FilterMisadjustmentEstimator> filter_misadjustment_estimators_;
Per Åhgren9d661982020-03-20 11:26:48 +0100139 std::vector<size_t> poor_coarse_filter_counters_;
Gustaf Ullberg992a96f2020-12-08 13:03:55 +0100140 std::vector<int> coarse_filter_reset_hangover_;
Per Åhgren7bdf0732019-09-25 14:53:30 +0200141 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
Per Åhgrenff045112020-03-20 11:20:39 +0100142 refined_frequency_responses_;
143 std::vector<std::vector<float>> refined_impulse_responses_;
Per Åhgren91a892f2021-05-07 23:28:42 +0000144 std::vector<std::vector<float>> coarse_impulse_responses_;
peah522d71b2017-02-23 05:16:26 -0800145};
146
147} // namespace webrtc
148
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200149#endif // MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_