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