blob: f2daf57345ee13c3985bdfcfe1f0799e909b6754 [file] [log] [blame]
peahe0eae3c2016-12-14 01:16:23 -08001/*
2 * Copyright (c) 2016 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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/audio_processing/aec3/echo_canceller3.h"
peahe0eae3c2016-12-14 01:16:23 -080011
Yves Gerey988cc082018-10-23 12:03:01 +020012#include <algorithm>
13#include <utility>
14
15#include "modules/audio_processing/aec3/aec3_common.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/atomic_ops.h"
peahe0eae3c2016-12-14 01:16:23 -080018
19namespace webrtc {
20
peahd0263542017-01-03 04:20:34 -080021namespace {
22
peahcf02cf12017-04-05 14:18:07 -070023enum class EchoCanceller3ApiCall { kCapture, kRender };
24
peahd0263542017-01-03 04:20:34 -080025bool DetectSaturation(rtc::ArrayView<const float> y) {
26 for (auto y_k : y) {
peah522d71b2017-02-23 05:16:26 -080027 if (y_k >= 32700.0f || y_k <= -32700.0f) {
peahd0263542017-01-03 04:20:34 -080028 return true;
29 }
30 }
31 return false;
32}
33
Per Åhgren251c7352018-03-28 16:31:57 +020034// Method for adjusting config parameter dependencies..
35EchoCanceller3Config AdjustConfig(const EchoCanceller3Config& config) {
36 EchoCanceller3Config adjusted_cfg = config;
Per Åhgren251c7352018-03-28 16:31:57 +020037 return adjusted_cfg;
38}
39
peahd0263542017-01-03 04:20:34 -080040void FillSubFrameView(AudioBuffer* frame,
41 size_t sub_frame_index,
42 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
43 RTC_DCHECK_GE(1, sub_frame_index);
44 RTC_DCHECK_LE(0, sub_frame_index);
45 RTC_DCHECK_EQ(frame->num_bands(), sub_frame_view->size());
46 for (size_t k = 0; k < sub_frame_view->size(); ++k) {
47 (*sub_frame_view)[k] = rtc::ArrayView<float>(
48 &frame->split_bands_f(0)[k][sub_frame_index * kSubFrameLength],
49 kSubFrameLength);
50 }
51}
52
53void FillSubFrameView(std::vector<std::vector<float>>* frame,
54 size_t sub_frame_index,
55 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
56 RTC_DCHECK_GE(1, sub_frame_index);
57 RTC_DCHECK_EQ(frame->size(), sub_frame_view->size());
58 for (size_t k = 0; k < frame->size(); ++k) {
59 (*sub_frame_view)[k] = rtc::ArrayView<float>(
60 &(*frame)[k][sub_frame_index * kSubFrameLength], kSubFrameLength);
61 }
62}
63
64void ProcessCaptureFrameContent(
65 AudioBuffer* capture,
peah69221db2017-01-27 03:28:19 -080066 bool level_change,
peahd0263542017-01-03 04:20:34 -080067 bool saturated_microphone_signal,
68 size_t sub_frame_index,
69 FrameBlocker* capture_blocker,
70 BlockFramer* output_framer,
71 BlockProcessor* block_processor,
72 std::vector<std::vector<float>>* block,
73 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
74 FillSubFrameView(capture, sub_frame_index, sub_frame_view);
75 capture_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
peah69221db2017-01-27 03:28:19 -080076 block_processor->ProcessCapture(level_change, saturated_microphone_signal,
77 block);
peahd0263542017-01-03 04:20:34 -080078 output_framer->InsertBlockAndExtractSubFrame(*block, sub_frame_view);
79}
80
81void ProcessRemainingCaptureFrameContent(
peah69221db2017-01-27 03:28:19 -080082 bool level_change,
peahd0263542017-01-03 04:20:34 -080083 bool saturated_microphone_signal,
84 FrameBlocker* capture_blocker,
85 BlockFramer* output_framer,
86 BlockProcessor* block_processor,
87 std::vector<std::vector<float>>* block) {
88 if (!capture_blocker->IsBlockAvailable()) {
89 return;
90 }
91
92 capture_blocker->ExtractBlock(block);
peah69221db2017-01-27 03:28:19 -080093 block_processor->ProcessCapture(level_change, saturated_microphone_signal,
94 block);
peahd0263542017-01-03 04:20:34 -080095 output_framer->InsertBlock(*block);
96}
97
peahcf02cf12017-04-05 14:18:07 -070098void BufferRenderFrameContent(
peahd0263542017-01-03 04:20:34 -080099 std::vector<std::vector<float>>* render_frame,
100 size_t sub_frame_index,
101 FrameBlocker* render_blocker,
102 BlockProcessor* block_processor,
103 std::vector<std::vector<float>>* block,
104 std::vector<rtc::ArrayView<float>>* sub_frame_view) {
105 FillSubFrameView(render_frame, sub_frame_index, sub_frame_view);
106 render_blocker->InsertSubFrameAndExtractBlock(*sub_frame_view, block);
peahcf02cf12017-04-05 14:18:07 -0700107 block_processor->BufferRender(*block);
peahd0263542017-01-03 04:20:34 -0800108}
109
peahcf02cf12017-04-05 14:18:07 -0700110void BufferRemainingRenderFrameContent(FrameBlocker* render_blocker,
peahd0263542017-01-03 04:20:34 -0800111 BlockProcessor* block_processor,
112 std::vector<std::vector<float>>* block) {
113 if (!render_blocker->IsBlockAvailable()) {
peahcf02cf12017-04-05 14:18:07 -0700114 return;
peahd0263542017-01-03 04:20:34 -0800115 }
116 render_blocker->ExtractBlock(block);
peahcf02cf12017-04-05 14:18:07 -0700117 block_processor->BufferRender(*block);
peahd0263542017-01-03 04:20:34 -0800118}
119
peahcf02cf12017-04-05 14:18:07 -0700120void CopyBufferIntoFrame(AudioBuffer* buffer,
121 size_t num_bands,
122 size_t frame_length,
123 std::vector<std::vector<float>>* frame) {
peahd0263542017-01-03 04:20:34 -0800124 RTC_DCHECK_EQ(num_bands, frame->size());
peah522d71b2017-02-23 05:16:26 -0800125 RTC_DCHECK_EQ(frame_length, (*frame)[0].size());
peahcf02cf12017-04-05 14:18:07 -0700126 for (size_t k = 0; k < num_bands; ++k) {
127 rtc::ArrayView<float> buffer_view(&buffer->split_bands_f(0)[k][0],
128 frame_length);
129 std::copy(buffer_view.begin(), buffer_view.end(), (*frame)[k].begin());
130 }
peahd0263542017-01-03 04:20:34 -0800131}
132
133// [B,A] = butter(2,100/4000,'high')
134const CascadedBiQuadFilter::BiQuadCoefficients
135 kHighPassFilterCoefficients_8kHz = {{0.94598f, -1.89195f, 0.94598f},
136 {-1.88903f, 0.89487f}};
137const int kNumberOfHighPassBiQuads_8kHz = 1;
138
139// [B,A] = butter(2,100/8000,'high')
140const CascadedBiQuadFilter::BiQuadCoefficients
141 kHighPassFilterCoefficients_16kHz = {{0.97261f, -1.94523f, 0.97261f},
142 {-1.94448f, 0.94598f}};
143const int kNumberOfHighPassBiQuads_16kHz = 1;
144
peahd0263542017-01-03 04:20:34 -0800145} // namespace
146
147class EchoCanceller3::RenderWriter {
148 public:
Mirko Bonadeif0d9cda2019-01-17 20:43:58 +0000149 RenderWriter(ApmDataDumper* data_dumper,
150 SwapQueue<std::vector<std::vector<float>>,
151 Aec3RenderQueueItemVerifier>* render_transfer_queue,
152 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter,
153 int sample_rate_hz,
154 int frame_length,
155 int num_bands);
peahd0263542017-01-03 04:20:34 -0800156 ~RenderWriter();
Alex Loiko890988c2017-08-31 10:25:48 +0200157 void Insert(AudioBuffer* input);
peahd0263542017-01-03 04:20:34 -0800158
159 private:
160 ApmDataDumper* data_dumper_;
161 const int sample_rate_hz_;
162 const size_t frame_length_;
163 const int num_bands_;
164 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter_;
165 std::vector<std::vector<float>> render_queue_input_frame_;
Mirko Bonadeif0d9cda2019-01-17 20:43:58 +0000166 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>*
167 render_transfer_queue_;
peahd0263542017-01-03 04:20:34 -0800168 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWriter);
169};
170
171EchoCanceller3::RenderWriter::RenderWriter(
172 ApmDataDumper* data_dumper,
Mirko Bonadeif0d9cda2019-01-17 20:43:58 +0000173 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>*
174 render_transfer_queue,
peahd0263542017-01-03 04:20:34 -0800175 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter,
176 int sample_rate_hz,
177 int frame_length,
178 int num_bands)
179 : data_dumper_(data_dumper),
180 sample_rate_hz_(sample_rate_hz),
181 frame_length_(frame_length),
182 num_bands_(num_bands),
183 render_highpass_filter_(std::move(render_highpass_filter)),
184 render_queue_input_frame_(num_bands_,
185 std::vector<float>(frame_length_, 0.f)),
186 render_transfer_queue_(render_transfer_queue) {
187 RTC_DCHECK(data_dumper);
188}
189
190EchoCanceller3::RenderWriter::~RenderWriter() = default;
191
peahcf02cf12017-04-05 14:18:07 -0700192void EchoCanceller3::RenderWriter::Insert(AudioBuffer* input) {
peahd0263542017-01-03 04:20:34 -0800193 RTC_DCHECK_EQ(1, input->num_channels());
peahd0263542017-01-03 04:20:34 -0800194 RTC_DCHECK_EQ(frame_length_, input->num_frames_per_band());
Gustaf Ullberg7d042782018-01-16 13:39:27 +0100195 RTC_DCHECK_EQ(num_bands_, input->num_bands());
196
197 // TODO(bugs.webrtc.org/8759) Temporary work-around.
198 if (num_bands_ != static_cast<int>(input->num_bands()))
199 return;
200
peahd0263542017-01-03 04:20:34 -0800201 data_dumper_->DumpWav("aec3_render_input", frame_length_,
peahcf02cf12017-04-05 14:18:07 -0700202 &input->split_bands_f(0)[0][0],
peahd0263542017-01-03 04:20:34 -0800203 LowestBandRate(sample_rate_hz_), 1);
204
peahcf02cf12017-04-05 14:18:07 -0700205 CopyBufferIntoFrame(input, num_bands_, frame_length_,
206 &render_queue_input_frame_);
peahd0263542017-01-03 04:20:34 -0800207
208 if (render_highpass_filter_) {
209 render_highpass_filter_->Process(render_queue_input_frame_[0]);
210 }
211
peah925e9d72017-04-10 04:18:38 -0700212 static_cast<void>(render_transfer_queue_->Insert(&render_queue_input_frame_));
peahd0263542017-01-03 04:20:34 -0800213}
214
peahe0eae3c2016-12-14 01:16:23 -0800215int EchoCanceller3::instance_count_ = 0;
216
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200217EchoCanceller3::EchoCanceller3(const EchoCanceller3Config& config,
218 int sample_rate_hz,
219 bool use_highpass_filter)
Gustaf Ullberge47433f2019-01-24 16:00:57 +0100220 : EchoCanceller3(
221 AdjustConfig(config),
222 sample_rate_hz,
223 use_highpass_filter,
224 std::unique_ptr<BlockProcessor>(
225 BlockProcessor::Create(AdjustConfig(config), sample_rate_hz))) {}
Per Åhgren8ba58612017-12-01 23:01:44 +0100226EchoCanceller3::EchoCanceller3(const EchoCanceller3Config& config,
227 int sample_rate_hz,
peahd0263542017-01-03 04:20:34 -0800228 bool use_highpass_filter,
229 std::unique_ptr<BlockProcessor> block_processor)
230 : data_dumper_(
231 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren398689f2018-08-23 11:38:27 +0200232 config_(config),
peahd0263542017-01-03 04:20:34 -0800233 sample_rate_hz_(sample_rate_hz),
234 num_bands_(NumBandsForRate(sample_rate_hz_)),
235 frame_length_(rtc::CheckedDivExact(LowestBandRate(sample_rate_hz_), 100)),
236 output_framer_(num_bands_),
237 capture_blocker_(num_bands_),
238 render_blocker_(num_bands_),
Mirko Bonadeif0d9cda2019-01-17 20:43:58 +0000239 render_transfer_queue_(
240 kRenderTransferQueueSizeFrames,
241 std::vector<std::vector<float>>(
242 num_bands_,
243 std::vector<float>(frame_length_, 0.f)),
244 Aec3RenderQueueItemVerifier(num_bands_, frame_length_)),
peahd0263542017-01-03 04:20:34 -0800245 block_processor_(std::move(block_processor)),
246 render_queue_output_frame_(num_bands_,
247 std::vector<float>(frame_length_, 0.f)),
248 block_(num_bands_, std::vector<float>(kBlockSize, 0.f)),
Per Åhgren398689f2018-08-23 11:38:27 +0200249 sub_frame_view_(num_bands_),
250 block_delay_buffer_(num_bands_,
251 frame_length_,
252 config_.delay.fixed_capture_delay_samples) {
peah21920892017-02-08 05:08:56 -0800253 RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
254
peahd0263542017-01-03 04:20:34 -0800255 std::unique_ptr<CascadedBiQuadFilter> render_highpass_filter;
256 if (use_highpass_filter) {
257 render_highpass_filter.reset(new CascadedBiQuadFilter(
258 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
259 : kHighPassFilterCoefficients_16kHz,
260 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
261 : kNumberOfHighPassBiQuads_16kHz));
262 capture_highpass_filter_.reset(new CascadedBiQuadFilter(
263 sample_rate_hz_ == 8000 ? kHighPassFilterCoefficients_8kHz
264 : kHighPassFilterCoefficients_16kHz,
265 sample_rate_hz_ == 8000 ? kNumberOfHighPassBiQuads_8kHz
266 : kNumberOfHighPassBiQuads_16kHz));
267 }
peahe0eae3c2016-12-14 01:16:23 -0800268
peahd0263542017-01-03 04:20:34 -0800269 render_writer_.reset(
270 new RenderWriter(data_dumper_.get(), &render_transfer_queue_,
271 std::move(render_highpass_filter), sample_rate_hz_,
272 frame_length_, num_bands_));
273
274 RTC_DCHECK_EQ(num_bands_, std::max(sample_rate_hz_, 16000) / 16000);
275 RTC_DCHECK_GE(kMaxNumBands, num_bands_);
peahe0eae3c2016-12-14 01:16:23 -0800276}
277
278EchoCanceller3::~EchoCanceller3() = default;
279
peahcf02cf12017-04-05 14:18:07 -0700280void EchoCanceller3::AnalyzeRender(AudioBuffer* render) {
peahd0263542017-01-03 04:20:34 -0800281 RTC_DCHECK_RUNS_SERIALIZED(&render_race_checker_);
282 RTC_DCHECK(render);
peahcf02cf12017-04-05 14:18:07 -0700283 data_dumper_->DumpRaw("aec3_call_order",
284 static_cast<int>(EchoCanceller3ApiCall::kRender));
285
peahd0263542017-01-03 04:20:34 -0800286 return render_writer_->Insert(render);
peahe0eae3c2016-12-14 01:16:23 -0800287}
288
peahd0263542017-01-03 04:20:34 -0800289void EchoCanceller3::AnalyzeCapture(AudioBuffer* capture) {
290 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
291 RTC_DCHECK(capture);
peahebe77782017-02-27 07:29:21 -0800292 data_dumper_->DumpWav("aec3_capture_analyze_input", capture->num_frames(),
peahd0263542017-01-03 04:20:34 -0800293 capture->channels_f()[0], sample_rate_hz_, 1);
294
295 saturated_microphone_signal_ = false;
296 for (size_t k = 0; k < capture->num_channels(); ++k) {
297 saturated_microphone_signal_ |=
298 DetectSaturation(rtc::ArrayView<const float>(capture->channels_f()[k],
299 capture->num_frames()));
300 if (saturated_microphone_signal_) {
301 break;
302 }
303 }
304}
peahe0eae3c2016-12-14 01:16:23 -0800305
peah69221db2017-01-27 03:28:19 -0800306void EchoCanceller3::ProcessCapture(AudioBuffer* capture, bool level_change) {
peahd0263542017-01-03 04:20:34 -0800307 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
308 RTC_DCHECK(capture);
peahe0eae3c2016-12-14 01:16:23 -0800309 RTC_DCHECK_EQ(1u, capture->num_channels());
peahd0263542017-01-03 04:20:34 -0800310 RTC_DCHECK_EQ(num_bands_, capture->num_bands());
peahe0eae3c2016-12-14 01:16:23 -0800311 RTC_DCHECK_EQ(frame_length_, capture->num_frames_per_band());
peahcf02cf12017-04-05 14:18:07 -0700312 data_dumper_->DumpRaw("aec3_call_order",
313 static_cast<int>(EchoCanceller3ApiCall::kCapture));
peahd0263542017-01-03 04:20:34 -0800314
Per Åhgren14f252a2018-11-27 18:02:56 +0100315 // Report capture call in the metrics and periodically update API call
316 // metrics.
317 api_call_metrics_.ReportCaptureCall();
318
Per Åhgren398689f2018-08-23 11:38:27 +0200319 // Optionally delay the capture signal.
320 if (config_.delay.fixed_capture_delay_samples > 0) {
321 block_delay_buffer_.DelaySignal(capture);
322 }
323
peahd0263542017-01-03 04:20:34 -0800324 rtc::ArrayView<float> capture_lower_band =
325 rtc::ArrayView<float>(&capture->split_bands_f(0)[0][0], frame_length_);
326
327 data_dumper_->DumpWav("aec3_capture_input", capture_lower_band,
328 LowestBandRate(sample_rate_hz_), 1);
329
peahcf02cf12017-04-05 14:18:07 -0700330 EmptyRenderQueue();
peahd0263542017-01-03 04:20:34 -0800331
332 if (capture_highpass_filter_) {
333 capture_highpass_filter_->Process(capture_lower_band);
334 }
335
peah69221db2017-01-27 03:28:19 -0800336 ProcessCaptureFrameContent(
337 capture, level_change, saturated_microphone_signal_, 0, &capture_blocker_,
338 &output_framer_, block_processor_.get(), &block_, &sub_frame_view_);
peahd0263542017-01-03 04:20:34 -0800339
340 if (sample_rate_hz_ != 8000) {
341 ProcessCaptureFrameContent(
peah69221db2017-01-27 03:28:19 -0800342 capture, level_change, saturated_microphone_signal_, 1,
peahd0263542017-01-03 04:20:34 -0800343 &capture_blocker_, &output_framer_, block_processor_.get(), &block_,
344 &sub_frame_view_);
345 }
346
347 ProcessRemainingCaptureFrameContent(
peah69221db2017-01-27 03:28:19 -0800348 level_change, saturated_microphone_signal_, &capture_blocker_,
peahd0263542017-01-03 04:20:34 -0800349 &output_framer_, block_processor_.get(), &block_);
350
351 data_dumper_->DumpWav("aec3_capture_output", frame_length_,
352 &capture->split_bands_f(0)[0][0],
353 LowestBandRate(sample_rate_hz_), 1);
peahe0eae3c2016-12-14 01:16:23 -0800354}
355
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100356EchoControl::Metrics EchoCanceller3::GetMetrics() const {
357 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
358 Metrics metrics;
359 block_processor_->GetMetrics(&metrics);
360 return metrics;
361}
362
Per Åhgrend0fa8202018-04-18 09:35:13 +0200363void EchoCanceller3::SetAudioBufferDelay(size_t delay_ms) {
364 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
365 block_processor_->SetAudioBufferDelay(delay_ms);
366}
367
peahcf02cf12017-04-05 14:18:07 -0700368void EchoCanceller3::EmptyRenderQueue() {
peahd0263542017-01-03 04:20:34 -0800369 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800370 bool frame_to_buffer =
371 render_transfer_queue_.Remove(&render_queue_output_frame_);
372 while (frame_to_buffer) {
Per Åhgren14f252a2018-11-27 18:02:56 +0100373 // Report render call in the metrics.
374 api_call_metrics_.ReportRenderCall();
375
peahcf02cf12017-04-05 14:18:07 -0700376 BufferRenderFrameContent(&render_queue_output_frame_, 0, &render_blocker_,
377 block_processor_.get(), &block_, &sub_frame_view_);
peahd0263542017-01-03 04:20:34 -0800378
379 if (sample_rate_hz_ != 8000) {
peahcf02cf12017-04-05 14:18:07 -0700380 BufferRenderFrameContent(&render_queue_output_frame_, 1, &render_blocker_,
381 block_processor_.get(), &block_,
382 &sub_frame_view_);
peahd0263542017-01-03 04:20:34 -0800383 }
384
peahcf02cf12017-04-05 14:18:07 -0700385 BufferRemainingRenderFrameContent(&render_blocker_, block_processor_.get(),
386 &block_);
peahd0263542017-01-03 04:20:34 -0800387
388 frame_to_buffer =
389 render_transfer_queue_.Remove(&render_queue_output_frame_);
390 }
peahd0263542017-01-03 04:20:34 -0800391}
peahe0eae3c2016-12-14 01:16:23 -0800392} // namespace webrtc