peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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_AEC3_ERLE_ESTIMATOR_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_ |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 16 | #include <array> |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 17 | #include <memory> |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 18 | #include <vector> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 19 | |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 21 | #include "api/array_view.h" |
Jesús de Vicente Peña | 44974e1 | 2018-11-20 12:54:23 +0100 | [diff] [blame] | 22 | #include "api/audio/echo_canceller3_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/aec3_common.h" |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 24 | #include "modules/audio_processing/aec3/fullband_erle_estimator.h" |
Jesús de Vicente Peña | 44974e1 | 2018-11-20 12:54:23 +0100 | [diff] [blame] | 25 | #include "modules/audio_processing/aec3/render_buffer.h" |
| 26 | #include "modules/audio_processing/aec3/signal_dependent_erle_estimator.h" |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 27 | #include "modules/audio_processing/aec3/subband_erle_estimator.h" |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame] | 28 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 32 | // Estimates the echo return loss enhancement. One estimate is done per subband |
| 33 | // and another one is done using the aggreation of energy over all the subbands. |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 34 | class ErleEstimator { |
| 35 | public: |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 36 | ErleEstimator(size_t startup_phase_length_blocks, |
Per Åhgren | b4161d3 | 2019-10-08 12:35:47 +0200 | [diff] [blame] | 37 | const EchoCanceller3Config& config, |
| 38 | size_t num_capture_channels); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 39 | ~ErleEstimator(); |
| 40 | |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 41 | // Resets the fullband ERLE estimator and the subbands ERLE estimators. |
Per Åhgren | c5a38ad | 2018-10-04 15:37:54 +0200 | [diff] [blame] | 42 | void Reset(bool delay_change); |
Jesús de Vicente Peña | 7015bb4 | 2018-08-29 11:15:30 +0200 | [diff] [blame] | 43 | |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 44 | // Updates the ERLE estimates. |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 45 | void Update( |
| 46 | const RenderBuffer& render_buffer, |
| 47 | rtc::ArrayView<const std::vector<std::array<float, kFftLengthBy2Plus1>>> |
| 48 | filter_frequency_responses, |
| 49 | rtc::ArrayView<const float, kFftLengthBy2Plus1> |
| 50 | avg_render_spectrum_with_reverb, |
| 51 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 52 | capture_spectra, |
| 53 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 54 | subtractor_spectra, |
| 55 | const std::vector<bool>& converged_filters); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 56 | |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 57 | // Returns the most recent subband ERLE estimates. |
Gustaf Ullberg | 437d129 | 2021-04-20 13:48:57 +0200 | [diff] [blame] | 58 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle( |
| 59 | bool onset_compensated) const { |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 60 | return signal_dependent_erle_estimator_ |
Gustaf Ullberg | 437d129 | 2021-04-20 13:48:57 +0200 | [diff] [blame] | 61 | ? signal_dependent_erle_estimator_->Erle(onset_compensated) |
| 62 | : subband_erle_estimator_.Erle(onset_compensated); |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 63 | } |
Per Åhgren | b4161d3 | 2019-10-08 12:35:47 +0200 | [diff] [blame] | 64 | |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 65 | // Returns the non-capped subband ERLE. |
| 66 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleUnbounded() |
| 67 | const { |
| 68 | // Unbounded ERLE is only used with the subband erle estimator where the |
| 69 | // ERLE is often capped at low values. When the signal dependent ERLE |
| 70 | // estimator is used the capped ERLE is returned. |
| 71 | return !signal_dependent_erle_estimator_ |
| 72 | ? subband_erle_estimator_.ErleUnbounded() |
| 73 | : signal_dependent_erle_estimator_->Erle( |
| 74 | /*onset_compensated=*/false); |
| 75 | } |
| 76 | |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 77 | // Returns the subband ERLE that are estimated during onsets (only used for |
| 78 | // testing). |
Gustaf Ullberg | 437d129 | 2021-04-20 13:48:57 +0200 | [diff] [blame] | 79 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleDuringOnsets() |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 80 | const { |
Gustaf Ullberg | 437d129 | 2021-04-20 13:48:57 +0200 | [diff] [blame] | 81 | return subband_erle_estimator_.ErleDuringOnsets(); |
Jesús de Vicente Peña | 7682c6e | 2018-03-22 14:53:23 +0100 | [diff] [blame] | 82 | } |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame] | 83 | |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 84 | // Returns the fullband ERLE estimate. |
| 85 | float FullbandErleLog2() const { |
| 86 | return fullband_erle_estimator_.FullbandErleLog2(); |
| 87 | } |
| 88 | |
| 89 | // Returns an estimation of the current linear filter quality based on the |
| 90 | // current and past fullband ERLE estimates. The returned value is a float |
Per Åhgren | 8be669f | 2019-10-11 23:02:26 +0200 | [diff] [blame] | 91 | // vector with content between 0 and 1 where 1 indicates that, at this current |
| 92 | // time instant, the linear filter is reaching its maximum subtraction |
| 93 | // performance. |
| 94 | rtc::ArrayView<const absl::optional<float>> GetInstLinearQualityEstimates() |
| 95 | const { |
| 96 | return fullband_erle_estimator_.GetInstLinearQualityEstimates(); |
Jesús de Vicente Peña | 496cedf | 2018-07-04 11:02:09 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 99 | void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 100 | |
| 101 | private: |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 102 | const size_t startup_phase_length_blocks_; |
Jesús de Vicente Peña | e9a7e90 | 2018-09-27 11:49:39 +0200 | [diff] [blame] | 103 | FullBandErleEstimator fullband_erle_estimator_; |
| 104 | SubbandErleEstimator subband_erle_estimator_; |
Per Åhgren | 785d4c4 | 2019-10-17 14:40:54 +0200 | [diff] [blame] | 105 | std::unique_ptr<SignalDependentErleEstimator> |
| 106 | signal_dependent_erle_estimator_; |
Per Åhgren | c5a38ad | 2018-10-04 15:37:54 +0200 | [diff] [blame] | 107 | size_t blocks_since_reset_ = 0; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace webrtc |
| 111 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 112 | #endif // MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_ |