OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 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 "webrtc/modules/audio_processing/aec3/main_filter_update_gain.h" |
| 12 |
| 13 #include <algorithm> |
| 14 #include <functional> |
| 15 |
| 16 #include "webrtc/base/atomicops.h" |
| 17 #include "webrtc/base/checks.h" |
| 18 #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 19 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| 20 |
| 21 namespace webrtc { |
| 22 namespace { |
| 23 |
| 24 constexpr float kHErrorInitial = 10000.f; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 int MainFilterUpdateGain::instance_count_ = 0; |
| 29 |
| 30 MainFilterUpdateGain::MainFilterUpdateGain() |
| 31 : data_dumper_( |
| 32 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 33 poor_excitation_counter_(1000) { |
| 34 H_error_.fill(kHErrorInitial); |
| 35 } |
| 36 |
| 37 MainFilterUpdateGain::~MainFilterUpdateGain() {} |
| 38 |
| 39 void MainFilterUpdateGain::HandleEchoPathChange() { |
| 40 H_error_.fill(kHErrorInitial); |
| 41 } |
| 42 |
| 43 void MainFilterUpdateGain::Compute( |
| 44 const FftBuffer& render_buffer, |
| 45 const RenderSignalAnalyzer& render_signal_analyzer, |
| 46 const SubtractorOutput& subtractor_output, |
| 47 const AdaptiveFirFilter& filter, |
| 48 bool saturated_capture_signal, |
| 49 FftData* gain_fft) { |
| 50 RTC_DCHECK(gain_fft); |
| 51 // Introducing shorter notation to improve readability. |
| 52 const FftBuffer& X_buffer = render_buffer; |
| 53 const FftData& E_main = subtractor_output.E_main; |
| 54 const auto& E2_main = subtractor_output.E2_main; |
| 55 const auto& E2_shadow = subtractor_output.E2_shadow; |
| 56 FftData* G = gain_fft; |
| 57 const size_t size_partitions = filter.SizePartitions(); |
| 58 const auto& X2 = X_buffer.SpectralSum(size_partitions); |
| 59 const auto& erl = filter.Erl(); |
| 60 |
| 61 ++call_counter_; |
| 62 |
| 63 if (render_signal_analyzer.PoorSignalExcitation()) { |
| 64 poor_excitation_counter_ = 0; |
| 65 } |
| 66 |
| 67 // Do not update the filter if the render is not sufficiently excited. |
| 68 if (++poor_excitation_counter_ < size_partitions || |
| 69 saturated_capture_signal || call_counter_ <= size_partitions) { |
| 70 G->re.fill(0.f); |
| 71 G->im.fill(0.f); |
| 72 } else { |
| 73 // Corresponds of WGN of power -46 dBFS. |
| 74 constexpr float kX2Min = 44015068.0f; |
| 75 std::array<float, kFftLengthBy2Plus1> mu; |
| 76 // mu = H_error / (0.5* H_error* X2 + n * E2). |
| 77 for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) { |
| 78 mu[k] = |
| 79 X2[k] > kX2Min |
| 80 ? H_error_[k] / |
| 81 (0.5f * H_error_[k] * X2[k] + size_partitions * E2_main[k]) |
| 82 : 0.f; |
| 83 } |
| 84 |
| 85 // Avoid updating the filter close to narrow bands in the render signals. |
| 86 render_signal_analyzer.MaskRegionsAroundNarrowBands(&mu); |
| 87 |
| 88 // H_error = H_error - 0.5 * mu * X2 * H_error. |
| 89 for (size_t k = 0; k < H_error_.size(); ++k) { |
| 90 H_error_[k] -= 0.5f * mu[k] * X2[k] * H_error_[k]; |
| 91 } |
| 92 |
| 93 // G = mu * E. |
| 94 std::transform(mu.begin(), mu.end(), E_main.re.begin(), G->re.begin(), |
| 95 std::multiplies<float>()); |
| 96 std::transform(mu.begin(), mu.end(), E_main.im.begin(), G->im.begin(), |
| 97 std::multiplies<float>()); |
| 98 } |
| 99 |
| 100 // H_error = H_error + factor * erl. |
| 101 std::array<float, kFftLengthBy2Plus1> H_error_increase; |
| 102 constexpr float kErlScaleAccurate = 1.f / 30.0f; |
| 103 constexpr float kErlScaleInaccurate = 1.f / 10.0f; |
| 104 std::transform(E2_shadow.begin(), E2_shadow.end(), E2_main.begin(), |
| 105 H_error_increase.begin(), [&](float a, float b) { |
| 106 return a >= b ? kErlScaleAccurate : kErlScaleInaccurate; |
| 107 }); |
| 108 std::transform(erl.begin(), erl.end(), H_error_increase.begin(), |
| 109 H_error_increase.begin(), std::multiplies<float>()); |
| 110 std::transform(H_error_.begin(), H_error_.end(), H_error_increase.begin(), |
| 111 H_error_.begin(), |
| 112 [&](float a, float b) { return std::max(a + b, 0.1f); }); |
| 113 |
| 114 data_dumper_->DumpRaw("aec3_main_gain_H_error", H_error_); |
| 115 } |
| 116 |
| 117 } // namespace webrtc |
OLD | NEW |