blob: de1b989110f0268d6e2f5b7962e290dc562bbd2d [file] [log] [blame]
ivoc9f4a4a02016-10-28 05:39:16 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_RESIDUAL_ECHO_DETECTOR_H_
12#define MODULES_AUDIO_PROCESSING_RESIDUAL_ECHO_DETECTOR_H_
ivoc9f4a4a02016-10-28 05:39:16 -070013
ivoc9f4a4a02016-10-28 05:39:16 -070014#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#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"
ivoc9f4a4a02016-10-28 05:39:16 -070021
22namespace webrtc {
23
peah9e6a2902017-05-15 07:19:21 -070024class ApmDataDumper;
ivoc9f4a4a02016-10-28 05:39:16 -070025class AudioBuffer;
26class EchoDetector;
27
28class ResidualEchoDetector {
29 public:
30 ResidualEchoDetector();
31 ~ResidualEchoDetector();
32
33 // This function should be called while holding the render lock.
ivocaf27ed02016-10-28 07:04:03 -070034 void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio);
ivoc9f4a4a02016-10-28 05:39:16 -070035
36 // This function should be called while holding the capture lock.
37 void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio);
38
39 // This function should be called while holding the capture lock.
40 void Initialize();
41
ivocfbb374d2016-11-17 06:19:47 -080042 // This function is for testing purposes only.
43 void SetReliabilityForTest(float value) { reliability_ = value; }
44
ivoc9f4a4a02016-10-28 05:39:16 -070045 static void PackRenderAudioBuffer(AudioBuffer* audio,
46 std::vector<float>* packed_buffer);
47
48 // This function should be called while holding the capture lock.
ivocaf27ed02016-10-28 07:04:03 -070049 float echo_likelihood() const { return echo_likelihood_; }
50
ivoc4e477a12017-01-15 08:29:46 -080051 float echo_likelihood_recent_max() const {
52 return recent_likelihood_max_.max();
53 }
54
ivocaf27ed02016-10-28 07:04:03 -070055 private:
peah9e6a2902017-05-15 07:19:21 -070056 static int instance_count_;
57 std::unique_ptr<ApmDataDumper> data_dumper_;
ivocaf27ed02016-10-28 07:04:03 -070058 // Keep track if the |Process| function has been previously called.
59 bool first_process_call_ = true;
60 // Buffer for storing the power of incoming farend buffers. This is needed for
61 // cases where calls to BufferFarend and Process are jittery.
62 CircularBuffer render_buffer_;
63 // Count how long ago it was that the size of |render_buffer_| was zero. This
64 // value is also reset to zero when clock drift is detected and a value from
65 // the renderbuffer is discarded, even though the buffer is not actually zero
66 // at that point. This is done to avoid repeatedly removing elements in this
67 // situation.
68 size_t frames_since_zero_buffer_size_ = 0;
69
70 // Circular buffers containing delayed versions of the power, mean and
71 // standard deviation, for calculating the delayed covariance values.
72 std::vector<float> render_power_;
73 std::vector<float> render_power_mean_;
74 std::vector<float> render_power_std_dev_;
75 // Covariance estimates for different delay values.
76 std::vector<NormalizedCovarianceEstimator> covariances_;
77 // Index where next element should be inserted in all of the above circular
78 // buffers.
79 size_t next_insertion_index_ = 0;
80
81 MeanVarianceEstimator render_statistics_;
82 MeanVarianceEstimator capture_statistics_;
83 // Current echo likelihood.
84 float echo_likelihood_ = 0.f;
ivocfbb374d2016-11-17 06:19:47 -080085 // Reliability of the current likelihood.
86 float reliability_ = 0.f;
ivoc4e477a12017-01-15 08:29:46 -080087 MovingMax recent_likelihood_max_;
ivoc1592c742017-05-17 09:53:02 -070088
89 int log_counter_ = 0;
ivoc9f4a4a02016-10-28 05:39:16 -070090};
91
92} // namespace webrtc
93
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020094#endif // MODULES_AUDIO_PROCESSING_RESIDUAL_ECHO_DETECTOR_H_