residual_echo_detector.h 3.5 KB

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