Per Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 1 | /* |
| 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 Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <stddef.h> |
Per Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <array> |
Per Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 16 | #include <numeric> |
| 17 | |
| 18 | #include "api/array_view.h" |
Per Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/aec3/aec3_common.h" |
| 20 | #include "rtc_base/checks.h" |
Per Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | namespace { |
| 25 | |
Per Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 26 | // 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. |
| 29 | float 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 Ullberg | 68d6d44 | 2019-01-29 10:08:15 +0100 | [diff] [blame^] | 51 | ReverbFrequencyResponse::ReverbFrequencyResponse() { |
Per Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 52 | tail_response_.fill(0.f); |
| 53 | } |
| 54 | ReverbFrequencyResponse::~ReverbFrequencyResponse() = default; |
| 55 | |
| 56 | void 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 Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 62 | |
| 63 | if (stationary_block || !linear_filter_quality) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | Update(frequency_response, filter_delay_blocks, *linear_filter_quality); |
| 68 | } |
| 69 | |
| 70 | void 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 Åhgren | ef5d5af | 2018-07-31 00:03:46 +0200 | [diff] [blame] | 91 | 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 |