OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/aec3/render_signal_analyzer.h" | 11 #include "webrtc/modules/audio_processing/aec3/render_signal_analyzer.h" |
12 | 12 |
| 13 #include <math.h> |
13 #include <algorithm> | 14 #include <algorithm> |
14 | 15 |
15 #include "webrtc/rtc_base/checks.h" | 16 #include "webrtc/rtc_base/checks.h" |
16 | 17 |
17 namespace webrtc { | 18 namespace webrtc { |
18 | 19 |
19 namespace { | 20 namespace { |
20 constexpr size_t kCounterThreshold = 5; | 21 constexpr size_t kCounterThreshold = 5; |
21 | 22 |
| 23 // Identifies local bands with narrow characteristics. |
| 24 void IdentifySmallNarrowBandRegions( |
| 25 const RenderBuffer& render_buffer, |
| 26 const rtc::Optional<size_t>& delay_partitions, |
| 27 std::array<size_t, kFftLengthBy2 - 1>* narrow_band_counters) { |
| 28 if (!delay_partitions) { |
| 29 narrow_band_counters->fill(0); |
| 30 return; |
| 31 } |
| 32 |
| 33 const std::array<float, kFftLengthBy2Plus1>& X2 = |
| 34 render_buffer.Spectrum(*delay_partitions); |
| 35 |
| 36 for (size_t k = 1; k < (X2.size() - 1); ++k) { |
| 37 (*narrow_band_counters)[k - 1] = X2[k] > 3 * std::max(X2[k - 1], X2[k + 1]) |
| 38 ? (*narrow_band_counters)[k - 1] + 1 |
| 39 : 0; |
| 40 } |
| 41 } |
| 42 |
| 43 // Identifies whether the signal has a single strong narrow-band component. |
| 44 void IdentifyStrongNarrowBandComponent(const RenderBuffer& render_buffer, |
| 45 rtc::Optional<int>* narrow_peak_band, |
| 46 size_t* narrow_peak_counter) { |
| 47 const auto X2_latest = render_buffer.Spectrum(0); |
| 48 |
| 49 // Identify the spectral peak. |
| 50 const int peak_bin = static_cast<int>( |
| 51 std::max_element(X2_latest.begin(), X2_latest.end()) - X2_latest.begin()); |
| 52 |
| 53 // Compute the level around the peak. |
| 54 float non_peak_power = 0.f; |
| 55 for (int k = std::max(5, peak_bin - 14); k < peak_bin - 4; ++k) { |
| 56 non_peak_power = std::max(X2_latest[k], non_peak_power); |
| 57 } |
| 58 for (int k = peak_bin + 5; |
| 59 k < std::min(peak_bin + 15, static_cast<int>(kFftLengthBy2Plus1)); ++k) { |
| 60 non_peak_power = std::max(X2_latest[k], non_peak_power); |
| 61 } |
| 62 |
| 63 // Assess the render signal strength |
| 64 const std::vector<std::vector<float>>& x_latest = |
| 65 render_buffer.MostRecentBlock(); |
| 66 auto result0 = std::minmax_element(x_latest[0].begin(), x_latest[0].end()); |
| 67 float max_abs = std::max(fabs(*result0.first), fabs(*result0.second)); |
| 68 |
| 69 if (x_latest.size() > 1) { |
| 70 const auto result1 = |
| 71 std::minmax_element(x_latest[1].begin(), x_latest[1].end()); |
| 72 max_abs = |
| 73 std::max(max_abs, static_cast<float>(std::max(fabs(*result1.first), |
| 74 fabs(*result1.second)))); |
| 75 } |
| 76 |
| 77 // Detect whether the spectal peak has as strong narrowband nature. |
| 78 if (peak_bin > 6 && max_abs > 100 && |
| 79 X2_latest[peak_bin] > 100 * non_peak_power) { |
| 80 *narrow_peak_band = rtc::Optional<int>(peak_bin); |
| 81 *narrow_peak_counter = 0; |
| 82 } else { |
| 83 if (*narrow_peak_band && ++(*narrow_peak_counter) > 7) { |
| 84 *narrow_peak_band = rtc::Optional<int>(); |
| 85 } |
| 86 } |
| 87 } |
| 88 |
22 } // namespace | 89 } // namespace |
23 | 90 |
24 RenderSignalAnalyzer::RenderSignalAnalyzer() { | 91 RenderSignalAnalyzer::RenderSignalAnalyzer() { |
25 narrow_band_counters_.fill(0); | 92 narrow_band_counters_.fill(0); |
26 } | 93 } |
27 RenderSignalAnalyzer::~RenderSignalAnalyzer() = default; | 94 RenderSignalAnalyzer::~RenderSignalAnalyzer() = default; |
28 | 95 |
29 void RenderSignalAnalyzer::Update( | 96 void RenderSignalAnalyzer::Update( |
30 const RenderBuffer& render_buffer, | 97 const RenderBuffer& render_buffer, |
31 const rtc::Optional<size_t>& delay_partitions) { | 98 const rtc::Optional<size_t>& delay_partitions) { |
32 if (!delay_partitions) { | 99 // Identify bands of narrow nature. |
33 narrow_band_counters_.fill(0); | 100 IdentifySmallNarrowBandRegions(render_buffer, delay_partitions, |
34 return; | 101 &narrow_band_counters_); |
35 } | |
36 | 102 |
37 const std::array<float, kFftLengthBy2Plus1>& X2 = | 103 // Identify the presence of a strong narrow band. |
38 render_buffer.Spectrum(*delay_partitions); | 104 IdentifyStrongNarrowBandComponent(render_buffer, &narrow_peak_band_, |
39 | 105 &narrow_peak_counter_); |
40 // Detect narrow band signal regions. | |
41 for (size_t k = 1; k < (X2.size() - 1); ++k) { | |
42 narrow_band_counters_[k - 1] = X2[k] > 3 * std::max(X2[k - 1], X2[k + 1]) | |
43 ? narrow_band_counters_[k - 1] + 1 | |
44 : 0; | |
45 } | |
46 } | 106 } |
47 | 107 |
48 void RenderSignalAnalyzer::MaskRegionsAroundNarrowBands( | 108 void RenderSignalAnalyzer::MaskRegionsAroundNarrowBands( |
49 std::array<float, kFftLengthBy2Plus1>* v) const { | 109 std::array<float, kFftLengthBy2Plus1>* v) const { |
50 RTC_DCHECK(v); | 110 RTC_DCHECK(v); |
51 | 111 |
52 // Set v to zero around narrow band signal regions. | 112 // Set v to zero around narrow band signal regions. |
53 if (narrow_band_counters_[0] > kCounterThreshold) { | 113 if (narrow_band_counters_[0] > kCounterThreshold) { |
54 (*v)[1] = (*v)[0] = 0.f; | 114 (*v)[1] = (*v)[0] = 0.f; |
55 } | 115 } |
56 for (size_t k = 2; k < kFftLengthBy2 - 1; ++k) { | 116 for (size_t k = 2; k < kFftLengthBy2 - 1; ++k) { |
57 if (narrow_band_counters_[k - 1] > kCounterThreshold) { | 117 if (narrow_band_counters_[k - 1] > kCounterThreshold) { |
58 (*v)[k - 2] = (*v)[k - 1] = (*v)[k] = (*v)[k + 1] = (*v)[k + 2] = 0.f; | 118 (*v)[k - 2] = (*v)[k - 1] = (*v)[k] = (*v)[k + 1] = (*v)[k + 2] = 0.f; |
59 } | 119 } |
60 } | 120 } |
61 if (narrow_band_counters_[kFftLengthBy2 - 2] > kCounterThreshold) { | 121 if (narrow_band_counters_[kFftLengthBy2 - 2] > kCounterThreshold) { |
62 (*v)[kFftLengthBy2] = (*v)[kFftLengthBy2 - 1] = 0.f; | 122 (*v)[kFftLengthBy2] = (*v)[kFftLengthBy2 - 1] = 0.f; |
63 } | 123 } |
64 } | 124 } |
65 | 125 |
66 } // namespace webrtc | 126 } // namespace webrtc |
OLD | NEW |