blob: cdca7c35244e3704a53761b11bcd35ad2c8c04c5 [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),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100564 frame_.data.data());
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),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100649 process_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000650 frame = frame_queue.front();
651 frame_queue.pop();
652 delete frame;
653
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000654 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000655 // Discard the first delay metrics to avoid convergence effects.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100656 static_cast<void>(apm_->GetStatistics());
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000657 }
658 }
659
660 rewind(near_file_);
661 while (!frame_queue.empty()) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100662 Int16FrameData* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 frame_queue.pop();
664 delete frame;
665 }
666 // Calculate expected delay estimate and acceptable regions. Further,
667 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700668 const size_t samples_per_ms =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100669 rtc::SafeMin<size_t>(16u, frame_.samples_per_channel / 10);
kwiberg07038562017-06-12 11:40:47 -0700670 const int expected_median =
671 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
672 const int expected_median_high = rtc::SafeClamp<int>(
673 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700674 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700675 const int expected_median_low = rtc::SafeClamp<int>(
676 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000678 // Verify delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100679 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200680 ASSERT_TRUE(stats.delay_median_ms.has_value());
681 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000682 EXPECT_GE(expected_median_high, median);
683 EXPECT_LE(expected_median_low, median);
684}
685
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000686void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000687 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000688 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000689
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000690 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200691 AudioProcessing::Config apm_config = apm_->GetConfig();
692 apm_config.gain_controller1.enabled = true;
693 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200694 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000695
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000696 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200697 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000698 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200699 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000700
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000701 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200702 apm_config.echo_canceller.enabled = true;
703 apm_config.echo_canceller.mobile_mode = false;
704 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000705 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200706 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200707 apm_config.gain_controller1.enabled = false;
708 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000709
710 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000711 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713
714 // Resets after successful ProcessStream().
715 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000716 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100717 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000718
719 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200720 apm_config.gain_controller1.enabled = true;
721 apm_->ApplyConfig(apm_config);
722 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100723 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200724 apm_config.gain_controller1.enabled = false;
725 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000726
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200728 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100729 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000731 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000732 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200733 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000734 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000735}
736
737TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000738 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000739}
740
741TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000743}
744
Michael Graczyk86c6d332015-07-23 11:41:39 -0700745void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800746 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700747 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100748 frame_.num_channels = num_channels;
749
750 EXPECT_EQ(expected_return,
751 apm_->ProcessStream(
752 frame_.data.data(),
753 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
754 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100755 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100756 EXPECT_EQ(expected_return,
757 apm_->ProcessReverseStream(
758 frame_.data.data(),
759 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
760 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
761 frame_.data.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000762}
763
Michael Graczyk86c6d332015-07-23 11:41:39 -0700764void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800765 size_t num_in_channels,
766 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700767 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100768 const StreamConfig input_stream = {frame_.sample_rate_hz, num_in_channels};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700769 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
770
771 EXPECT_EQ(expected_return,
772 apm_->ProcessStream(float_cb_->channels(), input_stream,
773 output_stream, float_cb_->channels()));
774}
775
776void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800777 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700778 AudioProcessing::Error expected_return) {
779 const ProcessingConfig processing_config = {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100780 {{frame_.sample_rate_hz, apm_->num_input_channels()},
ekmeyerson60d9b332015-08-14 10:35:55 -0700781 {output_sample_rate_hz_, apm_->num_output_channels()},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100782 {frame_.sample_rate_hz, num_rev_channels},
783 {frame_.sample_rate_hz, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700784
ekmeyerson60d9b332015-08-14 10:35:55 -0700785 EXPECT_EQ(
786 expected_return,
787 apm_->ProcessReverseStream(
788 float_cb_->channels(), processing_config.reverse_input_stream(),
789 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700790}
791
792TEST_F(ApmTest, ChannelsInt16Interface) {
793 // Testing number of invalid and valid channels.
794 Init(16000, 16000, 16000, 4, 4, 4, false);
795
796 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
797
Peter Kasting69558702016-01-12 16:26:35 -0800798 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700799 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000800 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 }
802}
803
Michael Graczyk86c6d332015-07-23 11:41:39 -0700804TEST_F(ApmTest, Channels) {
805 // Testing number of invalid and valid channels.
806 Init(16000, 16000, 16000, 4, 4, 4, false);
807
808 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
809 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
810
Peter Kasting69558702016-01-12 16:26:35 -0800811 for (size_t i = 1; i < 4; ++i) {
812 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700813 // Output channels much be one or match input channels.
814 if (j == 1 || i == j) {
815 TestChangingForwardChannels(i, j, kNoErr);
816 TestChangingReverseChannels(i, kNoErr);
817
818 EXPECT_EQ(i, apm_->num_input_channels());
819 EXPECT_EQ(j, apm_->num_output_channels());
820 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800821 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700822 } else {
823 TestChangingForwardChannels(i, j,
824 AudioProcessing::kBadNumberChannelsError);
825 }
826 }
827 }
828}
829
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000830TEST_F(ApmTest, SampleRatesInt) {
Sam Zackrisson12e319a2020-01-03 14:54:20 +0100831 // Testing some valid sample rates.
832 for (int sample_rate : {8000, 12000, 16000, 32000, 44100, 48000, 96000}) {
833 SetContainerFormat(sample_rate, 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000834 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 }
836}
837
Sam Zackrissone277bde2019-10-25 10:07:54 +0200838// This test repeatedly reconfigures the pre-amplifier in APM, processes a
839// number of frames, and checks that output signal has the right level.
840TEST_F(ApmTest, PreAmplifier) {
841 // Fill the audio frame with a sawtooth pattern.
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200842 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100843 const size_t samples_per_channel = frame_.samples_per_channel;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200844 for (size_t i = 0; i < samples_per_channel; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100845 for (size_t ch = 0; ch < frame_.num_channels; ++ch) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200846 frame_data[i + ch * samples_per_channel] = 10000 * ((i % 3) - 1);
847 }
848 }
849 // Cache the frame in tmp_frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100850 Int16FrameData tmp_frame;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200851 tmp_frame.CopyFrom(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200852
Per Åhgren2507f8c2020-03-19 12:33:29 +0100853 auto compute_power = [](const Int16FrameData& frame) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200854 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
855 return std::accumulate(data.begin(), data.end(), 0.0f,
856 [](float a, float b) { return a + b * b; }) /
857 data.size() / 32768 / 32768;
858 };
859
860 const float input_power = compute_power(tmp_frame);
861 // Double-check that the input data is large compared to the error kEpsilon.
862 constexpr float kEpsilon = 1e-4f;
863 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
864
865 // 1. Enable pre-amp with 0 dB gain.
866 AudioProcessing::Config config = apm_->GetConfig();
867 config.pre_amplifier.enabled = true;
868 config.pre_amplifier.fixed_gain_factor = 1.0f;
869 apm_->ApplyConfig(config);
870
871 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200872 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200873 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
874 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200875 float output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200876 EXPECT_NEAR(output_power, input_power, kEpsilon);
877 config = apm_->GetConfig();
878 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.0f);
879
880 // 2. Change pre-amp gain via ApplyConfig.
881 config.pre_amplifier.fixed_gain_factor = 2.0f;
882 apm_->ApplyConfig(config);
883
884 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200885 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200886 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
887 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200888 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200889 EXPECT_NEAR(output_power, 4 * input_power, kEpsilon);
890 config = apm_->GetConfig();
891 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 2.0f);
892
893 // 3. Change pre-amp gain via a RuntimeSetting.
894 apm_->SetRuntimeSetting(
895 AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.5f));
896
897 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200898 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200899 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
900 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200901 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200902 EXPECT_NEAR(output_power, 2.25 * input_power, kEpsilon);
903 config = apm_->GetConfig();
904 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.5f);
905}
906
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000907TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200908 AudioProcessing::Config config = apm_->GetConfig();
909 config.gain_controller1.enabled = false;
910 apm_->ApplyConfig(config);
911 config.gain_controller1.enabled = true;
912 apm_->ApplyConfig(config);
913
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +0200915 for (auto mode :
916 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
917 AudioProcessing::Config::GainController1::kFixedDigital,
918 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
919 config.gain_controller1.mode = mode;
920 apm_->ApplyConfig(config);
921 apm_->set_stream_analog_level(100);
922 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000923 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000924
Sam Zackrisson41478c72019-10-15 10:10:26 +0200925 // Testing target levels
926 for (int target_level_dbfs : {0, 15, 31}) {
927 config.gain_controller1.target_level_dbfs = target_level_dbfs;
928 apm_->ApplyConfig(config);
929 apm_->set_stream_analog_level(100);
930 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000931 }
932
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100933 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +0200934 for (int compression_gain_db : {0, 10, 90}) {
935 config.gain_controller1.compression_gain_db = compression_gain_db;
936 apm_->ApplyConfig(config);
937 apm_->set_stream_analog_level(100);
938 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000939 }
940
941 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +0200942 for (bool enable : {false, true}) {
943 config.gain_controller1.enable_limiter = enable;
944 apm_->ApplyConfig(config);
945 apm_->set_stream_analog_level(100);
946 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
947 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000948
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100949 // Testing level limits
Sam Zackrisson41478c72019-10-15 10:10:26 +0200950 std::array<int, 4> kMinLevels = {0, 0, 255, 65000};
951 std::array<int, 4> kMaxLevels = {255, 1024, 65535, 65535};
952 for (size_t i = 0; i < kMinLevels.size(); ++i) {
953 int min_level = kMinLevels[i];
954 int max_level = kMaxLevels[i];
955 config.gain_controller1.analog_level_minimum = min_level;
956 config.gain_controller1.analog_level_maximum = max_level;
957 apm_->ApplyConfig(config);
958 apm_->set_stream_analog_level((min_level + max_level) / 2);
959 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000960 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000961}
962
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100963#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
964TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200965 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100966 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200967 config.gain_controller1.target_level_dbfs = -1;
968 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100969}
970
971TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200972 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100973 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200974 config.gain_controller1.target_level_dbfs = 32;
975 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100976}
977
978TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200979 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100980 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200981 config.gain_controller1.compression_gain_db = -1;
982 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100983}
984
985TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200986 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100987 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200988 config.gain_controller1.compression_gain_db = 91;
989 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100990}
991
992TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200993 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100994 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200995 config.gain_controller1.analog_level_minimum = -1;
996 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100997}
998
999TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001000 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001001 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001002 config.gain_controller1.analog_level_maximum = 65536;
1003 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001004}
1005
1006TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001007 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001008 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001009 config.gain_controller1.analog_level_minimum = 512;
1010 config.gain_controller1.analog_level_maximum = 255;
1011 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001012}
1013
1014TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001015 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001016 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001017 config.gain_controller1.analog_level_minimum = 255;
1018 config.gain_controller1.analog_level_maximum = 512;
1019 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001020 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
1021}
1022
1023TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001024 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001025 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001026 config.gain_controller1.analog_level_minimum = 255;
1027 config.gain_controller1.analog_level_maximum = 512;
1028 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001029 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
1030}
1031#endif
1032
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001033void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001034 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001035 auto config = apm_->GetConfig();
1036 config.gain_controller1.enabled = true;
1037 config.gain_controller1.mode =
1038 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1039 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001040
1041 int out_analog_level = 0;
1042 for (int i = 0; i < 2000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001043 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001044 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001045 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001046
1047 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +02001048 apm_->set_stream_analog_level(100);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001049 EXPECT_EQ(apm_->kNoError,
1050 apm_->ProcessStream(
1051 frame_.data.data(),
1052 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1053 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001054 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001055 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001056 }
1057
1058 // Ensure the AGC is still able to reach the maximum.
1059 EXPECT_EQ(255, out_analog_level);
1060}
1061
1062// Verifies that despite volume slider quantization, the AGC can continue to
1063// increase its volume.
1064TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001065 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001066 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1067 }
1068}
1069
1070void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001071 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001072 auto config = apm_->GetConfig();
1073 config.gain_controller1.enabled = true;
1074 config.gain_controller1.mode =
1075 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1076 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001077
1078 int out_analog_level = 100;
1079 for (int i = 0; i < 1000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001080 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001081 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001082 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001083
Sam Zackrisson41478c72019-10-15 10:10:26 +02001084 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001085 EXPECT_EQ(apm_->kNoError,
1086 apm_->ProcessStream(
1087 frame_.data.data(),
1088 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1089 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001090 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001091 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001092 }
1093
1094 // Ensure the volume was raised.
1095 EXPECT_GT(out_analog_level, 100);
1096 int highest_level_reached = out_analog_level;
1097 // Simulate a user manual volume change.
1098 out_analog_level = 100;
1099
1100 for (int i = 0; i < 300; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001101 ReadFrameWithRewind(near_file_, &frame_);
1102 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001103
Sam Zackrisson41478c72019-10-15 10:10:26 +02001104 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001105 EXPECT_EQ(apm_->kNoError,
1106 apm_->ProcessStream(
1107 frame_.data.data(),
1108 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1109 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001110 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001111 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001112 // Check that AGC respected the manually adjusted volume.
1113 EXPECT_LT(out_analog_level, highest_level_reached);
1114 }
1115 // Check that the volume was still raised.
1116 EXPECT_GT(out_analog_level, 100);
1117}
1118
1119TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001120 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001121 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1122 }
1123}
1124
niklase@google.com470e71d2011-07-07 08:21:25 +00001125TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001126 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001127 AudioProcessing::Config apm_config;
1128 apm_config.high_pass_filter.enabled = true;
1129 apm_->ApplyConfig(apm_config);
1130 apm_config.high_pass_filter.enabled = false;
1131 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001132}
1133
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001134TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001135 AudioProcessing::Config config = apm_->GetConfig();
1136 EXPECT_FALSE(config.echo_canceller.enabled);
1137 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001138 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001139 EXPECT_FALSE(config.level_estimation.enabled);
saza0bad15f2019-10-16 11:46:11 +02001140 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001141 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001142}
1143
1144TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001145 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001146 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001147 SetFrameTo(&frame_, 1000, 2000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001148 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001149 frame_copy.CopyFrom(frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001150 for (int j = 0; j < 1000; j++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001151 EXPECT_EQ(apm_->kNoError,
1152 apm_->ProcessStream(
1153 frame_.data.data(),
1154 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1155 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001156 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001157 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001158 EXPECT_EQ(apm_->kNoError,
1159 apm_->ProcessReverseStream(
1160 frame_.data.data(),
1161 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1162 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1163 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001164 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001165 }
1166 }
1167}
1168
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001169TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1170 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001171 const size_t kSamples = 160;
1172 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001173 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001174 float dest[kSamples] = {};
1175
1176 auto src_channels = &src[0];
1177 auto dest_channels = &dest[0];
1178
Ivo Creusen62337e52018-01-09 14:17:33 +01001179 apm_.reset(AudioProcessingBuilder().Create());
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001180 EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
1181 StreamConfig(sample_rate, 1),
1182 &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001183
1184 for (size_t i = 0; i < kSamples; ++i) {
1185 EXPECT_EQ(src[i], dest[i]);
1186 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001187
1188 // Same for ProcessReverseStream.
1189 float rev_dest[kSamples] = {};
1190 auto rev_dest_channels = &rev_dest[0];
1191
1192 StreamConfig input_stream = {sample_rate, 1};
1193 StreamConfig output_stream = {sample_rate, 1};
1194 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1195 output_stream, &rev_dest_channels));
1196
1197 for (size_t i = 0; i < kSamples; ++i) {
1198 EXPECT_EQ(src[i], rev_dest[i]);
1199 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001200}
1201
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001202TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1203 EnableAllComponents();
1204
pkasting25702cb2016-01-08 13:50:27 -08001205 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001206 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1207 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001208 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001209 ASSERT_EQ(0, feof(far_file_));
1210 ASSERT_EQ(0, feof(near_file_));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001211 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001212 CopyLeftToRightChannel(revframe_.data.data(),
1213 revframe_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001214
Per Åhgren2507f8c2020-03-19 12:33:29 +01001215 ASSERT_EQ(
1216 kNoErr,
1217 apm_->ProcessReverseStream(
1218 revframe_.data.data(),
1219 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1220 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1221 revframe_.data.data()));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001222
Per Åhgren2507f8c2020-03-19 12:33:29 +01001223 CopyLeftToRightChannel(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001224
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001225 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001226 apm_->set_stream_analog_level(analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001227 ASSERT_EQ(kNoErr,
1228 apm_->ProcessStream(
1229 frame_.data.data(),
1230 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1231 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001232 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001233 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001234
Per Åhgren2507f8c2020-03-19 12:33:29 +01001235 VerifyChannelsAreEqual(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001236 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001237 rewind(far_file_);
1238 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001239 }
1240}
1241
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001242TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001243 // Verify the filter is not active through undistorted audio when:
1244 // 1. No components are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001245 SetFrameTo(&frame_, 1000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001246 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001247 frame_copy.CopyFrom(frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001248 EXPECT_EQ(apm_->kNoError,
1249 apm_->ProcessStream(
1250 frame_.data.data(),
1251 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1252 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001253 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001254 EXPECT_EQ(apm_->kNoError,
1255 apm_->ProcessStream(
1256 frame_.data.data(),
1257 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1258 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001259 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001260 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001261
1262 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001263 auto apm_config = apm_->GetConfig();
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001264 SetFrameTo(&frame_, 1000);
1265 frame_copy.CopyFrom(frame_);
saza6787f232019-10-11 19:31:07 +02001266 apm_config.level_estimation.enabled = true;
1267 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001268 EXPECT_EQ(apm_->kNoError,
1269 apm_->ProcessStream(
1270 frame_.data.data(),
1271 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1272 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001273 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001274 EXPECT_EQ(apm_->kNoError,
1275 apm_->ProcessStream(
1276 frame_.data.data(),
1277 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1278 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001279 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001280 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001281 apm_config.level_estimation.enabled = false;
1282 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001283
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001284 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001285 SetFrameTo(&frame_, 1000);
1286 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001287 apm_config.voice_detection.enabled = true;
1288 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001289 EXPECT_EQ(apm_->kNoError,
1290 apm_->ProcessStream(
1291 frame_.data.data(),
1292 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1293 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001294 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001295 EXPECT_EQ(apm_->kNoError,
1296 apm_->ProcessStream(
1297 frame_.data.data(),
1298 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1299 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001300 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001301 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001302 apm_config.voice_detection.enabled = false;
1303 apm_->ApplyConfig(apm_config);
1304
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001305 // 4. Both the VAD and the level estimator are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001306 SetFrameTo(&frame_, 1000);
1307 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001308 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001309 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001310 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001311 EXPECT_EQ(apm_->kNoError,
1312 apm_->ProcessStream(
1313 frame_.data.data(),
1314 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1315 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001316 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001317 EXPECT_EQ(apm_->kNoError,
1318 apm_->ProcessStream(
1319 frame_.data.data(),
1320 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1321 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001322 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001323 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001324 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001325 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001326 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001327
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001328 // Check the test is valid. We should have distortion from the filter
1329 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001330 apm_config.echo_canceller.enabled = true;
1331 apm_config.echo_canceller.mobile_mode = false;
1332 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001333 frame_.samples_per_channel = 320;
1334 frame_.num_channels = 2;
1335 frame_.sample_rate_hz = 32000;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001336 SetFrameTo(&frame_, 1000);
1337 frame_copy.CopyFrom(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001338 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001339 EXPECT_EQ(apm_->kNoError,
1340 apm_->ProcessStream(
1341 frame_.data.data(),
1342 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1343 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001344 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001345 EXPECT_FALSE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001346}
1347
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001348#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1349void ApmTest::ProcessDebugDump(const std::string& in_filename,
1350 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001351 Format format,
1352 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001353 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001354 FILE* in_file = fopen(in_filename.c_str(), "rb");
1355 ASSERT_TRUE(in_file != NULL);
1356 audioproc::Event event_msg;
1357 bool first_init = true;
1358
1359 while (ReadMessageFromFile(in_file, &event_msg)) {
1360 if (event_msg.type() == audioproc::Event::INIT) {
1361 const audioproc::Init msg = event_msg.init();
1362 int reverse_sample_rate = msg.sample_rate();
1363 if (msg.has_reverse_sample_rate()) {
1364 reverse_sample_rate = msg.reverse_sample_rate();
1365 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001366 int output_sample_rate = msg.sample_rate();
1367 if (msg.has_output_sample_rate()) {
1368 output_sample_rate = msg.output_sample_rate();
1369 }
1370
Jonas Olssona4d87372019-07-05 19:08:33 +02001371 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1372 msg.num_input_channels(), msg.num_output_channels(),
1373 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001374 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001375 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001376 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001377 auto aec_dump =
1378 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1379 EXPECT_TRUE(aec_dump);
1380 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001381 first_init = false;
1382 }
1383
1384 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1385 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1386
1387 if (msg.channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001388 ASSERT_EQ(revframe_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001389 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001390 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001391 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1392 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001393 }
1394 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001395 memcpy(revframe_.data.data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001396 if (format == kFloatFormat) {
1397 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001398 ConvertToFloat(revframe_, revfloat_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001399 }
1400 }
1401 AnalyzeReverseStreamChooser(format);
1402
1403 } else if (event_msg.type() == audioproc::Event::STREAM) {
1404 const audioproc::Stream msg = event_msg.stream();
1405 // ProcessStream could have changed this for the output frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001406 frame_.num_channels = apm_->num_input_channels();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001407
Sam Zackrisson41478c72019-10-15 10:10:26 +02001408 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001409 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410 if (msg.has_keypress()) {
1411 apm_->set_stream_key_pressed(msg.keypress());
1412 } else {
1413 apm_->set_stream_key_pressed(true);
1414 }
1415
1416 if (msg.input_channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001417 ASSERT_EQ(frame_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001418 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001419 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001420 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1421 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001422 }
1423 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001424 memcpy(frame_.data.data(), msg.input_data().data(),
yujo36b1a5f2017-06-12 12:45:32 -07001425 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001426 if (format == kFloatFormat) {
1427 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001428 ConvertToFloat(frame_, float_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001429 }
1430 }
1431 ProcessStreamChooser(format);
1432 }
1433 }
aleloif4dd1912017-06-15 01:55:38 -07001434 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001435 fclose(in_file);
1436}
1437
1438void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001439 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001440 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001441 std::string format_string;
1442 switch (format) {
1443 case kIntFormat:
1444 format_string = "_int";
1445 break;
1446 case kFloatFormat:
1447 format_string = "_float";
1448 break;
1449 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001450 const std::string ref_filename = test::TempFilename(
1451 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1452 const std::string out_filename = test::TempFilename(
1453 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001454 const std::string limited_filename = test::TempFilename(
1455 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1456 const size_t logging_limit_bytes = 100000;
1457 // We expect at least this many bytes in the created logfile.
1458 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001459 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001460 ProcessDebugDump(in_filename, ref_filename, format, -1);
1461 ProcessDebugDump(ref_filename, out_filename, format, -1);
1462 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001463
1464 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1465 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001466 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001467 ASSERT_TRUE(ref_file != NULL);
1468 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001469 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001470 std::unique_ptr<uint8_t[]> ref_bytes;
1471 std::unique_ptr<uint8_t[]> out_bytes;
1472 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001473
1474 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1475 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001476 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001477 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001478 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001479 while (ref_size > 0 && out_size > 0) {
1480 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001481 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001482 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001483 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001484 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001485 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001486 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1487 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001488 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001489 }
1490 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001491 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1492 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001493 EXPECT_NE(0, feof(ref_file));
1494 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001495 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001496 ASSERT_EQ(0, fclose(ref_file));
1497 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001498 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001499 remove(ref_filename.c_str());
1500 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001501 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001502}
1503
pbosc7a65692016-05-06 12:50:04 -07001504TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001505 VerifyDebugDumpTest(kIntFormat);
1506}
1507
pbosc7a65692016-05-06 12:50:04 -07001508TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001509 VerifyDebugDumpTest(kFloatFormat);
1510}
1511#endif
1512
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001513// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001514TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001515 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001516 const std::string filename =
1517 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001518 {
1519 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1520 EXPECT_FALSE(aec_dump);
1521 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001522
1523#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1524 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001525 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001526
aleloif4dd1912017-06-15 01:55:38 -07001527 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1528 EXPECT_TRUE(aec_dump);
1529 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001530 EXPECT_EQ(apm_->kNoError,
1531 apm_->ProcessStream(
1532 frame_.data.data(),
1533 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1534 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001535 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001536 EXPECT_EQ(apm_->kNoError,
1537 apm_->ProcessReverseStream(
1538 revframe_.data.data(),
1539 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1540 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1541 revframe_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001542 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001543
1544 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001545 FILE* fid = fopen(filename.c_str(), "r");
1546 ASSERT_TRUE(fid != NULL);
1547
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001548 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001549 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001550 ASSERT_EQ(0, remove(filename.c_str()));
1551#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001552 // Verify the file has NOT been written.
1553 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1554#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1555}
1556
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001557// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001558TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001559 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001560
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001561 const std::string filename =
1562 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001563 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1564 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001565
1566#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1567 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001568 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001569
Niels Möllere8e4dc42019-06-11 14:04:16 +02001570 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001571 EXPECT_TRUE(aec_dump);
1572 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001573 EXPECT_EQ(apm_->kNoError,
1574 apm_->ProcessReverseStream(
1575 revframe_.data.data(),
1576 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1577 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1578 revframe_.data.data()));
1579 EXPECT_EQ(apm_->kNoError,
1580 apm_->ProcessStream(
1581 frame_.data.data(),
1582 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1583 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001584 frame_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001585 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001586
1587 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001588 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001589 ASSERT_TRUE(fid != NULL);
1590
1591 // Clean it up.
1592 ASSERT_EQ(0, fclose(fid));
1593 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001594#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1595}
1596
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001597// TODO(andrew): Add a test to process a few frames with different combinations
1598// of enabled components.
1599
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001600TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001601 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001602 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001603
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001604 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001605 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001606 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001607 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001608 for (size_t i = 0; i < arraysize(kChannels); i++) {
1609 for (size_t j = 0; j < arraysize(kChannels); j++) {
1610 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001611 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001612 test->set_num_reverse_channels(kChannels[i]);
1613 test->set_num_input_channels(kChannels[j]);
1614 test->set_num_output_channels(kChannels[j]);
1615 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001616 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001617 }
1618 }
1619 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001620#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1621 // To test the extended filter mode.
1622 audioproc::Test* test = ref_data.add_test();
1623 test->set_num_reverse_channels(2);
1624 test->set_num_input_channels(2);
1625 test->set_num_output_channels(2);
1626 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1627 test->set_use_aec_extended_filter(true);
1628#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001629 }
1630
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001631 for (int i = 0; i < ref_data.test_size(); i++) {
1632 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001633
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001634 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001635 // TODO(ajm): We no longer allow different input and output channels. Skip
1636 // these tests for now, but they should be removed from the set.
1637 if (test->num_input_channels() != test->num_output_channels())
1638 continue;
1639
Per Åhgren0695df12020-01-13 14:43:13 +01001640 apm_.reset(AudioProcessingBuilder().Create());
1641 AudioProcessing::Config apm_config = apm_->GetConfig();
1642 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1643 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001644
1645 EnableAllComponents();
1646
Jonas Olssona4d87372019-07-05 19:08:33 +02001647 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001648 static_cast<size_t>(test->num_input_channels()),
1649 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001650 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001651
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001652 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001653 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001654 int analog_level = 127;
1655 int analog_level_average = 0;
1656 int max_output_average = 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001657 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001658#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001659 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001660#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001661
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001662 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001663 EXPECT_EQ(
1664 apm_->kNoError,
1665 apm_->ProcessReverseStream(
1666 revframe_.data.data(),
1667 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1668 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1669 revframe_.data.data()));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001670
1671 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001672 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001673
Per Åhgren2507f8c2020-03-19 12:33:29 +01001674 EXPECT_EQ(apm_->kNoError,
1675 apm_->ProcessStream(
1676 frame_.data.data(),
1677 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1678 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001679 frame_.data.data()));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001680
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001681 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001682 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01001683 frame_.num_channels);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001684
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001685 max_output_average += MaxAudioFrame(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001686
Sam Zackrisson41478c72019-10-15 10:10:26 +02001687 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001688 analog_level_average += analog_level;
Per Åhgrencf4c8722019-12-30 14:32:14 +01001689 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001690 EXPECT_TRUE(stats.voice_detected);
1691 EXPECT_TRUE(stats.output_rms_dbfs);
1692 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001693 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001694
Per Åhgren2507f8c2020-03-19 12:33:29 +01001695 size_t frame_size = frame_.samples_per_channel * frame_.num_channels;
Jonas Olssona4d87372019-07-05 19:08:33 +02001696 size_t write_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +01001697 fwrite(frame_.data.data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001698 ASSERT_EQ(frame_size, write_count);
1699
1700 // Reset in case of downmixing.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001701 frame_.num_channels = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001702 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001703
1704#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1705 const int kStatsAggregationFrameNum = 100; // 1 second.
1706 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001707 // Get echo and delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +01001708 AudioProcessingStats stats = apm_->GetStatistics();
minyue58530ed2016-05-24 05:50:12 -07001709
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001710 // Echo metrics.
1711 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1712 const float echo_return_loss_enhancement =
1713 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001714 const float residual_echo_likelihood =
1715 stats.residual_echo_likelihood.value_or(-1.0f);
1716 const float residual_echo_likelihood_recent_max =
1717 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1718
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001719 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001720 const audioproc::Test::EchoMetrics& reference =
1721 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001722 constexpr float kEpsilon = 0.01;
1723 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1724 EXPECT_NEAR(echo_return_loss_enhancement,
1725 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001726 EXPECT_NEAR(residual_echo_likelihood,
1727 reference.residual_echo_likelihood(), kEpsilon);
1728 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1729 reference.residual_echo_likelihood_recent_max(),
1730 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001731 ++stats_index;
1732 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001733 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1734 message_echo->set_echo_return_loss(echo_return_loss);
1735 message_echo->set_echo_return_loss_enhancement(
1736 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001737 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1738 message_echo->set_residual_echo_likelihood_recent_max(
1739 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001740 }
1741 }
1742#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001743 }
1744 max_output_average /= frame_count;
1745 analog_level_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001746 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001747
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001748 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001749 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001750 // When running the test on a N7 we get a {2, 6} difference of
1751 // |has_voice_count| and |max_output_average| is up to 18 higher.
1752 // All numbers being consistently higher on N7 compare to ref_data.
1753 // TODO(bjornv): If we start getting more of these offsets on Android we
1754 // should consider a different approach. Either using one slack for all,
1755 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001756#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001757 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001758 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001759 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001760 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001761#else
1762 const int kHasVoiceCountOffset = 0;
1763 const int kHasVoiceCountNear = kIntNear;
1764 const int kMaxOutputAverageOffset = 0;
1765 const int kMaxOutputAverageNear = kIntNear;
1766#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001767 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001768 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001769
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001770 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001771 EXPECT_NEAR(test->max_output_average(),
1772 max_output_average - kMaxOutputAverageOffset,
1773 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001774#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001775 const double kFloatNear = 0.0005;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001776 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001777#endif
1778 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001779 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001780
1781 test->set_analog_level_average(analog_level_average);
1782 test->set_max_output_average(max_output_average);
1783
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001784#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrisson11b87032018-12-18 17:13:58 +01001785 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001786#endif
1787 }
1788
1789 rewind(far_file_);
1790 rewind(near_file_);
1791 }
1792
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001793 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001794 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001795 }
1796}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001797
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001798TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1799 struct ChannelFormat {
1800 AudioProcessing::ChannelLayout in_layout;
1801 AudioProcessing::ChannelLayout out_layout;
1802 };
1803 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001804 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1805 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1806 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001807 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001808
Ivo Creusen62337e52018-01-09 14:17:33 +01001809 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001810 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001811 AudioProcessing::Config config;
1812 config.noise_suppression.enabled = true;
1813 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001814 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001815 const int in_rate = 44100;
1816 const int out_rate = 48000;
1817 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1818 TotalChannelsFromLayout(cf[i].in_layout));
1819 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1820 ChannelsFromLayout(cf[i].out_layout));
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001821 bool has_keyboard = cf[i].in_layout == AudioProcessing::kMonoAndKeyboard ||
1822 cf[i].in_layout == AudioProcessing::kStereoAndKeyboard;
1823 StreamConfig in_sc(in_rate, ChannelsFromLayout(cf[i].in_layout),
1824 has_keyboard);
1825 StreamConfig out_sc(out_rate, ChannelsFromLayout(cf[i].out_layout));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001826
1827 // Run over a few chunks.
1828 for (int j = 0; j < 10; ++j) {
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001829 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_sc, out_sc,
1830 out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001831 }
1832 }
1833}
1834
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001835// Compares the reference and test arrays over a region around the expected
1836// delay. Finds the highest SNR in that region and adds the variance and squared
1837// error results to the supplied accumulators.
1838void UpdateBestSNR(const float* ref,
1839 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001840 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001841 int expected_delay,
1842 double* variance_acc,
1843 double* sq_error_acc) {
1844 double best_snr = std::numeric_limits<double>::min();
1845 double best_variance = 0;
1846 double best_sq_error = 0;
1847 // Search over a region of eight samples around the expected delay.
1848 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1849 ++delay) {
1850 double sq_error = 0;
1851 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001852 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001853 double error = test[i + delay] - ref[i];
1854 sq_error += error * error;
1855 variance += ref[i] * ref[i];
1856 }
1857
1858 if (sq_error == 0) {
1859 *variance_acc += variance;
1860 return;
1861 }
1862 double snr = variance / sq_error;
1863 if (snr > best_snr) {
1864 best_snr = snr;
1865 best_variance = variance;
1866 best_sq_error = sq_error;
1867 }
1868 }
1869
1870 *variance_acc += best_variance;
1871 *sq_error_acc += best_sq_error;
1872}
1873
1874// Used to test a multitude of sample rate and channel combinations. It works
1875// by first producing a set of reference files (in SetUpTestCase) that are
1876// assumed to be correct, as the used parameters are verified by other tests
1877// in this collection. Primarily the reference files are all produced at
1878// "native" rates which do not involve any resampling.
1879
1880// Each test pass produces an output file with a particular format. The output
1881// is matched against the reference file closest to its internal processing
1882// format. If necessary the output is resampled back to its process format.
1883// Due to the resampling distortion, we don't expect identical results, but
1884// enforce SNR thresholds which vary depending on the format. 0 is a special
1885// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001886typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001887class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001888 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001889 public:
1890 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001891 : input_rate_(std::get<0>(GetParam())),
1892 output_rate_(std::get<1>(GetParam())),
1893 reverse_input_rate_(std::get<2>(GetParam())),
1894 reverse_output_rate_(std::get<3>(GetParam())),
1895 expected_snr_(std::get<4>(GetParam())),
1896 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001897
1898 virtual ~AudioProcessingTest() {}
1899
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001900 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001901 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001902 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001903 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001904 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1905 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1906 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001907 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001908 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1909 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1910 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001911 }
1912 }
1913 }
1914 }
1915
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001916 void TearDown() {
1917 // Remove "out" files after each test.
1918 ClearTempOutFiles();
1919 }
1920
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001921 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001922
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001923 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001924 // to a file specified with |output_file_prefix|. Both forward and reverse
1925 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001926 static void ProcessFormat(int input_rate,
1927 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001928 int reverse_input_rate,
1929 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001930 size_t num_input_channels,
1931 size_t num_output_channels,
1932 size_t num_reverse_input_channels,
1933 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001934 const std::string& output_file_prefix) {
Per Åhgren0695df12020-01-13 14:43:13 +01001935 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
1936 AudioProcessing::Config apm_config = ap->GetConfig();
1937 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1938 ap->ApplyConfig(apm_config);
1939
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001940 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001941
ekmeyerson60d9b332015-08-14 10:35:55 -07001942 ProcessingConfig processing_config = {
1943 {{input_rate, num_input_channels},
1944 {output_rate, num_output_channels},
1945 {reverse_input_rate, num_reverse_input_channels},
1946 {reverse_output_rate, num_reverse_output_channels}}};
1947 ap->Initialize(processing_config);
1948
1949 FILE* far_file =
1950 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001951 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001952 FILE* out_file = fopen(
1953 OutputFilePath(
1954 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1955 reverse_output_rate, num_input_channels, num_output_channels,
1956 num_reverse_input_channels, num_reverse_output_channels, kForward)
1957 .c_str(),
1958 "wb");
1959 FILE* rev_out_file = fopen(
1960 OutputFilePath(
1961 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1962 reverse_output_rate, num_input_channels, num_output_channels,
1963 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1964 .c_str(),
1965 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001966 ASSERT_TRUE(far_file != NULL);
1967 ASSERT_TRUE(near_file != NULL);
1968 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001969 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001970
1971 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1972 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001973 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1974 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001975 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1976 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001977 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1978 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001979
1980 // Temporary buffers.
1981 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001982 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1983 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001984 std::unique_ptr<float[]> float_data(new float[max_length]);
1985 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001986
1987 int analog_level = 127;
1988 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1989 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001990 EXPECT_NOERR(ap->ProcessReverseStream(
1991 rev_cb.channels(), processing_config.reverse_input_stream(),
1992 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001993
1994 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001995 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001996
1997 EXPECT_NOERR(ap->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001998 fwd_cb.channels(), StreamConfig(input_rate, num_input_channels),
1999 StreamConfig(output_rate, num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002000
ekmeyerson60d9b332015-08-14 10:35:55 -07002001 // Dump forward output to file.
2002 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002003 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002004 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002005
Jonas Olssona4d87372019-07-05 19:08:33 +02002006 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2007 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002008
ekmeyerson60d9b332015-08-14 10:35:55 -07002009 // Dump reverse output to file.
2010 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2011 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002012 size_t rev_out_length =
2013 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002014
Jonas Olssona4d87372019-07-05 19:08:33 +02002015 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2016 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07002017
Sam Zackrisson41478c72019-10-15 10:10:26 +02002018 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002019 }
2020 fclose(far_file);
2021 fclose(near_file);
2022 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002023 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002024 }
2025
2026 protected:
2027 int input_rate_;
2028 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002029 int reverse_input_rate_;
2030 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002031 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002032 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002033};
2034
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002035TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002036 struct ChannelFormat {
2037 int num_input;
2038 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002039 int num_reverse_input;
2040 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002041 };
2042 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02002043 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
2044 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002045 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002046
pkasting25702cb2016-01-08 13:50:27 -08002047 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002048 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2049 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2050 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002051
ekmeyerson60d9b332015-08-14 10:35:55 -07002052 // Verify output for both directions.
2053 std::vector<StreamDirection> stream_directions;
2054 stream_directions.push_back(kForward);
2055 stream_directions.push_back(kReverse);
2056 for (StreamDirection file_direction : stream_directions) {
2057 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2058 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2059 const int out_num =
2060 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2061 const double expected_snr =
2062 file_direction ? expected_reverse_snr_ : expected_snr_;
2063
2064 const int min_ref_rate = std::min(in_rate, out_rate);
2065 int ref_rate;
2066
2067 if (min_ref_rate > 32000) {
2068 ref_rate = 48000;
2069 } else if (min_ref_rate > 16000) {
2070 ref_rate = 32000;
2071 } else if (min_ref_rate > 8000) {
2072 ref_rate = 16000;
2073 } else {
2074 ref_rate = 8000;
2075 }
Per Åhgrenc0424252019-12-10 13:04:15 +01002076
ekmeyerson60d9b332015-08-14 10:35:55 -07002077 FILE* out_file = fopen(
2078 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2079 reverse_output_rate_, cf[i].num_input,
2080 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02002081 cf[i].num_reverse_output, file_direction)
2082 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07002083 "rb");
2084 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02002085 FILE* ref_file =
2086 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2087 cf[i].num_output, cf[i].num_output,
2088 cf[i].num_reverse_output,
2089 cf[i].num_reverse_output, file_direction)
2090 .c_str(),
2091 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002092 ASSERT_TRUE(out_file != NULL);
2093 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002094
pkasting25702cb2016-01-08 13:50:27 -08002095 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2096 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002097 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002098 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002099 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002100 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002101 // Data from the resampled output, in case the reference and output rates
2102 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002103 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002104
ekmeyerson60d9b332015-08-14 10:35:55 -07002105 PushResampler<float> resampler;
2106 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002107
ekmeyerson60d9b332015-08-14 10:35:55 -07002108 // Compute the resampling delay of the output relative to the reference,
2109 // to find the region over which we should search for the best SNR.
2110 float expected_delay_sec = 0;
2111 if (in_rate != ref_rate) {
2112 // Input resampling delay.
2113 expected_delay_sec +=
2114 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2115 }
2116 if (out_rate != ref_rate) {
2117 // Output resampling delay.
2118 expected_delay_sec +=
2119 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2120 // Delay of converting the output back to its processing rate for
2121 // testing.
2122 expected_delay_sec +=
2123 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2124 }
2125 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002126 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002127
ekmeyerson60d9b332015-08-14 10:35:55 -07002128 double variance = 0;
2129 double sq_error = 0;
2130 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2131 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2132 float* out_ptr = out_data.get();
2133 if (out_rate != ref_rate) {
2134 // Resample the output back to its internal processing rate if
2135 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002136 ASSERT_EQ(ref_length,
2137 static_cast<size_t>(resampler.Resample(
2138 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002139 out_ptr = cmp_data.get();
2140 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002141
ekmeyerson60d9b332015-08-14 10:35:55 -07002142 // Update the |sq_error| and |variance| accumulators with the highest
2143 // SNR of reference vs output.
2144 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2145 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002146 }
2147
ekmeyerson60d9b332015-08-14 10:35:55 -07002148 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2149 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2150 << cf[i].num_input << ", " << cf[i].num_output << ", "
2151 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2152 << ", " << file_direction << "): ";
2153 if (sq_error > 0) {
2154 double snr = 10 * log10(variance / sq_error);
2155 EXPECT_GE(snr, expected_snr);
2156 EXPECT_NE(0, expected_snr);
2157 std::cout << "SNR=" << snr << " dB" << std::endl;
2158 } else {
aluebs776593b2016-03-15 14:04:58 -07002159 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002160 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002161
ekmeyerson60d9b332015-08-14 10:35:55 -07002162 fclose(out_file);
2163 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002164 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002165 }
2166}
2167
2168#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002169INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002170 CommonFormats,
2171 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002172 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2173 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2174 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2175 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2176 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2177 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2178 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2179 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2180 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2181 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2182 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2183 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002184
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002185 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2186 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2187 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2188 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2189 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2190 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2191 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2192 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2193 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2194 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2195 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2196 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002197
Per Åhgrenc0424252019-12-10 13:04:15 +01002198 std::make_tuple(32000, 48000, 48000, 48000, 15, 0),
2199 std::make_tuple(32000, 48000, 32000, 48000, 15, 30),
2200 std::make_tuple(32000, 48000, 16000, 48000, 15, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002201 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2202 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2203 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2204 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2205 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2206 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2207 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2208 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2209 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002210
Per Åhgrenc0424252019-12-10 13:04:15 +01002211 std::make_tuple(16000, 48000, 48000, 48000, 9, 0),
2212 std::make_tuple(16000, 48000, 32000, 48000, 9, 30),
2213 std::make_tuple(16000, 48000, 16000, 48000, 9, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002214 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2215 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2216 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2217 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2218 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2219 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2220 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2221 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2222 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002223
2224#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002225INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002226 CommonFormats,
2227 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002228 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2229 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2230 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002231 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2232 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2233 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002234 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2235 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2236 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002237 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2238 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2239 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002240
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002241 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2242 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2243 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2244 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2245 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2246 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002247 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2248 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2249 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2250 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2251 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2252 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002253
Per Åhgrenc0424252019-12-10 13:04:15 +01002254 std::make_tuple(32000, 48000, 48000, 48000, 17, 0),
2255 std::make_tuple(32000, 48000, 32000, 48000, 17, 30),
2256 std::make_tuple(32000, 48000, 16000, 48000, 17, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002257 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2258 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2259 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002260 std::make_tuple(32000, 32000, 48000, 32000, 27, 35),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002261 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002262 std::make_tuple(32000, 32000, 16000, 32000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002263 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2264 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2265 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002266
Per Åhgrenc0424252019-12-10 13:04:15 +01002267 std::make_tuple(16000, 48000, 48000, 48000, 11, 0),
2268 std::make_tuple(16000, 48000, 32000, 48000, 11, 30),
2269 std::make_tuple(16000, 48000, 16000, 48000, 11, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002270 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2271 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2272 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002273 std::make_tuple(16000, 32000, 48000, 32000, 24, 35),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002274 std::make_tuple(16000, 32000, 32000, 32000, 24, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002275 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002276 std::make_tuple(16000, 16000, 48000, 16000, 28, 20),
2277 std::make_tuple(16000, 16000, 32000, 16000, 28, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002278 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002279#endif
2280
Per Åhgren3e8bf282019-08-29 23:38:40 +02002281// Produces a scoped trace debug output.
2282std::string ProduceDebugText(int render_input_sample_rate_hz,
2283 int render_output_sample_rate_hz,
2284 int capture_input_sample_rate_hz,
2285 int capture_output_sample_rate_hz,
2286 size_t render_input_num_channels,
2287 size_t render_output_num_channels,
2288 size_t capture_input_num_channels,
2289 size_t capture_output_num_channels) {
2290 rtc::StringBuilder ss;
2291 ss << "Sample rates:"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002292 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002293 << render_input_sample_rate_hz
2294 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002295 "\n Render output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002296 << render_output_sample_rate_hz
2297 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002298 "\n Capture input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002299 << capture_input_sample_rate_hz
2300 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002301 "\n Capture output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002302 << capture_output_sample_rate_hz
2303 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002304 "\nNumber of channels:"
2305 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002306 << render_input_num_channels
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002307 << "\n Render output: " << render_output_num_channels
2308 << "\n Capture input: " << capture_input_num_channels
2309 << "\n Capture output: " << capture_output_num_channels;
Per Åhgren3e8bf282019-08-29 23:38:40 +02002310 return ss.Release();
2311}
2312
2313// Validates that running the audio processing module using various combinations
2314// of sample rates and number of channels works as intended.
2315void RunApmRateAndChannelTest(
2316 rtc::ArrayView<const int> sample_rates_hz,
2317 rtc::ArrayView<const int> render_channel_counts,
2318 rtc::ArrayView<const int> capture_channel_counts) {
2319 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2320 webrtc::AudioProcessing::Config apm_config;
2321 apm_config.echo_canceller.enabled = true;
2322 apm->ApplyConfig(apm_config);
2323
2324 StreamConfig render_input_stream_config;
2325 StreamConfig render_output_stream_config;
2326 StreamConfig capture_input_stream_config;
2327 StreamConfig capture_output_stream_config;
2328
2329 std::vector<float> render_input_frame_channels;
2330 std::vector<float*> render_input_frame;
2331 std::vector<float> render_output_frame_channels;
2332 std::vector<float*> render_output_frame;
2333 std::vector<float> capture_input_frame_channels;
2334 std::vector<float*> capture_input_frame;
2335 std::vector<float> capture_output_frame_channels;
2336 std::vector<float*> capture_output_frame;
2337
2338 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2339 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2340 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2341 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2342 for (size_t render_input_num_channels : render_channel_counts) {
2343 for (size_t capture_input_num_channels : capture_channel_counts) {
2344 size_t render_output_num_channels = render_input_num_channels;
2345 size_t capture_output_num_channels = capture_input_num_channels;
2346 auto populate_audio_frame = [](int sample_rate_hz,
2347 size_t num_channels,
2348 StreamConfig* cfg,
2349 std::vector<float>* channels_data,
2350 std::vector<float*>* frame_data) {
2351 cfg->set_sample_rate_hz(sample_rate_hz);
2352 cfg->set_num_channels(num_channels);
2353 cfg->set_has_keyboard(false);
2354
2355 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2356 channels_data->resize(num_channels * max_frame_size);
2357 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2358 frame_data->resize(num_channels);
2359 for (size_t channel = 0; channel < num_channels; ++channel) {
2360 (*frame_data)[channel] =
2361 &(*channels_data)[channel * max_frame_size];
2362 }
2363 };
2364
2365 populate_audio_frame(
2366 render_input_sample_rate_hz, render_input_num_channels,
2367 &render_input_stream_config, &render_input_frame_channels,
2368 &render_input_frame);
2369 populate_audio_frame(
2370 render_output_sample_rate_hz, render_output_num_channels,
2371 &render_output_stream_config, &render_output_frame_channels,
2372 &render_output_frame);
2373 populate_audio_frame(
2374 capture_input_sample_rate_hz, capture_input_num_channels,
2375 &capture_input_stream_config, &capture_input_frame_channels,
2376 &capture_input_frame);
2377 populate_audio_frame(
2378 capture_output_sample_rate_hz, capture_output_num_channels,
2379 &capture_output_stream_config, &capture_output_frame_channels,
2380 &capture_output_frame);
2381
2382 for (size_t frame = 0; frame < 2; ++frame) {
2383 SCOPED_TRACE(ProduceDebugText(
2384 render_input_sample_rate_hz, render_output_sample_rate_hz,
2385 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2386 render_input_num_channels, render_output_num_channels,
2387 render_input_num_channels, capture_output_num_channels));
2388
2389 int result = apm->ProcessReverseStream(
2390 &render_input_frame[0], render_input_stream_config,
2391 render_output_stream_config, &render_output_frame[0]);
2392 EXPECT_EQ(result, AudioProcessing::kNoError);
2393 result = apm->ProcessStream(
2394 &capture_input_frame[0], capture_input_stream_config,
2395 capture_output_stream_config, &capture_output_frame[0]);
2396 EXPECT_EQ(result, AudioProcessing::kNoError);
2397 }
2398 }
2399 }
2400 }
2401 }
2402 }
2403 }
2404}
2405
niklase@google.com470e71d2011-07-07 08:21:25 +00002406} // namespace
peahc19f3122016-10-07 14:54:10 -07002407
Alessio Bazzicac054e782018-04-16 12:10:09 +02002408TEST(RuntimeSettingTest, TestDefaultCtor) {
2409 auto s = AudioProcessing::RuntimeSetting();
2410 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2411}
2412
2413TEST(RuntimeSettingTest, TestCapturePreGain) {
2414 using Type = AudioProcessing::RuntimeSetting::Type;
2415 {
2416 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2417 EXPECT_EQ(Type::kCapturePreGain, s.type());
2418 float v;
2419 s.GetFloat(&v);
2420 EXPECT_EQ(1.25f, v);
2421 }
2422
2423#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2424 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2425#endif
2426}
2427
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002428TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2429 using Type = AudioProcessing::RuntimeSetting::Type;
2430 {
2431 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2432 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2433 float v;
2434 s.GetFloat(&v);
2435 EXPECT_EQ(1.25f, v);
2436 }
2437
2438#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2439 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2440#endif
2441}
2442
Alessio Bazzicac054e782018-04-16 12:10:09 +02002443TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2444 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2445 auto s = AudioProcessing::RuntimeSetting();
2446 ASSERT_TRUE(q.Insert(&s));
2447 ASSERT_TRUE(q.Remove(&s));
2448 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2449}
2450
Sam Zackrisson0beac582017-09-25 12:04:02 +02002451TEST(ApmConfiguration, EnablePostProcessing) {
2452 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002453 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002454 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002455 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002456 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002457 rtc::scoped_refptr<AudioProcessing> apm =
2458 AudioProcessingBuilder()
2459 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002460 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002461
Per Åhgren2507f8c2020-03-19 12:33:29 +01002462 Int16FrameData audio;
2463 audio.num_channels = 1;
Sam Zackrisson0beac582017-09-25 12:04:02 +02002464 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2465
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002466 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002467 apm->ProcessStream(audio.data.data(),
2468 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2469 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002470 audio.data.data());
Sam Zackrisson0beac582017-09-25 12:04:02 +02002471}
2472
Alex Loiko5825aa62017-12-18 16:02:40 +01002473TEST(ApmConfiguration, EnablePreProcessing) {
2474 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002475 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002476 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002477 auto mock_pre_processor =
2478 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002479 rtc::scoped_refptr<AudioProcessing> apm =
2480 AudioProcessingBuilder()
2481 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002482 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002483
Per Åhgren2507f8c2020-03-19 12:33:29 +01002484 Int16FrameData audio;
2485 audio.num_channels = 1;
Alex Loiko5825aa62017-12-18 16:02:40 +01002486 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2487
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002488 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002489 apm->ProcessReverseStream(
2490 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2491 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2492 audio.data.data());
Alex Loiko5825aa62017-12-18 16:02:40 +01002493}
2494
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002495TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2496 // Verify that apm uses a capture analyzer if one is provided.
2497 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002498 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002499 auto mock_capture_analyzer =
2500 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2501 rtc::scoped_refptr<AudioProcessing> apm =
2502 AudioProcessingBuilder()
2503 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2504 .Create();
2505
Per Åhgren2507f8c2020-03-19 12:33:29 +01002506 Int16FrameData audio;
2507 audio.num_channels = 1;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002508 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2509
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002510 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002511 apm->ProcessStream(audio.data.data(),
2512 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2513 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002514 audio.data.data());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002515}
2516
Alex Loiko73ec0192018-05-15 10:52:28 +02002517TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2518 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002519 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002520 auto mock_pre_processor =
2521 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2522 rtc::scoped_refptr<AudioProcessing> apm =
2523 AudioProcessingBuilder()
2524 .SetRenderPreProcessing(std::move(mock_pre_processor))
2525 .Create();
2526 apm->SetRuntimeSetting(
2527 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2528
2529 // RuntimeSettings forwarded during 'Process*Stream' calls.
2530 // Therefore we have to make one such call.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002531 Int16FrameData audio;
2532 audio.num_channels = 1;
Alex Loiko73ec0192018-05-15 10:52:28 +02002533 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2534
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002535 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2536 .Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002537 apm->ProcessReverseStream(
2538 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2539 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2540 audio.data.data());
Alex Loiko73ec0192018-05-15 10:52:28 +02002541}
2542
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002543class MyEchoControlFactory : public EchoControlFactory {
2544 public:
2545 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2546 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002547 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2548 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
Per Åhgrenc20a19c2019-11-13 11:12:29 +01002549 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_, ::testing::_))
2550 .Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002551 return std::unique_ptr<EchoControl>(ec);
2552 }
Per Åhgrence202a02019-09-02 17:01:19 +02002553
2554 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
Per Åhgren4e5c7092019-11-01 20:44:11 +01002555 int num_render_channels,
2556 int num_capture_channels) {
Per Åhgrence202a02019-09-02 17:01:19 +02002557 return Create(sample_rate_hz);
2558 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002559};
2560
2561TEST(ApmConfiguration, EchoControlInjection) {
2562 // Verify that apm uses an injected echo controller if one is provided.
2563 webrtc::Config webrtc_config;
2564 std::unique_ptr<EchoControlFactory> echo_control_factory(
2565 new MyEchoControlFactory());
2566
Alex Loiko5825aa62017-12-18 16:02:40 +01002567 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002568 AudioProcessingBuilder()
2569 .SetEchoControlFactory(std::move(echo_control_factory))
2570 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002571
Per Åhgren2507f8c2020-03-19 12:33:29 +01002572 Int16FrameData audio;
2573 audio.num_channels = 1;
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002574 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002575 apm->ProcessStream(audio.data.data(),
2576 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2577 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002578 audio.data.data());
Per Åhgren2507f8c2020-03-19 12:33:29 +01002579 apm->ProcessReverseStream(
2580 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2581 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2582 audio.data.data());
2583 apm->ProcessStream(audio.data.data(),
2584 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2585 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002586 audio.data.data());
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002587}
Ivo Creusenae026092017-11-20 13:07:16 +01002588
Per Åhgren8607f842019-04-12 22:02:26 +02002589std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002590 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002591 std::unique_ptr<AudioProcessing> apm(
2592 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002593 if (!apm) {
2594 return apm;
2595 }
2596
2597 ProcessingConfig processing_config = {
2598 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2599
2600 if (apm->Initialize(processing_config) != 0) {
2601 return nullptr;
2602 }
2603
2604 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002605 AudioProcessing::Config apm_config;
2606 apm_config.residual_echo_detector.enabled = true;
2607 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002608 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002609 apm_config.gain_controller2.enabled = false;
2610 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002611 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002612 apm_config.noise_suppression.enabled = false;
2613 apm_config.level_estimation.enabled = false;
2614 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002615 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002616 return apm;
2617}
2618
2619#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2620#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2621#else
2622#define MAYBE_ApmStatistics ApmStatistics
2623#endif
2624
Per Åhgren8607f842019-04-12 22:02:26 +02002625TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2626 // Set up APM with AEC3 and process some audio.
2627 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002628 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002629 AudioProcessing::Config apm_config;
2630 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002631 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002632
2633 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002634 Int16FrameData frame;
2635 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002636 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002637
2638 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002639 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002640 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2641 ptr[i] = 10000 * ((i % 3) - 1);
2642 }
2643
2644 // Do some processing.
2645 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002646 EXPECT_EQ(apm->ProcessReverseStream(
2647 frame.data.data(),
2648 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2649 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2650 frame.data.data()),
2651 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002652 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002653 EXPECT_EQ(apm->ProcessStream(
2654 frame.data.data(),
2655 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2656 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002657 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002658 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002659 }
2660
2661 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002662 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002663 // We expect all statistics to be set and have a sensible value.
2664 ASSERT_TRUE(stats.residual_echo_likelihood);
2665 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2666 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2667 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2668 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2669 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2670 ASSERT_TRUE(stats.echo_return_loss);
2671 EXPECT_NE(*stats.echo_return_loss, -100.0);
2672 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2673 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002674}
2675
2676TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2677 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002678 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002679 ASSERT_TRUE(apm);
2680
2681 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002682 Int16FrameData frame;
2683 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002684 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002685
2686 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002687 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002688 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2689 ptr[i] = 10000 * ((i % 3) - 1);
2690 }
2691
2692 // Do some processing.
2693 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002694 EXPECT_EQ(apm->ProcessReverseStream(
2695 frame.data.data(),
2696 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2697 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2698 frame.data.data()),
2699 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002700 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002701 EXPECT_EQ(apm->ProcessStream(
2702 frame.data.data(),
2703 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2704 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002705 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002706 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002707 }
2708
2709 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002710 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002711 // We expect only the residual echo detector statistics to be set and have a
2712 // sensible value.
2713 EXPECT_TRUE(stats.residual_echo_likelihood);
2714 if (stats.residual_echo_likelihood) {
2715 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2716 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2717 }
2718 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2719 if (stats.residual_echo_likelihood_recent_max) {
2720 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2721 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2722 }
2723 EXPECT_FALSE(stats.echo_return_loss);
2724 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002725}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002726
2727TEST(ApmStatistics, ReportOutputRmsDbfs) {
2728 ProcessingConfig processing_config = {
2729 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2730 AudioProcessing::Config config;
2731
2732 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002733 Int16FrameData frame;
2734 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002735 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002736
2737 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002738 int16_t* ptr = frame.data.data();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002739 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2740 ptr[i] = 10000 * ((i % 3) - 1);
2741 }
2742
2743 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2744 apm->Initialize(processing_config);
2745
2746 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002747 EXPECT_EQ(
2748 apm->ProcessStream(frame.data.data(),
2749 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2750 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002751 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002752 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002753 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002754
2755 // If enabled, metrics should be reported.
2756 config.level_estimation.enabled = true;
2757 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002758 EXPECT_EQ(
2759 apm->ProcessStream(frame.data.data(),
2760 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2761 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002762 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002763 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002764 auto stats = apm->GetStatistics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002765 EXPECT_TRUE(stats.output_rms_dbfs);
2766 EXPECT_GE(*stats.output_rms_dbfs, 0);
2767
2768 // If re-disabled, the value is again not reported.
2769 config.level_estimation.enabled = false;
2770 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002771 EXPECT_EQ(
2772 apm->ProcessStream(frame.data.data(),
2773 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2774 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002775 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002776 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002777 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002778}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002779
2780TEST(ApmStatistics, ReportHasVoice) {
2781 ProcessingConfig processing_config = {
2782 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2783 AudioProcessing::Config config;
2784
2785 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002786 Int16FrameData frame;
2787 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002788 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2789
2790 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002791 int16_t* ptr = frame.data.data();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002792 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2793 ptr[i] = 10000 * ((i % 3) - 1);
2794 }
2795
2796 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2797 apm->Initialize(processing_config);
2798
2799 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002800 EXPECT_EQ(
2801 apm->ProcessStream(frame.data.data(),
2802 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2803 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002804 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002805 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002806 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002807
2808 // If enabled, metrics should be reported.
2809 config.voice_detection.enabled = true;
2810 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002811 EXPECT_EQ(
2812 apm->ProcessStream(frame.data.data(),
2813 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2814 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002815 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002816 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002817 auto stats = apm->GetStatistics();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002818 EXPECT_TRUE(stats.voice_detected);
2819
2820 // If re-disabled, the value is again not reported.
2821 config.voice_detection.enabled = false;
2822 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002823 EXPECT_EQ(
2824 apm->ProcessStream(frame.data.data(),
2825 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2826 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002827 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002828 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002829 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002830}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002831
2832TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2833 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2834 std::array<int, 2> render_channel_counts = {1, 7};
2835 std::array<int, 2> capture_channel_counts = {1, 7};
2836 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2837 capture_channel_counts);
2838}
2839
2840TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2841 std::array<int, 1> sample_rates_hz = {48000};
2842 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2843 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2844 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2845 capture_channel_counts);
2846}
2847
2848TEST(ApmConfiguration, HandlingOfRateCombinations) {
2849 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2850 48000, 96000, 192000, 384000};
2851 std::array<int, 1> render_channel_counts = {2};
2852 std::array<int, 1> capture_channel_counts = {2};
2853 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2854 capture_channel_counts);
2855}
2856
Yves Gerey1fce3f82019-12-05 17:45:31 +01002857TEST(ApmConfiguration, SelfAssignment) {
2858 // At some point memory sanitizer was complaining about self-assigment.
2859 // Make sure we don't regress.
2860 AudioProcessing::Config config;
2861 AudioProcessing::Config* config2 = &config;
2862 *config2 = *config2; // Workaround -Wself-assign-overloaded
2863 SUCCEED(); // Real success is absence of defects from asan/msan/ubsan.
2864}
2865
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002866} // namespace webrtc