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