OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 #include "webrtc/modules/audio_processing/aec3/echo_canceller3.h" | 10 #include "webrtc/modules/audio_processing/aec3/echo_canceller3.h" |
11 | 11 |
12 #include "webrtc/base/atomicops.h" | 12 #include "webrtc/base/atomicops.h" |
13 #include "webrtc/system_wrappers/include/logging.h" | 13 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
14 | 14 |
15 namespace webrtc { | 15 namespace webrtc { |
16 | 16 |
17 int EchoCanceller3::instance_count_ = 0; | 17 namespace { |
18 | 18 |
19 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_anti_hum_filter) { | 19 bool DetectSaturation(rtc::ArrayView<const float> y) { |
20 int band_sample_rate_hz = (sample_rate_hz == 8000 ? sample_rate_hz : 16000); | 20 for (auto y_k : y) { |
21 frame_length_ = rtc::CheckedDivExact(band_sample_rate_hz, 100); | 21 if (y_k > 32767.0f || y_k < -32768.0f) { |
ivoc
2016/12/19 11:30:22
How about if the signals are equal to the limits?
peah-webrtc
2016/12/20 10:10:25
Good find!
Done.
| |
22 | 22 return true; |
23 LOG(LS_INFO) << "AEC3 created : " | 23 } |
24 << "{ instance_count: " << instance_count_ << "}"; | 24 } |
25 instance_count_ = rtc::AtomicOps::Increment(&instance_count_); | 25 return false; |
26 } | 26 } |
27 | 27 |
28 EchoCanceller3::~EchoCanceller3() = default; | 28 void ProcessCaptureFrameContent(AudioBuffer* capture, |
29 | 29 bool known_echo_path_change, |
30 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) { | 30 bool saturated_microphone_signal, |
31 size_t subframe_index, | |
32 FrameBlocker* capture_blocker, | |
33 BlockFramer* output_framer, | |
34 BlockProcessor* block_processor) { | |
35 float capture_block[kMaxNumBands * kBlockSize]; | |
36 capture_blocker->InsertFrameAndExtractBlock( | |
37 subframe_index, capture->split_bands_f(0), capture_block); | |
38 block_processor->ProcessCapture(known_echo_path_change, | |
39 saturated_microphone_signal, capture_block); | |
40 output_framer->InsertBlock(capture_block); | |
41 output_framer->ExtractFrame(subframe_index, capture->split_bands_f(0)); | |
42 } | |
43 | |
44 void ProcessRemainingCaptureFrameContent(bool known_echo_path_change, | |
45 bool saturated_microphone_signal, | |
46 FrameBlocker* capture_blocker, | |
47 BlockFramer* output_framer, | |
48 BlockProcessor* block_processor) { | |
49 if (capture_blocker->IsBlockAvailable()) { | |
hlundin-webrtc
2016/12/16 10:04:47
Nit: I prefer early return.
if (!capture_blocker->
peah-webrtc
2016/12/20 10:10:25
Done.
| |
50 float capture_block[kMaxNumBands * kBlockSize]; | |
51 capture_blocker->ExtractBlockIfAvailable(capture_block); | |
hlundin-webrtc
2016/12/16 10:04:47
...IfAvailable? I thought we established that it w
peah-webrtc
2016/12/20 10:10:25
True, I changed the implementation of FrameBlocker
| |
52 block_processor->ProcessCapture(known_echo_path_change, | |
53 saturated_microphone_signal, capture_block); | |
54 output_framer->InsertBlock(capture_block); | |
55 } | |
56 } | |
57 | |
58 bool BufferRenderFrameContent(rtc::ArrayView<const float> render_frame, | |
59 size_t subframe_index, | |
60 FrameBlocker* render_blocker, | |
61 BlockProcessor* block_processor) { | |
62 return block_processor->BufferRender( | |
63 [render_blocker, render_frame, | |
64 subframe_index](rtc::ArrayView<float> block) mutable { | |
65 render_blocker->InsertFrameAndExtractBlock(subframe_index, render_frame, | |
66 block); | |
67 }); | |
ivoc
2016/12/19 11:30:22
Can you explain what this is supposed to do? The w
peah-webrtc
2016/12/20 10:10:25
Yes, the reason for this construct is to provide a
ivoc
2016/12/21 13:04:04
It looks easier to understand now.
peah-webrtc
2016/12/21 23:13:48
Acknowledged.
| |
68 } | |
69 | |
70 bool BufferRemainingRenderFrameContent(FrameBlocker* render_blocker, | |
71 BlockProcessor* block_processor) { | |
72 if (render_blocker->IsBlockAvailable()) { | |
73 return block_processor->BufferRender( | |
74 [render_blocker](rtc::ArrayView<float> block) mutable { | |
75 render_blocker->ExtractBlockIfAvailable(block); | |
76 }); | |
77 } | |
78 return false; | |
79 } | |
80 | |
81 void CopyAudioBufferIntoFrame(AudioBuffer* buffer, | |
hlundin-webrtc
2016/12/16 10:04:47
I assume this cannot be made a const argument beca
peah-webrtc
2016/12/20 10:10:25
Afaics, that is the case.
| |
82 size_t num_bands, | |
83 size_t frame_length, | |
84 rtc::ArrayView<float> frame) { | |
85 RTC_DCHECK_EQ(num_bands * frame_length, frame.size()); | |
86 for (size_t i = 0; i < num_bands; ++i) { | |
87 rtc::ArrayView<float> buffer_view(&buffer->split_bands_f(0)[i][0], | |
88 frame_length); | |
89 std::copy(buffer_view.begin(), buffer_view.end(), | |
90 frame.begin() + i * frame_length); | |
91 } | |
92 } | |
93 | |
94 // [B,A] = butter(2,100/4000,'high') | |
95 const CascadedBiQuadFilter::BiQuadCoefficients | |
96 kHighPassFilterCoefficients_8kHz = { | |
97 {0.945976856002790, -1.891953712005580, 0.945976856002790}, | |
98 {-1.889033079394525, 0.894874344616636}}; | |
99 const int kNumberOfHighPassBiQuads_8kHz = 1; | |
100 | |
101 // [B,A] = butter(2,100/8000,'high') | |
102 const CascadedBiQuadFilter::BiQuadCoefficients | |
103 kHighPassFilterCoefficients_16kHz = { | |
104 {0.972613898499844, -1.945227796999688, 0.972613898499844}, | |
105 {-1.944477657767094, 0.945977936232282}}; | |
106 const int kNumberOfHighPassBiQuads_16kHz = 1; | |
107 | |
108 } // namespace | |
109 | |
110 EchoCanceller3::RenderWriterState::RenderWriterState( | |
111 ApmDataDumper* data_dumper, | |
112 RenderTransferBufferWriter* transfer_buffer_writer, | |
113 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter, | |
114 int sample_rate_hz, | |
115 int frame_length, | |
116 int num_bands) | |
117 : data_dumper_(data_dumper), | |
hlundin-webrtc
2016/12/16 10:04:47
Is nullptr valid? Otherwise, add a DCHECK.
peah-webrtc
2016/12/20 10:10:25
Done.
| |
118 sample_rate_hz_(sample_rate_hz), | |
119 frame_length_(frame_length), | |
120 num_bands_(num_bands), | |
121 transfer_buffer_writer_(transfer_buffer_writer), | |
122 render_highpass_filter_(std::move(render_highpass_filter)), | |
123 render_queue_input_frame_(num_bands_ * frame_length_, 0.f) {} | |
124 | |
125 EchoCanceller3::RenderWriterState::~RenderWriterState() = default; | |
126 | |
127 bool EchoCanceller3::RenderWriterState::Insert(AudioBuffer* render) { | |
hlundin-webrtc
2016/12/16 10:04:46
"render" is a dubious name here. I think "input" w
peah-webrtc
2016/12/20 10:10:25
Done.
| |
31 RTC_DCHECK_EQ(1u, render->num_channels()); | 128 RTC_DCHECK_EQ(1u, render->num_channels()); |
hlundin-webrtc
2016/12/16 10:04:47
Nit: I think you no longer need the 'u' suffix, as
peah-webrtc
2016/12/20 10:10:25
Done.
| |
32 RTC_DCHECK_EQ(frame_length_, render->num_frames_per_band()); | 129 RTC_DCHECK_EQ(frame_length_, render->num_frames_per_band()); |
33 return true; | 130 data_dumper_->DumpWav("aec3_render_input", frame_length_, |
34 } | 131 &render->split_bands_f(0)[0][0], |
35 | 132 LowestBandRate(sample_rate_hz_), 1); |
36 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {} | 133 |
134 CopyAudioBufferIntoFrame(render, num_bands_, frame_length_, | |
135 render_queue_input_frame_); | |
136 | |
137 if (render_highpass_filter_) { | |
138 render_highpass_filter_->Process( | |
139 rtc::ArrayView<float>(&render_queue_input_frame_[0], frame_length_)); | |
140 } | |
141 | |
142 return transfer_buffer_writer_->Insert(&render_queue_input_frame_); | |
143 } | |
144 | |
145 int EchoCanceller3::instance_count_ = 0; | |
146 | |
147 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_highpass_filter) | |
148 : data_dumper_(new ApmDataDumper(instance_count_)), | |
149 sample_rate_hz_(sample_rate_hz), | |
150 num_bands_(sample_rate_hz_ == 8000 ? 1 : sample_rate_hz / 16000), | |
hlundin-webrtc
2016/12/16 10:04:47
You have this expression defined as NumBandsForRat
peah-webrtc
2016/12/20 10:10:25
Done.
| |
151 frame_length_(rtc::CheckedDivExact(LowestBandRate(sample_rate_hz_), 100)), | |
152 output_framer_(num_bands_, frame_length_), | |
153 capture_blocker_(num_bands_, frame_length_), | |
154 render_blocker_(num_bands_, frame_length_), | |
155 render_transfer_buffer_(num_bands_, frame_length_), | |
156 block_processor_(data_dumper_.get(), sample_rate_hz, num_bands_), | |
157 render_queue_output_frame_(num_bands_ * frame_length_, 0.f) { | |
158 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter; | |
159 if (use_highpass_filter) { | |
160 render_highpass_filter.reset(new CascadedBiQuadFilter( | |
161 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz | |
162 : kHighPassFilterCoefficients_16kHz, | |
163 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz | |
164 : kNumberOfHighPassBiQuads_16kHz)); | |
165 capture_highpass_filter_.reset(new CascadedBiQuadFilter( | |
166 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz | |
167 : kHighPassFilterCoefficients_16kHz, | |
168 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz | |
169 : kNumberOfHighPassBiQuads_16kHz)); | |
170 } | |
171 | |
172 render_writer_.reset( | |
173 new RenderWriterState(data_dumper_.get(), &render_transfer_buffer_, | |
174 std::move(render_highpass_filter), sample_rate_hz_, | |
hlundin-webrtc
2016/12/16 10:04:47
What happens when you move from an (explicitly) un
peah-webrtc
2016/12/20 10:10:25
Good point! I cannot find whether it actually init
| |
175 frame_length_, num_bands_)); | |
176 | |
177 RTC_DCHECK_EQ(num_bands_, std::max(sample_rate_hz_, 16000) / 16000); | |
178 RTC_DCHECK_GE(kMaxNumBands, num_bands_); | |
179 instance_count_ = rtc::AtomicOps::Increment(&instance_count_); | |
180 } | |
181 | |
182 EchoCanceller3::~EchoCanceller3() = default; | |
183 | |
184 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) { | |
185 return render_writer_->Insert(render); | |
186 } | |
187 | |
188 bool EchoCanceller3::EmptyRenderQueue() { | |
hlundin-webrtc
2016/12/16 10:04:47
Check the order of the methods in this file, and m
peah-webrtc
2016/12/20 10:10:25
Done.
| |
189 bool render_buffer_overrun = false; | |
190 bool frame_to_buffer = | |
191 render_transfer_buffer_.Remove(&render_queue_output_frame_); | |
192 while (frame_to_buffer) { | |
193 render_buffer_overrun |= BufferRenderFrameContent( | |
194 render_queue_output_frame_, 0, &render_blocker_, &block_processor_); | |
195 | |
196 if (sample_rate_hz_ != 8000) { | |
197 render_buffer_overrun |= BufferRenderFrameContent( | |
198 render_queue_output_frame_, 1, &render_blocker_, &block_processor_); | |
199 } | |
200 | |
201 render_buffer_overrun |= | |
202 BufferRemainingRenderFrameContent(&render_blocker_, &block_processor_); | |
203 | |
204 frame_to_buffer = | |
205 render_transfer_buffer_.Remove(&render_queue_output_frame_); | |
206 } | |
207 return render_buffer_overrun; | |
208 } | |
209 | |
210 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) { | |
211 data_dumper_->DumpWav("aec3_capture_analyze_input", frame_length_, | |
212 capture->channels_f()[0], sample_rate_hz_, 1); | |
213 | |
214 saturated_microphone_signal_ = false; | |
hlundin-webrtc
2016/12/16 10:04:47
You reset saturated_microphone_signal_ both here a
peah-webrtc
2016/12/20 10:10:25
Done.
| |
215 for (size_t k = 0; k < capture->num_channels(); ++k) { | |
216 saturated_microphone_signal_ |= | |
hlundin-webrtc
2016/12/16 10:04:47
I assume you will expand the work of this for loop
peah-webrtc
2016/12/20 10:10:25
There will be no more work on this loop. I added e
| |
217 DetectSaturation(rtc::ArrayView<const float>(capture->channels_f()[k], | |
218 capture->num_frames())); | |
219 } | |
220 } | |
37 | 221 |
38 void EchoCanceller3::ProcessCapture(AudioBuffer* capture, | 222 void EchoCanceller3::ProcessCapture(AudioBuffer* capture, |
39 bool known_echo_path_change) { | 223 bool known_echo_path_change) { |
40 RTC_DCHECK_EQ(1u, capture->num_channels()); | 224 RTC_DCHECK_EQ(1u, capture->num_channels()); |
41 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band()); | 225 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band()); |
226 | |
227 rtc::ArrayView<float> capture_lower_band = | |
228 rtc::ArrayView<float>(&capture->split_bands_f(0)[0][0], frame_length_); | |
229 | |
230 data_dumper_->DumpWav("aec3_capture_input", capture_lower_band, | |
231 LowestBandRate(sample_rate_hz_), 1); | |
232 | |
233 bool render_buffer_overrun = EmptyRenderQueue(); | |
234 RTC_DCHECK(!render_buffer_overrun); | |
235 | |
236 if (capture_highpass_filter_) { | |
237 capture_highpass_filter_->Process(capture_lower_band); | |
238 } | |
239 | |
240 ProcessCaptureFrameContent(capture, known_echo_path_change, | |
241 saturated_microphone_signal_, 0, &capture_blocker_, | |
242 &output_framer_, &block_processor_); | |
243 | |
244 if (sample_rate_hz_ != 8000) { | |
245 ProcessCaptureFrameContent( | |
246 capture, known_echo_path_change, saturated_microphone_signal_, 1, | |
247 &capture_blocker_, &output_framer_, &block_processor_); | |
248 } | |
249 | |
250 ProcessRemainingCaptureFrameContent( | |
251 known_echo_path_change, saturated_microphone_signal_, &capture_blocker_, | |
252 &output_framer_, &block_processor_); | |
253 | |
254 data_dumper_->DumpWav("aec3_capture_output", frame_length_, | |
255 &capture->split_bands_f(0)[0][0], | |
256 LowestBandRate(sample_rate_hz_), 1); | |
257 | |
258 saturated_microphone_signal_ = false; | |
42 } | 259 } |
43 | 260 |
44 std::string EchoCanceller3::ToString( | 261 std::string EchoCanceller3::ToString( |
45 const AudioProcessing::Config::EchoCanceller3& config) { | 262 const AudioProcessing::Config::EchoCanceller3& config) { |
46 std::stringstream ss; | 263 std::stringstream ss; |
47 ss << "{" | 264 ss << "{" |
48 << "enabled: " << (config.enabled ? "true" : "false") << "}"; | 265 << "enabled: " << (config.enabled ? "true" : "false") << "}"; |
49 return ss.str(); | 266 return ss.str(); |
50 } | 267 } |
51 | 268 |
52 bool EchoCanceller3::Validate( | 269 bool EchoCanceller3::Validate( |
53 const AudioProcessing::Config::EchoCanceller3& config) { | 270 const AudioProcessing::Config::EchoCanceller3& config) { |
54 return true; | 271 return true; |
55 } | 272 } |
56 | 273 |
57 } // namespace webrtc | 274 } // namespace webrtc |
OLD | NEW |