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