blob: 0d82515ec4b25688d7354be132a82208a965dc6b [file] [log] [blame]
Per Åhgrenef5d5af2018-07-31 00:03:46 +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#include "modules/audio_processing/aec3/reverb_frequency_response.h"
12
13#include <algorithm>
14#include <array>
15#include <cmath>
16#include <memory>
17#include <numeric>
18
19#include "api/array_view.h"
20#include "api/audio/echo_canceller3_config.h"
21#include "modules/audio_processing/aec3/aec3_common.h"
22#include "rtc_base/checks.h"
23#include "system_wrappers/include/field_trial.h"
24
25namespace webrtc {
26
27namespace {
28
29bool EnableSmoothUpdatesTailFreqResp() {
30 return !field_trial::IsEnabled(
31 "WebRTC-Aec3SmoothUpdatesTailFreqRespKillSwitch");
32}
33
34// Computes the ratio of the energies between the direct path and the tail. The
35// energy is computed in the power spectrum domain discarding the DC
36// contributions.
37float AverageDecayWithinFilter(
38 rtc::ArrayView<const float> freq_resp_direct_path,
39 rtc::ArrayView<const float> freq_resp_tail) {
40 // Skipping the DC for the ratio computation
41 constexpr size_t kSkipBins = 1;
42 RTC_CHECK_EQ(freq_resp_direct_path.size(), freq_resp_tail.size());
43
44 float direct_path_energy =
45 std::accumulate(freq_resp_direct_path.begin() + kSkipBins,
46 freq_resp_direct_path.end(), 0.f);
47
48 if (direct_path_energy == 0.f) {
49 return 0.f;
50 }
51
52 float tail_energy = std::accumulate(freq_resp_tail.begin() + kSkipBins,
53 freq_resp_tail.end(), 0.f);
54 return tail_energy / direct_path_energy;
55}
56
57} // namespace
58
59ReverbFrequencyResponse::ReverbFrequencyResponse()
60 : enable_smooth_tail_response_updates_(EnableSmoothUpdatesTailFreqResp()) {
61 tail_response_.fill(0.f);
62}
63ReverbFrequencyResponse::~ReverbFrequencyResponse() = default;
64
65void ReverbFrequencyResponse::Update(
66 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
67 frequency_response,
68 int filter_delay_blocks,
69 const absl::optional<float>& linear_filter_quality,
70 bool stationary_block) {
71 if (!enable_smooth_tail_response_updates_) {
Jesús de Vicente Peña5b7a4842018-08-28 09:16:56 +020072 Update(frequency_response, filter_delay_blocks, 0.5f);
Per Åhgrenef5d5af2018-07-31 00:03:46 +020073 return;
74 }
75
76 if (stationary_block || !linear_filter_quality) {
77 return;
78 }
79
80 Update(frequency_response, filter_delay_blocks, *linear_filter_quality);
81}
82
83void ReverbFrequencyResponse::Update(
84 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
85 frequency_response,
86 int filter_delay_blocks,
87 float linear_filter_quality) {
88 rtc::ArrayView<const float> freq_resp_tail(
89 frequency_response[frequency_response.size() - 1]);
90
91 rtc::ArrayView<const float> freq_resp_direct_path(
92 frequency_response[filter_delay_blocks]);
93
94 float average_decay =
95 AverageDecayWithinFilter(freq_resp_direct_path, freq_resp_tail);
96
97 const float smoothing = 0.2f * linear_filter_quality;
98 average_decay_ += smoothing * (average_decay - average_decay_);
99
100 for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) {
101 tail_response_[k] = freq_resp_direct_path[k] * average_decay_;
102 }
103
Per Åhgrenef5d5af2018-07-31 00:03:46 +0200104 for (size_t k = 1; k < kFftLengthBy2; ++k) {
105 const float avg_neighbour =
106 0.5f * (tail_response_[k - 1] + tail_response_[k + 1]);
107 tail_response_[k] = std::max(tail_response_[k], avg_neighbour);
108 }
109}
110
111} // namespace webrtc