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 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 (kMainFilterLength != kShadowFilterLength) { | |
42 return {kMainFilterLength, kShadowFilterLength}; | |
43 } else { | |
44 return {kMainFilterLength}; | |
45 } | |
46 } | |
47 | |
48 Subtractor::Subtractor(ApmDataDumper* data_dumper, | |
49 Aec3Optimization optimization) | |
50 : data_dumper_(data_dumper), | |
51 optimization_(optimization), | |
52 main_filter_(kMainFilterLength, true, optimization, data_dumper_), | |
53 shadow_filter_(kShadowFilterLength, false, optimization, data_dumper_) { | |
54 RTC_DCHECK(data_dumper_); | |
55 } | |
56 | |
57 Subtractor::~Subtractor() {} | |
58 | |
59 void Subtractor::HandleEchoPathChange( | |
60 const EchoPathVariability& echo_path_variability) { | |
61 if (echo_path_variability.AudioPathChanged()) { | |
62 if (echo_path_variability.delay_change) { | |
aleloi
2017/02/22 16:20:31
Just 'if (echo_path_variability.delay_change) ' in
hlundin-webrtc
2017/02/22 21:17:28
bool AudioPathChanged() const { return gain_change
peah-webrtc
2017/02/22 23:51:39
You are both correct. I will adopt the proposal by
peah-webrtc
2017/02/22 23:51:39
You are both correct. I will adopt the proposal by
| |
63 main_filter_.HandleEchoPathChange(); | |
64 shadow_filter_.HandleEchoPathChange(); | |
65 G_main_.HandleEchoPathChange(); | |
66 } | |
67 } | |
68 } | |
69 | |
70 void Subtractor::Process(const FftBuffer& render_buffer, | |
71 const rtc::ArrayView<const float> capture, | |
72 const RenderSignalAnalyzer& render_signal_analyzer, | |
73 bool saturation, | |
74 SubtractorOutput* output) { | |
75 RTC_DCHECK_EQ(kBlockSize, capture.size()); | |
76 rtc::ArrayView<const float> y = capture; | |
77 const FftBuffer& X_buffer = render_buffer; | |
78 FftData& E_main = output->E_main; | |
79 FftData& E_shadow = output->E_shadow; | |
80 std::array<float, kBlockSize>& e_main = output->e_main; | |
81 std::array<float, kBlockSize>& e_shadow = output->e_shadow; | |
82 | |
83 FftData S; | |
84 FftData& G = S; | |
85 | |
86 // Form and analyze the output of the main filter. | |
87 main_filter_.Filter(X_buffer, &S); | |
88 ComputeError(fft_, S, y, &e_main, &E_main); | |
89 | |
90 // Form and analyze the output of the shadow filter. | |
91 shadow_filter_.Filter(X_buffer, &S); | |
92 ComputeError(fft_, S, y, &e_shadow, &E_shadow); | |
93 | |
94 // Compute spectra for future use. | |
95 E_main.Spectrum(optimization_, &output->E2_main); | |
96 E_shadow.Spectrum(optimization_, &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 main_filter_.DumpFilter("aec3_subtractor_H_main"); | |
113 shadow_filter_.DumpFilter("aec3_subtractor_H_shadow"); | |
114 } | |
115 | |
116 } // namespace webrtc | |
OLD | NEW |