blob: a320fcd940a9f0a7c29c988716a3d24849455e8e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "modules/audio_processing/include/audio_processing.h"
11
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000012#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000013#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080014
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000015#include <algorithm>
Oleh Prypin708eccc2019-03-27 09:38:52 +010016#include <cmath>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000017#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080018#include <memory>
Sam Zackrissone277bde2019-10-25 10:07:54 +020019#include <numeric>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000020#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000021
Sam Zackrisson6558fa52019-08-26 10:12:41 +020022#include "absl/flags/flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
24#include "common_audio/resampler/include/push_resampler.h"
25#include "common_audio/resampler/push_sinc_resampler.h"
26#include "common_audio/signal_processing/include/signal_processing_library.h"
27#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
28#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/common.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020030#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_processing/test/protobuf_utils.h"
32#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/arraysize.h"
34#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/gtest_prod_util.h"
37#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010038#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010039#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020042#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020043#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020044#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010045#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070049
50RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000051#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000052#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000053#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000055#endif
kwiberg77eab702016-09-28 17:42:01 -070056RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000057
Sam Zackrisson6558fa52019-08-26 10:12:41 +020058ABSL_FLAG(bool,
59 write_apm_ref_data,
60 false,
61 "Write ApmTest.Process results to file, instead of comparing results "
62 "to the existing reference data file.");
63
andrew@webrtc.org27c69802014-02-18 20:24:56 +000064namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000065namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000066
ekmeyerson60d9b332015-08-14 10:35:55 -070067// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
68// applicable.
69
mbonadei7c2c8432017-04-07 00:59:12 -070070const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070071const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000072
aluebseb3603b2016-04-20 15:27:58 -070073#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
74// Android doesn't support 48kHz.
75const int kProcessSampleRates[] = {8000, 16000, 32000};
76#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070077const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070078#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000079
ekmeyerson60d9b332015-08-14 10:35:55 -070080enum StreamDirection { kForward = 0, kReverse };
81
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
Jonas Olssona4d87372019-07-05 19:08:33 +020083 ChannelBuffer<int16_t> cb_int(cb->num_frames(), cb->num_channels());
84 Deinterleave(int_data, cb->num_frames(), cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080086 for (size_t i = 0; i < cb->num_channels(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +020087 S16ToFloat(cb_int.channels()[i], cb->num_frames(), cb->channels()[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000088 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000089}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000090
Per Åhgren2507f8c2020-03-19 12:33:29 +010091void ConvertToFloat(const Int16FrameData& frame, ChannelBuffer<float>* cb) {
92 ConvertToFloat(frame.data.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093}
94
andrew@webrtc.org103657b2014-04-24 18:28:56 +000095// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080096size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097 switch (layout) {
98 case AudioProcessing::kMono:
99 return 1;
100 case AudioProcessing::kMonoAndKeyboard:
101 case AudioProcessing::kStereo:
102 return 2;
103 case AudioProcessing::kStereoAndKeyboard:
104 return 3;
105 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700106 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800107 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000108}
109
Jonas Olssona4d87372019-07-05 19:08:33 +0200110void MixStereoToMono(const float* stereo,
111 float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800112 size_t samples_per_channel) {
113 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000115}
116
Jonas Olssona4d87372019-07-05 19:08:33 +0200117void MixStereoToMono(const int16_t* stereo,
118 int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800119 size_t samples_per_channel) {
120 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
122}
123
pkasting25702cb2016-01-08 13:50:27 -0800124void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
125 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000126 stereo[i * 2 + 1] = stereo[i * 2];
127 }
128}
129
yujo36b1a5f2017-06-12 12:45:32 -0700130void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800131 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000132 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
133 }
134}
135
Per Åhgren2507f8c2020-03-19 12:33:29 +0100136void SetFrameTo(Int16FrameData* frame, int16_t value) {
137 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100139 frame->data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000140 }
141}
142
Per Åhgren2507f8c2020-03-19 12:33:29 +0100143void SetFrameTo(Int16FrameData* frame, int16_t left, int16_t right) {
144 ASSERT_EQ(2u, frame->num_channels);
145 for (size_t i = 0; i < frame->samples_per_channel * 2; i += 2) {
146 frame->data[i] = left;
147 frame->data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000148 }
149}
150
Per Åhgren2507f8c2020-03-19 12:33:29 +0100151void ScaleFrame(Int16FrameData* frame, float scale) {
152 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700153 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100154 frame->data[i] = FloatS16ToS16(frame->data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000155 }
156}
157
Per Åhgren2507f8c2020-03-19 12:33:29 +0100158bool FrameDataAreEqual(const Int16FrameData& frame1,
159 const Int16FrameData& frame2) {
160 if (frame1.samples_per_channel != frame2.samples_per_channel) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000161 return false;
162 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100163 if (frame1.num_channels != frame2.num_channels) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000164 return false;
165 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100166 if (memcmp(
167 frame1.data.data(), frame2.data.data(),
168 frame1.samples_per_channel * frame1.num_channels * sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000169 return false;
170 }
171 return true;
172}
173
Per Åhgren2507f8c2020-03-19 12:33:29 +0100174rtc::ArrayView<int16_t> GetMutableFrameData(Int16FrameData* frame) {
175 int16_t* ptr = frame->data.data();
176 const size_t len = frame->samples_per_channel * frame->num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200177 return rtc::ArrayView<int16_t>(ptr, len);
178}
179
Per Åhgren2507f8c2020-03-19 12:33:29 +0100180rtc::ArrayView<const int16_t> GetFrameData(const Int16FrameData& frame) {
181 const int16_t* ptr = frame.data.data();
182 const size_t len = frame.samples_per_channel * frame.num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200183 return rtc::ArrayView<const int16_t>(ptr, len);
184}
185
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200187 AudioProcessing::Config apm_config = ap->GetConfig();
188 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000189#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200190 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100191
192 apm_config.gain_controller1.enabled = true;
193 apm_config.gain_controller1.mode =
194 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000195#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200196 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000197
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100198 apm_config.gain_controller1.enabled = true;
199 apm_config.gain_controller1.mode =
200 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
201 apm_config.gain_controller1.analog_level_minimum = 0;
202 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000203#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000204
saza0bad15f2019-10-16 11:46:11 +0200205 apm_config.noise_suppression.enabled = true;
206
peah8271d042016-11-22 07:24:52 -0800207 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100208 apm_config.level_estimation.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 {
kwiberg9e2be5f2016-09-14 05:23:22 -0700276 RTC_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 {
kwiberg9e2be5f2016-09-14 05:23:22 -0700284 RTC_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
322// returns the result in |cb|. Returns false if the file ended (or on error) and
323// true otherwise.
324//
325// |int_data| and |float_data| are just temporary space that must be
326// 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
niklase@google.com470e71d2011-07-07 08:21:25 +0000350class ApmTest : public ::testing::Test {
351 protected:
352 ApmTest();
353 virtual void SetUp();
354 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000355
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200356 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000357
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200358 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000359
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000360 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200361 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000362
363 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000364 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000365 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800366 size_t num_input_channels,
367 size_t num_output_channels,
368 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000369 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000370 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000371 void EnableAllComponents();
Per Åhgren2507f8c2020-03-19 12:33:29 +0100372 bool ReadFrame(FILE* file, Int16FrameData* frame);
373 bool ReadFrame(FILE* file, Int16FrameData* frame, ChannelBuffer<float>* cb);
374 void ReadFrameWithRewind(FILE* file, Int16FrameData* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200375 void ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100376 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000377 ChannelBuffer<float>* cb);
Jonas Olssona4d87372019-07-05 19:08:33 +0200378 void ProcessDelayVerificationTest(int delay_ms,
379 int system_delay_ms,
380 int delay_min,
381 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700382 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800383 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700384 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800385 void TestChangingForwardChannels(size_t num_in_channels,
386 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700387 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800388 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700389 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000390 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
391 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000392 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393 int ProcessStreamChooser(Format format);
394 int AnalyzeReverseStreamChooser(Format format);
395 void ProcessDebugDump(const std::string& in_filename,
396 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800397 Format format,
398 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000399 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000400
401 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000402 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800403 std::unique_ptr<AudioProcessing> apm_;
Per Åhgren2507f8c2020-03-19 12:33:29 +0100404 Int16FrameData frame_;
405 Int16FrameData revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800406 std::unique_ptr<ChannelBuffer<float> > float_cb_;
407 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000408 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800409 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 FILE* far_file_;
411 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000412 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413};
414
415ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000416 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000417#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200418 ref_filename_(
419 test::ResourcePath("audio_processing/output_data_fixed", "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000420#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200421 ref_filename_(
422 test::ResourcePath("audio_processing/output_data_float", "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000423#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000424 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000425 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000426 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000427 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000428 out_file_(NULL) {
Per Åhgren0695df12020-01-13 14:43:13 +0100429 apm_.reset(AudioProcessingBuilder().Create());
Per Åhgrenc0424252019-12-10 13:04:15 +0100430 AudioProcessing::Config apm_config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100431 apm_config.gain_controller1.analog_gain_controller.enabled = false;
Per Åhgrenc0424252019-12-10 13:04:15 +0100432 apm_config.pipeline.maximum_internal_processing_rate = 48000;
433 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000434}
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
436void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000437 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000439 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
442void ApmTest::TearDown() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000443 if (far_file_) {
444 ASSERT_EQ(0, fclose(far_file_));
445 }
446 far_file_ = NULL;
447
448 if (near_file_) {
449 ASSERT_EQ(0, fclose(near_file_));
450 }
451 near_file_ = NULL;
452
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000453 if (out_file_) {
454 ASSERT_EQ(0, fclose(out_file_));
455 }
456 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000457}
458
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000459void ApmTest::Init(AudioProcessing* ap) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200460 ASSERT_EQ(
461 kNoErr,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100462 ap->Initialize({{{frame_.sample_rate_hz, frame_.num_channels},
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200463 {output_sample_rate_hz_, num_output_channels_},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100464 {revframe_.sample_rate_hz, revframe_.num_channels},
465 {revframe_.sample_rate_hz, revframe_.num_channels}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000466}
467
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000468void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000469 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000470 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800471 size_t num_input_channels,
472 size_t num_output_channels,
473 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000474 bool open_output_file) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200475 SetContainerFormat(sample_rate_hz, num_input_channels, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000476 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000477 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000478
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200479 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, &revframe_,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000480 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000481 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000482
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000483 if (far_file_) {
484 ASSERT_EQ(0, fclose(far_file_));
485 }
486 std::string filename = ResourceFilePath("far", sample_rate_hz);
487 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200488 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000489
490 if (near_file_) {
491 ASSERT_EQ(0, fclose(near_file_));
492 }
493 filename = ResourceFilePath("near", sample_rate_hz);
494 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200495 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000496
497 if (open_output_file) {
498 if (out_file_) {
499 ASSERT_EQ(0, fclose(out_file_));
500 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700501 filename = OutputFilePath(
502 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
503 reverse_sample_rate_hz, num_input_channels, num_output_channels,
504 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000505 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200506 ASSERT_TRUE(out_file_ != NULL)
507 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000508 }
509}
510
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000511void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000512 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000513}
514
Jonas Olssona4d87372019-07-05 19:08:33 +0200515bool ApmTest::ReadFrame(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100516 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000517 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000518 // The files always contain stereo audio.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100519 size_t frame_size = frame->samples_per_channel * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200520 size_t read_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100521 fread(frame->data.data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000522 if (read_count != frame_size) {
523 // Check that the file really ended.
524 EXPECT_NE(0, feof(file));
525 return false; // This is expected.
526 }
527
Per Åhgren2507f8c2020-03-19 12:33:29 +0100528 if (frame->num_channels == 1) {
529 MixStereoToMono(frame->data.data(), frame->data.data(),
530 frame->samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000531 }
532
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000533 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000534 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000535 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000536 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000537}
538
Per Åhgren2507f8c2020-03-19 12:33:29 +0100539bool ApmTest::ReadFrame(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540 return ReadFrame(file, frame, NULL);
541}
542
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000543// If the end of the file has been reached, rewind it and attempt to read the
544// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200545void ApmTest::ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100546 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000547 ChannelBuffer<float>* cb) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200548 if (!ReadFrame(near_file_, &frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000549 rewind(near_file_);
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200550 ASSERT_TRUE(ReadFrame(near_file_, &frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000551 }
552}
553
Per Åhgren2507f8c2020-03-19 12:33:29 +0100554void ApmTest::ReadFrameWithRewind(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000555 ReadFrameWithRewind(file, frame, NULL);
556}
557
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000558int ApmTest::ProcessStreamChooser(Format format) {
559 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100560 return apm_->ProcessStream(
561 frame_.data.data(),
562 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
563 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
564 frame_.data.data(), &frame_.vad_activity);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000565 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200566 return apm_->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100567 float_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100568 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100569 StreamConfig(output_sample_rate_hz_, num_output_channels_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200570 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571}
572
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000573int ApmTest::AnalyzeReverseStreamChooser(Format format) {
574 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100575 return apm_->ProcessReverseStream(
576 revframe_.data.data(),
577 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
578 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
579 revframe_.data.data());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000580 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581 return apm_->AnalyzeReverseStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100582 revfloat_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100583 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000584}
585
Jonas Olssona4d87372019-07-05 19:08:33 +0200586void ApmTest::ProcessDelayVerificationTest(int delay_ms,
587 int system_delay_ms,
588 int delay_min,
589 int delay_max) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000590 // The |revframe_| and |frame_| should include the proper frame information,
591 // hence can be used for extracting information.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100592 Int16FrameData tmp_frame;
593 std::queue<Int16FrameData*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000594 bool causal = true;
595
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200596 tmp_frame.CopyFrom(revframe_);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000597 SetFrameTo(&tmp_frame, 0);
598
599 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
600 // Initialize the |frame_queue| with empty frames.
601 int frame_delay = delay_ms / 10;
602 while (frame_delay < 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100603 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000604 frame->CopyFrom(tmp_frame);
605 frame_queue.push(frame);
606 frame_delay++;
607 causal = false;
608 }
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 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000615 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
616 // need enough frames with audio to have reliable estimates, but as few as
617 // possible to keep processing time down. 4.5 seconds seemed to be a good
618 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000619 for (int frame_count = 0; frame_count < 450; ++frame_count) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100620 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000621 frame->CopyFrom(tmp_frame);
622 // Use the near end recording, since that has more speech in it.
623 ASSERT_TRUE(ReadFrame(near_file_, frame));
624 frame_queue.push(frame);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100625 Int16FrameData* reverse_frame = frame;
626 Int16FrameData* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000627 if (!causal) {
628 reverse_frame = frame_queue.front();
629 // When we call ProcessStream() the frame is modified, so we can't use the
630 // pointer directly when things are non-causal. Use an intermediate frame
631 // and copy the data.
632 process_frame = &tmp_frame;
633 process_frame->CopyFrom(*frame);
634 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100635 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(
636 reverse_frame->data.data(),
637 StreamConfig(reverse_frame->sample_rate_hz,
638 reverse_frame->num_channels),
639 StreamConfig(reverse_frame->sample_rate_hz,
640 reverse_frame->num_channels),
641 reverse_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000642 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100643 EXPECT_EQ(apm_->kNoError,
644 apm_->ProcessStream(process_frame->data.data(),
645 StreamConfig(process_frame->sample_rate_hz,
646 process_frame->num_channels),
647 StreamConfig(process_frame->sample_rate_hz,
648 process_frame->num_channels),
649 process_frame->data.data(),
650 &process_frame->vad_activity));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000651 frame = frame_queue.front();
652 frame_queue.pop();
653 delete frame;
654
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000655 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 // Discard the first delay metrics to avoid convergence effects.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100657 static_cast<void>(apm_->GetStatistics());
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000658 }
659 }
660
661 rewind(near_file_);
662 while (!frame_queue.empty()) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100663 Int16FrameData* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 frame_queue.pop();
665 delete frame;
666 }
667 // Calculate expected delay estimate and acceptable regions. Further,
668 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700669 const size_t samples_per_ms =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100670 rtc::SafeMin<size_t>(16u, frame_.samples_per_channel / 10);
kwiberg07038562017-06-12 11:40:47 -0700671 const int expected_median =
672 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
673 const int expected_median_high = rtc::SafeClamp<int>(
674 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700675 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700676 const int expected_median_low = rtc::SafeClamp<int>(
677 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000679 // Verify delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100680 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200681 ASSERT_TRUE(stats.delay_median_ms.has_value());
682 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 EXPECT_GE(expected_median_high, median);
684 EXPECT_LE(expected_median_low, median);
685}
686
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000687void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000688 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000689 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000691 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200692 AudioProcessing::Config apm_config = apm_->GetConfig();
693 apm_config.gain_controller1.enabled = true;
694 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200695 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000696
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000697 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200698 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200700 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000702 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200703 apm_config.echo_canceller.enabled = true;
704 apm_config.echo_canceller.mobile_mode = false;
705 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000706 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200707 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200708 apm_config.gain_controller1.enabled = false;
709 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000710
711 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100713 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000714
715 // Resets after successful ProcessStream().
716 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000717 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100718 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719
720 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200721 apm_config.gain_controller1.enabled = true;
722 apm_->ApplyConfig(apm_config);
723 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100724 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200725 apm_config.gain_controller1.enabled = false;
726 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200729 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100730 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000731
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000732 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200734 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736}
737
738TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000740}
741
742TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000743 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
Michael Graczyk86c6d332015-07-23 11:41:39 -0700746void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800747 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700748 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100749 frame_.num_channels = num_channels;
750
751 EXPECT_EQ(expected_return,
752 apm_->ProcessStream(
753 frame_.data.data(),
754 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
755 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
756 frame_.data.data(), &frame_.vad_activity));
757 EXPECT_EQ(expected_return,
758 apm_->ProcessReverseStream(
759 frame_.data.data(),
760 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
761 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
762 frame_.data.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000763}
764
Michael Graczyk86c6d332015-07-23 11:41:39 -0700765void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800766 size_t num_in_channels,
767 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700768 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100769 const StreamConfig input_stream = {frame_.sample_rate_hz, num_in_channels};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700770 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
771
772 EXPECT_EQ(expected_return,
773 apm_->ProcessStream(float_cb_->channels(), input_stream,
774 output_stream, float_cb_->channels()));
775}
776
777void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800778 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700779 AudioProcessing::Error expected_return) {
780 const ProcessingConfig processing_config = {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100781 {{frame_.sample_rate_hz, apm_->num_input_channels()},
ekmeyerson60d9b332015-08-14 10:35:55 -0700782 {output_sample_rate_hz_, apm_->num_output_channels()},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100783 {frame_.sample_rate_hz, num_rev_channels},
784 {frame_.sample_rate_hz, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700785
ekmeyerson60d9b332015-08-14 10:35:55 -0700786 EXPECT_EQ(
787 expected_return,
788 apm_->ProcessReverseStream(
789 float_cb_->channels(), processing_config.reverse_input_stream(),
790 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700791}
792
793TEST_F(ApmTest, ChannelsInt16Interface) {
794 // Testing number of invalid and valid channels.
795 Init(16000, 16000, 16000, 4, 4, 4, false);
796
797 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
798
Peter Kasting69558702016-01-12 16:26:35 -0800799 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700800 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
803}
804
Michael Graczyk86c6d332015-07-23 11:41:39 -0700805TEST_F(ApmTest, Channels) {
806 // Testing number of invalid and valid channels.
807 Init(16000, 16000, 16000, 4, 4, 4, false);
808
809 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
810 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
811
Peter Kasting69558702016-01-12 16:26:35 -0800812 for (size_t i = 1; i < 4; ++i) {
813 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814 // Output channels much be one or match input channels.
815 if (j == 1 || i == j) {
816 TestChangingForwardChannels(i, j, kNoErr);
817 TestChangingReverseChannels(i, kNoErr);
818
819 EXPECT_EQ(i, apm_->num_input_channels());
820 EXPECT_EQ(j, apm_->num_output_channels());
821 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800822 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823 } else {
824 TestChangingForwardChannels(i, j,
825 AudioProcessing::kBadNumberChannelsError);
826 }
827 }
828 }
829}
830
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000831TEST_F(ApmTest, SampleRatesInt) {
Sam Zackrisson12e319a2020-01-03 14:54:20 +0100832 // Testing some valid sample rates.
833 for (int sample_rate : {8000, 12000, 16000, 32000, 44100, 48000, 96000}) {
834 SetContainerFormat(sample_rate, 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000835 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000836 }
837}
838
Sam Zackrissone277bde2019-10-25 10:07:54 +0200839// This test repeatedly reconfigures the pre-amplifier in APM, processes a
840// number of frames, and checks that output signal has the right level.
841TEST_F(ApmTest, PreAmplifier) {
842 // Fill the audio frame with a sawtooth pattern.
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200843 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100844 const size_t samples_per_channel = frame_.samples_per_channel;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200845 for (size_t i = 0; i < samples_per_channel; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100846 for (size_t ch = 0; ch < frame_.num_channels; ++ch) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200847 frame_data[i + ch * samples_per_channel] = 10000 * ((i % 3) - 1);
848 }
849 }
850 // Cache the frame in tmp_frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100851 Int16FrameData tmp_frame;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200852 tmp_frame.CopyFrom(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200853
Per Åhgren2507f8c2020-03-19 12:33:29 +0100854 auto compute_power = [](const Int16FrameData& frame) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200855 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
856 return std::accumulate(data.begin(), data.end(), 0.0f,
857 [](float a, float b) { return a + b * b; }) /
858 data.size() / 32768 / 32768;
859 };
860
861 const float input_power = compute_power(tmp_frame);
862 // Double-check that the input data is large compared to the error kEpsilon.
863 constexpr float kEpsilon = 1e-4f;
864 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
865
866 // 1. Enable pre-amp with 0 dB gain.
867 AudioProcessing::Config config = apm_->GetConfig();
868 config.pre_amplifier.enabled = true;
869 config.pre_amplifier.fixed_gain_factor = 1.0f;
870 apm_->ApplyConfig(config);
871
872 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200873 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200874 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
875 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200876 float output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200877 EXPECT_NEAR(output_power, input_power, kEpsilon);
878 config = apm_->GetConfig();
879 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.0f);
880
881 // 2. Change pre-amp gain via ApplyConfig.
882 config.pre_amplifier.fixed_gain_factor = 2.0f;
883 apm_->ApplyConfig(config);
884
885 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200886 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200887 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
888 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200889 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200890 EXPECT_NEAR(output_power, 4 * input_power, kEpsilon);
891 config = apm_->GetConfig();
892 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 2.0f);
893
894 // 3. Change pre-amp gain via a RuntimeSetting.
895 apm_->SetRuntimeSetting(
896 AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.5f));
897
898 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200899 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200900 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
901 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200902 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200903 EXPECT_NEAR(output_power, 2.25 * input_power, kEpsilon);
904 config = apm_->GetConfig();
905 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.5f);
906}
907
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000908TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200909 AudioProcessing::Config config = apm_->GetConfig();
910 config.gain_controller1.enabled = false;
911 apm_->ApplyConfig(config);
912 config.gain_controller1.enabled = true;
913 apm_->ApplyConfig(config);
914
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +0200916 for (auto mode :
917 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
918 AudioProcessing::Config::GainController1::kFixedDigital,
919 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
920 config.gain_controller1.mode = mode;
921 apm_->ApplyConfig(config);
922 apm_->set_stream_analog_level(100);
923 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000925
Sam Zackrisson41478c72019-10-15 10:10:26 +0200926 // Testing target levels
927 for (int target_level_dbfs : {0, 15, 31}) {
928 config.gain_controller1.target_level_dbfs = target_level_dbfs;
929 apm_->ApplyConfig(config);
930 apm_->set_stream_analog_level(100);
931 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000932 }
933
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100934 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +0200935 for (int compression_gain_db : {0, 10, 90}) {
936 config.gain_controller1.compression_gain_db = compression_gain_db;
937 apm_->ApplyConfig(config);
938 apm_->set_stream_analog_level(100);
939 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000940 }
941
942 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +0200943 for (bool enable : {false, true}) {
944 config.gain_controller1.enable_limiter = enable;
945 apm_->ApplyConfig(config);
946 apm_->set_stream_analog_level(100);
947 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
948 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000949
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100950 // Testing level limits
Sam Zackrisson41478c72019-10-15 10:10:26 +0200951 std::array<int, 4> kMinLevels = {0, 0, 255, 65000};
952 std::array<int, 4> kMaxLevels = {255, 1024, 65535, 65535};
953 for (size_t i = 0; i < kMinLevels.size(); ++i) {
954 int min_level = kMinLevels[i];
955 int max_level = kMaxLevels[i];
956 config.gain_controller1.analog_level_minimum = min_level;
957 config.gain_controller1.analog_level_maximum = max_level;
958 apm_->ApplyConfig(config);
959 apm_->set_stream_analog_level((min_level + max_level) / 2);
960 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000961 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000962}
963
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100964#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
965TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200966 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100967 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200968 config.gain_controller1.target_level_dbfs = -1;
969 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100970}
971
972TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200973 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100974 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200975 config.gain_controller1.target_level_dbfs = 32;
976 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100977}
978
979TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200980 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100981 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200982 config.gain_controller1.compression_gain_db = -1;
983 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100984}
985
986TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200987 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100988 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200989 config.gain_controller1.compression_gain_db = 91;
990 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100991}
992
993TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200994 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100995 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200996 config.gain_controller1.analog_level_minimum = -1;
997 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100998}
999
1000TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001001 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001002 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001003 config.gain_controller1.analog_level_maximum = 65536;
1004 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001005}
1006
1007TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001008 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001009 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001010 config.gain_controller1.analog_level_minimum = 512;
1011 config.gain_controller1.analog_level_maximum = 255;
1012 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001013}
1014
1015TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001016 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001017 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001018 config.gain_controller1.analog_level_minimum = 255;
1019 config.gain_controller1.analog_level_maximum = 512;
1020 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001021 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
1022}
1023
1024TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001025 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001026 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001027 config.gain_controller1.analog_level_minimum = 255;
1028 config.gain_controller1.analog_level_maximum = 512;
1029 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001030 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
1031}
1032#endif
1033
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001034void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001035 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001036 auto config = apm_->GetConfig();
1037 config.gain_controller1.enabled = true;
1038 config.gain_controller1.mode =
1039 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1040 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001041
1042 int out_analog_level = 0;
1043 for (int i = 0; i < 2000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001044 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001045 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001046 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001047
1048 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +02001049 apm_->set_stream_analog_level(100);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001050 EXPECT_EQ(apm_->kNoError,
1051 apm_->ProcessStream(
1052 frame_.data.data(),
1053 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1054 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1055 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001056 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001057 }
1058
1059 // Ensure the AGC is still able to reach the maximum.
1060 EXPECT_EQ(255, out_analog_level);
1061}
1062
1063// Verifies that despite volume slider quantization, the AGC can continue to
1064// increase its volume.
1065TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001066 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001067 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1068 }
1069}
1070
1071void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001072 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001073 auto config = apm_->GetConfig();
1074 config.gain_controller1.enabled = true;
1075 config.gain_controller1.mode =
1076 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1077 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001078
1079 int out_analog_level = 100;
1080 for (int i = 0; i < 1000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001081 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001082 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001083 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001084
Sam Zackrisson41478c72019-10-15 10:10:26 +02001085 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001086 EXPECT_EQ(apm_->kNoError,
1087 apm_->ProcessStream(
1088 frame_.data.data(),
1089 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1090 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1091 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001092 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001093 }
1094
1095 // Ensure the volume was raised.
1096 EXPECT_GT(out_analog_level, 100);
1097 int highest_level_reached = out_analog_level;
1098 // Simulate a user manual volume change.
1099 out_analog_level = 100;
1100
1101 for (int i = 0; i < 300; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001102 ReadFrameWithRewind(near_file_, &frame_);
1103 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001104
Sam Zackrisson41478c72019-10-15 10:10:26 +02001105 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001106 EXPECT_EQ(apm_->kNoError,
1107 apm_->ProcessStream(
1108 frame_.data.data(),
1109 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1110 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1111 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001112 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001113 // Check that AGC respected the manually adjusted volume.
1114 EXPECT_LT(out_analog_level, highest_level_reached);
1115 }
1116 // Check that the volume was still raised.
1117 EXPECT_GT(out_analog_level, 100);
1118}
1119
1120TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001121 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001122 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1123 }
1124}
1125
niklase@google.com470e71d2011-07-07 08:21:25 +00001126TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001127 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001128 AudioProcessing::Config apm_config;
1129 apm_config.high_pass_filter.enabled = true;
1130 apm_->ApplyConfig(apm_config);
1131 apm_config.high_pass_filter.enabled = false;
1132 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001133}
1134
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001135TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001136 AudioProcessing::Config config = apm_->GetConfig();
1137 EXPECT_FALSE(config.echo_canceller.enabled);
1138 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001139 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001140 EXPECT_FALSE(config.level_estimation.enabled);
saza0bad15f2019-10-16 11:46:11 +02001141 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001142 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001143}
1144
1145TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001146 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001147 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001148 SetFrameTo(&frame_, 1000, 2000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001149 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001150 frame_copy.CopyFrom(frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001151 for (int j = 0; j < 1000; j++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001152 EXPECT_EQ(apm_->kNoError,
1153 apm_->ProcessStream(
1154 frame_.data.data(),
1155 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1156 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1157 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001158 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001159 EXPECT_EQ(apm_->kNoError,
1160 apm_->ProcessReverseStream(
1161 frame_.data.data(),
1162 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1163 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1164 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001165 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001166 }
1167 }
1168}
1169
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001170TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1171 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001172 const size_t kSamples = 160;
1173 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001174 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001175 float dest[kSamples] = {};
1176
1177 auto src_channels = &src[0];
1178 auto dest_channels = &dest[0];
1179
Ivo Creusen62337e52018-01-09 14:17:33 +01001180 apm_.reset(AudioProcessingBuilder().Create());
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001181 EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
1182 StreamConfig(sample_rate, 1),
1183 &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001184
1185 for (size_t i = 0; i < kSamples; ++i) {
1186 EXPECT_EQ(src[i], dest[i]);
1187 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001188
1189 // Same for ProcessReverseStream.
1190 float rev_dest[kSamples] = {};
1191 auto rev_dest_channels = &rev_dest[0];
1192
1193 StreamConfig input_stream = {sample_rate, 1};
1194 StreamConfig output_stream = {sample_rate, 1};
1195 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1196 output_stream, &rev_dest_channels));
1197
1198 for (size_t i = 0; i < kSamples; ++i) {
1199 EXPECT_EQ(src[i], rev_dest[i]);
1200 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001201}
1202
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001203TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1204 EnableAllComponents();
1205
pkasting25702cb2016-01-08 13:50:27 -08001206 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001207 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1208 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001209 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001210 ASSERT_EQ(0, feof(far_file_));
1211 ASSERT_EQ(0, feof(near_file_));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001212 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001213 CopyLeftToRightChannel(revframe_.data.data(),
1214 revframe_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001215
Per Åhgren2507f8c2020-03-19 12:33:29 +01001216 ASSERT_EQ(
1217 kNoErr,
1218 apm_->ProcessReverseStream(
1219 revframe_.data.data(),
1220 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1221 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1222 revframe_.data.data()));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001223
Per Åhgren2507f8c2020-03-19 12:33:29 +01001224 CopyLeftToRightChannel(frame_.data.data(), frame_.samples_per_channel);
1225 frame_.vad_activity =
1226 AudioProcessing::VoiceDetectionResult::kNotAvailable;
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001227
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001228 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001229 apm_->set_stream_analog_level(analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001230 ASSERT_EQ(kNoErr,
1231 apm_->ProcessStream(
1232 frame_.data.data(),
1233 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1234 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1235 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001236 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001237
Per Åhgren2507f8c2020-03-19 12:33:29 +01001238 VerifyChannelsAreEqual(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001239 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001240 rewind(far_file_);
1241 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001242 }
1243}
1244
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001245TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001246 // Verify the filter is not active through undistorted audio when:
1247 // 1. No components are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001248 SetFrameTo(&frame_, 1000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001249 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001250 frame_copy.CopyFrom(frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001251 EXPECT_EQ(apm_->kNoError,
1252 apm_->ProcessStream(
1253 frame_.data.data(),
1254 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1255 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1256 frame_.data.data(), &frame_.vad_activity));
1257 EXPECT_EQ(apm_->kNoError,
1258 apm_->ProcessStream(
1259 frame_.data.data(),
1260 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1261 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1262 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001263 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001264
1265 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001266 auto apm_config = apm_->GetConfig();
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001267 SetFrameTo(&frame_, 1000);
1268 frame_copy.CopyFrom(frame_);
saza6787f232019-10-11 19:31:07 +02001269 apm_config.level_estimation.enabled = true;
1270 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001271 EXPECT_EQ(apm_->kNoError,
1272 apm_->ProcessStream(
1273 frame_.data.data(),
1274 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1275 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1276 frame_.data.data(), &frame_.vad_activity));
1277 EXPECT_EQ(apm_->kNoError,
1278 apm_->ProcessStream(
1279 frame_.data.data(),
1280 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1281 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1282 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001283 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001284 apm_config.level_estimation.enabled = false;
1285 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001286
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001287 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001288 SetFrameTo(&frame_, 1000);
1289 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001290 apm_config.voice_detection.enabled = true;
1291 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001292 EXPECT_EQ(apm_->kNoError,
1293 apm_->ProcessStream(
1294 frame_.data.data(),
1295 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1296 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1297 frame_.data.data(), &frame_.vad_activity));
1298 EXPECT_EQ(apm_->kNoError,
1299 apm_->ProcessStream(
1300 frame_.data.data(),
1301 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1302 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1303 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001304 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001305 apm_config.voice_detection.enabled = false;
1306 apm_->ApplyConfig(apm_config);
1307
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001308 // 4. Both the VAD and the level estimator are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001309 SetFrameTo(&frame_, 1000);
1310 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001311 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001312 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001313 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001314 EXPECT_EQ(apm_->kNoError,
1315 apm_->ProcessStream(
1316 frame_.data.data(),
1317 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1318 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1319 frame_.data.data(), &frame_.vad_activity));
1320 EXPECT_EQ(apm_->kNoError,
1321 apm_->ProcessStream(
1322 frame_.data.data(),
1323 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1324 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1325 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001326 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001327 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001328 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001329 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001330
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001331 // Check the test is valid. We should have distortion from the filter
1332 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001333 apm_config.echo_canceller.enabled = true;
1334 apm_config.echo_canceller.mobile_mode = false;
1335 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001336 frame_.samples_per_channel = 320;
1337 frame_.num_channels = 2;
1338 frame_.sample_rate_hz = 32000;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001339 SetFrameTo(&frame_, 1000);
1340 frame_copy.CopyFrom(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001341 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001342 EXPECT_EQ(apm_->kNoError,
1343 apm_->ProcessStream(
1344 frame_.data.data(),
1345 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1346 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1347 frame_.data.data(), &frame_.vad_activity));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001348 EXPECT_FALSE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001349}
1350
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001351#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1352void ApmTest::ProcessDebugDump(const std::string& in_filename,
1353 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001354 Format format,
1355 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001356 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001357 FILE* in_file = fopen(in_filename.c_str(), "rb");
1358 ASSERT_TRUE(in_file != NULL);
1359 audioproc::Event event_msg;
1360 bool first_init = true;
1361
1362 while (ReadMessageFromFile(in_file, &event_msg)) {
1363 if (event_msg.type() == audioproc::Event::INIT) {
1364 const audioproc::Init msg = event_msg.init();
1365 int reverse_sample_rate = msg.sample_rate();
1366 if (msg.has_reverse_sample_rate()) {
1367 reverse_sample_rate = msg.reverse_sample_rate();
1368 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001369 int output_sample_rate = msg.sample_rate();
1370 if (msg.has_output_sample_rate()) {
1371 output_sample_rate = msg.output_sample_rate();
1372 }
1373
Jonas Olssona4d87372019-07-05 19:08:33 +02001374 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1375 msg.num_input_channels(), msg.num_output_channels(),
1376 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001377 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001378 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001379 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001380 auto aec_dump =
1381 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1382 EXPECT_TRUE(aec_dump);
1383 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001384 first_init = false;
1385 }
1386
1387 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1388 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1389
1390 if (msg.channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001391 ASSERT_EQ(revframe_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001392 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001393 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001394 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1395 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001396 }
1397 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001398 memcpy(revframe_.data.data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001399 if (format == kFloatFormat) {
1400 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001401 ConvertToFloat(revframe_, revfloat_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001402 }
1403 }
1404 AnalyzeReverseStreamChooser(format);
1405
1406 } else if (event_msg.type() == audioproc::Event::STREAM) {
1407 const audioproc::Stream msg = event_msg.stream();
1408 // ProcessStream could have changed this for the output frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001409 frame_.num_channels = apm_->num_input_channels();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410
Sam Zackrisson41478c72019-10-15 10:10:26 +02001411 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001412 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001413 if (msg.has_keypress()) {
1414 apm_->set_stream_key_pressed(msg.keypress());
1415 } else {
1416 apm_->set_stream_key_pressed(true);
1417 }
1418
1419 if (msg.input_channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001420 ASSERT_EQ(frame_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001421 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001422 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001423 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1424 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001425 }
1426 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001427 memcpy(frame_.data.data(), msg.input_data().data(),
yujo36b1a5f2017-06-12 12:45:32 -07001428 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001429 if (format == kFloatFormat) {
1430 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001431 ConvertToFloat(frame_, float_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001432 }
1433 }
1434 ProcessStreamChooser(format);
1435 }
1436 }
aleloif4dd1912017-06-15 01:55:38 -07001437 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001438 fclose(in_file);
1439}
1440
1441void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001442 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001443 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001444 std::string format_string;
1445 switch (format) {
1446 case kIntFormat:
1447 format_string = "_int";
1448 break;
1449 case kFloatFormat:
1450 format_string = "_float";
1451 break;
1452 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001453 const std::string ref_filename = test::TempFilename(
1454 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1455 const std::string out_filename = test::TempFilename(
1456 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001457 const std::string limited_filename = test::TempFilename(
1458 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1459 const size_t logging_limit_bytes = 100000;
1460 // We expect at least this many bytes in the created logfile.
1461 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001462 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001463 ProcessDebugDump(in_filename, ref_filename, format, -1);
1464 ProcessDebugDump(ref_filename, out_filename, format, -1);
1465 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001466
1467 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1468 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001469 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001470 ASSERT_TRUE(ref_file != NULL);
1471 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001472 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001473 std::unique_ptr<uint8_t[]> ref_bytes;
1474 std::unique_ptr<uint8_t[]> out_bytes;
1475 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001476
1477 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1478 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001479 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001480 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001481 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001482 while (ref_size > 0 && out_size > 0) {
1483 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001484 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001485 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001486 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001487 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001488 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001489 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1490 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001491 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001492 }
1493 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001494 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1495 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001496 EXPECT_NE(0, feof(ref_file));
1497 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001498 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001499 ASSERT_EQ(0, fclose(ref_file));
1500 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001501 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001502 remove(ref_filename.c_str());
1503 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001504 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001505}
1506
pbosc7a65692016-05-06 12:50:04 -07001507TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001508 VerifyDebugDumpTest(kIntFormat);
1509}
1510
pbosc7a65692016-05-06 12:50:04 -07001511TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001512 VerifyDebugDumpTest(kFloatFormat);
1513}
1514#endif
1515
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001516// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001517TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001518 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001519 const std::string filename =
1520 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001521 {
1522 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1523 EXPECT_FALSE(aec_dump);
1524 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001525
1526#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1527 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001528 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001529
aleloif4dd1912017-06-15 01:55:38 -07001530 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1531 EXPECT_TRUE(aec_dump);
1532 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001533 EXPECT_EQ(apm_->kNoError,
1534 apm_->ProcessStream(
1535 frame_.data.data(),
1536 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1537 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1538 frame_.data.data(), &frame_.vad_activity));
1539 EXPECT_EQ(apm_->kNoError,
1540 apm_->ProcessReverseStream(
1541 revframe_.data.data(),
1542 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1543 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1544 revframe_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001545 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001546
1547 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001548 FILE* fid = fopen(filename.c_str(), "r");
1549 ASSERT_TRUE(fid != NULL);
1550
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001551 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001552 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001553 ASSERT_EQ(0, remove(filename.c_str()));
1554#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001555 // Verify the file has NOT been written.
1556 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1557#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1558}
1559
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001560// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001561TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001562 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001563
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001564 const std::string filename =
1565 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001566 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1567 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001568
1569#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1570 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001571 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001572
Niels Möllere8e4dc42019-06-11 14:04:16 +02001573 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001574 EXPECT_TRUE(aec_dump);
1575 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001576 EXPECT_EQ(apm_->kNoError,
1577 apm_->ProcessReverseStream(
1578 revframe_.data.data(),
1579 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1580 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1581 revframe_.data.data()));
1582 EXPECT_EQ(apm_->kNoError,
1583 apm_->ProcessStream(
1584 frame_.data.data(),
1585 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1586 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1587 frame_.data.data(), &frame_.vad_activity));
aleloif4dd1912017-06-15 01:55:38 -07001588 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001589
1590 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001591 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001592 ASSERT_TRUE(fid != NULL);
1593
1594 // Clean it up.
1595 ASSERT_EQ(0, fclose(fid));
1596 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001597#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1598}
1599
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001600// TODO(andrew): Add a test to process a few frames with different combinations
1601// of enabled components.
1602
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001603TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001604 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001605 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001606
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001607 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001608 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001609 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001610 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001611 for (size_t i = 0; i < arraysize(kChannels); i++) {
1612 for (size_t j = 0; j < arraysize(kChannels); j++) {
1613 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001614 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001615 test->set_num_reverse_channels(kChannels[i]);
1616 test->set_num_input_channels(kChannels[j]);
1617 test->set_num_output_channels(kChannels[j]);
1618 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001619 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001620 }
1621 }
1622 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001623#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1624 // To test the extended filter mode.
1625 audioproc::Test* test = ref_data.add_test();
1626 test->set_num_reverse_channels(2);
1627 test->set_num_input_channels(2);
1628 test->set_num_output_channels(2);
1629 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1630 test->set_use_aec_extended_filter(true);
1631#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001632 }
1633
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001634 for (int i = 0; i < ref_data.test_size(); i++) {
1635 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001636
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001637 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001638 // TODO(ajm): We no longer allow different input and output channels. Skip
1639 // these tests for now, but they should be removed from the set.
1640 if (test->num_input_channels() != test->num_output_channels())
1641 continue;
1642
Per Åhgren0695df12020-01-13 14:43:13 +01001643 apm_.reset(AudioProcessingBuilder().Create());
1644 AudioProcessing::Config apm_config = apm_->GetConfig();
1645 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1646 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001647
1648 EnableAllComponents();
1649
Jonas Olssona4d87372019-07-05 19:08:33 +02001650 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001651 static_cast<size_t>(test->num_input_channels()),
1652 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001653 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001654
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001655 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001656 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001657 int analog_level = 127;
1658 int analog_level_average = 0;
1659 int max_output_average = 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001660 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001661#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001662 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001663#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001664
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001665 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001666 EXPECT_EQ(
1667 apm_->kNoError,
1668 apm_->ProcessReverseStream(
1669 revframe_.data.data(),
1670 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1671 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1672 revframe_.data.data()));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001673
Per Åhgren2507f8c2020-03-19 12:33:29 +01001674 frame_.vad_activity =
1675 AudioProcessing::VoiceDetectionResult::kNotAvailable;
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001676
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001677 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001678 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001679
Per Åhgren2507f8c2020-03-19 12:33:29 +01001680 EXPECT_EQ(apm_->kNoError,
1681 apm_->ProcessStream(
1682 frame_.data.data(),
1683 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1684 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1685 frame_.data.data(), &frame_.vad_activity));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001686
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001687 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001688 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01001689 frame_.num_channels);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001690
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001691 max_output_average += MaxAudioFrame(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001692
Sam Zackrisson41478c72019-10-15 10:10:26 +02001693 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001694 analog_level_average += analog_level;
Per Åhgrencf4c8722019-12-30 14:32:14 +01001695 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001696 EXPECT_TRUE(stats.voice_detected);
1697 EXPECT_TRUE(stats.output_rms_dbfs);
1698 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001699 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001700
Per Åhgren2507f8c2020-03-19 12:33:29 +01001701 size_t frame_size = frame_.samples_per_channel * frame_.num_channels;
Jonas Olssona4d87372019-07-05 19:08:33 +02001702 size_t write_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +01001703 fwrite(frame_.data.data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001704 ASSERT_EQ(frame_size, write_count);
1705
1706 // Reset in case of downmixing.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001707 frame_.num_channels = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001708 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001709
1710#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1711 const int kStatsAggregationFrameNum = 100; // 1 second.
1712 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001713 // Get echo and delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +01001714 AudioProcessingStats stats = apm_->GetStatistics();
minyue58530ed2016-05-24 05:50:12 -07001715
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001716 // Echo metrics.
1717 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1718 const float echo_return_loss_enhancement =
1719 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001720 const float residual_echo_likelihood =
1721 stats.residual_echo_likelihood.value_or(-1.0f);
1722 const float residual_echo_likelihood_recent_max =
1723 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1724
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001725 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001726 const audioproc::Test::EchoMetrics& reference =
1727 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001728 constexpr float kEpsilon = 0.01;
1729 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1730 EXPECT_NEAR(echo_return_loss_enhancement,
1731 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001732 EXPECT_NEAR(residual_echo_likelihood,
1733 reference.residual_echo_likelihood(), kEpsilon);
1734 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1735 reference.residual_echo_likelihood_recent_max(),
1736 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001737 ++stats_index;
1738 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001739 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1740 message_echo->set_echo_return_loss(echo_return_loss);
1741 message_echo->set_echo_return_loss_enhancement(
1742 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001743 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1744 message_echo->set_residual_echo_likelihood_recent_max(
1745 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001746 }
1747 }
1748#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001749 }
1750 max_output_average /= frame_count;
1751 analog_level_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001752 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001753
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001754 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001755 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001756 // When running the test on a N7 we get a {2, 6} difference of
1757 // |has_voice_count| and |max_output_average| is up to 18 higher.
1758 // All numbers being consistently higher on N7 compare to ref_data.
1759 // TODO(bjornv): If we start getting more of these offsets on Android we
1760 // should consider a different approach. Either using one slack for all,
1761 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001762#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001763 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001764 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001765 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001766 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001767#else
1768 const int kHasVoiceCountOffset = 0;
1769 const int kHasVoiceCountNear = kIntNear;
1770 const int kMaxOutputAverageOffset = 0;
1771 const int kMaxOutputAverageNear = kIntNear;
1772#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001773 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001774 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001775
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001776 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001777 EXPECT_NEAR(test->max_output_average(),
1778 max_output_average - kMaxOutputAverageOffset,
1779 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001780#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001781 const double kFloatNear = 0.0005;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001782 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001783#endif
1784 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001785 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001786
1787 test->set_analog_level_average(analog_level_average);
1788 test->set_max_output_average(max_output_average);
1789
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001790#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrisson11b87032018-12-18 17:13:58 +01001791 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001792#endif
1793 }
1794
1795 rewind(far_file_);
1796 rewind(near_file_);
1797 }
1798
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001799 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001800 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001801 }
1802}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001803
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001804TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1805 struct ChannelFormat {
1806 AudioProcessing::ChannelLayout in_layout;
1807 AudioProcessing::ChannelLayout out_layout;
1808 };
1809 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001810 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1811 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1812 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001813 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001814
Ivo Creusen62337e52018-01-09 14:17:33 +01001815 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001816 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001817 AudioProcessing::Config config;
1818 config.noise_suppression.enabled = true;
1819 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001820 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001821 const int in_rate = 44100;
1822 const int out_rate = 48000;
1823 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1824 TotalChannelsFromLayout(cf[i].in_layout));
1825 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1826 ChannelsFromLayout(cf[i].out_layout));
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001827 bool has_keyboard = cf[i].in_layout == AudioProcessing::kMonoAndKeyboard ||
1828 cf[i].in_layout == AudioProcessing::kStereoAndKeyboard;
1829 StreamConfig in_sc(in_rate, ChannelsFromLayout(cf[i].in_layout),
1830 has_keyboard);
1831 StreamConfig out_sc(out_rate, ChannelsFromLayout(cf[i].out_layout));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001832
1833 // Run over a few chunks.
1834 for (int j = 0; j < 10; ++j) {
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001835 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_sc, out_sc,
1836 out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001837 }
1838 }
1839}
1840
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001841// Compares the reference and test arrays over a region around the expected
1842// delay. Finds the highest SNR in that region and adds the variance and squared
1843// error results to the supplied accumulators.
1844void UpdateBestSNR(const float* ref,
1845 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001846 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001847 int expected_delay,
1848 double* variance_acc,
1849 double* sq_error_acc) {
1850 double best_snr = std::numeric_limits<double>::min();
1851 double best_variance = 0;
1852 double best_sq_error = 0;
1853 // Search over a region of eight samples around the expected delay.
1854 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1855 ++delay) {
1856 double sq_error = 0;
1857 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001858 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001859 double error = test[i + delay] - ref[i];
1860 sq_error += error * error;
1861 variance += ref[i] * ref[i];
1862 }
1863
1864 if (sq_error == 0) {
1865 *variance_acc += variance;
1866 return;
1867 }
1868 double snr = variance / sq_error;
1869 if (snr > best_snr) {
1870 best_snr = snr;
1871 best_variance = variance;
1872 best_sq_error = sq_error;
1873 }
1874 }
1875
1876 *variance_acc += best_variance;
1877 *sq_error_acc += best_sq_error;
1878}
1879
1880// Used to test a multitude of sample rate and channel combinations. It works
1881// by first producing a set of reference files (in SetUpTestCase) that are
1882// assumed to be correct, as the used parameters are verified by other tests
1883// in this collection. Primarily the reference files are all produced at
1884// "native" rates which do not involve any resampling.
1885
1886// Each test pass produces an output file with a particular format. The output
1887// is matched against the reference file closest to its internal processing
1888// format. If necessary the output is resampled back to its process format.
1889// Due to the resampling distortion, we don't expect identical results, but
1890// enforce SNR thresholds which vary depending on the format. 0 is a special
1891// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001892typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001893class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001894 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001895 public:
1896 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001897 : input_rate_(std::get<0>(GetParam())),
1898 output_rate_(std::get<1>(GetParam())),
1899 reverse_input_rate_(std::get<2>(GetParam())),
1900 reverse_output_rate_(std::get<3>(GetParam())),
1901 expected_snr_(std::get<4>(GetParam())),
1902 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001903
1904 virtual ~AudioProcessingTest() {}
1905
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001906 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001907 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001908 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001909 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001910 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1911 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1912 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001913 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001914 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1915 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1916 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001917 }
1918 }
1919 }
1920 }
1921
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001922 void TearDown() {
1923 // Remove "out" files after each test.
1924 ClearTempOutFiles();
1925 }
1926
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001927 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001928
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001929 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001930 // to a file specified with |output_file_prefix|. Both forward and reverse
1931 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001932 static void ProcessFormat(int input_rate,
1933 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001934 int reverse_input_rate,
1935 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001936 size_t num_input_channels,
1937 size_t num_output_channels,
1938 size_t num_reverse_input_channels,
1939 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001940 const std::string& output_file_prefix) {
Per Åhgren0695df12020-01-13 14:43:13 +01001941 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
1942 AudioProcessing::Config apm_config = ap->GetConfig();
1943 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1944 ap->ApplyConfig(apm_config);
1945
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001946 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001947
ekmeyerson60d9b332015-08-14 10:35:55 -07001948 ProcessingConfig processing_config = {
1949 {{input_rate, num_input_channels},
1950 {output_rate, num_output_channels},
1951 {reverse_input_rate, num_reverse_input_channels},
1952 {reverse_output_rate, num_reverse_output_channels}}};
1953 ap->Initialize(processing_config);
1954
1955 FILE* far_file =
1956 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001957 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001958 FILE* out_file = fopen(
1959 OutputFilePath(
1960 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1961 reverse_output_rate, num_input_channels, num_output_channels,
1962 num_reverse_input_channels, num_reverse_output_channels, kForward)
1963 .c_str(),
1964 "wb");
1965 FILE* rev_out_file = fopen(
1966 OutputFilePath(
1967 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1968 reverse_output_rate, num_input_channels, num_output_channels,
1969 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1970 .c_str(),
1971 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001972 ASSERT_TRUE(far_file != NULL);
1973 ASSERT_TRUE(near_file != NULL);
1974 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001975 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001976
1977 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1978 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001979 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1980 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001981 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1982 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001983 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1984 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001985
1986 // Temporary buffers.
1987 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001988 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1989 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001990 std::unique_ptr<float[]> float_data(new float[max_length]);
1991 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001992
1993 int analog_level = 127;
1994 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1995 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001996 EXPECT_NOERR(ap->ProcessReverseStream(
1997 rev_cb.channels(), processing_config.reverse_input_stream(),
1998 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001999
2000 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02002001 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002002
2003 EXPECT_NOERR(ap->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +01002004 fwd_cb.channels(), StreamConfig(input_rate, num_input_channels),
2005 StreamConfig(output_rate, num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002006
ekmeyerson60d9b332015-08-14 10:35:55 -07002007 // Dump forward output to file.
2008 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002009 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002010 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002011
Jonas Olssona4d87372019-07-05 19:08:33 +02002012 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2013 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002014
ekmeyerson60d9b332015-08-14 10:35:55 -07002015 // Dump reverse output to file.
2016 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2017 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002018 size_t rev_out_length =
2019 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002020
Jonas Olssona4d87372019-07-05 19:08:33 +02002021 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2022 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07002023
Sam Zackrisson41478c72019-10-15 10:10:26 +02002024 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002025 }
2026 fclose(far_file);
2027 fclose(near_file);
2028 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002029 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002030 }
2031
2032 protected:
2033 int input_rate_;
2034 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002035 int reverse_input_rate_;
2036 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002037 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002038 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002039};
2040
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002041TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002042 struct ChannelFormat {
2043 int num_input;
2044 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002045 int num_reverse_input;
2046 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002047 };
2048 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02002049 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
2050 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002051 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002052
pkasting25702cb2016-01-08 13:50:27 -08002053 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002054 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2055 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2056 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002057
ekmeyerson60d9b332015-08-14 10:35:55 -07002058 // Verify output for both directions.
2059 std::vector<StreamDirection> stream_directions;
2060 stream_directions.push_back(kForward);
2061 stream_directions.push_back(kReverse);
2062 for (StreamDirection file_direction : stream_directions) {
2063 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2064 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2065 const int out_num =
2066 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2067 const double expected_snr =
2068 file_direction ? expected_reverse_snr_ : expected_snr_;
2069
2070 const int min_ref_rate = std::min(in_rate, out_rate);
2071 int ref_rate;
2072
2073 if (min_ref_rate > 32000) {
2074 ref_rate = 48000;
2075 } else if (min_ref_rate > 16000) {
2076 ref_rate = 32000;
2077 } else if (min_ref_rate > 8000) {
2078 ref_rate = 16000;
2079 } else {
2080 ref_rate = 8000;
2081 }
Per Åhgrenc0424252019-12-10 13:04:15 +01002082
ekmeyerson60d9b332015-08-14 10:35:55 -07002083 FILE* out_file = fopen(
2084 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2085 reverse_output_rate_, cf[i].num_input,
2086 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02002087 cf[i].num_reverse_output, file_direction)
2088 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07002089 "rb");
2090 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02002091 FILE* ref_file =
2092 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2093 cf[i].num_output, cf[i].num_output,
2094 cf[i].num_reverse_output,
2095 cf[i].num_reverse_output, file_direction)
2096 .c_str(),
2097 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002098 ASSERT_TRUE(out_file != NULL);
2099 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002100
pkasting25702cb2016-01-08 13:50:27 -08002101 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2102 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002103 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002104 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002105 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002106 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002107 // Data from the resampled output, in case the reference and output rates
2108 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002109 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002110
ekmeyerson60d9b332015-08-14 10:35:55 -07002111 PushResampler<float> resampler;
2112 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002113
ekmeyerson60d9b332015-08-14 10:35:55 -07002114 // Compute the resampling delay of the output relative to the reference,
2115 // to find the region over which we should search for the best SNR.
2116 float expected_delay_sec = 0;
2117 if (in_rate != ref_rate) {
2118 // Input resampling delay.
2119 expected_delay_sec +=
2120 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2121 }
2122 if (out_rate != ref_rate) {
2123 // Output resampling delay.
2124 expected_delay_sec +=
2125 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2126 // Delay of converting the output back to its processing rate for
2127 // testing.
2128 expected_delay_sec +=
2129 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2130 }
2131 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002132 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002133
ekmeyerson60d9b332015-08-14 10:35:55 -07002134 double variance = 0;
2135 double sq_error = 0;
2136 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2137 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2138 float* out_ptr = out_data.get();
2139 if (out_rate != ref_rate) {
2140 // Resample the output back to its internal processing rate if
2141 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002142 ASSERT_EQ(ref_length,
2143 static_cast<size_t>(resampler.Resample(
2144 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002145 out_ptr = cmp_data.get();
2146 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002147
ekmeyerson60d9b332015-08-14 10:35:55 -07002148 // Update the |sq_error| and |variance| accumulators with the highest
2149 // SNR of reference vs output.
2150 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2151 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002152 }
2153
ekmeyerson60d9b332015-08-14 10:35:55 -07002154 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2155 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2156 << cf[i].num_input << ", " << cf[i].num_output << ", "
2157 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2158 << ", " << file_direction << "): ";
2159 if (sq_error > 0) {
2160 double snr = 10 * log10(variance / sq_error);
2161 EXPECT_GE(snr, expected_snr);
2162 EXPECT_NE(0, expected_snr);
2163 std::cout << "SNR=" << snr << " dB" << std::endl;
2164 } else {
aluebs776593b2016-03-15 14:04:58 -07002165 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002166 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002167
ekmeyerson60d9b332015-08-14 10:35:55 -07002168 fclose(out_file);
2169 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002170 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002171 }
2172}
2173
2174#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002175INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002176 CommonFormats,
2177 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002178 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2179 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2180 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2181 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2182 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2183 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2184 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2185 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2186 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2187 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2188 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2189 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002190
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002191 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2192 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2193 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2194 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2195 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2196 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2197 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2198 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2199 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2200 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2201 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2202 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002203
Per Åhgrenc0424252019-12-10 13:04:15 +01002204 std::make_tuple(32000, 48000, 48000, 48000, 15, 0),
2205 std::make_tuple(32000, 48000, 32000, 48000, 15, 30),
2206 std::make_tuple(32000, 48000, 16000, 48000, 15, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002207 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2208 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2209 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2210 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2211 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2212 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2213 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2214 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2215 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002216
Per Åhgrenc0424252019-12-10 13:04:15 +01002217 std::make_tuple(16000, 48000, 48000, 48000, 9, 0),
2218 std::make_tuple(16000, 48000, 32000, 48000, 9, 30),
2219 std::make_tuple(16000, 48000, 16000, 48000, 9, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002220 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2221 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2222 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2223 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2224 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2225 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2226 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2227 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2228 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002229
2230#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002231INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002232 CommonFormats,
2233 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002234 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2235 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2236 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002237 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2238 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2239 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002240 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2241 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2242 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002243 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2244 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2245 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002246
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002247 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2248 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2249 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2250 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2251 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2252 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002253 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2254 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2255 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2256 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2257 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2258 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002259
Per Åhgrenc0424252019-12-10 13:04:15 +01002260 std::make_tuple(32000, 48000, 48000, 48000, 17, 0),
2261 std::make_tuple(32000, 48000, 32000, 48000, 17, 30),
2262 std::make_tuple(32000, 48000, 16000, 48000, 17, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002263 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2264 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2265 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002266 std::make_tuple(32000, 32000, 48000, 32000, 27, 35),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002267 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002268 std::make_tuple(32000, 32000, 16000, 32000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002269 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2270 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2271 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002272
Per Åhgrenc0424252019-12-10 13:04:15 +01002273 std::make_tuple(16000, 48000, 48000, 48000, 11, 0),
2274 std::make_tuple(16000, 48000, 32000, 48000, 11, 30),
2275 std::make_tuple(16000, 48000, 16000, 48000, 11, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002276 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2277 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2278 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002279 std::make_tuple(16000, 32000, 48000, 32000, 24, 35),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002280 std::make_tuple(16000, 32000, 32000, 32000, 24, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002281 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002282 std::make_tuple(16000, 16000, 48000, 16000, 28, 20),
2283 std::make_tuple(16000, 16000, 32000, 16000, 28, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002284 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002285#endif
2286
Per Åhgren3e8bf282019-08-29 23:38:40 +02002287// Produces a scoped trace debug output.
2288std::string ProduceDebugText(int render_input_sample_rate_hz,
2289 int render_output_sample_rate_hz,
2290 int capture_input_sample_rate_hz,
2291 int capture_output_sample_rate_hz,
2292 size_t render_input_num_channels,
2293 size_t render_output_num_channels,
2294 size_t capture_input_num_channels,
2295 size_t capture_output_num_channels) {
2296 rtc::StringBuilder ss;
2297 ss << "Sample rates:"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002298 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002299 << render_input_sample_rate_hz
2300 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002301 "\n Render output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002302 << render_output_sample_rate_hz
2303 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002304 "\n Capture input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002305 << capture_input_sample_rate_hz
2306 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002307 "\n Capture output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002308 << capture_output_sample_rate_hz
2309 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002310 "\nNumber of channels:"
2311 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002312 << render_input_num_channels
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002313 << "\n Render output: " << render_output_num_channels
2314 << "\n Capture input: " << capture_input_num_channels
2315 << "\n Capture output: " << capture_output_num_channels;
Per Åhgren3e8bf282019-08-29 23:38:40 +02002316 return ss.Release();
2317}
2318
2319// Validates that running the audio processing module using various combinations
2320// of sample rates and number of channels works as intended.
2321void RunApmRateAndChannelTest(
2322 rtc::ArrayView<const int> sample_rates_hz,
2323 rtc::ArrayView<const int> render_channel_counts,
2324 rtc::ArrayView<const int> capture_channel_counts) {
2325 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2326 webrtc::AudioProcessing::Config apm_config;
2327 apm_config.echo_canceller.enabled = true;
2328 apm->ApplyConfig(apm_config);
2329
2330 StreamConfig render_input_stream_config;
2331 StreamConfig render_output_stream_config;
2332 StreamConfig capture_input_stream_config;
2333 StreamConfig capture_output_stream_config;
2334
2335 std::vector<float> render_input_frame_channels;
2336 std::vector<float*> render_input_frame;
2337 std::vector<float> render_output_frame_channels;
2338 std::vector<float*> render_output_frame;
2339 std::vector<float> capture_input_frame_channels;
2340 std::vector<float*> capture_input_frame;
2341 std::vector<float> capture_output_frame_channels;
2342 std::vector<float*> capture_output_frame;
2343
2344 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2345 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2346 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2347 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2348 for (size_t render_input_num_channels : render_channel_counts) {
2349 for (size_t capture_input_num_channels : capture_channel_counts) {
2350 size_t render_output_num_channels = render_input_num_channels;
2351 size_t capture_output_num_channels = capture_input_num_channels;
2352 auto populate_audio_frame = [](int sample_rate_hz,
2353 size_t num_channels,
2354 StreamConfig* cfg,
2355 std::vector<float>* channels_data,
2356 std::vector<float*>* frame_data) {
2357 cfg->set_sample_rate_hz(sample_rate_hz);
2358 cfg->set_num_channels(num_channels);
2359 cfg->set_has_keyboard(false);
2360
2361 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2362 channels_data->resize(num_channels * max_frame_size);
2363 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2364 frame_data->resize(num_channels);
2365 for (size_t channel = 0; channel < num_channels; ++channel) {
2366 (*frame_data)[channel] =
2367 &(*channels_data)[channel * max_frame_size];
2368 }
2369 };
2370
2371 populate_audio_frame(
2372 render_input_sample_rate_hz, render_input_num_channels,
2373 &render_input_stream_config, &render_input_frame_channels,
2374 &render_input_frame);
2375 populate_audio_frame(
2376 render_output_sample_rate_hz, render_output_num_channels,
2377 &render_output_stream_config, &render_output_frame_channels,
2378 &render_output_frame);
2379 populate_audio_frame(
2380 capture_input_sample_rate_hz, capture_input_num_channels,
2381 &capture_input_stream_config, &capture_input_frame_channels,
2382 &capture_input_frame);
2383 populate_audio_frame(
2384 capture_output_sample_rate_hz, capture_output_num_channels,
2385 &capture_output_stream_config, &capture_output_frame_channels,
2386 &capture_output_frame);
2387
2388 for (size_t frame = 0; frame < 2; ++frame) {
2389 SCOPED_TRACE(ProduceDebugText(
2390 render_input_sample_rate_hz, render_output_sample_rate_hz,
2391 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2392 render_input_num_channels, render_output_num_channels,
2393 render_input_num_channels, capture_output_num_channels));
2394
2395 int result = apm->ProcessReverseStream(
2396 &render_input_frame[0], render_input_stream_config,
2397 render_output_stream_config, &render_output_frame[0]);
2398 EXPECT_EQ(result, AudioProcessing::kNoError);
2399 result = apm->ProcessStream(
2400 &capture_input_frame[0], capture_input_stream_config,
2401 capture_output_stream_config, &capture_output_frame[0]);
2402 EXPECT_EQ(result, AudioProcessing::kNoError);
2403 }
2404 }
2405 }
2406 }
2407 }
2408 }
2409 }
2410}
2411
niklase@google.com470e71d2011-07-07 08:21:25 +00002412} // namespace
peahc19f3122016-10-07 14:54:10 -07002413
Alessio Bazzicac054e782018-04-16 12:10:09 +02002414TEST(RuntimeSettingTest, TestDefaultCtor) {
2415 auto s = AudioProcessing::RuntimeSetting();
2416 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2417}
2418
2419TEST(RuntimeSettingTest, TestCapturePreGain) {
2420 using Type = AudioProcessing::RuntimeSetting::Type;
2421 {
2422 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2423 EXPECT_EQ(Type::kCapturePreGain, s.type());
2424 float v;
2425 s.GetFloat(&v);
2426 EXPECT_EQ(1.25f, v);
2427 }
2428
2429#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2430 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2431#endif
2432}
2433
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002434TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2435 using Type = AudioProcessing::RuntimeSetting::Type;
2436 {
2437 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2438 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2439 float v;
2440 s.GetFloat(&v);
2441 EXPECT_EQ(1.25f, v);
2442 }
2443
2444#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2445 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2446#endif
2447}
2448
Alessio Bazzicac054e782018-04-16 12:10:09 +02002449TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2450 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2451 auto s = AudioProcessing::RuntimeSetting();
2452 ASSERT_TRUE(q.Insert(&s));
2453 ASSERT_TRUE(q.Remove(&s));
2454 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2455}
2456
Sam Zackrisson0beac582017-09-25 12:04:02 +02002457TEST(ApmConfiguration, EnablePostProcessing) {
2458 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002459 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002460 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002461 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002462 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002463 rtc::scoped_refptr<AudioProcessing> apm =
2464 AudioProcessingBuilder()
2465 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002466 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002467
Per Åhgren2507f8c2020-03-19 12:33:29 +01002468 Int16FrameData audio;
2469 audio.num_channels = 1;
Sam Zackrisson0beac582017-09-25 12:04:02 +02002470 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2471
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002472 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002473 apm->ProcessStream(audio.data.data(),
2474 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2475 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2476 audio.data.data(), &audio.vad_activity);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002477}
2478
Alex Loiko5825aa62017-12-18 16:02:40 +01002479TEST(ApmConfiguration, EnablePreProcessing) {
2480 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002481 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002482 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002483 auto mock_pre_processor =
2484 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002485 rtc::scoped_refptr<AudioProcessing> apm =
2486 AudioProcessingBuilder()
2487 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002488 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002489
Per Åhgren2507f8c2020-03-19 12:33:29 +01002490 Int16FrameData audio;
2491 audio.num_channels = 1;
Alex Loiko5825aa62017-12-18 16:02:40 +01002492 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2493
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002494 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002495 apm->ProcessReverseStream(
2496 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2497 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2498 audio.data.data());
Alex Loiko5825aa62017-12-18 16:02:40 +01002499}
2500
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002501TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2502 // Verify that apm uses a capture analyzer if one is provided.
2503 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002504 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002505 auto mock_capture_analyzer =
2506 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2507 rtc::scoped_refptr<AudioProcessing> apm =
2508 AudioProcessingBuilder()
2509 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2510 .Create();
2511
Per Åhgren2507f8c2020-03-19 12:33:29 +01002512 Int16FrameData audio;
2513 audio.num_channels = 1;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002514 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2515
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002516 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002517 apm->ProcessStream(audio.data.data(),
2518 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2519 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2520 audio.data.data(), &audio.vad_activity);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002521}
2522
Alex Loiko73ec0192018-05-15 10:52:28 +02002523TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2524 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002525 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002526 auto mock_pre_processor =
2527 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2528 rtc::scoped_refptr<AudioProcessing> apm =
2529 AudioProcessingBuilder()
2530 .SetRenderPreProcessing(std::move(mock_pre_processor))
2531 .Create();
2532 apm->SetRuntimeSetting(
2533 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2534
2535 // RuntimeSettings forwarded during 'Process*Stream' calls.
2536 // Therefore we have to make one such call.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002537 Int16FrameData audio;
2538 audio.num_channels = 1;
Alex Loiko73ec0192018-05-15 10:52:28 +02002539 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2540
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002541 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2542 .Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002543 apm->ProcessReverseStream(
2544 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2545 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2546 audio.data.data());
Alex Loiko73ec0192018-05-15 10:52:28 +02002547}
2548
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002549class MyEchoControlFactory : public EchoControlFactory {
2550 public:
2551 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2552 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002553 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2554 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
Per Åhgrenc20a19c2019-11-13 11:12:29 +01002555 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_, ::testing::_))
2556 .Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002557 return std::unique_ptr<EchoControl>(ec);
2558 }
Per Åhgrence202a02019-09-02 17:01:19 +02002559
2560 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
Per Åhgren4e5c7092019-11-01 20:44:11 +01002561 int num_render_channels,
2562 int num_capture_channels) {
Per Åhgrence202a02019-09-02 17:01:19 +02002563 return Create(sample_rate_hz);
2564 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002565};
2566
2567TEST(ApmConfiguration, EchoControlInjection) {
2568 // Verify that apm uses an injected echo controller if one is provided.
2569 webrtc::Config webrtc_config;
2570 std::unique_ptr<EchoControlFactory> echo_control_factory(
2571 new MyEchoControlFactory());
2572
Alex Loiko5825aa62017-12-18 16:02:40 +01002573 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002574 AudioProcessingBuilder()
2575 .SetEchoControlFactory(std::move(echo_control_factory))
2576 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002577
Per Åhgren2507f8c2020-03-19 12:33:29 +01002578 Int16FrameData audio;
2579 audio.num_channels = 1;
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002580 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002581 apm->ProcessStream(audio.data.data(),
2582 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2583 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2584 audio.data.data(), &audio.vad_activity);
2585 apm->ProcessReverseStream(
2586 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2587 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2588 audio.data.data());
2589 apm->ProcessStream(audio.data.data(),
2590 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2591 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2592 audio.data.data(), &audio.vad_activity);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002593}
Ivo Creusenae026092017-11-20 13:07:16 +01002594
Per Åhgren8607f842019-04-12 22:02:26 +02002595std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002596 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002597 std::unique_ptr<AudioProcessing> apm(
2598 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002599 if (!apm) {
2600 return apm;
2601 }
2602
2603 ProcessingConfig processing_config = {
2604 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2605
2606 if (apm->Initialize(processing_config) != 0) {
2607 return nullptr;
2608 }
2609
2610 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002611 AudioProcessing::Config apm_config;
2612 apm_config.residual_echo_detector.enabled = true;
2613 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002614 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002615 apm_config.gain_controller2.enabled = false;
2616 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002617 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002618 apm_config.noise_suppression.enabled = false;
2619 apm_config.level_estimation.enabled = false;
2620 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002621 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002622 return apm;
2623}
2624
2625#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2626#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2627#else
2628#define MAYBE_ApmStatistics ApmStatistics
2629#endif
2630
Per Åhgren8607f842019-04-12 22:02:26 +02002631TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2632 // Set up APM with AEC3 and process some audio.
2633 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002634 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002635 AudioProcessing::Config apm_config;
2636 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002637 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002638
2639 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002640 Int16FrameData frame;
2641 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002642 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002643
2644 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002645 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002646 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2647 ptr[i] = 10000 * ((i % 3) - 1);
2648 }
2649
2650 // Do some processing.
2651 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002652 EXPECT_EQ(apm->ProcessReverseStream(
2653 frame.data.data(),
2654 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2655 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2656 frame.data.data()),
2657 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002658 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002659 EXPECT_EQ(apm->ProcessStream(
2660 frame.data.data(),
2661 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2662 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2663 frame.data.data(), &frame.vad_activity),
2664 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002665 }
2666
2667 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002668 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002669 // We expect all statistics to be set and have a sensible value.
2670 ASSERT_TRUE(stats.residual_echo_likelihood);
2671 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2672 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2673 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2674 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2675 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2676 ASSERT_TRUE(stats.echo_return_loss);
2677 EXPECT_NE(*stats.echo_return_loss, -100.0);
2678 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2679 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002680}
2681
2682TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2683 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002684 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002685 ASSERT_TRUE(apm);
2686
2687 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002688 Int16FrameData frame;
2689 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002690 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002691
2692 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002693 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002694 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2695 ptr[i] = 10000 * ((i % 3) - 1);
2696 }
2697
2698 // Do some processing.
2699 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002700 EXPECT_EQ(apm->ProcessReverseStream(
2701 frame.data.data(),
2702 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2703 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2704 frame.data.data()),
2705 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002706 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002707 EXPECT_EQ(apm->ProcessStream(
2708 frame.data.data(),
2709 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2710 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2711 frame.data.data(), &frame.vad_activity),
2712 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002713 }
2714
2715 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002716 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002717 // We expect only the residual echo detector statistics to be set and have a
2718 // sensible value.
2719 EXPECT_TRUE(stats.residual_echo_likelihood);
2720 if (stats.residual_echo_likelihood) {
2721 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2722 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2723 }
2724 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2725 if (stats.residual_echo_likelihood_recent_max) {
2726 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2727 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2728 }
2729 EXPECT_FALSE(stats.echo_return_loss);
2730 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002731}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002732
2733TEST(ApmStatistics, ReportOutputRmsDbfs) {
2734 ProcessingConfig processing_config = {
2735 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2736 AudioProcessing::Config config;
2737
2738 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002739 Int16FrameData frame;
2740 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002741 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002742
2743 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002744 int16_t* ptr = frame.data.data();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002745 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2746 ptr[i] = 10000 * ((i % 3) - 1);
2747 }
2748
2749 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2750 apm->Initialize(processing_config);
2751
2752 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002753 EXPECT_EQ(
2754 apm->ProcessStream(frame.data.data(),
2755 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2756 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2757 frame.data.data(), &frame.vad_activity),
2758 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002759 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002760
2761 // If enabled, metrics should be reported.
2762 config.level_estimation.enabled = true;
2763 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002764 EXPECT_EQ(
2765 apm->ProcessStream(frame.data.data(),
2766 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2767 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2768 frame.data.data(), &frame.vad_activity),
2769 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002770 auto stats = apm->GetStatistics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002771 EXPECT_TRUE(stats.output_rms_dbfs);
2772 EXPECT_GE(*stats.output_rms_dbfs, 0);
2773
2774 // If re-disabled, the value is again not reported.
2775 config.level_estimation.enabled = false;
2776 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002777 EXPECT_EQ(
2778 apm->ProcessStream(frame.data.data(),
2779 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2780 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2781 frame.data.data(), &frame.vad_activity),
2782 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002783 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002784}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002785
2786TEST(ApmStatistics, ReportHasVoice) {
2787 ProcessingConfig processing_config = {
2788 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2789 AudioProcessing::Config config;
2790
2791 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002792 Int16FrameData frame;
2793 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002794 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2795
2796 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002797 int16_t* ptr = frame.data.data();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002798 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2799 ptr[i] = 10000 * ((i % 3) - 1);
2800 }
2801
2802 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2803 apm->Initialize(processing_config);
2804
2805 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002806 EXPECT_EQ(
2807 apm->ProcessStream(frame.data.data(),
2808 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2809 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2810 frame.data.data(), &frame.vad_activity),
2811 0);
2812 EXPECT_EQ(frame.vad_activity,
2813 AudioProcessing::VoiceDetectionResult::kNotAvailable);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002814 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002815
2816 // If enabled, metrics should be reported.
2817 config.voice_detection.enabled = true;
2818 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002819 EXPECT_EQ(
2820 apm->ProcessStream(frame.data.data(),
2821 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2822 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2823 frame.data.data(), &frame.vad_activity),
2824 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002825 auto stats = apm->GetStatistics();
Per Åhgren2507f8c2020-03-19 12:33:29 +01002826 EXPECT_EQ(frame.vad_activity,
2827 AudioProcessing::VoiceDetectionResult::kDetected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002828 EXPECT_TRUE(stats.voice_detected);
2829
2830 // If re-disabled, the value is again not reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002831 frame.vad_activity = AudioProcessing::VoiceDetectionResult::kNotAvailable;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002832 config.voice_detection.enabled = false;
2833 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002834 EXPECT_EQ(
2835 apm->ProcessStream(frame.data.data(),
2836 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2837 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2838 frame.data.data(), &frame.vad_activity),
2839 0);
2840 EXPECT_EQ(frame.vad_activity,
2841 AudioProcessing::VoiceDetectionResult::kNotAvailable);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002842 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002843}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002844
2845TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2846 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2847 std::array<int, 2> render_channel_counts = {1, 7};
2848 std::array<int, 2> capture_channel_counts = {1, 7};
2849 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2850 capture_channel_counts);
2851}
2852
2853TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2854 std::array<int, 1> sample_rates_hz = {48000};
2855 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2856 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2857 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2858 capture_channel_counts);
2859}
2860
2861TEST(ApmConfiguration, HandlingOfRateCombinations) {
2862 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2863 48000, 96000, 192000, 384000};
2864 std::array<int, 1> render_channel_counts = {2};
2865 std::array<int, 1> capture_channel_counts = {2};
2866 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2867 capture_channel_counts);
2868}
2869
Yves Gerey1fce3f82019-12-05 17:45:31 +01002870TEST(ApmConfiguration, SelfAssignment) {
2871 // At some point memory sanitizer was complaining about self-assigment.
2872 // Make sure we don't regress.
2873 AudioProcessing::Config config;
2874 AudioProcessing::Config* config2 = &config;
2875 *config2 = *config2; // Workaround -Wself-assign-overloaded
2876 SUCCEED(); // Real success is absence of defects from asan/msan/ubsan.
2877}
2878
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002879} // namespace webrtc