stationarity_estimator.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018 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_AEC3_STATIONARITY_ESTIMATOR_H_
  11. #define MODULES_AUDIO_PROCESSING_AEC3_STATIONARITY_ESTIMATOR_H_
  12. #include <stddef.h>
  13. #include <array>
  14. #include <memory>
  15. #include "api/array_view.h"
  16. #include "modules/audio_processing/aec3/aec3_common.h" // kFftLengthBy2Plus1...
  17. #include "modules/audio_processing/aec3/reverb_model.h"
  18. #include "rtc_base/checks.h"
  19. namespace webrtc {
  20. class ApmDataDumper;
  21. struct SpectrumBuffer;
  22. class StationarityEstimator {
  23. public:
  24. StationarityEstimator();
  25. ~StationarityEstimator();
  26. // Reset the stationarity estimator.
  27. void Reset();
  28. // Update just the noise estimator. Usefull until the delay is known
  29. void UpdateNoiseEstimator(
  30. rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> spectrum);
  31. // Update the flag indicating whether this current frame is stationary. For
  32. // getting a more robust estimation, it looks at future and/or past frames.
  33. void UpdateStationarityFlags(
  34. const SpectrumBuffer& spectrum_buffer,
  35. rtc::ArrayView<const float> render_reverb_contribution_spectrum,
  36. int idx_current,
  37. int num_lookahead);
  38. // Returns true if the current band is stationary.
  39. bool IsBandStationary(size_t band) const {
  40. return stationarity_flags_[band] && (hangovers_[band] == 0);
  41. }
  42. // Returns true if the current block is estimated as stationary.
  43. bool IsBlockStationary() const;
  44. private:
  45. static constexpr int kWindowLength = 13;
  46. // Returns the power of the stationary noise spectrum at a band.
  47. float GetStationarityPowerBand(size_t k) const { return noise_.Power(k); }
  48. // Get an estimation of the stationarity for the current band by looking
  49. // at the past/present/future available data.
  50. bool EstimateBandStationarity(const SpectrumBuffer& spectrum_buffer,
  51. rtc::ArrayView<const float> average_reverb,
  52. const std::array<int, kWindowLength>& indexes,
  53. size_t band) const;
  54. // True if all bands at the current point are stationary.
  55. bool AreAllBandsStationary();
  56. // Update the hangover depending on the stationary status of the current
  57. // frame.
  58. void UpdateHangover();
  59. // Smooth the stationarity detection by looking at neighbouring frequency
  60. // bands.
  61. void SmoothStationaryPerFreq();
  62. class NoiseSpectrum {
  63. public:
  64. NoiseSpectrum();
  65. ~NoiseSpectrum();
  66. // Reset the noise power spectrum estimate state.
  67. void Reset();
  68. // Update the noise power spectrum with a new frame.
  69. void Update(
  70. rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> spectrum);
  71. // Get the noise estimation power spectrum.
  72. rtc::ArrayView<const float> Spectrum() const { return noise_spectrum_; }
  73. // Get the noise power spectrum at a certain band.
  74. float Power(size_t band) const {
  75. RTC_DCHECK_LT(band, noise_spectrum_.size());
  76. return noise_spectrum_[band];
  77. }
  78. private:
  79. // Get the update coefficient to be used for the current frame.
  80. float GetAlpha() const;
  81. // Update the noise power spectrum at a certain band with a new frame.
  82. float UpdateBandBySmoothing(float power_band,
  83. float power_band_noise,
  84. float alpha) const;
  85. std::array<float, kFftLengthBy2Plus1> noise_spectrum_;
  86. size_t block_counter_;
  87. };
  88. static int instance_count_;
  89. std::unique_ptr<ApmDataDumper> data_dumper_;
  90. NoiseSpectrum noise_;
  91. std::array<int, kFftLengthBy2Plus1> hangovers_;
  92. std::array<bool, kFftLengthBy2Plus1> stationarity_flags_;
  93. };
  94. } // namespace webrtc
  95. #endif // MODULES_AUDIO_PROCESSING_AEC3_STATIONARITY_ESTIMATOR_H_