blob: 98eeca63c9bbb17629724bf9fdc0fbe0031bdc3b [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"
Per Åhgrenef5d5af2018-07-31 00:03:46 +020021
22namespace webrtc {
23
24namespace {
25
Per Åhgrenef5d5af2018-07-31 00:03:46 +020026// Computes the ratio of the energies between the direct path and the tail. The
27// energy is computed in the power spectrum domain discarding the DC
28// contributions.
29float AverageDecayWithinFilter(
30 rtc::ArrayView<const float> freq_resp_direct_path,
31 rtc::ArrayView<const float> freq_resp_tail) {
32 // Skipping the DC for the ratio computation
33 constexpr size_t kSkipBins = 1;
34 RTC_CHECK_EQ(freq_resp_direct_path.size(), freq_resp_tail.size());
35
36 float direct_path_energy =
37 std::accumulate(freq_resp_direct_path.begin() + kSkipBins,
38 freq_resp_direct_path.end(), 0.f);
39
40 if (direct_path_energy == 0.f) {
41 return 0.f;
42 }
43
44 float tail_energy = std::accumulate(freq_resp_tail.begin() + kSkipBins,
45 freq_resp_tail.end(), 0.f);
46 return tail_energy / direct_path_energy;
47}
48
49} // namespace
50
Gustaf Ullberg68d6d442019-01-29 10:08:15 +010051ReverbFrequencyResponse::ReverbFrequencyResponse() {
Per Åhgrenef5d5af2018-07-31 00:03:46 +020052 tail_response_.fill(0.f);
53}
54ReverbFrequencyResponse::~ReverbFrequencyResponse() = default;
55
56void ReverbFrequencyResponse::Update(
57 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
58 frequency_response,
59 int filter_delay_blocks,
60 const absl::optional<float>& linear_filter_quality,
61 bool stationary_block) {
Per Åhgrenef5d5af2018-07-31 00:03:46 +020062
63 if (stationary_block || !linear_filter_quality) {
64 return;
65 }
66
67 Update(frequency_response, filter_delay_blocks, *linear_filter_quality);
68}
69
70void ReverbFrequencyResponse::Update(
71 const std::vector<std::array<float, kFftLengthBy2Plus1>>&
72 frequency_response,
73 int filter_delay_blocks,
74 float linear_filter_quality) {
75 rtc::ArrayView<const float> freq_resp_tail(
76 frequency_response[frequency_response.size() - 1]);
77
78 rtc::ArrayView<const float> freq_resp_direct_path(
79 frequency_response[filter_delay_blocks]);
80
81 float average_decay =
82 AverageDecayWithinFilter(freq_resp_direct_path, freq_resp_tail);
83
84 const float smoothing = 0.2f * linear_filter_quality;
85 average_decay_ += smoothing * (average_decay - average_decay_);
86
87 for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) {
88 tail_response_[k] = freq_resp_direct_path[k] * average_decay_;
89 }
90
Per Åhgrenef5d5af2018-07-31 00:03:46 +020091 for (size_t k = 1; k < kFftLengthBy2; ++k) {
92 const float avg_neighbour =
93 0.5f * (tail_response_[k - 1] + tail_response_[k + 1]);
94 tail_response_[k] = std::max(tail_response_[k], avg_neighbour);
95 }
96}
97
98} // namespace webrtc