blob: 55797592a9f1c4f9f83756eaccfdaf6e4eaa7d7e [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_
peah522d71b2017-02-23 05:16:26 -080013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
peah522d71b2017-02-23 05:16:26 -080016#include <array>
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020017#include <memory>
Per Åhgren785d4c42019-10-17 14:40:54 +020018#include <vector>
peah522d71b2017-02-23 05:16:26 -080019
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020020#include "absl/types/optional.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010021#include "api/array_view.h"
Jesús de Vicente Peña44974e12018-11-20 12:54:23 +010022#include "api/audio/echo_canceller3_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/aec3/aec3_common.h"
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020024#include "modules/audio_processing/aec3/fullband_erle_estimator.h"
Jesús de Vicente Peña44974e12018-11-20 12:54:23 +010025#include "modules/audio_processing/aec3/render_buffer.h"
26#include "modules/audio_processing/aec3/signal_dependent_erle_estimator.h"
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020027#include "modules/audio_processing/aec3/subband_erle_estimator.h"
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020028#include "modules/audio_processing/logging/apm_data_dumper.h"
peah522d71b2017-02-23 05:16:26 -080029
30namespace webrtc {
31
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020032// 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.
peah522d71b2017-02-23 05:16:26 -080034class ErleEstimator {
35 public:
Per Åhgren785d4c42019-10-17 14:40:54 +020036 ErleEstimator(size_t startup_phase_length_blocks,
Per Åhgrenb4161d32019-10-08 12:35:47 +020037 const EchoCanceller3Config& config,
38 size_t num_capture_channels);
peah522d71b2017-02-23 05:16:26 -080039 ~ErleEstimator();
40
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020041 // Resets the fullband ERLE estimator and the subbands ERLE estimators.
Per Åhgrenc5a38ad2018-10-04 15:37:54 +020042 void Reset(bool delay_change);
Jesús de Vicente Peña7015bb42018-08-29 11:15:30 +020043
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020044 // Updates the ERLE estimates.
Per Åhgren785d4c42019-10-17 14:40:54 +020045 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);
peah522d71b2017-02-23 05:16:26 -080056
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020057 // Returns the most recent subband ERLE estimates.
Gustaf Ullberg437d1292021-04-20 13:48:57 +020058 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Erle(
59 bool onset_compensated) const {
Per Åhgren785d4c42019-10-17 14:40:54 +020060 return signal_dependent_erle_estimator_
Gustaf Ullberg437d1292021-04-20 13:48:57 +020061 ? signal_dependent_erle_estimator_->Erle(onset_compensated)
62 : subband_erle_estimator_.Erle(onset_compensated);
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020063 }
Per Åhgrenb4161d32019-10-08 12:35:47 +020064
Gustaf Ullberga63d1522021-06-11 14:02:53 +020065 // 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 Åhgren785d4c42019-10-17 14:40:54 +020077 // Returns the subband ERLE that are estimated during onsets (only used for
78 // testing).
Gustaf Ullberg437d1292021-04-20 13:48:57 +020079 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> ErleDuringOnsets()
Per Åhgren785d4c42019-10-17 14:40:54 +020080 const {
Gustaf Ullberg437d1292021-04-20 13:48:57 +020081 return subband_erle_estimator_.ErleDuringOnsets();
Jesús de Vicente Peña7682c6e2018-03-22 14:53:23 +010082 }
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020083
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020084 // 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 Åhgren8be669f2019-10-11 23:02:26 +020091 // 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ña496cedf2018-07-04 11:02:09 +020097 }
98
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +020099 void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
peah522d71b2017-02-23 05:16:26 -0800100
101 private:
Per Åhgren785d4c42019-10-17 14:40:54 +0200102 const size_t startup_phase_length_blocks_;
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200103 FullBandErleEstimator fullband_erle_estimator_;
104 SubbandErleEstimator subband_erle_estimator_;
Per Åhgren785d4c42019-10-17 14:40:54 +0200105 std::unique_ptr<SignalDependentErleEstimator>
106 signal_dependent_erle_estimator_;
Per Åhgrenc5a38ad2018-10-04 15:37:54 +0200107 size_t blocks_since_reset_ = 0;
peah522d71b2017-02-23 05:16:26 -0800108};
109
110} // namespace webrtc
111
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200112#endif // MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_