blob: 90413a84bee34b9ea21f6c4da05fe93a97bcb665 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "modules/audio_processing/include/audio_processing.h"
11
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000012#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000013#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080014
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000015#include <algorithm>
Oleh Prypin708eccc2019-03-27 09:38:52 +010016#include <cmath>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000017#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080018#include <memory>
Sam Zackrissone277bde2019-10-25 10:07:54 +020019#include <numeric>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000020#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000021
Sam Zackrisson6558fa52019-08-26 10:12:41 +020022#include "absl/flags/flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
24#include "common_audio/resampler/include/push_resampler.h"
25#include "common_audio/resampler/push_sinc_resampler.h"
26#include "common_audio/signal_processing/include/signal_processing_library.h"
27#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
28#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/common.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020030#include "modules/audio_processing/include/mock_audio_processing.h"
Per Åhgrencc73ed32020-04-26 23:56:17 +020031#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_processing/test/protobuf_utils.h"
33#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/arraysize.h"
35#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/gtest_prod_util.h"
38#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010039#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010040#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020043#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020044#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020045#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010046#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070050
51RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000053#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000054#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000056#endif
kwiberg77eab702016-09-28 17:42:01 -070057RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000058
Sam Zackrisson6558fa52019-08-26 10:12:41 +020059ABSL_FLAG(bool,
60 write_apm_ref_data,
61 false,
62 "Write ApmTest.Process results to file, instead of comparing results "
63 "to the existing reference data file.");
64
andrew@webrtc.org27c69802014-02-18 20:24:56 +000065namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000066namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000067
ekmeyerson60d9b332015-08-14 10:35:55 -070068// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
69// applicable.
70
mbonadei7c2c8432017-04-07 00:59:12 -070071const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070072const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000073
aluebseb3603b2016-04-20 15:27:58 -070074#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
75// Android doesn't support 48kHz.
76const int kProcessSampleRates[] = {8000, 16000, 32000};
77#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070078const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070079#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000080
ekmeyerson60d9b332015-08-14 10:35:55 -070081enum StreamDirection { kForward = 0, kReverse };
82
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000083void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
Jonas Olssona4d87372019-07-05 19:08:33 +020084 ChannelBuffer<int16_t> cb_int(cb->num_frames(), cb->num_channels());
85 Deinterleave(int_data, cb->num_frames(), cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000086 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080087 for (size_t i = 0; i < cb->num_channels(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +020088 S16ToFloat(cb_int.channels()[i], cb->num_frames(), cb->channels()[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000089 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000090}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000091
Per Åhgren2507f8c2020-03-19 12:33:29 +010092void ConvertToFloat(const Int16FrameData& frame, ChannelBuffer<float>* cb) {
93 ConvertToFloat(frame.data.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094}
95
andrew@webrtc.org103657b2014-04-24 18:28:56 +000096// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080097size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098 switch (layout) {
99 case AudioProcessing::kMono:
100 return 1;
101 case AudioProcessing::kMonoAndKeyboard:
102 case AudioProcessing::kStereo:
103 return 2;
104 case AudioProcessing::kStereoAndKeyboard:
105 return 3;
106 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700107 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800108 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000109}
110
Jonas Olssona4d87372019-07-05 19:08:33 +0200111void MixStereoToMono(const float* stereo,
112 float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800113 size_t samples_per_channel) {
114 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000116}
117
Jonas Olssona4d87372019-07-05 19:08:33 +0200118void MixStereoToMono(const int16_t* stereo,
119 int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800120 size_t samples_per_channel) {
121 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
123}
124
pkasting25702cb2016-01-08 13:50:27 -0800125void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
126 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000127 stereo[i * 2 + 1] = stereo[i * 2];
128 }
129}
130
yujo36b1a5f2017-06-12 12:45:32 -0700131void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800132 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000133 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
134 }
135}
136
Per Åhgren2507f8c2020-03-19 12:33:29 +0100137void SetFrameTo(Int16FrameData* frame, int16_t value) {
138 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700139 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100140 frame->data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000141 }
142}
143
Per Åhgren2507f8c2020-03-19 12:33:29 +0100144void SetFrameTo(Int16FrameData* frame, int16_t left, int16_t right) {
145 ASSERT_EQ(2u, frame->num_channels);
146 for (size_t i = 0; i < frame->samples_per_channel * 2; i += 2) {
147 frame->data[i] = left;
148 frame->data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000149 }
150}
151
Per Åhgren2507f8c2020-03-19 12:33:29 +0100152void ScaleFrame(Int16FrameData* frame, float scale) {
153 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700154 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100155 frame->data[i] = FloatS16ToS16(frame->data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000156 }
157}
158
Per Åhgren2507f8c2020-03-19 12:33:29 +0100159bool FrameDataAreEqual(const Int16FrameData& frame1,
160 const Int16FrameData& frame2) {
161 if (frame1.samples_per_channel != frame2.samples_per_channel) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000162 return false;
163 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100164 if (frame1.num_channels != frame2.num_channels) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165 return false;
166 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100167 if (memcmp(
168 frame1.data.data(), frame2.data.data(),
169 frame1.samples_per_channel * frame1.num_channels * sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
172 return true;
173}
174
Per Åhgren2507f8c2020-03-19 12:33:29 +0100175rtc::ArrayView<int16_t> GetMutableFrameData(Int16FrameData* frame) {
176 int16_t* ptr = frame->data.data();
177 const size_t len = frame->samples_per_channel * frame->num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200178 return rtc::ArrayView<int16_t>(ptr, len);
179}
180
Per Åhgren2507f8c2020-03-19 12:33:29 +0100181rtc::ArrayView<const int16_t> GetFrameData(const Int16FrameData& frame) {
182 const int16_t* ptr = frame.data.data();
183 const size_t len = frame.samples_per_channel * frame.num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200184 return rtc::ArrayView<const int16_t>(ptr, len);
185}
186
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000187void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200188 AudioProcessing::Config apm_config = ap->GetConfig();
189 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000190#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200191 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100192
193 apm_config.gain_controller1.enabled = true;
194 apm_config.gain_controller1.mode =
195 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000196#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200197 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000198
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100199 apm_config.gain_controller1.enabled = true;
200 apm_config.gain_controller1.mode =
201 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
202 apm_config.gain_controller1.analog_level_minimum = 0;
203 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000204#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000205
saza0bad15f2019-10-16 11:46:11 +0200206 apm_config.noise_suppression.enabled = true;
207
peah8271d042016-11-22 07:24:52 -0800208 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100209 apm_config.level_estimation.enabled = true;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200210 apm_config.voice_detection.enabled = true;
Per Åhgrenc0424252019-12-10 13:04:15 +0100211 apm_config.pipeline.maximum_internal_processing_rate = 48000;
peah8271d042016-11-22 07:24:52 -0800212 ap->ApplyConfig(apm_config);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000213}
214
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000215// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000216template <class T>
217T AbsValue(T a) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200218 return a > 0 ? a : -a;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000219}
220
Per Åhgren2507f8c2020-03-19 12:33:29 +0100221int16_t MaxAudioFrame(const Int16FrameData& frame) {
222 const size_t length = frame.samples_per_channel * frame.num_channels;
223 int16_t max_data = AbsValue(frame.data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800224 for (size_t i = 1; i < length; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100225 max_data = std::max(max_data, AbsValue(frame.data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000226 }
227
228 return max_data;
229}
230
Alex Loiko890988c2017-08-31 10:25:48 +0200231void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700232 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000233 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234 ASSERT_TRUE(file != NULL);
235
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100236 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800238 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000239 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242 ASSERT_EQ(static_cast<size_t>(size),
Jonas Olssona4d87372019-07-05 19:08:33 +0200243 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244 fclose(file);
245}
246
Alex Loiko890988c2017-08-31 10:25:48 +0200247std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200248 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000249 // Resource files are all stereo.
250 ss << name << sample_rate_hz / 1000 << "_stereo";
251 return test::ResourcePath(ss.str(), "pcm");
252}
253
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000254// Temporary filenames unique to this process. Used to be able to run these
255// tests in parallel as each process needs to be running in isolation they can't
256// have competing filenames.
257std::map<std::string, std::string> temp_filenames;
258
Alex Loiko890988c2017-08-31 10:25:48 +0200259std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000260 int input_rate,
261 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700262 int reverse_input_rate,
263 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800264 size_t num_input_channels,
265 size_t num_output_channels,
266 size_t num_reverse_input_channels,
267 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700268 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200269 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700270 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
271 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000272 if (num_output_channels == 1) {
273 ss << "mono";
274 } else if (num_output_channels == 2) {
275 ss << "stereo";
276 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700277 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000278 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 ss << output_rate / 1000;
280 if (num_reverse_output_channels == 1) {
281 ss << "_rmono";
282 } else if (num_reverse_output_channels == 2) {
283 ss << "_rstereo";
284 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700285 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700286 }
287 ss << reverse_output_rate / 1000;
288 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000290 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700291 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000292 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
293 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000294}
295
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000296void ClearTempFiles() {
297 for (auto& kv : temp_filenames)
298 remove(kv.second.c_str());
299}
300
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200301// Only remove "out" files. Keep "ref" files.
302void ClearTempOutFiles() {
303 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
304 const std::string& filename = it->first;
305 if (filename.substr(0, 3).compare("out") == 0) {
306 remove(it->second.c_str());
307 temp_filenames.erase(it++);
308 } else {
309 it++;
310 }
311 }
312}
313
Alex Loiko890988c2017-08-31 10:25:48 +0200314void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000315 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000316 ASSERT_TRUE(file != NULL);
317 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000318 fclose(file);
319}
320
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000321// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
322// stereo) file, converts to deinterleaved float (optionally downmixing) and
323// returns the result in |cb|. Returns false if the file ended (or on error) and
324// true otherwise.
325//
326// |int_data| and |float_data| are just temporary space that must be
327// sufficiently large to hold the 10 ms chunk.
Jonas Olssona4d87372019-07-05 19:08:33 +0200328bool ReadChunk(FILE* file,
329 int16_t* int_data,
330 float* float_data,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000331 ChannelBuffer<float>* cb) {
332 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000333 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000334 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
335 if (read_count != frame_size) {
336 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700337 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000338 return false; // This is expected.
339 }
340
341 S16ToFloat(int_data, frame_size, float_data);
342 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000343 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000344 } else {
Jonas Olssona4d87372019-07-05 19:08:33 +0200345 Deinterleave(float_data, cb->num_frames(), 2, cb->channels());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000346 }
347
348 return true;
349}
350
niklase@google.com470e71d2011-07-07 08:21:25 +0000351class ApmTest : public ::testing::Test {
352 protected:
353 ApmTest();
354 virtual void SetUp();
355 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000356
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200357 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000358
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200359 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000360
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000361 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200362 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363
364 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000365 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000366 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800367 size_t num_input_channels,
368 size_t num_output_channels,
369 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000370 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000371 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000372 void EnableAllComponents();
Per Åhgren2507f8c2020-03-19 12:33:29 +0100373 bool ReadFrame(FILE* file, Int16FrameData* frame);
374 bool ReadFrame(FILE* file, Int16FrameData* frame, ChannelBuffer<float>* cb);
375 void ReadFrameWithRewind(FILE* file, Int16FrameData* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200376 void ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100377 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000378 ChannelBuffer<float>* cb);
Jonas Olssona4d87372019-07-05 19:08:33 +0200379 void ProcessDelayVerificationTest(int delay_ms,
380 int system_delay_ms,
381 int delay_min,
382 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700383 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800384 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800386 void TestChangingForwardChannels(size_t num_in_channels,
387 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700388 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800389 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000391 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
392 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000394 int ProcessStreamChooser(Format format);
395 int AnalyzeReverseStreamChooser(Format format);
396 void ProcessDebugDump(const std::string& in_filename,
397 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800398 Format format,
399 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000400 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000401
402 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800404 std::unique_ptr<AudioProcessing> apm_;
Per Åhgren2507f8c2020-03-19 12:33:29 +0100405 Int16FrameData frame_;
406 Int16FrameData revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800407 std::unique_ptr<ChannelBuffer<float> > float_cb_;
408 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800410 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 FILE* far_file_;
412 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000413 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414};
415
416ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000417 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000418#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200419 ref_filename_(
420 test::ResourcePath("audio_processing/output_data_fixed", "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000421#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200422 ref_filename_(
423 test::ResourcePath("audio_processing/output_data_float", "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000424#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000425 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000426 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000427 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000428 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000429 out_file_(NULL) {
Per Åhgrencc73ed32020-04-26 23:56:17 +0200430 apm_.reset(AudioProcessingBuilderForTesting().Create());
Per Åhgrenc0424252019-12-10 13:04:15 +0100431 AudioProcessing::Config apm_config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100432 apm_config.gain_controller1.analog_gain_controller.enabled = false;
Per Åhgrenc0424252019-12-10 13:04:15 +0100433 apm_config.pipeline.maximum_internal_processing_rate = 48000;
434 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000435}
niklase@google.com470e71d2011-07-07 08:21:25 +0000436
437void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000438 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
443void ApmTest::TearDown() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 if (far_file_) {
445 ASSERT_EQ(0, fclose(far_file_));
446 }
447 far_file_ = NULL;
448
449 if (near_file_) {
450 ASSERT_EQ(0, fclose(near_file_));
451 }
452 near_file_ = NULL;
453
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000454 if (out_file_) {
455 ASSERT_EQ(0, fclose(out_file_));
456 }
457 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000460void ApmTest::Init(AudioProcessing* ap) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200461 ASSERT_EQ(
462 kNoErr,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100463 ap->Initialize({{{frame_.sample_rate_hz, frame_.num_channels},
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200464 {output_sample_rate_hz_, num_output_channels_},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100465 {revframe_.sample_rate_hz, revframe_.num_channels},
466 {revframe_.sample_rate_hz, revframe_.num_channels}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000467}
468
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000469void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000471 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800472 size_t num_input_channels,
473 size_t num_output_channels,
474 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000475 bool open_output_file) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200476 SetContainerFormat(sample_rate_hz, num_input_channels, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000478 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000479
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200480 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, &revframe_,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000482 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000483
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000484 if (far_file_) {
485 ASSERT_EQ(0, fclose(far_file_));
486 }
487 std::string filename = ResourceFilePath("far", sample_rate_hz);
488 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200489 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490
491 if (near_file_) {
492 ASSERT_EQ(0, fclose(near_file_));
493 }
494 filename = ResourceFilePath("near", sample_rate_hz);
495 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200496 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000497
498 if (open_output_file) {
499 if (out_file_) {
500 ASSERT_EQ(0, fclose(out_file_));
501 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700502 filename = OutputFilePath(
503 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
504 reverse_sample_rate_hz, num_input_channels, num_output_channels,
505 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000506 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200507 ASSERT_TRUE(out_file_ != NULL)
508 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000509 }
510}
511
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000512void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000513 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000514}
515
Jonas Olssona4d87372019-07-05 19:08:33 +0200516bool ApmTest::ReadFrame(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100517 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000518 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000519 // The files always contain stereo audio.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100520 size_t frame_size = frame->samples_per_channel * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200521 size_t read_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100522 fread(frame->data.data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000523 if (read_count != frame_size) {
524 // Check that the file really ended.
525 EXPECT_NE(0, feof(file));
526 return false; // This is expected.
527 }
528
Per Åhgren2507f8c2020-03-19 12:33:29 +0100529 if (frame->num_channels == 1) {
530 MixStereoToMono(frame->data.data(), frame->data.data(),
531 frame->samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000532 }
533
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000534 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000535 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000536 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000537 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000538}
539
Per Åhgren2507f8c2020-03-19 12:33:29 +0100540bool ApmTest::ReadFrame(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000541 return ReadFrame(file, frame, NULL);
542}
543
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000544// If the end of the file has been reached, rewind it and attempt to read the
545// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200546void ApmTest::ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100547 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548 ChannelBuffer<float>* cb) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200549 if (!ReadFrame(near_file_, &frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000550 rewind(near_file_);
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200551 ASSERT_TRUE(ReadFrame(near_file_, &frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000552 }
553}
554
Per Åhgren2507f8c2020-03-19 12:33:29 +0100555void ApmTest::ReadFrameWithRewind(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556 ReadFrameWithRewind(file, frame, NULL);
557}
558
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000559int ApmTest::ProcessStreamChooser(Format format) {
560 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100561 return apm_->ProcessStream(
562 frame_.data.data(),
563 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
564 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100565 frame_.data.data());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000566 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200567 return apm_->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100568 float_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100569 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100570 StreamConfig(output_sample_rate_hz_, num_output_channels_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200571 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572}
573
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000574int ApmTest::AnalyzeReverseStreamChooser(Format format) {
575 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100576 return apm_->ProcessReverseStream(
577 revframe_.data.data(),
578 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
579 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
580 revframe_.data.data());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582 return apm_->AnalyzeReverseStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100583 revfloat_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100584 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000585}
586
Jonas Olssona4d87372019-07-05 19:08:33 +0200587void ApmTest::ProcessDelayVerificationTest(int delay_ms,
588 int system_delay_ms,
589 int delay_min,
590 int delay_max) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000591 // The |revframe_| and |frame_| should include the proper frame information,
592 // hence can be used for extracting information.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100593 Int16FrameData tmp_frame;
594 std::queue<Int16FrameData*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000595 bool causal = true;
596
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200597 tmp_frame.CopyFrom(revframe_);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000598 SetFrameTo(&tmp_frame, 0);
599
600 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
601 // Initialize the |frame_queue| with empty frames.
602 int frame_delay = delay_ms / 10;
603 while (frame_delay < 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100604 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000605 frame->CopyFrom(tmp_frame);
606 frame_queue.push(frame);
607 frame_delay++;
608 causal = false;
609 }
610 while (frame_delay > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100611 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000612 frame->CopyFrom(tmp_frame);
613 frame_queue.push(frame);
614 frame_delay--;
615 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000616 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
617 // need enough frames with audio to have reliable estimates, but as few as
618 // possible to keep processing time down. 4.5 seconds seemed to be a good
619 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 for (int frame_count = 0; frame_count < 450; ++frame_count) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100621 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000622 frame->CopyFrom(tmp_frame);
623 // Use the near end recording, since that has more speech in it.
624 ASSERT_TRUE(ReadFrame(near_file_, frame));
625 frame_queue.push(frame);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100626 Int16FrameData* reverse_frame = frame;
627 Int16FrameData* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000628 if (!causal) {
629 reverse_frame = frame_queue.front();
630 // When we call ProcessStream() the frame is modified, so we can't use the
631 // pointer directly when things are non-causal. Use an intermediate frame
632 // and copy the data.
633 process_frame = &tmp_frame;
634 process_frame->CopyFrom(*frame);
635 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100636 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(
637 reverse_frame->data.data(),
638 StreamConfig(reverse_frame->sample_rate_hz,
639 reverse_frame->num_channels),
640 StreamConfig(reverse_frame->sample_rate_hz,
641 reverse_frame->num_channels),
642 reverse_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000643 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100644 EXPECT_EQ(apm_->kNoError,
645 apm_->ProcessStream(process_frame->data.data(),
646 StreamConfig(process_frame->sample_rate_hz,
647 process_frame->num_channels),
648 StreamConfig(process_frame->sample_rate_hz,
649 process_frame->num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100650 process_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000651 frame = frame_queue.front();
652 frame_queue.pop();
653 delete frame;
654
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000655 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 // Discard the first delay metrics to avoid convergence effects.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100657 static_cast<void>(apm_->GetStatistics());
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000658 }
659 }
660
661 rewind(near_file_);
662 while (!frame_queue.empty()) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100663 Int16FrameData* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 frame_queue.pop();
665 delete frame;
666 }
667 // Calculate expected delay estimate and acceptable regions. Further,
668 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700669 const size_t samples_per_ms =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100670 rtc::SafeMin<size_t>(16u, frame_.samples_per_channel / 10);
kwiberg07038562017-06-12 11:40:47 -0700671 const int expected_median =
672 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
673 const int expected_median_high = rtc::SafeClamp<int>(
674 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700675 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700676 const int expected_median_low = rtc::SafeClamp<int>(
677 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000679 // Verify delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100680 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200681 ASSERT_TRUE(stats.delay_median_ms.has_value());
682 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 EXPECT_GE(expected_median_high, median);
684 EXPECT_LE(expected_median_low, median);
685}
686
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000687void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000688 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000689 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000691 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200692 AudioProcessing::Config apm_config = apm_->GetConfig();
693 apm_config.gain_controller1.enabled = true;
694 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200695 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000696
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000697 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200698 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200700 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000702 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200703 apm_config.echo_canceller.enabled = true;
704 apm_config.echo_canceller.mobile_mode = false;
705 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000706 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200707 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200708 apm_config.gain_controller1.enabled = false;
709 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000710
711 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100713 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000714
715 // Resets after successful ProcessStream().
716 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000717 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100718 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719
720 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200721 apm_config.gain_controller1.enabled = true;
722 apm_->ApplyConfig(apm_config);
723 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100724 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200725 apm_config.gain_controller1.enabled = false;
726 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200729 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100730 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000731
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000732 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200734 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736}
737
738TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000740}
741
742TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000743 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
Michael Graczyk86c6d332015-07-23 11:41:39 -0700746void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800747 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700748 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100749 frame_.num_channels = num_channels;
750
751 EXPECT_EQ(expected_return,
752 apm_->ProcessStream(
753 frame_.data.data(),
754 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
755 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100756 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100757 EXPECT_EQ(expected_return,
758 apm_->ProcessReverseStream(
759 frame_.data.data(),
760 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
761 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
762 frame_.data.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000763}
764
Michael Graczyk86c6d332015-07-23 11:41:39 -0700765void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800766 size_t num_in_channels,
767 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700768 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100769 const StreamConfig input_stream = {frame_.sample_rate_hz, num_in_channels};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700770 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
771
772 EXPECT_EQ(expected_return,
773 apm_->ProcessStream(float_cb_->channels(), input_stream,
774 output_stream, float_cb_->channels()));
775}
776
777void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800778 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700779 AudioProcessing::Error expected_return) {
780 const ProcessingConfig processing_config = {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100781 {{frame_.sample_rate_hz, apm_->num_input_channels()},
ekmeyerson60d9b332015-08-14 10:35:55 -0700782 {output_sample_rate_hz_, apm_->num_output_channels()},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100783 {frame_.sample_rate_hz, num_rev_channels},
784 {frame_.sample_rate_hz, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700785
ekmeyerson60d9b332015-08-14 10:35:55 -0700786 EXPECT_EQ(
787 expected_return,
788 apm_->ProcessReverseStream(
789 float_cb_->channels(), processing_config.reverse_input_stream(),
790 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700791}
792
793TEST_F(ApmTest, ChannelsInt16Interface) {
794 // Testing number of invalid and valid channels.
795 Init(16000, 16000, 16000, 4, 4, 4, false);
796
797 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
798
Peter Kasting69558702016-01-12 16:26:35 -0800799 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700800 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
803}
804
Michael Graczyk86c6d332015-07-23 11:41:39 -0700805TEST_F(ApmTest, Channels) {
806 // Testing number of invalid and valid channels.
807 Init(16000, 16000, 16000, 4, 4, 4, false);
808
809 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
810 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
811
Peter Kasting69558702016-01-12 16:26:35 -0800812 for (size_t i = 1; i < 4; ++i) {
813 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814 // Output channels much be one or match input channels.
815 if (j == 1 || i == j) {
816 TestChangingForwardChannels(i, j, kNoErr);
817 TestChangingReverseChannels(i, kNoErr);
818
819 EXPECT_EQ(i, apm_->num_input_channels());
820 EXPECT_EQ(j, apm_->num_output_channels());
821 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800822 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823 } else {
824 TestChangingForwardChannels(i, j,
825 AudioProcessing::kBadNumberChannelsError);
826 }
827 }
828 }
829}
830
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000831TEST_F(ApmTest, SampleRatesInt) {
Sam Zackrisson12e319a2020-01-03 14:54:20 +0100832 // Testing some valid sample rates.
833 for (int sample_rate : {8000, 12000, 16000, 32000, 44100, 48000, 96000}) {
834 SetContainerFormat(sample_rate, 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000835 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000836 }
837}
838
Sam Zackrissone277bde2019-10-25 10:07:54 +0200839// This test repeatedly reconfigures the pre-amplifier in APM, processes a
840// number of frames, and checks that output signal has the right level.
841TEST_F(ApmTest, PreAmplifier) {
842 // Fill the audio frame with a sawtooth pattern.
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200843 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100844 const size_t samples_per_channel = frame_.samples_per_channel;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200845 for (size_t i = 0; i < samples_per_channel; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100846 for (size_t ch = 0; ch < frame_.num_channels; ++ch) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200847 frame_data[i + ch * samples_per_channel] = 10000 * ((i % 3) - 1);
848 }
849 }
850 // Cache the frame in tmp_frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100851 Int16FrameData tmp_frame;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200852 tmp_frame.CopyFrom(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200853
Per Åhgren2507f8c2020-03-19 12:33:29 +0100854 auto compute_power = [](const Int16FrameData& frame) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200855 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
856 return std::accumulate(data.begin(), data.end(), 0.0f,
857 [](float a, float b) { return a + b * b; }) /
858 data.size() / 32768 / 32768;
859 };
860
861 const float input_power = compute_power(tmp_frame);
862 // Double-check that the input data is large compared to the error kEpsilon.
863 constexpr float kEpsilon = 1e-4f;
864 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
865
866 // 1. Enable pre-amp with 0 dB gain.
867 AudioProcessing::Config config = apm_->GetConfig();
868 config.pre_amplifier.enabled = true;
869 config.pre_amplifier.fixed_gain_factor = 1.0f;
870 apm_->ApplyConfig(config);
871
872 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200873 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200874 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
875 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200876 float output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200877 EXPECT_NEAR(output_power, input_power, kEpsilon);
878 config = apm_->GetConfig();
879 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.0f);
880
881 // 2. Change pre-amp gain via ApplyConfig.
882 config.pre_amplifier.fixed_gain_factor = 2.0f;
883 apm_->ApplyConfig(config);
884
885 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200886 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200887 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
888 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200889 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200890 EXPECT_NEAR(output_power, 4 * input_power, kEpsilon);
891 config = apm_->GetConfig();
892 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 2.0f);
893
894 // 3. Change pre-amp gain via a RuntimeSetting.
895 apm_->SetRuntimeSetting(
896 AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.5f));
897
898 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200899 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200900 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
901 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200902 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200903 EXPECT_NEAR(output_power, 2.25 * input_power, kEpsilon);
904 config = apm_->GetConfig();
905 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.5f);
906}
907
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000908TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200909 AudioProcessing::Config config = apm_->GetConfig();
910 config.gain_controller1.enabled = false;
911 apm_->ApplyConfig(config);
912 config.gain_controller1.enabled = true;
913 apm_->ApplyConfig(config);
914
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +0200916 for (auto mode :
917 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
918 AudioProcessing::Config::GainController1::kFixedDigital,
919 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
920 config.gain_controller1.mode = mode;
921 apm_->ApplyConfig(config);
922 apm_->set_stream_analog_level(100);
923 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000925
Sam Zackrisson41478c72019-10-15 10:10:26 +0200926 // Testing target levels
927 for (int target_level_dbfs : {0, 15, 31}) {
928 config.gain_controller1.target_level_dbfs = target_level_dbfs;
929 apm_->ApplyConfig(config);
930 apm_->set_stream_analog_level(100);
931 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000932 }
933
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100934 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +0200935 for (int compression_gain_db : {0, 10, 90}) {
936 config.gain_controller1.compression_gain_db = compression_gain_db;
937 apm_->ApplyConfig(config);
938 apm_->set_stream_analog_level(100);
939 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000940 }
941
942 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +0200943 for (bool enable : {false, true}) {
944 config.gain_controller1.enable_limiter = enable;
945 apm_->ApplyConfig(config);
946 apm_->set_stream_analog_level(100);
947 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
948 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000949
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100950 // Testing level limits
Sam Zackrisson41478c72019-10-15 10:10:26 +0200951 std::array<int, 4> kMinLevels = {0, 0, 255, 65000};
952 std::array<int, 4> kMaxLevels = {255, 1024, 65535, 65535};
953 for (size_t i = 0; i < kMinLevels.size(); ++i) {
954 int min_level = kMinLevels[i];
955 int max_level = kMaxLevels[i];
956 config.gain_controller1.analog_level_minimum = min_level;
957 config.gain_controller1.analog_level_maximum = max_level;
958 apm_->ApplyConfig(config);
959 apm_->set_stream_analog_level((min_level + max_level) / 2);
960 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000961 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000962}
963
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100964#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
965TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200966 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100967 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200968 config.gain_controller1.target_level_dbfs = -1;
969 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100970}
971
972TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200973 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100974 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200975 config.gain_controller1.target_level_dbfs = 32;
976 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100977}
978
979TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200980 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100981 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200982 config.gain_controller1.compression_gain_db = -1;
983 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100984}
985
986TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200987 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100988 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200989 config.gain_controller1.compression_gain_db = 91;
990 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100991}
992
993TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200994 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100995 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200996 config.gain_controller1.analog_level_minimum = -1;
997 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100998}
999
1000TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001001 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001002 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001003 config.gain_controller1.analog_level_maximum = 65536;
1004 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001005}
1006
1007TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001008 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001009 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001010 config.gain_controller1.analog_level_minimum = 512;
1011 config.gain_controller1.analog_level_maximum = 255;
1012 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001013}
1014
1015TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001016 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001017 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001018 config.gain_controller1.analog_level_minimum = 255;
1019 config.gain_controller1.analog_level_maximum = 512;
1020 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001021 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
1022}
1023
1024TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001025 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001026 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001027 config.gain_controller1.analog_level_minimum = 255;
1028 config.gain_controller1.analog_level_maximum = 512;
1029 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001030 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
1031}
1032#endif
1033
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001034void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001035 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001036 auto config = apm_->GetConfig();
1037 config.gain_controller1.enabled = true;
1038 config.gain_controller1.mode =
1039 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1040 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001041
1042 int out_analog_level = 0;
1043 for (int i = 0; i < 2000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001044 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001045 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001046 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001047
1048 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +02001049 apm_->set_stream_analog_level(100);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001050 EXPECT_EQ(apm_->kNoError,
1051 apm_->ProcessStream(
1052 frame_.data.data(),
1053 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1054 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001055 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001056 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001057 }
1058
1059 // Ensure the AGC is still able to reach the maximum.
1060 EXPECT_EQ(255, out_analog_level);
1061}
1062
1063// Verifies that despite volume slider quantization, the AGC can continue to
1064// increase its volume.
1065TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001066 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001067 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1068 }
1069}
1070
1071void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001072 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001073 auto config = apm_->GetConfig();
1074 config.gain_controller1.enabled = true;
1075 config.gain_controller1.mode =
1076 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1077 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001078
1079 int out_analog_level = 100;
1080 for (int i = 0; i < 1000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001081 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001082 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001083 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001084
Sam Zackrisson41478c72019-10-15 10:10:26 +02001085 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001086 EXPECT_EQ(apm_->kNoError,
1087 apm_->ProcessStream(
1088 frame_.data.data(),
1089 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1090 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001091 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001092 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001093 }
1094
1095 // Ensure the volume was raised.
1096 EXPECT_GT(out_analog_level, 100);
1097 int highest_level_reached = out_analog_level;
1098 // Simulate a user manual volume change.
1099 out_analog_level = 100;
1100
1101 for (int i = 0; i < 300; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001102 ReadFrameWithRewind(near_file_, &frame_);
1103 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001104
Sam Zackrisson41478c72019-10-15 10:10:26 +02001105 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001106 EXPECT_EQ(apm_->kNoError,
1107 apm_->ProcessStream(
1108 frame_.data.data(),
1109 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1110 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001111 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001112 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001113 // Check that AGC respected the manually adjusted volume.
1114 EXPECT_LT(out_analog_level, highest_level_reached);
1115 }
1116 // Check that the volume was still raised.
1117 EXPECT_GT(out_analog_level, 100);
1118}
1119
1120TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001121 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001122 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1123 }
1124}
1125
niklase@google.com470e71d2011-07-07 08:21:25 +00001126TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001127 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001128 AudioProcessing::Config apm_config;
1129 apm_config.high_pass_filter.enabled = true;
1130 apm_->ApplyConfig(apm_config);
1131 apm_config.high_pass_filter.enabled = false;
1132 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001133}
1134
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001135TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001136 AudioProcessing::Config config = apm_->GetConfig();
1137 EXPECT_FALSE(config.echo_canceller.enabled);
1138 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001139 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001140 EXPECT_FALSE(config.level_estimation.enabled);
saza0bad15f2019-10-16 11:46:11 +02001141 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001142 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001143}
1144
1145TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001146 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001147 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001148 SetFrameTo(&frame_, 1000, 2000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001149 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001150 frame_copy.CopyFrom(frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001151 for (int j = 0; j < 1000; j++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001152 EXPECT_EQ(apm_->kNoError,
1153 apm_->ProcessStream(
1154 frame_.data.data(),
1155 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1156 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001157 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001158 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001159 EXPECT_EQ(apm_->kNoError,
1160 apm_->ProcessReverseStream(
1161 frame_.data.data(),
1162 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1163 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1164 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001165 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001166 }
1167 }
1168}
1169
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001170TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1171 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001172 const size_t kSamples = 160;
1173 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001174 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001175 float dest[kSamples] = {};
1176
1177 auto src_channels = &src[0];
1178 auto dest_channels = &dest[0];
1179
Per Åhgrencc73ed32020-04-26 23:56:17 +02001180 apm_.reset(AudioProcessingBuilderForTesting().Create());
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001181 EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
1182 StreamConfig(sample_rate, 1),
1183 &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001184
1185 for (size_t i = 0; i < kSamples; ++i) {
1186 EXPECT_EQ(src[i], dest[i]);
1187 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001188
1189 // Same for ProcessReverseStream.
1190 float rev_dest[kSamples] = {};
1191 auto rev_dest_channels = &rev_dest[0];
1192
1193 StreamConfig input_stream = {sample_rate, 1};
1194 StreamConfig output_stream = {sample_rate, 1};
1195 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1196 output_stream, &rev_dest_channels));
1197
1198 for (size_t i = 0; i < kSamples; ++i) {
1199 EXPECT_EQ(src[i], rev_dest[i]);
1200 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001201}
1202
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001203TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1204 EnableAllComponents();
1205
pkasting25702cb2016-01-08 13:50:27 -08001206 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001207 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1208 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001209 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001210 ASSERT_EQ(0, feof(far_file_));
1211 ASSERT_EQ(0, feof(near_file_));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001212 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001213 CopyLeftToRightChannel(revframe_.data.data(),
1214 revframe_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001215
Per Åhgren2507f8c2020-03-19 12:33:29 +01001216 ASSERT_EQ(
1217 kNoErr,
1218 apm_->ProcessReverseStream(
1219 revframe_.data.data(),
1220 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1221 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1222 revframe_.data.data()));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001223
Per Åhgren2507f8c2020-03-19 12:33:29 +01001224 CopyLeftToRightChannel(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001225
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001226 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001227 apm_->set_stream_analog_level(analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001228 ASSERT_EQ(kNoErr,
1229 apm_->ProcessStream(
1230 frame_.data.data(),
1231 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1232 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001233 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001234 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001235
Per Åhgren2507f8c2020-03-19 12:33:29 +01001236 VerifyChannelsAreEqual(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001237 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001238 rewind(far_file_);
1239 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001240 }
1241}
1242
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001243TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001244 // Verify the filter is not active through undistorted audio when:
1245 // 1. No components are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001246 SetFrameTo(&frame_, 1000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001247 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001248 frame_copy.CopyFrom(frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001249 EXPECT_EQ(apm_->kNoError,
1250 apm_->ProcessStream(
1251 frame_.data.data(),
1252 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1253 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001254 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001255 EXPECT_EQ(apm_->kNoError,
1256 apm_->ProcessStream(
1257 frame_.data.data(),
1258 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1259 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001260 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001261 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001262
1263 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001264 auto apm_config = apm_->GetConfig();
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001265 SetFrameTo(&frame_, 1000);
1266 frame_copy.CopyFrom(frame_);
saza6787f232019-10-11 19:31:07 +02001267 apm_config.level_estimation.enabled = true;
1268 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001269 EXPECT_EQ(apm_->kNoError,
1270 apm_->ProcessStream(
1271 frame_.data.data(),
1272 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1273 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001274 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001275 EXPECT_EQ(apm_->kNoError,
1276 apm_->ProcessStream(
1277 frame_.data.data(),
1278 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1279 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001280 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001281 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001282 apm_config.level_estimation.enabled = false;
1283 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001284
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001285 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001286 SetFrameTo(&frame_, 1000);
1287 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001288 apm_config.voice_detection.enabled = true;
1289 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001290 EXPECT_EQ(apm_->kNoError,
1291 apm_->ProcessStream(
1292 frame_.data.data(),
1293 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1294 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001295 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001296 EXPECT_EQ(apm_->kNoError,
1297 apm_->ProcessStream(
1298 frame_.data.data(),
1299 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1300 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001301 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001302 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001303 apm_config.voice_detection.enabled = false;
1304 apm_->ApplyConfig(apm_config);
1305
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001306 // 4. Both the VAD and the level estimator are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001307 SetFrameTo(&frame_, 1000);
1308 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001309 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001310 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001311 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001312 EXPECT_EQ(apm_->kNoError,
1313 apm_->ProcessStream(
1314 frame_.data.data(),
1315 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1316 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001317 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001318 EXPECT_EQ(apm_->kNoError,
1319 apm_->ProcessStream(
1320 frame_.data.data(),
1321 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1322 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001323 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001324 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001325 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001326 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001327 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001328
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001329 // Check the test is valid. We should have distortion from the filter
1330 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001331 apm_config.echo_canceller.enabled = true;
1332 apm_config.echo_canceller.mobile_mode = false;
1333 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001334 frame_.samples_per_channel = 320;
1335 frame_.num_channels = 2;
1336 frame_.sample_rate_hz = 32000;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001337 SetFrameTo(&frame_, 1000);
1338 frame_copy.CopyFrom(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001339 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001340 EXPECT_EQ(apm_->kNoError,
1341 apm_->ProcessStream(
1342 frame_.data.data(),
1343 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1344 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001345 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001346 EXPECT_FALSE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001347}
1348
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001349#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1350void ApmTest::ProcessDebugDump(const std::string& in_filename,
1351 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001352 Format format,
1353 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001354 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001355 FILE* in_file = fopen(in_filename.c_str(), "rb");
1356 ASSERT_TRUE(in_file != NULL);
1357 audioproc::Event event_msg;
1358 bool first_init = true;
1359
1360 while (ReadMessageFromFile(in_file, &event_msg)) {
1361 if (event_msg.type() == audioproc::Event::INIT) {
1362 const audioproc::Init msg = event_msg.init();
1363 int reverse_sample_rate = msg.sample_rate();
1364 if (msg.has_reverse_sample_rate()) {
1365 reverse_sample_rate = msg.reverse_sample_rate();
1366 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001367 int output_sample_rate = msg.sample_rate();
1368 if (msg.has_output_sample_rate()) {
1369 output_sample_rate = msg.output_sample_rate();
1370 }
1371
Jonas Olssona4d87372019-07-05 19:08:33 +02001372 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1373 msg.num_input_channels(), msg.num_output_channels(),
1374 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001375 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001376 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001377 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001378 auto aec_dump =
1379 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1380 EXPECT_TRUE(aec_dump);
1381 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001382 first_init = false;
1383 }
1384
1385 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1386 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1387
1388 if (msg.channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001389 ASSERT_EQ(revframe_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001390 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001391 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001392 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1393 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001394 }
1395 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001396 memcpy(revframe_.data.data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001397 if (format == kFloatFormat) {
1398 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001399 ConvertToFloat(revframe_, revfloat_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001400 }
1401 }
1402 AnalyzeReverseStreamChooser(format);
1403
1404 } else if (event_msg.type() == audioproc::Event::STREAM) {
1405 const audioproc::Stream msg = event_msg.stream();
1406 // ProcessStream could have changed this for the output frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001407 frame_.num_channels = apm_->num_input_channels();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001408
Sam Zackrisson41478c72019-10-15 10:10:26 +02001409 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001411 if (msg.has_keypress()) {
1412 apm_->set_stream_key_pressed(msg.keypress());
1413 } else {
1414 apm_->set_stream_key_pressed(true);
1415 }
1416
1417 if (msg.input_channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001418 ASSERT_EQ(frame_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001419 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001420 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001421 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1422 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001423 }
1424 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001425 memcpy(frame_.data.data(), msg.input_data().data(),
yujo36b1a5f2017-06-12 12:45:32 -07001426 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001427 if (format == kFloatFormat) {
1428 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001429 ConvertToFloat(frame_, float_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001430 }
1431 }
1432 ProcessStreamChooser(format);
1433 }
1434 }
aleloif4dd1912017-06-15 01:55:38 -07001435 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001436 fclose(in_file);
1437}
1438
1439void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001440 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001441 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001442 std::string format_string;
1443 switch (format) {
1444 case kIntFormat:
1445 format_string = "_int";
1446 break;
1447 case kFloatFormat:
1448 format_string = "_float";
1449 break;
1450 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001451 const std::string ref_filename = test::TempFilename(
1452 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1453 const std::string out_filename = test::TempFilename(
1454 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001455 const std::string limited_filename = test::TempFilename(
1456 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1457 const size_t logging_limit_bytes = 100000;
1458 // We expect at least this many bytes in the created logfile.
1459 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001460 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001461 ProcessDebugDump(in_filename, ref_filename, format, -1);
1462 ProcessDebugDump(ref_filename, out_filename, format, -1);
1463 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001464
1465 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1466 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001467 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001468 ASSERT_TRUE(ref_file != NULL);
1469 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001470 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001471 std::unique_ptr<uint8_t[]> ref_bytes;
1472 std::unique_ptr<uint8_t[]> out_bytes;
1473 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001474
1475 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1476 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001477 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001478 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001479 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001480 while (ref_size > 0 && out_size > 0) {
1481 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001482 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001483 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001484 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001485 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001486 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001487 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1488 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001489 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001490 }
1491 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001492 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1493 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001494 EXPECT_NE(0, feof(ref_file));
1495 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001496 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001497 ASSERT_EQ(0, fclose(ref_file));
1498 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001499 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001500 remove(ref_filename.c_str());
1501 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001502 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001503}
1504
pbosc7a65692016-05-06 12:50:04 -07001505TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001506 VerifyDebugDumpTest(kIntFormat);
1507}
1508
pbosc7a65692016-05-06 12:50:04 -07001509TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001510 VerifyDebugDumpTest(kFloatFormat);
1511}
1512#endif
1513
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001514// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001515TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001516 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001517 const std::string filename =
1518 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001519 {
1520 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1521 EXPECT_FALSE(aec_dump);
1522 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001523
1524#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1525 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001526 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001527
aleloif4dd1912017-06-15 01:55:38 -07001528 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1529 EXPECT_TRUE(aec_dump);
1530 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001531 EXPECT_EQ(apm_->kNoError,
1532 apm_->ProcessStream(
1533 frame_.data.data(),
1534 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1535 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001536 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001537 EXPECT_EQ(apm_->kNoError,
1538 apm_->ProcessReverseStream(
1539 revframe_.data.data(),
1540 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1541 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1542 revframe_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001543 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001544
1545 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001546 FILE* fid = fopen(filename.c_str(), "r");
1547 ASSERT_TRUE(fid != NULL);
1548
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001549 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001550 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001551 ASSERT_EQ(0, remove(filename.c_str()));
1552#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001553 // Verify the file has NOT been written.
1554 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1555#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1556}
1557
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001558// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001559TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001560 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001561
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001562 const std::string filename =
1563 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001564 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1565 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001566
1567#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1568 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001569 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001570
Niels Möllere8e4dc42019-06-11 14:04:16 +02001571 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001572 EXPECT_TRUE(aec_dump);
1573 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001574 EXPECT_EQ(apm_->kNoError,
1575 apm_->ProcessReverseStream(
1576 revframe_.data.data(),
1577 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1578 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1579 revframe_.data.data()));
1580 EXPECT_EQ(apm_->kNoError,
1581 apm_->ProcessStream(
1582 frame_.data.data(),
1583 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1584 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001585 frame_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001586 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001587
1588 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001589 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001590 ASSERT_TRUE(fid != NULL);
1591
1592 // Clean it up.
1593 ASSERT_EQ(0, fclose(fid));
1594 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001595#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1596}
1597
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001598// TODO(andrew): Add a test to process a few frames with different combinations
1599// of enabled components.
1600
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001601TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001602 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001603 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001604
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001605 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001606 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001607 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001608 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001609 for (size_t i = 0; i < arraysize(kChannels); i++) {
1610 for (size_t j = 0; j < arraysize(kChannels); j++) {
1611 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001612 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001613 test->set_num_reverse_channels(kChannels[i]);
1614 test->set_num_input_channels(kChannels[j]);
1615 test->set_num_output_channels(kChannels[j]);
1616 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001617 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001618 }
1619 }
1620 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001621#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1622 // To test the extended filter mode.
1623 audioproc::Test* test = ref_data.add_test();
1624 test->set_num_reverse_channels(2);
1625 test->set_num_input_channels(2);
1626 test->set_num_output_channels(2);
1627 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1628 test->set_use_aec_extended_filter(true);
1629#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001630 }
1631
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001632 for (int i = 0; i < ref_data.test_size(); i++) {
1633 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001634
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001635 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001636 // TODO(ajm): We no longer allow different input and output channels. Skip
1637 // these tests for now, but they should be removed from the set.
1638 if (test->num_input_channels() != test->num_output_channels())
1639 continue;
1640
Per Åhgrencc73ed32020-04-26 23:56:17 +02001641 apm_.reset(AudioProcessingBuilderForTesting().Create());
Per Åhgren0695df12020-01-13 14:43:13 +01001642 AudioProcessing::Config apm_config = apm_->GetConfig();
1643 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1644 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001645
1646 EnableAllComponents();
1647
Jonas Olssona4d87372019-07-05 19:08:33 +02001648 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001649 static_cast<size_t>(test->num_input_channels()),
1650 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001651 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001652
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001653 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001654 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001655 int analog_level = 127;
1656 int analog_level_average = 0;
1657 int max_output_average = 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001658 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001659#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001660 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001661#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001662
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001663 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001664 EXPECT_EQ(
1665 apm_->kNoError,
1666 apm_->ProcessReverseStream(
1667 revframe_.data.data(),
1668 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1669 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1670 revframe_.data.data()));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001671
1672 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001673 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001674
Per Åhgren2507f8c2020-03-19 12:33:29 +01001675 EXPECT_EQ(apm_->kNoError,
1676 apm_->ProcessStream(
1677 frame_.data.data(),
1678 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1679 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001680 frame_.data.data()));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001681
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001682 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001683 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01001684 frame_.num_channels);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001685
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001686 max_output_average += MaxAudioFrame(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001687
Sam Zackrisson41478c72019-10-15 10:10:26 +02001688 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001689 analog_level_average += analog_level;
Per Åhgrencf4c8722019-12-30 14:32:14 +01001690 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001691 EXPECT_TRUE(stats.voice_detected);
1692 EXPECT_TRUE(stats.output_rms_dbfs);
1693 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001694 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001695
Per Åhgren2507f8c2020-03-19 12:33:29 +01001696 size_t frame_size = frame_.samples_per_channel * frame_.num_channels;
Jonas Olssona4d87372019-07-05 19:08:33 +02001697 size_t write_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +01001698 fwrite(frame_.data.data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001699 ASSERT_EQ(frame_size, write_count);
1700
1701 // Reset in case of downmixing.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001702 frame_.num_channels = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001703 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001704
1705#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1706 const int kStatsAggregationFrameNum = 100; // 1 second.
1707 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001708 // Get echo and delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +01001709 AudioProcessingStats stats = apm_->GetStatistics();
minyue58530ed2016-05-24 05:50:12 -07001710
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001711 // Echo metrics.
1712 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1713 const float echo_return_loss_enhancement =
1714 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001715 const float residual_echo_likelihood =
1716 stats.residual_echo_likelihood.value_or(-1.0f);
1717 const float residual_echo_likelihood_recent_max =
1718 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1719
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001720 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001721 const audioproc::Test::EchoMetrics& reference =
1722 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001723 constexpr float kEpsilon = 0.01;
1724 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1725 EXPECT_NEAR(echo_return_loss_enhancement,
1726 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001727 EXPECT_NEAR(residual_echo_likelihood,
1728 reference.residual_echo_likelihood(), kEpsilon);
1729 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1730 reference.residual_echo_likelihood_recent_max(),
1731 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001732 ++stats_index;
1733 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001734 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1735 message_echo->set_echo_return_loss(echo_return_loss);
1736 message_echo->set_echo_return_loss_enhancement(
1737 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001738 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1739 message_echo->set_residual_echo_likelihood_recent_max(
1740 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001741 }
1742 }
1743#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001744 }
1745 max_output_average /= frame_count;
1746 analog_level_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001747 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001748
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001749 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001750 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001751 // When running the test on a N7 we get a {2, 6} difference of
1752 // |has_voice_count| and |max_output_average| is up to 18 higher.
1753 // All numbers being consistently higher on N7 compare to ref_data.
1754 // TODO(bjornv): If we start getting more of these offsets on Android we
1755 // should consider a different approach. Either using one slack for all,
1756 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001757#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001758 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001759 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001760 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001761 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001762#else
1763 const int kHasVoiceCountOffset = 0;
1764 const int kHasVoiceCountNear = kIntNear;
1765 const int kMaxOutputAverageOffset = 0;
1766 const int kMaxOutputAverageNear = kIntNear;
1767#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001768 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001769 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001770
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001771 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001772 EXPECT_NEAR(test->max_output_average(),
1773 max_output_average - kMaxOutputAverageOffset,
1774 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001775#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001776 const double kFloatNear = 0.0005;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001777 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001778#endif
1779 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001780 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001781
1782 test->set_analog_level_average(analog_level_average);
1783 test->set_max_output_average(max_output_average);
1784
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001785#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrisson11b87032018-12-18 17:13:58 +01001786 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001787#endif
1788 }
1789
1790 rewind(far_file_);
1791 rewind(near_file_);
1792 }
1793
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001794 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001795 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001796 }
1797}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001798
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001799TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1800 struct ChannelFormat {
1801 AudioProcessing::ChannelLayout in_layout;
1802 AudioProcessing::ChannelLayout out_layout;
1803 };
1804 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001805 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1806 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1807 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001808 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001809
Per Åhgrencc73ed32020-04-26 23:56:17 +02001810 std::unique_ptr<AudioProcessing> ap(
1811 AudioProcessingBuilderForTesting().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001812 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001813 AudioProcessing::Config config;
1814 config.noise_suppression.enabled = true;
1815 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001816 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001817 const int in_rate = 44100;
1818 const int out_rate = 48000;
1819 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1820 TotalChannelsFromLayout(cf[i].in_layout));
1821 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1822 ChannelsFromLayout(cf[i].out_layout));
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001823 bool has_keyboard = cf[i].in_layout == AudioProcessing::kMonoAndKeyboard ||
1824 cf[i].in_layout == AudioProcessing::kStereoAndKeyboard;
1825 StreamConfig in_sc(in_rate, ChannelsFromLayout(cf[i].in_layout),
1826 has_keyboard);
1827 StreamConfig out_sc(out_rate, ChannelsFromLayout(cf[i].out_layout));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001828
1829 // Run over a few chunks.
1830 for (int j = 0; j < 10; ++j) {
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001831 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_sc, out_sc,
1832 out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001833 }
1834 }
1835}
1836
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001837// Compares the reference and test arrays over a region around the expected
1838// delay. Finds the highest SNR in that region and adds the variance and squared
1839// error results to the supplied accumulators.
1840void UpdateBestSNR(const float* ref,
1841 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001842 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001843 int expected_delay,
1844 double* variance_acc,
1845 double* sq_error_acc) {
1846 double best_snr = std::numeric_limits<double>::min();
1847 double best_variance = 0;
1848 double best_sq_error = 0;
1849 // Search over a region of eight samples around the expected delay.
1850 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1851 ++delay) {
1852 double sq_error = 0;
1853 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001854 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001855 double error = test[i + delay] - ref[i];
1856 sq_error += error * error;
1857 variance += ref[i] * ref[i];
1858 }
1859
1860 if (sq_error == 0) {
1861 *variance_acc += variance;
1862 return;
1863 }
1864 double snr = variance / sq_error;
1865 if (snr > best_snr) {
1866 best_snr = snr;
1867 best_variance = variance;
1868 best_sq_error = sq_error;
1869 }
1870 }
1871
1872 *variance_acc += best_variance;
1873 *sq_error_acc += best_sq_error;
1874}
1875
1876// Used to test a multitude of sample rate and channel combinations. It works
1877// by first producing a set of reference files (in SetUpTestCase) that are
1878// assumed to be correct, as the used parameters are verified by other tests
1879// in this collection. Primarily the reference files are all produced at
1880// "native" rates which do not involve any resampling.
1881
1882// Each test pass produces an output file with a particular format. The output
1883// is matched against the reference file closest to its internal processing
1884// format. If necessary the output is resampled back to its process format.
1885// Due to the resampling distortion, we don't expect identical results, but
1886// enforce SNR thresholds which vary depending on the format. 0 is a special
1887// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001888typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001889class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001890 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001891 public:
1892 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001893 : input_rate_(std::get<0>(GetParam())),
1894 output_rate_(std::get<1>(GetParam())),
1895 reverse_input_rate_(std::get<2>(GetParam())),
1896 reverse_output_rate_(std::get<3>(GetParam())),
1897 expected_snr_(std::get<4>(GetParam())),
1898 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001899
1900 virtual ~AudioProcessingTest() {}
1901
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001902 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001903 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001904 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001905 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001906 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1907 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1908 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001909 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001910 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1911 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1912 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001913 }
1914 }
1915 }
1916 }
1917
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001918 void TearDown() {
1919 // Remove "out" files after each test.
1920 ClearTempOutFiles();
1921 }
1922
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001923 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001924
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001925 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001926 // to a file specified with |output_file_prefix|. Both forward and reverse
1927 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001928 static void ProcessFormat(int input_rate,
1929 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001930 int reverse_input_rate,
1931 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001932 size_t num_input_channels,
1933 size_t num_output_channels,
1934 size_t num_reverse_input_channels,
1935 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001936 const std::string& output_file_prefix) {
Per Åhgrencc73ed32020-04-26 23:56:17 +02001937 std::unique_ptr<AudioProcessing> ap(
1938 AudioProcessingBuilderForTesting().Create());
Per Åhgren0695df12020-01-13 14:43:13 +01001939 AudioProcessing::Config apm_config = ap->GetConfig();
1940 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1941 ap->ApplyConfig(apm_config);
1942
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001943 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001944
ekmeyerson60d9b332015-08-14 10:35:55 -07001945 ProcessingConfig processing_config = {
1946 {{input_rate, num_input_channels},
1947 {output_rate, num_output_channels},
1948 {reverse_input_rate, num_reverse_input_channels},
1949 {reverse_output_rate, num_reverse_output_channels}}};
1950 ap->Initialize(processing_config);
1951
1952 FILE* far_file =
1953 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001954 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001955 FILE* out_file = fopen(
1956 OutputFilePath(
1957 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1958 reverse_output_rate, num_input_channels, num_output_channels,
1959 num_reverse_input_channels, num_reverse_output_channels, kForward)
1960 .c_str(),
1961 "wb");
1962 FILE* rev_out_file = fopen(
1963 OutputFilePath(
1964 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1965 reverse_output_rate, num_input_channels, num_output_channels,
1966 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1967 .c_str(),
1968 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001969 ASSERT_TRUE(far_file != NULL);
1970 ASSERT_TRUE(near_file != NULL);
1971 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001972 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001973
1974 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1975 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001976 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1977 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001978 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1979 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001980 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1981 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001982
1983 // Temporary buffers.
1984 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001985 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1986 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001987 std::unique_ptr<float[]> float_data(new float[max_length]);
1988 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001989
1990 int analog_level = 127;
1991 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1992 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001993 EXPECT_NOERR(ap->ProcessReverseStream(
1994 rev_cb.channels(), processing_config.reverse_input_stream(),
1995 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001996
1997 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001998 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001999
2000 EXPECT_NOERR(ap->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +01002001 fwd_cb.channels(), StreamConfig(input_rate, num_input_channels),
2002 StreamConfig(output_rate, num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002003
ekmeyerson60d9b332015-08-14 10:35:55 -07002004 // Dump forward output to file.
2005 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002006 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002007 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002008
Jonas Olssona4d87372019-07-05 19:08:33 +02002009 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2010 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002011
ekmeyerson60d9b332015-08-14 10:35:55 -07002012 // Dump reverse output to file.
2013 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2014 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002015 size_t rev_out_length =
2016 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002017
Jonas Olssona4d87372019-07-05 19:08:33 +02002018 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2019 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07002020
Sam Zackrisson41478c72019-10-15 10:10:26 +02002021 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002022 }
2023 fclose(far_file);
2024 fclose(near_file);
2025 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002026 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002027 }
2028
2029 protected:
2030 int input_rate_;
2031 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002032 int reverse_input_rate_;
2033 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002034 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002035 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002036};
2037
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002038TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002039 struct ChannelFormat {
2040 int num_input;
2041 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002042 int num_reverse_input;
2043 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002044 };
2045 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02002046 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
2047 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002048 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002049
pkasting25702cb2016-01-08 13:50:27 -08002050 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002051 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2052 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2053 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002054
ekmeyerson60d9b332015-08-14 10:35:55 -07002055 // Verify output for both directions.
2056 std::vector<StreamDirection> stream_directions;
2057 stream_directions.push_back(kForward);
2058 stream_directions.push_back(kReverse);
2059 for (StreamDirection file_direction : stream_directions) {
2060 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2061 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2062 const int out_num =
2063 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2064 const double expected_snr =
2065 file_direction ? expected_reverse_snr_ : expected_snr_;
2066
2067 const int min_ref_rate = std::min(in_rate, out_rate);
2068 int ref_rate;
2069
2070 if (min_ref_rate > 32000) {
2071 ref_rate = 48000;
2072 } else if (min_ref_rate > 16000) {
2073 ref_rate = 32000;
2074 } else if (min_ref_rate > 8000) {
2075 ref_rate = 16000;
2076 } else {
2077 ref_rate = 8000;
2078 }
Per Åhgrenc0424252019-12-10 13:04:15 +01002079
ekmeyerson60d9b332015-08-14 10:35:55 -07002080 FILE* out_file = fopen(
2081 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2082 reverse_output_rate_, cf[i].num_input,
2083 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02002084 cf[i].num_reverse_output, file_direction)
2085 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07002086 "rb");
2087 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02002088 FILE* ref_file =
2089 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2090 cf[i].num_output, cf[i].num_output,
2091 cf[i].num_reverse_output,
2092 cf[i].num_reverse_output, file_direction)
2093 .c_str(),
2094 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002095 ASSERT_TRUE(out_file != NULL);
2096 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002097
pkasting25702cb2016-01-08 13:50:27 -08002098 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2099 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002100 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002101 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002102 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002103 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002104 // Data from the resampled output, in case the reference and output rates
2105 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002106 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002107
ekmeyerson60d9b332015-08-14 10:35:55 -07002108 PushResampler<float> resampler;
2109 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002110
ekmeyerson60d9b332015-08-14 10:35:55 -07002111 // Compute the resampling delay of the output relative to the reference,
2112 // to find the region over which we should search for the best SNR.
2113 float expected_delay_sec = 0;
2114 if (in_rate != ref_rate) {
2115 // Input resampling delay.
2116 expected_delay_sec +=
2117 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2118 }
2119 if (out_rate != ref_rate) {
2120 // Output resampling delay.
2121 expected_delay_sec +=
2122 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2123 // Delay of converting the output back to its processing rate for
2124 // testing.
2125 expected_delay_sec +=
2126 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2127 }
2128 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002129 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002130
ekmeyerson60d9b332015-08-14 10:35:55 -07002131 double variance = 0;
2132 double sq_error = 0;
2133 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2134 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2135 float* out_ptr = out_data.get();
2136 if (out_rate != ref_rate) {
2137 // Resample the output back to its internal processing rate if
2138 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002139 ASSERT_EQ(ref_length,
2140 static_cast<size_t>(resampler.Resample(
2141 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002142 out_ptr = cmp_data.get();
2143 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002144
ekmeyerson60d9b332015-08-14 10:35:55 -07002145 // Update the |sq_error| and |variance| accumulators with the highest
2146 // SNR of reference vs output.
2147 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2148 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002149 }
2150
ekmeyerson60d9b332015-08-14 10:35:55 -07002151 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2152 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2153 << cf[i].num_input << ", " << cf[i].num_output << ", "
2154 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2155 << ", " << file_direction << "): ";
2156 if (sq_error > 0) {
2157 double snr = 10 * log10(variance / sq_error);
2158 EXPECT_GE(snr, expected_snr);
2159 EXPECT_NE(0, expected_snr);
2160 std::cout << "SNR=" << snr << " dB" << std::endl;
2161 } else {
aluebs776593b2016-03-15 14:04:58 -07002162 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002163 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002164
ekmeyerson60d9b332015-08-14 10:35:55 -07002165 fclose(out_file);
2166 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002167 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002168 }
2169}
2170
2171#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002172INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002173 CommonFormats,
2174 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002175 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2176 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2177 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2178 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2179 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2180 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2181 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2182 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2183 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2184 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2185 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2186 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002187
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002188 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2189 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2190 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2191 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2192 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2193 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2194 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2195 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2196 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2197 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2198 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2199 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002200
Per Åhgrenc0424252019-12-10 13:04:15 +01002201 std::make_tuple(32000, 48000, 48000, 48000, 15, 0),
2202 std::make_tuple(32000, 48000, 32000, 48000, 15, 30),
2203 std::make_tuple(32000, 48000, 16000, 48000, 15, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002204 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2205 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2206 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2207 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2208 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2209 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2210 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2211 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2212 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002213
Per Åhgrenc0424252019-12-10 13:04:15 +01002214 std::make_tuple(16000, 48000, 48000, 48000, 9, 0),
2215 std::make_tuple(16000, 48000, 32000, 48000, 9, 30),
2216 std::make_tuple(16000, 48000, 16000, 48000, 9, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002217 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2218 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2219 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2220 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2221 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2222 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2223 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2224 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2225 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002226
2227#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002228INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002229 CommonFormats,
2230 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002231 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2232 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2233 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002234 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2235 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2236 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002237 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2238 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2239 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002240 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2241 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2242 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002243
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002244 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2245 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2246 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2247 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2248 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2249 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002250 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2251 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2252 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2253 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2254 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2255 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002256
Per Åhgrenc0424252019-12-10 13:04:15 +01002257 std::make_tuple(32000, 48000, 48000, 48000, 17, 0),
2258 std::make_tuple(32000, 48000, 32000, 48000, 17, 30),
2259 std::make_tuple(32000, 48000, 16000, 48000, 17, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002260 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2261 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2262 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002263 std::make_tuple(32000, 32000, 48000, 32000, 27, 35),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002264 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002265 std::make_tuple(32000, 32000, 16000, 32000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002266 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2267 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2268 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002269
Per Åhgrenc0424252019-12-10 13:04:15 +01002270 std::make_tuple(16000, 48000, 48000, 48000, 11, 0),
2271 std::make_tuple(16000, 48000, 32000, 48000, 11, 30),
2272 std::make_tuple(16000, 48000, 16000, 48000, 11, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002273 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2274 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2275 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002276 std::make_tuple(16000, 32000, 48000, 32000, 24, 35),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002277 std::make_tuple(16000, 32000, 32000, 32000, 24, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002278 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002279 std::make_tuple(16000, 16000, 48000, 16000, 28, 20),
2280 std::make_tuple(16000, 16000, 32000, 16000, 28, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002281 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002282#endif
2283
Per Åhgren3e8bf282019-08-29 23:38:40 +02002284// Produces a scoped trace debug output.
2285std::string ProduceDebugText(int render_input_sample_rate_hz,
2286 int render_output_sample_rate_hz,
2287 int capture_input_sample_rate_hz,
2288 int capture_output_sample_rate_hz,
2289 size_t render_input_num_channels,
2290 size_t render_output_num_channels,
2291 size_t capture_input_num_channels,
2292 size_t capture_output_num_channels) {
2293 rtc::StringBuilder ss;
2294 ss << "Sample rates:"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002295 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002296 << render_input_sample_rate_hz
2297 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002298 "\n Render output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002299 << render_output_sample_rate_hz
2300 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002301 "\n Capture input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002302 << capture_input_sample_rate_hz
2303 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002304 "\n Capture output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002305 << capture_output_sample_rate_hz
2306 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002307 "\nNumber of channels:"
2308 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002309 << render_input_num_channels
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002310 << "\n Render output: " << render_output_num_channels
2311 << "\n Capture input: " << capture_input_num_channels
2312 << "\n Capture output: " << capture_output_num_channels;
Per Åhgren3e8bf282019-08-29 23:38:40 +02002313 return ss.Release();
2314}
2315
2316// Validates that running the audio processing module using various combinations
2317// of sample rates and number of channels works as intended.
2318void RunApmRateAndChannelTest(
2319 rtc::ArrayView<const int> sample_rates_hz,
2320 rtc::ArrayView<const int> render_channel_counts,
2321 rtc::ArrayView<const int> capture_channel_counts) {
Per Åhgrencc73ed32020-04-26 23:56:17 +02002322 std::unique_ptr<AudioProcessing> apm(
2323 AudioProcessingBuilderForTesting().Create());
Per Åhgren3e8bf282019-08-29 23:38:40 +02002324 webrtc::AudioProcessing::Config apm_config;
2325 apm_config.echo_canceller.enabled = true;
2326 apm->ApplyConfig(apm_config);
2327
2328 StreamConfig render_input_stream_config;
2329 StreamConfig render_output_stream_config;
2330 StreamConfig capture_input_stream_config;
2331 StreamConfig capture_output_stream_config;
2332
2333 std::vector<float> render_input_frame_channels;
2334 std::vector<float*> render_input_frame;
2335 std::vector<float> render_output_frame_channels;
2336 std::vector<float*> render_output_frame;
2337 std::vector<float> capture_input_frame_channels;
2338 std::vector<float*> capture_input_frame;
2339 std::vector<float> capture_output_frame_channels;
2340 std::vector<float*> capture_output_frame;
2341
2342 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2343 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2344 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2345 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2346 for (size_t render_input_num_channels : render_channel_counts) {
2347 for (size_t capture_input_num_channels : capture_channel_counts) {
2348 size_t render_output_num_channels = render_input_num_channels;
2349 size_t capture_output_num_channels = capture_input_num_channels;
2350 auto populate_audio_frame = [](int sample_rate_hz,
2351 size_t num_channels,
2352 StreamConfig* cfg,
2353 std::vector<float>* channels_data,
2354 std::vector<float*>* frame_data) {
2355 cfg->set_sample_rate_hz(sample_rate_hz);
2356 cfg->set_num_channels(num_channels);
2357 cfg->set_has_keyboard(false);
2358
2359 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2360 channels_data->resize(num_channels * max_frame_size);
2361 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2362 frame_data->resize(num_channels);
2363 for (size_t channel = 0; channel < num_channels; ++channel) {
2364 (*frame_data)[channel] =
2365 &(*channels_data)[channel * max_frame_size];
2366 }
2367 };
2368
2369 populate_audio_frame(
2370 render_input_sample_rate_hz, render_input_num_channels,
2371 &render_input_stream_config, &render_input_frame_channels,
2372 &render_input_frame);
2373 populate_audio_frame(
2374 render_output_sample_rate_hz, render_output_num_channels,
2375 &render_output_stream_config, &render_output_frame_channels,
2376 &render_output_frame);
2377 populate_audio_frame(
2378 capture_input_sample_rate_hz, capture_input_num_channels,
2379 &capture_input_stream_config, &capture_input_frame_channels,
2380 &capture_input_frame);
2381 populate_audio_frame(
2382 capture_output_sample_rate_hz, capture_output_num_channels,
2383 &capture_output_stream_config, &capture_output_frame_channels,
2384 &capture_output_frame);
2385
2386 for (size_t frame = 0; frame < 2; ++frame) {
2387 SCOPED_TRACE(ProduceDebugText(
2388 render_input_sample_rate_hz, render_output_sample_rate_hz,
2389 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2390 render_input_num_channels, render_output_num_channels,
2391 render_input_num_channels, capture_output_num_channels));
2392
2393 int result = apm->ProcessReverseStream(
2394 &render_input_frame[0], render_input_stream_config,
2395 render_output_stream_config, &render_output_frame[0]);
2396 EXPECT_EQ(result, AudioProcessing::kNoError);
2397 result = apm->ProcessStream(
2398 &capture_input_frame[0], capture_input_stream_config,
2399 capture_output_stream_config, &capture_output_frame[0]);
2400 EXPECT_EQ(result, AudioProcessing::kNoError);
2401 }
2402 }
2403 }
2404 }
2405 }
2406 }
2407 }
2408}
2409
niklase@google.com470e71d2011-07-07 08:21:25 +00002410} // namespace
peahc19f3122016-10-07 14:54:10 -07002411
Alessio Bazzicac054e782018-04-16 12:10:09 +02002412TEST(RuntimeSettingTest, TestDefaultCtor) {
2413 auto s = AudioProcessing::RuntimeSetting();
2414 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2415}
2416
2417TEST(RuntimeSettingTest, TestCapturePreGain) {
2418 using Type = AudioProcessing::RuntimeSetting::Type;
2419 {
2420 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2421 EXPECT_EQ(Type::kCapturePreGain, s.type());
2422 float v;
2423 s.GetFloat(&v);
2424 EXPECT_EQ(1.25f, v);
2425 }
2426
2427#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2428 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2429#endif
2430}
2431
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002432TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2433 using Type = AudioProcessing::RuntimeSetting::Type;
2434 {
2435 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2436 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2437 float v;
2438 s.GetFloat(&v);
2439 EXPECT_EQ(1.25f, v);
2440 }
2441
2442#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2443 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2444#endif
2445}
2446
Alessio Bazzicac054e782018-04-16 12:10:09 +02002447TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2448 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2449 auto s = AudioProcessing::RuntimeSetting();
2450 ASSERT_TRUE(q.Insert(&s));
2451 ASSERT_TRUE(q.Remove(&s));
2452 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2453}
2454
Sam Zackrisson0beac582017-09-25 12:04:02 +02002455TEST(ApmConfiguration, EnablePostProcessing) {
2456 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002457 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002458 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002459 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002460 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002461 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002462 AudioProcessingBuilderForTesting()
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002463 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002464 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002465
Per Åhgren2507f8c2020-03-19 12:33:29 +01002466 Int16FrameData audio;
2467 audio.num_channels = 1;
Sam Zackrisson0beac582017-09-25 12:04:02 +02002468 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2469
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002470 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002471 apm->ProcessStream(audio.data.data(),
2472 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2473 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002474 audio.data.data());
Sam Zackrisson0beac582017-09-25 12:04:02 +02002475}
2476
Alex Loiko5825aa62017-12-18 16:02:40 +01002477TEST(ApmConfiguration, EnablePreProcessing) {
2478 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002479 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002480 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002481 auto mock_pre_processor =
2482 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002483 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002484 AudioProcessingBuilderForTesting()
Ivo Creusen62337e52018-01-09 14:17:33 +01002485 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002486 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002487
Per Åhgren2507f8c2020-03-19 12:33:29 +01002488 Int16FrameData audio;
2489 audio.num_channels = 1;
Alex Loiko5825aa62017-12-18 16:02:40 +01002490 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2491
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002492 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002493 apm->ProcessReverseStream(
2494 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2495 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2496 audio.data.data());
Alex Loiko5825aa62017-12-18 16:02:40 +01002497}
2498
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002499TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2500 // Verify that apm uses a capture analyzer if one is provided.
2501 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002502 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002503 auto mock_capture_analyzer =
2504 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2505 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002506 AudioProcessingBuilderForTesting()
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002507 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2508 .Create();
2509
Per Åhgren2507f8c2020-03-19 12:33:29 +01002510 Int16FrameData audio;
2511 audio.num_channels = 1;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002512 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2513
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002514 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002515 apm->ProcessStream(audio.data.data(),
2516 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2517 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002518 audio.data.data());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002519}
2520
Alex Loiko73ec0192018-05-15 10:52:28 +02002521TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2522 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002523 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002524 auto mock_pre_processor =
2525 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2526 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002527 AudioProcessingBuilderForTesting()
Alex Loiko73ec0192018-05-15 10:52:28 +02002528 .SetRenderPreProcessing(std::move(mock_pre_processor))
2529 .Create();
2530 apm->SetRuntimeSetting(
2531 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2532
2533 // RuntimeSettings forwarded during 'Process*Stream' calls.
2534 // Therefore we have to make one such call.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002535 Int16FrameData audio;
2536 audio.num_channels = 1;
Alex Loiko73ec0192018-05-15 10:52:28 +02002537 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2538
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002539 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2540 .Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002541 apm->ProcessReverseStream(
2542 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2543 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2544 audio.data.data());
Alex Loiko73ec0192018-05-15 10:52:28 +02002545}
2546
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002547class MyEchoControlFactory : public EchoControlFactory {
2548 public:
2549 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2550 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002551 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2552 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
Per Åhgrenc20a19c2019-11-13 11:12:29 +01002553 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_, ::testing::_))
2554 .Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002555 return std::unique_ptr<EchoControl>(ec);
2556 }
Per Åhgrence202a02019-09-02 17:01:19 +02002557
2558 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
Per Åhgren4e5c7092019-11-01 20:44:11 +01002559 int num_render_channels,
2560 int num_capture_channels) {
Per Åhgrence202a02019-09-02 17:01:19 +02002561 return Create(sample_rate_hz);
2562 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002563};
2564
2565TEST(ApmConfiguration, EchoControlInjection) {
2566 // Verify that apm uses an injected echo controller if one is provided.
2567 webrtc::Config webrtc_config;
2568 std::unique_ptr<EchoControlFactory> echo_control_factory(
2569 new MyEchoControlFactory());
2570
Alex Loiko5825aa62017-12-18 16:02:40 +01002571 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002572 AudioProcessingBuilderForTesting()
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002573 .SetEchoControlFactory(std::move(echo_control_factory))
2574 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002575
Per Åhgren2507f8c2020-03-19 12:33:29 +01002576 Int16FrameData audio;
2577 audio.num_channels = 1;
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002578 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002579 apm->ProcessStream(audio.data.data(),
2580 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2581 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002582 audio.data.data());
Per Åhgren2507f8c2020-03-19 12:33:29 +01002583 apm->ProcessReverseStream(
2584 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2585 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2586 audio.data.data());
2587 apm->ProcessStream(audio.data.data(),
2588 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2589 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002590 audio.data.data());
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002591}
Ivo Creusenae026092017-11-20 13:07:16 +01002592
Per Åhgren8607f842019-04-12 22:02:26 +02002593std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002594 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002595 std::unique_ptr<AudioProcessing> apm(
Per Åhgrencc73ed32020-04-26 23:56:17 +02002596 AudioProcessingBuilderForTesting().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002597 if (!apm) {
2598 return apm;
2599 }
2600
2601 ProcessingConfig processing_config = {
2602 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2603
2604 if (apm->Initialize(processing_config) != 0) {
2605 return nullptr;
2606 }
2607
2608 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002609 AudioProcessing::Config apm_config;
2610 apm_config.residual_echo_detector.enabled = true;
2611 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002612 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002613 apm_config.gain_controller2.enabled = false;
2614 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002615 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002616 apm_config.noise_suppression.enabled = false;
2617 apm_config.level_estimation.enabled = false;
2618 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002619 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002620 return apm;
2621}
2622
2623#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2624#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2625#else
2626#define MAYBE_ApmStatistics ApmStatistics
2627#endif
2628
Per Åhgren8607f842019-04-12 22:02:26 +02002629TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2630 // Set up APM with AEC3 and process some audio.
2631 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002632 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002633 AudioProcessing::Config apm_config;
2634 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002635 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002636
2637 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002638 Int16FrameData frame;
2639 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002640 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002641
2642 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002643 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002644 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2645 ptr[i] = 10000 * ((i % 3) - 1);
2646 }
2647
2648 // Do some processing.
2649 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002650 EXPECT_EQ(apm->ProcessReverseStream(
2651 frame.data.data(),
2652 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2653 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2654 frame.data.data()),
2655 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002656 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002657 EXPECT_EQ(apm->ProcessStream(
2658 frame.data.data(),
2659 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2660 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002661 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002662 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002663 }
2664
2665 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002666 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002667 // We expect all statistics to be set and have a sensible value.
2668 ASSERT_TRUE(stats.residual_echo_likelihood);
2669 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2670 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2671 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2672 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2673 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2674 ASSERT_TRUE(stats.echo_return_loss);
2675 EXPECT_NE(*stats.echo_return_loss, -100.0);
2676 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2677 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002678}
2679
2680TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2681 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002682 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002683 ASSERT_TRUE(apm);
2684
2685 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002686 Int16FrameData frame;
2687 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002688 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002689
2690 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002691 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002692 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2693 ptr[i] = 10000 * ((i % 3) - 1);
2694 }
2695
2696 // Do some processing.
2697 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002698 EXPECT_EQ(apm->ProcessReverseStream(
2699 frame.data.data(),
2700 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2701 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2702 frame.data.data()),
2703 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002704 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002705 EXPECT_EQ(apm->ProcessStream(
2706 frame.data.data(),
2707 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2708 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002709 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002710 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002711 }
2712
2713 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002714 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002715 // We expect only the residual echo detector statistics to be set and have a
2716 // sensible value.
2717 EXPECT_TRUE(stats.residual_echo_likelihood);
2718 if (stats.residual_echo_likelihood) {
2719 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2720 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2721 }
2722 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2723 if (stats.residual_echo_likelihood_recent_max) {
2724 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2725 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2726 }
2727 EXPECT_FALSE(stats.echo_return_loss);
2728 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002729}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002730
2731TEST(ApmStatistics, ReportOutputRmsDbfs) {
2732 ProcessingConfig processing_config = {
2733 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2734 AudioProcessing::Config config;
2735
2736 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002737 Int16FrameData frame;
2738 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002739 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002740
2741 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002742 int16_t* ptr = frame.data.data();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002743 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2744 ptr[i] = 10000 * ((i % 3) - 1);
2745 }
2746
Per Åhgrencc73ed32020-04-26 23:56:17 +02002747 std::unique_ptr<AudioProcessing> apm(
2748 AudioProcessingBuilderForTesting().Create());
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002749 apm->Initialize(processing_config);
2750
2751 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002752 EXPECT_EQ(
2753 apm->ProcessStream(frame.data.data(),
2754 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2755 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002756 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002757 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002758 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002759
2760 // If enabled, metrics should be reported.
2761 config.level_estimation.enabled = true;
2762 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002763 EXPECT_EQ(
2764 apm->ProcessStream(frame.data.data(),
2765 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2766 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002767 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002768 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002769 auto stats = apm->GetStatistics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002770 EXPECT_TRUE(stats.output_rms_dbfs);
2771 EXPECT_GE(*stats.output_rms_dbfs, 0);
2772
2773 // If re-disabled, the value is again not reported.
2774 config.level_estimation.enabled = false;
2775 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002776 EXPECT_EQ(
2777 apm->ProcessStream(frame.data.data(),
2778 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2779 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002780 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002781 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002782 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002783}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002784
2785TEST(ApmStatistics, ReportHasVoice) {
2786 ProcessingConfig processing_config = {
2787 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2788 AudioProcessing::Config config;
2789
2790 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002791 Int16FrameData frame;
2792 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002793 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2794
2795 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002796 int16_t* ptr = frame.data.data();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002797 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2798 ptr[i] = 10000 * ((i % 3) - 1);
2799 }
2800
Per Åhgrencc73ed32020-04-26 23:56:17 +02002801 std::unique_ptr<AudioProcessing> apm(
2802 AudioProcessingBuilderForTesting().Create());
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002803 apm->Initialize(processing_config);
2804
2805 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002806 EXPECT_EQ(
2807 apm->ProcessStream(frame.data.data(),
2808 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2809 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002810 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002811 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002812 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002813
2814 // If enabled, metrics should be reported.
2815 config.voice_detection.enabled = true;
2816 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002817 EXPECT_EQ(
2818 apm->ProcessStream(frame.data.data(),
2819 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2820 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002821 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002822 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002823 auto stats = apm->GetStatistics();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002824 EXPECT_TRUE(stats.voice_detected);
2825
2826 // If re-disabled, the value is again not reported.
2827 config.voice_detection.enabled = false;
2828 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002829 EXPECT_EQ(
2830 apm->ProcessStream(frame.data.data(),
2831 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2832 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002833 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002834 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002835 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002836}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002837
2838TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2839 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2840 std::array<int, 2> render_channel_counts = {1, 7};
2841 std::array<int, 2> capture_channel_counts = {1, 7};
2842 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2843 capture_channel_counts);
2844}
2845
2846TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2847 std::array<int, 1> sample_rates_hz = {48000};
2848 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2849 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2850 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2851 capture_channel_counts);
2852}
2853
2854TEST(ApmConfiguration, HandlingOfRateCombinations) {
2855 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2856 48000, 96000, 192000, 384000};
2857 std::array<int, 1> render_channel_counts = {2};
2858 std::array<int, 1> capture_channel_counts = {2};
2859 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2860 capture_channel_counts);
2861}
2862
Yves Gerey1fce3f82019-12-05 17:45:31 +01002863TEST(ApmConfiguration, SelfAssignment) {
2864 // At some point memory sanitizer was complaining about self-assigment.
2865 // Make sure we don't regress.
2866 AudioProcessing::Config config;
2867 AudioProcessing::Config* config2 = &config;
2868 *config2 = *config2; // Workaround -Wself-assign-overloaded
2869 SUCCEED(); // Real success is absence of defects from asan/msan/ubsan.
2870}
2871
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002872} // namespace webrtc