Chromium Code Reviews| Index: webrtc/modules/audio_processing/aec3/subtractor.cc |
| diff --git a/webrtc/modules/audio_processing/aec3/subtractor.cc b/webrtc/modules/audio_processing/aec3/subtractor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..72339b57767d6f5c4898f4a993c9636a47b4402b |
| --- /dev/null |
| +++ b/webrtc/modules/audio_processing/aec3/subtractor.cc |
| @@ -0,0 +1,113 @@ |
| +/* |
| + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/audio_processing/aec3/subtractor.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "webrtc/base/array_view.h" |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| + |
| +namespace webrtc { |
| + |
| +std::vector<size_t> Subtractor::NumBlocksInRenderSums() const { |
| + if (kMainFilterLength != kShadowFilterLength) { |
| + std::vector<size_t> v(2); |
| + v[0] = kMainFilterLength; |
| + v[1] = kShadowFilterLength; |
| + return v; |
|
aleloi
2017/02/13 16:44:51
return {kMainFilterLength, kShadowFilterLength};
peah-webrtc
2017/02/20 07:37:19
Done.
|
| + } else { |
| + std::vector<size_t> v(1); |
| + v[0] = kMainFilterLength; |
| + return v; |
| + } |
| +} |
| + |
| +Subtractor::Subtractor(ApmDataDumper* data_dumper) |
| + : data_dumper_(data_dumper), |
| + main_filter_(kMainFilterLength, true, data_dumper_), |
| + shadow_filter_(kShadowFilterLength, false, data_dumper_) { |
| + RTC_DCHECK(data_dumper_); |
| +} |
| + |
| +Subtractor::~Subtractor() {} |
| + |
| +void Subtractor::HandleEchoPathChange( |
| + const EchoPathVariability& echo_path_variability) { |
| + if (echo_path_variability.AudioPathChanged()) { |
| + if (echo_path_variability.delay_change) { |
| + main_filter_.HandleEchoPathChange(); |
| + shadow_filter_.HandleEchoPathChange(); |
| + G_main_.HandleEchoPathChange(); |
| + } |
| + } |
| +} |
| + |
| +void Subtractor::Process(const FftBuffer& render_buffer, |
| + const rtc::ArrayView<const float> capture, |
| + const RenderSignalAnalyzer& render_signal_analyzer, |
| + bool saturation, |
| + SubtractorOutput* output) { |
| + RTC_DCHECK_EQ(kBlockSize, capture.size()); |
| + const rtc::ArrayView<const float> y = capture; |
| + const FftBuffer& X_buffer = render_buffer; |
| + FftData& E_main = output->E_main; |
| + FftData& E_shadow = output->E_shadow; |
| + std::array<float, kBlockSize>& e_main = output->e_main; |
| + std::array<float, kBlockSize>& e_shadow = output->e_shadow; |
| + |
| + FftData S; |
| + FftData& G = S; |
| + |
| + std::array<float, kFftLength> s; |
| + constexpr float kScale = 1.0f / kFftLengthBy2; |
| + |
| + // Form and analyze the output of the main filter. |
| + main_filter_.Filter(X_buffer, &S); |
| + fft_.Ifft(S, &s); |
| + std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2, e_main.begin(), |
| + [&](float a, float b) { return a - b * kScale; }); |
|
aleloi
2017/02/13 16:44:51
Beginner question: why the scaling? Is it because
peah-webrtc
2017/02/20 07:37:19
It is not included in the ifft method. My guess is
|
| + std::for_each(e_main.begin(), e_main.end(), [](float& a) { |
| + a = std::max(std::min(a, 32767.0f), -32768.0f); |
| + }); |
| + fft_.ZeroPaddedFft(e_main, &E_main); |
| + |
| + // Form and analyze the output of the shadow filter. |
| + shadow_filter_.Filter(X_buffer, &S); |
| + fft_.Ifft(S, &s); |
| + std::transform(y.begin(), y.end(), s.begin() + kFftLengthBy2, |
| + e_shadow.begin(), |
| + [&](float a, float b) { return a - b * kScale; }); |
| + std::for_each(e_shadow.begin(), e_shadow.end(), [](float& a) { |
| + a = std::max(std::min(a, 32767.0f), -32768.0f); |
| + }); |
| + fft_.ZeroPaddedFft(e_shadow, &E_shadow); |
| + |
|
aleloi
2017/02/13 16:44:51
I suggest putting the filtering & subtraction in a
peah-webrtc
2017/02/20 07:37:19
I did a variant of that. I think it is nice to see
|
| + // Compute spectra for future use. |
| + E_main.Spectrum(&output->E2_main); |
| + E_shadow.Spectrum(&output->E2_shadow); |
| + |
| + // Update the main filter. |
| + G_main_.Compute(X_buffer, render_signal_analyzer, *output, main_filter_, |
| + saturation, &G); |
| + main_filter_.Adapt(X_buffer, G); |
| + data_dumper_->DumpRaw("aec3_subtractor_G_main", G.re); |
| + data_dumper_->DumpRaw("aec3_subtractor_G_main", G.im); |
| + |
| + // Update the shadow filter. |
| + G_shadow_.Compute(X_buffer, render_signal_analyzer, E_shadow, |
| + shadow_filter_.SizePartitions(), saturation, &G); |
| + shadow_filter_.Adapt(X_buffer, G); |
| + data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.re); |
| + data_dumper_->DumpRaw("aec3_subtractor_G_shadow", G.im); |
| +} |
| + |
| +} // namespace webrtc |