blob: b9d6f87d2ab5735f702265d85f0d66155a0c9656 [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"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020022
23namespace webrtc {
24
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020025class EchoAudibility {
26 public:
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020027 explicit EchoAudibility(bool use_render_stationarity_at_init);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020028 ~EchoAudibility();
29
Per Åhgren8718afb2019-10-15 10:31:35 +020030 EchoAudibility(const EchoAudibility&) = delete;
31 EchoAudibility& operator=(const EchoAudibility&) = delete;
32
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020033 // Feed new render data to the echo audibility estimator.
34 void Update(const RenderBuffer& render_buffer,
Per Åhgren8718afb2019-10-15 10:31:35 +020035 rtc::ArrayView<const float> average_reverb,
36 int min_channel_delay_blocks,
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +020037 bool external_delay_seen);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020038 // Get the residual echo scaling.
Per Åhgrenb2d71162018-09-10 14:10:34 +020039 void GetResidualEchoScaling(bool filter_has_had_time_to_converge,
40 rtc::ArrayView<float> residual_scaling) const {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020041 for (size_t band = 0; band < residual_scaling.size(); ++band) {
Per Åhgrenb2d71162018-09-10 14:10:34 +020042 if (render_stationarity_.IsBandStationary(band) &&
Jesús de Vicente Peñaba801f62019-04-16 11:32:11 +020043 (filter_has_had_time_to_converge ||
44 use_render_stationarity_at_init_)) {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020045 residual_scaling[band] = 0.f;
46 } else {
47 residual_scaling[band] = 1.0f;
48 }
49 }
50 }
51
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020052 // Returns true if the current render block is estimated as stationary.
53 bool IsBlockStationary() const {
54 return render_stationarity_.IsBlockStationary();
55 }
56
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020057 private:
58 // Reset the EchoAudibility class.
59 void Reset();
60
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020061 // Updates the render stationarity flags for the current frame.
Per Åhgren8718afb2019-10-15 10:31:35 +020062 void UpdateRenderStationarityFlags(const RenderBuffer& render_buffer,
63 rtc::ArrayView<const float> average_reverb,
64 int delay_blocks);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020065
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020066 // Updates the noise estimator with the new render data since the previous
67 // call to this method.
Sam Zackrissonf3f61592019-09-05 11:30:49 +020068 void UpdateRenderNoiseEstimator(const SpectrumBuffer& spectrum_buffer,
69 const BlockBuffer& block_buffer,
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020070 bool external_delay_seen);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020071
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020072 // Returns a bool being true if the render signal contains just close to zero
73 // values.
Sam Zackrissonf3f61592019-09-05 11:30:49 +020074 bool IsRenderTooLow(const BlockBuffer& block_buffer);
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020075
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020076 absl::optional<int> render_spectrum_write_prev_;
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020077 int render_block_write_prev_;
78 bool non_zero_render_seen_;
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020079 const bool use_render_stationarity_at_init_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020080 StationarityEstimator render_stationarity_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020081};
82
83} // namespace webrtc
84
85#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_