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 
11 #ifndef MODULES_AUDIO_PROCESSING_AEC3_REFINED_FILTER_UPDATE_GAIN_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_REFINED_FILTER_UPDATE_GAIN_H_
13 
14 #include <stddef.h>
15 
16 #include <array>
17 #include <memory>
18 
19 #include "api/array_view.h"
20 #include "api/audio/echo_canceller3_config.h"
21 #include "modules/audio_processing/aec3/aec3_common.h"
22 
23 namespace webrtc {
24 
25 class AdaptiveFirFilter;
26 class ApmDataDumper;
27 struct EchoPathVariability;
28 struct FftData;
29 class RenderSignalAnalyzer;
30 struct SubtractorOutput;
31 
32 // Provides functionality for  computing the adaptive gain for the refined
33 // filter.
34 class RefinedFilterUpdateGain {
35  public:
36   RefinedFilterUpdateGain(
37       const EchoCanceller3Config::Filter::RefinedConfiguration& config,
38       size_t config_change_duration_blocks);
39   ~RefinedFilterUpdateGain();
40 
41   RefinedFilterUpdateGain(const RefinedFilterUpdateGain&) = delete;
42   RefinedFilterUpdateGain& operator=(const RefinedFilterUpdateGain&) = delete;
43 
44   // Takes action in the case of a known echo path change.
45   void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
46 
47   // Computes the gain.
48   void Compute(const std::array<float, kFftLengthBy2Plus1>& render_power,
49                const RenderSignalAnalyzer& render_signal_analyzer,
50                const SubtractorOutput& subtractor_output,
51                rtc::ArrayView<const float> erl,
52                size_t size_partitions,
53                bool saturated_capture_signal,
54                FftData* gain_fft);
55 
56   // Sets a new config.
SetConfig(const EchoCanceller3Config::Filter::RefinedConfiguration & config,bool immediate_effect)57   void SetConfig(
58       const EchoCanceller3Config::Filter::RefinedConfiguration& config,
59       bool immediate_effect) {
60     if (immediate_effect) {
61       old_target_config_ = current_config_ = target_config_ = config;
62       config_change_counter_ = 0;
63     } else {
64       old_target_config_ = current_config_;
65       target_config_ = config;
66       config_change_counter_ = config_change_duration_blocks_;
67     }
68   }
69 
70  private:
71   static int instance_count_;
72   std::unique_ptr<ApmDataDumper> data_dumper_;
73   const int config_change_duration_blocks_;
74   float one_by_config_change_duration_blocks_;
75   EchoCanceller3Config::Filter::RefinedConfiguration current_config_;
76   EchoCanceller3Config::Filter::RefinedConfiguration target_config_;
77   EchoCanceller3Config::Filter::RefinedConfiguration old_target_config_;
78   std::array<float, kFftLengthBy2Plus1> H_error_;
79   size_t poor_excitation_counter_;
80   size_t call_counter_ = 0;
81   int config_change_counter_ = 0;
82 
83   // Updates the current config towards the target config.
84   void UpdateCurrentConfig();
85 };
86 
87 }  // namespace webrtc
88 
89 #endif  // MODULES_AUDIO_PROCESSING_AEC3_REFINED_FILTER_UPDATE_GAIN_H_
90