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