peah | 522d71b | 2017-02-23 05:16:26 -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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_ |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <math.h> |
| 15 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 17 | #include <array> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "api/array_view.h" |
| 21 | #include "api/audio/echo_canceller3_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #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 Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 29 | #include "modules/audio_processing/aec3/render_signal_analyzer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #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 Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 33 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "rtc_base/constructor_magic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 35 | |
| 36 | namespace webrtc { |
| 37 | |
| 38 | // Proves linear echo cancellation functionality |
| 39 | class Subtractor { |
| 40 | public: |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 41 | Subtractor(const EchoCanceller3Config& config, |
Per Åhgren | a33dc01 | 2019-09-03 23:59:52 +0200 | [diff] [blame^] | 42 | size_t num_render_channels, |
| 43 | size_t num_capture_channels, |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 44 | ApmDataDumper* data_dumper, |
| 45 | Aec3Optimization optimization); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 46 | ~Subtractor(); |
| 47 | |
| 48 | // Performs the echo subtraction. |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 49 | void Process(const RenderBuffer& render_buffer, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 50 | const rtc::ArrayView<const float> capture, |
| 51 | const RenderSignalAnalyzer& render_signal_analyzer, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 52 | const AecState& aec_state, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 53 | SubtractorOutput* output); |
| 54 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | void HandleEchoPathChange(const EchoPathVariability& echo_path_variability); |
| 56 | |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 57 | // Exits the initial state. |
| 58 | void ExitInitialState(); |
| 59 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 60 | // Returns the block-wise frequency response for the main adaptive filter. |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 61 | const std::vector<std::array<float, kFftLengthBy2Plus1>>& |
| 62 | FilterFrequencyResponse() const { |
Per Åhgren | b20b937 | 2018-07-13 00:22:54 +0200 | [diff] [blame] | 63 | return main_filter_.FilterFrequencyResponse(); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 64 | } |
| 65 | |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 66 | // Returns the estimate of the impulse response for the main adaptive filter. |
Per Åhgren | 09a718a | 2017-12-11 22:28:45 +0100 | [diff] [blame] | 67 | const std::vector<float>& FilterImpulseResponse() const { |
Per Åhgren | b20b937 | 2018-07-13 00:22:54 +0200 | [diff] [blame] | 68 | return main_filter_.FilterImpulseResponse(); |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Per Åhgren | 5c532d3 | 2018-03-22 00:29:25 +0100 | [diff] [blame] | 71 | void DumpFilters() { |
| 72 | main_filter_.DumpFilter("aec3_subtractor_H_main", "aec3_subtractor_h_main"); |
| 73 | shadow_filter_.DumpFilter("aec3_subtractor_H_shadow", |
| 74 | "aec3_subtractor_h_shadow"); |
| 75 | } |
| 76 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 77 | private: |
Jesús de Vicente Peña | 2e79d2b | 2018-06-29 16:35:08 +0200 | [diff] [blame] | 78 | class FilterMisadjustmentEstimator { |
| 79 | public: |
| 80 | FilterMisadjustmentEstimator() = default; |
| 81 | ~FilterMisadjustmentEstimator() = default; |
| 82 | // Update the misadjustment estimator. |
Per Åhgren | e4db6a1 | 2018-07-26 15:32:24 +0200 | [diff] [blame] | 83 | void Update(const SubtractorOutput& output); |
Jesús de Vicente Peña | 2e79d2b | 2018-06-29 16:35:08 +0200 | [diff] [blame] | 84 | // GetMisadjustment() Returns a recommended scale for the filter so the |
| 85 | // prediction error energy gets closer to the energy that is seen at the |
| 86 | // microphone input. |
| 87 | float GetMisadjustment() const { |
| 88 | RTC_DCHECK_GT(inv_misadjustment_, 0.0f); |
| 89 | // It is not aiming to adjust all the estimated mismatch. Instead, |
| 90 | // it adjusts half of that estimated mismatch. |
| 91 | return 2.f / sqrtf(inv_misadjustment_); |
| 92 | } |
| 93 | // Returns true if the prediciton error energy is significantly larger |
| 94 | // than the microphone signal energy and, therefore, an adjustment is |
| 95 | // recommended. |
| 96 | bool IsAdjustmentNeeded() const { return inv_misadjustment_ > 10.f; } |
| 97 | void Reset(); |
| 98 | void Dump(ApmDataDumper* data_dumper) const; |
| 99 | |
| 100 | private: |
| 101 | const int n_blocks_ = 4; |
| 102 | int n_blocks_acum_ = 0; |
| 103 | float e2_acum_ = 0.f; |
| 104 | float y2_acum_ = 0.f; |
| 105 | float inv_misadjustment_ = 0.f; |
| 106 | int overhang_ = 0.f; |
| 107 | }; |
| 108 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 109 | const Aec3Fft fft_; |
| 110 | ApmDataDumper* data_dumper_; |
| 111 | const Aec3Optimization optimization_; |
Per Åhgren | a98c807 | 2018-01-15 19:17:16 +0100 | [diff] [blame] | 112 | const EchoCanceller3Config config_; |
Per Åhgren | 2275439 | 2018-08-10 18:37:38 +0200 | [diff] [blame] | 113 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 114 | AdaptiveFirFilter main_filter_; |
| 115 | AdaptiveFirFilter shadow_filter_; |
| 116 | MainFilterUpdateGain G_main_; |
| 117 | ShadowFilterUpdateGain G_shadow_; |
Jesús de Vicente Peña | 2e79d2b | 2018-06-29 16:35:08 +0200 | [diff] [blame] | 118 | FilterMisadjustmentEstimator filter_misadjustment_estimator_; |
Per Åhgren | e4db6a1 | 2018-07-26 15:32:24 +0200 | [diff] [blame] | 119 | size_t poor_shadow_filter_counter_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 120 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Subtractor); |
| 121 | }; |
| 122 | |
| 123 | } // namespace webrtc |
| 124 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 125 | #endif // MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_ |