blob: 8bd138f1558a3e7f8179be61c96197c2b8e49d5c [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"
Sam Zackrisson03cb7e52021-12-06 15:40:04 +010023#include "api/audio/echo_detector_creator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "common_audio/include/audio_util.h"
25#include "common_audio/resampler/include/push_resampler.h"
26#include "common_audio/resampler/push_sinc_resampler.h"
27#include "common_audio/signal_processing/include/signal_processing_library.h"
28#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
29#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_processing/common.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020031#include "modules/audio_processing/include/mock_audio_processing.h"
Per Åhgrencc73ed32020-04-26 23:56:17 +020032#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "modules/audio_processing/test/protobuf_utils.h"
34#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/arraysize.h"
36#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/gtest_prod_util.h"
39#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010040#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010041#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080043#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020044#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020045#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020046#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010047#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "rtc_base/thread.h"
Per Åhgrena43178c2020-09-25 12:02:32 +020049#include "system_wrappers/include/cpu_features_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080051#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070052
53RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000054#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000055#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000056#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000058#endif
kwiberg77eab702016-09-28 17:42:01 -070059RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000060
Sam Zackrisson6558fa52019-08-26 10:12:41 +020061ABSL_FLAG(bool,
62 write_apm_ref_data,
63 false,
64 "Write ApmTest.Process results to file, instead of comparing results "
65 "to the existing reference data file.");
66
andrew@webrtc.org27c69802014-02-18 20:24:56 +000067namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000068namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000069
ekmeyerson60d9b332015-08-14 10:35:55 -070070// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
71// applicable.
72
mbonadei7c2c8432017-04-07 00:59:12 -070073const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070074const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000075
aluebseb3603b2016-04-20 15:27:58 -070076#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
77// Android doesn't support 48kHz.
78const int kProcessSampleRates[] = {8000, 16000, 32000};
79#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070080const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070081#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000082
ekmeyerson60d9b332015-08-14 10:35:55 -070083enum StreamDirection { kForward = 0, kReverse };
84
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000085void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
Jonas Olssona4d87372019-07-05 19:08:33 +020086 ChannelBuffer<int16_t> cb_int(cb->num_frames(), cb->num_channels());
87 Deinterleave(int_data, cb->num_frames(), cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000088 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080089 for (size_t i = 0; i < cb->num_channels(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +020090 S16ToFloat(cb_int.channels()[i], cb->num_frames(), cb->channels()[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000091 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000092}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000093
Per Åhgren2507f8c2020-03-19 12:33:29 +010094void ConvertToFloat(const Int16FrameData& frame, ChannelBuffer<float>* cb) {
95 ConvertToFloat(frame.data.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000096}
97
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080099size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000100 switch (layout) {
101 case AudioProcessing::kMono:
102 return 1;
103 case AudioProcessing::kMonoAndKeyboard:
104 case AudioProcessing::kStereo:
105 return 2;
106 case AudioProcessing::kStereoAndKeyboard:
107 return 3;
108 }
Artem Titovd3251962021-11-15 16:57:07 +0100109 RTC_DCHECK_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800110 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000111}
112
Jonas Olssona4d87372019-07-05 19:08:33 +0200113void MixStereoToMono(const float* stereo,
114 float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800115 size_t samples_per_channel) {
116 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000118}
119
Jonas Olssona4d87372019-07-05 19:08:33 +0200120void MixStereoToMono(const int16_t* stereo,
121 int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800122 size_t samples_per_channel) {
123 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000124 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
125}
126
pkasting25702cb2016-01-08 13:50:27 -0800127void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
128 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000129 stereo[i * 2 + 1] = stereo[i * 2];
130 }
131}
132
yujo36b1a5f2017-06-12 12:45:32 -0700133void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800134 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000135 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
136 }
137}
138
Per Åhgren2507f8c2020-03-19 12:33:29 +0100139void SetFrameTo(Int16FrameData* frame, int16_t value) {
140 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700141 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100142 frame->data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000143 }
144}
145
Per Åhgren2507f8c2020-03-19 12:33:29 +0100146void SetFrameTo(Int16FrameData* frame, int16_t left, int16_t right) {
147 ASSERT_EQ(2u, frame->num_channels);
148 for (size_t i = 0; i < frame->samples_per_channel * 2; i += 2) {
149 frame->data[i] = left;
150 frame->data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000151 }
152}
153
Per Åhgren2507f8c2020-03-19 12:33:29 +0100154void ScaleFrame(Int16FrameData* frame, float scale) {
155 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700156 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100157 frame->data[i] = FloatS16ToS16(frame->data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000158 }
159}
160
Per Åhgren2507f8c2020-03-19 12:33:29 +0100161bool FrameDataAreEqual(const Int16FrameData& frame1,
162 const Int16FrameData& frame2) {
163 if (frame1.samples_per_channel != frame2.samples_per_channel) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000164 return false;
165 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100166 if (frame1.num_channels != frame2.num_channels) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000167 return false;
168 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100169 if (memcmp(
170 frame1.data.data(), frame2.data.data(),
171 frame1.samples_per_channel * frame1.num_channels * sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000172 return false;
173 }
174 return true;
175}
176
Per Åhgren2507f8c2020-03-19 12:33:29 +0100177rtc::ArrayView<int16_t> GetMutableFrameData(Int16FrameData* frame) {
178 int16_t* ptr = frame->data.data();
179 const size_t len = frame->samples_per_channel * frame->num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200180 return rtc::ArrayView<int16_t>(ptr, len);
181}
182
Per Åhgren2507f8c2020-03-19 12:33:29 +0100183rtc::ArrayView<const int16_t> GetFrameData(const Int16FrameData& frame) {
184 const int16_t* ptr = frame.data.data();
185 const size_t len = frame.samples_per_channel * frame.num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200186 return rtc::ArrayView<const int16_t>(ptr, len);
187}
188
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000189void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200190 AudioProcessing::Config apm_config = ap->GetConfig();
191 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000192#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200193 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100194
195 apm_config.gain_controller1.enabled = true;
196 apm_config.gain_controller1.mode =
197 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000198#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200199 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000200
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100201 apm_config.gain_controller1.enabled = true;
202 apm_config.gain_controller1.mode =
203 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
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 Zackrisson0824c6f2019-10-07 14:03:56 +0200209 apm_config.voice_detection.enabled = true;
Per Åhgrenc0424252019-12-10 13:04:15 +0100210 apm_config.pipeline.maximum_internal_processing_rate = 48000;
peah8271d042016-11-22 07:24:52 -0800211 ap->ApplyConfig(apm_config);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000212}
213
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000214// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000215template <class T>
216T AbsValue(T a) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200217 return a > 0 ? a : -a;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000218}
219
Per Åhgren2507f8c2020-03-19 12:33:29 +0100220int16_t MaxAudioFrame(const Int16FrameData& frame) {
221 const size_t length = frame.samples_per_channel * frame.num_channels;
222 int16_t max_data = AbsValue(frame.data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800223 for (size_t i = 1; i < length; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100224 max_data = std::max(max_data, AbsValue(frame.data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000225 }
226
227 return max_data;
228}
229
Alex Loiko890988c2017-08-31 10:25:48 +0200230void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700231 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 ASSERT_TRUE(file != NULL);
234
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100235 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800237 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000238 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000240 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241 ASSERT_EQ(static_cast<size_t>(size),
Jonas Olssona4d87372019-07-05 19:08:33 +0200242 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243 fclose(file);
244}
245
Alex Loiko890988c2017-08-31 10:25:48 +0200246std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200247 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000248 // Resource files are all stereo.
249 ss << name << sample_rate_hz / 1000 << "_stereo";
250 return test::ResourcePath(ss.str(), "pcm");
251}
252
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000253// Temporary filenames unique to this process. Used to be able to run these
254// tests in parallel as each process needs to be running in isolation they can't
255// have competing filenames.
256std::map<std::string, std::string> temp_filenames;
257
Alex Loiko890988c2017-08-31 10:25:48 +0200258std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000259 int input_rate,
260 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700261 int reverse_input_rate,
262 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800263 size_t num_input_channels,
264 size_t num_output_channels,
265 size_t num_reverse_input_channels,
266 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700267 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200268 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700269 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
270 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000271 if (num_output_channels == 1) {
272 ss << "mono";
273 } else if (num_output_channels == 2) {
274 ss << "stereo";
275 } else {
Artem Titovd3251962021-11-15 16:57:07 +0100276 RTC_DCHECK_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000277 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 ss << output_rate / 1000;
279 if (num_reverse_output_channels == 1) {
280 ss << "_rmono";
281 } else if (num_reverse_output_channels == 2) {
282 ss << "_rstereo";
283 } else {
Artem Titovd3251962021-11-15 16:57:07 +0100284 RTC_DCHECK_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700285 }
286 ss << reverse_output_rate / 1000;
287 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000288
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000289 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700290 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000291 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
292 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000293}
294
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000295void ClearTempFiles() {
296 for (auto& kv : temp_filenames)
297 remove(kv.second.c_str());
298}
299
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200300// Only remove "out" files. Keep "ref" files.
301void ClearTempOutFiles() {
302 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
303 const std::string& filename = it->first;
304 if (filename.substr(0, 3).compare("out") == 0) {
305 remove(it->second.c_str());
306 temp_filenames.erase(it++);
307 } else {
308 it++;
309 }
310 }
311}
312
Alex Loiko890988c2017-08-31 10:25:48 +0200313void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000314 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000315 ASSERT_TRUE(file != NULL);
316 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000317 fclose(file);
318}
319
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000320// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
321// stereo) file, converts to deinterleaved float (optionally downmixing) and
Artem Titov0b489302021-07-28 20:50:03 +0200322// returns the result in `cb`. Returns false if the file ended (or on error) and
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000323// true otherwise.
324//
Artem Titov0b489302021-07-28 20:50:03 +0200325// `int_data` and `float_data` are just temporary space that must be
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000326// sufficiently large to hold the 10 ms chunk.
Jonas Olssona4d87372019-07-05 19:08:33 +0200327bool ReadChunk(FILE* file,
328 int16_t* int_data,
329 float* float_data,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000330 ChannelBuffer<float>* cb) {
331 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000332 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000333 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
334 if (read_count != frame_size) {
335 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700336 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000337 return false; // This is expected.
338 }
339
340 S16ToFloat(int_data, frame_size, float_data);
341 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000342 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000343 } else {
Jonas Olssona4d87372019-07-05 19:08:33 +0200344 Deinterleave(float_data, cb->num_frames(), 2, cb->channels());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000345 }
346
347 return true;
348}
349
Per Åhgrena43178c2020-09-25 12:02:32 +0200350// Returns the reference file name that matches the current CPU
351// architecture/optimizations.
352std::string GetReferenceFilename() {
353#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
354 return test::ResourcePath("audio_processing/output_data_fixed", "pb");
355#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
356 if (GetCPUInfo(kAVX2) != 0) {
357 return test::ResourcePath("audio_processing/output_data_float_avx2", "pb");
358 }
359 return test::ResourcePath("audio_processing/output_data_float", "pb");
360#endif
361}
362
niklase@google.com470e71d2011-07-07 08:21:25 +0000363class ApmTest : public ::testing::Test {
364 protected:
365 ApmTest();
366 virtual void SetUp();
367 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000368
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200369 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000370
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200371 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000372
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000373 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200374 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000375
376 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000377 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000378 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800379 size_t num_input_channels,
380 size_t num_output_channels,
381 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000382 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000383 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000384 void EnableAllComponents();
Per Åhgren2507f8c2020-03-19 12:33:29 +0100385 bool ReadFrame(FILE* file, Int16FrameData* frame);
386 bool ReadFrame(FILE* file, Int16FrameData* frame, ChannelBuffer<float>* cb);
387 void ReadFrameWithRewind(FILE* file, Int16FrameData* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200388 void ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100389 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000390 ChannelBuffer<float>* cb);
Jonas Olssona4d87372019-07-05 19:08:33 +0200391 void ProcessDelayVerificationTest(int delay_ms,
392 int system_delay_ms,
393 int delay_min,
394 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700395 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800396 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700397 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800398 void TestChangingForwardChannels(size_t num_in_channels,
399 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700400 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800401 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700402 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000403 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
404 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000405 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000406 int ProcessStreamChooser(Format format);
407 int AnalyzeReverseStreamChooser(Format format);
408 void ProcessDebugDump(const std::string& in_filename,
409 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800410 Format format,
411 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000412 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000413
414 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000415 const std::string ref_filename_;
Niels Möller4f776ac2021-07-02 11:30:54 +0200416 rtc::scoped_refptr<AudioProcessing> apm_;
Per Åhgren2507f8c2020-03-19 12:33:29 +0100417 Int16FrameData frame_;
418 Int16FrameData revframe_;
Sam Zackrisson03cb7e52021-12-06 15:40:04 +0100419 std::unique_ptr<ChannelBuffer<float>> float_cb_;
420 std::unique_ptr<ChannelBuffer<float>> revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000421 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800422 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 FILE* far_file_;
424 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000425 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000426};
427
428ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000429 : output_path_(test::OutputPath()),
Per Åhgrena43178c2020-09-25 12:02:32 +0200430 ref_filename_(GetReferenceFilename()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000431 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000432 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000433 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000434 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000435 out_file_(NULL) {
Niels Möller4f776ac2021-07-02 11:30:54 +0200436 apm_ = AudioProcessingBuilderForTesting().Create();
Per Åhgrenc0424252019-12-10 13:04:15 +0100437 AudioProcessing::Config apm_config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100438 apm_config.gain_controller1.analog_gain_controller.enabled = false;
Per Åhgrenc0424252019-12-10 13:04:15 +0100439 apm_config.pipeline.maximum_internal_processing_rate = 48000;
440 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000441}
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
443void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000444 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000446 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
449void ApmTest::TearDown() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 if (far_file_) {
451 ASSERT_EQ(0, fclose(far_file_));
452 }
453 far_file_ = NULL;
454
455 if (near_file_) {
456 ASSERT_EQ(0, fclose(near_file_));
457 }
458 near_file_ = NULL;
459
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000460 if (out_file_) {
461 ASSERT_EQ(0, fclose(out_file_));
462 }
463 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000464}
465
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000466void ApmTest::Init(AudioProcessing* ap) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200467 ASSERT_EQ(
468 kNoErr,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100469 ap->Initialize({{{frame_.sample_rate_hz, frame_.num_channels},
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200470 {output_sample_rate_hz_, num_output_channels_},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100471 {revframe_.sample_rate_hz, revframe_.num_channels},
472 {revframe_.sample_rate_hz, revframe_.num_channels}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000473}
474
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000475void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000476 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000477 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800478 size_t num_input_channels,
479 size_t num_output_channels,
480 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000481 bool open_output_file) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200482 SetContainerFormat(sample_rate_hz, num_input_channels, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000483 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000485
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200486 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, &revframe_,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000487 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000488 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000489
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490 if (far_file_) {
491 ASSERT_EQ(0, fclose(far_file_));
492 }
493 std::string filename = ResourceFilePath("far", sample_rate_hz);
494 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200495 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000496
497 if (near_file_) {
498 ASSERT_EQ(0, fclose(near_file_));
499 }
500 filename = ResourceFilePath("near", sample_rate_hz);
501 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200502 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000503
504 if (open_output_file) {
505 if (out_file_) {
506 ASSERT_EQ(0, fclose(out_file_));
507 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700508 filename = OutputFilePath(
509 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
510 reverse_sample_rate_hz, num_input_channels, num_output_channels,
511 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000512 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200513 ASSERT_TRUE(out_file_ != NULL)
514 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000515 }
516}
517
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000518void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000519 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000520}
521
Jonas Olssona4d87372019-07-05 19:08:33 +0200522bool ApmTest::ReadFrame(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100523 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000524 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000525 // The files always contain stereo audio.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100526 size_t frame_size = frame->samples_per_channel * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200527 size_t read_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100528 fread(frame->data.data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000529 if (read_count != frame_size) {
530 // Check that the file really ended.
531 EXPECT_NE(0, feof(file));
532 return false; // This is expected.
533 }
534
Per Åhgren2507f8c2020-03-19 12:33:29 +0100535 if (frame->num_channels == 1) {
536 MixStereoToMono(frame->data.data(), frame->data.data(),
537 frame->samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000538 }
539
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000541 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000542 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000543 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000544}
545
Per Åhgren2507f8c2020-03-19 12:33:29 +0100546bool ApmTest::ReadFrame(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000547 return ReadFrame(file, frame, NULL);
548}
549
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000550// If the end of the file has been reached, rewind it and attempt to read the
551// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200552void ApmTest::ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100553 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554 ChannelBuffer<float>* cb) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200555 if (!ReadFrame(near_file_, &frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000556 rewind(near_file_);
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200557 ASSERT_TRUE(ReadFrame(near_file_, &frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000558 }
559}
560
Per Åhgren2507f8c2020-03-19 12:33:29 +0100561void ApmTest::ReadFrameWithRewind(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000562 ReadFrameWithRewind(file, frame, NULL);
563}
564
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000565int ApmTest::ProcessStreamChooser(Format format) {
566 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100567 return apm_->ProcessStream(
568 frame_.data.data(),
569 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
570 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100571 frame_.data.data());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200573 return apm_->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100574 float_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100575 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100576 StreamConfig(output_sample_rate_hz_, num_output_channels_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200577 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000578}
579
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000580int ApmTest::AnalyzeReverseStreamChooser(Format format) {
581 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100582 return apm_->ProcessReverseStream(
583 revframe_.data.data(),
584 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
585 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
586 revframe_.data.data());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000588 return apm_->AnalyzeReverseStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100589 revfloat_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100590 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591}
592
Jonas Olssona4d87372019-07-05 19:08:33 +0200593void ApmTest::ProcessDelayVerificationTest(int delay_ms,
594 int system_delay_ms,
595 int delay_min,
596 int delay_max) {
Artem Titov0b489302021-07-28 20:50:03 +0200597 // The `revframe_` and `frame_` should include the proper frame information,
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000598 // hence can be used for extracting information.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100599 Int16FrameData tmp_frame;
600 std::queue<Int16FrameData*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000601 bool causal = true;
602
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200603 tmp_frame.CopyFrom(revframe_);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000604 SetFrameTo(&tmp_frame, 0);
605
606 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
Artem Titov0b489302021-07-28 20:50:03 +0200607 // Initialize the `frame_queue` with empty frames.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000608 int frame_delay = delay_ms / 10;
609 while (frame_delay < 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100610 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000611 frame->CopyFrom(tmp_frame);
612 frame_queue.push(frame);
613 frame_delay++;
614 causal = false;
615 }
616 while (frame_delay > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100617 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000618 frame->CopyFrom(tmp_frame);
619 frame_queue.push(frame);
620 frame_delay--;
621 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000622 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
623 // need enough frames with audio to have reliable estimates, but as few as
624 // possible to keep processing time down. 4.5 seconds seemed to be a good
625 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000626 for (int frame_count = 0; frame_count < 450; ++frame_count) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100627 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000628 frame->CopyFrom(tmp_frame);
629 // Use the near end recording, since that has more speech in it.
630 ASSERT_TRUE(ReadFrame(near_file_, frame));
631 frame_queue.push(frame);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100632 Int16FrameData* reverse_frame = frame;
633 Int16FrameData* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000634 if (!causal) {
635 reverse_frame = frame_queue.front();
636 // When we call ProcessStream() the frame is modified, so we can't use the
637 // pointer directly when things are non-causal. Use an intermediate frame
638 // and copy the data.
639 process_frame = &tmp_frame;
640 process_frame->CopyFrom(*frame);
641 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100642 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(
643 reverse_frame->data.data(),
644 StreamConfig(reverse_frame->sample_rate_hz,
645 reverse_frame->num_channels),
646 StreamConfig(reverse_frame->sample_rate_hz,
647 reverse_frame->num_channels),
648 reverse_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000649 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100650 EXPECT_EQ(apm_->kNoError,
651 apm_->ProcessStream(process_frame->data.data(),
652 StreamConfig(process_frame->sample_rate_hz,
653 process_frame->num_channels),
654 StreamConfig(process_frame->sample_rate_hz,
655 process_frame->num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100656 process_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000657 frame = frame_queue.front();
658 frame_queue.pop();
659 delete frame;
660
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000661 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 // Discard the first delay metrics to avoid convergence effects.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100663 static_cast<void>(apm_->GetStatistics());
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 }
665 }
666
667 rewind(near_file_);
668 while (!frame_queue.empty()) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100669 Int16FrameData* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000670 frame_queue.pop();
671 delete frame;
672 }
673 // Calculate expected delay estimate and acceptable regions. Further,
674 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700675 const size_t samples_per_ms =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100676 rtc::SafeMin<size_t>(16u, frame_.samples_per_channel / 10);
kwiberg07038562017-06-12 11:40:47 -0700677 const int expected_median =
678 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
679 const int expected_median_high = rtc::SafeClamp<int>(
680 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700682 const int expected_median_low = rtc::SafeClamp<int>(
683 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700684 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000685 // Verify delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100686 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200687 ASSERT_TRUE(stats.delay_median_ms.has_value());
688 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000689 EXPECT_GE(expected_median_high, median);
690 EXPECT_LE(expected_median_low, median);
691}
692
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000693void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000695 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000696
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000697 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200698 AudioProcessing::Config apm_config = apm_->GetConfig();
699 apm_config.gain_controller1.enabled = true;
700 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200701 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000702
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000703 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200704 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000705 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200706 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000707
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200709 apm_config.echo_canceller.enabled = true;
710 apm_config.echo_canceller.mobile_mode = false;
711 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200713 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200714 apm_config.gain_controller1.enabled = false;
715 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716
717 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000718 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100719 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000720
721 // Resets after successful ProcessStream().
722 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100724 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725
726 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200727 apm_config.gain_controller1.enabled = true;
728 apm_->ApplyConfig(apm_config);
729 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100730 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200731 apm_config.gain_controller1.enabled = false;
732 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000733
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000734 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200735 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100736 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000737
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000738 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200740 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000741 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000742}
743
744TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000745 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000746}
747
748TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000749 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000750}
751
Michael Graczyk86c6d332015-07-23 11:41:39 -0700752void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800753 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700754 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100755 frame_.num_channels = num_channels;
756
757 EXPECT_EQ(expected_return,
758 apm_->ProcessStream(
759 frame_.data.data(),
760 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
761 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100762 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100763 EXPECT_EQ(expected_return,
764 apm_->ProcessReverseStream(
765 frame_.data.data(),
766 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
767 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
768 frame_.data.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000769}
770
Michael Graczyk86c6d332015-07-23 11:41:39 -0700771void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800772 size_t num_in_channels,
773 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700774 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100775 const StreamConfig input_stream = {frame_.sample_rate_hz, num_in_channels};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700776 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
777
778 EXPECT_EQ(expected_return,
779 apm_->ProcessStream(float_cb_->channels(), input_stream,
780 output_stream, float_cb_->channels()));
781}
782
783void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800784 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700785 AudioProcessing::Error expected_return) {
786 const ProcessingConfig processing_config = {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100787 {{frame_.sample_rate_hz, apm_->num_input_channels()},
ekmeyerson60d9b332015-08-14 10:35:55 -0700788 {output_sample_rate_hz_, apm_->num_output_channels()},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100789 {frame_.sample_rate_hz, num_rev_channels},
790 {frame_.sample_rate_hz, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700791
ekmeyerson60d9b332015-08-14 10:35:55 -0700792 EXPECT_EQ(
793 expected_return,
794 apm_->ProcessReverseStream(
795 float_cb_->channels(), processing_config.reverse_input_stream(),
796 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700797}
798
799TEST_F(ApmTest, ChannelsInt16Interface) {
800 // Testing number of invalid and valid channels.
801 Init(16000, 16000, 16000, 4, 4, 4, false);
802
803 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
804
Peter Kasting69558702016-01-12 16:26:35 -0800805 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700806 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000808 }
809}
810
Michael Graczyk86c6d332015-07-23 11:41:39 -0700811TEST_F(ApmTest, Channels) {
812 // Testing number of invalid and valid channels.
813 Init(16000, 16000, 16000, 4, 4, 4, false);
814
815 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
816 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
817
Peter Kasting69558702016-01-12 16:26:35 -0800818 for (size_t i = 1; i < 4; ++i) {
819 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700820 // Output channels much be one or match input channels.
821 if (j == 1 || i == j) {
822 TestChangingForwardChannels(i, j, kNoErr);
823 TestChangingReverseChannels(i, kNoErr);
824
825 EXPECT_EQ(i, apm_->num_input_channels());
826 EXPECT_EQ(j, apm_->num_output_channels());
827 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800828 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700829 } else {
830 TestChangingForwardChannels(i, j,
831 AudioProcessing::kBadNumberChannelsError);
832 }
833 }
834 }
835}
836
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000837TEST_F(ApmTest, SampleRatesInt) {
Sam Zackrisson12e319a2020-01-03 14:54:20 +0100838 // Testing some valid sample rates.
839 for (int sample_rate : {8000, 12000, 16000, 32000, 44100, 48000, 96000}) {
840 SetContainerFormat(sample_rate, 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000841 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000842 }
843}
844
Sam Zackrissone277bde2019-10-25 10:07:54 +0200845// This test repeatedly reconfigures the pre-amplifier in APM, processes a
846// number of frames, and checks that output signal has the right level.
847TEST_F(ApmTest, PreAmplifier) {
848 // Fill the audio frame with a sawtooth pattern.
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200849 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100850 const size_t samples_per_channel = frame_.samples_per_channel;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200851 for (size_t i = 0; i < samples_per_channel; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100852 for (size_t ch = 0; ch < frame_.num_channels; ++ch) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200853 frame_data[i + ch * samples_per_channel] = 10000 * ((i % 3) - 1);
854 }
855 }
856 // Cache the frame in tmp_frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100857 Int16FrameData tmp_frame;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200858 tmp_frame.CopyFrom(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200859
Per Åhgren2507f8c2020-03-19 12:33:29 +0100860 auto compute_power = [](const Int16FrameData& frame) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200861 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
862 return std::accumulate(data.begin(), data.end(), 0.0f,
863 [](float a, float b) { return a + b * b; }) /
864 data.size() / 32768 / 32768;
865 };
866
867 const float input_power = compute_power(tmp_frame);
868 // Double-check that the input data is large compared to the error kEpsilon.
869 constexpr float kEpsilon = 1e-4f;
870 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
871
872 // 1. Enable pre-amp with 0 dB gain.
873 AudioProcessing::Config config = apm_->GetConfig();
874 config.pre_amplifier.enabled = true;
875 config.pre_amplifier.fixed_gain_factor = 1.0f;
876 apm_->ApplyConfig(config);
877
878 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200879 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200880 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
881 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200882 float output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200883 EXPECT_NEAR(output_power, input_power, kEpsilon);
884 config = apm_->GetConfig();
885 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.0f);
886
887 // 2. Change pre-amp gain via ApplyConfig.
888 config.pre_amplifier.fixed_gain_factor = 2.0f;
889 apm_->ApplyConfig(config);
890
891 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200892 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200893 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
894 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200895 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200896 EXPECT_NEAR(output_power, 4 * input_power, kEpsilon);
897 config = apm_->GetConfig();
898 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 2.0f);
899
900 // 3. Change pre-amp gain via a RuntimeSetting.
901 apm_->SetRuntimeSetting(
902 AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.5f));
903
904 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200905 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200906 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
907 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200908 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200909 EXPECT_NEAR(output_power, 2.25 * input_power, kEpsilon);
910 config = apm_->GetConfig();
911 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.5f);
912}
913
Per Åhgrendb5d7282021-03-15 16:31:04 +0000914// This test a simple test that ensures that the emulated analog mic gain
915// functionality runs without crashing.
916TEST_F(ApmTest, AnalogMicGainEmulation) {
917 // Fill the audio frame with a sawtooth pattern.
918 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
919 const size_t samples_per_channel = frame_.samples_per_channel;
920 for (size_t i = 0; i < samples_per_channel; i++) {
921 for (size_t ch = 0; ch < frame_.num_channels; ++ch) {
922 frame_data[i + ch * samples_per_channel] = 100 * ((i % 3) - 1);
923 }
924 }
925 // Cache the frame in tmp_frame.
926 Int16FrameData tmp_frame;
927 tmp_frame.CopyFrom(frame_);
928
929 // Enable the analog gain emulation.
930 AudioProcessing::Config config = apm_->GetConfig();
931 config.capture_level_adjustment.enabled = true;
932 config.capture_level_adjustment.analog_mic_gain_emulation.enabled = true;
933 config.capture_level_adjustment.analog_mic_gain_emulation.initial_level = 21;
934 config.gain_controller1.enabled = true;
935 config.gain_controller1.mode =
936 AudioProcessing::Config::GainController1::Mode::kAdaptiveAnalog;
937 config.gain_controller1.analog_gain_controller.enabled = true;
938 apm_->ApplyConfig(config);
939
940 // Process a number of frames to ensure that the code runs without crashes.
941 for (int i = 0; i < 20; ++i) {
942 frame_.CopyFrom(tmp_frame);
943 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
944 }
945}
946
947// This test repeatedly reconfigures the capture level adjustment functionality
948// in APM, processes a number of frames, and checks that output signal has the
949// right level.
950TEST_F(ApmTest, CaptureLevelAdjustment) {
951 // Fill the audio frame with a sawtooth pattern.
952 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
953 const size_t samples_per_channel = frame_.samples_per_channel;
954 for (size_t i = 0; i < samples_per_channel; i++) {
955 for (size_t ch = 0; ch < frame_.num_channels; ++ch) {
956 frame_data[i + ch * samples_per_channel] = 100 * ((i % 3) - 1);
957 }
958 }
959 // Cache the frame in tmp_frame.
960 Int16FrameData tmp_frame;
961 tmp_frame.CopyFrom(frame_);
962
963 auto compute_power = [](const Int16FrameData& frame) {
964 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
965 return std::accumulate(data.begin(), data.end(), 0.0f,
966 [](float a, float b) { return a + b * b; }) /
967 data.size() / 32768 / 32768;
968 };
969
970 const float input_power = compute_power(tmp_frame);
971 // Double-check that the input data is large compared to the error kEpsilon.
972 constexpr float kEpsilon = 1e-20f;
973 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
974
975 // 1. Enable pre-amp with 0 dB gain.
976 AudioProcessing::Config config = apm_->GetConfig();
977 config.capture_level_adjustment.enabled = true;
978 config.capture_level_adjustment.pre_gain_factor = 0.5f;
979 config.capture_level_adjustment.post_gain_factor = 4.f;
980 const float expected_output_power1 =
981 config.capture_level_adjustment.pre_gain_factor *
982 config.capture_level_adjustment.pre_gain_factor *
983 config.capture_level_adjustment.post_gain_factor *
984 config.capture_level_adjustment.post_gain_factor * input_power;
985 apm_->ApplyConfig(config);
986
987 for (int i = 0; i < 20; ++i) {
988 frame_.CopyFrom(tmp_frame);
989 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
990 }
991 float output_power = compute_power(frame_);
992 EXPECT_NEAR(output_power, expected_output_power1, kEpsilon);
993 config = apm_->GetConfig();
994 EXPECT_EQ(config.capture_level_adjustment.pre_gain_factor, 0.5f);
995 EXPECT_EQ(config.capture_level_adjustment.post_gain_factor, 4.f);
996
997 // 2. Change pre-amp gain via ApplyConfig.
998 config.capture_level_adjustment.pre_gain_factor = 1.0f;
999 config.capture_level_adjustment.post_gain_factor = 2.f;
1000 const float expected_output_power2 =
1001 config.capture_level_adjustment.pre_gain_factor *
1002 config.capture_level_adjustment.pre_gain_factor *
1003 config.capture_level_adjustment.post_gain_factor *
1004 config.capture_level_adjustment.post_gain_factor * input_power;
1005 apm_->ApplyConfig(config);
1006
1007 for (int i = 0; i < 20; ++i) {
1008 frame_.CopyFrom(tmp_frame);
1009 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
1010 }
1011 output_power = compute_power(frame_);
1012 EXPECT_NEAR(output_power, expected_output_power2, kEpsilon);
1013 config = apm_->GetConfig();
1014 EXPECT_EQ(config.capture_level_adjustment.pre_gain_factor, 1.0f);
1015 EXPECT_EQ(config.capture_level_adjustment.post_gain_factor, 2.f);
1016
1017 // 3. Change pre-amp gain via a RuntimeSetting.
1018 constexpr float kPreGain3 = 0.5f;
1019 constexpr float kPostGain3 = 3.f;
1020 const float expected_output_power3 =
1021 kPreGain3 * kPreGain3 * kPostGain3 * kPostGain3 * input_power;
1022
1023 apm_->SetRuntimeSetting(
1024 AudioProcessing::RuntimeSetting::CreateCapturePreGain(kPreGain3));
1025 apm_->SetRuntimeSetting(
1026 AudioProcessing::RuntimeSetting::CreateCapturePostGain(kPostGain3));
1027
1028 for (int i = 0; i < 20; ++i) {
1029 frame_.CopyFrom(tmp_frame);
1030 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
1031 }
1032 output_power = compute_power(frame_);
1033 EXPECT_NEAR(output_power, expected_output_power3, kEpsilon);
1034 config = apm_->GetConfig();
1035 EXPECT_EQ(config.capture_level_adjustment.pre_gain_factor, 0.5f);
1036 EXPECT_EQ(config.capture_level_adjustment.post_gain_factor, 3.f);
1037}
1038
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001039TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001040 AudioProcessing::Config config = apm_->GetConfig();
1041 config.gain_controller1.enabled = false;
1042 apm_->ApplyConfig(config);
1043 config.gain_controller1.enabled = true;
1044 apm_->ApplyConfig(config);
1045
niklase@google.com470e71d2011-07-07 08:21:25 +00001046 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +02001047 for (auto mode :
1048 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
1049 AudioProcessing::Config::GainController1::kFixedDigital,
1050 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
1051 config.gain_controller1.mode = mode;
1052 apm_->ApplyConfig(config);
1053 apm_->set_stream_analog_level(100);
1054 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001056
Sam Zackrisson41478c72019-10-15 10:10:26 +02001057 // Testing target levels
1058 for (int target_level_dbfs : {0, 15, 31}) {
1059 config.gain_controller1.target_level_dbfs = target_level_dbfs;
1060 apm_->ApplyConfig(config);
1061 apm_->set_stream_analog_level(100);
1062 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +00001063 }
1064
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001065 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +02001066 for (int compression_gain_db : {0, 10, 90}) {
1067 config.gain_controller1.compression_gain_db = compression_gain_db;
1068 apm_->ApplyConfig(config);
1069 apm_->set_stream_analog_level(100);
1070 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 }
1072
1073 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +02001074 for (bool enable : {false, true}) {
1075 config.gain_controller1.enable_limiter = enable;
1076 apm_->ApplyConfig(config);
1077 apm_->set_stream_analog_level(100);
1078 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
1079 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001080
Hanna Silencd597042021-11-02 11:02:48 +01001081 // Testing level limits.
1082 constexpr int kMinLevel = 0;
1083 constexpr int kMaxLevel = 255;
1084 apm_->set_stream_analog_level(kMinLevel);
1085 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
1086 apm_->set_stream_analog_level((kMinLevel + kMaxLevel) / 2);
1087 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
1088 apm_->set_stream_analog_level(kMaxLevel);
1089 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +00001090}
1091
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001092#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Tommia5e07cc2020-05-26 21:40:37 +02001093using ApmDeathTest = ApmTest;
1094
1095TEST_F(ApmDeathTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001096 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001097 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001098 config.gain_controller1.target_level_dbfs = -1;
1099 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001100}
1101
Tommia5e07cc2020-05-26 21:40:37 +02001102TEST_F(ApmDeathTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001103 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001104 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001105 config.gain_controller1.target_level_dbfs = 32;
1106 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001107}
1108
Tommia5e07cc2020-05-26 21:40:37 +02001109TEST_F(ApmDeathTest, GainControlDiesOnTooLowCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001110 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001111 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001112 config.gain_controller1.compression_gain_db = -1;
1113 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001114}
1115
Tommia5e07cc2020-05-26 21:40:37 +02001116TEST_F(ApmDeathTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001117 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001118 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001119 config.gain_controller1.compression_gain_db = 91;
1120 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001121}
1122
Tommia5e07cc2020-05-26 21:40:37 +02001123TEST_F(ApmDeathTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001124 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001125 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001126 apm_->ApplyConfig(config);
Hanna Silencd597042021-11-02 11:02:48 +01001127 EXPECT_DEATH(apm_->set_stream_analog_level(-1), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001128}
1129
Tommia5e07cc2020-05-26 21:40:37 +02001130TEST_F(ApmDeathTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001131 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001132 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001133 apm_->ApplyConfig(config);
Hanna Silencd597042021-11-02 11:02:48 +01001134 EXPECT_DEATH(apm_->set_stream_analog_level(256), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001135}
1136#endif
1137
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001138void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001139 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001140 auto config = apm_->GetConfig();
1141 config.gain_controller1.enabled = true;
1142 config.gain_controller1.mode =
1143 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1144 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001145
1146 int out_analog_level = 0;
1147 for (int i = 0; i < 2000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001148 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001149 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001150 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001151
1152 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +02001153 apm_->set_stream_analog_level(100);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001154 EXPECT_EQ(apm_->kNoError,
1155 apm_->ProcessStream(
1156 frame_.data.data(),
1157 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1158 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001159 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001160 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001161 }
1162
1163 // Ensure the AGC is still able to reach the maximum.
1164 EXPECT_EQ(255, out_analog_level);
1165}
1166
1167// Verifies that despite volume slider quantization, the AGC can continue to
1168// increase its volume.
1169TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001170 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001171 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1172 }
1173}
1174
1175void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001176 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001177 auto config = apm_->GetConfig();
1178 config.gain_controller1.enabled = true;
1179 config.gain_controller1.mode =
1180 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1181 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001182
1183 int out_analog_level = 100;
1184 for (int i = 0; i < 1000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001185 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001186 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001187 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001188
Sam Zackrisson41478c72019-10-15 10:10:26 +02001189 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001190 EXPECT_EQ(apm_->kNoError,
1191 apm_->ProcessStream(
1192 frame_.data.data(),
1193 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1194 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001195 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001196 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001197 }
1198
1199 // Ensure the volume was raised.
1200 EXPECT_GT(out_analog_level, 100);
1201 int highest_level_reached = out_analog_level;
1202 // Simulate a user manual volume change.
1203 out_analog_level = 100;
1204
1205 for (int i = 0; i < 300; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001206 ReadFrameWithRewind(near_file_, &frame_);
1207 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001208
Sam Zackrisson41478c72019-10-15 10:10:26 +02001209 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001210 EXPECT_EQ(apm_->kNoError,
1211 apm_->ProcessStream(
1212 frame_.data.data(),
1213 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1214 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001215 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001216 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001217 // Check that AGC respected the manually adjusted volume.
1218 EXPECT_LT(out_analog_level, highest_level_reached);
1219 }
1220 // Check that the volume was still raised.
1221 EXPECT_GT(out_analog_level, 100);
1222}
1223
1224TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001225 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001226 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1227 }
1228}
1229
niklase@google.com470e71d2011-07-07 08:21:25 +00001230TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001231 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001232 AudioProcessing::Config apm_config;
1233 apm_config.high_pass_filter.enabled = true;
1234 apm_->ApplyConfig(apm_config);
1235 apm_config.high_pass_filter.enabled = false;
1236 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001237}
1238
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001239TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001240 AudioProcessing::Config config = apm_->GetConfig();
1241 EXPECT_FALSE(config.echo_canceller.enabled);
1242 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001243 EXPECT_FALSE(config.gain_controller1.enabled);
saza0bad15f2019-10-16 11:46:11 +02001244 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001245 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001246}
1247
1248TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001249 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001250 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001251 SetFrameTo(&frame_, 1000, 2000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001252 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001253 frame_copy.CopyFrom(frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001254 for (int j = 0; j < 1000; j++) {
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));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001262 EXPECT_EQ(apm_->kNoError,
1263 apm_->ProcessReverseStream(
1264 frame_.data.data(),
1265 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1266 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1267 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001268 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001269 }
1270 }
1271}
1272
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001273TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1274 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001275 const size_t kSamples = 160;
1276 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001277 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001278 float dest[kSamples] = {};
1279
1280 auto src_channels = &src[0];
1281 auto dest_channels = &dest[0];
1282
Niels Möller4f776ac2021-07-02 11:30:54 +02001283 apm_ = AudioProcessingBuilderForTesting().Create();
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001284 EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
1285 StreamConfig(sample_rate, 1),
1286 &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001287
1288 for (size_t i = 0; i < kSamples; ++i) {
1289 EXPECT_EQ(src[i], dest[i]);
1290 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001291
1292 // Same for ProcessReverseStream.
1293 float rev_dest[kSamples] = {};
1294 auto rev_dest_channels = &rev_dest[0];
1295
1296 StreamConfig input_stream = {sample_rate, 1};
1297 StreamConfig output_stream = {sample_rate, 1};
1298 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1299 output_stream, &rev_dest_channels));
1300
1301 for (size_t i = 0; i < kSamples; ++i) {
1302 EXPECT_EQ(src[i], rev_dest[i]);
1303 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001304}
1305
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001306TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1307 EnableAllComponents();
1308
pkasting25702cb2016-01-08 13:50:27 -08001309 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001310 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1311 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001312 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001313 ASSERT_EQ(0, feof(far_file_));
1314 ASSERT_EQ(0, feof(near_file_));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001315 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001316 CopyLeftToRightChannel(revframe_.data.data(),
1317 revframe_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001318
Per Åhgren2507f8c2020-03-19 12:33:29 +01001319 ASSERT_EQ(
1320 kNoErr,
1321 apm_->ProcessReverseStream(
1322 revframe_.data.data(),
1323 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1324 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1325 revframe_.data.data()));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001326
Per Åhgren2507f8c2020-03-19 12:33:29 +01001327 CopyLeftToRightChannel(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001328
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001329 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001330 apm_->set_stream_analog_level(analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001331 ASSERT_EQ(kNoErr,
1332 apm_->ProcessStream(
1333 frame_.data.data(),
1334 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1335 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001336 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001337 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001338
Per Åhgren2507f8c2020-03-19 12:33:29 +01001339 VerifyChannelsAreEqual(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001340 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001341 rewind(far_file_);
1342 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001343 }
1344}
1345
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001346TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001347 // Verify the filter is not active through undistorted audio when:
1348 // 1. No components are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001349 SetFrameTo(&frame_, 1000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001350 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001351 frame_copy.CopyFrom(frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001352 EXPECT_EQ(apm_->kNoError,
1353 apm_->ProcessStream(
1354 frame_.data.data(),
1355 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1356 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001357 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001358 EXPECT_EQ(apm_->kNoError,
1359 apm_->ProcessStream(
1360 frame_.data.data(),
1361 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1362 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001363 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001364 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001365
1366 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001367 auto apm_config = apm_->GetConfig();
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001368 SetFrameTo(&frame_, 1000);
1369 frame_copy.CopyFrom(frame_);
saza6787f232019-10-11 19:31:07 +02001370 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001371 EXPECT_EQ(apm_->kNoError,
1372 apm_->ProcessStream(
1373 frame_.data.data(),
1374 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1375 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001376 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001377 EXPECT_EQ(apm_->kNoError,
1378 apm_->ProcessStream(
1379 frame_.data.data(),
1380 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1381 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001382 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001383 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001384 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001385
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001386 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001387 SetFrameTo(&frame_, 1000);
1388 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001389 apm_config.voice_detection.enabled = true;
1390 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001391 EXPECT_EQ(apm_->kNoError,
1392 apm_->ProcessStream(
1393 frame_.data.data(),
1394 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1395 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001396 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001397 EXPECT_EQ(apm_->kNoError,
1398 apm_->ProcessStream(
1399 frame_.data.data(),
1400 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1401 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001402 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001403 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001404 apm_config.voice_detection.enabled = false;
1405 apm_->ApplyConfig(apm_config);
1406
Alessio Bazzica183c64c2021-10-19 13:32:02 +02001407 // 4. The VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001408 SetFrameTo(&frame_, 1000);
1409 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001410 apm_config.voice_detection.enabled = true;
1411 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001412 EXPECT_EQ(apm_->kNoError,
1413 apm_->ProcessStream(
1414 frame_.data.data(),
1415 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1416 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001417 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001418 EXPECT_EQ(apm_->kNoError,
1419 apm_->ProcessStream(
1420 frame_.data.data(),
1421 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1422 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001423 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001424 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001425 apm_config.voice_detection.enabled = false;
1426 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001427
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001428 // Check the test is valid. We should have distortion from the filter
1429 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001430 apm_config.echo_canceller.enabled = true;
1431 apm_config.echo_canceller.mobile_mode = false;
1432 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001433 frame_.samples_per_channel = 320;
1434 frame_.num_channels = 2;
1435 frame_.sample_rate_hz = 32000;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001436 SetFrameTo(&frame_, 1000);
1437 frame_copy.CopyFrom(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001438 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001439 EXPECT_EQ(apm_->kNoError,
1440 apm_->ProcessStream(
1441 frame_.data.data(),
1442 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1443 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001444 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001445 EXPECT_FALSE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001446}
1447
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001448#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1449void ApmTest::ProcessDebugDump(const std::string& in_filename,
1450 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001451 Format format,
1452 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001453 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001454 FILE* in_file = fopen(in_filename.c_str(), "rb");
1455 ASSERT_TRUE(in_file != NULL);
1456 audioproc::Event event_msg;
1457 bool first_init = true;
1458
1459 while (ReadMessageFromFile(in_file, &event_msg)) {
1460 if (event_msg.type() == audioproc::Event::INIT) {
1461 const audioproc::Init msg = event_msg.init();
1462 int reverse_sample_rate = msg.sample_rate();
1463 if (msg.has_reverse_sample_rate()) {
1464 reverse_sample_rate = msg.reverse_sample_rate();
1465 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001466 int output_sample_rate = msg.sample_rate();
1467 if (msg.has_output_sample_rate()) {
1468 output_sample_rate = msg.output_sample_rate();
1469 }
1470
Jonas Olssona4d87372019-07-05 19:08:33 +02001471 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1472 msg.num_input_channels(), msg.num_output_channels(),
1473 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001474 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001475 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001476 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001477 auto aec_dump =
1478 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1479 EXPECT_TRUE(aec_dump);
1480 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001481 first_init = false;
1482 }
1483
1484 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1485 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1486
1487 if (msg.channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001488 ASSERT_EQ(revframe_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001489 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001490 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001491 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1492 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001493 }
1494 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001495 memcpy(revframe_.data.data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001496 if (format == kFloatFormat) {
1497 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001498 ConvertToFloat(revframe_, revfloat_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001499 }
1500 }
1501 AnalyzeReverseStreamChooser(format);
1502
1503 } else if (event_msg.type() == audioproc::Event::STREAM) {
1504 const audioproc::Stream msg = event_msg.stream();
1505 // ProcessStream could have changed this for the output frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001506 frame_.num_channels = apm_->num_input_channels();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001507
Sam Zackrisson41478c72019-10-15 10:10:26 +02001508 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001509 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001510 if (msg.has_keypress()) {
1511 apm_->set_stream_key_pressed(msg.keypress());
1512 } else {
1513 apm_->set_stream_key_pressed(true);
1514 }
1515
1516 if (msg.input_channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001517 ASSERT_EQ(frame_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001518 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001519 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001520 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1521 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001522 }
1523 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001524 memcpy(frame_.data.data(), msg.input_data().data(),
yujo36b1a5f2017-06-12 12:45:32 -07001525 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001526 if (format == kFloatFormat) {
1527 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001528 ConvertToFloat(frame_, float_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001529 }
1530 }
1531 ProcessStreamChooser(format);
1532 }
1533 }
aleloif4dd1912017-06-15 01:55:38 -07001534 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001535 fclose(in_file);
1536}
1537
1538void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001539 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001540 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001541 std::string format_string;
1542 switch (format) {
1543 case kIntFormat:
1544 format_string = "_int";
1545 break;
1546 case kFloatFormat:
1547 format_string = "_float";
1548 break;
1549 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001550 const std::string ref_filename = test::TempFilename(
1551 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1552 const std::string out_filename = test::TempFilename(
1553 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001554 const std::string limited_filename = test::TempFilename(
1555 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1556 const size_t logging_limit_bytes = 100000;
1557 // We expect at least this many bytes in the created logfile.
1558 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001559 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001560 ProcessDebugDump(in_filename, ref_filename, format, -1);
1561 ProcessDebugDump(ref_filename, out_filename, format, -1);
1562 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001563
1564 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1565 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001566 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001567 ASSERT_TRUE(ref_file != NULL);
1568 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001569 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001570 std::unique_ptr<uint8_t[]> ref_bytes;
1571 std::unique_ptr<uint8_t[]> out_bytes;
1572 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001573
1574 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1575 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001576 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001577 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001578 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001579 while (ref_size > 0 && out_size > 0) {
1580 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001581 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001582 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001583 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001584 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001585 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001586 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1587 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001588 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001589 }
1590 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001591 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1592 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001593 EXPECT_NE(0, feof(ref_file));
1594 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001595 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001596 ASSERT_EQ(0, fclose(ref_file));
1597 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001598 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001599 remove(ref_filename.c_str());
1600 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001601 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001602}
1603
pbosc7a65692016-05-06 12:50:04 -07001604TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001605 VerifyDebugDumpTest(kIntFormat);
1606}
1607
pbosc7a65692016-05-06 12:50:04 -07001608TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001609 VerifyDebugDumpTest(kFloatFormat);
1610}
1611#endif
1612
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001613// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001614TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001615 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001616 const std::string filename =
1617 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001618 {
1619 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1620 EXPECT_FALSE(aec_dump);
1621 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001622
1623#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1624 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001625 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001626
aleloif4dd1912017-06-15 01:55:38 -07001627 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1628 EXPECT_TRUE(aec_dump);
1629 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001630 EXPECT_EQ(apm_->kNoError,
1631 apm_->ProcessStream(
1632 frame_.data.data(),
1633 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1634 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001635 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001636 EXPECT_EQ(apm_->kNoError,
1637 apm_->ProcessReverseStream(
1638 revframe_.data.data(),
1639 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1640 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1641 revframe_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001642 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001643
1644 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001645 FILE* fid = fopen(filename.c_str(), "r");
1646 ASSERT_TRUE(fid != NULL);
1647
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001648 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001649 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001650 ASSERT_EQ(0, remove(filename.c_str()));
1651#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001652 // Verify the file has NOT been written.
1653 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1654#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1655}
1656
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001657// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001658TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001659 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001660
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001661 const std::string filename =
1662 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001663 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1664 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001665
1666#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1667 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001668 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001669
Niels Möllere8e4dc42019-06-11 14:04:16 +02001670 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001671 EXPECT_TRUE(aec_dump);
1672 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001673 EXPECT_EQ(apm_->kNoError,
1674 apm_->ProcessReverseStream(
1675 revframe_.data.data(),
1676 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1677 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1678 revframe_.data.data()));
1679 EXPECT_EQ(apm_->kNoError,
1680 apm_->ProcessStream(
1681 frame_.data.data(),
1682 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1683 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001684 frame_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001685 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001686
1687 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001688 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001689 ASSERT_TRUE(fid != NULL);
1690
1691 // Clean it up.
1692 ASSERT_EQ(0, fclose(fid));
1693 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001694#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1695}
1696
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001697// TODO(andrew): Add a test to process a few frames with different combinations
1698// of enabled components.
1699
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001700TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001701 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001702 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001703
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001704 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001705 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001706 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001707 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001708 for (size_t i = 0; i < arraysize(kChannels); i++) {
1709 for (size_t j = 0; j < arraysize(kChannels); j++) {
1710 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001711 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001712 test->set_num_reverse_channels(kChannels[i]);
1713 test->set_num_input_channels(kChannels[j]);
1714 test->set_num_output_channels(kChannels[j]);
1715 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001716 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001717 }
1718 }
1719 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001720#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1721 // To test the extended filter mode.
1722 audioproc::Test* test = ref_data.add_test();
1723 test->set_num_reverse_channels(2);
1724 test->set_num_input_channels(2);
1725 test->set_num_output_channels(2);
1726 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1727 test->set_use_aec_extended_filter(true);
1728#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001729 }
1730
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001731 for (int i = 0; i < ref_data.test_size(); i++) {
1732 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001733
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001734 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001735 // TODO(ajm): We no longer allow different input and output channels. Skip
1736 // these tests for now, but they should be removed from the set.
1737 if (test->num_input_channels() != test->num_output_channels())
1738 continue;
1739
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01001740 apm_ = AudioProcessingBuilderForTesting()
1741 .SetEchoDetector(CreateEchoDetector())
1742 .Create();
Per Åhgren0695df12020-01-13 14:43:13 +01001743 AudioProcessing::Config apm_config = apm_->GetConfig();
1744 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1745 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001746
1747 EnableAllComponents();
1748
Jonas Olssona4d87372019-07-05 19:08:33 +02001749 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001750 static_cast<size_t>(test->num_input_channels()),
1751 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001752 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001753
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001754 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001755 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001756 int analog_level = 127;
1757 int analog_level_average = 0;
1758 int max_output_average = 0;
minyue58530ed2016-05-24 05:50:12 -07001759#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001760 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001761#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001762
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001763 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001764 EXPECT_EQ(
1765 apm_->kNoError,
1766 apm_->ProcessReverseStream(
1767 revframe_.data.data(),
1768 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1769 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1770 revframe_.data.data()));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001771
1772 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001773 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001774
Per Åhgren2507f8c2020-03-19 12:33:29 +01001775 EXPECT_EQ(apm_->kNoError,
1776 apm_->ProcessStream(
1777 frame_.data.data(),
1778 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1779 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001780 frame_.data.data()));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001781
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001782 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001783 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01001784 frame_.num_channels);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001785
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001786 max_output_average += MaxAudioFrame(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001787
Sam Zackrisson41478c72019-10-15 10:10:26 +02001788 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001789 analog_level_average += analog_level;
Per Åhgrencf4c8722019-12-30 14:32:14 +01001790 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001791 EXPECT_TRUE(stats.voice_detected);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001792 has_voice_count += *stats.voice_detected ? 1 : 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001793
Per Åhgren2507f8c2020-03-19 12:33:29 +01001794 size_t frame_size = frame_.samples_per_channel * frame_.num_channels;
Jonas Olssona4d87372019-07-05 19:08:33 +02001795 size_t write_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +01001796 fwrite(frame_.data.data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001797 ASSERT_EQ(frame_size, write_count);
1798
1799 // Reset in case of downmixing.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001800 frame_.num_channels = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001801 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001802
1803#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1804 const int kStatsAggregationFrameNum = 100; // 1 second.
1805 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001806 // Get echo and delay metrics.
Mirko Bonadei54c90f22021-10-03 11:26:11 +02001807 AudioProcessingStats stats2 = apm_->GetStatistics();
minyue58530ed2016-05-24 05:50:12 -07001808
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001809 // Echo metrics.
Mirko Bonadei54c90f22021-10-03 11:26:11 +02001810 const float echo_return_loss = stats2.echo_return_loss.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001811 const float echo_return_loss_enhancement =
Mirko Bonadei54c90f22021-10-03 11:26:11 +02001812 stats2.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001813 const float residual_echo_likelihood =
Mirko Bonadei54c90f22021-10-03 11:26:11 +02001814 stats2.residual_echo_likelihood.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001815 const float residual_echo_likelihood_recent_max =
Mirko Bonadei54c90f22021-10-03 11:26:11 +02001816 stats2.residual_echo_likelihood_recent_max.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001817
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001818 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001819 const audioproc::Test::EchoMetrics& reference =
1820 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001821 constexpr float kEpsilon = 0.01;
1822 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1823 EXPECT_NEAR(echo_return_loss_enhancement,
1824 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001825 EXPECT_NEAR(residual_echo_likelihood,
1826 reference.residual_echo_likelihood(), kEpsilon);
1827 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1828 reference.residual_echo_likelihood_recent_max(),
1829 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001830 ++stats_index;
1831 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001832 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1833 message_echo->set_echo_return_loss(echo_return_loss);
1834 message_echo->set_echo_return_loss_enhancement(
1835 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001836 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1837 message_echo->set_residual_echo_likelihood_recent_max(
1838 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001839 }
1840 }
1841#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001842 }
1843 max_output_average /= frame_count;
1844 analog_level_average /= frame_count;
1845
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001846 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001847 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001848 // When running the test on a N7 we get a {2, 6} difference of
Artem Titov0b489302021-07-28 20:50:03 +02001849 // `has_voice_count` and `max_output_average` is up to 18 higher.
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001850 // All numbers being consistently higher on N7 compare to ref_data.
1851 // TODO(bjornv): If we start getting more of these offsets on Android we
1852 // should consider a different approach. Either using one slack for all,
1853 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001854#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001855 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001856 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001857 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001858 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001859#else
1860 const int kHasVoiceCountOffset = 0;
1861 const int kHasVoiceCountNear = kIntNear;
1862 const int kMaxOutputAverageOffset = 0;
1863 const int kMaxOutputAverageNear = kIntNear;
1864#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001865 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001866 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001867
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001868 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001869 EXPECT_NEAR(test->max_output_average(),
1870 max_output_average - kMaxOutputAverageOffset,
1871 kMaxOutputAverageNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001872 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001873 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001874
1875 test->set_analog_level_average(analog_level_average);
1876 test->set_max_output_average(max_output_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001877 }
1878
1879 rewind(far_file_);
1880 rewind(near_file_);
1881 }
1882
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001883 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001884 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001885 }
1886}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001887
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001888TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1889 struct ChannelFormat {
1890 AudioProcessing::ChannelLayout in_layout;
1891 AudioProcessing::ChannelLayout out_layout;
1892 };
1893 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001894 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1895 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1896 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001897 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001898
Niels Möller4f776ac2021-07-02 11:30:54 +02001899 rtc::scoped_refptr<AudioProcessing> ap =
1900 AudioProcessingBuilderForTesting().Create();
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001901 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001902 AudioProcessing::Config config;
1903 config.noise_suppression.enabled = true;
1904 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001905 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001906 const int in_rate = 44100;
1907 const int out_rate = 48000;
1908 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1909 TotalChannelsFromLayout(cf[i].in_layout));
1910 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1911 ChannelsFromLayout(cf[i].out_layout));
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001912 bool has_keyboard = cf[i].in_layout == AudioProcessing::kMonoAndKeyboard ||
1913 cf[i].in_layout == AudioProcessing::kStereoAndKeyboard;
1914 StreamConfig in_sc(in_rate, ChannelsFromLayout(cf[i].in_layout),
1915 has_keyboard);
1916 StreamConfig out_sc(out_rate, ChannelsFromLayout(cf[i].out_layout));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001917
1918 // Run over a few chunks.
1919 for (int j = 0; j < 10; ++j) {
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001920 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_sc, out_sc,
1921 out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001922 }
1923 }
1924}
1925
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001926// Compares the reference and test arrays over a region around the expected
1927// delay. Finds the highest SNR in that region and adds the variance and squared
1928// error results to the supplied accumulators.
1929void UpdateBestSNR(const float* ref,
1930 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001931 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001932 int expected_delay,
1933 double* variance_acc,
1934 double* sq_error_acc) {
1935 double best_snr = std::numeric_limits<double>::min();
1936 double best_variance = 0;
1937 double best_sq_error = 0;
1938 // Search over a region of eight samples around the expected delay.
1939 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1940 ++delay) {
1941 double sq_error = 0;
1942 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001943 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001944 double error = test[i + delay] - ref[i];
1945 sq_error += error * error;
1946 variance += ref[i] * ref[i];
1947 }
1948
1949 if (sq_error == 0) {
1950 *variance_acc += variance;
1951 return;
1952 }
1953 double snr = variance / sq_error;
1954 if (snr > best_snr) {
1955 best_snr = snr;
1956 best_variance = variance;
1957 best_sq_error = sq_error;
1958 }
1959 }
1960
1961 *variance_acc += best_variance;
1962 *sq_error_acc += best_sq_error;
1963}
1964
1965// Used to test a multitude of sample rate and channel combinations. It works
1966// by first producing a set of reference files (in SetUpTestCase) that are
1967// assumed to be correct, as the used parameters are verified by other tests
1968// in this collection. Primarily the reference files are all produced at
1969// "native" rates which do not involve any resampling.
1970
1971// Each test pass produces an output file with a particular format. The output
1972// is matched against the reference file closest to its internal processing
1973// format. If necessary the output is resampled back to its process format.
1974// Due to the resampling distortion, we don't expect identical results, but
1975// enforce SNR thresholds which vary depending on the format. 0 is a special
1976// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001977typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001978class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001979 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001980 public:
1981 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001982 : input_rate_(std::get<0>(GetParam())),
1983 output_rate_(std::get<1>(GetParam())),
1984 reverse_input_rate_(std::get<2>(GetParam())),
1985 reverse_output_rate_(std::get<3>(GetParam())),
1986 expected_snr_(std::get<4>(GetParam())),
1987 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001988
1989 virtual ~AudioProcessingTest() {}
1990
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001991 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001992 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001993 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001994 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001995 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1996 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1997 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001998 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001999 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2000 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2001 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002002 }
2003 }
2004 }
2005 }
2006
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002007 void TearDown() {
2008 // Remove "out" files after each test.
2009 ClearTempOutFiles();
2010 }
2011
Mirko Bonadei71061bc2019-06-04 09:01:51 +02002012 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07002013
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002014 // Runs a process pass on files with the given parameters and dumps the output
Artem Titov0b489302021-07-28 20:50:03 +02002015 // to a file specified with `output_file_prefix`. Both forward and reverse
ekmeyerson60d9b332015-08-14 10:35:55 -07002016 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002017 static void ProcessFormat(int input_rate,
2018 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002019 int reverse_input_rate,
2020 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002021 size_t num_input_channels,
2022 size_t num_output_channels,
2023 size_t num_reverse_input_channels,
2024 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002025 const std::string& output_file_prefix) {
Niels Möller4f776ac2021-07-02 11:30:54 +02002026 rtc::scoped_refptr<AudioProcessing> ap =
2027 AudioProcessingBuilderForTesting().Create();
Per Åhgren0695df12020-01-13 14:43:13 +01002028 AudioProcessing::Config apm_config = ap->GetConfig();
2029 apm_config.gain_controller1.analog_gain_controller.enabled = false;
2030 ap->ApplyConfig(apm_config);
2031
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002032 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002033
ekmeyerson60d9b332015-08-14 10:35:55 -07002034 ProcessingConfig processing_config = {
2035 {{input_rate, num_input_channels},
2036 {output_rate, num_output_channels},
2037 {reverse_input_rate, num_reverse_input_channels},
2038 {reverse_output_rate, num_reverse_output_channels}}};
2039 ap->Initialize(processing_config);
2040
2041 FILE* far_file =
2042 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002043 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02002044 FILE* out_file = fopen(
2045 OutputFilePath(
2046 output_file_prefix, input_rate, output_rate, reverse_input_rate,
2047 reverse_output_rate, num_input_channels, num_output_channels,
2048 num_reverse_input_channels, num_reverse_output_channels, kForward)
2049 .c_str(),
2050 "wb");
2051 FILE* rev_out_file = fopen(
2052 OutputFilePath(
2053 output_file_prefix, input_rate, output_rate, reverse_input_rate,
2054 reverse_output_rate, num_input_channels, num_output_channels,
2055 num_reverse_input_channels, num_reverse_output_channels, kReverse)
2056 .c_str(),
2057 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002058 ASSERT_TRUE(far_file != NULL);
2059 ASSERT_TRUE(near_file != NULL);
2060 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002061 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002062
2063 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2064 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002065 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2066 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002067 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2068 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002069 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2070 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002071
2072 // Temporary buffers.
2073 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002074 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2075 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002076 std::unique_ptr<float[]> float_data(new float[max_length]);
2077 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002078
2079 int analog_level = 127;
2080 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2081 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002082 EXPECT_NOERR(ap->ProcessReverseStream(
2083 rev_cb.channels(), processing_config.reverse_input_stream(),
2084 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002085
2086 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02002087 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002088
2089 EXPECT_NOERR(ap->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +01002090 fwd_cb.channels(), StreamConfig(input_rate, num_input_channels),
2091 StreamConfig(output_rate, num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002092
ekmeyerson60d9b332015-08-14 10:35:55 -07002093 // Dump forward output to file.
2094 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002095 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002096 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002097
Jonas Olssona4d87372019-07-05 19:08:33 +02002098 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2099 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002100
ekmeyerson60d9b332015-08-14 10:35:55 -07002101 // Dump reverse output to file.
2102 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2103 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002104 size_t rev_out_length =
2105 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002106
Jonas Olssona4d87372019-07-05 19:08:33 +02002107 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2108 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07002109
Sam Zackrisson41478c72019-10-15 10:10:26 +02002110 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002111 }
2112 fclose(far_file);
2113 fclose(near_file);
2114 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002115 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002116 }
2117
2118 protected:
2119 int input_rate_;
2120 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002121 int reverse_input_rate_;
2122 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002123 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002124 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002125};
2126
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002127TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002128 struct ChannelFormat {
2129 int num_input;
2130 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002131 int num_reverse_input;
2132 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002133 };
2134 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02002135 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
2136 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002137 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002138
pkasting25702cb2016-01-08 13:50:27 -08002139 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002140 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2141 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2142 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002143
ekmeyerson60d9b332015-08-14 10:35:55 -07002144 // Verify output for both directions.
2145 std::vector<StreamDirection> stream_directions;
2146 stream_directions.push_back(kForward);
2147 stream_directions.push_back(kReverse);
2148 for (StreamDirection file_direction : stream_directions) {
2149 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2150 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2151 const int out_num =
2152 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2153 const double expected_snr =
2154 file_direction ? expected_reverse_snr_ : expected_snr_;
2155
2156 const int min_ref_rate = std::min(in_rate, out_rate);
2157 int ref_rate;
2158
2159 if (min_ref_rate > 32000) {
2160 ref_rate = 48000;
2161 } else if (min_ref_rate > 16000) {
2162 ref_rate = 32000;
2163 } else if (min_ref_rate > 8000) {
2164 ref_rate = 16000;
2165 } else {
2166 ref_rate = 8000;
2167 }
Per Åhgrenc0424252019-12-10 13:04:15 +01002168
ekmeyerson60d9b332015-08-14 10:35:55 -07002169 FILE* out_file = fopen(
2170 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2171 reverse_output_rate_, cf[i].num_input,
2172 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02002173 cf[i].num_reverse_output, file_direction)
2174 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07002175 "rb");
2176 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02002177 FILE* ref_file =
2178 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2179 cf[i].num_output, cf[i].num_output,
2180 cf[i].num_reverse_output,
2181 cf[i].num_reverse_output, file_direction)
2182 .c_str(),
2183 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002184 ASSERT_TRUE(out_file != NULL);
2185 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002186
pkasting25702cb2016-01-08 13:50:27 -08002187 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2188 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002189 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002190 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002191 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002192 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002193 // Data from the resampled output, in case the reference and output rates
2194 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002195 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002196
ekmeyerson60d9b332015-08-14 10:35:55 -07002197 PushResampler<float> resampler;
2198 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002199
ekmeyerson60d9b332015-08-14 10:35:55 -07002200 // Compute the resampling delay of the output relative to the reference,
2201 // to find the region over which we should search for the best SNR.
2202 float expected_delay_sec = 0;
2203 if (in_rate != ref_rate) {
2204 // Input resampling delay.
2205 expected_delay_sec +=
2206 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2207 }
2208 if (out_rate != ref_rate) {
2209 // Output resampling delay.
2210 expected_delay_sec +=
2211 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2212 // Delay of converting the output back to its processing rate for
2213 // testing.
2214 expected_delay_sec +=
2215 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2216 }
2217 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002218 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002219
ekmeyerson60d9b332015-08-14 10:35:55 -07002220 double variance = 0;
2221 double sq_error = 0;
2222 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2223 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2224 float* out_ptr = out_data.get();
2225 if (out_rate != ref_rate) {
2226 // Resample the output back to its internal processing rate if
2227 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002228 ASSERT_EQ(ref_length,
2229 static_cast<size_t>(resampler.Resample(
2230 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002231 out_ptr = cmp_data.get();
2232 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002233
Artem Titov0b489302021-07-28 20:50:03 +02002234 // Update the `sq_error` and `variance` accumulators with the highest
ekmeyerson60d9b332015-08-14 10:35:55 -07002235 // SNR of reference vs output.
2236 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2237 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002238 }
2239
ekmeyerson60d9b332015-08-14 10:35:55 -07002240 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2241 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2242 << cf[i].num_input << ", " << cf[i].num_output << ", "
2243 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2244 << ", " << file_direction << "): ";
2245 if (sq_error > 0) {
2246 double snr = 10 * log10(variance / sq_error);
2247 EXPECT_GE(snr, expected_snr);
2248 EXPECT_NE(0, expected_snr);
2249 std::cout << "SNR=" << snr << " dB" << std::endl;
2250 } else {
aluebs776593b2016-03-15 14:04:58 -07002251 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002252 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002253
ekmeyerson60d9b332015-08-14 10:35:55 -07002254 fclose(out_file);
2255 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002256 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002257 }
2258}
2259
2260#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002261INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002262 CommonFormats,
2263 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002264 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2265 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2266 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2267 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2268 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2269 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2270 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2271 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2272 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2273 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2274 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2275 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002276
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002277 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2278 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2279 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2280 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2281 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2282 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2283 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2284 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2285 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2286 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2287 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2288 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002289
Per Åhgrenc0424252019-12-10 13:04:15 +01002290 std::make_tuple(32000, 48000, 48000, 48000, 15, 0),
2291 std::make_tuple(32000, 48000, 32000, 48000, 15, 30),
2292 std::make_tuple(32000, 48000, 16000, 48000, 15, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002293 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2294 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2295 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2296 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2297 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Gustaf Ullberg09226fc2021-02-19 13:03:14 +01002298 std::make_tuple(32000, 32000, 16000, 32000, 39, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002299 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2300 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2301 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002302
Per Åhgrenc0424252019-12-10 13:04:15 +01002303 std::make_tuple(16000, 48000, 48000, 48000, 9, 0),
2304 std::make_tuple(16000, 48000, 32000, 48000, 9, 30),
2305 std::make_tuple(16000, 48000, 16000, 48000, 9, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002306 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2307 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2308 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2309 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2310 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2311 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2312 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
Gustaf Ullberg09226fc2021-02-19 13:03:14 +01002313 std::make_tuple(16000, 16000, 32000, 16000, 39, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002314 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002315
2316#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002317INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002318 CommonFormats,
2319 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002320 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2321 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2322 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002323 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2324 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2325 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002326 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2327 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2328 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002329 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2330 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2331 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002332
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002333 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2334 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2335 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2336 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2337 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2338 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002339 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2340 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2341 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2342 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2343 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2344 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002345
Per Åhgrenc0424252019-12-10 13:04:15 +01002346 std::make_tuple(32000, 48000, 48000, 48000, 17, 0),
2347 std::make_tuple(32000, 48000, 32000, 48000, 17, 30),
2348 std::make_tuple(32000, 48000, 16000, 48000, 17, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002349 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2350 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2351 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002352 std::make_tuple(32000, 32000, 48000, 32000, 27, 35),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002353 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002354 std::make_tuple(32000, 32000, 16000, 32000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002355 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2356 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2357 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002358
Per Åhgrenc0424252019-12-10 13:04:15 +01002359 std::make_tuple(16000, 48000, 48000, 48000, 11, 0),
2360 std::make_tuple(16000, 48000, 32000, 48000, 11, 30),
2361 std::make_tuple(16000, 48000, 16000, 48000, 11, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002362 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2363 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2364 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002365 std::make_tuple(16000, 32000, 48000, 32000, 24, 35),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002366 std::make_tuple(16000, 32000, 32000, 32000, 24, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002367 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002368 std::make_tuple(16000, 16000, 48000, 16000, 28, 20),
2369 std::make_tuple(16000, 16000, 32000, 16000, 28, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002370 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002371#endif
2372
Per Åhgren3e8bf282019-08-29 23:38:40 +02002373// Produces a scoped trace debug output.
2374std::string ProduceDebugText(int render_input_sample_rate_hz,
2375 int render_output_sample_rate_hz,
2376 int capture_input_sample_rate_hz,
2377 int capture_output_sample_rate_hz,
2378 size_t render_input_num_channels,
2379 size_t render_output_num_channels,
2380 size_t capture_input_num_channels,
2381 size_t capture_output_num_channels) {
2382 rtc::StringBuilder ss;
2383 ss << "Sample rates:"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002384 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002385 << render_input_sample_rate_hz
2386 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002387 "\n Render output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002388 << render_output_sample_rate_hz
2389 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002390 "\n Capture input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002391 << capture_input_sample_rate_hz
2392 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002393 "\n Capture output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002394 << capture_output_sample_rate_hz
2395 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002396 "\nNumber of channels:"
2397 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002398 << render_input_num_channels
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002399 << "\n Render output: " << render_output_num_channels
2400 << "\n Capture input: " << capture_input_num_channels
2401 << "\n Capture output: " << capture_output_num_channels;
Per Åhgren3e8bf282019-08-29 23:38:40 +02002402 return ss.Release();
2403}
2404
2405// Validates that running the audio processing module using various combinations
2406// of sample rates and number of channels works as intended.
2407void RunApmRateAndChannelTest(
2408 rtc::ArrayView<const int> sample_rates_hz,
2409 rtc::ArrayView<const int> render_channel_counts,
2410 rtc::ArrayView<const int> capture_channel_counts) {
Niels Möller4f776ac2021-07-02 11:30:54 +02002411 rtc::scoped_refptr<AudioProcessing> apm =
2412 AudioProcessingBuilderForTesting().Create();
Per Åhgren3e8bf282019-08-29 23:38:40 +02002413 webrtc::AudioProcessing::Config apm_config;
2414 apm_config.echo_canceller.enabled = true;
2415 apm->ApplyConfig(apm_config);
2416
2417 StreamConfig render_input_stream_config;
2418 StreamConfig render_output_stream_config;
2419 StreamConfig capture_input_stream_config;
2420 StreamConfig capture_output_stream_config;
2421
2422 std::vector<float> render_input_frame_channels;
2423 std::vector<float*> render_input_frame;
2424 std::vector<float> render_output_frame_channels;
2425 std::vector<float*> render_output_frame;
2426 std::vector<float> capture_input_frame_channels;
2427 std::vector<float*> capture_input_frame;
2428 std::vector<float> capture_output_frame_channels;
2429 std::vector<float*> capture_output_frame;
2430
2431 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2432 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2433 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2434 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2435 for (size_t render_input_num_channels : render_channel_counts) {
2436 for (size_t capture_input_num_channels : capture_channel_counts) {
2437 size_t render_output_num_channels = render_input_num_channels;
2438 size_t capture_output_num_channels = capture_input_num_channels;
2439 auto populate_audio_frame = [](int sample_rate_hz,
2440 size_t num_channels,
2441 StreamConfig* cfg,
2442 std::vector<float>* channels_data,
2443 std::vector<float*>* frame_data) {
2444 cfg->set_sample_rate_hz(sample_rate_hz);
2445 cfg->set_num_channels(num_channels);
2446 cfg->set_has_keyboard(false);
2447
2448 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2449 channels_data->resize(num_channels * max_frame_size);
2450 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2451 frame_data->resize(num_channels);
2452 for (size_t channel = 0; channel < num_channels; ++channel) {
2453 (*frame_data)[channel] =
2454 &(*channels_data)[channel * max_frame_size];
2455 }
2456 };
2457
2458 populate_audio_frame(
2459 render_input_sample_rate_hz, render_input_num_channels,
2460 &render_input_stream_config, &render_input_frame_channels,
2461 &render_input_frame);
2462 populate_audio_frame(
2463 render_output_sample_rate_hz, render_output_num_channels,
2464 &render_output_stream_config, &render_output_frame_channels,
2465 &render_output_frame);
2466 populate_audio_frame(
2467 capture_input_sample_rate_hz, capture_input_num_channels,
2468 &capture_input_stream_config, &capture_input_frame_channels,
2469 &capture_input_frame);
2470 populate_audio_frame(
2471 capture_output_sample_rate_hz, capture_output_num_channels,
2472 &capture_output_stream_config, &capture_output_frame_channels,
2473 &capture_output_frame);
2474
2475 for (size_t frame = 0; frame < 2; ++frame) {
2476 SCOPED_TRACE(ProduceDebugText(
2477 render_input_sample_rate_hz, render_output_sample_rate_hz,
2478 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2479 render_input_num_channels, render_output_num_channels,
2480 render_input_num_channels, capture_output_num_channels));
2481
2482 int result = apm->ProcessReverseStream(
2483 &render_input_frame[0], render_input_stream_config,
2484 render_output_stream_config, &render_output_frame[0]);
2485 EXPECT_EQ(result, AudioProcessing::kNoError);
2486 result = apm->ProcessStream(
2487 &capture_input_frame[0], capture_input_stream_config,
2488 capture_output_stream_config, &capture_output_frame[0]);
2489 EXPECT_EQ(result, AudioProcessing::kNoError);
2490 }
2491 }
2492 }
2493 }
2494 }
2495 }
2496 }
2497}
2498
Alessio Bazzica3438a932020-10-14 12:47:50 +02002499constexpr void Toggle(bool& b) {
2500 b ^= true;
2501}
2502
niklase@google.com470e71d2011-07-07 08:21:25 +00002503} // namespace
peahc19f3122016-10-07 14:54:10 -07002504
Alessio Bazzicac054e782018-04-16 12:10:09 +02002505TEST(RuntimeSettingTest, TestDefaultCtor) {
2506 auto s = AudioProcessing::RuntimeSetting();
2507 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2508}
2509
Alessio Bazzicac054e782018-04-16 12:10:09 +02002510TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2511 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2512 auto s = AudioProcessing::RuntimeSetting();
2513 ASSERT_TRUE(q.Insert(&s));
2514 ASSERT_TRUE(q.Remove(&s));
2515 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2516}
2517
Sam Zackrisson0beac582017-09-25 12:04:02 +02002518TEST(ApmConfiguration, EnablePostProcessing) {
2519 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002520 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002521 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002522 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002523 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002524 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002525 AudioProcessingBuilderForTesting()
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002526 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002527 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002528
Per Åhgren2507f8c2020-03-19 12:33:29 +01002529 Int16FrameData audio;
2530 audio.num_channels = 1;
Sam Zackrisson0beac582017-09-25 12:04:02 +02002531 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2532
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002533 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002534 apm->ProcessStream(audio.data.data(),
2535 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2536 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002537 audio.data.data());
Sam Zackrisson0beac582017-09-25 12:04:02 +02002538}
2539
Alex Loiko5825aa62017-12-18 16:02:40 +01002540TEST(ApmConfiguration, EnablePreProcessing) {
2541 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002542 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002543 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002544 auto mock_pre_processor =
2545 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002546 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002547 AudioProcessingBuilderForTesting()
Ivo Creusen62337e52018-01-09 14:17:33 +01002548 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002549 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002550
Per Åhgren2507f8c2020-03-19 12:33:29 +01002551 Int16FrameData audio;
2552 audio.num_channels = 1;
Alex Loiko5825aa62017-12-18 16:02:40 +01002553 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2554
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002555 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002556 apm->ProcessReverseStream(
2557 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2558 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2559 audio.data.data());
Alex Loiko5825aa62017-12-18 16:02:40 +01002560}
2561
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002562TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2563 // Verify that apm uses a capture analyzer if one is provided.
2564 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002565 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002566 auto mock_capture_analyzer =
2567 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2568 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002569 AudioProcessingBuilderForTesting()
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002570 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2571 .Create();
2572
Per Åhgren2507f8c2020-03-19 12:33:29 +01002573 Int16FrameData audio;
2574 audio.num_channels = 1;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002575 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2576
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002577 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002578 apm->ProcessStream(audio.data.data(),
2579 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2580 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002581 audio.data.data());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002582}
2583
Alex Loiko73ec0192018-05-15 10:52:28 +02002584TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2585 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002586 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002587 auto mock_pre_processor =
2588 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2589 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002590 AudioProcessingBuilderForTesting()
Alex Loiko73ec0192018-05-15 10:52:28 +02002591 .SetRenderPreProcessing(std::move(mock_pre_processor))
2592 .Create();
2593 apm->SetRuntimeSetting(
2594 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2595
2596 // RuntimeSettings forwarded during 'Process*Stream' calls.
2597 // Therefore we have to make one such call.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002598 Int16FrameData audio;
2599 audio.num_channels = 1;
Alex Loiko73ec0192018-05-15 10:52:28 +02002600 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2601
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002602 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2603 .Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002604 apm->ProcessReverseStream(
2605 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2606 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2607 audio.data.data());
Alex Loiko73ec0192018-05-15 10:52:28 +02002608}
2609
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002610class MyEchoControlFactory : public EchoControlFactory {
2611 public:
2612 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2613 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002614 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2615 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
Per Åhgrenc20a19c2019-11-13 11:12:29 +01002616 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_, ::testing::_))
2617 .Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002618 return std::unique_ptr<EchoControl>(ec);
2619 }
Per Åhgrence202a02019-09-02 17:01:19 +02002620
2621 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
Per Åhgren4e5c7092019-11-01 20:44:11 +01002622 int num_render_channels,
2623 int num_capture_channels) {
Per Åhgrence202a02019-09-02 17:01:19 +02002624 return Create(sample_rate_hz);
2625 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002626};
2627
2628TEST(ApmConfiguration, EchoControlInjection) {
2629 // Verify that apm uses an injected echo controller if one is provided.
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002630 std::unique_ptr<EchoControlFactory> echo_control_factory(
2631 new MyEchoControlFactory());
2632
Alex Loiko5825aa62017-12-18 16:02:40 +01002633 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002634 AudioProcessingBuilderForTesting()
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002635 .SetEchoControlFactory(std::move(echo_control_factory))
Alessio Bazzicabe1b8982021-09-17 08:26:10 +02002636 .Create();
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002637
Per Åhgren2507f8c2020-03-19 12:33:29 +01002638 Int16FrameData audio;
2639 audio.num_channels = 1;
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002640 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002641 apm->ProcessStream(audio.data.data(),
2642 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2643 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002644 audio.data.data());
Per Åhgren2507f8c2020-03-19 12:33:29 +01002645 apm->ProcessReverseStream(
2646 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2647 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2648 audio.data.data());
2649 apm->ProcessStream(audio.data.data(),
2650 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2651 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002652 audio.data.data());
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002653}
Ivo Creusenae026092017-11-20 13:07:16 +01002654
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002655TEST(ApmConfiguration, EchoDetectorInjection) {
2656 using ::testing::_;
2657 rtc::scoped_refptr<test::MockEchoDetector> mock_echo_detector =
2658 rtc::make_ref_counted<::testing::StrictMock<test::MockEchoDetector>>();
2659 EXPECT_CALL(*mock_echo_detector,
2660 Initialize(/*capture_sample_rate_hz=*/16000, _,
2661 /*render_sample_rate_hz=*/16000, _))
2662 .Times(1);
Niels Möller4f776ac2021-07-02 11:30:54 +02002663 rtc::scoped_refptr<AudioProcessing> apm =
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002664 AudioProcessingBuilderForTesting()
2665 .SetEchoDetector(mock_echo_detector)
2666 .Create();
2667
2668 // The echo detector is included in processing when enabled.
2669 EXPECT_CALL(*mock_echo_detector, AnalyzeRenderAudio(_))
2670 .WillOnce([](rtc::ArrayView<const float> render_audio) {
2671 EXPECT_EQ(render_audio.size(), 160u);
2672 });
2673 EXPECT_CALL(*mock_echo_detector, AnalyzeCaptureAudio(_))
2674 .WillOnce([](rtc::ArrayView<const float> capture_audio) {
2675 EXPECT_EQ(capture_audio.size(), 160u);
2676 });
2677 EXPECT_CALL(*mock_echo_detector, GetMetrics()).Times(1);
2678
2679 Int16FrameData frame;
2680 frame.num_channels = 1;
2681 SetFrameSampleRate(&frame, 16000);
2682
2683 apm->ProcessReverseStream(frame.data.data(), StreamConfig(16000, 1),
2684 StreamConfig(16000, 1), frame.data.data());
2685 apm->ProcessStream(frame.data.data(), StreamConfig(16000, 1),
2686 StreamConfig(16000, 1), frame.data.data());
2687
2688 // When processing rates change, the echo detector is also reinitialized to
2689 // match those.
2690 EXPECT_CALL(*mock_echo_detector,
2691 Initialize(/*capture_sample_rate_hz=*/48000, _,
2692 /*render_sample_rate_hz=*/16000, _))
2693 .Times(1);
2694 EXPECT_CALL(*mock_echo_detector,
2695 Initialize(/*capture_sample_rate_hz=*/48000, _,
2696 /*render_sample_rate_hz=*/48000, _))
2697 .Times(1);
2698 EXPECT_CALL(*mock_echo_detector, AnalyzeRenderAudio(_))
2699 .WillOnce([](rtc::ArrayView<const float> render_audio) {
2700 EXPECT_EQ(render_audio.size(), 480u);
2701 });
2702 EXPECT_CALL(*mock_echo_detector, AnalyzeCaptureAudio(_))
2703 .Times(2)
2704 .WillRepeatedly([](rtc::ArrayView<const float> capture_audio) {
2705 EXPECT_EQ(capture_audio.size(), 480u);
2706 });
2707 EXPECT_CALL(*mock_echo_detector, GetMetrics()).Times(2);
2708
2709 SetFrameSampleRate(&frame, 48000);
2710 apm->ProcessStream(frame.data.data(), StreamConfig(48000, 1),
2711 StreamConfig(48000, 1), frame.data.data());
2712 apm->ProcessReverseStream(frame.data.data(), StreamConfig(48000, 1),
2713 StreamConfig(48000, 1), frame.data.data());
2714 apm->ProcessStream(frame.data.data(), StreamConfig(48000, 1),
2715 StreamConfig(48000, 1), frame.data.data());
2716}
2717
2718rtc::scoped_refptr<AudioProcessing> CreateApm(bool mobile_aec) {
2719 // Enable residual echo detection, for stats.
2720 rtc::scoped_refptr<AudioProcessing> apm =
2721 AudioProcessingBuilderForTesting()
2722 .SetEchoDetector(CreateEchoDetector())
2723 .Create();
Ivo Creusenae026092017-11-20 13:07:16 +01002724 if (!apm) {
2725 return apm;
2726 }
2727
2728 ProcessingConfig processing_config = {
2729 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2730
2731 if (apm->Initialize(processing_config) != 0) {
2732 return nullptr;
2733 }
2734
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002735 // Disable all components except for an AEC.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002736 AudioProcessing::Config apm_config;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002737 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002738 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002739 apm_config.gain_controller2.enabled = false;
2740 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002741 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002742 apm_config.noise_suppression.enabled = false;
saza0bad15f2019-10-16 11:46:11 +02002743 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002744 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002745 return apm;
2746}
2747
2748#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2749#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2750#else
2751#define MAYBE_ApmStatistics ApmStatistics
2752#endif
2753
Per Åhgren8607f842019-04-12 22:02:26 +02002754TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2755 // Set up APM with AEC3 and process some audio.
Niels Möller4f776ac2021-07-02 11:30:54 +02002756 rtc::scoped_refptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002757 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002758 AudioProcessing::Config apm_config;
2759 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002760 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002761
2762 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002763 Int16FrameData frame;
2764 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002765 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002766
2767 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002768 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002769 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2770 ptr[i] = 10000 * ((i % 3) - 1);
2771 }
2772
2773 // Do some processing.
2774 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002775 EXPECT_EQ(apm->ProcessReverseStream(
2776 frame.data.data(),
2777 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2778 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2779 frame.data.data()),
2780 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002781 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002782 EXPECT_EQ(apm->ProcessStream(
2783 frame.data.data(),
2784 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2785 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002786 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002787 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002788 }
2789
2790 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002791 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002792 // We expect all statistics to be set and have a sensible value.
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002793 ASSERT_TRUE(stats.residual_echo_likelihood.has_value());
Ivo Creusenae026092017-11-20 13:07:16 +01002794 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2795 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002796 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max.has_value());
Ivo Creusenae026092017-11-20 13:07:16 +01002797 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2798 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002799 ASSERT_TRUE(stats.echo_return_loss.has_value());
Ivo Creusenae026092017-11-20 13:07:16 +01002800 EXPECT_NE(*stats.echo_return_loss, -100.0);
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002801 ASSERT_TRUE(stats.echo_return_loss_enhancement.has_value());
Ivo Creusenae026092017-11-20 13:07:16 +01002802 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002803}
2804
2805TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2806 // Set up APM with AECM and process some audio.
Niels Möller4f776ac2021-07-02 11:30:54 +02002807 rtc::scoped_refptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002808 ASSERT_TRUE(apm);
2809
2810 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002811 Int16FrameData frame;
2812 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002813 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002814
2815 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002816 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002817 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2818 ptr[i] = 10000 * ((i % 3) - 1);
2819 }
2820
2821 // Do some processing.
2822 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002823 EXPECT_EQ(apm->ProcessReverseStream(
2824 frame.data.data(),
2825 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2826 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2827 frame.data.data()),
2828 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002829 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002830 EXPECT_EQ(apm->ProcessStream(
2831 frame.data.data(),
2832 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2833 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002834 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002835 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002836 }
2837
2838 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002839 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002840 // We expect only the residual echo detector statistics to be set and have a
2841 // sensible value.
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002842 ASSERT_TRUE(stats.residual_echo_likelihood.has_value());
2843 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2844 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2845 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max.has_value());
2846 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2847 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2848 EXPECT_FALSE(stats.echo_return_loss.has_value());
2849 EXPECT_FALSE(stats.echo_return_loss_enhancement.has_value());
Ivo Creusenae026092017-11-20 13:07:16 +01002850}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002851
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002852TEST(ApmStatistics, ReportHasVoice) {
2853 ProcessingConfig processing_config = {
2854 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2855 AudioProcessing::Config config;
2856
2857 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002858 Int16FrameData frame;
2859 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002860 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2861
2862 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002863 int16_t* ptr = frame.data.data();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002864 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2865 ptr[i] = 10000 * ((i % 3) - 1);
2866 }
2867
Niels Möller4f776ac2021-07-02 11:30:54 +02002868 rtc::scoped_refptr<AudioProcessing> apm =
2869 AudioProcessingBuilderForTesting().Create();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002870 apm->Initialize(processing_config);
2871
2872 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002873 EXPECT_EQ(
2874 apm->ProcessStream(frame.data.data(),
2875 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2876 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002877 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002878 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002879 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002880
2881 // If enabled, metrics should be reported.
2882 config.voice_detection.enabled = true;
2883 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002884 EXPECT_EQ(
2885 apm->ProcessStream(frame.data.data(),
2886 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2887 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002888 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002889 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002890 auto stats = apm->GetStatistics();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002891 EXPECT_TRUE(stats.voice_detected);
2892
2893 // If re-disabled, the value is again not reported.
2894 config.voice_detection.enabled = false;
2895 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002896 EXPECT_EQ(
2897 apm->ProcessStream(frame.data.data(),
2898 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2899 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002900 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002901 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002902 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002903}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002904
Sam Zackrisson03cb7e52021-12-06 15:40:04 +01002905TEST(ApmStatistics, GetStatisticsReportsNoEchoDetectorStatsWhenDisabled) {
2906 rtc::scoped_refptr<AudioProcessing> apm =
2907 AudioProcessingBuilderForTesting().Create();
2908 Int16FrameData frame;
2909 frame.num_channels = 1;
2910 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2911 ASSERT_EQ(
2912 apm->ProcessStream(frame.data.data(),
2913 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2914 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2915 frame.data.data()),
2916 0);
2917 // Echo detector is disabled by default, no stats reported.
2918 AudioProcessingStats stats = apm->GetStatistics();
2919 EXPECT_FALSE(stats.residual_echo_likelihood.has_value());
2920 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max.has_value());
2921}
2922
2923TEST(ApmStatistics, GetStatisticsReportsEchoDetectorStatsWhenEnabled) {
2924 // Create APM with an echo detector injected.
2925 rtc::scoped_refptr<AudioProcessing> apm =
2926 AudioProcessingBuilderForTesting()
2927 .SetEchoDetector(CreateEchoDetector())
2928 .Create();
2929 Int16FrameData frame;
2930 frame.num_channels = 1;
2931 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2932 // Echo detector enabled: Report stats.
2933 ASSERT_EQ(
2934 apm->ProcessStream(frame.data.data(),
2935 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2936 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2937 frame.data.data()),
2938 0);
2939 AudioProcessingStats stats = apm->GetStatistics();
2940 EXPECT_TRUE(stats.residual_echo_likelihood.has_value());
2941 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max.has_value());
2942}
2943
Per Åhgren3e8bf282019-08-29 23:38:40 +02002944TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2945 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2946 std::array<int, 2> render_channel_counts = {1, 7};
2947 std::array<int, 2> capture_channel_counts = {1, 7};
2948 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2949 capture_channel_counts);
2950}
2951
2952TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2953 std::array<int, 1> sample_rates_hz = {48000};
2954 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2955 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2956 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2957 capture_channel_counts);
2958}
2959
2960TEST(ApmConfiguration, HandlingOfRateCombinations) {
2961 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2962 48000, 96000, 192000, 384000};
2963 std::array<int, 1> render_channel_counts = {2};
2964 std::array<int, 1> capture_channel_counts = {2};
2965 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2966 capture_channel_counts);
2967}
2968
Yves Gerey1fce3f82019-12-05 17:45:31 +01002969TEST(ApmConfiguration, SelfAssignment) {
2970 // At some point memory sanitizer was complaining about self-assigment.
2971 // Make sure we don't regress.
2972 AudioProcessing::Config config;
2973 AudioProcessing::Config* config2 = &config;
2974 *config2 = *config2; // Workaround -Wself-assign-overloaded
2975 SUCCEED(); // Real success is absence of defects from asan/msan/ubsan.
2976}
2977
Alessio Bazzica3438a932020-10-14 12:47:50 +02002978TEST(AudioProcessing, GainController1ConfigEqual) {
2979 AudioProcessing::Config::GainController1 a;
2980 AudioProcessing::Config::GainController1 b;
2981 EXPECT_EQ(a, b);
2982
2983 Toggle(a.enabled);
2984 b.enabled = a.enabled;
2985 EXPECT_EQ(a, b);
2986
2987 a.mode = AudioProcessing::Config::GainController1::Mode::kAdaptiveDigital;
2988 b.mode = a.mode;
2989 EXPECT_EQ(a, b);
2990
2991 a.target_level_dbfs++;
2992 b.target_level_dbfs = a.target_level_dbfs;
2993 EXPECT_EQ(a, b);
2994
2995 a.compression_gain_db++;
2996 b.compression_gain_db = a.compression_gain_db;
2997 EXPECT_EQ(a, b);
2998
2999 Toggle(a.enable_limiter);
3000 b.enable_limiter = a.enable_limiter;
3001 EXPECT_EQ(a, b);
3002
Alessio Bazzica3438a932020-10-14 12:47:50 +02003003 auto& a_analog = a.analog_gain_controller;
3004 auto& b_analog = b.analog_gain_controller;
3005
3006 Toggle(a_analog.enabled);
3007 b_analog.enabled = a_analog.enabled;
3008 EXPECT_EQ(a, b);
3009
3010 a_analog.startup_min_volume++;
3011 b_analog.startup_min_volume = a_analog.startup_min_volume;
3012 EXPECT_EQ(a, b);
3013
3014 a_analog.clipped_level_min++;
3015 b_analog.clipped_level_min = a_analog.clipped_level_min;
3016 EXPECT_EQ(a, b);
3017
Alessio Bazzica3438a932020-10-14 12:47:50 +02003018 Toggle(a_analog.enable_digital_adaptive);
3019 b_analog.enable_digital_adaptive = a_analog.enable_digital_adaptive;
3020 EXPECT_EQ(a, b);
3021}
3022
3023// Checks that one differing parameter is sufficient to make two configs
3024// different.
3025TEST(AudioProcessing, GainController1ConfigNotEqual) {
3026 AudioProcessing::Config::GainController1 a;
3027 const AudioProcessing::Config::GainController1 b;
3028
3029 Toggle(a.enabled);
3030 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003031 a = b;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003032
3033 a.mode = AudioProcessing::Config::GainController1::Mode::kAdaptiveDigital;
3034 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003035 a = b;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003036
3037 a.target_level_dbfs++;
3038 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003039 a = b;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003040
3041 a.compression_gain_db++;
3042 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003043 a = b;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003044
3045 Toggle(a.enable_limiter);
3046 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003047 a = b;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003048
Alessio Bazzica3438a932020-10-14 12:47:50 +02003049 auto& a_analog = a.analog_gain_controller;
3050 const auto& b_analog = b.analog_gain_controller;
3051
3052 Toggle(a_analog.enabled);
3053 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003054 a_analog = b_analog;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003055
3056 a_analog.startup_min_volume++;
3057 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003058 a_analog = b_analog;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003059
3060 a_analog.clipped_level_min++;
3061 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003062 a_analog = b_analog;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003063
Alessio Bazzica3438a932020-10-14 12:47:50 +02003064 Toggle(a_analog.enable_digital_adaptive);
3065 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003066 a_analog = b_analog;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003067}
3068
3069TEST(AudioProcessing, GainController2ConfigEqual) {
3070 AudioProcessing::Config::GainController2 a;
3071 AudioProcessing::Config::GainController2 b;
3072 EXPECT_EQ(a, b);
3073
3074 Toggle(a.enabled);
3075 b.enabled = a.enabled;
3076 EXPECT_EQ(a, b);
3077
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003078 a.fixed_digital.gain_db += 1.0f;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003079 b.fixed_digital.gain_db = a.fixed_digital.gain_db;
3080 EXPECT_EQ(a, b);
3081
3082 auto& a_adaptive = a.adaptive_digital;
3083 auto& b_adaptive = b.adaptive_digital;
3084
3085 Toggle(a_adaptive.enabled);
3086 b_adaptive.enabled = a_adaptive.enabled;
3087 EXPECT_EQ(a, b);
3088
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003089 Toggle(a_adaptive.dry_run);
3090 b_adaptive.dry_run = a_adaptive.dry_run;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003091 EXPECT_EQ(a, b);
3092
Alessio Bazzicaa850e6c2021-10-04 13:35:55 +02003093 a_adaptive.headroom_db += 1.0f;
3094 b_adaptive.headroom_db = a_adaptive.headroom_db;
3095 EXPECT_EQ(a, b);
3096
3097 a_adaptive.max_gain_db += 1.0f;
3098 b_adaptive.max_gain_db = a_adaptive.max_gain_db;
3099 EXPECT_EQ(a, b);
3100
3101 a_adaptive.initial_gain_db += 1.0f;
3102 b_adaptive.initial_gain_db = a_adaptive.initial_gain_db;
3103 EXPECT_EQ(a, b);
3104
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003105 a_adaptive.vad_reset_period_ms++;
3106 b_adaptive.vad_reset_period_ms = a_adaptive.vad_reset_period_ms;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003107 EXPECT_EQ(a, b);
3108
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003109 a_adaptive.adjacent_speech_frames_threshold++;
3110 b_adaptive.adjacent_speech_frames_threshold =
3111 a_adaptive.adjacent_speech_frames_threshold;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003112 EXPECT_EQ(a, b);
3113
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003114 a_adaptive.max_gain_change_db_per_second += 1.0f;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003115 b_adaptive.max_gain_change_db_per_second =
3116 a_adaptive.max_gain_change_db_per_second;
3117 EXPECT_EQ(a, b);
3118
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003119 a_adaptive.max_output_noise_level_dbfs += 1.0f;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003120 b_adaptive.max_output_noise_level_dbfs =
3121 a_adaptive.max_output_noise_level_dbfs;
3122 EXPECT_EQ(a, b);
3123}
3124
3125// Checks that one differing parameter is sufficient to make two configs
3126// different.
3127TEST(AudioProcessing, GainController2ConfigNotEqual) {
3128 AudioProcessing::Config::GainController2 a;
3129 const AudioProcessing::Config::GainController2 b;
3130
3131 Toggle(a.enabled);
3132 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003133 a = b;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003134
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003135 a.fixed_digital.gain_db += 1.0f;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003136 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003137 a.fixed_digital = b.fixed_digital;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003138
3139 auto& a_adaptive = a.adaptive_digital;
3140 const auto& b_adaptive = b.adaptive_digital;
3141
3142 Toggle(a_adaptive.enabled);
3143 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003144 a_adaptive = b_adaptive;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003145
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003146 Toggle(a_adaptive.dry_run);
Alessio Bazzica3438a932020-10-14 12:47:50 +02003147 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003148 a_adaptive = b_adaptive;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003149
Alessio Bazzicaa850e6c2021-10-04 13:35:55 +02003150 a_adaptive.headroom_db += 1.0f;
3151 EXPECT_NE(a, b);
3152 a_adaptive = b_adaptive;
3153
3154 a_adaptive.max_gain_db += 1.0f;
3155 EXPECT_NE(a, b);
3156 a_adaptive = b_adaptive;
3157
3158 a_adaptive.initial_gain_db += 1.0f;
3159 EXPECT_NE(a, b);
3160 a_adaptive = b_adaptive;
3161
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003162 a_adaptive.vad_reset_period_ms++;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003163 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003164 a_adaptive = b_adaptive;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003165
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003166 a_adaptive.adjacent_speech_frames_threshold++;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003167 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003168 a_adaptive = b_adaptive;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003169
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003170 a_adaptive.max_gain_change_db_per_second += 1.0f;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003171 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003172 a_adaptive = b_adaptive;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003173
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003174 a_adaptive.max_output_noise_level_dbfs += 1.0f;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003175 EXPECT_NE(a, b);
Alessio Bazzicaa2efd152021-04-29 16:17:49 +02003176 a_adaptive = b_adaptive;
Alessio Bazzica3438a932020-10-14 12:47:50 +02003177}
3178
andrew@webrtc.org27c69802014-02-18 20:24:56 +00003179} // namespace webrtc