1 /*
2  *  Copyright (c) 2016 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_RESIDUAL_ECHO_DETECTOR_H_
12 #define MODULES_AUDIO_PROCESSING_RESIDUAL_ECHO_DETECTOR_H_
13 
14 #include <vector>
15 
16 #include "api/array_view.h"
17 #include "modules/audio_processing/echo_detector/circular_buffer.h"
18 #include "modules/audio_processing/echo_detector/mean_variance_estimator.h"
19 #include "modules/audio_processing/echo_detector/moving_max.h"
20 #include "modules/audio_processing/echo_detector/normalized_covariance_estimator.h"
21 #include "modules/audio_processing/include/audio_processing.h"
22 
23 namespace webrtc {
24 
25 class ApmDataDumper;
26 class AudioBuffer;
27 
28 class ResidualEchoDetector : public EchoDetector {
29  public:
30   ResidualEchoDetector();
31   ~ResidualEchoDetector() override;
32 
33   // This function should be called while holding the render lock.
34   void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override;
35 
36   // This function should be called while holding the capture lock.
37   void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override;
38 
39   // This function should be called while holding the capture lock.
40   void Initialize(int capture_sample_rate_hz,
41                   int num_capture_channels,
42                   int render_sample_rate_hz,
43                   int num_render_channels) override;
44 
45   // This function is for testing purposes only.
SetReliabilityForTest(float value)46   void SetReliabilityForTest(float value) { reliability_ = value; }
47 
48   // This function should be called while holding the capture lock.
49   EchoDetector::Metrics GetMetrics() const override;
50 
51  private:
52   static int instance_count_;
53   std::unique_ptr<ApmDataDumper> data_dumper_;
54   // Keep track if the |Process| function has been previously called.
55   bool first_process_call_ = true;
56   // Buffer for storing the power of incoming farend buffers. This is needed for
57   // cases where calls to BufferFarend and Process are jittery.
58   CircularBuffer render_buffer_;
59   // Count how long ago it was that the size of |render_buffer_| was zero. This
60   // value is also reset to zero when clock drift is detected and a value from
61   // the renderbuffer is discarded, even though the buffer is not actually zero
62   // at that point. This is done to avoid repeatedly removing elements in this
63   // situation.
64   size_t frames_since_zero_buffer_size_ = 0;
65 
66   // Circular buffers containing delayed versions of the power, mean and
67   // standard deviation, for calculating the delayed covariance values.
68   std::vector<float> render_power_;
69   std::vector<float> render_power_mean_;
70   std::vector<float> render_power_std_dev_;
71   // Covariance estimates for different delay values.
72   std::vector<NormalizedCovarianceEstimator> covariances_;
73   // Index where next element should be inserted in all of the above circular
74   // buffers.
75   size_t next_insertion_index_ = 0;
76 
77   MeanVarianceEstimator render_statistics_;
78   MeanVarianceEstimator capture_statistics_;
79   // Current echo likelihood.
80   float echo_likelihood_ = 0.f;
81   // Reliability of the current likelihood.
82   float reliability_ = 0.f;
83   MovingMax recent_likelihood_max_;
84 
85   int log_counter_ = 0;
86 };
87 
88 }  // namespace webrtc
89 
90 #endif  // MODULES_AUDIO_PROCESSING_RESIDUAL_ECHO_DETECTOR_H_
91