peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 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/subtractor.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | |
| 15 | #include "webrtc/base/array_view.h" |
| 16 | #include "webrtc/base/checks.h" |
| 17 | #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | void ComputeError(const Aec3Fft& fft, |
| 24 | const FftData& S, |
| 25 | rtc::ArrayView<const float> y, |
| 26 | std::array<float, kBlockSize>* e, |
| 27 | FftData* E) { |
| 28 | std::array<float, kFftLength> s; |
| 29 | fft.Ifft(S, &s); |
| 30 | constexpr float kScale = 1.0f / kFftLengthBy2; |
| 31 | std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2, e->begin(), |
| 32 | [&](float a, float b) { return a - b * kScale; }); |
| 33 | std::for_each(e->begin(), e->end(), [](float& a) { |
| 34 | a = std::max(std::min(a, 32767.0f), -32768.0f); |
| 35 | }); |
| 36 | fft.ZeroPaddedFft(*e, E); |
| 37 | } |
| 38 | } // namespace |
| 39 | |
| 40 | std::vector<size_t> Subtractor::NumBlocksInRenderSums() const { |
| 41 | if (kMainFilterSizePartitions != kShadowFilterSizePartitions) { |
| 42 | return {kMainFilterSizePartitions, kShadowFilterSizePartitions}; |
| 43 | } else { |
| 44 | return {kMainFilterSizePartitions}; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Subtractor::Subtractor(ApmDataDumper* data_dumper, |
| 49 | Aec3Optimization optimization) |
| 50 | : data_dumper_(data_dumper), |
| 51 | optimization_(optimization), |
| 52 | main_filter_(kMainFilterSizePartitions, true, optimization, data_dumper_), |
| 53 | shadow_filter_(kShadowFilterSizePartitions, |
| 54 | false, |
| 55 | optimization, |
| 56 | data_dumper_) { |
| 57 | RTC_DCHECK(data_dumper_); |
| 58 | } |
| 59 | |
| 60 | Subtractor::~Subtractor() {} |
| 61 | |
| 62 | void Subtractor::HandleEchoPathChange( |
| 63 | const EchoPathVariability& echo_path_variability) { |
| 64 | if (echo_path_variability.delay_change) { |
| 65 | main_filter_.HandleEchoPathChange(); |
| 66 | shadow_filter_.HandleEchoPathChange(); |
| 67 | G_main_.HandleEchoPathChange(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void Subtractor::Process(const FftBuffer& render_buffer, |
| 72 | const rtc::ArrayView<const float> capture, |
| 73 | const RenderSignalAnalyzer& render_signal_analyzer, |
| 74 | bool saturation, |
| 75 | SubtractorOutput* output) { |
| 76 | RTC_DCHECK_EQ(kBlockSize, capture.size()); |
| 77 | rtc::ArrayView<const float> y = capture; |
| 78 | const FftBuffer& X_buffer = render_buffer; |
| 79 | FftData& E_main = output->E_main; |
| 80 | FftData& E_shadow = output->E_shadow; |
| 81 | std::array<float, kBlockSize>& e_main = output->e_main; |
| 82 | std::array<float, kBlockSize>& e_shadow = output->e_shadow; |
| 83 | |
| 84 | FftData S; |
| 85 | FftData& G = S; |
| 86 | |
| 87 | // Form and analyze the output of the main filter. |
| 88 | main_filter_.Filter(X_buffer, &S); |
| 89 | ComputeError(fft_, S, y, &e_main, &E_main); |
| 90 | |
| 91 | // Form and analyze the output of the shadow filter. |
| 92 | shadow_filter_.Filter(X_buffer, &S); |
| 93 | ComputeError(fft_, S, y, &e_shadow, &E_shadow); |
| 94 | |
| 95 | // Compute spectra for future use. |
| 96 | E_main.Spectrum(optimization_, &output->E2_main); |
| 97 | E_shadow.Spectrum(optimization_, &output->E2_shadow); |
| 98 | |
| 99 | // Update the main filter. |
| 100 | G_main_.Compute(X_buffer, render_signal_analyzer, *output, main_filter_, |
| 101 | saturation, &G); |
| 102 | main_filter_.Adapt(X_buffer, G); |
| 103 | data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re); |
| 104 | data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im); |
| 105 | |
| 106 | // Update the shadow filter. |
| 107 | G_shadow_.Compute(X_buffer, render_signal_analyzer, E_shadow, |
| 108 | shadow_filter_.SizePartitions(), saturation, &G); |
| 109 | shadow_filter_.Adapt(X_buffer, G); |
| 110 | data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re); |
| 111 | data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im); |
| 112 | |
| 113 | main_filter_.DumpFilter("aec3_subtractor_H_main"); |
| 114 | shadow_filter_.DumpFilter("aec3_subtractor_H_shadow"); |
| 115 | } |
| 116 | |
| 117 | } // namespace webrtc |