blob: e8ae552d6c0979e53c984a774eb3888bdc1d1e9e [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"
Ivo Creusen09fa4b02018-01-11 16:08:54 +010021#include "modules/audio_processing/include/audio_processing.h"
ivoc9f4a4a02016-10-28 05:39:16 -070022
23namespace webrtc {
24
peah9e6a2902017-05-15 07:19:21 -070025class ApmDataDumper;
ivoc9f4a4a02016-10-28 05:39:16 -070026class AudioBuffer;
ivoc9f4a4a02016-10-28 05:39:16 -070027
Ivo Creusen09fa4b02018-01-11 16:08:54 +010028class ResidualEchoDetector : public EchoDetector {
ivoc9f4a4a02016-10-28 05:39:16 -070029 public:
30 ResidualEchoDetector();
Ivo Creusen09fa4b02018-01-11 16:08:54 +010031 ~ResidualEchoDetector() override;
ivoc9f4a4a02016-10-28 05:39:16 -070032
33 // This function should be called while holding the render lock.
Ivo Creusen09fa4b02018-01-11 16:08:54 +010034 void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) override;
ivoc9f4a4a02016-10-28 05:39:16 -070035
36 // This function should be called while holding the capture lock.
Ivo Creusen09fa4b02018-01-11 16:08:54 +010037 void AnalyzeCaptureAudio(rtc::ArrayView<const float> capture_audio) override;
ivoc9f4a4a02016-10-28 05:39:16 -070038
39 // This function should be called while holding the capture lock.
Ivo Creusen09fa4b02018-01-11 16:08:54 +010040 void Initialize(int sample_rate_hz, int num_channels) override;
ivoc9f4a4a02016-10-28 05:39:16 -070041
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 // This function should be called while holding the capture lock.
Ivo Creusen09fa4b02018-01-11 16:08:54 +010046 EchoDetector::Metrics GetMetrics() const override;
ivoc4e477a12017-01-15 08:29:46 -080047
ivocaf27ed02016-10-28 07:04:03 -070048 private:
peah9e6a2902017-05-15 07:19:21 -070049 static int instance_count_;
50 std::unique_ptr<ApmDataDumper> data_dumper_;
ivocaf27ed02016-10-28 07:04:03 -070051 // Keep track if the |Process| function has been previously called.
52 bool first_process_call_ = true;
53 // Buffer for storing the power of incoming farend buffers. This is needed for
54 // cases where calls to BufferFarend and Process are jittery.
55 CircularBuffer render_buffer_;
56 // Count how long ago it was that the size of |render_buffer_| was zero. This
57 // value is also reset to zero when clock drift is detected and a value from
58 // the renderbuffer is discarded, even though the buffer is not actually zero
59 // at that point. This is done to avoid repeatedly removing elements in this
60 // situation.
61 size_t frames_since_zero_buffer_size_ = 0;
62
63 // Circular buffers containing delayed versions of the power, mean and
64 // standard deviation, for calculating the delayed covariance values.
65 std::vector<float> render_power_;
66 std::vector<float> render_power_mean_;
67 std::vector<float> render_power_std_dev_;
68 // Covariance estimates for different delay values.
69 std::vector<NormalizedCovarianceEstimator> covariances_;
70 // Index where next element should be inserted in all of the above circular
71 // buffers.
72 size_t next_insertion_index_ = 0;
73
74 MeanVarianceEstimator render_statistics_;
75 MeanVarianceEstimator capture_statistics_;
76 // Current echo likelihood.
77 float echo_likelihood_ = 0.f;
ivocfbb374d2016-11-17 06:19:47 -080078 // Reliability of the current likelihood.
79 float reliability_ = 0.f;
ivoc4e477a12017-01-15 08:29:46 -080080 MovingMax recent_likelihood_max_;
ivoc1592c742017-05-17 09:53:02 -070081
82 int log_counter_ = 0;
ivoc9f4a4a02016-10-28 05:39:16 -070083};
84
85} // namespace webrtc
86
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020087#endif // MODULES_AUDIO_PROCESSING_RESIDUAL_ECHO_DETECTOR_H_