blob: 93ddc973660ef35411a3aa6d01ec5836e906fbec [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "modules/audio_processing/include/audio_processing.h"
11
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000012#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000013#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080014
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000015#include <algorithm>
Oleh Prypin708eccc2019-03-27 09:38:52 +010016#include <cmath>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000017#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080018#include <memory>
Sam Zackrissone277bde2019-10-25 10:07:54 +020019#include <numeric>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000020#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000021
Sam Zackrisson6558fa52019-08-26 10:12:41 +020022#include "absl/flags/flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
24#include "common_audio/resampler/include/push_resampler.h"
25#include "common_audio/resampler/push_sinc_resampler.h"
26#include "common_audio/signal_processing/include/signal_processing_library.h"
27#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
28#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/common.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020030#include "modules/audio_processing/include/mock_audio_processing.h"
Per Åhgrencc73ed32020-04-26 23:56:17 +020031#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_processing/test/protobuf_utils.h"
33#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/arraysize.h"
35#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/gtest_prod_util.h"
38#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010039#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010040#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020043#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020044#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020045#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010046#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070050
51RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000053#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000054#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000056#endif
kwiberg77eab702016-09-28 17:42:01 -070057RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000058
Sam Zackrisson6558fa52019-08-26 10:12:41 +020059ABSL_FLAG(bool,
60 write_apm_ref_data,
61 false,
62 "Write ApmTest.Process results to file, instead of comparing results "
63 "to the existing reference data file.");
64
andrew@webrtc.org27c69802014-02-18 20:24:56 +000065namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000066namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000067
ekmeyerson60d9b332015-08-14 10:35:55 -070068// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
69// applicable.
70
mbonadei7c2c8432017-04-07 00:59:12 -070071const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070072const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000073
aluebseb3603b2016-04-20 15:27:58 -070074#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
75// Android doesn't support 48kHz.
76const int kProcessSampleRates[] = {8000, 16000, 32000};
77#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070078const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070079#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000080
ekmeyerson60d9b332015-08-14 10:35:55 -070081enum StreamDirection { kForward = 0, kReverse };
82
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000083void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
Jonas Olssona4d87372019-07-05 19:08:33 +020084 ChannelBuffer<int16_t> cb_int(cb->num_frames(), cb->num_channels());
85 Deinterleave(int_data, cb->num_frames(), cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000086 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080087 for (size_t i = 0; i < cb->num_channels(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +020088 S16ToFloat(cb_int.channels()[i], cb->num_frames(), cb->channels()[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000089 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000090}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000091
Per Åhgren2507f8c2020-03-19 12:33:29 +010092void ConvertToFloat(const Int16FrameData& frame, ChannelBuffer<float>* cb) {
93 ConvertToFloat(frame.data.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094}
95
andrew@webrtc.org103657b2014-04-24 18:28:56 +000096// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080097size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098 switch (layout) {
99 case AudioProcessing::kMono:
100 return 1;
101 case AudioProcessing::kMonoAndKeyboard:
102 case AudioProcessing::kStereo:
103 return 2;
104 case AudioProcessing::kStereoAndKeyboard:
105 return 3;
106 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700107 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800108 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000109}
110
Jonas Olssona4d87372019-07-05 19:08:33 +0200111void MixStereoToMono(const float* stereo,
112 float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800113 size_t samples_per_channel) {
114 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000116}
117
Jonas Olssona4d87372019-07-05 19:08:33 +0200118void MixStereoToMono(const int16_t* stereo,
119 int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800120 size_t samples_per_channel) {
121 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
123}
124
pkasting25702cb2016-01-08 13:50:27 -0800125void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
126 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000127 stereo[i * 2 + 1] = stereo[i * 2];
128 }
129}
130
yujo36b1a5f2017-06-12 12:45:32 -0700131void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800132 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000133 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
134 }
135}
136
Per Åhgren2507f8c2020-03-19 12:33:29 +0100137void SetFrameTo(Int16FrameData* frame, int16_t value) {
138 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700139 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100140 frame->data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000141 }
142}
143
Per Åhgren2507f8c2020-03-19 12:33:29 +0100144void SetFrameTo(Int16FrameData* frame, int16_t left, int16_t right) {
145 ASSERT_EQ(2u, frame->num_channels);
146 for (size_t i = 0; i < frame->samples_per_channel * 2; i += 2) {
147 frame->data[i] = left;
148 frame->data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000149 }
150}
151
Per Åhgren2507f8c2020-03-19 12:33:29 +0100152void ScaleFrame(Int16FrameData* frame, float scale) {
153 for (size_t i = 0; i < frame->samples_per_channel * frame->num_channels;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700154 ++i) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100155 frame->data[i] = FloatS16ToS16(frame->data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000156 }
157}
158
Per Åhgren2507f8c2020-03-19 12:33:29 +0100159bool FrameDataAreEqual(const Int16FrameData& frame1,
160 const Int16FrameData& frame2) {
161 if (frame1.samples_per_channel != frame2.samples_per_channel) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000162 return false;
163 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100164 if (frame1.num_channels != frame2.num_channels) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165 return false;
166 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100167 if (memcmp(
168 frame1.data.data(), frame2.data.data(),
169 frame1.samples_per_channel * frame1.num_channels * sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
172 return true;
173}
174
Per Åhgren2507f8c2020-03-19 12:33:29 +0100175rtc::ArrayView<int16_t> GetMutableFrameData(Int16FrameData* frame) {
176 int16_t* ptr = frame->data.data();
177 const size_t len = frame->samples_per_channel * frame->num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200178 return rtc::ArrayView<int16_t>(ptr, len);
179}
180
Per Åhgren2507f8c2020-03-19 12:33:29 +0100181rtc::ArrayView<const int16_t> GetFrameData(const Int16FrameData& frame) {
182 const int16_t* ptr = frame.data.data();
183 const size_t len = frame.samples_per_channel * frame.num_channels;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200184 return rtc::ArrayView<const int16_t>(ptr, len);
185}
186
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000187void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200188 AudioProcessing::Config apm_config = ap->GetConfig();
189 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000190#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200191 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100192
193 apm_config.gain_controller1.enabled = true;
194 apm_config.gain_controller1.mode =
195 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000196#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200197 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000198
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100199 apm_config.gain_controller1.enabled = true;
200 apm_config.gain_controller1.mode =
201 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
202 apm_config.gain_controller1.analog_level_minimum = 0;
203 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000204#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000205
saza0bad15f2019-10-16 11:46:11 +0200206 apm_config.noise_suppression.enabled = true;
207
peah8271d042016-11-22 07:24:52 -0800208 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100209 apm_config.level_estimation.enabled = true;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200210 apm_config.voice_detection.enabled = true;
Per Åhgrenc0424252019-12-10 13:04:15 +0100211 apm_config.pipeline.maximum_internal_processing_rate = 48000;
peah8271d042016-11-22 07:24:52 -0800212 ap->ApplyConfig(apm_config);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000213}
214
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000215// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000216template <class T>
217T AbsValue(T a) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200218 return a > 0 ? a : -a;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000219}
220
Per Åhgren2507f8c2020-03-19 12:33:29 +0100221int16_t MaxAudioFrame(const Int16FrameData& frame) {
222 const size_t length = frame.samples_per_channel * frame.num_channels;
223 int16_t max_data = AbsValue(frame.data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800224 for (size_t i = 1; i < length; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100225 max_data = std::max(max_data, AbsValue(frame.data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000226 }
227
228 return max_data;
229}
230
Alex Loiko890988c2017-08-31 10:25:48 +0200231void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700232 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000233 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234 ASSERT_TRUE(file != NULL);
235
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100236 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800238 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000239 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242 ASSERT_EQ(static_cast<size_t>(size),
Jonas Olssona4d87372019-07-05 19:08:33 +0200243 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244 fclose(file);
245}
246
Alex Loiko890988c2017-08-31 10:25:48 +0200247std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200248 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000249 // Resource files are all stereo.
250 ss << name << sample_rate_hz / 1000 << "_stereo";
251 return test::ResourcePath(ss.str(), "pcm");
252}
253
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000254// Temporary filenames unique to this process. Used to be able to run these
255// tests in parallel as each process needs to be running in isolation they can't
256// have competing filenames.
257std::map<std::string, std::string> temp_filenames;
258
Alex Loiko890988c2017-08-31 10:25:48 +0200259std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000260 int input_rate,
261 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700262 int reverse_input_rate,
263 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800264 size_t num_input_channels,
265 size_t num_output_channels,
266 size_t num_reverse_input_channels,
267 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700268 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200269 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700270 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
271 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000272 if (num_output_channels == 1) {
273 ss << "mono";
274 } else if (num_output_channels == 2) {
275 ss << "stereo";
276 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700277 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000278 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 ss << output_rate / 1000;
280 if (num_reverse_output_channels == 1) {
281 ss << "_rmono";
282 } else if (num_reverse_output_channels == 2) {
283 ss << "_rstereo";
284 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700285 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700286 }
287 ss << reverse_output_rate / 1000;
288 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000290 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700291 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000292 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
293 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000294}
295
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000296void ClearTempFiles() {
297 for (auto& kv : temp_filenames)
298 remove(kv.second.c_str());
299}
300
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200301// Only remove "out" files. Keep "ref" files.
302void ClearTempOutFiles() {
303 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
304 const std::string& filename = it->first;
305 if (filename.substr(0, 3).compare("out") == 0) {
306 remove(it->second.c_str());
307 temp_filenames.erase(it++);
308 } else {
309 it++;
310 }
311 }
312}
313
Alex Loiko890988c2017-08-31 10:25:48 +0200314void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000315 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000316 ASSERT_TRUE(file != NULL);
317 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000318 fclose(file);
319}
320
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000321// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
322// stereo) file, converts to deinterleaved float (optionally downmixing) and
323// returns the result in |cb|. Returns false if the file ended (or on error) and
324// true otherwise.
325//
326// |int_data| and |float_data| are just temporary space that must be
327// sufficiently large to hold the 10 ms chunk.
Jonas Olssona4d87372019-07-05 19:08:33 +0200328bool ReadChunk(FILE* file,
329 int16_t* int_data,
330 float* float_data,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000331 ChannelBuffer<float>* cb) {
332 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000333 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000334 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
335 if (read_count != frame_size) {
336 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700337 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000338 return false; // This is expected.
339 }
340
341 S16ToFloat(int_data, frame_size, float_data);
342 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000343 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000344 } else {
Jonas Olssona4d87372019-07-05 19:08:33 +0200345 Deinterleave(float_data, cb->num_frames(), 2, cb->channels());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000346 }
347
348 return true;
349}
350
niklase@google.com470e71d2011-07-07 08:21:25 +0000351class ApmTest : public ::testing::Test {
352 protected:
353 ApmTest();
354 virtual void SetUp();
355 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000356
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200357 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000358
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200359 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000360
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000361 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200362 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363
364 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000365 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000366 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800367 size_t num_input_channels,
368 size_t num_output_channels,
369 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000370 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000371 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000372 void EnableAllComponents();
Per Åhgren2507f8c2020-03-19 12:33:29 +0100373 bool ReadFrame(FILE* file, Int16FrameData* frame);
374 bool ReadFrame(FILE* file, Int16FrameData* frame, ChannelBuffer<float>* cb);
375 void ReadFrameWithRewind(FILE* file, Int16FrameData* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200376 void ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100377 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000378 ChannelBuffer<float>* cb);
Jonas Olssona4d87372019-07-05 19:08:33 +0200379 void ProcessDelayVerificationTest(int delay_ms,
380 int system_delay_ms,
381 int delay_min,
382 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700383 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800384 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800386 void TestChangingForwardChannels(size_t num_in_channels,
387 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700388 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800389 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000391 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
392 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000394 int ProcessStreamChooser(Format format);
395 int AnalyzeReverseStreamChooser(Format format);
396 void ProcessDebugDump(const std::string& in_filename,
397 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800398 Format format,
399 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000400 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000401
402 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800404 std::unique_ptr<AudioProcessing> apm_;
Per Åhgren2507f8c2020-03-19 12:33:29 +0100405 Int16FrameData frame_;
406 Int16FrameData revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800407 std::unique_ptr<ChannelBuffer<float> > float_cb_;
408 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800410 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 FILE* far_file_;
412 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000413 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414};
415
416ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000417 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000418#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200419 ref_filename_(
420 test::ResourcePath("audio_processing/output_data_fixed", "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000421#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200422 ref_filename_(
423 test::ResourcePath("audio_processing/output_data_float", "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000424#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000425 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000426 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000427 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000428 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000429 out_file_(NULL) {
Per Åhgrencc73ed32020-04-26 23:56:17 +0200430 apm_.reset(AudioProcessingBuilderForTesting().Create());
Per Åhgrenc0424252019-12-10 13:04:15 +0100431 AudioProcessing::Config apm_config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100432 apm_config.gain_controller1.analog_gain_controller.enabled = false;
Per Åhgrenc0424252019-12-10 13:04:15 +0100433 apm_config.pipeline.maximum_internal_processing_rate = 48000;
434 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000435}
niklase@google.com470e71d2011-07-07 08:21:25 +0000436
437void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000438 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
443void ApmTest::TearDown() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 if (far_file_) {
445 ASSERT_EQ(0, fclose(far_file_));
446 }
447 far_file_ = NULL;
448
449 if (near_file_) {
450 ASSERT_EQ(0, fclose(near_file_));
451 }
452 near_file_ = NULL;
453
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000454 if (out_file_) {
455 ASSERT_EQ(0, fclose(out_file_));
456 }
457 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000460void ApmTest::Init(AudioProcessing* ap) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200461 ASSERT_EQ(
462 kNoErr,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100463 ap->Initialize({{{frame_.sample_rate_hz, frame_.num_channels},
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200464 {output_sample_rate_hz_, num_output_channels_},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100465 {revframe_.sample_rate_hz, revframe_.num_channels},
466 {revframe_.sample_rate_hz, revframe_.num_channels}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000467}
468
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000469void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000471 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800472 size_t num_input_channels,
473 size_t num_output_channels,
474 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000475 bool open_output_file) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200476 SetContainerFormat(sample_rate_hz, num_input_channels, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000478 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000479
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200480 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, &revframe_,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000482 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000483
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000484 if (far_file_) {
485 ASSERT_EQ(0, fclose(far_file_));
486 }
487 std::string filename = ResourceFilePath("far", sample_rate_hz);
488 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200489 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490
491 if (near_file_) {
492 ASSERT_EQ(0, fclose(near_file_));
493 }
494 filename = ResourceFilePath("near", sample_rate_hz);
495 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200496 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000497
498 if (open_output_file) {
499 if (out_file_) {
500 ASSERT_EQ(0, fclose(out_file_));
501 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700502 filename = OutputFilePath(
503 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
504 reverse_sample_rate_hz, num_input_channels, num_output_channels,
505 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000506 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200507 ASSERT_TRUE(out_file_ != NULL)
508 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000509 }
510}
511
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000512void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000513 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000514}
515
Jonas Olssona4d87372019-07-05 19:08:33 +0200516bool ApmTest::ReadFrame(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100517 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000518 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000519 // The files always contain stereo audio.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100520 size_t frame_size = frame->samples_per_channel * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200521 size_t read_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100522 fread(frame->data.data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000523 if (read_count != frame_size) {
524 // Check that the file really ended.
525 EXPECT_NE(0, feof(file));
526 return false; // This is expected.
527 }
528
Per Åhgren2507f8c2020-03-19 12:33:29 +0100529 if (frame->num_channels == 1) {
530 MixStereoToMono(frame->data.data(), frame->data.data(),
531 frame->samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000532 }
533
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000534 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000535 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000536 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000537 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000538}
539
Per Åhgren2507f8c2020-03-19 12:33:29 +0100540bool ApmTest::ReadFrame(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000541 return ReadFrame(file, frame, NULL);
542}
543
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000544// If the end of the file has been reached, rewind it and attempt to read the
545// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200546void ApmTest::ReadFrameWithRewind(FILE* file,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100547 Int16FrameData* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548 ChannelBuffer<float>* cb) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200549 if (!ReadFrame(near_file_, &frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000550 rewind(near_file_);
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200551 ASSERT_TRUE(ReadFrame(near_file_, &frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000552 }
553}
554
Per Åhgren2507f8c2020-03-19 12:33:29 +0100555void ApmTest::ReadFrameWithRewind(FILE* file, Int16FrameData* frame) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556 ReadFrameWithRewind(file, frame, NULL);
557}
558
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000559int ApmTest::ProcessStreamChooser(Format format) {
560 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100561 return apm_->ProcessStream(
562 frame_.data.data(),
563 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
564 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100565 frame_.data.data());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000566 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200567 return apm_->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100568 float_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100569 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100570 StreamConfig(output_sample_rate_hz_, num_output_channels_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200571 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572}
573
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000574int ApmTest::AnalyzeReverseStreamChooser(Format format) {
575 if (format == kIntFormat) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100576 return apm_->ProcessReverseStream(
577 revframe_.data.data(),
578 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
579 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
580 revframe_.data.data());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582 return apm_->AnalyzeReverseStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100583 revfloat_cb_->channels(),
Per Åhgren2507f8c2020-03-19 12:33:29 +0100584 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000585}
586
Jonas Olssona4d87372019-07-05 19:08:33 +0200587void ApmTest::ProcessDelayVerificationTest(int delay_ms,
588 int system_delay_ms,
589 int delay_min,
590 int delay_max) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000591 // The |revframe_| and |frame_| should include the proper frame information,
592 // hence can be used for extracting information.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100593 Int16FrameData tmp_frame;
594 std::queue<Int16FrameData*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000595 bool causal = true;
596
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200597 tmp_frame.CopyFrom(revframe_);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000598 SetFrameTo(&tmp_frame, 0);
599
600 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
601 // Initialize the |frame_queue| with empty frames.
602 int frame_delay = delay_ms / 10;
603 while (frame_delay < 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100604 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000605 frame->CopyFrom(tmp_frame);
606 frame_queue.push(frame);
607 frame_delay++;
608 causal = false;
609 }
610 while (frame_delay > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100611 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000612 frame->CopyFrom(tmp_frame);
613 frame_queue.push(frame);
614 frame_delay--;
615 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000616 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
617 // need enough frames with audio to have reliable estimates, but as few as
618 // possible to keep processing time down. 4.5 seconds seemed to be a good
619 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 for (int frame_count = 0; frame_count < 450; ++frame_count) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100621 Int16FrameData* frame = new Int16FrameData();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000622 frame->CopyFrom(tmp_frame);
623 // Use the near end recording, since that has more speech in it.
624 ASSERT_TRUE(ReadFrame(near_file_, frame));
625 frame_queue.push(frame);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100626 Int16FrameData* reverse_frame = frame;
627 Int16FrameData* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000628 if (!causal) {
629 reverse_frame = frame_queue.front();
630 // When we call ProcessStream() the frame is modified, so we can't use the
631 // pointer directly when things are non-causal. Use an intermediate frame
632 // and copy the data.
633 process_frame = &tmp_frame;
634 process_frame->CopyFrom(*frame);
635 }
Per Åhgren2507f8c2020-03-19 12:33:29 +0100636 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(
637 reverse_frame->data.data(),
638 StreamConfig(reverse_frame->sample_rate_hz,
639 reverse_frame->num_channels),
640 StreamConfig(reverse_frame->sample_rate_hz,
641 reverse_frame->num_channels),
642 reverse_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000643 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100644 EXPECT_EQ(apm_->kNoError,
645 apm_->ProcessStream(process_frame->data.data(),
646 StreamConfig(process_frame->sample_rate_hz,
647 process_frame->num_channels),
648 StreamConfig(process_frame->sample_rate_hz,
649 process_frame->num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100650 process_frame->data.data()));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000651 frame = frame_queue.front();
652 frame_queue.pop();
653 delete frame;
654
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000655 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 // Discard the first delay metrics to avoid convergence effects.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100657 static_cast<void>(apm_->GetStatistics());
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000658 }
659 }
660
661 rewind(near_file_);
662 while (!frame_queue.empty()) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100663 Int16FrameData* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 frame_queue.pop();
665 delete frame;
666 }
667 // Calculate expected delay estimate and acceptable regions. Further,
668 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700669 const size_t samples_per_ms =
Per Åhgren2507f8c2020-03-19 12:33:29 +0100670 rtc::SafeMin<size_t>(16u, frame_.samples_per_channel / 10);
kwiberg07038562017-06-12 11:40:47 -0700671 const int expected_median =
672 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
673 const int expected_median_high = rtc::SafeClamp<int>(
674 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700675 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700676 const int expected_median_low = rtc::SafeClamp<int>(
677 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000679 // Verify delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100680 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200681 ASSERT_TRUE(stats.delay_median_ms.has_value());
682 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 EXPECT_GE(expected_median_high, median);
684 EXPECT_LE(expected_median_low, median);
685}
686
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000687void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000688 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000689 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000691 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200692 AudioProcessing::Config apm_config = apm_->GetConfig();
693 apm_config.gain_controller1.enabled = true;
694 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200695 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000696
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000697 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200698 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200700 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000702 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200703 apm_config.echo_canceller.enabled = true;
704 apm_config.echo_canceller.mobile_mode = false;
705 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000706 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200707 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200708 apm_config.gain_controller1.enabled = false;
709 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000710
711 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100713 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000714
715 // Resets after successful ProcessStream().
716 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000717 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100718 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719
720 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200721 apm_config.gain_controller1.enabled = true;
722 apm_->ApplyConfig(apm_config);
723 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100724 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200725 apm_config.gain_controller1.enabled = false;
726 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200729 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100730 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000731
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000732 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200734 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736}
737
738TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000740}
741
742TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000743 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
Michael Graczyk86c6d332015-07-23 11:41:39 -0700746void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800747 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700748 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100749 frame_.num_channels = num_channels;
750
751 EXPECT_EQ(expected_return,
752 apm_->ProcessStream(
753 frame_.data.data(),
754 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
755 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +0100756 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +0100757 EXPECT_EQ(expected_return,
758 apm_->ProcessReverseStream(
759 frame_.data.data(),
760 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
761 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
762 frame_.data.data()));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000763}
764
Michael Graczyk86c6d332015-07-23 11:41:39 -0700765void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800766 size_t num_in_channels,
767 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700768 AudioProcessing::Error expected_return) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100769 const StreamConfig input_stream = {frame_.sample_rate_hz, num_in_channels};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700770 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
771
772 EXPECT_EQ(expected_return,
773 apm_->ProcessStream(float_cb_->channels(), input_stream,
774 output_stream, float_cb_->channels()));
775}
776
777void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800778 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700779 AudioProcessing::Error expected_return) {
780 const ProcessingConfig processing_config = {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100781 {{frame_.sample_rate_hz, apm_->num_input_channels()},
ekmeyerson60d9b332015-08-14 10:35:55 -0700782 {output_sample_rate_hz_, apm_->num_output_channels()},
Per Åhgren2507f8c2020-03-19 12:33:29 +0100783 {frame_.sample_rate_hz, num_rev_channels},
784 {frame_.sample_rate_hz, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700785
ekmeyerson60d9b332015-08-14 10:35:55 -0700786 EXPECT_EQ(
787 expected_return,
788 apm_->ProcessReverseStream(
789 float_cb_->channels(), processing_config.reverse_input_stream(),
790 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700791}
792
793TEST_F(ApmTest, ChannelsInt16Interface) {
794 // Testing number of invalid and valid channels.
795 Init(16000, 16000, 16000, 4, 4, 4, false);
796
797 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
798
Peter Kasting69558702016-01-12 16:26:35 -0800799 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700800 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
803}
804
Michael Graczyk86c6d332015-07-23 11:41:39 -0700805TEST_F(ApmTest, Channels) {
806 // Testing number of invalid and valid channels.
807 Init(16000, 16000, 16000, 4, 4, 4, false);
808
809 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
810 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
811
Peter Kasting69558702016-01-12 16:26:35 -0800812 for (size_t i = 1; i < 4; ++i) {
813 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814 // Output channels much be one or match input channels.
815 if (j == 1 || i == j) {
816 TestChangingForwardChannels(i, j, kNoErr);
817 TestChangingReverseChannels(i, kNoErr);
818
819 EXPECT_EQ(i, apm_->num_input_channels());
820 EXPECT_EQ(j, apm_->num_output_channels());
821 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800822 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823 } else {
824 TestChangingForwardChannels(i, j,
825 AudioProcessing::kBadNumberChannelsError);
826 }
827 }
828 }
829}
830
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000831TEST_F(ApmTest, SampleRatesInt) {
Sam Zackrisson12e319a2020-01-03 14:54:20 +0100832 // Testing some valid sample rates.
833 for (int sample_rate : {8000, 12000, 16000, 32000, 44100, 48000, 96000}) {
834 SetContainerFormat(sample_rate, 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000835 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000836 }
837}
838
Sam Zackrissone277bde2019-10-25 10:07:54 +0200839// This test repeatedly reconfigures the pre-amplifier in APM, processes a
840// number of frames, and checks that output signal has the right level.
841TEST_F(ApmTest, PreAmplifier) {
842 // Fill the audio frame with a sawtooth pattern.
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200843 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100844 const size_t samples_per_channel = frame_.samples_per_channel;
Sam Zackrissone277bde2019-10-25 10:07:54 +0200845 for (size_t i = 0; i < samples_per_channel; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +0100846 for (size_t ch = 0; ch < frame_.num_channels; ++ch) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200847 frame_data[i + ch * samples_per_channel] = 10000 * ((i % 3) - 1);
848 }
849 }
850 // Cache the frame in tmp_frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +0100851 Int16FrameData tmp_frame;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200852 tmp_frame.CopyFrom(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200853
Per Åhgren2507f8c2020-03-19 12:33:29 +0100854 auto compute_power = [](const Int16FrameData& frame) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200855 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
856 return std::accumulate(data.begin(), data.end(), 0.0f,
857 [](float a, float b) { return a + b * b; }) /
858 data.size() / 32768 / 32768;
859 };
860
861 const float input_power = compute_power(tmp_frame);
862 // Double-check that the input data is large compared to the error kEpsilon.
863 constexpr float kEpsilon = 1e-4f;
864 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
865
866 // 1. Enable pre-amp with 0 dB gain.
867 AudioProcessing::Config config = apm_->GetConfig();
868 config.pre_amplifier.enabled = true;
869 config.pre_amplifier.fixed_gain_factor = 1.0f;
870 apm_->ApplyConfig(config);
871
872 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200873 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200874 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
875 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200876 float output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200877 EXPECT_NEAR(output_power, input_power, kEpsilon);
878 config = apm_->GetConfig();
879 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.0f);
880
881 // 2. Change pre-amp gain via ApplyConfig.
882 config.pre_amplifier.fixed_gain_factor = 2.0f;
883 apm_->ApplyConfig(config);
884
885 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200886 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200887 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
888 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200889 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200890 EXPECT_NEAR(output_power, 4 * input_power, kEpsilon);
891 config = apm_->GetConfig();
892 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 2.0f);
893
894 // 3. Change pre-amp gain via a RuntimeSetting.
895 apm_->SetRuntimeSetting(
896 AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.5f));
897
898 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200899 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200900 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
901 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200902 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200903 EXPECT_NEAR(output_power, 2.25 * input_power, kEpsilon);
904 config = apm_->GetConfig();
905 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.5f);
906}
907
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000908TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200909 AudioProcessing::Config config = apm_->GetConfig();
910 config.gain_controller1.enabled = false;
911 apm_->ApplyConfig(config);
912 config.gain_controller1.enabled = true;
913 apm_->ApplyConfig(config);
914
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +0200916 for (auto mode :
917 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
918 AudioProcessing::Config::GainController1::kFixedDigital,
919 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
920 config.gain_controller1.mode = mode;
921 apm_->ApplyConfig(config);
922 apm_->set_stream_analog_level(100);
923 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000925
Sam Zackrisson41478c72019-10-15 10:10:26 +0200926 // Testing target levels
927 for (int target_level_dbfs : {0, 15, 31}) {
928 config.gain_controller1.target_level_dbfs = target_level_dbfs;
929 apm_->ApplyConfig(config);
930 apm_->set_stream_analog_level(100);
931 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000932 }
933
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100934 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +0200935 for (int compression_gain_db : {0, 10, 90}) {
936 config.gain_controller1.compression_gain_db = compression_gain_db;
937 apm_->ApplyConfig(config);
938 apm_->set_stream_analog_level(100);
939 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000940 }
941
942 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +0200943 for (bool enable : {false, true}) {
944 config.gain_controller1.enable_limiter = enable;
945 apm_->ApplyConfig(config);
946 apm_->set_stream_analog_level(100);
947 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
948 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000949
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100950 // Testing level limits
Sam Zackrisson41478c72019-10-15 10:10:26 +0200951 std::array<int, 4> kMinLevels = {0, 0, 255, 65000};
952 std::array<int, 4> kMaxLevels = {255, 1024, 65535, 65535};
953 for (size_t i = 0; i < kMinLevels.size(); ++i) {
954 int min_level = kMinLevels[i];
955 int max_level = kMaxLevels[i];
956 config.gain_controller1.analog_level_minimum = min_level;
957 config.gain_controller1.analog_level_maximum = max_level;
958 apm_->ApplyConfig(config);
959 apm_->set_stream_analog_level((min_level + max_level) / 2);
960 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000961 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000962}
963
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100964#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Tommia5e07cc2020-05-26 21:40:37 +0200965using ApmDeathTest = ApmTest;
966
967TEST_F(ApmDeathTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200968 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100969 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200970 config.gain_controller1.target_level_dbfs = -1;
971 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100972}
973
Tommia5e07cc2020-05-26 21:40:37 +0200974TEST_F(ApmDeathTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200975 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100976 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200977 config.gain_controller1.target_level_dbfs = 32;
978 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100979}
980
Tommia5e07cc2020-05-26 21:40:37 +0200981TEST_F(ApmDeathTest, GainControlDiesOnTooLowCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200982 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100983 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200984 config.gain_controller1.compression_gain_db = -1;
985 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100986}
987
Tommia5e07cc2020-05-26 21:40:37 +0200988TEST_F(ApmDeathTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200989 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100990 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200991 config.gain_controller1.compression_gain_db = 91;
992 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100993}
994
Tommia5e07cc2020-05-26 21:40:37 +0200995TEST_F(ApmDeathTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200996 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100997 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200998 config.gain_controller1.analog_level_minimum = -1;
999 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001000}
1001
Tommia5e07cc2020-05-26 21:40:37 +02001002TEST_F(ApmDeathTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001003 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001004 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001005 config.gain_controller1.analog_level_maximum = 65536;
1006 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001007}
1008
Tommia5e07cc2020-05-26 21:40:37 +02001009TEST_F(ApmDeathTest, GainControlDiesOnInvertedAnalogLevelLimits) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001010 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001011 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001012 config.gain_controller1.analog_level_minimum = 512;
1013 config.gain_controller1.analog_level_maximum = 255;
1014 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001015}
1016
Tommia5e07cc2020-05-26 21:40:37 +02001017TEST_F(ApmDeathTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001018 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001019 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001020 config.gain_controller1.analog_level_minimum = 255;
1021 config.gain_controller1.analog_level_maximum = 512;
1022 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001023 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
1024}
1025
Tommia5e07cc2020-05-26 21:40:37 +02001026TEST_F(ApmDeathTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001027 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001028 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001029 config.gain_controller1.analog_level_minimum = 255;
1030 config.gain_controller1.analog_level_maximum = 512;
1031 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001032 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
1033}
1034#endif
1035
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001036void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001037 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001038 auto config = apm_->GetConfig();
1039 config.gain_controller1.enabled = true;
1040 config.gain_controller1.mode =
1041 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1042 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001043
1044 int out_analog_level = 0;
1045 for (int i = 0; i < 2000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001046 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001047 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001048 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001049
1050 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +02001051 apm_->set_stream_analog_level(100);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001052 EXPECT_EQ(apm_->kNoError,
1053 apm_->ProcessStream(
1054 frame_.data.data(),
1055 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1056 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001057 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001058 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001059 }
1060
1061 // Ensure the AGC is still able to reach the maximum.
1062 EXPECT_EQ(255, out_analog_level);
1063}
1064
1065// Verifies that despite volume slider quantization, the AGC can continue to
1066// increase its volume.
1067TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001068 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001069 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1070 }
1071}
1072
1073void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001074 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001075 auto config = apm_->GetConfig();
1076 config.gain_controller1.enabled = true;
1077 config.gain_controller1.mode =
1078 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1079 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001080
1081 int out_analog_level = 100;
1082 for (int i = 0; i < 1000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001083 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001084 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001085 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001086
Sam Zackrisson41478c72019-10-15 10:10:26 +02001087 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001088 EXPECT_EQ(apm_->kNoError,
1089 apm_->ProcessStream(
1090 frame_.data.data(),
1091 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1092 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001093 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001094 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001095 }
1096
1097 // Ensure the volume was raised.
1098 EXPECT_GT(out_analog_level, 100);
1099 int highest_level_reached = out_analog_level;
1100 // Simulate a user manual volume change.
1101 out_analog_level = 100;
1102
1103 for (int i = 0; i < 300; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001104 ReadFrameWithRewind(near_file_, &frame_);
1105 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001106
Sam Zackrisson41478c72019-10-15 10:10:26 +02001107 apm_->set_stream_analog_level(out_analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001108 EXPECT_EQ(apm_->kNoError,
1109 apm_->ProcessStream(
1110 frame_.data.data(),
1111 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1112 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001113 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001114 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001115 // Check that AGC respected the manually adjusted volume.
1116 EXPECT_LT(out_analog_level, highest_level_reached);
1117 }
1118 // Check that the volume was still raised.
1119 EXPECT_GT(out_analog_level, 100);
1120}
1121
1122TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001123 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001124 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1125 }
1126}
1127
niklase@google.com470e71d2011-07-07 08:21:25 +00001128TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001129 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001130 AudioProcessing::Config apm_config;
1131 apm_config.high_pass_filter.enabled = true;
1132 apm_->ApplyConfig(apm_config);
1133 apm_config.high_pass_filter.enabled = false;
1134 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001135}
1136
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001137TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001138 AudioProcessing::Config config = apm_->GetConfig();
1139 EXPECT_FALSE(config.echo_canceller.enabled);
1140 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001141 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001142 EXPECT_FALSE(config.level_estimation.enabled);
saza0bad15f2019-10-16 11:46:11 +02001143 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001144 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001145}
1146
1147TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001148 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001149 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001150 SetFrameTo(&frame_, 1000, 2000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001151 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001152 frame_copy.CopyFrom(frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001153 for (int j = 0; j < 1000; j++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001154 EXPECT_EQ(apm_->kNoError,
1155 apm_->ProcessStream(
1156 frame_.data.data(),
1157 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1158 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001159 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001160 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001161 EXPECT_EQ(apm_->kNoError,
1162 apm_->ProcessReverseStream(
1163 frame_.data.data(),
1164 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1165 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1166 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001167 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001168 }
1169 }
1170}
1171
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001172TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1173 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001174 const size_t kSamples = 160;
1175 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001176 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001177 float dest[kSamples] = {};
1178
1179 auto src_channels = &src[0];
1180 auto dest_channels = &dest[0];
1181
Per Åhgrencc73ed32020-04-26 23:56:17 +02001182 apm_.reset(AudioProcessingBuilderForTesting().Create());
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001183 EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
1184 StreamConfig(sample_rate, 1),
1185 &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001186
1187 for (size_t i = 0; i < kSamples; ++i) {
1188 EXPECT_EQ(src[i], dest[i]);
1189 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001190
1191 // Same for ProcessReverseStream.
1192 float rev_dest[kSamples] = {};
1193 auto rev_dest_channels = &rev_dest[0];
1194
1195 StreamConfig input_stream = {sample_rate, 1};
1196 StreamConfig output_stream = {sample_rate, 1};
1197 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1198 output_stream, &rev_dest_channels));
1199
1200 for (size_t i = 0; i < kSamples; ++i) {
1201 EXPECT_EQ(src[i], rev_dest[i]);
1202 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001203}
1204
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001205TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1206 EnableAllComponents();
1207
pkasting25702cb2016-01-08 13:50:27 -08001208 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001209 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1210 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001211 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001212 ASSERT_EQ(0, feof(far_file_));
1213 ASSERT_EQ(0, feof(near_file_));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001214 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001215 CopyLeftToRightChannel(revframe_.data.data(),
1216 revframe_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001217
Per Åhgren2507f8c2020-03-19 12:33:29 +01001218 ASSERT_EQ(
1219 kNoErr,
1220 apm_->ProcessReverseStream(
1221 revframe_.data.data(),
1222 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1223 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1224 revframe_.data.data()));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001225
Per Åhgren2507f8c2020-03-19 12:33:29 +01001226 CopyLeftToRightChannel(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001227
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001228 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001229 apm_->set_stream_analog_level(analog_level);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001230 ASSERT_EQ(kNoErr,
1231 apm_->ProcessStream(
1232 frame_.data.data(),
1233 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1234 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001235 frame_.data.data()));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001236 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001237
Per Åhgren2507f8c2020-03-19 12:33:29 +01001238 VerifyChannelsAreEqual(frame_.data.data(), frame_.samples_per_channel);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001239 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001240 rewind(far_file_);
1241 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001242 }
1243}
1244
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001245TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001246 // Verify the filter is not active through undistorted audio when:
1247 // 1. No components are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001248 SetFrameTo(&frame_, 1000);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001249 Int16FrameData frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001250 frame_copy.CopyFrom(frame_);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001251 EXPECT_EQ(apm_->kNoError,
1252 apm_->ProcessStream(
1253 frame_.data.data(),
1254 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1255 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001256 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001257 EXPECT_EQ(apm_->kNoError,
1258 apm_->ProcessStream(
1259 frame_.data.data(),
1260 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1261 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001262 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001263 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001264
1265 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001266 auto apm_config = apm_->GetConfig();
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001267 SetFrameTo(&frame_, 1000);
1268 frame_copy.CopyFrom(frame_);
saza6787f232019-10-11 19:31:07 +02001269 apm_config.level_estimation.enabled = true;
1270 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001271 EXPECT_EQ(apm_->kNoError,
1272 apm_->ProcessStream(
1273 frame_.data.data(),
1274 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1275 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001276 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001277 EXPECT_EQ(apm_->kNoError,
1278 apm_->ProcessStream(
1279 frame_.data.data(),
1280 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1281 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001282 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001283 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001284 apm_config.level_estimation.enabled = false;
1285 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001286
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001287 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001288 SetFrameTo(&frame_, 1000);
1289 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001290 apm_config.voice_detection.enabled = true;
1291 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001292 EXPECT_EQ(apm_->kNoError,
1293 apm_->ProcessStream(
1294 frame_.data.data(),
1295 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1296 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001297 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001298 EXPECT_EQ(apm_->kNoError,
1299 apm_->ProcessStream(
1300 frame_.data.data(),
1301 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1302 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001303 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001304 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001305 apm_config.voice_detection.enabled = false;
1306 apm_->ApplyConfig(apm_config);
1307
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001308 // 4. Both the VAD and the level estimator are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001309 SetFrameTo(&frame_, 1000);
1310 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001311 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001312 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001313 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001314 EXPECT_EQ(apm_->kNoError,
1315 apm_->ProcessStream(
1316 frame_.data.data(),
1317 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1318 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001319 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001320 EXPECT_EQ(apm_->kNoError,
1321 apm_->ProcessStream(
1322 frame_.data.data(),
1323 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1324 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001325 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001326 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001327 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001328 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001329 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001330
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001331 // Check the test is valid. We should have distortion from the filter
1332 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001333 apm_config.echo_canceller.enabled = true;
1334 apm_config.echo_canceller.mobile_mode = false;
1335 apm_->ApplyConfig(apm_config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01001336 frame_.samples_per_channel = 320;
1337 frame_.num_channels = 2;
1338 frame_.sample_rate_hz = 32000;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001339 SetFrameTo(&frame_, 1000);
1340 frame_copy.CopyFrom(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001341 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001342 EXPECT_EQ(apm_->kNoError,
1343 apm_->ProcessStream(
1344 frame_.data.data(),
1345 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1346 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001347 frame_.data.data()));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001348 EXPECT_FALSE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001349}
1350
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001351#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1352void ApmTest::ProcessDebugDump(const std::string& in_filename,
1353 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001354 Format format,
1355 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001356 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001357 FILE* in_file = fopen(in_filename.c_str(), "rb");
1358 ASSERT_TRUE(in_file != NULL);
1359 audioproc::Event event_msg;
1360 bool first_init = true;
1361
1362 while (ReadMessageFromFile(in_file, &event_msg)) {
1363 if (event_msg.type() == audioproc::Event::INIT) {
1364 const audioproc::Init msg = event_msg.init();
1365 int reverse_sample_rate = msg.sample_rate();
1366 if (msg.has_reverse_sample_rate()) {
1367 reverse_sample_rate = msg.reverse_sample_rate();
1368 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001369 int output_sample_rate = msg.sample_rate();
1370 if (msg.has_output_sample_rate()) {
1371 output_sample_rate = msg.output_sample_rate();
1372 }
1373
Jonas Olssona4d87372019-07-05 19:08:33 +02001374 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1375 msg.num_input_channels(), msg.num_output_channels(),
1376 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001377 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001378 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001379 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001380 auto aec_dump =
1381 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1382 EXPECT_TRUE(aec_dump);
1383 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001384 first_init = false;
1385 }
1386
1387 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1388 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1389
1390 if (msg.channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001391 ASSERT_EQ(revframe_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001392 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001393 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001394 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1395 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001396 }
1397 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001398 memcpy(revframe_.data.data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001399 if (format == kFloatFormat) {
1400 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001401 ConvertToFloat(revframe_, revfloat_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001402 }
1403 }
1404 AnalyzeReverseStreamChooser(format);
1405
1406 } else if (event_msg.type() == audioproc::Event::STREAM) {
1407 const audioproc::Stream msg = event_msg.stream();
1408 // ProcessStream could have changed this for the output frame.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001409 frame_.num_channels = apm_->num_input_channels();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410
Sam Zackrisson41478c72019-10-15 10:10:26 +02001411 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001412 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001413 if (msg.has_keypress()) {
1414 apm_->set_stream_key_pressed(msg.keypress());
1415 } else {
1416 apm_->set_stream_key_pressed(true);
1417 }
1418
1419 if (msg.input_channel_size() > 0) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001420 ASSERT_EQ(frame_.num_channels,
Peter Kasting69558702016-01-12 16:26:35 -08001421 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001422 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001423 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1424 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001425 }
1426 } else {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001427 memcpy(frame_.data.data(), msg.input_data().data(),
yujo36b1a5f2017-06-12 12:45:32 -07001428 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001429 if (format == kFloatFormat) {
1430 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001431 ConvertToFloat(frame_, float_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001432 }
1433 }
1434 ProcessStreamChooser(format);
1435 }
1436 }
aleloif4dd1912017-06-15 01:55:38 -07001437 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001438 fclose(in_file);
1439}
1440
1441void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001442 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001443 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001444 std::string format_string;
1445 switch (format) {
1446 case kIntFormat:
1447 format_string = "_int";
1448 break;
1449 case kFloatFormat:
1450 format_string = "_float";
1451 break;
1452 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001453 const std::string ref_filename = test::TempFilename(
1454 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1455 const std::string out_filename = test::TempFilename(
1456 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001457 const std::string limited_filename = test::TempFilename(
1458 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1459 const size_t logging_limit_bytes = 100000;
1460 // We expect at least this many bytes in the created logfile.
1461 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001462 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001463 ProcessDebugDump(in_filename, ref_filename, format, -1);
1464 ProcessDebugDump(ref_filename, out_filename, format, -1);
1465 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001466
1467 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1468 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001469 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001470 ASSERT_TRUE(ref_file != NULL);
1471 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001472 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001473 std::unique_ptr<uint8_t[]> ref_bytes;
1474 std::unique_ptr<uint8_t[]> out_bytes;
1475 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001476
1477 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1478 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001479 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001480 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001481 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001482 while (ref_size > 0 && out_size > 0) {
1483 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001484 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001485 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001486 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001487 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001488 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001489 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1490 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001491 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001492 }
1493 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001494 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1495 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001496 EXPECT_NE(0, feof(ref_file));
1497 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001498 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001499 ASSERT_EQ(0, fclose(ref_file));
1500 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001501 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001502 remove(ref_filename.c_str());
1503 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001504 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001505}
1506
pbosc7a65692016-05-06 12:50:04 -07001507TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001508 VerifyDebugDumpTest(kIntFormat);
1509}
1510
pbosc7a65692016-05-06 12:50:04 -07001511TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001512 VerifyDebugDumpTest(kFloatFormat);
1513}
1514#endif
1515
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001516// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001517TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001518 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001519 const std::string filename =
1520 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001521 {
1522 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1523 EXPECT_FALSE(aec_dump);
1524 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001525
1526#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1527 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001528 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001529
aleloif4dd1912017-06-15 01:55:38 -07001530 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1531 EXPECT_TRUE(aec_dump);
1532 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001533 EXPECT_EQ(apm_->kNoError,
1534 apm_->ProcessStream(
1535 frame_.data.data(),
1536 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1537 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001538 frame_.data.data()));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001539 EXPECT_EQ(apm_->kNoError,
1540 apm_->ProcessReverseStream(
1541 revframe_.data.data(),
1542 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1543 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1544 revframe_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001545 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001546
1547 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001548 FILE* fid = fopen(filename.c_str(), "r");
1549 ASSERT_TRUE(fid != NULL);
1550
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001551 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001552 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001553 ASSERT_EQ(0, remove(filename.c_str()));
1554#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001555 // Verify the file has NOT been written.
1556 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1557#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1558}
1559
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001560// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001561TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001562 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001563
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001564 const std::string filename =
1565 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001566 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1567 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001568
1569#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1570 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001571 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001572
Niels Möllere8e4dc42019-06-11 14:04:16 +02001573 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001574 EXPECT_TRUE(aec_dump);
1575 apm_->AttachAecDump(std::move(aec_dump));
Per Åhgren2507f8c2020-03-19 12:33:29 +01001576 EXPECT_EQ(apm_->kNoError,
1577 apm_->ProcessReverseStream(
1578 revframe_.data.data(),
1579 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1580 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1581 revframe_.data.data()));
1582 EXPECT_EQ(apm_->kNoError,
1583 apm_->ProcessStream(
1584 frame_.data.data(),
1585 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1586 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001587 frame_.data.data()));
aleloif4dd1912017-06-15 01:55:38 -07001588 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001589
1590 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001591 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001592 ASSERT_TRUE(fid != NULL);
1593
1594 // Clean it up.
1595 ASSERT_EQ(0, fclose(fid));
1596 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001597#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1598}
1599
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001600// TODO(andrew): Add a test to process a few frames with different combinations
1601// of enabled components.
1602
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001603TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001604 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001605 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001606
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001607 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001608 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001609 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001610 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001611 for (size_t i = 0; i < arraysize(kChannels); i++) {
1612 for (size_t j = 0; j < arraysize(kChannels); j++) {
1613 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001614 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001615 test->set_num_reverse_channels(kChannels[i]);
1616 test->set_num_input_channels(kChannels[j]);
1617 test->set_num_output_channels(kChannels[j]);
1618 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001619 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001620 }
1621 }
1622 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001623#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1624 // To test the extended filter mode.
1625 audioproc::Test* test = ref_data.add_test();
1626 test->set_num_reverse_channels(2);
1627 test->set_num_input_channels(2);
1628 test->set_num_output_channels(2);
1629 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1630 test->set_use_aec_extended_filter(true);
1631#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001632 }
1633
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001634 for (int i = 0; i < ref_data.test_size(); i++) {
1635 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001636
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001637 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001638 // TODO(ajm): We no longer allow different input and output channels. Skip
1639 // these tests for now, but they should be removed from the set.
1640 if (test->num_input_channels() != test->num_output_channels())
1641 continue;
1642
Per Åhgrencc73ed32020-04-26 23:56:17 +02001643 apm_.reset(AudioProcessingBuilderForTesting().Create());
Per Åhgren0695df12020-01-13 14:43:13 +01001644 AudioProcessing::Config apm_config = apm_->GetConfig();
1645 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1646 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001647
1648 EnableAllComponents();
1649
Jonas Olssona4d87372019-07-05 19:08:33 +02001650 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001651 static_cast<size_t>(test->num_input_channels()),
1652 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001653 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001654
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001655 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001656 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001657 int analog_level = 127;
1658 int analog_level_average = 0;
1659 int max_output_average = 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001660 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001661#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001662 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001663#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001664
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001665 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01001666 EXPECT_EQ(
1667 apm_->kNoError,
1668 apm_->ProcessReverseStream(
1669 revframe_.data.data(),
1670 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1671 StreamConfig(revframe_.sample_rate_hz, revframe_.num_channels),
1672 revframe_.data.data()));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001673
1674 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001675 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001676
Per Åhgren2507f8c2020-03-19 12:33:29 +01001677 EXPECT_EQ(apm_->kNoError,
1678 apm_->ProcessStream(
1679 frame_.data.data(),
1680 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
1681 StreamConfig(frame_.sample_rate_hz, frame_.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01001682 frame_.data.data()));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001683
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001684 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001685 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01001686 frame_.num_channels);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001687
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001688 max_output_average += MaxAudioFrame(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001689
Sam Zackrisson41478c72019-10-15 10:10:26 +02001690 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001691 analog_level_average += analog_level;
Per Åhgrencf4c8722019-12-30 14:32:14 +01001692 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001693 EXPECT_TRUE(stats.voice_detected);
1694 EXPECT_TRUE(stats.output_rms_dbfs);
1695 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001696 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001697
Per Åhgren2507f8c2020-03-19 12:33:29 +01001698 size_t frame_size = frame_.samples_per_channel * frame_.num_channels;
Jonas Olssona4d87372019-07-05 19:08:33 +02001699 size_t write_count =
Per Åhgren2507f8c2020-03-19 12:33:29 +01001700 fwrite(frame_.data.data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001701 ASSERT_EQ(frame_size, write_count);
1702
1703 // Reset in case of downmixing.
Per Åhgren2507f8c2020-03-19 12:33:29 +01001704 frame_.num_channels = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001705 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001706
1707#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1708 const int kStatsAggregationFrameNum = 100; // 1 second.
1709 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001710 // Get echo and delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +01001711 AudioProcessingStats stats = apm_->GetStatistics();
minyue58530ed2016-05-24 05:50:12 -07001712
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001713 // Echo metrics.
1714 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1715 const float echo_return_loss_enhancement =
1716 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001717 const float residual_echo_likelihood =
1718 stats.residual_echo_likelihood.value_or(-1.0f);
1719 const float residual_echo_likelihood_recent_max =
1720 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1721
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001722 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001723 const audioproc::Test::EchoMetrics& reference =
1724 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001725 constexpr float kEpsilon = 0.01;
1726 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1727 EXPECT_NEAR(echo_return_loss_enhancement,
1728 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001729 EXPECT_NEAR(residual_echo_likelihood,
1730 reference.residual_echo_likelihood(), kEpsilon);
1731 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1732 reference.residual_echo_likelihood_recent_max(),
1733 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001734 ++stats_index;
1735 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001736 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1737 message_echo->set_echo_return_loss(echo_return_loss);
1738 message_echo->set_echo_return_loss_enhancement(
1739 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001740 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1741 message_echo->set_residual_echo_likelihood_recent_max(
1742 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001743 }
1744 }
1745#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001746 }
1747 max_output_average /= frame_count;
1748 analog_level_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001749 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001750
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001751 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001752 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001753 // When running the test on a N7 we get a {2, 6} difference of
1754 // |has_voice_count| and |max_output_average| is up to 18 higher.
1755 // All numbers being consistently higher on N7 compare to ref_data.
1756 // TODO(bjornv): If we start getting more of these offsets on Android we
1757 // should consider a different approach. Either using one slack for all,
1758 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001759#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001760 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001761 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001762 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001763 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001764#else
1765 const int kHasVoiceCountOffset = 0;
1766 const int kHasVoiceCountNear = kIntNear;
1767 const int kMaxOutputAverageOffset = 0;
1768 const int kMaxOutputAverageNear = kIntNear;
1769#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001770 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001771 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001772
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001773 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001774 EXPECT_NEAR(test->max_output_average(),
1775 max_output_average - kMaxOutputAverageOffset,
1776 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001777#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001778 const double kFloatNear = 0.0005;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001779 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001780#endif
1781 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001782 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001783
1784 test->set_analog_level_average(analog_level_average);
1785 test->set_max_output_average(max_output_average);
1786
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001787#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrisson11b87032018-12-18 17:13:58 +01001788 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001789#endif
1790 }
1791
1792 rewind(far_file_);
1793 rewind(near_file_);
1794 }
1795
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001796 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001797 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001798 }
1799}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001800
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001801TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1802 struct ChannelFormat {
1803 AudioProcessing::ChannelLayout in_layout;
1804 AudioProcessing::ChannelLayout out_layout;
1805 };
1806 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001807 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1808 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1809 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001810 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001811
Per Åhgrencc73ed32020-04-26 23:56:17 +02001812 std::unique_ptr<AudioProcessing> ap(
1813 AudioProcessingBuilderForTesting().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001814 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001815 AudioProcessing::Config config;
1816 config.noise_suppression.enabled = true;
1817 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001818 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001819 const int in_rate = 44100;
1820 const int out_rate = 48000;
1821 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1822 TotalChannelsFromLayout(cf[i].in_layout));
1823 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1824 ChannelsFromLayout(cf[i].out_layout));
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001825 bool has_keyboard = cf[i].in_layout == AudioProcessing::kMonoAndKeyboard ||
1826 cf[i].in_layout == AudioProcessing::kStereoAndKeyboard;
1827 StreamConfig in_sc(in_rate, ChannelsFromLayout(cf[i].in_layout),
1828 has_keyboard);
1829 StreamConfig out_sc(out_rate, ChannelsFromLayout(cf[i].out_layout));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001830
1831 // Run over a few chunks.
1832 for (int j = 0; j < 10; ++j) {
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001833 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_sc, out_sc,
1834 out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001835 }
1836 }
1837}
1838
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001839// Compares the reference and test arrays over a region around the expected
1840// delay. Finds the highest SNR in that region and adds the variance and squared
1841// error results to the supplied accumulators.
1842void UpdateBestSNR(const float* ref,
1843 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001844 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001845 int expected_delay,
1846 double* variance_acc,
1847 double* sq_error_acc) {
1848 double best_snr = std::numeric_limits<double>::min();
1849 double best_variance = 0;
1850 double best_sq_error = 0;
1851 // Search over a region of eight samples around the expected delay.
1852 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1853 ++delay) {
1854 double sq_error = 0;
1855 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001856 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001857 double error = test[i + delay] - ref[i];
1858 sq_error += error * error;
1859 variance += ref[i] * ref[i];
1860 }
1861
1862 if (sq_error == 0) {
1863 *variance_acc += variance;
1864 return;
1865 }
1866 double snr = variance / sq_error;
1867 if (snr > best_snr) {
1868 best_snr = snr;
1869 best_variance = variance;
1870 best_sq_error = sq_error;
1871 }
1872 }
1873
1874 *variance_acc += best_variance;
1875 *sq_error_acc += best_sq_error;
1876}
1877
1878// Used to test a multitude of sample rate and channel combinations. It works
1879// by first producing a set of reference files (in SetUpTestCase) that are
1880// assumed to be correct, as the used parameters are verified by other tests
1881// in this collection. Primarily the reference files are all produced at
1882// "native" rates which do not involve any resampling.
1883
1884// Each test pass produces an output file with a particular format. The output
1885// is matched against the reference file closest to its internal processing
1886// format. If necessary the output is resampled back to its process format.
1887// Due to the resampling distortion, we don't expect identical results, but
1888// enforce SNR thresholds which vary depending on the format. 0 is a special
1889// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001890typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001891class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001892 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001893 public:
1894 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001895 : input_rate_(std::get<0>(GetParam())),
1896 output_rate_(std::get<1>(GetParam())),
1897 reverse_input_rate_(std::get<2>(GetParam())),
1898 reverse_output_rate_(std::get<3>(GetParam())),
1899 expected_snr_(std::get<4>(GetParam())),
1900 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001901
1902 virtual ~AudioProcessingTest() {}
1903
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001904 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001905 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001906 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001907 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001908 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1909 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1910 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001911 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001912 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1913 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1914 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001915 }
1916 }
1917 }
1918 }
1919
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001920 void TearDown() {
1921 // Remove "out" files after each test.
1922 ClearTempOutFiles();
1923 }
1924
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001925 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001926
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001927 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001928 // to a file specified with |output_file_prefix|. Both forward and reverse
1929 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001930 static void ProcessFormat(int input_rate,
1931 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001932 int reverse_input_rate,
1933 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001934 size_t num_input_channels,
1935 size_t num_output_channels,
1936 size_t num_reverse_input_channels,
1937 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001938 const std::string& output_file_prefix) {
Per Åhgrencc73ed32020-04-26 23:56:17 +02001939 std::unique_ptr<AudioProcessing> ap(
1940 AudioProcessingBuilderForTesting().Create());
Per Åhgren0695df12020-01-13 14:43:13 +01001941 AudioProcessing::Config apm_config = ap->GetConfig();
1942 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1943 ap->ApplyConfig(apm_config);
1944
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001945 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001946
ekmeyerson60d9b332015-08-14 10:35:55 -07001947 ProcessingConfig processing_config = {
1948 {{input_rate, num_input_channels},
1949 {output_rate, num_output_channels},
1950 {reverse_input_rate, num_reverse_input_channels},
1951 {reverse_output_rate, num_reverse_output_channels}}};
1952 ap->Initialize(processing_config);
1953
1954 FILE* far_file =
1955 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001956 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001957 FILE* out_file = fopen(
1958 OutputFilePath(
1959 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1960 reverse_output_rate, num_input_channels, num_output_channels,
1961 num_reverse_input_channels, num_reverse_output_channels, kForward)
1962 .c_str(),
1963 "wb");
1964 FILE* rev_out_file = fopen(
1965 OutputFilePath(
1966 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1967 reverse_output_rate, num_input_channels, num_output_channels,
1968 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1969 .c_str(),
1970 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001971 ASSERT_TRUE(far_file != NULL);
1972 ASSERT_TRUE(near_file != NULL);
1973 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001974 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001975
1976 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1977 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001978 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1979 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001980 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1981 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001982 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1983 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001984
1985 // Temporary buffers.
1986 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001987 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1988 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001989 std::unique_ptr<float[]> float_data(new float[max_length]);
1990 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001991
1992 int analog_level = 127;
1993 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1994 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001995 EXPECT_NOERR(ap->ProcessReverseStream(
1996 rev_cb.channels(), processing_config.reverse_input_stream(),
1997 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001998
1999 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02002000 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002001
2002 EXPECT_NOERR(ap->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +01002003 fwd_cb.channels(), StreamConfig(input_rate, num_input_channels),
2004 StreamConfig(output_rate, num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002005
ekmeyerson60d9b332015-08-14 10:35:55 -07002006 // Dump forward output to file.
2007 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002008 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002009 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002010
Jonas Olssona4d87372019-07-05 19:08:33 +02002011 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2012 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002013
ekmeyerson60d9b332015-08-14 10:35:55 -07002014 // Dump reverse output to file.
2015 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2016 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002017 size_t rev_out_length =
2018 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002019
Jonas Olssona4d87372019-07-05 19:08:33 +02002020 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
2021 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07002022
Sam Zackrisson41478c72019-10-15 10:10:26 +02002023 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002024 }
2025 fclose(far_file);
2026 fclose(near_file);
2027 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002028 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002029 }
2030
2031 protected:
2032 int input_rate_;
2033 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002034 int reverse_input_rate_;
2035 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002036 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002037 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002038};
2039
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002040TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002041 struct ChannelFormat {
2042 int num_input;
2043 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002044 int num_reverse_input;
2045 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002046 };
2047 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02002048 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
2049 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002050 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002051
pkasting25702cb2016-01-08 13:50:27 -08002052 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002053 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2054 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2055 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002056
ekmeyerson60d9b332015-08-14 10:35:55 -07002057 // Verify output for both directions.
2058 std::vector<StreamDirection> stream_directions;
2059 stream_directions.push_back(kForward);
2060 stream_directions.push_back(kReverse);
2061 for (StreamDirection file_direction : stream_directions) {
2062 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2063 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2064 const int out_num =
2065 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2066 const double expected_snr =
2067 file_direction ? expected_reverse_snr_ : expected_snr_;
2068
2069 const int min_ref_rate = std::min(in_rate, out_rate);
2070 int ref_rate;
2071
2072 if (min_ref_rate > 32000) {
2073 ref_rate = 48000;
2074 } else if (min_ref_rate > 16000) {
2075 ref_rate = 32000;
2076 } else if (min_ref_rate > 8000) {
2077 ref_rate = 16000;
2078 } else {
2079 ref_rate = 8000;
2080 }
Per Åhgrenc0424252019-12-10 13:04:15 +01002081
ekmeyerson60d9b332015-08-14 10:35:55 -07002082 FILE* out_file = fopen(
2083 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2084 reverse_output_rate_, cf[i].num_input,
2085 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02002086 cf[i].num_reverse_output, file_direction)
2087 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07002088 "rb");
2089 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02002090 FILE* ref_file =
2091 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2092 cf[i].num_output, cf[i].num_output,
2093 cf[i].num_reverse_output,
2094 cf[i].num_reverse_output, file_direction)
2095 .c_str(),
2096 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002097 ASSERT_TRUE(out_file != NULL);
2098 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002099
pkasting25702cb2016-01-08 13:50:27 -08002100 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2101 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002102 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002103 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002104 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002105 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002106 // Data from the resampled output, in case the reference and output rates
2107 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002108 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002109
ekmeyerson60d9b332015-08-14 10:35:55 -07002110 PushResampler<float> resampler;
2111 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002112
ekmeyerson60d9b332015-08-14 10:35:55 -07002113 // Compute the resampling delay of the output relative to the reference,
2114 // to find the region over which we should search for the best SNR.
2115 float expected_delay_sec = 0;
2116 if (in_rate != ref_rate) {
2117 // Input resampling delay.
2118 expected_delay_sec +=
2119 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2120 }
2121 if (out_rate != ref_rate) {
2122 // Output resampling delay.
2123 expected_delay_sec +=
2124 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2125 // Delay of converting the output back to its processing rate for
2126 // testing.
2127 expected_delay_sec +=
2128 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2129 }
2130 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002131 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002132
ekmeyerson60d9b332015-08-14 10:35:55 -07002133 double variance = 0;
2134 double sq_error = 0;
2135 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2136 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2137 float* out_ptr = out_data.get();
2138 if (out_rate != ref_rate) {
2139 // Resample the output back to its internal processing rate if
2140 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002141 ASSERT_EQ(ref_length,
2142 static_cast<size_t>(resampler.Resample(
2143 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002144 out_ptr = cmp_data.get();
2145 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002146
ekmeyerson60d9b332015-08-14 10:35:55 -07002147 // Update the |sq_error| and |variance| accumulators with the highest
2148 // SNR of reference vs output.
2149 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2150 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002151 }
2152
ekmeyerson60d9b332015-08-14 10:35:55 -07002153 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2154 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2155 << cf[i].num_input << ", " << cf[i].num_output << ", "
2156 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2157 << ", " << file_direction << "): ";
2158 if (sq_error > 0) {
2159 double snr = 10 * log10(variance / sq_error);
2160 EXPECT_GE(snr, expected_snr);
2161 EXPECT_NE(0, expected_snr);
2162 std::cout << "SNR=" << snr << " dB" << std::endl;
2163 } else {
aluebs776593b2016-03-15 14:04:58 -07002164 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002165 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002166
ekmeyerson60d9b332015-08-14 10:35:55 -07002167 fclose(out_file);
2168 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002169 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002170 }
2171}
2172
2173#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002174INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002175 CommonFormats,
2176 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002177 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2178 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2179 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2180 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2181 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2182 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2183 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2184 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2185 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2186 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2187 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2188 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002189
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002190 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2191 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2192 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2193 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2194 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2195 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2196 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2197 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2198 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2199 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2200 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2201 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002202
Per Åhgrenc0424252019-12-10 13:04:15 +01002203 std::make_tuple(32000, 48000, 48000, 48000, 15, 0),
2204 std::make_tuple(32000, 48000, 32000, 48000, 15, 30),
2205 std::make_tuple(32000, 48000, 16000, 48000, 15, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002206 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2207 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2208 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2209 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2210 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2211 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2212 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2213 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2214 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002215
Per Åhgrenc0424252019-12-10 13:04:15 +01002216 std::make_tuple(16000, 48000, 48000, 48000, 9, 0),
2217 std::make_tuple(16000, 48000, 32000, 48000, 9, 30),
2218 std::make_tuple(16000, 48000, 16000, 48000, 9, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002219 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2220 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2221 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2222 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2223 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2224 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2225 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2226 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2227 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002228
2229#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002230INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002231 CommonFormats,
2232 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002233 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2234 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2235 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002236 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2237 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2238 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002239 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2240 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2241 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002242 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2243 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2244 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002245
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002246 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2247 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2248 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2249 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2250 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2251 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002252 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2253 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2254 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2255 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2256 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2257 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002258
Per Åhgrenc0424252019-12-10 13:04:15 +01002259 std::make_tuple(32000, 48000, 48000, 48000, 17, 0),
2260 std::make_tuple(32000, 48000, 32000, 48000, 17, 30),
2261 std::make_tuple(32000, 48000, 16000, 48000, 17, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002262 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2263 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2264 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002265 std::make_tuple(32000, 32000, 48000, 32000, 27, 35),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002266 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002267 std::make_tuple(32000, 32000, 16000, 32000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002268 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2269 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2270 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002271
Per Åhgrenc0424252019-12-10 13:04:15 +01002272 std::make_tuple(16000, 48000, 48000, 48000, 11, 0),
2273 std::make_tuple(16000, 48000, 32000, 48000, 11, 30),
2274 std::make_tuple(16000, 48000, 16000, 48000, 11, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002275 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2276 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2277 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002278 std::make_tuple(16000, 32000, 48000, 32000, 24, 35),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002279 std::make_tuple(16000, 32000, 32000, 32000, 24, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002280 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002281 std::make_tuple(16000, 16000, 48000, 16000, 28, 20),
2282 std::make_tuple(16000, 16000, 32000, 16000, 28, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002283 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002284#endif
2285
Per Åhgren3e8bf282019-08-29 23:38:40 +02002286// Produces a scoped trace debug output.
2287std::string ProduceDebugText(int render_input_sample_rate_hz,
2288 int render_output_sample_rate_hz,
2289 int capture_input_sample_rate_hz,
2290 int capture_output_sample_rate_hz,
2291 size_t render_input_num_channels,
2292 size_t render_output_num_channels,
2293 size_t capture_input_num_channels,
2294 size_t capture_output_num_channels) {
2295 rtc::StringBuilder ss;
2296 ss << "Sample rates:"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002297 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002298 << render_input_sample_rate_hz
2299 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002300 "\n Render output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002301 << render_output_sample_rate_hz
2302 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002303 "\n Capture input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002304 << capture_input_sample_rate_hz
2305 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002306 "\n Capture output: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002307 << capture_output_sample_rate_hz
2308 << " Hz"
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002309 "\nNumber of channels:"
2310 "\n Render input: "
Jonas Olssonb2b20312020-01-14 12:11:31 +01002311 << render_input_num_channels
Jonas Olsson6c9bc392020-01-14 15:54:35 +01002312 << "\n Render output: " << render_output_num_channels
2313 << "\n Capture input: " << capture_input_num_channels
2314 << "\n Capture output: " << capture_output_num_channels;
Per Åhgren3e8bf282019-08-29 23:38:40 +02002315 return ss.Release();
2316}
2317
2318// Validates that running the audio processing module using various combinations
2319// of sample rates and number of channels works as intended.
2320void RunApmRateAndChannelTest(
2321 rtc::ArrayView<const int> sample_rates_hz,
2322 rtc::ArrayView<const int> render_channel_counts,
2323 rtc::ArrayView<const int> capture_channel_counts) {
Per Åhgrencc73ed32020-04-26 23:56:17 +02002324 std::unique_ptr<AudioProcessing> apm(
2325 AudioProcessingBuilderForTesting().Create());
Per Åhgren3e8bf282019-08-29 23:38:40 +02002326 webrtc::AudioProcessing::Config apm_config;
2327 apm_config.echo_canceller.enabled = true;
2328 apm->ApplyConfig(apm_config);
2329
2330 StreamConfig render_input_stream_config;
2331 StreamConfig render_output_stream_config;
2332 StreamConfig capture_input_stream_config;
2333 StreamConfig capture_output_stream_config;
2334
2335 std::vector<float> render_input_frame_channels;
2336 std::vector<float*> render_input_frame;
2337 std::vector<float> render_output_frame_channels;
2338 std::vector<float*> render_output_frame;
2339 std::vector<float> capture_input_frame_channels;
2340 std::vector<float*> capture_input_frame;
2341 std::vector<float> capture_output_frame_channels;
2342 std::vector<float*> capture_output_frame;
2343
2344 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2345 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2346 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2347 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2348 for (size_t render_input_num_channels : render_channel_counts) {
2349 for (size_t capture_input_num_channels : capture_channel_counts) {
2350 size_t render_output_num_channels = render_input_num_channels;
2351 size_t capture_output_num_channels = capture_input_num_channels;
2352 auto populate_audio_frame = [](int sample_rate_hz,
2353 size_t num_channels,
2354 StreamConfig* cfg,
2355 std::vector<float>* channels_data,
2356 std::vector<float*>* frame_data) {
2357 cfg->set_sample_rate_hz(sample_rate_hz);
2358 cfg->set_num_channels(num_channels);
2359 cfg->set_has_keyboard(false);
2360
2361 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2362 channels_data->resize(num_channels * max_frame_size);
2363 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2364 frame_data->resize(num_channels);
2365 for (size_t channel = 0; channel < num_channels; ++channel) {
2366 (*frame_data)[channel] =
2367 &(*channels_data)[channel * max_frame_size];
2368 }
2369 };
2370
2371 populate_audio_frame(
2372 render_input_sample_rate_hz, render_input_num_channels,
2373 &render_input_stream_config, &render_input_frame_channels,
2374 &render_input_frame);
2375 populate_audio_frame(
2376 render_output_sample_rate_hz, render_output_num_channels,
2377 &render_output_stream_config, &render_output_frame_channels,
2378 &render_output_frame);
2379 populate_audio_frame(
2380 capture_input_sample_rate_hz, capture_input_num_channels,
2381 &capture_input_stream_config, &capture_input_frame_channels,
2382 &capture_input_frame);
2383 populate_audio_frame(
2384 capture_output_sample_rate_hz, capture_output_num_channels,
2385 &capture_output_stream_config, &capture_output_frame_channels,
2386 &capture_output_frame);
2387
2388 for (size_t frame = 0; frame < 2; ++frame) {
2389 SCOPED_TRACE(ProduceDebugText(
2390 render_input_sample_rate_hz, render_output_sample_rate_hz,
2391 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2392 render_input_num_channels, render_output_num_channels,
2393 render_input_num_channels, capture_output_num_channels));
2394
2395 int result = apm->ProcessReverseStream(
2396 &render_input_frame[0], render_input_stream_config,
2397 render_output_stream_config, &render_output_frame[0]);
2398 EXPECT_EQ(result, AudioProcessing::kNoError);
2399 result = apm->ProcessStream(
2400 &capture_input_frame[0], capture_input_stream_config,
2401 capture_output_stream_config, &capture_output_frame[0]);
2402 EXPECT_EQ(result, AudioProcessing::kNoError);
2403 }
2404 }
2405 }
2406 }
2407 }
2408 }
2409 }
2410}
2411
niklase@google.com470e71d2011-07-07 08:21:25 +00002412} // namespace
peahc19f3122016-10-07 14:54:10 -07002413
Alessio Bazzicac054e782018-04-16 12:10:09 +02002414TEST(RuntimeSettingTest, TestDefaultCtor) {
2415 auto s = AudioProcessing::RuntimeSetting();
2416 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2417}
2418
Tommia5e07cc2020-05-26 21:40:37 +02002419TEST(RuntimeSettingDeathTest, TestCapturePreGain) {
Alessio Bazzicac054e782018-04-16 12:10:09 +02002420 using Type = AudioProcessing::RuntimeSetting::Type;
2421 {
2422 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2423 EXPECT_EQ(Type::kCapturePreGain, s.type());
2424 float v;
2425 s.GetFloat(&v);
2426 EXPECT_EQ(1.25f, v);
2427 }
2428
2429#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2430 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2431#endif
2432}
2433
Tommia5e07cc2020-05-26 21:40:37 +02002434TEST(RuntimeSettingDeathTest, TestCaptureFixedPostGain) {
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002435 using Type = AudioProcessing::RuntimeSetting::Type;
2436 {
2437 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2438 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2439 float v;
2440 s.GetFloat(&v);
2441 EXPECT_EQ(1.25f, v);
2442 }
2443
2444#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2445 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2446#endif
2447}
2448
Alessio Bazzicac054e782018-04-16 12:10:09 +02002449TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2450 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2451 auto s = AudioProcessing::RuntimeSetting();
2452 ASSERT_TRUE(q.Insert(&s));
2453 ASSERT_TRUE(q.Remove(&s));
2454 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2455}
2456
Sam Zackrisson0beac582017-09-25 12:04:02 +02002457TEST(ApmConfiguration, EnablePostProcessing) {
2458 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002459 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002460 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002461 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002462 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002463 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002464 AudioProcessingBuilderForTesting()
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002465 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002466 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002467
Per Åhgren2507f8c2020-03-19 12:33:29 +01002468 Int16FrameData audio;
2469 audio.num_channels = 1;
Sam Zackrisson0beac582017-09-25 12:04:02 +02002470 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2471
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002472 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002473 apm->ProcessStream(audio.data.data(),
2474 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2475 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002476 audio.data.data());
Sam Zackrisson0beac582017-09-25 12:04:02 +02002477}
2478
Alex Loiko5825aa62017-12-18 16:02:40 +01002479TEST(ApmConfiguration, EnablePreProcessing) {
2480 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002481 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002482 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002483 auto mock_pre_processor =
2484 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002485 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002486 AudioProcessingBuilderForTesting()
Ivo Creusen62337e52018-01-09 14:17:33 +01002487 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002488 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002489
Per Åhgren2507f8c2020-03-19 12:33:29 +01002490 Int16FrameData audio;
2491 audio.num_channels = 1;
Alex Loiko5825aa62017-12-18 16:02:40 +01002492 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2493
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002494 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002495 apm->ProcessReverseStream(
2496 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2497 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2498 audio.data.data());
Alex Loiko5825aa62017-12-18 16:02:40 +01002499}
2500
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002501TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2502 // Verify that apm uses a capture analyzer if one is provided.
2503 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002504 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002505 auto mock_capture_analyzer =
2506 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2507 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002508 AudioProcessingBuilderForTesting()
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002509 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2510 .Create();
2511
Per Åhgren2507f8c2020-03-19 12:33:29 +01002512 Int16FrameData audio;
2513 audio.num_channels = 1;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002514 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2515
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002516 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002517 apm->ProcessStream(audio.data.data(),
2518 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2519 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002520 audio.data.data());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002521}
2522
Alex Loiko73ec0192018-05-15 10:52:28 +02002523TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2524 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002525 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002526 auto mock_pre_processor =
2527 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2528 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002529 AudioProcessingBuilderForTesting()
Alex Loiko73ec0192018-05-15 10:52:28 +02002530 .SetRenderPreProcessing(std::move(mock_pre_processor))
2531 .Create();
2532 apm->SetRuntimeSetting(
2533 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2534
2535 // RuntimeSettings forwarded during 'Process*Stream' calls.
2536 // Therefore we have to make one such call.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002537 Int16FrameData audio;
2538 audio.num_channels = 1;
Alex Loiko73ec0192018-05-15 10:52:28 +02002539 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2540
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002541 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2542 .Times(1);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002543 apm->ProcessReverseStream(
2544 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2545 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2546 audio.data.data());
Alex Loiko73ec0192018-05-15 10:52:28 +02002547}
2548
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002549class MyEchoControlFactory : public EchoControlFactory {
2550 public:
2551 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2552 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002553 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2554 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
Per Åhgrenc20a19c2019-11-13 11:12:29 +01002555 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_, ::testing::_))
2556 .Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002557 return std::unique_ptr<EchoControl>(ec);
2558 }
Per Åhgrence202a02019-09-02 17:01:19 +02002559
2560 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
Per Åhgren4e5c7092019-11-01 20:44:11 +01002561 int num_render_channels,
2562 int num_capture_channels) {
Per Åhgrence202a02019-09-02 17:01:19 +02002563 return Create(sample_rate_hz);
2564 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002565};
2566
2567TEST(ApmConfiguration, EchoControlInjection) {
2568 // Verify that apm uses an injected echo controller if one is provided.
2569 webrtc::Config webrtc_config;
2570 std::unique_ptr<EchoControlFactory> echo_control_factory(
2571 new MyEchoControlFactory());
2572
Alex Loiko5825aa62017-12-18 16:02:40 +01002573 rtc::scoped_refptr<AudioProcessing> apm =
Per Åhgrencc73ed32020-04-26 23:56:17 +02002574 AudioProcessingBuilderForTesting()
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002575 .SetEchoControlFactory(std::move(echo_control_factory))
2576 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002577
Per Åhgren2507f8c2020-03-19 12:33:29 +01002578 Int16FrameData audio;
2579 audio.num_channels = 1;
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002580 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002581 apm->ProcessStream(audio.data.data(),
2582 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2583 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002584 audio.data.data());
Per Åhgren2507f8c2020-03-19 12:33:29 +01002585 apm->ProcessReverseStream(
2586 audio.data.data(), StreamConfig(audio.sample_rate_hz, audio.num_channels),
2587 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2588 audio.data.data());
2589 apm->ProcessStream(audio.data.data(),
2590 StreamConfig(audio.sample_rate_hz, audio.num_channels),
2591 StreamConfig(audio.sample_rate_hz, audio.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002592 audio.data.data());
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002593}
Ivo Creusenae026092017-11-20 13:07:16 +01002594
Per Åhgren8607f842019-04-12 22:02:26 +02002595std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002596 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002597 std::unique_ptr<AudioProcessing> apm(
Per Åhgrencc73ed32020-04-26 23:56:17 +02002598 AudioProcessingBuilderForTesting().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002599 if (!apm) {
2600 return apm;
2601 }
2602
2603 ProcessingConfig processing_config = {
2604 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2605
2606 if (apm->Initialize(processing_config) != 0) {
2607 return nullptr;
2608 }
2609
2610 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002611 AudioProcessing::Config apm_config;
2612 apm_config.residual_echo_detector.enabled = true;
2613 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002614 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002615 apm_config.gain_controller2.enabled = false;
2616 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002617 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002618 apm_config.noise_suppression.enabled = false;
2619 apm_config.level_estimation.enabled = false;
2620 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002621 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002622 return apm;
2623}
2624
2625#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2626#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2627#else
2628#define MAYBE_ApmStatistics ApmStatistics
2629#endif
2630
Per Åhgren8607f842019-04-12 22:02:26 +02002631TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2632 // Set up APM with AEC3 and process some audio.
2633 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002634 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002635 AudioProcessing::Config apm_config;
2636 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002637 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002638
2639 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002640 Int16FrameData frame;
2641 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002642 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002643
2644 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002645 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002646 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2647 ptr[i] = 10000 * ((i % 3) - 1);
2648 }
2649
2650 // Do some processing.
2651 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002652 EXPECT_EQ(apm->ProcessReverseStream(
2653 frame.data.data(),
2654 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2655 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2656 frame.data.data()),
2657 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002658 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002659 EXPECT_EQ(apm->ProcessStream(
2660 frame.data.data(),
2661 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2662 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002663 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002664 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002665 }
2666
2667 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002668 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002669 // We expect all statistics to be set and have a sensible value.
2670 ASSERT_TRUE(stats.residual_echo_likelihood);
2671 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2672 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2673 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2674 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2675 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2676 ASSERT_TRUE(stats.echo_return_loss);
2677 EXPECT_NE(*stats.echo_return_loss, -100.0);
2678 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2679 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002680}
2681
2682TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2683 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002684 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002685 ASSERT_TRUE(apm);
2686
2687 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002688 Int16FrameData frame;
2689 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002690 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002691
2692 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002693 int16_t* ptr = frame.data.data();
Ivo Creusenae026092017-11-20 13:07:16 +01002694 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2695 ptr[i] = 10000 * ((i % 3) - 1);
2696 }
2697
2698 // Do some processing.
2699 for (int i = 0; i < 200; i++) {
Per Åhgren2507f8c2020-03-19 12:33:29 +01002700 EXPECT_EQ(apm->ProcessReverseStream(
2701 frame.data.data(),
2702 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2703 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2704 frame.data.data()),
2705 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002706 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002707 EXPECT_EQ(apm->ProcessStream(
2708 frame.data.data(),
2709 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2710 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002711 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002712 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002713 }
2714
2715 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002716 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002717 // We expect only the residual echo detector statistics to be set and have a
2718 // sensible value.
2719 EXPECT_TRUE(stats.residual_echo_likelihood);
2720 if (stats.residual_echo_likelihood) {
2721 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2722 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2723 }
2724 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2725 if (stats.residual_echo_likelihood_recent_max) {
2726 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2727 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2728 }
2729 EXPECT_FALSE(stats.echo_return_loss);
2730 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002731}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002732
2733TEST(ApmStatistics, ReportOutputRmsDbfs) {
2734 ProcessingConfig processing_config = {
2735 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2736 AudioProcessing::Config config;
2737
2738 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002739 Int16FrameData frame;
2740 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002741 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002742
2743 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002744 int16_t* ptr = frame.data.data();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002745 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2746 ptr[i] = 10000 * ((i % 3) - 1);
2747 }
2748
Per Åhgrencc73ed32020-04-26 23:56:17 +02002749 std::unique_ptr<AudioProcessing> apm(
2750 AudioProcessingBuilderForTesting().Create());
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002751 apm->Initialize(processing_config);
2752
2753 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002754 EXPECT_EQ(
2755 apm->ProcessStream(frame.data.data(),
2756 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2757 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002758 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002759 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002760 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002761
2762 // If enabled, metrics should be reported.
2763 config.level_estimation.enabled = true;
2764 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002765 EXPECT_EQ(
2766 apm->ProcessStream(frame.data.data(),
2767 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2768 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002769 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002770 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002771 auto stats = apm->GetStatistics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002772 EXPECT_TRUE(stats.output_rms_dbfs);
2773 EXPECT_GE(*stats.output_rms_dbfs, 0);
2774
2775 // If re-disabled, the value is again not reported.
2776 config.level_estimation.enabled = false;
2777 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002778 EXPECT_EQ(
2779 apm->ProcessStream(frame.data.data(),
2780 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2781 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002782 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002783 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002784 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002785}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002786
2787TEST(ApmStatistics, ReportHasVoice) {
2788 ProcessingConfig processing_config = {
2789 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2790 AudioProcessing::Config config;
2791
2792 // Set up an audioframe.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002793 Int16FrameData frame;
2794 frame.num_channels = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002795 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2796
2797 // Fill the audio frame with a sawtooth pattern.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002798 int16_t* ptr = frame.data.data();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002799 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2800 ptr[i] = 10000 * ((i % 3) - 1);
2801 }
2802
Per Åhgrencc73ed32020-04-26 23:56:17 +02002803 std::unique_ptr<AudioProcessing> apm(
2804 AudioProcessingBuilderForTesting().Create());
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002805 apm->Initialize(processing_config);
2806
2807 // If not enabled, no metric should be reported.
Per Åhgren2507f8c2020-03-19 12:33:29 +01002808 EXPECT_EQ(
2809 apm->ProcessStream(frame.data.data(),
2810 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2811 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002812 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002813 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002814 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002815
2816 // If enabled, metrics should be reported.
2817 config.voice_detection.enabled = true;
2818 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002819 EXPECT_EQ(
2820 apm->ProcessStream(frame.data.data(),
2821 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2822 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002823 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002824 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002825 auto stats = apm->GetStatistics();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002826 EXPECT_TRUE(stats.voice_detected);
2827
2828 // If re-disabled, the value is again not reported.
2829 config.voice_detection.enabled = false;
2830 apm->ApplyConfig(config);
Per Åhgren2507f8c2020-03-19 12:33:29 +01002831 EXPECT_EQ(
2832 apm->ProcessStream(frame.data.data(),
2833 StreamConfig(frame.sample_rate_hz, frame.num_channels),
2834 StreamConfig(frame.sample_rate_hz, frame.num_channels),
Per Åhgrendc5522b2020-03-19 14:55:58 +01002835 frame.data.data()),
Per Åhgren2507f8c2020-03-19 12:33:29 +01002836 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002837 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002838}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002839
2840TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2841 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2842 std::array<int, 2> render_channel_counts = {1, 7};
2843 std::array<int, 2> capture_channel_counts = {1, 7};
2844 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2845 capture_channel_counts);
2846}
2847
2848TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2849 std::array<int, 1> sample_rates_hz = {48000};
2850 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2851 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2852 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2853 capture_channel_counts);
2854}
2855
2856TEST(ApmConfiguration, HandlingOfRateCombinations) {
2857 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2858 48000, 96000, 192000, 384000};
2859 std::array<int, 1> render_channel_counts = {2};
2860 std::array<int, 1> capture_channel_counts = {2};
2861 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2862 capture_channel_counts);
2863}
2864
Yves Gerey1fce3f82019-12-05 17:45:31 +01002865TEST(ApmConfiguration, SelfAssignment) {
2866 // At some point memory sanitizer was complaining about self-assigment.
2867 // Make sure we don't regress.
2868 AudioProcessing::Config config;
2869 AudioProcessing::Config* config2 = &config;
2870 *config2 = *config2; // Workaround -Wself-assign-overloaded
2871 SUCCEED(); // Real success is absence of defects from asan/msan/ubsan.
2872}
2873
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002874} // namespace webrtc