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