blob: 7ff37cb2d7482129d0853ed117551a29a0bb54fb [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 */
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000010#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
Oleh Prypin708eccc2019-03-27 09:38:52 +010014#include <cmath>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_audio/include/audio_util.h"
20#include "common_audio/resampler/include/push_resampler.h"
21#include "common_audio/resampler/push_sinc_resampler.h"
22#include "common_audio/signal_processing/include/signal_processing_library.h"
23#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
24#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/common.h"
26#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020027#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/test/protobuf_utils.h"
29#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/arraysize.h"
31#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/gtest_prod_util.h"
34#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010035#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010036#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020039#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020040#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020041#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010042#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080045#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070046
47RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000049#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#endif
kwiberg77eab702016-09-28 17:42:01 -070053RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000054
andrew@webrtc.org27c69802014-02-18 20:24:56 +000055namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000056namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000057
ekmeyerson60d9b332015-08-14 10:35:55 -070058// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
59// applicable.
60
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000061// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000062// re-implemented; see comment at the bottom of this file. For now, the user has
63// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000064// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000065// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000066// be set to true with the command-line switch --write_ref_data.
67bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070068const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070069const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000070
aluebseb3603b2016-04-20 15:27:58 -070071#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
72// Android doesn't support 48kHz.
73const int kProcessSampleRates[] = {8000, 16000, 32000};
74#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070075const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070076#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000077
ekmeyerson60d9b332015-08-14 10:35:55 -070078enum StreamDirection { kForward = 0, kReverse };
79
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082 cb->num_channels());
83 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000085 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) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000088 S16ToFloat(cb_int.channels()[i],
89 cb->num_frames(),
90 cb->channels()[i]);
91 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000092}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000093
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070095 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000096}
97
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080099size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000100 switch (layout) {
101 case AudioProcessing::kMono:
102 return 1;
103 case AudioProcessing::kMonoAndKeyboard:
104 case AudioProcessing::kStereo:
105 return 2;
106 case AudioProcessing::kStereoAndKeyboard:
107 return 3;
108 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700109 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800110 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000111}
112
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000113int TruncateToMultipleOf10(int value) {
114 return (value / 10) * 10;
115}
116
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800118 size_t samples_per_channel) {
119 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000121}
122
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800124 size_t samples_per_channel) {
125 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000126 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
127}
128
pkasting25702cb2016-01-08 13:50:27 -0800129void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
130 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 stereo[i * 2 + 1] = stereo[i * 2];
132 }
133}
134
yujo36b1a5f2017-06-12 12:45:32 -0700135void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800136 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000137 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
138 }
139}
140
141void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700142 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
144 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700145 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000146 }
147}
148
149void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800150 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700151 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700153 frame_data[i] = left;
154 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000155 }
156}
157
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000158void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700159 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
161 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700162 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000163 }
164}
165
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 return false;
169 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 return false;
172 }
yujo36b1a5f2017-06-12 12:45:32 -0700173 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000174 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000175 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000176 return false;
177 }
178 return true;
179}
180
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000181void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200182 AudioProcessing::Config apm_config = ap->GetConfig();
183 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200185 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100186
187 apm_config.gain_controller1.enabled = true;
188 apm_config.gain_controller1.mode =
189 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000190#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Per Ã…hgren200feba2019-03-06 04:16:46 +0100191 // TODO(peah): Update tests to instead use AEC3.
192 apm_config.echo_canceller.use_legacy_aec = true;
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200193 apm_config.echo_canceller.mobile_mode = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200194 apm_config.echo_canceller.legacy_moderate_suppression_level = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000195
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100196 apm_config.gain_controller1.enabled = true;
197 apm_config.gain_controller1.mode =
198 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
199 apm_config.gain_controller1.analog_level_minimum = 0;
200 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000201#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000202
peah8271d042016-11-22 07:24:52 -0800203 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100204 apm_config.level_estimation.enabled = true;
peah8271d042016-11-22 07:24:52 -0800205 ap->ApplyConfig(apm_config);
206
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000207 EXPECT_NOERR(ap->level_estimator()->Enable(true));
208 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
209
210 EXPECT_NOERR(ap->voice_detection()->Enable(true));
211}
212
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000213// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000214template <class T>
215T AbsValue(T a) {
216 return a > 0 ? a: -a;
217}
218
219int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800220 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700221 const int16_t* frame_data = frame.data();
222 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800223 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700224 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000225 }
226
227 return max_data;
228}
229
Alex Loiko890988c2017-08-31 10:25:48 +0200230void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700231 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 ASSERT_TRUE(file != NULL);
234
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100235 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800237 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000238 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000240 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000242 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243 fclose(file);
244}
245
Alex Loiko890988c2017-08-31 10:25:48 +0200246std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200247 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000248 // Resource files are all stereo.
249 ss << name << sample_rate_hz / 1000 << "_stereo";
250 return test::ResourcePath(ss.str(), "pcm");
251}
252
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000253// Temporary filenames unique to this process. Used to be able to run these
254// tests in parallel as each process needs to be running in isolation they can't
255// have competing filenames.
256std::map<std::string, std::string> temp_filenames;
257
Alex Loiko890988c2017-08-31 10:25:48 +0200258std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000259 int input_rate,
260 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700261 int reverse_input_rate,
262 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800263 size_t num_input_channels,
264 size_t num_output_channels,
265 size_t num_reverse_input_channels,
266 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700267 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200268 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700269 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
270 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000271 if (num_output_channels == 1) {
272 ss << "mono";
273 } else if (num_output_channels == 2) {
274 ss << "stereo";
275 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700276 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000277 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 ss << output_rate / 1000;
279 if (num_reverse_output_channels == 1) {
280 ss << "_rmono";
281 } else if (num_reverse_output_channels == 2) {
282 ss << "_rstereo";
283 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700284 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700285 }
286 ss << reverse_output_rate / 1000;
287 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000288
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000289 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700290 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000291 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
292 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000293}
294
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000295void ClearTempFiles() {
296 for (auto& kv : temp_filenames)
297 remove(kv.second.c_str());
298}
299
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200300// Only remove "out" files. Keep "ref" files.
301void ClearTempOutFiles() {
302 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
303 const std::string& filename = it->first;
304 if (filename.substr(0, 3).compare("out") == 0) {
305 remove(it->second.c_str());
306 temp_filenames.erase(it++);
307 } else {
308 it++;
309 }
310 }
311}
312
Alex Loiko890988c2017-08-31 10:25:48 +0200313void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000314 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000315 ASSERT_TRUE(file != NULL);
316 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000317 fclose(file);
318}
319
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000320// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
321// stereo) file, converts to deinterleaved float (optionally downmixing) and
322// returns the result in |cb|. Returns false if the file ended (or on error) and
323// true otherwise.
324//
325// |int_data| and |float_data| are just temporary space that must be
326// sufficiently large to hold the 10 ms chunk.
327bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
328 ChannelBuffer<float>* cb) {
329 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000330 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000331 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
332 if (read_count != frame_size) {
333 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700334 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000335 return false; // This is expected.
336 }
337
338 S16ToFloat(int_data, frame_size, float_data);
339 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000340 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000341 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000342 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000343 cb->channels());
344 }
345
346 return true;
347}
348
niklase@google.com470e71d2011-07-07 08:21:25 +0000349class ApmTest : public ::testing::Test {
350 protected:
351 ApmTest();
352 virtual void SetUp();
353 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000354
355 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000356 }
357
358 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000359 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000360 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000361
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000362 // Used to select between int and float interface tests.
363 enum Format {
364 kIntFormat,
365 kFloatFormat
366 };
367
368 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000369 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000370 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800371 size_t num_input_channels,
372 size_t num_output_channels,
373 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000374 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000375 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000376 void EnableAllComponents();
377 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000378 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000379 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000380 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
381 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000382 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000383 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
384 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800386 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700387 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800388 void TestChangingForwardChannels(size_t num_in_channels,
389 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800391 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700392 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000393 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
394 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000395 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000396 int ProcessStreamChooser(Format format);
397 int AnalyzeReverseStreamChooser(Format format);
398 void ProcessDebugDump(const std::string& in_filename,
399 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800400 Format format,
401 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000402 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403
404 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000405 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800406 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000407 AudioFrame* frame_;
408 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800409 std::unique_ptr<ChannelBuffer<float> > float_cb_;
410 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800412 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 FILE* far_file_;
414 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000415 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416};
417
418ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000419 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000420#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800421 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
422 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000423#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000424#if defined(WEBRTC_MAC)
425 // A different file for Mac is needed because on this platform the AEC
426 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800427 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
428 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000429#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800430 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
431 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000432#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000433#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000434 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000435 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000436 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000437 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000438 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000439 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000440 out_file_(NULL) {
441 Config config;
442 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100443 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000444}
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
446void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000447 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000448
449 frame_ = new AudioFrame();
450 revframe_ = new AudioFrame();
451
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000452 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000453}
454
455void ApmTest::TearDown() {
456 if (frame_) {
457 delete frame_;
458 }
459 frame_ = NULL;
460
461 if (revframe_) {
462 delete revframe_;
463 }
464 revframe_ = NULL;
465
466 if (far_file_) {
467 ASSERT_EQ(0, fclose(far_file_));
468 }
469 far_file_ = NULL;
470
471 if (near_file_) {
472 ASSERT_EQ(0, fclose(near_file_));
473 }
474 near_file_ = NULL;
475
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000476 if (out_file_) {
477 ASSERT_EQ(0, fclose(out_file_));
478 }
479 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000480}
481
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000482void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000483 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700484 ap->Initialize(
485 {{{frame_->sample_rate_hz_, frame_->num_channels_},
486 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700487 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700488 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000489}
490
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000491void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000492 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000493 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800494 size_t num_input_channels,
495 size_t num_output_channels,
496 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000497 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000498 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000499 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000500 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000501
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000502 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
503 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000504 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000505
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000506 if (far_file_) {
507 ASSERT_EQ(0, fclose(far_file_));
508 }
509 std::string filename = ResourceFilePath("far", sample_rate_hz);
510 far_file_ = fopen(filename.c_str(), "rb");
511 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
512 filename << "\n";
513
514 if (near_file_) {
515 ASSERT_EQ(0, fclose(near_file_));
516 }
517 filename = ResourceFilePath("near", sample_rate_hz);
518 near_file_ = fopen(filename.c_str(), "rb");
519 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
520 filename << "\n";
521
522 if (open_output_file) {
523 if (out_file_) {
524 ASSERT_EQ(0, fclose(out_file_));
525 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700526 filename = OutputFilePath(
527 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
528 reverse_sample_rate_hz, num_input_channels, num_output_channels,
529 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000530 out_file_ = fopen(filename.c_str(), "wb");
531 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
532 filename << "\n";
533 }
534}
535
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000536void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000537 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000538}
539
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
541 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000542 // The files always contain stereo audio.
543 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700544 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000545 sizeof(int16_t),
546 frame_size,
547 file);
548 if (read_count != frame_size) {
549 // Check that the file really ended.
550 EXPECT_NE(0, feof(file));
551 return false; // This is expected.
552 }
553
554 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700555 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000556 frame->samples_per_channel_);
557 }
558
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000559 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000560 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000562 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000563}
564
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000565bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
566 return ReadFrame(file, frame, NULL);
567}
568
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000569// If the end of the file has been reached, rewind it and attempt to read the
570// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
572 ChannelBuffer<float>* cb) {
573 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000574 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000575 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000576 }
577}
578
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000579void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
580 ReadFrameWithRewind(file, frame, NULL);
581}
582
andrew@webrtc.org81865342012-10-27 00:28:27 +0000583void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
584 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000585 EXPECT_EQ(apm_->kNoError,
586 apm_->gain_control()->set_stream_analog_level(127));
587 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000588}
589
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000590int ApmTest::ProcessStreamChooser(Format format) {
591 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000592 return apm_->ProcessStream(frame_);
593 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000594 return apm_->ProcessStream(float_cb_->channels(),
595 frame_->samples_per_channel_,
596 frame_->sample_rate_hz_,
597 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000598 output_sample_rate_hz_,
599 LayoutFromChannels(num_output_channels_),
600 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000601}
602
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000603int ApmTest::AnalyzeReverseStreamChooser(Format format) {
604 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700605 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000606 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000607 return apm_->AnalyzeReverseStream(
608 revfloat_cb_->channels(),
609 revframe_->samples_per_channel_,
610 revframe_->sample_rate_hz_,
611 LayoutFromChannels(revframe_->num_channels_));
612}
613
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000614void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
615 int delay_min, int delay_max) {
616 // The |revframe_| and |frame_| should include the proper frame information,
617 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000618 AudioFrame tmp_frame;
619 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 bool causal = true;
621
622 tmp_frame.CopyFrom(*revframe_);
623 SetFrameTo(&tmp_frame, 0);
624
625 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
626 // Initialize the |frame_queue| with empty frames.
627 int frame_delay = delay_ms / 10;
628 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000629 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000630 frame->CopyFrom(tmp_frame);
631 frame_queue.push(frame);
632 frame_delay++;
633 causal = false;
634 }
635 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000636 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000637 frame->CopyFrom(tmp_frame);
638 frame_queue.push(frame);
639 frame_delay--;
640 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000641 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
642 // need enough frames with audio to have reliable estimates, but as few as
643 // possible to keep processing time down. 4.5 seconds seemed to be a good
644 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000645 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000646 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 frame->CopyFrom(tmp_frame);
648 // Use the near end recording, since that has more speech in it.
649 ASSERT_TRUE(ReadFrame(near_file_, frame));
650 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000651 AudioFrame* reverse_frame = frame;
652 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000653 if (!causal) {
654 reverse_frame = frame_queue.front();
655 // When we call ProcessStream() the frame is modified, so we can't use the
656 // pointer directly when things are non-causal. Use an intermediate frame
657 // and copy the data.
658 process_frame = &tmp_frame;
659 process_frame->CopyFrom(*frame);
660 }
aluebsb0319552016-03-17 20:39:53 -0700661 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
663 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
664 frame = frame_queue.front();
665 frame_queue.pop();
666 delete frame;
667
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000668 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000669 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200670 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000671 }
672 }
673
674 rewind(near_file_);
675 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000676 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000677 frame_queue.pop();
678 delete frame;
679 }
680 // Calculate expected delay estimate and acceptable regions. Further,
681 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700682 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700683 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700684 const int expected_median =
685 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
686 const int expected_median_high = rtc::SafeClamp<int>(
687 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700688 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700689 const int expected_median_low = rtc::SafeClamp<int>(
690 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700691 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000692 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200693 AudioProcessingStats stats =
694 apm_->GetStatistics(true /* has_remote_tracks */);
695 ASSERT_TRUE(stats.delay_median_ms.has_value());
696 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000697 EXPECT_GE(expected_median_high, median);
698 EXPECT_LE(expected_median_low, median);
699}
700
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000701void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000703 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000704
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000705 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000707 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000708 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000709
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000710 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 EXPECT_EQ(apm_->kNoError,
712 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000713 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000714 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000715 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000716
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200718 AudioProcessing::Config apm_config = apm_->GetConfig();
719 apm_config.echo_canceller.enabled = true;
720 apm_config.echo_canceller.mobile_mode = false;
721 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000722 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000723 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000726
727 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Ã…hgren200feba2019-03-06 04:16:46 +0100729 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000730
731 // Resets after successful ProcessStream().
732 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000733 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Ã…hgren200feba2019-03-06 04:16:46 +0100734 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000735
736 // Other stream parameters set correctly.
737 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
738 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000739 apm_->gain_control()->set_stream_analog_level(127));
Per Ã…hgren200feba2019-03-06 04:16:46 +0100740 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000741 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
742
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000743 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000744 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000745 AnalyzeReverseStreamChooser(format));
Per Ã…hgren200feba2019-03-06 04:16:46 +0100746 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000747
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000748 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000749 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000750 EXPECT_EQ(apm_->kNoError,
751 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000752 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000753}
754
755TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000756 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000757}
758
759TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000760 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000761}
762
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000763TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
764 EXPECT_EQ(0, apm_->delay_offset_ms());
765 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
766 EXPECT_EQ(50, apm_->stream_delay_ms());
767}
768
769TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
770 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000771 apm_->set_delay_offset_ms(100);
772 EXPECT_EQ(100, apm_->delay_offset_ms());
773 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000774 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000775 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
776 EXPECT_EQ(200, apm_->stream_delay_ms());
777
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000778 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000779 apm_->set_delay_offset_ms(-50);
780 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000781 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
782 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000783 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
784 EXPECT_EQ(50, apm_->stream_delay_ms());
785}
786
Michael Graczyk86c6d332015-07-23 11:41:39 -0700787void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800788 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700789 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000790 frame_->num_channels_ = num_channels;
791 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700792 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000793}
794
Michael Graczyk86c6d332015-07-23 11:41:39 -0700795void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800796 size_t num_in_channels,
797 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700798 AudioProcessing::Error expected_return) {
799 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
800 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
801
802 EXPECT_EQ(expected_return,
803 apm_->ProcessStream(float_cb_->channels(), input_stream,
804 output_stream, float_cb_->channels()));
805}
806
807void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800808 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700809 AudioProcessing::Error expected_return) {
810 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700811 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
812 {output_sample_rate_hz_, apm_->num_output_channels()},
813 {frame_->sample_rate_hz_, num_rev_channels},
814 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700815
ekmeyerson60d9b332015-08-14 10:35:55 -0700816 EXPECT_EQ(
817 expected_return,
818 apm_->ProcessReverseStream(
819 float_cb_->channels(), processing_config.reverse_input_stream(),
820 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700821}
822
823TEST_F(ApmTest, ChannelsInt16Interface) {
824 // Testing number of invalid and valid channels.
825 Init(16000, 16000, 16000, 4, 4, 4, false);
826
827 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
828
Peter Kasting69558702016-01-12 16:26:35 -0800829 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700830 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000831 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000832 }
833}
834
Michael Graczyk86c6d332015-07-23 11:41:39 -0700835TEST_F(ApmTest, Channels) {
836 // Testing number of invalid and valid channels.
837 Init(16000, 16000, 16000, 4, 4, 4, false);
838
839 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
840 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
841
Peter Kasting69558702016-01-12 16:26:35 -0800842 for (size_t i = 1; i < 4; ++i) {
843 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700844 // Output channels much be one or match input channels.
845 if (j == 1 || i == j) {
846 TestChangingForwardChannels(i, j, kNoErr);
847 TestChangingReverseChannels(i, kNoErr);
848
849 EXPECT_EQ(i, apm_->num_input_channels());
850 EXPECT_EQ(j, apm_->num_output_channels());
851 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800852 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700853 } else {
854 TestChangingForwardChannels(i, j,
855 AudioProcessing::kBadNumberChannelsError);
856 }
857 }
858 }
859}
860
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000861TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000862 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000863 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000864 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700866 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800867 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000868 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000869 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000870 }
871}
872
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000873TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000874 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000875 // Enable AEC only.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200876 AudioProcessing::Config apm_config = apm_->GetConfig();
877 apm_config.echo_canceller.enabled = true;
Per Ã…hgren200feba2019-03-06 04:16:46 +0100878 // TODO(peah): Update tests to instead use AEC3.
879 apm_config.echo_canceller.use_legacy_aec = true;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200880 apm_config.echo_canceller.mobile_mode = false;
881 apm_->ApplyConfig(apm_config);
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000882 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700883 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000884 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000885
886 // Internally in the AEC the amount of lookahead the delay estimation can
887 // handle is 15 blocks and the maximum delay is set to 60 blocks.
888 const int kLookaheadBlocks = 15;
889 const int kMaxDelayBlocks = 60;
890 // The AEC has a startup time before it actually starts to process. This
891 // procedure can flush the internal far-end buffer, which of course affects
892 // the delay estimation. Therefore, we set a system_delay high enough to
893 // avoid that. The smallest system_delay you can report without flushing the
894 // buffer is 66 ms in 8 kHz.
895 //
896 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
897 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
898 // delay estimation. This should be noted though. In case of test failure,
899 // this could be the cause.
900 const int kSystemDelayMs = 66;
901 // Test a couple of corner cases and verify that the estimated delay is
902 // within a valid region (set to +-1.5 blocks). Note that these cases are
903 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800904 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000905 Init(kProcessSampleRates[i],
906 kProcessSampleRates[i],
907 kProcessSampleRates[i],
908 2,
909 2,
910 2,
911 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000912 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700913 const int num_ms_per_block =
914 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000915 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
916 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
917
918 // 1) Verify correct delay estimate at lookahead boundary.
919 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
920 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
921 delay_max_ms);
922 // 2) A delay less than maximum lookahead should give an delay estimate at
923 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
924 delay_ms -= 20;
925 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
926 delay_max_ms);
927 // 3) Three values around zero delay. Note that we need to compensate for
928 // the fake system_delay.
929 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
930 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
931 delay_max_ms);
932 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
933 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
934 delay_max_ms);
935 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
936 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
937 delay_max_ms);
938 // 4) Verify correct delay estimate at maximum delay boundary.
939 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
940 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
941 delay_max_ms);
942 // 5) A delay above the maximum delay should give an estimate at the
943 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
944 delay_ms += 20;
945 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
946 delay_max_ms);
947 }
948}
949
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000950TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000951 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +0000952 EXPECT_EQ(apm_->kNoError,
953 apm_->gain_control()->set_mode(
954 apm_->gain_control()->mode()));
955
956 GainControl::Mode mode[] = {
957 GainControl::kAdaptiveAnalog,
958 GainControl::kAdaptiveDigital,
959 GainControl::kFixedDigital
960 };
pkasting25702cb2016-01-08 13:50:27 -0800961 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000962 EXPECT_EQ(apm_->kNoError,
963 apm_->gain_control()->set_mode(mode[i]));
964 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
965 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100966 // Testing target levels
niklase@google.com470e71d2011-07-07 08:21:25 +0000967 EXPECT_EQ(apm_->kNoError,
968 apm_->gain_control()->set_target_level_dbfs(
969 apm_->gain_control()->target_level_dbfs()));
970
971 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -0800972 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000973 EXPECT_EQ(apm_->kNoError,
974 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
975 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
976 }
977
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100978 // Testing compression gains
niklase@google.com470e71d2011-07-07 08:21:25 +0000979 EXPECT_EQ(apm_->kNoError,
980 apm_->gain_control()->set_compression_gain_db(
981 apm_->gain_control()->compression_gain_db()));
982
983 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -0800984 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000985 EXPECT_EQ(apm_->kNoError,
986 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100987 ProcessStreamChooser(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000988 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
989 }
990
991 // Testing limiter off/on
992 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
993 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
994 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
995 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
996
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100997 // Testing level limits
niklase@google.com470e71d2011-07-07 08:21:25 +0000998 EXPECT_EQ(apm_->kNoError,
999 apm_->gain_control()->set_analog_level_limits(
1000 apm_->gain_control()->analog_level_minimum(),
1001 apm_->gain_control()->analog_level_maximum()));
1002
1003 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001004 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001005 EXPECT_EQ(apm_->kNoError,
1006 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1007 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1008 }
1009
1010 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001011 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001012 EXPECT_EQ(apm_->kNoError,
1013 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1014 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1015 }
1016
1017 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1018
1019 // Turn AGC off
1020 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1021 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1022}
1023
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001024#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
1025TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
1026 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(-1), "");
1027}
1028
1029TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
1030 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(32), "");
1031}
1032
1033TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
1034 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(-1), "");
1035}
1036
1037TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
1038 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(91), "");
1039}
1040
1041TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
1042 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(-1, 512), "");
1043}
1044
1045TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
1046 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 65536), "");
1047}
1048
1049TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
1050 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 255), "");
1051}
1052
1053TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
1054 apm_->gain_control()->set_analog_level_limits(255, 512);
1055 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
1056}
1057
1058TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
1059 apm_->gain_control()->set_analog_level_limits(255, 512);
1060 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
1061}
1062#endif
1063
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001064void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001065 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001066 EXPECT_EQ(apm_->kNoError,
1067 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1068 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1069
1070 int out_analog_level = 0;
1071 for (int i = 0; i < 2000; ++i) {
1072 ReadFrameWithRewind(near_file_, frame_);
1073 // Ensure the audio is at a low level, so the AGC will try to increase it.
1074 ScaleFrame(frame_, 0.25);
1075
1076 // Always pass in the same volume.
1077 EXPECT_EQ(apm_->kNoError,
1078 apm_->gain_control()->set_stream_analog_level(100));
1079 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1080 out_analog_level = apm_->gain_control()->stream_analog_level();
1081 }
1082
1083 // Ensure the AGC is still able to reach the maximum.
1084 EXPECT_EQ(255, out_analog_level);
1085}
1086
1087// Verifies that despite volume slider quantization, the AGC can continue to
1088// increase its volume.
1089TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001090 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001091 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1092 }
1093}
1094
1095void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001096 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001097 EXPECT_EQ(apm_->kNoError,
1098 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1099 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1100
1101 int out_analog_level = 100;
1102 for (int i = 0; i < 1000; ++i) {
1103 ReadFrameWithRewind(near_file_, frame_);
1104 // Ensure the audio is at a low level, so the AGC will try to increase it.
1105 ScaleFrame(frame_, 0.25);
1106
1107 EXPECT_EQ(apm_->kNoError,
1108 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1109 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1110 out_analog_level = apm_->gain_control()->stream_analog_level();
1111 }
1112
1113 // Ensure the volume was raised.
1114 EXPECT_GT(out_analog_level, 100);
1115 int highest_level_reached = out_analog_level;
1116 // Simulate a user manual volume change.
1117 out_analog_level = 100;
1118
1119 for (int i = 0; i < 300; ++i) {
1120 ReadFrameWithRewind(near_file_, frame_);
1121 ScaleFrame(frame_, 0.25);
1122
1123 EXPECT_EQ(apm_->kNoError,
1124 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1125 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1126 out_analog_level = apm_->gain_control()->stream_analog_level();
1127 // Check that AGC respected the manually adjusted volume.
1128 EXPECT_LT(out_analog_level, highest_level_reached);
1129 }
1130 // Check that the volume was still raised.
1131 EXPECT_GT(out_analog_level, 100);
1132}
1133
1134TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001135 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001136 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1137 }
1138}
1139
niklase@google.com470e71d2011-07-07 08:21:25 +00001140TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001141 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 NoiseSuppression::Level level[] = {
1143 NoiseSuppression::kLow,
1144 NoiseSuppression::kModerate,
1145 NoiseSuppression::kHigh,
1146 NoiseSuppression::kVeryHigh
1147 };
pkasting25702cb2016-01-08 13:50:27 -08001148 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001149 EXPECT_EQ(apm_->kNoError,
1150 apm_->noise_suppression()->set_level(level[i]));
1151 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1152 }
1153
andrew@webrtc.org648af742012-02-08 01:57:29 +00001154 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001155 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1156 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1157 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1158 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1159}
1160
1161TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001162 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001163 AudioProcessing::Config apm_config;
1164 apm_config.high_pass_filter.enabled = true;
1165 apm_->ApplyConfig(apm_config);
1166 apm_config.high_pass_filter.enabled = false;
1167 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001168}
1169
1170TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001171 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001172 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001173 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001174
1175 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1176
1177 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1178 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1179
1180 // Run this test in wideband; in super-wb, the splitting filter distorts the
1181 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001182 frame_->samples_per_channel_ = 160;
1183 frame_->num_channels_ = 2;
1184 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001185
1186 // Min value if no frames have been processed.
1187 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1188
1189 // Min value on zero frames.
1190 SetFrameTo(frame_, 0);
1191 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1192 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1193 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1194
1195 // Try a few RMS values.
1196 // (These also test that the value resets after retrieving it.)
1197 SetFrameTo(frame_, 32767);
1198 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1199 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1200 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1201
1202 SetFrameTo(frame_, 30000);
1203 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1204 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1205 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1206
1207 SetFrameTo(frame_, 10000);
1208 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1209 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1210 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1211
1212 SetFrameTo(frame_, 10);
1213 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1214 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1215 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1216
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001217 // Verify reset after enable/disable.
1218 SetFrameTo(frame_, 32767);
1219 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1220 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1221 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1222 SetFrameTo(frame_, 1);
1223 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1224 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1225
1226 // Verify reset after initialize.
1227 SetFrameTo(frame_, 32767);
1228 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1229 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1230 SetFrameTo(frame_, 1);
1231 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1232 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001233}
1234
1235TEST_F(ApmTest, VoiceDetection) {
1236 // Test external VAD
1237 EXPECT_EQ(apm_->kNoError,
1238 apm_->voice_detection()->set_stream_has_voice(true));
1239 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1240 EXPECT_EQ(apm_->kNoError,
1241 apm_->voice_detection()->set_stream_has_voice(false));
1242 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1243
andrew@webrtc.org648af742012-02-08 01:57:29 +00001244 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001245 VoiceDetection::Likelihood likelihood[] = {
1246 VoiceDetection::kVeryLowLikelihood,
1247 VoiceDetection::kLowLikelihood,
1248 VoiceDetection::kModerateLikelihood,
1249 VoiceDetection::kHighLikelihood
1250 };
pkasting25702cb2016-01-08 13:50:27 -08001251 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001252 EXPECT_EQ(apm_->kNoError,
1253 apm_->voice_detection()->set_likelihood(likelihood[i]));
1254 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1255 }
1256
1257 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001258 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001259 EXPECT_EQ(apm_->kBadParameterError,
1260 apm_->voice_detection()->set_frame_size_ms(12));
1261
andrew@webrtc.org648af742012-02-08 01:57:29 +00001262 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001263 for (int i = 10; i <= 30; i += 10) {
1264 EXPECT_EQ(apm_->kNoError,
1265 apm_->voice_detection()->set_frame_size_ms(i));
1266 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1267 }
1268 */
1269
andrew@webrtc.org648af742012-02-08 01:57:29 +00001270 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001271 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1272 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1273 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1274 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1275
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001276 // Test that AudioFrame activity is maintained when VAD is disabled.
1277 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1278 AudioFrame::VADActivity activity[] = {
1279 AudioFrame::kVadActive,
1280 AudioFrame::kVadPassive,
1281 AudioFrame::kVadUnknown
1282 };
pkasting25702cb2016-01-08 13:50:27 -08001283 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001284 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001285 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001286 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001287 }
1288
1289 // Test that AudioFrame activity is set when VAD is enabled.
1290 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001291 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001292 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001293 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001294
niklase@google.com470e71d2011-07-07 08:21:25 +00001295 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1296}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001297
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001298TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001299 AudioProcessing::Config config = apm_->GetConfig();
1300 EXPECT_FALSE(config.echo_canceller.enabled);
1301 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001302 EXPECT_FALSE(config.level_estimation.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001303 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001304 EXPECT_FALSE(apm_->gain_control()->is_enabled());
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001305 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1306 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1307 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1308}
1309
1310TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001311 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001312 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001313 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001314 AudioFrame frame_copy;
1315 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001316 for (int j = 0; j < 1000; j++) {
1317 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1318 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001319 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1320 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001321 }
1322 }
1323}
1324
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001325TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1326 // Test that ProcessStream copies input to output even with no processing.
1327 const size_t kSamples = 80;
1328 const int sample_rate = 8000;
1329 const float src[kSamples] = {
1330 -1.0f, 0.0f, 1.0f
1331 };
1332 float dest[kSamples] = {};
1333
1334 auto src_channels = &src[0];
1335 auto dest_channels = &dest[0];
1336
Ivo Creusen62337e52018-01-09 14:17:33 +01001337 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001338 EXPECT_NOERR(apm_->ProcessStream(
1339 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1340 sample_rate, LayoutFromChannels(1), &dest_channels));
1341
1342 for (size_t i = 0; i < kSamples; ++i) {
1343 EXPECT_EQ(src[i], dest[i]);
1344 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001345
1346 // Same for ProcessReverseStream.
1347 float rev_dest[kSamples] = {};
1348 auto rev_dest_channels = &rev_dest[0];
1349
1350 StreamConfig input_stream = {sample_rate, 1};
1351 StreamConfig output_stream = {sample_rate, 1};
1352 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1353 output_stream, &rev_dest_channels));
1354
1355 for (size_t i = 0; i < kSamples; ++i) {
1356 EXPECT_EQ(src[i], rev_dest[i]);
1357 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001358}
1359
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001360TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1361 EnableAllComponents();
1362
pkasting25702cb2016-01-08 13:50:27 -08001363 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001364 Init(kProcessSampleRates[i],
1365 kProcessSampleRates[i],
1366 kProcessSampleRates[i],
1367 2,
1368 2,
1369 2,
1370 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001371 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001372 ASSERT_EQ(0, feof(far_file_));
1373 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001374 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001375 CopyLeftToRightChannel(revframe_->mutable_data(),
1376 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001377
aluebsb0319552016-03-17 20:39:53 -07001378 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001379
yujo36b1a5f2017-06-12 12:45:32 -07001380 CopyLeftToRightChannel(frame_->mutable_data(),
1381 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001382 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1383
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001384 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001385 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001386 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001387 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001388 analog_level = apm_->gain_control()->stream_analog_level();
1389
yujo36b1a5f2017-06-12 12:45:32 -07001390 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001391 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001392 rewind(far_file_);
1393 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001394 }
1395}
1396
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001397TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001398 // Verify the filter is not active through undistorted audio when:
1399 // 1. No components are enabled...
1400 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001401 AudioFrame frame_copy;
1402 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001403 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1404 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1405 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1406
1407 // 2. Only the level estimator is enabled...
1408 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001409 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001410 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1411 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1412 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1413 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1414 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1415
1416 // 3. Only VAD is enabled...
1417 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001418 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001419 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1420 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1421 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1422 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1423 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1424
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001425 // 4. Only GetStatistics-reporting VAD is enabled...
1426 SetFrameTo(frame_, 1000);
1427 frame_copy.CopyFrom(*frame_);
1428 auto apm_config = apm_->GetConfig();
1429 apm_config.voice_detection.enabled = true;
1430 apm_->ApplyConfig(apm_config);
1431 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1432 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1433 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1434 apm_config.voice_detection.enabled = false;
1435 apm_->ApplyConfig(apm_config);
1436
1437 // 5. Both VADs and the level estimator are enabled...
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001438 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001439 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001440 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1441 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001442 apm_config.voice_detection.enabled = true;
1443 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001444 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1445 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1446 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1447 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1448 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001449 apm_config.voice_detection.enabled = false;
1450 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001451
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001452 // Check the test is valid. We should have distortion from the filter
1453 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001454 apm_config.echo_canceller.enabled = true;
Per Ã…hgren200feba2019-03-06 04:16:46 +01001455 // TODO(peah): Update tests to instead use AEC3.
1456 apm_config.echo_canceller.use_legacy_aec = true;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001457 apm_config.echo_canceller.mobile_mode = false;
1458 apm_->ApplyConfig(apm_config);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001459 frame_->samples_per_channel_ = 320;
1460 frame_->num_channels_ = 2;
1461 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001462 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001463 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001464 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001465 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1466 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1467}
1468
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001469#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1470void ApmTest::ProcessDebugDump(const std::string& in_filename,
1471 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001472 Format format,
1473 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001474 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001475 FILE* in_file = fopen(in_filename.c_str(), "rb");
1476 ASSERT_TRUE(in_file != NULL);
1477 audioproc::Event event_msg;
1478 bool first_init = true;
1479
1480 while (ReadMessageFromFile(in_file, &event_msg)) {
1481 if (event_msg.type() == audioproc::Event::INIT) {
1482 const audioproc::Init msg = event_msg.init();
1483 int reverse_sample_rate = msg.sample_rate();
1484 if (msg.has_reverse_sample_rate()) {
1485 reverse_sample_rate = msg.reverse_sample_rate();
1486 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001487 int output_sample_rate = msg.sample_rate();
1488 if (msg.has_output_sample_rate()) {
1489 output_sample_rate = msg.output_sample_rate();
1490 }
1491
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001492 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001493 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001494 reverse_sample_rate,
1495 msg.num_input_channels(),
1496 msg.num_output_channels(),
1497 msg.num_reverse_channels(),
1498 false);
1499 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001500 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001501 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001502 auto aec_dump =
1503 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1504 EXPECT_TRUE(aec_dump);
1505 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001506 first_init = false;
1507 }
1508
1509 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1510 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1511
1512 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001513 ASSERT_EQ(revframe_->num_channels_,
1514 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001515 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001516 memcpy(revfloat_cb_->channels()[i],
1517 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001518 msg.channel(i).size());
1519 }
1520 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001521 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001522 if (format == kFloatFormat) {
1523 // We're using an int16 input file; convert to float.
1524 ConvertToFloat(*revframe_, revfloat_cb_.get());
1525 }
1526 }
1527 AnalyzeReverseStreamChooser(format);
1528
1529 } else if (event_msg.type() == audioproc::Event::STREAM) {
1530 const audioproc::Stream msg = event_msg.stream();
1531 // ProcessStream could have changed this for the output frame.
1532 frame_->num_channels_ = apm_->num_input_channels();
1533
1534 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1535 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001536 if (msg.has_keypress()) {
1537 apm_->set_stream_key_pressed(msg.keypress());
1538 } else {
1539 apm_->set_stream_key_pressed(true);
1540 }
1541
1542 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001543 ASSERT_EQ(frame_->num_channels_,
1544 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001545 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001546 memcpy(float_cb_->channels()[i],
1547 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001548 msg.input_channel(i).size());
1549 }
1550 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001551 memcpy(frame_->mutable_data(), msg.input_data().data(),
1552 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001553 if (format == kFloatFormat) {
1554 // We're using an int16 input file; convert to float.
1555 ConvertToFloat(*frame_, float_cb_.get());
1556 }
1557 }
1558 ProcessStreamChooser(format);
1559 }
1560 }
aleloif4dd1912017-06-15 01:55:38 -07001561 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001562 fclose(in_file);
1563}
1564
1565void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001566 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001567 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001568 std::string format_string;
1569 switch (format) {
1570 case kIntFormat:
1571 format_string = "_int";
1572 break;
1573 case kFloatFormat:
1574 format_string = "_float";
1575 break;
1576 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001577 const std::string ref_filename = test::TempFilename(
1578 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1579 const std::string out_filename = test::TempFilename(
1580 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001581 const std::string limited_filename = test::TempFilename(
1582 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1583 const size_t logging_limit_bytes = 100000;
1584 // We expect at least this many bytes in the created logfile.
1585 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001586 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001587 ProcessDebugDump(in_filename, ref_filename, format, -1);
1588 ProcessDebugDump(ref_filename, out_filename, format, -1);
1589 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001590
1591 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1592 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001593 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001594 ASSERT_TRUE(ref_file != NULL);
1595 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001596 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001597 std::unique_ptr<uint8_t[]> ref_bytes;
1598 std::unique_ptr<uint8_t[]> out_bytes;
1599 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001600
1601 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1602 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001603 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001604 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001605 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001606 while (ref_size > 0 && out_size > 0) {
1607 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001608 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001609 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001610 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001611 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001612 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001613 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1614 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001615 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001616 }
1617 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001618 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1619 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001620 EXPECT_NE(0, feof(ref_file));
1621 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001622 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001623 ASSERT_EQ(0, fclose(ref_file));
1624 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001625 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001626 remove(ref_filename.c_str());
1627 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001628 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001629}
1630
pbosc7a65692016-05-06 12:50:04 -07001631TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001632 VerifyDebugDumpTest(kIntFormat);
1633}
1634
pbosc7a65692016-05-06 12:50:04 -07001635TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001636 VerifyDebugDumpTest(kFloatFormat);
1637}
1638#endif
1639
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001640// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001641TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001642 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001643 const std::string filename =
1644 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001645 {
1646 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1647 EXPECT_FALSE(aec_dump);
1648 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001649
1650#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1651 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001652 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001653
aleloif4dd1912017-06-15 01:55:38 -07001654 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1655 EXPECT_TRUE(aec_dump);
1656 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001658 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001659 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001660
1661 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001662 FILE* fid = fopen(filename.c_str(), "r");
1663 ASSERT_TRUE(fid != NULL);
1664
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001665 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001666 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001667 ASSERT_EQ(0, remove(filename.c_str()));
1668#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001669 // Verify the file has NOT been written.
1670 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1671#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1672}
1673
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001674// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001675TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001676 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001677
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001678 const std::string filename =
1679 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001680 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001681 ASSERT_TRUE(fid);
1682
1683#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1684 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001685 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001686
aleloif4dd1912017-06-15 01:55:38 -07001687 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1688 EXPECT_TRUE(aec_dump);
1689 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001690 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001691 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001692 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001693
1694 // Verify the file has been written.
1695 fid = fopen(filename.c_str(), "r");
1696 ASSERT_TRUE(fid != NULL);
1697
1698 // Clean it up.
1699 ASSERT_EQ(0, fclose(fid));
1700 ASSERT_EQ(0, remove(filename.c_str()));
1701#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001702 ASSERT_EQ(0, fclose(fid));
1703#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1704}
1705
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001706TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001707 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001708 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001709
1710 Config config;
1711 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001712 std::unique_ptr<AudioProcessing> fapm(
1713 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001714 EnableAllComponents();
1715 EnableAllAPComponents(fapm.get());
1716 for (int i = 0; i < ref_data.test_size(); i++) {
1717 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1718
1719 audioproc::Test* test = ref_data.mutable_test(i);
1720 // TODO(ajm): Restore downmixing test cases.
1721 if (test->num_input_channels() != test->num_output_channels())
1722 continue;
1723
Peter Kasting69558702016-01-12 16:26:35 -08001724 const size_t num_render_channels =
1725 static_cast<size_t>(test->num_reverse_channels());
1726 const size_t num_input_channels =
1727 static_cast<size_t>(test->num_input_channels());
1728 const size_t num_output_channels =
1729 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001730 const size_t samples_per_channel = static_cast<size_t>(
1731 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001732
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001733 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1734 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001735 Init(fapm.get());
1736
1737 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001738 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1739 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001740
1741 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001742 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001743 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1744 ReadFrame(near_file_, frame_, float_cb_.get())) {
1745 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1746
aluebsb0319552016-03-17 20:39:53 -07001747 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001748 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1749 revfloat_cb_->channels(),
1750 samples_per_channel,
1751 test->sample_rate(),
1752 LayoutFromChannels(num_render_channels)));
1753
1754 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1755 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001756 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1757 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1758
1759 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001760 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001761 output_int16.channels());
1762
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001763 EXPECT_NOERR(fapm->ProcessStream(
1764 float_cb_->channels(),
1765 samples_per_channel,
1766 test->sample_rate(),
1767 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001768 test->sample_rate(),
1769 LayoutFromChannels(num_output_channels),
1770 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001771 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001772 FloatToS16(float_cb_->channels()[j],
1773 samples_per_channel,
1774 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001775 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001776 float snr = ComputeSNR(output_int16.channels()[j],
1777 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001778 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001779
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001780 const float kVarianceThreshold = 20;
1781 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001782
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001783 // Skip frames with low energy.
Oleh Prypin708eccc2019-03-27 09:38:52 +01001784 if (std::sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
aluebs776593b2016-03-15 14:04:58 -07001785 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001786 }
1787 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001788
1789 analog_level = fapm->gain_control()->stream_analog_level();
1790 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1791 fapm->gain_control()->stream_analog_level());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001792 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1793 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001794 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001795
1796 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001797 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001798 }
aluebs776593b2016-03-15 14:04:58 -07001799
1800#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1801 const size_t kMaxNumBadChunks = 0;
1802#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1803 // There are a few chunks in the fixed-point profile that give low SNR.
1804 // Listening confirmed the difference is acceptable.
1805 const size_t kMaxNumBadChunks = 60;
1806#endif
1807 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1808
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001809 rewind(far_file_);
1810 rewind(near_file_);
1811 }
1812}
1813
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001814// TODO(andrew): Add a test to process a few frames with different combinations
1815// of enabled components.
1816
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001817TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001818 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001819 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001820
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001821 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001822 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001823 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001824 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001825 for (size_t i = 0; i < arraysize(kChannels); i++) {
1826 for (size_t j = 0; j < arraysize(kChannels); j++) {
1827 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001828 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001829 test->set_num_reverse_channels(kChannels[i]);
1830 test->set_num_input_channels(kChannels[j]);
1831 test->set_num_output_channels(kChannels[j]);
1832 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001833 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001834 }
1835 }
1836 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001837#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1838 // To test the extended filter mode.
1839 audioproc::Test* test = ref_data.add_test();
1840 test->set_num_reverse_channels(2);
1841 test->set_num_input_channels(2);
1842 test->set_num_output_channels(2);
1843 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1844 test->set_use_aec_extended_filter(true);
1845#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001846 }
1847
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001848 for (int i = 0; i < ref_data.test_size(); i++) {
1849 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001850
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001851 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001852 // TODO(ajm): We no longer allow different input and output channels. Skip
1853 // these tests for now, but they should be removed from the set.
1854 if (test->num_input_channels() != test->num_output_channels())
1855 continue;
1856
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001857 Config config;
1858 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001859 config.Set<ExtendedFilter>(
1860 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001861 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001862
1863 EnableAllComponents();
1864
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001865 Init(test->sample_rate(),
1866 test->sample_rate(),
1867 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001868 static_cast<size_t>(test->num_input_channels()),
1869 static_cast<size_t>(test->num_output_channels()),
1870 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001871 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001872
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001873 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001874 int has_voice_count = 0;
1875 int is_saturated_count = 0;
1876 int analog_level = 127;
1877 int analog_level_average = 0;
1878 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001879 float ns_speech_prob_average = 0.0f;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001880 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001881#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1882 int stats_index = 0;
1883#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001884
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001885 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001886 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001887
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001888 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1889
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001890 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001891 EXPECT_EQ(apm_->kNoError,
1892 apm_->gain_control()->set_stream_analog_level(analog_level));
1893
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001894 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001895
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001896 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001897 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1898 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001899
1900 max_output_average += MaxAudioFrame(*frame_);
1901
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001902 analog_level = apm_->gain_control()->stream_analog_level();
1903 analog_level_average += analog_level;
1904 if (apm_->gain_control()->stream_is_saturated()) {
1905 is_saturated_count++;
1906 }
1907 if (apm_->voice_detection()->stream_has_voice()) {
1908 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001909 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001910 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001911 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001912 }
1913
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001914 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
Sam Zackrisson11b87032018-12-18 17:13:58 +01001915 AudioProcessingStats stats =
1916 apm_->GetStatistics(/*has_remote_tracks=*/false);
1917 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001918
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001919 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001920 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001921 sizeof(int16_t),
1922 frame_size,
1923 out_file_);
1924 ASSERT_EQ(frame_size, write_count);
1925
1926 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001927 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001928 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001929
1930#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1931 const int kStatsAggregationFrameNum = 100; // 1 second.
1932 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001933 // Get echo and delay metrics.
1934 AudioProcessingStats stats =
1935 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001936
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001937 // Echo metrics.
1938 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1939 const float echo_return_loss_enhancement =
1940 stats.echo_return_loss_enhancement.value_or(-1.0f);
1941 const float divergent_filter_fraction =
1942 stats.divergent_filter_fraction.value_or(-1.0f);
1943 const float residual_echo_likelihood =
1944 stats.residual_echo_likelihood.value_or(-1.0f);
1945 const float residual_echo_likelihood_recent_max =
1946 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1947
1948 // Delay metrics.
1949 const int32_t delay_median_ms = stats.delay_median_ms.value_or(-1.0);
1950 const int32_t delay_standard_deviation_ms =
1951 stats.delay_standard_deviation_ms.value_or(-1.0);
minyue58530ed2016-05-24 05:50:12 -07001952
minyue58530ed2016-05-24 05:50:12 -07001953 if (!write_ref_data) {
1954 const audioproc::Test::EchoMetrics& reference =
1955 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001956 constexpr float kEpsilon = 0.01;
1957 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1958 EXPECT_NEAR(echo_return_loss_enhancement,
1959 reference.echo_return_loss_enhancement(), kEpsilon);
1960 EXPECT_NEAR(divergent_filter_fraction,
1961 reference.divergent_filter_fraction(), kEpsilon);
1962 EXPECT_NEAR(residual_echo_likelihood,
1963 reference.residual_echo_likelihood(), kEpsilon);
1964 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1965 reference.residual_echo_likelihood_recent_max(),
1966 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001967
1968 const audioproc::Test::DelayMetrics& reference_delay =
1969 test->delay_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001970 EXPECT_EQ(reference_delay.median(), delay_median_ms);
1971 EXPECT_EQ(reference_delay.std(), delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07001972
minyue58530ed2016-05-24 05:50:12 -07001973 ++stats_index;
1974 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001975 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1976 message_echo->set_echo_return_loss(echo_return_loss);
1977 message_echo->set_echo_return_loss_enhancement(
1978 echo_return_loss_enhancement);
1979 message_echo->set_divergent_filter_fraction(
1980 divergent_filter_fraction);
1981 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1982 message_echo->set_residual_echo_likelihood_recent_max(
1983 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001984 audioproc::Test::DelayMetrics* message_delay =
1985 test->add_delay_metrics();
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001986 message_delay->set_median(delay_median_ms);
1987 message_delay->set_std(delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07001988 }
1989 }
1990#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001991 }
1992 max_output_average /= frame_count;
1993 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001994 ns_speech_prob_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001995 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001996
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001997 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001998 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001999 // When running the test on a N7 we get a {2, 6} difference of
2000 // |has_voice_count| and |max_output_average| is up to 18 higher.
2001 // All numbers being consistently higher on N7 compare to ref_data.
2002 // TODO(bjornv): If we start getting more of these offsets on Android we
2003 // should consider a different approach. Either using one slack for all,
2004 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02002005#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002006 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002007 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002008 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002009 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002010#else
2011 const int kHasVoiceCountOffset = 0;
2012 const int kHasVoiceCountNear = kIntNear;
2013 const int kMaxOutputAverageOffset = 0;
2014 const int kMaxOutputAverageNear = kIntNear;
2015#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002016 EXPECT_NEAR(test->has_voice_count(),
2017 has_voice_count - kHasVoiceCountOffset,
2018 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002019 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002020
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002021 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002022 EXPECT_NEAR(test->max_output_average(),
2023 max_output_average - kMaxOutputAverageOffset,
2024 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002025#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002026 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002027 EXPECT_NEAR(test->ns_speech_probability_average(),
2028 ns_speech_prob_average,
2029 kFloatNear);
Sam Zackrisson11b87032018-12-18 17:13:58 +01002030 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002031#endif
2032 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002033 test->set_has_voice_count(has_voice_count);
2034 test->set_is_saturated_count(is_saturated_count);
2035
2036 test->set_analog_level_average(analog_level_average);
2037 test->set_max_output_average(max_output_average);
2038
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002039#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002040 EXPECT_LE(0.0f, ns_speech_prob_average);
2041 EXPECT_GE(1.0f, ns_speech_prob_average);
2042 test->set_ns_speech_probability_average(ns_speech_prob_average);
Sam Zackrisson11b87032018-12-18 17:13:58 +01002043 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002044#endif
2045 }
2046
2047 rewind(far_file_);
2048 rewind(near_file_);
2049 }
2050
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002051 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002052 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002053 }
2054}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002055
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002056TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2057 struct ChannelFormat {
2058 AudioProcessing::ChannelLayout in_layout;
2059 AudioProcessing::ChannelLayout out_layout;
2060 };
2061 ChannelFormat cf[] = {
2062 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2063 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2064 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2065 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002066
Ivo Creusen62337e52018-01-09 14:17:33 +01002067 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002068 // Enable one component just to ensure some processing takes place.
2069 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002070 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002071 const int in_rate = 44100;
2072 const int out_rate = 48000;
2073 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2074 TotalChannelsFromLayout(cf[i].in_layout));
2075 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2076 ChannelsFromLayout(cf[i].out_layout));
2077
2078 // Run over a few chunks.
2079 for (int j = 0; j < 10; ++j) {
2080 EXPECT_NOERR(ap->ProcessStream(
2081 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002082 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002083 in_rate,
2084 cf[i].in_layout,
2085 out_rate,
2086 cf[i].out_layout,
2087 out_cb.channels()));
2088 }
2089 }
2090}
2091
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002092// Compares the reference and test arrays over a region around the expected
2093// delay. Finds the highest SNR in that region and adds the variance and squared
2094// error results to the supplied accumulators.
2095void UpdateBestSNR(const float* ref,
2096 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002097 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002098 int expected_delay,
2099 double* variance_acc,
2100 double* sq_error_acc) {
2101 double best_snr = std::numeric_limits<double>::min();
2102 double best_variance = 0;
2103 double best_sq_error = 0;
2104 // Search over a region of eight samples around the expected delay.
2105 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2106 ++delay) {
2107 double sq_error = 0;
2108 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002109 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002110 double error = test[i + delay] - ref[i];
2111 sq_error += error * error;
2112 variance += ref[i] * ref[i];
2113 }
2114
2115 if (sq_error == 0) {
2116 *variance_acc += variance;
2117 return;
2118 }
2119 double snr = variance / sq_error;
2120 if (snr > best_snr) {
2121 best_snr = snr;
2122 best_variance = variance;
2123 best_sq_error = sq_error;
2124 }
2125 }
2126
2127 *variance_acc += best_variance;
2128 *sq_error_acc += best_sq_error;
2129}
2130
2131// Used to test a multitude of sample rate and channel combinations. It works
2132// by first producing a set of reference files (in SetUpTestCase) that are
2133// assumed to be correct, as the used parameters are verified by other tests
2134// in this collection. Primarily the reference files are all produced at
2135// "native" rates which do not involve any resampling.
2136
2137// Each test pass produces an output file with a particular format. The output
2138// is matched against the reference file closest to its internal processing
2139// format. If necessary the output is resampled back to its process format.
2140// Due to the resampling distortion, we don't expect identical results, but
2141// enforce SNR thresholds which vary depending on the format. 0 is a special
2142// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002143typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002144class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002145 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002146 public:
2147 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002148 : input_rate_(std::get<0>(GetParam())),
2149 output_rate_(std::get<1>(GetParam())),
2150 reverse_input_rate_(std::get<2>(GetParam())),
2151 reverse_output_rate_(std::get<3>(GetParam())),
2152 expected_snr_(std::get<4>(GetParam())),
2153 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002154
2155 virtual ~AudioProcessingTest() {}
2156
2157 static void SetUpTestCase() {
2158 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002159 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002160 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002161 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2162 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2163 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002164 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002165 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2166 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2167 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002168 }
2169 }
2170 }
2171 }
2172
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002173 void TearDown() {
2174 // Remove "out" files after each test.
2175 ClearTempOutFiles();
2176 }
2177
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002178 static void TearDownTestCase() {
2179 ClearTempFiles();
2180 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002181
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002182 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002183 // to a file specified with |output_file_prefix|. Both forward and reverse
2184 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002185 static void ProcessFormat(int input_rate,
2186 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002187 int reverse_input_rate,
2188 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002189 size_t num_input_channels,
2190 size_t num_output_channels,
2191 size_t num_reverse_input_channels,
2192 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002193 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002194 Config config;
2195 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002196 std::unique_ptr<AudioProcessing> ap(
2197 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002198 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002199
ekmeyerson60d9b332015-08-14 10:35:55 -07002200 ProcessingConfig processing_config = {
2201 {{input_rate, num_input_channels},
2202 {output_rate, num_output_channels},
2203 {reverse_input_rate, num_reverse_input_channels},
2204 {reverse_output_rate, num_reverse_output_channels}}};
2205 ap->Initialize(processing_config);
2206
2207 FILE* far_file =
2208 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002209 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002210 FILE* out_file =
2211 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2212 reverse_input_rate, reverse_output_rate,
2213 num_input_channels, num_output_channels,
2214 num_reverse_input_channels,
2215 num_reverse_output_channels, kForward).c_str(),
2216 "wb");
2217 FILE* rev_out_file =
2218 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2219 reverse_input_rate, reverse_output_rate,
2220 num_input_channels, num_output_channels,
2221 num_reverse_input_channels,
2222 num_reverse_output_channels, kReverse).c_str(),
2223 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002224 ASSERT_TRUE(far_file != NULL);
2225 ASSERT_TRUE(near_file != NULL);
2226 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002227 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002228
2229 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2230 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002231 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2232 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002233 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2234 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002235 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2236 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002237
2238 // Temporary buffers.
2239 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002240 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2241 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002242 std::unique_ptr<float[]> float_data(new float[max_length]);
2243 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002244
2245 int analog_level = 127;
2246 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2247 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002248 EXPECT_NOERR(ap->ProcessReverseStream(
2249 rev_cb.channels(), processing_config.reverse_input_stream(),
2250 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002251
2252 EXPECT_NOERR(ap->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002253 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2254
2255 EXPECT_NOERR(ap->ProcessStream(
2256 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002257 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002258 input_rate,
2259 LayoutFromChannels(num_input_channels),
2260 output_rate,
2261 LayoutFromChannels(num_output_channels),
2262 out_cb.channels()));
2263
ekmeyerson60d9b332015-08-14 10:35:55 -07002264 // Dump forward output to file.
2265 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002266 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002267 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002268
pkasting25702cb2016-01-08 13:50:27 -08002269 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002270 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002271 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002272
ekmeyerson60d9b332015-08-14 10:35:55 -07002273 // Dump reverse output to file.
2274 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2275 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002276 size_t rev_out_length =
2277 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002278
pkasting25702cb2016-01-08 13:50:27 -08002279 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002280 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2281 rev_out_file));
2282
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002283 analog_level = ap->gain_control()->stream_analog_level();
2284 }
2285 fclose(far_file);
2286 fclose(near_file);
2287 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002288 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002289 }
2290
2291 protected:
2292 int input_rate_;
2293 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002294 int reverse_input_rate_;
2295 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002296 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002297 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002298};
2299
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002300TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002301 struct ChannelFormat {
2302 int num_input;
2303 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002304 int num_reverse_input;
2305 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002306 };
2307 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002308 {1, 1, 1, 1},
2309 {1, 1, 2, 1},
2310 {2, 1, 1, 1},
2311 {2, 1, 2, 1},
2312 {2, 2, 1, 1},
2313 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002314 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002315
pkasting25702cb2016-01-08 13:50:27 -08002316 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002317 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2318 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2319 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002320
ekmeyerson60d9b332015-08-14 10:35:55 -07002321 // Verify output for both directions.
2322 std::vector<StreamDirection> stream_directions;
2323 stream_directions.push_back(kForward);
2324 stream_directions.push_back(kReverse);
2325 for (StreamDirection file_direction : stream_directions) {
2326 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2327 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2328 const int out_num =
2329 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2330 const double expected_snr =
2331 file_direction ? expected_reverse_snr_ : expected_snr_;
2332
2333 const int min_ref_rate = std::min(in_rate, out_rate);
2334 int ref_rate;
2335
2336 if (min_ref_rate > 32000) {
2337 ref_rate = 48000;
2338 } else if (min_ref_rate > 16000) {
2339 ref_rate = 32000;
2340 } else if (min_ref_rate > 8000) {
2341 ref_rate = 16000;
2342 } else {
2343 ref_rate = 8000;
2344 }
aluebs776593b2016-03-15 14:04:58 -07002345#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002346 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002347 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002348 }
2349#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002350 FILE* out_file = fopen(
2351 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2352 reverse_output_rate_, cf[i].num_input,
2353 cf[i].num_output, cf[i].num_reverse_input,
2354 cf[i].num_reverse_output, file_direction).c_str(),
2355 "rb");
2356 // The reference files always have matching input and output channels.
2357 FILE* ref_file = fopen(
2358 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2359 cf[i].num_output, cf[i].num_output,
2360 cf[i].num_reverse_output, cf[i].num_reverse_output,
2361 file_direction).c_str(),
2362 "rb");
2363 ASSERT_TRUE(out_file != NULL);
2364 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002365
pkasting25702cb2016-01-08 13:50:27 -08002366 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2367 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002368 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002369 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002370 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002371 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002372 // Data from the resampled output, in case the reference and output rates
2373 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002374 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002375
ekmeyerson60d9b332015-08-14 10:35:55 -07002376 PushResampler<float> resampler;
2377 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002378
ekmeyerson60d9b332015-08-14 10:35:55 -07002379 // Compute the resampling delay of the output relative to the reference,
2380 // to find the region over which we should search for the best SNR.
2381 float expected_delay_sec = 0;
2382 if (in_rate != ref_rate) {
2383 // Input resampling delay.
2384 expected_delay_sec +=
2385 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2386 }
2387 if (out_rate != ref_rate) {
2388 // Output resampling delay.
2389 expected_delay_sec +=
2390 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2391 // Delay of converting the output back to its processing rate for
2392 // testing.
2393 expected_delay_sec +=
2394 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2395 }
2396 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002397 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002398
ekmeyerson60d9b332015-08-14 10:35:55 -07002399 double variance = 0;
2400 double sq_error = 0;
2401 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2402 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2403 float* out_ptr = out_data.get();
2404 if (out_rate != ref_rate) {
2405 // Resample the output back to its internal processing rate if
2406 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002407 ASSERT_EQ(ref_length,
2408 static_cast<size_t>(resampler.Resample(
2409 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002410 out_ptr = cmp_data.get();
2411 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002412
ekmeyerson60d9b332015-08-14 10:35:55 -07002413 // Update the |sq_error| and |variance| accumulators with the highest
2414 // SNR of reference vs output.
2415 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2416 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002417 }
2418
ekmeyerson60d9b332015-08-14 10:35:55 -07002419 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2420 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2421 << cf[i].num_input << ", " << cf[i].num_output << ", "
2422 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2423 << ", " << file_direction << "): ";
2424 if (sq_error > 0) {
2425 double snr = 10 * log10(variance / sq_error);
2426 EXPECT_GE(snr, expected_snr);
2427 EXPECT_NE(0, expected_snr);
2428 std::cout << "SNR=" << snr << " dB" << std::endl;
2429 } else {
aluebs776593b2016-03-15 14:04:58 -07002430 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002431 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432
ekmeyerson60d9b332015-08-14 10:35:55 -07002433 fclose(out_file);
2434 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002435 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002436 }
2437}
2438
2439#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002440INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002441 CommonFormats,
2442 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002443 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2444 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2445 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2446 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2447 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2448 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2449 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2450 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2451 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2452 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2453 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2454 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002455
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002456 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2457 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2458 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2459 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2460 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2461 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2462 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2463 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2464 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2465 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2466 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2467 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002468
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002469 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2470 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
2471 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2472 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2473 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2474 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2475 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2476 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2477 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2478 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2479 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2480 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002481
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002482 std::make_tuple(16000, 48000, 48000, 48000, 24, 0),
2483 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2484 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
2485 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2486 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2487 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2488 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2489 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2490 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2491 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2492 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2493 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002494
2495#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002496INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002497 CommonFormats,
2498 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002499 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2500 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2501 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2502 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2503 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2504 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2505 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2506 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2507 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2508 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2509 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2510 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002511
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002512 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2513 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2514 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2515 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2516 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2517 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2518 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2519 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2520 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2521 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2522 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2523 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002524
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002525 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2526 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2527 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2528 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2529 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2530 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2531 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2532 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2533 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2534 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2535 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2536 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002537
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002538 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2539 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2540 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2541 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2542 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2543 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2544 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2545 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2546 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2547 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2548 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2549 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002550#endif
2551
niklase@google.com470e71d2011-07-07 08:21:25 +00002552} // namespace
peahc19f3122016-10-07 14:54:10 -07002553
Alessio Bazzicac054e782018-04-16 12:10:09 +02002554TEST(RuntimeSettingTest, TestDefaultCtor) {
2555 auto s = AudioProcessing::RuntimeSetting();
2556 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2557}
2558
2559TEST(RuntimeSettingTest, TestCapturePreGain) {
2560 using Type = AudioProcessing::RuntimeSetting::Type;
2561 {
2562 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2563 EXPECT_EQ(Type::kCapturePreGain, s.type());
2564 float v;
2565 s.GetFloat(&v);
2566 EXPECT_EQ(1.25f, v);
2567 }
2568
2569#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2570 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2571#endif
2572}
2573
2574TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2575 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2576 auto s = AudioProcessing::RuntimeSetting();
2577 ASSERT_TRUE(q.Insert(&s));
2578 ASSERT_TRUE(q.Remove(&s));
2579 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2580}
2581
Sam Zackrisson0beac582017-09-25 12:04:02 +02002582TEST(ApmConfiguration, EnablePostProcessing) {
2583 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002584 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002585 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002586 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002587 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002588 rtc::scoped_refptr<AudioProcessing> apm =
2589 AudioProcessingBuilder()
2590 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002591 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002592
2593 AudioFrame audio;
2594 audio.num_channels_ = 1;
2595 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2596
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002597 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002598 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002599}
2600
Alex Loiko5825aa62017-12-18 16:02:40 +01002601TEST(ApmConfiguration, EnablePreProcessing) {
2602 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002603 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002604 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002605 auto mock_pre_processor =
2606 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002607 rtc::scoped_refptr<AudioProcessing> apm =
2608 AudioProcessingBuilder()
2609 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002610 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002611
2612 AudioFrame audio;
2613 audio.num_channels_ = 1;
2614 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2615
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002616 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002617 apm->ProcessReverseStream(&audio);
2618}
2619
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002620TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2621 // Verify that apm uses a capture analyzer if one is provided.
2622 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002623 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002624 auto mock_capture_analyzer =
2625 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2626 rtc::scoped_refptr<AudioProcessing> apm =
2627 AudioProcessingBuilder()
2628 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2629 .Create();
2630
2631 AudioFrame audio;
2632 audio.num_channels_ = 1;
2633 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2634
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002635 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002636 apm->ProcessStream(&audio);
2637}
2638
Alex Loiko73ec0192018-05-15 10:52:28 +02002639TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2640 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002641 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002642 auto mock_pre_processor =
2643 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2644 rtc::scoped_refptr<AudioProcessing> apm =
2645 AudioProcessingBuilder()
2646 .SetRenderPreProcessing(std::move(mock_pre_processor))
2647 .Create();
2648 apm->SetRuntimeSetting(
2649 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2650
2651 // RuntimeSettings forwarded during 'Process*Stream' calls.
2652 // Therefore we have to make one such call.
2653 AudioFrame audio;
2654 audio.num_channels_ = 1;
2655 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2656
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002657 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2658 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002659 apm->ProcessReverseStream(&audio);
2660}
2661
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002662class MyEchoControlFactory : public EchoControlFactory {
2663 public:
2664 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2665 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002666 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2667 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
2668 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_)).Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002669 return std::unique_ptr<EchoControl>(ec);
2670 }
2671};
2672
2673TEST(ApmConfiguration, EchoControlInjection) {
2674 // Verify that apm uses an injected echo controller if one is provided.
2675 webrtc::Config webrtc_config;
2676 std::unique_ptr<EchoControlFactory> echo_control_factory(
2677 new MyEchoControlFactory());
2678
Alex Loiko5825aa62017-12-18 16:02:40 +01002679 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002680 AudioProcessingBuilder()
2681 .SetEchoControlFactory(std::move(echo_control_factory))
2682 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002683
2684 AudioFrame audio;
2685 audio.num_channels_ = 1;
2686 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2687 apm->ProcessStream(&audio);
2688 apm->ProcessReverseStream(&audio);
2689 apm->ProcessStream(&audio);
2690}
Ivo Creusenae026092017-11-20 13:07:16 +01002691
2692std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2693 Config old_config;
2694 if (use_AEC2) {
2695 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2696 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2697 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002698 std::unique_ptr<AudioProcessing> apm(
2699 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002700 if (!apm) {
2701 return apm;
2702 }
2703
2704 ProcessingConfig processing_config = {
2705 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2706
2707 if (apm->Initialize(processing_config) != 0) {
2708 return nullptr;
2709 }
2710
2711 // Disable all components except for an AEC and the residual echo detector.
Per Ã…hgren200feba2019-03-06 04:16:46 +01002712 // TODO(peah): Update this to also work on AEC3.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002713 AudioProcessing::Config apm_config;
2714 apm_config.residual_echo_detector.enabled = true;
2715 apm_config.high_pass_filter.enabled = false;
2716 apm_config.gain_controller2.enabled = false;
2717 apm_config.echo_canceller.enabled = true;
2718 apm_config.echo_canceller.mobile_mode = !use_AEC2;
Per Ã…hgren200feba2019-03-06 04:16:46 +01002719 apm_config.echo_canceller.use_legacy_aec = use_AEC2;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002720 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002721 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2722 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2723 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2724 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002725 return apm;
2726}
2727
2728#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2729#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2730#else
2731#define MAYBE_ApmStatistics ApmStatistics
2732#endif
2733
2734TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2735 // Set up APM with AEC2 and process some audio.
2736 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2737 ASSERT_TRUE(apm);
Per Ã…hgren200feba2019-03-06 04:16:46 +01002738 AudioProcessing::Config apm_config;
2739 apm_config.echo_canceller.enabled = true;
2740 // TODO(peah): Update tests to instead use AEC3.
2741 apm_config.echo_canceller.use_legacy_aec = true;
2742 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002743
2744 // Set up an audioframe.
2745 AudioFrame frame;
2746 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002747 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002748
2749 // Fill the audio frame with a sawtooth pattern.
2750 int16_t* ptr = frame.mutable_data();
2751 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2752 ptr[i] = 10000 * ((i % 3) - 1);
2753 }
2754
2755 // Do some processing.
2756 for (int i = 0; i < 200; i++) {
2757 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2758 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2759 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2760 }
2761
2762 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002763 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002764 // We expect all statistics to be set and have a sensible value.
2765 ASSERT_TRUE(stats.residual_echo_likelihood);
2766 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2767 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2768 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2769 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2770 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2771 ASSERT_TRUE(stats.echo_return_loss);
2772 EXPECT_NE(*stats.echo_return_loss, -100.0);
2773 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2774 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2775 ASSERT_TRUE(stats.divergent_filter_fraction);
2776 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2777 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2778 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2779 // We don't check stats.delay_median_ms since it takes too long to settle to a
2780 // value. At least 20 seconds of data need to be processed before it will get
2781 // a value, which would make this test take too much time.
2782
2783 // If there are no receive streams, we expect the stats not to be set. The
2784 // 'false' argument signals to APM that no receive streams are currently
2785 // active. In that situation the statistics would get stuck at their last
2786 // calculated value (AEC and echo detection need at least one stream in each
2787 // direction), so to avoid that, they should not be set by APM.
2788 stats = apm->GetStatistics(false);
2789 EXPECT_FALSE(stats.residual_echo_likelihood);
2790 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2791 EXPECT_FALSE(stats.echo_return_loss);
2792 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2793 EXPECT_FALSE(stats.divergent_filter_fraction);
2794 EXPECT_FALSE(stats.delay_median_ms);
2795 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2796}
2797
2798TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2799 // Set up APM with AECM and process some audio.
2800 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2801 ASSERT_TRUE(apm);
2802
2803 // Set up an audioframe.
2804 AudioFrame frame;
2805 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002806 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002807
2808 // Fill the audio frame with a sawtooth pattern.
2809 int16_t* ptr = frame.mutable_data();
2810 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2811 ptr[i] = 10000 * ((i % 3) - 1);
2812 }
2813
2814 // Do some processing.
2815 for (int i = 0; i < 200; i++) {
2816 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2817 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2818 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2819 }
2820
2821 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002822 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002823 // We expect only the residual echo detector statistics to be set and have a
2824 // sensible value.
2825 EXPECT_TRUE(stats.residual_echo_likelihood);
2826 if (stats.residual_echo_likelihood) {
2827 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2828 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2829 }
2830 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2831 if (stats.residual_echo_likelihood_recent_max) {
2832 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2833 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2834 }
2835 EXPECT_FALSE(stats.echo_return_loss);
2836 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2837 EXPECT_FALSE(stats.divergent_filter_fraction);
2838 EXPECT_FALSE(stats.delay_median_ms);
2839 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2840
2841 // If there are no receive streams, we expect the stats not to be set.
2842 stats = apm->GetStatistics(false);
2843 EXPECT_FALSE(stats.residual_echo_likelihood);
2844 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2845 EXPECT_FALSE(stats.echo_return_loss);
2846 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2847 EXPECT_FALSE(stats.divergent_filter_fraction);
2848 EXPECT_FALSE(stats.delay_median_ms);
2849 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2850}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002851
2852TEST(ApmStatistics, ReportOutputRmsDbfs) {
2853 ProcessingConfig processing_config = {
2854 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2855 AudioProcessing::Config config;
2856
2857 // Set up an audioframe.
2858 AudioFrame frame;
2859 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002860 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002861
2862 // Fill the audio frame with a sawtooth pattern.
2863 int16_t* ptr = frame.mutable_data();
2864 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2865 ptr[i] = 10000 * ((i % 3) - 1);
2866 }
2867
2868 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2869 apm->Initialize(processing_config);
2870
2871 // If not enabled, no metric should be reported.
2872 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2873 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2874
2875 // If enabled, metrics should be reported.
2876 config.level_estimation.enabled = true;
2877 apm->ApplyConfig(config);
2878 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2879 auto stats = apm->GetStatistics(false);
2880 EXPECT_TRUE(stats.output_rms_dbfs);
2881 EXPECT_GE(*stats.output_rms_dbfs, 0);
2882
2883 // If re-disabled, the value is again not reported.
2884 config.level_estimation.enabled = false;
2885 apm->ApplyConfig(config);
2886 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2887 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2888}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002889
2890TEST(ApmStatistics, ReportHasVoice) {
2891 ProcessingConfig processing_config = {
2892 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2893 AudioProcessing::Config config;
2894
2895 // Set up an audioframe.
2896 AudioFrame frame;
2897 frame.num_channels_ = 1;
2898 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2899
2900 // Fill the audio frame with a sawtooth pattern.
2901 int16_t* ptr = frame.mutable_data();
2902 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2903 ptr[i] = 10000 * ((i % 3) - 1);
2904 }
2905
2906 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2907 apm->Initialize(processing_config);
2908
2909 // If not enabled, no metric should be reported.
2910 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2911 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2912
2913 // If enabled, metrics should be reported.
2914 config.voice_detection.enabled = true;
2915 apm->ApplyConfig(config);
2916 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2917 auto stats = apm->GetStatistics(false);
2918 EXPECT_TRUE(stats.voice_detected);
2919
2920 // If re-disabled, the value is again not reported.
2921 config.voice_detection.enabled = false;
2922 apm->ApplyConfig(config);
2923 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2924 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2925}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002926} // namespace webrtc