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