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 namespace { | |
18 | |
19 bool DetectSaturation(rtc::ArrayView<const float> y) { | |
20 for (auto y_k : y) { | |
21 if (y_k >= 32767.0f || y_k <= -32768.0f) { | |
22 return true; | |
23 } | |
24 } | |
25 return false; | |
26 } | |
27 | |
28 void FillSubFrameView(AudioBuffer* frame, | |
29 size_t sub_frame_index, | |
30 rtc::ArrayView<rtc::ArrayView<float>> sub_frame_view) { | |
aleloi
2016/12/20 15:55:35
Can this FillSubFrameView be implemented in terms
peah-webrtc
2016/12/21 23:13:51
I changed this to instead used a vector stored on
| |
31 for (size_t k = 0; k < frame->num_bands(); ++k) { | |
32 sub_frame_view[k] = rtc::ArrayView<float>( | |
33 &frame->split_bands_f(0)[k][sub_frame_index * kSubFrameLength], | |
34 kSubFrameLength); | |
35 } | |
36 } | |
37 | |
38 void FillSubFrameView( | |
39 AudioBuffer* frame, | |
40 size_t sub_frame_index, | |
41 rtc::ArrayView<rtc::ArrayView<const float>> sub_frame_view) { | |
42 for (size_t k = 0; k < frame->num_bands(); ++k) { | |
43 sub_frame_view[k] = rtc::ArrayView<const float>( | |
44 &frame->split_bands_f(0)[k][sub_frame_index * kSubFrameLength], | |
45 kSubFrameLength); | |
46 } | |
47 } | |
48 | |
49 void FillSubFrameView( | |
50 const std::vector<std::vector<float>>& frame, | |
51 size_t sub_frame_index, | |
52 rtc::ArrayView<rtc::ArrayView<const float>> sub_frame_view) { | |
53 for (size_t k = 0; k < frame.size(); ++k) { | |
54 sub_frame_view[k] = rtc::ArrayView<const float>( | |
55 &frame[k][sub_frame_index * kSubFrameLength], kSubFrameLength); | |
56 } | |
57 } | |
58 | |
59 void ProcessCaptureFrameContent(AudioBuffer* capture, | |
60 bool known_echo_path_change, | |
61 bool saturated_microphone_signal, | |
62 size_t sub_frame_index, | |
63 FrameBlocker* capture_blocker, | |
64 BlockFramer* output_framer, | |
65 BlockProcessor* block_processor, | |
66 std::vector<std::vector<float>>* block) { | |
67 // Array of ArrayViews. | |
68 rtc::ArrayView<float> sub_frame_view_data[block->size()]; | |
hlundin-webrtc
2016/12/20 15:10:35
This is not pretty. Sorry...
Is there any way we c
aleloi
2016/12/21 10:07:45
Parts of the complexity is here because ArrayViews
peah-webrtc
2016/12/21 23:13:50
I changed to using a vector that is cached on the
peah-webrtc
2016/12/21 23:13:51
Agree. I instead solved this by using a vector sto
| |
69 rtc::ArrayView<const float> sub_frame_const_view_data[block->size()]; | |
70 auto sub_frame_view = rtc::ArrayView<rtc::ArrayView<float>>( | |
71 &sub_frame_view_data[0], block->size()); | |
72 auto sub_frame_const_view = rtc::ArrayView<rtc::ArrayView<const float>>( | |
73 &sub_frame_const_view_data[0], block->size()); | |
74 | |
75 FillSubFrameView(capture, sub_frame_index, sub_frame_view); | |
aleloi
2016/12/21 10:07:45
Better to move creation and filling of sub_frame_v
peah-webrtc
2016/12/21 23:13:51
Agree.
With the new change, it looks better.
PTAL
| |
76 FillSubFrameView(capture, sub_frame_index, sub_frame_const_view); | |
77 | |
78 capture_blocker->InsertSubFrameAndExtractBlock(sub_frame_const_view, block); | |
aleloi
2016/12/21 10:07:45
Is there a type error if a non-const view is passe
peah-webrtc
2016/12/21 23:13:50
Yes, there was a type error. I don't think the aut
| |
79 block_processor->ProcessCapture(known_echo_path_change, | |
80 saturated_microphone_signal, block); | |
81 output_framer->InsertBlockAndExtractSubFrame(*block, sub_frame_view); | |
82 } | |
83 | |
84 void ProcessRemainingCaptureFrameContent( | |
85 bool known_echo_path_change, | |
86 bool saturated_microphone_signal, | |
87 FrameBlocker* capture_blocker, | |
88 BlockFramer* output_framer, | |
89 BlockProcessor* block_processor, | |
90 std::vector<std::vector<float>>* block) { | |
91 if (!capture_blocker->IsBlockAvailable()) { | |
92 return; | |
93 } | |
94 | |
95 capture_blocker->ExtractBlock(block); | |
96 block_processor->ProcessCapture(known_echo_path_change, | |
97 saturated_microphone_signal, block); | |
98 output_framer->InsertBlock(*block); | |
99 } | |
100 | |
101 bool BufferRenderFrameContent( | |
102 const std::vector<std::vector<float>>& render_frame, | |
103 size_t sub_frame_index, | |
104 FrameBlocker* render_blocker, | |
105 BlockProcessor* block_processor, | |
106 std::vector<std::vector<float>>* block) { | |
107 // Array of ArrayViews. | |
108 rtc::ArrayView<const float> sub_frame_const_view_data[block->size()]; | |
109 auto sub_frame_const_view = rtc::ArrayView<rtc::ArrayView<const float>>( | |
110 &sub_frame_const_view_data[0], block->size()); | |
111 | |
112 FillSubFrameView(render_frame, sub_frame_index, sub_frame_const_view); | |
113 | |
114 render_blocker->InsertSubFrameAndExtractBlock(sub_frame_const_view, block); | |
115 return block_processor->BufferRender(block); | |
116 } | |
117 | |
118 bool BufferRemainingRenderFrameContent(FrameBlocker* render_blocker, | |
119 BlockProcessor* block_processor, | |
120 std::vector<std::vector<float>>* block) { | |
121 if (!render_blocker->IsBlockAvailable()) { | |
122 return false; | |
123 } | |
124 render_blocker->ExtractBlock(block); | |
125 return block_processor->BufferRender(block); | |
126 } | |
127 | |
128 void CopyAudioBufferIntoFrame(AudioBuffer* buffer, | |
129 size_t num_bands, | |
130 size_t frame_length, | |
131 std::vector<std::vector<float>>* frame) { | |
132 RTC_DCHECK_EQ(num_bands, frame->size()); | |
133 for (size_t i = 0; i < num_bands; ++i) { | |
134 rtc::ArrayView<float> buffer_view(&buffer->split_bands_f(0)[i][0], | |
135 frame_length); | |
136 std::copy(buffer_view.begin(), buffer_view.end(), (*frame)[i].begin()); | |
137 } | |
138 } | |
139 | |
140 // [B,A] = butter(2,100/4000,'high') | |
141 const CascadedBiQuadFilter::BiQuadCoefficients | |
142 kHighPassFilterCoefficients_8kHz = { | |
143 {0.945976856002790, -1.891953712005580, 0.945976856002790}, | |
144 {-1.889033079394525, 0.894874344616636}}; | |
145 const int kNumberOfHighPassBiQuads_8kHz = 1; | |
146 | |
147 // [B,A] = butter(2,100/8000,'high') | |
148 const CascadedBiQuadFilter::BiQuadCoefficients | |
149 kHighPassFilterCoefficients_16kHz = { | |
150 {0.972613898499844, -1.945227796999688, 0.972613898499844}, | |
151 {-1.944477657767094, 0.945977936232282}}; | |
152 const int kNumberOfHighPassBiQuads_16kHz = 1; | |
153 | |
154 static const size_t kRenderTransferQueueSize = 30; | |
155 | |
156 } // namespace | |
157 | |
158 class EchoCanceller3::RenderWriter { | |
159 public: | |
160 RenderWriter(ApmDataDumper* data_dumper, | |
161 SwapQueue<std::vector<std::vector<float>>, | |
162 Aec3RenderQueueItemVerifier>* render_transfer_queue, | |
163 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter, | |
164 int sample_rate_hz, | |
165 int frame_length, | |
166 int num_bands); | |
167 ~RenderWriter(); | |
168 bool Insert(AudioBuffer* render); | |
169 | |
170 private: | |
171 ApmDataDumper* data_dumper_; | |
172 const int sample_rate_hz_; | |
173 const size_t frame_length_; | |
174 const int num_bands_; | |
175 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter_; | |
176 std::vector<std::vector<float>> render_queue_input_frame_; | |
177 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>* | |
178 render_transfer_queue_; | |
179 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWriter); | |
180 }; | |
181 | |
182 EchoCanceller3::RenderWriter::RenderWriter( | |
183 ApmDataDumper* data_dumper, | |
184 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>* | |
185 render_transfer_queue, | |
186 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter, | |
187 int sample_rate_hz, | |
188 int frame_length, | |
189 int num_bands) | |
190 : data_dumper_(data_dumper), | |
191 sample_rate_hz_(sample_rate_hz), | |
192 frame_length_(frame_length), | |
193 num_bands_(num_bands), | |
194 render_highpass_filter_(std::move(render_highpass_filter)), | |
195 render_queue_input_frame_(num_bands_, | |
196 std::vector<float>(frame_length_, 0.f)), | |
197 render_transfer_queue_(render_transfer_queue) { | |
198 RTC_DCHECK(data_dumper); | |
199 } | |
200 | |
201 EchoCanceller3::RenderWriter::~RenderWriter() = default; | |
202 | |
203 bool EchoCanceller3::RenderWriter::Insert(AudioBuffer* input) { | |
204 RTC_DCHECK_EQ(1, input->num_channels()); | |
205 RTC_DCHECK_EQ(frame_length_, input->num_frames_per_band()); | |
206 data_dumper_->DumpWav("aec3_render_input", frame_length_, | |
207 &input->split_bands_f(0)[0][0], | |
208 LowestBandRate(sample_rate_hz_), 1); | |
209 | |
210 CopyAudioBufferIntoFrame(input, num_bands_, frame_length_, | |
211 &render_queue_input_frame_); | |
212 | |
213 if (render_highpass_filter_) { | |
214 render_highpass_filter_->Process(render_queue_input_frame_[0]); | |
215 } | |
216 | |
217 return render_transfer_queue_->Insert(&render_queue_input_frame_); | |
218 } | |
219 | |
17 int EchoCanceller3::instance_count_ = 0; | 220 int EchoCanceller3::instance_count_ = 0; |
18 | 221 |
19 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_anti_hum_filter) { | 222 EchoCanceller3::EchoCanceller3(int sample_rate_hz, bool use_highpass_filter) |
20 int band_sample_rate_hz = (sample_rate_hz == 8000 ? sample_rate_hz : 16000); | 223 : EchoCanceller3(sample_rate_hz, |
21 frame_length_ = rtc::CheckedDivExact(band_sample_rate_hz, 100); | 224 use_highpass_filter, |
22 | 225 BlockProcessor::Create(sample_rate_hz, |
23 LOG(LS_INFO) << "AEC3 created : " | 226 NumBandsForRate(sample_rate_hz))) {} |
24 << "{ instance_count: " << instance_count_ << "}"; | 227 EchoCanceller3::EchoCanceller3(int sample_rate_hz, |
228 bool use_highpass_filter, | |
229 BlockProcessor* block_processor) | |
230 : data_dumper_(new ApmDataDumper(instance_count_)), | |
231 sample_rate_hz_(sample_rate_hz), | |
232 num_bands_(NumBandsForRate(sample_rate_hz_)), | |
233 frame_length_(rtc::CheckedDivExact(LowestBandRate(sample_rate_hz_), 100)), | |
234 output_framer_(num_bands_), | |
235 capture_blocker_(num_bands_), | |
236 render_blocker_(num_bands_), | |
237 render_transfer_queue_( | |
238 kRenderTransferQueueSize, | |
239 std::vector<std::vector<float>>( | |
240 num_bands_, | |
241 std::vector<float>(frame_length_, 0.f)), | |
242 Aec3RenderQueueItemVerifier(num_bands_, frame_length_)), | |
243 block_processor_(block_processor), | |
244 render_queue_output_frame_(num_bands_, | |
245 std::vector<float>(frame_length_, 0.f)), | |
246 block_(num_bands_, std::vector<float>(kBlockSize, 0.f)) { | |
247 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter; | |
248 if (use_highpass_filter) { | |
249 render_highpass_filter.reset(new CascadedBiQuadFilter( | |
250 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz | |
251 : kHighPassFilterCoefficients_16kHz, | |
252 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz | |
253 : kNumberOfHighPassBiQuads_16kHz)); | |
254 capture_highpass_filter_.reset(new CascadedBiQuadFilter( | |
255 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz | |
256 : kHighPassFilterCoefficients_16kHz, | |
257 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz | |
258 : kNumberOfHighPassBiQuads_16kHz)); | |
259 } else { | |
260 render_highpass_filter.reset(nullptr); | |
ivoc
2016/12/21 13:04:05
Is this really necessary? Isn't it set to nullptr
peah-webrtc
2016/12/21 23:13:51
I'm not sure. The issue was that there was an expl
ivoc
2016/12/22 13:38:13
I'm pretty sure that it's not needed, the default
peah-webrtc
2017/01/02 08:45:10
Thanks! I agree.
Done.
| |
261 capture_highpass_filter_.reset(nullptr); | |
262 } | |
263 | |
264 render_writer_.reset( | |
265 new RenderWriter(data_dumper_.get(), &render_transfer_queue_, | |
266 std::move(render_highpass_filter), sample_rate_hz_, | |
267 frame_length_, num_bands_)); | |
268 | |
269 RTC_DCHECK_EQ(num_bands_, std::max(sample_rate_hz_, 16000) / 16000); | |
270 RTC_DCHECK_GE(kMaxNumBands, num_bands_); | |
25 instance_count_ = rtc::AtomicOps::Increment(&instance_count_); | 271 instance_count_ = rtc::AtomicOps::Increment(&instance_count_); |
26 } | 272 } |
27 | 273 |
28 EchoCanceller3::~EchoCanceller3() = default; | 274 EchoCanceller3::~EchoCanceller3() = default; |
29 | 275 |
30 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) { | 276 bool EchoCanceller3::AnalyzeRender(AudioBuffer* render) { |
31 RTC_DCHECK_EQ(1u, render->num_channels()); | 277 return render_writer_->Insert(render); |
32 RTC_DCHECK_EQ(frame_length_, render->num_frames_per_band()); | 278 } |
33 return true; | 279 |
34 } | 280 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) { |
35 | 281 data_dumper_->DumpWav("aec3_capture_analyze_input", frame_length_, |
36 void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {} | 282 capture->channels_f()[0], sample_rate_hz_, 1); |
283 | |
284 saturated_microphone_signal_ = false; | |
285 for (size_t k = 0; k < capture->num_channels(); ++k) { | |
286 saturated_microphone_signal_ |= | |
287 DetectSaturation(rtc::ArrayView<const float>(capture->channels_f()[k], | |
288 capture->num_frames())); | |
289 if (saturated_microphone_signal_) { | |
290 break; | |
291 } | |
292 } | |
293 } | |
37 | 294 |
38 void EchoCanceller3::ProcessCapture(AudioBuffer* capture, | 295 void EchoCanceller3::ProcessCapture(AudioBuffer* capture, |
39 bool known_echo_path_change) { | 296 bool known_echo_path_change) { |
40 RTC_DCHECK_EQ(1u, capture->num_channels()); | 297 RTC_DCHECK_EQ(1u, capture->num_channels()); |
41 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band()); | 298 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band()); |
299 | |
300 rtc::ArrayView<float> capture_lower_band = | |
301 rtc::ArrayView<float>(&capture->split_bands_f(0)[0][0], frame_length_); | |
302 | |
303 data_dumper_->DumpWav("aec3_capture_input", capture_lower_band, | |
304 LowestBandRate(sample_rate_hz_), 1); | |
305 | |
306 bool render_buffer_overrun = EmptyRenderQueue(); | |
307 RTC_DCHECK(!render_buffer_overrun); | |
308 | |
309 if (capture_highpass_filter_) { | |
310 capture_highpass_filter_->Process(capture_lower_band); | |
311 } | |
312 | |
313 ProcessCaptureFrameContent(capture, known_echo_path_change, | |
314 saturated_microphone_signal_, 0, &capture_blocker_, | |
315 &output_framer_, block_processor_.get(), &block_); | |
316 | |
317 if (sample_rate_hz_ != 8000) { | |
318 ProcessCaptureFrameContent( | |
319 capture, known_echo_path_change, saturated_microphone_signal_, 1, | |
320 &capture_blocker_, &output_framer_, block_processor_.get(), &block_); | |
321 } | |
322 | |
323 ProcessRemainingCaptureFrameContent( | |
324 known_echo_path_change, saturated_microphone_signal_, &capture_blocker_, | |
325 &output_framer_, block_processor_.get(), &block_); | |
326 | |
327 data_dumper_->DumpWav("aec3_capture_output", frame_length_, | |
328 &capture->split_bands_f(0)[0][0], | |
329 LowestBandRate(sample_rate_hz_), 1); | |
42 } | 330 } |
43 | 331 |
44 std::string EchoCanceller3::ToString( | 332 std::string EchoCanceller3::ToString( |
45 const AudioProcessing::Config::EchoCanceller3& config) { | 333 const AudioProcessing::Config::EchoCanceller3& config) { |
46 std::stringstream ss; | 334 std::stringstream ss; |
47 ss << "{" | 335 ss << "{" |
48 << "enabled: " << (config.enabled ? "true" : "false") << "}"; | 336 << "enabled: " << (config.enabled ? "true" : "false") << "}"; |
49 return ss.str(); | 337 return ss.str(); |
50 } | 338 } |
51 | 339 |
52 bool EchoCanceller3::Validate( | 340 bool EchoCanceller3::Validate( |
53 const AudioProcessing::Config::EchoCanceller3& config) { | 341 const AudioProcessing::Config::EchoCanceller3& config) { |
54 return true; | 342 return true; |
55 } | 343 } |
56 | 344 |
345 bool EchoCanceller3::EmptyRenderQueue() { | |
346 bool render_buffer_overrun = false; | |
347 bool frame_to_buffer = | |
348 render_transfer_queue_.Remove(&render_queue_output_frame_); | |
349 while (frame_to_buffer) { | |
350 render_buffer_overrun |= BufferRenderFrameContent( | |
351 render_queue_output_frame_, 0, &render_blocker_, block_processor_.get(), | |
352 &block_); | |
353 | |
354 if (sample_rate_hz_ != 8000) { | |
355 render_buffer_overrun |= BufferRenderFrameContent( | |
356 render_queue_output_frame_, 1, &render_blocker_, | |
357 block_processor_.get(), &block_); | |
358 } | |
359 | |
360 render_buffer_overrun |= BufferRemainingRenderFrameContent( | |
361 &render_blocker_, block_processor_.get(), &block_); | |
362 | |
363 frame_to_buffer = | |
364 render_transfer_queue_.Remove(&render_queue_output_frame_); | |
365 } | |
366 return render_buffer_overrun; | |
367 } | |
368 | |
57 } // namespace webrtc | 369 } // namespace webrtc |
OLD | NEW |