blob: 1ffc017b7d316459d5b07a1f5c3afe63aa73f62c [file] [log] [blame]
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +02001/*
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
11#ifndef MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_
13
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020015
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020016#include "absl/types/optional.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020017#include "api/array_view.h"
Sam Zackrissonf3f61592019-09-05 11:30:49 +020018#include "modules/audio_processing/aec3/block_buffer.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020019#include "modules/audio_processing/aec3/render_buffer.h"
Sam Zackrissonf3f61592019-09-05 11:30:49 +020020#include "modules/audio_processing/aec3/spectrum_buffer.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020021#include "modules/audio_processing/aec3/stationarity_estimator.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/constructor_magic.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020023
24namespace webrtc {
25
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020026class EchoAudibility {
27 public:
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020028 explicit EchoAudibility(bool use_render_stationarity_at_init);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020029 ~EchoAudibility();
30
Per Åhgren8718afb2019-10-15 10:31:35 +020031 EchoAudibility(const EchoAudibility&) = delete;
32 EchoAudibility& operator=(const EchoAudibility&) = delete;
33
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020034 // Feed new render data to the echo audibility estimator.
35 void Update(const RenderBuffer& render_buffer,
Per Åhgren8718afb2019-10-15 10:31:35 +020036 rtc::ArrayView<const float> average_reverb,
37 int min_channel_delay_blocks,
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +020038 bool external_delay_seen);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020039 // Get the residual echo scaling.
Per Åhgrenb2d71162018-09-10 14:10:34 +020040 void GetResidualEchoScaling(bool filter_has_had_time_to_converge,
41 rtc::ArrayView<float> residual_scaling) const {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020042 for (size_t band = 0; band < residual_scaling.size(); ++band) {
Per Åhgrenb2d71162018-09-10 14:10:34 +020043 if (render_stationarity_.IsBandStationary(band) &&
Jesús de Vicente Peñaba801f62019-04-16 11:32:11 +020044 (filter_has_had_time_to_converge ||
45 use_render_stationarity_at_init_)) {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020046 residual_scaling[band] = 0.f;
47 } else {
48 residual_scaling[band] = 1.0f;
49 }
50 }
51 }
52
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020053 // Returns true if the current render block is estimated as stationary.
54 bool IsBlockStationary() const {
55 return render_stationarity_.IsBlockStationary();
56 }
57
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020058 private:
59 // Reset the EchoAudibility class.
60 void Reset();
61
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020062 // Updates the render stationarity flags for the current frame.
Per Åhgren8718afb2019-10-15 10:31:35 +020063 void UpdateRenderStationarityFlags(const RenderBuffer& render_buffer,
64 rtc::ArrayView<const float> average_reverb,
65 int delay_blocks);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020066
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020067 // Updates the noise estimator with the new render data since the previous
68 // call to this method.
Sam Zackrissonf3f61592019-09-05 11:30:49 +020069 void UpdateRenderNoiseEstimator(const SpectrumBuffer& spectrum_buffer,
70 const BlockBuffer& block_buffer,
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020071 bool external_delay_seen);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020072
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020073 // Returns a bool being true if the render signal contains just close to zero
74 // values.
Sam Zackrissonf3f61592019-09-05 11:30:49 +020075 bool IsRenderTooLow(const BlockBuffer& block_buffer);
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020076
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020077 absl::optional<int> render_spectrum_write_prev_;
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020078 int render_block_write_prev_;
79 bool non_zero_render_seen_;
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020080 const bool use_render_stationarity_at_init_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020081 StationarityEstimator render_stationarity_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020082};
83
84} // namespace webrtc
85
86#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_