blob: 33fdde5809790a282cd43657de116778f448e71a [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>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/aec3/adaptive_fir_filter.h"
19#include "modules/audio_processing/aec3/aec3_common.h"
20#include "modules/audio_processing/aec3/aec3_fft.h"
21#include "modules/audio_processing/aec3/aec_state.h"
22#include "modules/audio_processing/aec3/echo_path_variability.h"
23#include "modules/audio_processing/aec3/main_filter_update_gain.h"
24#include "modules/audio_processing/aec3/render_buffer.h"
25#include "modules/audio_processing/aec3/shadow_filter_update_gain.h"
26#include "modules/audio_processing/aec3/subtractor_output.h"
27#include "modules/audio_processing/logging/apm_data_dumper.h"
28#include "modules/audio_processing/utility/ooura_fft.h"
29#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080030
31namespace webrtc {
32
33// Proves linear echo cancellation functionality
34class Subtractor {
35 public:
Per Åhgren09a718a2017-12-11 22:28:45 +010036 Subtractor(const EchoCanceller3Config& config,
37 ApmDataDumper* data_dumper,
38 Aec3Optimization optimization);
peah522d71b2017-02-23 05:16:26 -080039 ~Subtractor();
40
41 // Performs the echo subtraction.
peahcf02cf12017-04-05 14:18:07 -070042 void Process(const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080043 const rtc::ArrayView<const float> capture,
44 const RenderSignalAnalyzer& render_signal_analyzer,
peah86afe9d2017-04-06 15:45:32 -070045 const AecState& aec_state,
peah522d71b2017-02-23 05:16:26 -080046 SubtractorOutput* output);
47
peah522d71b2017-02-23 05:16:26 -080048 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
49
Per Åhgrena98c8072018-01-15 19:17:16 +010050 // Exits the initial state.
51 void ExitInitialState();
52
peah29103572017-07-11 02:54:02 -070053 // Returns the block-wise frequency response for the main adaptive filter.
peah522d71b2017-02-23 05:16:26 -080054 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
55 FilterFrequencyResponse() const {
Per Åhgren5c532d32018-03-22 00:29:25 +010056 return main_filter_once_converged_ || (!shadow_filter_converged_)
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010057 ? main_filter_.FilterFrequencyResponse()
58 : shadow_filter_.FilterFrequencyResponse();
peah522d71b2017-02-23 05:16:26 -080059 }
60
peah29103572017-07-11 02:54:02 -070061 // Returns the estimate of the impulse response for the main adaptive filter.
Per Åhgren09a718a2017-12-11 22:28:45 +010062 const std::vector<float>& FilterImpulseResponse() const {
Per Åhgren5c532d32018-03-22 00:29:25 +010063 return main_filter_once_converged_ || (!shadow_filter_converged_)
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010064 ? main_filter_.FilterImpulseResponse()
65 : shadow_filter_.FilterImpulseResponse();
peah29103572017-07-11 02:54:02 -070066 }
67
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010068 bool ConvergedFilter() const {
69 return main_filter_converged_ || shadow_filter_converged_;
70 }
Per Åhgren1b4059e2017-10-15 20:19:21 +020071
Per Åhgren5c532d32018-03-22 00:29:25 +010072 bool DivergedFilter() const { return main_filter_diverged_; }
73
74 void DumpFilters() {
75 main_filter_.DumpFilter("aec3_subtractor_H_main", "aec3_subtractor_h_main");
76 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow",
77 "aec3_subtractor_h_shadow");
78 }
79
peah522d71b2017-02-23 05:16:26 -080080 private:
peah522d71b2017-02-23 05:16:26 -080081 const Aec3Fft fft_;
82 ApmDataDumper* data_dumper_;
83 const Aec3Optimization optimization_;
Per Åhgrena98c8072018-01-15 19:17:16 +010084 const EchoCanceller3Config config_;
peah522d71b2017-02-23 05:16:26 -080085 AdaptiveFirFilter main_filter_;
86 AdaptiveFirFilter shadow_filter_;
87 MainFilterUpdateGain G_main_;
88 ShadowFilterUpdateGain G_shadow_;
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010089 bool main_filter_converged_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +010090 bool main_filter_once_converged_ = false;
Per Åhgrenb5adc9e2018-01-15 13:20:20 +010091 bool shadow_filter_converged_ = false;
Per Åhgren5c532d32018-03-22 00:29:25 +010092 bool main_filter_diverged_ = false;
peah522d71b2017-02-23 05:16:26 -080093 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Subtractor);
94};
95
96} // namespace webrtc
97
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020098#endif // MODULES_AUDIO_PROCESSING_AEC3_SUBTRACTOR_H_