blob: d1e5801ca5bd3bab1460abeb65bce7f41090e5b6 [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>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080015#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000016#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/include/push_resampler.h"
20#include "common_audio/resampler/push_sinc_resampler.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
22#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
23#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/common.h"
25#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020026#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/test/protobuf_utils.h"
28#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/arraysize.h"
30#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/gtest_prod_util.h"
33#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010034#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020038#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020039#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020040#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010041#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070045
46RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000048#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000049#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000051#endif
kwiberg77eab702016-09-28 17:42:01 -070052RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000053
andrew@webrtc.org27c69802014-02-18 20:24:56 +000054namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000055namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000056
ekmeyerson60d9b332015-08-14 10:35:55 -070057// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
58// applicable.
59
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000060// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000061// re-implemented; see comment at the bottom of this file. For now, the user has
62// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000063// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000064// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000065// be set to true with the command-line switch --write_ref_data.
66bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070067const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070068const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000069
aluebseb3603b2016-04-20 15:27:58 -070070#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
71// Android doesn't support 48kHz.
72const int kProcessSampleRates[] = {8000, 16000, 32000};
73#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070074const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070075#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000076
ekmeyerson60d9b332015-08-14 10:35:55 -070077enum StreamDirection { kForward = 0, kReverse };
78
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000079void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000080 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 cb->num_channels());
82 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000083 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000084 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080086 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 S16ToFloat(cb_int.channels()[i],
88 cb->num_frames(),
89 cb->channels()[i]);
90 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000091}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000092
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070094 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095}
96
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080098size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000099 switch (layout) {
100 case AudioProcessing::kMono:
101 return 1;
102 case AudioProcessing::kMonoAndKeyboard:
103 case AudioProcessing::kStereo:
104 return 2;
105 case AudioProcessing::kStereoAndKeyboard:
106 return 3;
107 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700108 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800109 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000110}
111
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000112int TruncateToMultipleOf10(int value) {
113 return (value / 10) * 10;
114}
115
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800117 size_t samples_per_channel) {
118 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000119 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000120}
121
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800123 size_t samples_per_channel) {
124 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000125 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
126}
127
pkasting25702cb2016-01-08 13:50:27 -0800128void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
129 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000130 stereo[i * 2 + 1] = stereo[i * 2];
131 }
132}
133
yujo36b1a5f2017-06-12 12:45:32 -0700134void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800135 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000136 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
137 }
138}
139
140void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700141 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
143 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700144 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000145 }
146}
147
148void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800149 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700150 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700152 frame_data[i] = left;
153 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000154 }
155}
156
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000157void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700158 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
160 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700161 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000162 }
163}
164
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000166 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000167 return false;
168 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000169 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
yujo36b1a5f2017-06-12 12:45:32 -0700172 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000173 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000174 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000175 return false;
176 }
177 return true;
178}
179
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000180void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200181 AudioProcessing::Config apm_config = ap->GetConfig();
182 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000183#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200184 apm_config.echo_canceller.mobile_mode = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000185 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
186 EXPECT_NOERR(ap->gain_control()->Enable(true));
187#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Per Ã…hgren200feba2019-03-06 04:16:46 +0100188 // TODO(peah): Update tests to instead use AEC3.
189 apm_config.echo_canceller.use_legacy_aec = true;
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200190 apm_config.echo_canceller.mobile_mode = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200191 apm_config.echo_canceller.legacy_moderate_suppression_level = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000192
193 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
194 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
195 EXPECT_NOERR(ap->gain_control()->Enable(true));
196#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000197
peah8271d042016-11-22 07:24:52 -0800198 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100199 apm_config.level_estimation.enabled = true;
peah8271d042016-11-22 07:24:52 -0800200 ap->ApplyConfig(apm_config);
201
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000202 EXPECT_NOERR(ap->level_estimator()->Enable(true));
203 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
204
205 EXPECT_NOERR(ap->voice_detection()->Enable(true));
206}
207
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000208// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000209template <class T>
210T AbsValue(T a) {
211 return a > 0 ? a: -a;
212}
213
214int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800215 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700216 const int16_t* frame_data = frame.data();
217 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800218 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700219 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000220 }
221
222 return max_data;
223}
224
Alex Loiko890988c2017-08-31 10:25:48 +0200225void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700226 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000227 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000228 ASSERT_TRUE(file != NULL);
229
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100230 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000231 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800232 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000235 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000237 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000238 fclose(file);
239}
240
Alex Loiko890988c2017-08-31 10:25:48 +0200241std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200242 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000243 // Resource files are all stereo.
244 ss << name << sample_rate_hz / 1000 << "_stereo";
245 return test::ResourcePath(ss.str(), "pcm");
246}
247
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000248// Temporary filenames unique to this process. Used to be able to run these
249// tests in parallel as each process needs to be running in isolation they can't
250// have competing filenames.
251std::map<std::string, std::string> temp_filenames;
252
Alex Loiko890988c2017-08-31 10:25:48 +0200253std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000254 int input_rate,
255 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700256 int reverse_input_rate,
257 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800258 size_t num_input_channels,
259 size_t num_output_channels,
260 size_t num_reverse_input_channels,
261 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700262 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200263 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700264 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
265 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000266 if (num_output_channels == 1) {
267 ss << "mono";
268 } else if (num_output_channels == 2) {
269 ss << "stereo";
270 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700271 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000272 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700273 ss << output_rate / 1000;
274 if (num_reverse_output_channels == 1) {
275 ss << "_rmono";
276 } else if (num_reverse_output_channels == 2) {
277 ss << "_rstereo";
278 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700279 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700280 }
281 ss << reverse_output_rate / 1000;
282 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000283
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000284 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700285 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000286 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
287 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000288}
289
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000290void ClearTempFiles() {
291 for (auto& kv : temp_filenames)
292 remove(kv.second.c_str());
293}
294
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200295// Only remove "out" files. Keep "ref" files.
296void ClearTempOutFiles() {
297 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
298 const std::string& filename = it->first;
299 if (filename.substr(0, 3).compare("out") == 0) {
300 remove(it->second.c_str());
301 temp_filenames.erase(it++);
302 } else {
303 it++;
304 }
305 }
306}
307
Alex Loiko890988c2017-08-31 10:25:48 +0200308void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000309 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000310 ASSERT_TRUE(file != NULL);
311 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000312 fclose(file);
313}
314
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000315// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
316// stereo) file, converts to deinterleaved float (optionally downmixing) and
317// returns the result in |cb|. Returns false if the file ended (or on error) and
318// true otherwise.
319//
320// |int_data| and |float_data| are just temporary space that must be
321// sufficiently large to hold the 10 ms chunk.
322bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
323 ChannelBuffer<float>* cb) {
324 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000325 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000326 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
327 if (read_count != frame_size) {
328 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700329 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000330 return false; // This is expected.
331 }
332
333 S16ToFloat(int_data, frame_size, float_data);
334 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000335 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000336 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000337 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000338 cb->channels());
339 }
340
341 return true;
342}
343
niklase@google.com470e71d2011-07-07 08:21:25 +0000344class ApmTest : public ::testing::Test {
345 protected:
346 ApmTest();
347 virtual void SetUp();
348 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000349
350 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000351 }
352
353 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000354 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000355 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000356
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000357 // Used to select between int and float interface tests.
358 enum Format {
359 kIntFormat,
360 kFloatFormat
361 };
362
363 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000364 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000365 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800366 size_t num_input_channels,
367 size_t num_output_channels,
368 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000369 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000370 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000371 void EnableAllComponents();
372 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000373 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000374 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000375 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
376 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000377 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000378 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
379 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700380 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800381 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700382 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800383 void TestChangingForwardChannels(size_t num_in_channels,
384 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800386 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700387 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000388 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
389 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000390 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000391 int ProcessStreamChooser(Format format);
392 int AnalyzeReverseStreamChooser(Format format);
393 void ProcessDebugDump(const std::string& in_filename,
394 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800395 Format format,
396 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000397 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000398
399 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000400 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800401 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000402 AudioFrame* frame_;
403 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800404 std::unique_ptr<ChannelBuffer<float> > float_cb_;
405 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000406 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800407 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408 FILE* far_file_;
409 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000410 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411};
412
413ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000414 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000415#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800416 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
417 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000418#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000419#if defined(WEBRTC_MAC)
420 // A different file for Mac is needed because on this platform the AEC
421 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800422 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
423 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000424#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800425 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
426 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000427#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000428#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000429 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000430 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000431 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000432 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000433 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000434 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000435 out_file_(NULL) {
436 Config config;
437 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100438 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000439}
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
441void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000442 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
444 frame_ = new AudioFrame();
445 revframe_ = new AudioFrame();
446
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000447 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000448}
449
450void ApmTest::TearDown() {
451 if (frame_) {
452 delete frame_;
453 }
454 frame_ = NULL;
455
456 if (revframe_) {
457 delete revframe_;
458 }
459 revframe_ = NULL;
460
461 if (far_file_) {
462 ASSERT_EQ(0, fclose(far_file_));
463 }
464 far_file_ = NULL;
465
466 if (near_file_) {
467 ASSERT_EQ(0, fclose(near_file_));
468 }
469 near_file_ = NULL;
470
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000471 if (out_file_) {
472 ASSERT_EQ(0, fclose(out_file_));
473 }
474 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000475}
476
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000477void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000478 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700479 ap->Initialize(
480 {{{frame_->sample_rate_hz_, frame_->num_channels_},
481 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700482 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700483 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000484}
485
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000486void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000487 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000488 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800489 size_t num_input_channels,
490 size_t num_output_channels,
491 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000492 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000493 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000494 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000495 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000496
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000497 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
498 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000499 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000500
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000501 if (far_file_) {
502 ASSERT_EQ(0, fclose(far_file_));
503 }
504 std::string filename = ResourceFilePath("far", sample_rate_hz);
505 far_file_ = fopen(filename.c_str(), "rb");
506 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
507 filename << "\n";
508
509 if (near_file_) {
510 ASSERT_EQ(0, fclose(near_file_));
511 }
512 filename = ResourceFilePath("near", sample_rate_hz);
513 near_file_ = fopen(filename.c_str(), "rb");
514 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
515 filename << "\n";
516
517 if (open_output_file) {
518 if (out_file_) {
519 ASSERT_EQ(0, fclose(out_file_));
520 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700521 filename = OutputFilePath(
522 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
523 reverse_sample_rate_hz, num_input_channels, num_output_channels,
524 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000525 out_file_ = fopen(filename.c_str(), "wb");
526 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
527 filename << "\n";
528 }
529}
530
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000531void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000532 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000533}
534
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000535bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
536 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000537 // The files always contain stereo audio.
538 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700539 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000540 sizeof(int16_t),
541 frame_size,
542 file);
543 if (read_count != frame_size) {
544 // Check that the file really ended.
545 EXPECT_NE(0, feof(file));
546 return false; // This is expected.
547 }
548
549 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700550 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000551 frame->samples_per_channel_);
552 }
553
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000555 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000557 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000558}
559
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000560bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
561 return ReadFrame(file, frame, NULL);
562}
563
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000564// If the end of the file has been reached, rewind it and attempt to read the
565// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000566void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
567 ChannelBuffer<float>* cb) {
568 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000569 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000570 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000571 }
572}
573
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000574void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
575 ReadFrameWithRewind(file, frame, NULL);
576}
577
andrew@webrtc.org81865342012-10-27 00:28:27 +0000578void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
579 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000580 EXPECT_EQ(apm_->kNoError,
581 apm_->gain_control()->set_stream_analog_level(127));
582 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000583}
584
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000585int ApmTest::ProcessStreamChooser(Format format) {
586 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587 return apm_->ProcessStream(frame_);
588 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000589 return apm_->ProcessStream(float_cb_->channels(),
590 frame_->samples_per_channel_,
591 frame_->sample_rate_hz_,
592 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000593 output_sample_rate_hz_,
594 LayoutFromChannels(num_output_channels_),
595 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000596}
597
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000598int ApmTest::AnalyzeReverseStreamChooser(Format format) {
599 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700600 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000601 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000602 return apm_->AnalyzeReverseStream(
603 revfloat_cb_->channels(),
604 revframe_->samples_per_channel_,
605 revframe_->sample_rate_hz_,
606 LayoutFromChannels(revframe_->num_channels_));
607}
608
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000609void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
610 int delay_min, int delay_max) {
611 // The |revframe_| and |frame_| should include the proper frame information,
612 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000613 AudioFrame tmp_frame;
614 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000615 bool causal = true;
616
617 tmp_frame.CopyFrom(*revframe_);
618 SetFrameTo(&tmp_frame, 0);
619
620 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
621 // Initialize the |frame_queue| with empty frames.
622 int frame_delay = delay_ms / 10;
623 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000624 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000625 frame->CopyFrom(tmp_frame);
626 frame_queue.push(frame);
627 frame_delay++;
628 causal = false;
629 }
630 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000631 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000632 frame->CopyFrom(tmp_frame);
633 frame_queue.push(frame);
634 frame_delay--;
635 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000636 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
637 // need enough frames with audio to have reliable estimates, but as few as
638 // possible to keep processing time down. 4.5 seconds seemed to be a good
639 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000640 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000641 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000642 frame->CopyFrom(tmp_frame);
643 // Use the near end recording, since that has more speech in it.
644 ASSERT_TRUE(ReadFrame(near_file_, frame));
645 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000646 AudioFrame* reverse_frame = frame;
647 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000648 if (!causal) {
649 reverse_frame = frame_queue.front();
650 // When we call ProcessStream() the frame is modified, so we can't use the
651 // pointer directly when things are non-causal. Use an intermediate frame
652 // and copy the data.
653 process_frame = &tmp_frame;
654 process_frame->CopyFrom(*frame);
655 }
aluebsb0319552016-03-17 20:39:53 -0700656 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000657 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
658 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
659 frame = frame_queue.front();
660 frame_queue.pop();
661 delete frame;
662
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000663 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200665 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000666 }
667 }
668
669 rewind(near_file_);
670 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000671 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000672 frame_queue.pop();
673 delete frame;
674 }
675 // Calculate expected delay estimate and acceptable regions. Further,
676 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700678 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700679 const int expected_median =
680 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
681 const int expected_median_high = rtc::SafeClamp<int>(
682 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700683 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700684 const int expected_median_low = rtc::SafeClamp<int>(
685 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700686 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000687 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200688 AudioProcessingStats stats =
689 apm_->GetStatistics(true /* has_remote_tracks */);
690 ASSERT_TRUE(stats.delay_median_ms.has_value());
691 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000692 EXPECT_GE(expected_median_high, median);
693 EXPECT_LE(expected_median_low, median);
694}
695
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000696void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000697 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000698 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000699
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000700 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000701 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000702 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000703 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000704
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000705 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 EXPECT_EQ(apm_->kNoError,
707 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000708 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000709 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000710 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200713 AudioProcessing::Config apm_config = apm_->GetConfig();
714 apm_config.echo_canceller.enabled = true;
715 apm_config.echo_canceller.mobile_mode = false;
716 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000718 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000719 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000720 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000721
722 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Ã…hgren200feba2019-03-06 04:16:46 +0100724 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725
726 // Resets after successful ProcessStream().
727 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
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 // Other stream parameters set correctly.
732 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
733 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000734 apm_->gain_control()->set_stream_analog_level(127));
Per Ã…hgren200feba2019-03-06 04:16:46 +0100735 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000736 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
737
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000738 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000740 AnalyzeReverseStreamChooser(format));
Per Ã…hgren200feba2019-03-06 04:16:46 +0100741 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000742
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000743 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000744 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000745 EXPECT_EQ(apm_->kNoError,
746 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000747 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000748}
749
750TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000751 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000752}
753
754TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000756}
757
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000758TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
759 EXPECT_EQ(0, apm_->delay_offset_ms());
760 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
761 EXPECT_EQ(50, apm_->stream_delay_ms());
762}
763
764TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
765 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000766 apm_->set_delay_offset_ms(100);
767 EXPECT_EQ(100, apm_->delay_offset_ms());
768 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000769 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000770 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
771 EXPECT_EQ(200, apm_->stream_delay_ms());
772
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000773 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000774 apm_->set_delay_offset_ms(-50);
775 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000776 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
777 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000778 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
779 EXPECT_EQ(50, apm_->stream_delay_ms());
780}
781
Michael Graczyk86c6d332015-07-23 11:41:39 -0700782void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800783 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700784 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000785 frame_->num_channels_ = num_channels;
786 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700787 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000788}
789
Michael Graczyk86c6d332015-07-23 11:41:39 -0700790void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800791 size_t num_in_channels,
792 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700793 AudioProcessing::Error expected_return) {
794 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
795 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
796
797 EXPECT_EQ(expected_return,
798 apm_->ProcessStream(float_cb_->channels(), input_stream,
799 output_stream, float_cb_->channels()));
800}
801
802void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800803 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700804 AudioProcessing::Error expected_return) {
805 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700806 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
807 {output_sample_rate_hz_, apm_->num_output_channels()},
808 {frame_->sample_rate_hz_, num_rev_channels},
809 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700810
ekmeyerson60d9b332015-08-14 10:35:55 -0700811 EXPECT_EQ(
812 expected_return,
813 apm_->ProcessReverseStream(
814 float_cb_->channels(), processing_config.reverse_input_stream(),
815 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700816}
817
818TEST_F(ApmTest, ChannelsInt16Interface) {
819 // Testing number of invalid and valid channels.
820 Init(16000, 16000, 16000, 4, 4, 4, false);
821
822 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
823
Peter Kasting69558702016-01-12 16:26:35 -0800824 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000826 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000827 }
828}
829
Michael Graczyk86c6d332015-07-23 11:41:39 -0700830TEST_F(ApmTest, Channels) {
831 // Testing number of invalid and valid channels.
832 Init(16000, 16000, 16000, 4, 4, 4, false);
833
834 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
835 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
836
Peter Kasting69558702016-01-12 16:26:35 -0800837 for (size_t i = 1; i < 4; ++i) {
838 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700839 // Output channels much be one or match input channels.
840 if (j == 1 || i == j) {
841 TestChangingForwardChannels(i, j, kNoErr);
842 TestChangingReverseChannels(i, kNoErr);
843
844 EXPECT_EQ(i, apm_->num_input_channels());
845 EXPECT_EQ(j, apm_->num_output_channels());
846 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800847 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700848 } else {
849 TestChangingForwardChannels(i, j,
850 AudioProcessing::kBadNumberChannelsError);
851 }
852 }
853 }
854}
855
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000856TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000857 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000858 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000859 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000860 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700861 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800862 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000863 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000864 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 }
866}
867
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000868TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000869 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000870 // Enable AEC only.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200871 AudioProcessing::Config apm_config = apm_->GetConfig();
872 apm_config.echo_canceller.enabled = true;
Per Ã…hgren200feba2019-03-06 04:16:46 +0100873 // TODO(peah): Update tests to instead use AEC3.
874 apm_config.echo_canceller.use_legacy_aec = true;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200875 apm_config.echo_canceller.mobile_mode = false;
876 apm_->ApplyConfig(apm_config);
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000877 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700878 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000879 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000880
881 // Internally in the AEC the amount of lookahead the delay estimation can
882 // handle is 15 blocks and the maximum delay is set to 60 blocks.
883 const int kLookaheadBlocks = 15;
884 const int kMaxDelayBlocks = 60;
885 // The AEC has a startup time before it actually starts to process. This
886 // procedure can flush the internal far-end buffer, which of course affects
887 // the delay estimation. Therefore, we set a system_delay high enough to
888 // avoid that. The smallest system_delay you can report without flushing the
889 // buffer is 66 ms in 8 kHz.
890 //
891 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
892 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
893 // delay estimation. This should be noted though. In case of test failure,
894 // this could be the cause.
895 const int kSystemDelayMs = 66;
896 // Test a couple of corner cases and verify that the estimated delay is
897 // within a valid region (set to +-1.5 blocks). Note that these cases are
898 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800899 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000900 Init(kProcessSampleRates[i],
901 kProcessSampleRates[i],
902 kProcessSampleRates[i],
903 2,
904 2,
905 2,
906 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000907 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700908 const int num_ms_per_block =
909 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000910 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
911 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
912
913 // 1) Verify correct delay estimate at lookahead boundary.
914 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
915 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
916 delay_max_ms);
917 // 2) A delay less than maximum lookahead should give an delay estimate at
918 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
919 delay_ms -= 20;
920 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
921 delay_max_ms);
922 // 3) Three values around zero delay. Note that we need to compensate for
923 // the fake system_delay.
924 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
925 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
926 delay_max_ms);
927 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
928 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
929 delay_max_ms);
930 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
931 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
932 delay_max_ms);
933 // 4) Verify correct delay estimate at maximum delay boundary.
934 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
935 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
936 delay_max_ms);
937 // 5) A delay above the maximum delay should give an estimate at the
938 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
939 delay_ms += 20;
940 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
941 delay_max_ms);
942 }
943}
944
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000945TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000946 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +0000947 EXPECT_EQ(apm_->kNoError,
948 apm_->gain_control()->set_mode(
949 apm_->gain_control()->mode()));
950
951 GainControl::Mode mode[] = {
952 GainControl::kAdaptiveAnalog,
953 GainControl::kAdaptiveDigital,
954 GainControl::kFixedDigital
955 };
pkasting25702cb2016-01-08 13:50:27 -0800956 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000957 EXPECT_EQ(apm_->kNoError,
958 apm_->gain_control()->set_mode(mode[i]));
959 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
960 }
961 // Testing invalid target levels
962 EXPECT_EQ(apm_->kBadParameterError,
963 apm_->gain_control()->set_target_level_dbfs(-3));
964 EXPECT_EQ(apm_->kBadParameterError,
965 apm_->gain_control()->set_target_level_dbfs(-40));
966 // Testing valid target levels
967 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
978 // Testing invalid compression gains
979 EXPECT_EQ(apm_->kBadParameterError,
980 apm_->gain_control()->set_compression_gain_db(-1));
981 EXPECT_EQ(apm_->kBadParameterError,
982 apm_->gain_control()->set_compression_gain_db(100));
983
984 // Testing valid compression gains
985 EXPECT_EQ(apm_->kNoError,
986 apm_->gain_control()->set_compression_gain_db(
987 apm_->gain_control()->compression_gain_db()));
988
989 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -0800990 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000991 EXPECT_EQ(apm_->kNoError,
992 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
993 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
994 }
995
996 // Testing limiter off/on
997 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
998 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
999 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1000 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1001
1002 // Testing invalid level limits
1003 EXPECT_EQ(apm_->kBadParameterError,
1004 apm_->gain_control()->set_analog_level_limits(-1, 512));
1005 EXPECT_EQ(apm_->kBadParameterError,
1006 apm_->gain_control()->set_analog_level_limits(100000, 512));
1007 EXPECT_EQ(apm_->kBadParameterError,
1008 apm_->gain_control()->set_analog_level_limits(512, -1));
1009 EXPECT_EQ(apm_->kBadParameterError,
1010 apm_->gain_control()->set_analog_level_limits(512, 100000));
1011 EXPECT_EQ(apm_->kBadParameterError,
1012 apm_->gain_control()->set_analog_level_limits(512, 255));
1013
1014 // Testing valid level limits
1015 EXPECT_EQ(apm_->kNoError,
1016 apm_->gain_control()->set_analog_level_limits(
1017 apm_->gain_control()->analog_level_minimum(),
1018 apm_->gain_control()->analog_level_maximum()));
1019
1020 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001021 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001022 EXPECT_EQ(apm_->kNoError,
1023 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1024 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1025 }
1026
1027 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001028 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001029 EXPECT_EQ(apm_->kNoError,
1030 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1031 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1032 }
1033
1034 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1035
1036 // Turn AGC off
1037 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1038 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1039}
1040
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001041void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001042 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001043 EXPECT_EQ(apm_->kNoError,
1044 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1045 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1046
1047 int out_analog_level = 0;
1048 for (int i = 0; i < 2000; ++i) {
1049 ReadFrameWithRewind(near_file_, frame_);
1050 // Ensure the audio is at a low level, so the AGC will try to increase it.
1051 ScaleFrame(frame_, 0.25);
1052
1053 // Always pass in the same volume.
1054 EXPECT_EQ(apm_->kNoError,
1055 apm_->gain_control()->set_stream_analog_level(100));
1056 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1057 out_analog_level = apm_->gain_control()->stream_analog_level();
1058 }
1059
1060 // Ensure the AGC is still able to reach the maximum.
1061 EXPECT_EQ(255, out_analog_level);
1062}
1063
1064// Verifies that despite volume slider quantization, the AGC can continue to
1065// increase its volume.
1066TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001067 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001068 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1069 }
1070}
1071
1072void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001073 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001074 EXPECT_EQ(apm_->kNoError,
1075 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1076 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1077
1078 int out_analog_level = 100;
1079 for (int i = 0; i < 1000; ++i) {
1080 ReadFrameWithRewind(near_file_, frame_);
1081 // Ensure the audio is at a low level, so the AGC will try to increase it.
1082 ScaleFrame(frame_, 0.25);
1083
1084 EXPECT_EQ(apm_->kNoError,
1085 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1086 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1087 out_analog_level = apm_->gain_control()->stream_analog_level();
1088 }
1089
1090 // Ensure the volume was raised.
1091 EXPECT_GT(out_analog_level, 100);
1092 int highest_level_reached = out_analog_level;
1093 // Simulate a user manual volume change.
1094 out_analog_level = 100;
1095
1096 for (int i = 0; i < 300; ++i) {
1097 ReadFrameWithRewind(near_file_, frame_);
1098 ScaleFrame(frame_, 0.25);
1099
1100 EXPECT_EQ(apm_->kNoError,
1101 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1102 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1103 out_analog_level = apm_->gain_control()->stream_analog_level();
1104 // Check that AGC respected the manually adjusted volume.
1105 EXPECT_LT(out_analog_level, highest_level_reached);
1106 }
1107 // Check that the volume was still raised.
1108 EXPECT_GT(out_analog_level, 100);
1109}
1110
1111TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001112 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001113 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1114 }
1115}
1116
niklase@google.com470e71d2011-07-07 08:21:25 +00001117TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001118 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001119 NoiseSuppression::Level level[] = {
1120 NoiseSuppression::kLow,
1121 NoiseSuppression::kModerate,
1122 NoiseSuppression::kHigh,
1123 NoiseSuppression::kVeryHigh
1124 };
pkasting25702cb2016-01-08 13:50:27 -08001125 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001126 EXPECT_EQ(apm_->kNoError,
1127 apm_->noise_suppression()->set_level(level[i]));
1128 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1129 }
1130
andrew@webrtc.org648af742012-02-08 01:57:29 +00001131 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001132 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1133 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1134 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1135 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1136}
1137
1138TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001139 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001140 AudioProcessing::Config apm_config;
1141 apm_config.high_pass_filter.enabled = true;
1142 apm_->ApplyConfig(apm_config);
1143 apm_config.high_pass_filter.enabled = false;
1144 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001145}
1146
1147TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001148 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001149 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001150 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001151
1152 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1153
1154 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1155 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1156
1157 // Run this test in wideband; in super-wb, the splitting filter distorts the
1158 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001159 frame_->samples_per_channel_ = 160;
1160 frame_->num_channels_ = 2;
1161 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001162
1163 // Min value if no frames have been processed.
1164 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1165
1166 // Min value on zero frames.
1167 SetFrameTo(frame_, 0);
1168 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1169 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1170 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1171
1172 // Try a few RMS values.
1173 // (These also test that the value resets after retrieving it.)
1174 SetFrameTo(frame_, 32767);
1175 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1176 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1177 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1178
1179 SetFrameTo(frame_, 30000);
1180 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1181 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1182 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1183
1184 SetFrameTo(frame_, 10000);
1185 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1186 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1187 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1188
1189 SetFrameTo(frame_, 10);
1190 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1191 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1192 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1193
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001194 // Verify reset after enable/disable.
1195 SetFrameTo(frame_, 32767);
1196 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1197 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1198 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1199 SetFrameTo(frame_, 1);
1200 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1201 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1202
1203 // Verify reset after initialize.
1204 SetFrameTo(frame_, 32767);
1205 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1206 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1207 SetFrameTo(frame_, 1);
1208 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1209 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001210}
1211
1212TEST_F(ApmTest, VoiceDetection) {
1213 // Test external VAD
1214 EXPECT_EQ(apm_->kNoError,
1215 apm_->voice_detection()->set_stream_has_voice(true));
1216 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1217 EXPECT_EQ(apm_->kNoError,
1218 apm_->voice_detection()->set_stream_has_voice(false));
1219 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1220
andrew@webrtc.org648af742012-02-08 01:57:29 +00001221 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001222 VoiceDetection::Likelihood likelihood[] = {
1223 VoiceDetection::kVeryLowLikelihood,
1224 VoiceDetection::kLowLikelihood,
1225 VoiceDetection::kModerateLikelihood,
1226 VoiceDetection::kHighLikelihood
1227 };
pkasting25702cb2016-01-08 13:50:27 -08001228 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001229 EXPECT_EQ(apm_->kNoError,
1230 apm_->voice_detection()->set_likelihood(likelihood[i]));
1231 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1232 }
1233
1234 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001235 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001236 EXPECT_EQ(apm_->kBadParameterError,
1237 apm_->voice_detection()->set_frame_size_ms(12));
1238
andrew@webrtc.org648af742012-02-08 01:57:29 +00001239 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 for (int i = 10; i <= 30; i += 10) {
1241 EXPECT_EQ(apm_->kNoError,
1242 apm_->voice_detection()->set_frame_size_ms(i));
1243 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1244 }
1245 */
1246
andrew@webrtc.org648af742012-02-08 01:57:29 +00001247 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001248 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1249 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1250 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1251 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1252
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001253 // Test that AudioFrame activity is maintained when VAD is disabled.
1254 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1255 AudioFrame::VADActivity activity[] = {
1256 AudioFrame::kVadActive,
1257 AudioFrame::kVadPassive,
1258 AudioFrame::kVadUnknown
1259 };
pkasting25702cb2016-01-08 13:50:27 -08001260 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001261 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001262 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001263 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001264 }
1265
1266 // Test that AudioFrame activity is set when VAD is enabled.
1267 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001268 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001269 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001270 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001271
niklase@google.com470e71d2011-07-07 08:21:25 +00001272 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1273}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001274
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001275TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001276 AudioProcessing::Config config = apm_->GetConfig();
1277 EXPECT_FALSE(config.echo_canceller.enabled);
1278 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001279 EXPECT_FALSE(config.level_estimation.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001280 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001281 EXPECT_FALSE(apm_->gain_control()->is_enabled());
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001282 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1283 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1284 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1285}
1286
1287TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001288 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001289 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001290 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001291 AudioFrame frame_copy;
1292 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001293 for (int j = 0; j < 1000; j++) {
1294 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1295 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001296 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1297 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001298 }
1299 }
1300}
1301
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001302TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1303 // Test that ProcessStream copies input to output even with no processing.
1304 const size_t kSamples = 80;
1305 const int sample_rate = 8000;
1306 const float src[kSamples] = {
1307 -1.0f, 0.0f, 1.0f
1308 };
1309 float dest[kSamples] = {};
1310
1311 auto src_channels = &src[0];
1312 auto dest_channels = &dest[0];
1313
Ivo Creusen62337e52018-01-09 14:17:33 +01001314 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001315 EXPECT_NOERR(apm_->ProcessStream(
1316 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1317 sample_rate, LayoutFromChannels(1), &dest_channels));
1318
1319 for (size_t i = 0; i < kSamples; ++i) {
1320 EXPECT_EQ(src[i], dest[i]);
1321 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001322
1323 // Same for ProcessReverseStream.
1324 float rev_dest[kSamples] = {};
1325 auto rev_dest_channels = &rev_dest[0];
1326
1327 StreamConfig input_stream = {sample_rate, 1};
1328 StreamConfig output_stream = {sample_rate, 1};
1329 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1330 output_stream, &rev_dest_channels));
1331
1332 for (size_t i = 0; i < kSamples; ++i) {
1333 EXPECT_EQ(src[i], rev_dest[i]);
1334 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001335}
1336
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001337TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1338 EnableAllComponents();
1339
pkasting25702cb2016-01-08 13:50:27 -08001340 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001341 Init(kProcessSampleRates[i],
1342 kProcessSampleRates[i],
1343 kProcessSampleRates[i],
1344 2,
1345 2,
1346 2,
1347 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001348 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001349 ASSERT_EQ(0, feof(far_file_));
1350 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001351 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001352 CopyLeftToRightChannel(revframe_->mutable_data(),
1353 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001354
aluebsb0319552016-03-17 20:39:53 -07001355 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001356
yujo36b1a5f2017-06-12 12:45:32 -07001357 CopyLeftToRightChannel(frame_->mutable_data(),
1358 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001359 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1360
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001361 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001362 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001363 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001364 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001365 analog_level = apm_->gain_control()->stream_analog_level();
1366
yujo36b1a5f2017-06-12 12:45:32 -07001367 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001368 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001369 rewind(far_file_);
1370 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001371 }
1372}
1373
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001374TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001375 // Verify the filter is not active through undistorted audio when:
1376 // 1. No components are enabled...
1377 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001378 AudioFrame frame_copy;
1379 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001380 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1381 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1382 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1383
1384 // 2. Only the level estimator is enabled...
1385 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001386 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001387 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1388 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1389 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1390 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1391 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1392
1393 // 3. Only VAD is enabled...
1394 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001395 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001396 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1397 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1398 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1399 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1400 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1401
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001402 // 4. Only GetStatistics-reporting VAD is enabled...
1403 SetFrameTo(frame_, 1000);
1404 frame_copy.CopyFrom(*frame_);
1405 auto apm_config = apm_->GetConfig();
1406 apm_config.voice_detection.enabled = true;
1407 apm_->ApplyConfig(apm_config);
1408 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1409 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1410 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1411 apm_config.voice_detection.enabled = false;
1412 apm_->ApplyConfig(apm_config);
1413
1414 // 5. Both VADs and the level estimator are enabled...
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001415 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001416 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001417 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1418 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001419 apm_config.voice_detection.enabled = true;
1420 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001421 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1422 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1423 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1424 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1425 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001426 apm_config.voice_detection.enabled = false;
1427 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001428
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001429 // Check the test is valid. We should have distortion from the filter
1430 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001431 apm_config.echo_canceller.enabled = true;
Per Ã…hgren200feba2019-03-06 04:16:46 +01001432 // TODO(peah): Update tests to instead use AEC3.
1433 apm_config.echo_canceller.use_legacy_aec = true;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001434 apm_config.echo_canceller.mobile_mode = false;
1435 apm_->ApplyConfig(apm_config);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001436 frame_->samples_per_channel_ = 320;
1437 frame_->num_channels_ = 2;
1438 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001439 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001440 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001441 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001442 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1443 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1444}
1445
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001446#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1447void ApmTest::ProcessDebugDump(const std::string& in_filename,
1448 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001449 Format format,
1450 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001451 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001452 FILE* in_file = fopen(in_filename.c_str(), "rb");
1453 ASSERT_TRUE(in_file != NULL);
1454 audioproc::Event event_msg;
1455 bool first_init = true;
1456
1457 while (ReadMessageFromFile(in_file, &event_msg)) {
1458 if (event_msg.type() == audioproc::Event::INIT) {
1459 const audioproc::Init msg = event_msg.init();
1460 int reverse_sample_rate = msg.sample_rate();
1461 if (msg.has_reverse_sample_rate()) {
1462 reverse_sample_rate = msg.reverse_sample_rate();
1463 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001464 int output_sample_rate = msg.sample_rate();
1465 if (msg.has_output_sample_rate()) {
1466 output_sample_rate = msg.output_sample_rate();
1467 }
1468
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001469 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001470 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001471 reverse_sample_rate,
1472 msg.num_input_channels(),
1473 msg.num_output_channels(),
1474 msg.num_reverse_channels(),
1475 false);
1476 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001477 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001478 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001479 auto aec_dump =
1480 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1481 EXPECT_TRUE(aec_dump);
1482 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001483 first_init = false;
1484 }
1485
1486 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1487 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1488
1489 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001490 ASSERT_EQ(revframe_->num_channels_,
1491 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001492 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001493 memcpy(revfloat_cb_->channels()[i],
1494 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001495 msg.channel(i).size());
1496 }
1497 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001498 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001499 if (format == kFloatFormat) {
1500 // We're using an int16 input file; convert to float.
1501 ConvertToFloat(*revframe_, revfloat_cb_.get());
1502 }
1503 }
1504 AnalyzeReverseStreamChooser(format);
1505
1506 } else if (event_msg.type() == audioproc::Event::STREAM) {
1507 const audioproc::Stream msg = event_msg.stream();
1508 // ProcessStream could have changed this for the output frame.
1509 frame_->num_channels_ = apm_->num_input_channels();
1510
1511 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1512 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001513 if (msg.has_keypress()) {
1514 apm_->set_stream_key_pressed(msg.keypress());
1515 } else {
1516 apm_->set_stream_key_pressed(true);
1517 }
1518
1519 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001520 ASSERT_EQ(frame_->num_channels_,
1521 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001522 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001523 memcpy(float_cb_->channels()[i],
1524 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001525 msg.input_channel(i).size());
1526 }
1527 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001528 memcpy(frame_->mutable_data(), msg.input_data().data(),
1529 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001530 if (format == kFloatFormat) {
1531 // We're using an int16 input file; convert to float.
1532 ConvertToFloat(*frame_, float_cb_.get());
1533 }
1534 }
1535 ProcessStreamChooser(format);
1536 }
1537 }
aleloif4dd1912017-06-15 01:55:38 -07001538 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001539 fclose(in_file);
1540}
1541
1542void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001543 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001544 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001545 std::string format_string;
1546 switch (format) {
1547 case kIntFormat:
1548 format_string = "_int";
1549 break;
1550 case kFloatFormat:
1551 format_string = "_float";
1552 break;
1553 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001554 const std::string ref_filename = test::TempFilename(
1555 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1556 const std::string out_filename = test::TempFilename(
1557 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001558 const std::string limited_filename = test::TempFilename(
1559 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1560 const size_t logging_limit_bytes = 100000;
1561 // We expect at least this many bytes in the created logfile.
1562 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001563 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001564 ProcessDebugDump(in_filename, ref_filename, format, -1);
1565 ProcessDebugDump(ref_filename, out_filename, format, -1);
1566 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001567
1568 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1569 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001570 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001571 ASSERT_TRUE(ref_file != NULL);
1572 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001573 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001574 std::unique_ptr<uint8_t[]> ref_bytes;
1575 std::unique_ptr<uint8_t[]> out_bytes;
1576 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001577
1578 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1579 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001580 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001581 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001582 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001583 while (ref_size > 0 && out_size > 0) {
1584 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001585 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001586 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001587 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001588 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001589 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001590 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1591 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001592 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001593 }
1594 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001595 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1596 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001597 EXPECT_NE(0, feof(ref_file));
1598 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001599 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001600 ASSERT_EQ(0, fclose(ref_file));
1601 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001602 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001603 remove(ref_filename.c_str());
1604 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001605 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001606}
1607
pbosc7a65692016-05-06 12:50:04 -07001608TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001609 VerifyDebugDumpTest(kIntFormat);
1610}
1611
pbosc7a65692016-05-06 12:50:04 -07001612TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001613 VerifyDebugDumpTest(kFloatFormat);
1614}
1615#endif
1616
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001617// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001618TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001619 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001620 const std::string filename =
1621 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001622 {
1623 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1624 EXPECT_FALSE(aec_dump);
1625 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001626
1627#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1628 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001629 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001630
aleloif4dd1912017-06-15 01:55:38 -07001631 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1632 EXPECT_TRUE(aec_dump);
1633 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001634 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001635 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001636 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001637
1638 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001639 FILE* fid = fopen(filename.c_str(), "r");
1640 ASSERT_TRUE(fid != NULL);
1641
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001642 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001643 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001644 ASSERT_EQ(0, remove(filename.c_str()));
1645#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001646 // Verify the file has NOT been written.
1647 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1648#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1649}
1650
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001651// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001652TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001653 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001654
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001655 const std::string filename =
1656 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001657 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001658 ASSERT_TRUE(fid);
1659
1660#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1661 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001662 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001663
aleloif4dd1912017-06-15 01:55:38 -07001664 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1665 EXPECT_TRUE(aec_dump);
1666 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001667 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001668 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001669 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001670
1671 // Verify the file has been written.
1672 fid = fopen(filename.c_str(), "r");
1673 ASSERT_TRUE(fid != NULL);
1674
1675 // Clean it up.
1676 ASSERT_EQ(0, fclose(fid));
1677 ASSERT_EQ(0, remove(filename.c_str()));
1678#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001679 ASSERT_EQ(0, fclose(fid));
1680#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1681}
1682
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001683TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001684 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001685 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001686
1687 Config config;
1688 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001689 std::unique_ptr<AudioProcessing> fapm(
1690 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001691 EnableAllComponents();
1692 EnableAllAPComponents(fapm.get());
1693 for (int i = 0; i < ref_data.test_size(); i++) {
1694 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1695
1696 audioproc::Test* test = ref_data.mutable_test(i);
1697 // TODO(ajm): Restore downmixing test cases.
1698 if (test->num_input_channels() != test->num_output_channels())
1699 continue;
1700
Peter Kasting69558702016-01-12 16:26:35 -08001701 const size_t num_render_channels =
1702 static_cast<size_t>(test->num_reverse_channels());
1703 const size_t num_input_channels =
1704 static_cast<size_t>(test->num_input_channels());
1705 const size_t num_output_channels =
1706 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001707 const size_t samples_per_channel = static_cast<size_t>(
1708 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001709
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001710 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1711 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001712 Init(fapm.get());
1713
1714 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001715 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1716 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001717
1718 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001719 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001720 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1721 ReadFrame(near_file_, frame_, float_cb_.get())) {
1722 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1723
aluebsb0319552016-03-17 20:39:53 -07001724 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001725 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1726 revfloat_cb_->channels(),
1727 samples_per_channel,
1728 test->sample_rate(),
1729 LayoutFromChannels(num_render_channels)));
1730
1731 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1732 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001733 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1734 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1735
1736 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001737 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001738 output_int16.channels());
1739
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001740 EXPECT_NOERR(fapm->ProcessStream(
1741 float_cb_->channels(),
1742 samples_per_channel,
1743 test->sample_rate(),
1744 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001745 test->sample_rate(),
1746 LayoutFromChannels(num_output_channels),
1747 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001748 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001749 FloatToS16(float_cb_->channels()[j],
1750 samples_per_channel,
1751 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001752 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001753 float snr = ComputeSNR(output_int16.channels()[j],
1754 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001755 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001756
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001757 const float kVarianceThreshold = 20;
1758 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001759
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001760 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001761 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1762 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001763 }
1764 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001765
1766 analog_level = fapm->gain_control()->stream_analog_level();
1767 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1768 fapm->gain_control()->stream_analog_level());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001769 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1770 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001771 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001772
1773 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001774 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001775 }
aluebs776593b2016-03-15 14:04:58 -07001776
1777#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1778 const size_t kMaxNumBadChunks = 0;
1779#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1780 // There are a few chunks in the fixed-point profile that give low SNR.
1781 // Listening confirmed the difference is acceptable.
1782 const size_t kMaxNumBadChunks = 60;
1783#endif
1784 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1785
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001786 rewind(far_file_);
1787 rewind(near_file_);
1788 }
1789}
1790
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001791// TODO(andrew): Add a test to process a few frames with different combinations
1792// of enabled components.
1793
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001794TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001795 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001796 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001797
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001798 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001799 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001800 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001801 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001802 for (size_t i = 0; i < arraysize(kChannels); i++) {
1803 for (size_t j = 0; j < arraysize(kChannels); j++) {
1804 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001805 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001806 test->set_num_reverse_channels(kChannels[i]);
1807 test->set_num_input_channels(kChannels[j]);
1808 test->set_num_output_channels(kChannels[j]);
1809 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001810 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001811 }
1812 }
1813 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001814#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1815 // To test the extended filter mode.
1816 audioproc::Test* test = ref_data.add_test();
1817 test->set_num_reverse_channels(2);
1818 test->set_num_input_channels(2);
1819 test->set_num_output_channels(2);
1820 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1821 test->set_use_aec_extended_filter(true);
1822#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001823 }
1824
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001825 for (int i = 0; i < ref_data.test_size(); i++) {
1826 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001827
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001828 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001829 // TODO(ajm): We no longer allow different input and output channels. Skip
1830 // these tests for now, but they should be removed from the set.
1831 if (test->num_input_channels() != test->num_output_channels())
1832 continue;
1833
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001834 Config config;
1835 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001836 config.Set<ExtendedFilter>(
1837 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001838 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001839
1840 EnableAllComponents();
1841
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001842 Init(test->sample_rate(),
1843 test->sample_rate(),
1844 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001845 static_cast<size_t>(test->num_input_channels()),
1846 static_cast<size_t>(test->num_output_channels()),
1847 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001848 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001849
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001850 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001851 int has_voice_count = 0;
1852 int is_saturated_count = 0;
1853 int analog_level = 127;
1854 int analog_level_average = 0;
1855 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001856 float ns_speech_prob_average = 0.0f;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001857 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001858#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1859 int stats_index = 0;
1860#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001861
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001862 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001863 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001864
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001865 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1866
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001867 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001868 EXPECT_EQ(apm_->kNoError,
1869 apm_->gain_control()->set_stream_analog_level(analog_level));
1870
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001871 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001872
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001873 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001874 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1875 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001876
1877 max_output_average += MaxAudioFrame(*frame_);
1878
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001879 analog_level = apm_->gain_control()->stream_analog_level();
1880 analog_level_average += analog_level;
1881 if (apm_->gain_control()->stream_is_saturated()) {
1882 is_saturated_count++;
1883 }
1884 if (apm_->voice_detection()->stream_has_voice()) {
1885 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001886 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001887 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001888 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001889 }
1890
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001891 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
Sam Zackrisson11b87032018-12-18 17:13:58 +01001892 AudioProcessingStats stats =
1893 apm_->GetStatistics(/*has_remote_tracks=*/false);
1894 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001895
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001896 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001897 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001898 sizeof(int16_t),
1899 frame_size,
1900 out_file_);
1901 ASSERT_EQ(frame_size, write_count);
1902
1903 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001904 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001905 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001906
1907#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1908 const int kStatsAggregationFrameNum = 100; // 1 second.
1909 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001910 // Get echo and delay metrics.
1911 AudioProcessingStats stats =
1912 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001913
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001914 // Echo metrics.
1915 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1916 const float echo_return_loss_enhancement =
1917 stats.echo_return_loss_enhancement.value_or(-1.0f);
1918 const float divergent_filter_fraction =
1919 stats.divergent_filter_fraction.value_or(-1.0f);
1920 const float residual_echo_likelihood =
1921 stats.residual_echo_likelihood.value_or(-1.0f);
1922 const float residual_echo_likelihood_recent_max =
1923 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1924
1925 // Delay metrics.
1926 const int32_t delay_median_ms = stats.delay_median_ms.value_or(-1.0);
1927 const int32_t delay_standard_deviation_ms =
1928 stats.delay_standard_deviation_ms.value_or(-1.0);
minyue58530ed2016-05-24 05:50:12 -07001929
minyue58530ed2016-05-24 05:50:12 -07001930 if (!write_ref_data) {
1931 const audioproc::Test::EchoMetrics& reference =
1932 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001933 constexpr float kEpsilon = 0.01;
1934 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1935 EXPECT_NEAR(echo_return_loss_enhancement,
1936 reference.echo_return_loss_enhancement(), kEpsilon);
1937 EXPECT_NEAR(divergent_filter_fraction,
1938 reference.divergent_filter_fraction(), kEpsilon);
1939 EXPECT_NEAR(residual_echo_likelihood,
1940 reference.residual_echo_likelihood(), kEpsilon);
1941 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1942 reference.residual_echo_likelihood_recent_max(),
1943 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001944
1945 const audioproc::Test::DelayMetrics& reference_delay =
1946 test->delay_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001947 EXPECT_EQ(reference_delay.median(), delay_median_ms);
1948 EXPECT_EQ(reference_delay.std(), delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07001949
minyue58530ed2016-05-24 05:50:12 -07001950 ++stats_index;
1951 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001952 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1953 message_echo->set_echo_return_loss(echo_return_loss);
1954 message_echo->set_echo_return_loss_enhancement(
1955 echo_return_loss_enhancement);
1956 message_echo->set_divergent_filter_fraction(
1957 divergent_filter_fraction);
1958 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1959 message_echo->set_residual_echo_likelihood_recent_max(
1960 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001961 audioproc::Test::DelayMetrics* message_delay =
1962 test->add_delay_metrics();
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001963 message_delay->set_median(delay_median_ms);
1964 message_delay->set_std(delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07001965 }
1966 }
1967#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001968 }
1969 max_output_average /= frame_count;
1970 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001971 ns_speech_prob_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001972 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001973
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001974 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001975 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001976 // When running the test on a N7 we get a {2, 6} difference of
1977 // |has_voice_count| and |max_output_average| is up to 18 higher.
1978 // All numbers being consistently higher on N7 compare to ref_data.
1979 // TODO(bjornv): If we start getting more of these offsets on Android we
1980 // should consider a different approach. Either using one slack for all,
1981 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001982#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001983 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001984 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001985 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001986 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001987#else
1988 const int kHasVoiceCountOffset = 0;
1989 const int kHasVoiceCountNear = kIntNear;
1990 const int kMaxOutputAverageOffset = 0;
1991 const int kMaxOutputAverageNear = kIntNear;
1992#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001993 EXPECT_NEAR(test->has_voice_count(),
1994 has_voice_count - kHasVoiceCountOffset,
1995 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001996 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001997
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001998 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001999 EXPECT_NEAR(test->max_output_average(),
2000 max_output_average - kMaxOutputAverageOffset,
2001 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002002#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002003 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002004 EXPECT_NEAR(test->ns_speech_probability_average(),
2005 ns_speech_prob_average,
2006 kFloatNear);
Sam Zackrisson11b87032018-12-18 17:13:58 +01002007 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002008#endif
2009 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002010 test->set_has_voice_count(has_voice_count);
2011 test->set_is_saturated_count(is_saturated_count);
2012
2013 test->set_analog_level_average(analog_level_average);
2014 test->set_max_output_average(max_output_average);
2015
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002016#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002017 EXPECT_LE(0.0f, ns_speech_prob_average);
2018 EXPECT_GE(1.0f, ns_speech_prob_average);
2019 test->set_ns_speech_probability_average(ns_speech_prob_average);
Sam Zackrisson11b87032018-12-18 17:13:58 +01002020 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002021#endif
2022 }
2023
2024 rewind(far_file_);
2025 rewind(near_file_);
2026 }
2027
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002028 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002029 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002030 }
2031}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002032
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002033TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2034 struct ChannelFormat {
2035 AudioProcessing::ChannelLayout in_layout;
2036 AudioProcessing::ChannelLayout out_layout;
2037 };
2038 ChannelFormat cf[] = {
2039 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2040 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2041 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2042 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002043
Ivo Creusen62337e52018-01-09 14:17:33 +01002044 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002045 // Enable one component just to ensure some processing takes place.
2046 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002047 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002048 const int in_rate = 44100;
2049 const int out_rate = 48000;
2050 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2051 TotalChannelsFromLayout(cf[i].in_layout));
2052 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2053 ChannelsFromLayout(cf[i].out_layout));
2054
2055 // Run over a few chunks.
2056 for (int j = 0; j < 10; ++j) {
2057 EXPECT_NOERR(ap->ProcessStream(
2058 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002059 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002060 in_rate,
2061 cf[i].in_layout,
2062 out_rate,
2063 cf[i].out_layout,
2064 out_cb.channels()));
2065 }
2066 }
2067}
2068
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002069// Compares the reference and test arrays over a region around the expected
2070// delay. Finds the highest SNR in that region and adds the variance and squared
2071// error results to the supplied accumulators.
2072void UpdateBestSNR(const float* ref,
2073 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002074 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002075 int expected_delay,
2076 double* variance_acc,
2077 double* sq_error_acc) {
2078 double best_snr = std::numeric_limits<double>::min();
2079 double best_variance = 0;
2080 double best_sq_error = 0;
2081 // Search over a region of eight samples around the expected delay.
2082 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2083 ++delay) {
2084 double sq_error = 0;
2085 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002086 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002087 double error = test[i + delay] - ref[i];
2088 sq_error += error * error;
2089 variance += ref[i] * ref[i];
2090 }
2091
2092 if (sq_error == 0) {
2093 *variance_acc += variance;
2094 return;
2095 }
2096 double snr = variance / sq_error;
2097 if (snr > best_snr) {
2098 best_snr = snr;
2099 best_variance = variance;
2100 best_sq_error = sq_error;
2101 }
2102 }
2103
2104 *variance_acc += best_variance;
2105 *sq_error_acc += best_sq_error;
2106}
2107
2108// Used to test a multitude of sample rate and channel combinations. It works
2109// by first producing a set of reference files (in SetUpTestCase) that are
2110// assumed to be correct, as the used parameters are verified by other tests
2111// in this collection. Primarily the reference files are all produced at
2112// "native" rates which do not involve any resampling.
2113
2114// Each test pass produces an output file with a particular format. The output
2115// is matched against the reference file closest to its internal processing
2116// format. If necessary the output is resampled back to its process format.
2117// Due to the resampling distortion, we don't expect identical results, but
2118// enforce SNR thresholds which vary depending on the format. 0 is a special
2119// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002120typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002121class AudioProcessingTest
2122 : public testing::TestWithParam<AudioProcessingTestData> {
2123 public:
2124 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002125 : input_rate_(std::get<0>(GetParam())),
2126 output_rate_(std::get<1>(GetParam())),
2127 reverse_input_rate_(std::get<2>(GetParam())),
2128 reverse_output_rate_(std::get<3>(GetParam())),
2129 expected_snr_(std::get<4>(GetParam())),
2130 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002131
2132 virtual ~AudioProcessingTest() {}
2133
2134 static void SetUpTestCase() {
2135 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002136 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002137 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002138 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2139 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2140 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002141 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002142 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2143 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2144 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002145 }
2146 }
2147 }
2148 }
2149
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002150 void TearDown() {
2151 // Remove "out" files after each test.
2152 ClearTempOutFiles();
2153 }
2154
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002155 static void TearDownTestCase() {
2156 ClearTempFiles();
2157 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002158
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002159 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002160 // to a file specified with |output_file_prefix|. Both forward and reverse
2161 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002162 static void ProcessFormat(int input_rate,
2163 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002164 int reverse_input_rate,
2165 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002166 size_t num_input_channels,
2167 size_t num_output_channels,
2168 size_t num_reverse_input_channels,
2169 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002170 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002171 Config config;
2172 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002173 std::unique_ptr<AudioProcessing> ap(
2174 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002175 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002176
ekmeyerson60d9b332015-08-14 10:35:55 -07002177 ProcessingConfig processing_config = {
2178 {{input_rate, num_input_channels},
2179 {output_rate, num_output_channels},
2180 {reverse_input_rate, num_reverse_input_channels},
2181 {reverse_output_rate, num_reverse_output_channels}}};
2182 ap->Initialize(processing_config);
2183
2184 FILE* far_file =
2185 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002186 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002187 FILE* out_file =
2188 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2189 reverse_input_rate, reverse_output_rate,
2190 num_input_channels, num_output_channels,
2191 num_reverse_input_channels,
2192 num_reverse_output_channels, kForward).c_str(),
2193 "wb");
2194 FILE* rev_out_file =
2195 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2196 reverse_input_rate, reverse_output_rate,
2197 num_input_channels, num_output_channels,
2198 num_reverse_input_channels,
2199 num_reverse_output_channels, kReverse).c_str(),
2200 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002201 ASSERT_TRUE(far_file != NULL);
2202 ASSERT_TRUE(near_file != NULL);
2203 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002204 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002205
2206 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2207 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002208 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2209 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002210 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2211 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002212 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2213 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002214
2215 // Temporary buffers.
2216 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002217 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2218 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002219 std::unique_ptr<float[]> float_data(new float[max_length]);
2220 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002221
2222 int analog_level = 127;
2223 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2224 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002225 EXPECT_NOERR(ap->ProcessReverseStream(
2226 rev_cb.channels(), processing_config.reverse_input_stream(),
2227 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002228
2229 EXPECT_NOERR(ap->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002230 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2231
2232 EXPECT_NOERR(ap->ProcessStream(
2233 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002234 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002235 input_rate,
2236 LayoutFromChannels(num_input_channels),
2237 output_rate,
2238 LayoutFromChannels(num_output_channels),
2239 out_cb.channels()));
2240
ekmeyerson60d9b332015-08-14 10:35:55 -07002241 // Dump forward output to file.
2242 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002243 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002244 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002245
pkasting25702cb2016-01-08 13:50:27 -08002246 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002247 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002248 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002249
ekmeyerson60d9b332015-08-14 10:35:55 -07002250 // Dump reverse output to file.
2251 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2252 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002253 size_t rev_out_length =
2254 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002255
pkasting25702cb2016-01-08 13:50:27 -08002256 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002257 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2258 rev_out_file));
2259
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002260 analog_level = ap->gain_control()->stream_analog_level();
2261 }
2262 fclose(far_file);
2263 fclose(near_file);
2264 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002265 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002266 }
2267
2268 protected:
2269 int input_rate_;
2270 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002271 int reverse_input_rate_;
2272 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002273 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002274 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002275};
2276
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002277TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002278 struct ChannelFormat {
2279 int num_input;
2280 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002281 int num_reverse_input;
2282 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002283 };
2284 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002285 {1, 1, 1, 1},
2286 {1, 1, 2, 1},
2287 {2, 1, 1, 1},
2288 {2, 1, 2, 1},
2289 {2, 2, 1, 1},
2290 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002291 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002292
pkasting25702cb2016-01-08 13:50:27 -08002293 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002294 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2295 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2296 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002297
ekmeyerson60d9b332015-08-14 10:35:55 -07002298 // Verify output for both directions.
2299 std::vector<StreamDirection> stream_directions;
2300 stream_directions.push_back(kForward);
2301 stream_directions.push_back(kReverse);
2302 for (StreamDirection file_direction : stream_directions) {
2303 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2304 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2305 const int out_num =
2306 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2307 const double expected_snr =
2308 file_direction ? expected_reverse_snr_ : expected_snr_;
2309
2310 const int min_ref_rate = std::min(in_rate, out_rate);
2311 int ref_rate;
2312
2313 if (min_ref_rate > 32000) {
2314 ref_rate = 48000;
2315 } else if (min_ref_rate > 16000) {
2316 ref_rate = 32000;
2317 } else if (min_ref_rate > 8000) {
2318 ref_rate = 16000;
2319 } else {
2320 ref_rate = 8000;
2321 }
aluebs776593b2016-03-15 14:04:58 -07002322#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002323 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002324 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002325 }
2326#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002327 FILE* out_file = fopen(
2328 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2329 reverse_output_rate_, cf[i].num_input,
2330 cf[i].num_output, cf[i].num_reverse_input,
2331 cf[i].num_reverse_output, file_direction).c_str(),
2332 "rb");
2333 // The reference files always have matching input and output channels.
2334 FILE* ref_file = fopen(
2335 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2336 cf[i].num_output, cf[i].num_output,
2337 cf[i].num_reverse_output, cf[i].num_reverse_output,
2338 file_direction).c_str(),
2339 "rb");
2340 ASSERT_TRUE(out_file != NULL);
2341 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002342
pkasting25702cb2016-01-08 13:50:27 -08002343 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2344 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002345 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002346 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002347 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002348 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002349 // Data from the resampled output, in case the reference and output rates
2350 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002351 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002352
ekmeyerson60d9b332015-08-14 10:35:55 -07002353 PushResampler<float> resampler;
2354 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002355
ekmeyerson60d9b332015-08-14 10:35:55 -07002356 // Compute the resampling delay of the output relative to the reference,
2357 // to find the region over which we should search for the best SNR.
2358 float expected_delay_sec = 0;
2359 if (in_rate != ref_rate) {
2360 // Input resampling delay.
2361 expected_delay_sec +=
2362 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2363 }
2364 if (out_rate != ref_rate) {
2365 // Output resampling delay.
2366 expected_delay_sec +=
2367 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2368 // Delay of converting the output back to its processing rate for
2369 // testing.
2370 expected_delay_sec +=
2371 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2372 }
2373 int expected_delay =
2374 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002375
ekmeyerson60d9b332015-08-14 10:35:55 -07002376 double variance = 0;
2377 double sq_error = 0;
2378 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2379 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2380 float* out_ptr = out_data.get();
2381 if (out_rate != ref_rate) {
2382 // Resample the output back to its internal processing rate if
2383 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002384 ASSERT_EQ(ref_length,
2385 static_cast<size_t>(resampler.Resample(
2386 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002387 out_ptr = cmp_data.get();
2388 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002389
ekmeyerson60d9b332015-08-14 10:35:55 -07002390 // Update the |sq_error| and |variance| accumulators with the highest
2391 // SNR of reference vs output.
2392 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2393 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002394 }
2395
ekmeyerson60d9b332015-08-14 10:35:55 -07002396 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2397 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2398 << cf[i].num_input << ", " << cf[i].num_output << ", "
2399 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2400 << ", " << file_direction << "): ";
2401 if (sq_error > 0) {
2402 double snr = 10 * log10(variance / sq_error);
2403 EXPECT_GE(snr, expected_snr);
2404 EXPECT_NE(0, expected_snr);
2405 std::cout << "SNR=" << snr << " dB" << std::endl;
2406 } else {
aluebs776593b2016-03-15 14:04:58 -07002407 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002408 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409
ekmeyerson60d9b332015-08-14 10:35:55 -07002410 fclose(out_file);
2411 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002412 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002413 }
2414}
2415
2416#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002417INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002418 CommonFormats,
2419 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002420 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2421 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2422 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2423 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2424 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2425 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2426 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2427 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2428 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2429 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2430 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2431 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002432
Edward Lemurc5ee9872017-10-23 23:33:04 +02002433 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2434 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2435 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2436 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2437 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2438 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2439 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2440 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2441 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2442 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2443 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2444 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002445
Edward Lemurc5ee9872017-10-23 23:33:04 +02002446 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
Per Ã…hgren200feba2019-03-06 04:16:46 +01002447 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
Edward Lemurc5ee9872017-10-23 23:33:04 +02002448 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
Per Ã…hgren200feba2019-03-06 04:16:46 +01002449 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2450 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2451 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
Edward Lemurc5ee9872017-10-23 23:33:04 +02002452 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2453 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2454 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2455 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2456 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2457 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002458
Per Ã…hgren200feba2019-03-06 04:16:46 +01002459 std::make_tuple(16000, 48000, 48000, 48000, 24, 0),
2460 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2461 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
Edward Lemurc5ee9872017-10-23 23:33:04 +02002462 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2463 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2464 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2465 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2466 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2467 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Ã…hgren200feba2019-03-06 04:16:46 +01002468 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
Edward Lemurc5ee9872017-10-23 23:33:04 +02002469 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2470 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002471
2472#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002473INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 CommonFormats,
2475 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002476 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2477 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2478 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2479 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2480 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2481 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2482 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2483 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2484 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2485 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2486 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2487 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002488
Edward Lemurc5ee9872017-10-23 23:33:04 +02002489 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2490 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2491 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2492 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2493 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2494 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2495 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2496 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2497 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2498 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2499 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2500 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002501
Edward Lemurc5ee9872017-10-23 23:33:04 +02002502 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2503 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2504 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2505 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2506 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2507 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2508 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2509 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2510 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2511 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2512 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2513 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002514
Edward Lemurc5ee9872017-10-23 23:33:04 +02002515 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2516 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2517 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2518 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2519 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2520 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2521 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2522 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2523 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2524 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2525 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2526 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002527#endif
2528
niklase@google.com470e71d2011-07-07 08:21:25 +00002529} // namespace
peahc19f3122016-10-07 14:54:10 -07002530
Alessio Bazzicac054e782018-04-16 12:10:09 +02002531TEST(RuntimeSettingTest, TestDefaultCtor) {
2532 auto s = AudioProcessing::RuntimeSetting();
2533 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2534}
2535
2536TEST(RuntimeSettingTest, TestCapturePreGain) {
2537 using Type = AudioProcessing::RuntimeSetting::Type;
2538 {
2539 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2540 EXPECT_EQ(Type::kCapturePreGain, s.type());
2541 float v;
2542 s.GetFloat(&v);
2543 EXPECT_EQ(1.25f, v);
2544 }
2545
2546#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2547 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2548#endif
2549}
2550
2551TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2552 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2553 auto s = AudioProcessing::RuntimeSetting();
2554 ASSERT_TRUE(q.Insert(&s));
2555 ASSERT_TRUE(q.Remove(&s));
2556 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2557}
2558
Sam Zackrisson0beac582017-09-25 12:04:02 +02002559TEST(ApmConfiguration, EnablePostProcessing) {
2560 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002561 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002562 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002563 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002564 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002565 rtc::scoped_refptr<AudioProcessing> apm =
2566 AudioProcessingBuilder()
2567 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002568 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002569
2570 AudioFrame audio;
2571 audio.num_channels_ = 1;
2572 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2573
2574 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002575 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002576}
2577
Alex Loiko5825aa62017-12-18 16:02:40 +01002578TEST(ApmConfiguration, EnablePreProcessing) {
2579 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002580 auto mock_pre_processor_ptr =
2581 new testing::NiceMock<test::MockCustomProcessing>();
2582 auto mock_pre_processor =
2583 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002584 rtc::scoped_refptr<AudioProcessing> apm =
2585 AudioProcessingBuilder()
2586 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002587 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002588
2589 AudioFrame audio;
2590 audio.num_channels_ = 1;
2591 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2592
2593 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2594 apm->ProcessReverseStream(&audio);
2595}
2596
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002597TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2598 // Verify that apm uses a capture analyzer if one is provided.
2599 auto mock_capture_analyzer_ptr =
2600 new testing::NiceMock<test::MockCustomAudioAnalyzer>();
2601 auto mock_capture_analyzer =
2602 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2603 rtc::scoped_refptr<AudioProcessing> apm =
2604 AudioProcessingBuilder()
2605 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2606 .Create();
2607
2608 AudioFrame audio;
2609 audio.num_channels_ = 1;
2610 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2611
2612 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(testing::_)).Times(1);
2613 apm->ProcessStream(&audio);
2614}
2615
Alex Loiko73ec0192018-05-15 10:52:28 +02002616TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2617 auto mock_pre_processor_ptr =
2618 new testing::NiceMock<test::MockCustomProcessing>();
2619 auto mock_pre_processor =
2620 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2621 rtc::scoped_refptr<AudioProcessing> apm =
2622 AudioProcessingBuilder()
2623 .SetRenderPreProcessing(std::move(mock_pre_processor))
2624 .Create();
2625 apm->SetRuntimeSetting(
2626 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2627
2628 // RuntimeSettings forwarded during 'Process*Stream' calls.
2629 // Therefore we have to make one such call.
2630 AudioFrame audio;
2631 audio.num_channels_ = 1;
2632 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2633
2634 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2635 apm->ProcessReverseStream(&audio);
2636}
2637
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002638class MyEchoControlFactory : public EchoControlFactory {
2639 public:
2640 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2641 auto ec = new test::MockEchoControl();
2642 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2643 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2644 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2645 return std::unique_ptr<EchoControl>(ec);
2646 }
2647};
2648
2649TEST(ApmConfiguration, EchoControlInjection) {
2650 // Verify that apm uses an injected echo controller if one is provided.
2651 webrtc::Config webrtc_config;
2652 std::unique_ptr<EchoControlFactory> echo_control_factory(
2653 new MyEchoControlFactory());
2654
Alex Loiko5825aa62017-12-18 16:02:40 +01002655 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002656 AudioProcessingBuilder()
2657 .SetEchoControlFactory(std::move(echo_control_factory))
2658 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002659
2660 AudioFrame audio;
2661 audio.num_channels_ = 1;
2662 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2663 apm->ProcessStream(&audio);
2664 apm->ProcessReverseStream(&audio);
2665 apm->ProcessStream(&audio);
2666}
Ivo Creusenae026092017-11-20 13:07:16 +01002667
2668std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2669 Config old_config;
2670 if (use_AEC2) {
2671 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2672 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2673 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002674 std::unique_ptr<AudioProcessing> apm(
2675 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002676 if (!apm) {
2677 return apm;
2678 }
2679
2680 ProcessingConfig processing_config = {
2681 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2682
2683 if (apm->Initialize(processing_config) != 0) {
2684 return nullptr;
2685 }
2686
2687 // Disable all components except for an AEC and the residual echo detector.
Per Ã…hgren200feba2019-03-06 04:16:46 +01002688 // TODO(peah): Update this to also work on AEC3.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002689 AudioProcessing::Config apm_config;
2690 apm_config.residual_echo_detector.enabled = true;
2691 apm_config.high_pass_filter.enabled = false;
2692 apm_config.gain_controller2.enabled = false;
2693 apm_config.echo_canceller.enabled = true;
2694 apm_config.echo_canceller.mobile_mode = !use_AEC2;
Per Ã…hgren200feba2019-03-06 04:16:46 +01002695 apm_config.echo_canceller.use_legacy_aec = use_AEC2;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002696 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002697 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2698 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2699 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2700 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002701 return apm;
2702}
2703
2704#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2705#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2706#else
2707#define MAYBE_ApmStatistics ApmStatistics
2708#endif
2709
2710TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2711 // Set up APM with AEC2 and process some audio.
2712 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2713 ASSERT_TRUE(apm);
Per Ã…hgren200feba2019-03-06 04:16:46 +01002714 AudioProcessing::Config apm_config;
2715 apm_config.echo_canceller.enabled = true;
2716 // TODO(peah): Update tests to instead use AEC3.
2717 apm_config.echo_canceller.use_legacy_aec = true;
2718 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002719
2720 // Set up an audioframe.
2721 AudioFrame frame;
2722 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002723 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002724
2725 // Fill the audio frame with a sawtooth pattern.
2726 int16_t* ptr = frame.mutable_data();
2727 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2728 ptr[i] = 10000 * ((i % 3) - 1);
2729 }
2730
2731 // Do some processing.
2732 for (int i = 0; i < 200; i++) {
2733 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2734 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2735 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2736 }
2737
2738 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002739 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002740 // We expect all statistics to be set and have a sensible value.
2741 ASSERT_TRUE(stats.residual_echo_likelihood);
2742 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2743 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2744 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2745 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2746 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2747 ASSERT_TRUE(stats.echo_return_loss);
2748 EXPECT_NE(*stats.echo_return_loss, -100.0);
2749 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2750 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2751 ASSERT_TRUE(stats.divergent_filter_fraction);
2752 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2753 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2754 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2755 // We don't check stats.delay_median_ms since it takes too long to settle to a
2756 // value. At least 20 seconds of data need to be processed before it will get
2757 // a value, which would make this test take too much time.
2758
2759 // If there are no receive streams, we expect the stats not to be set. The
2760 // 'false' argument signals to APM that no receive streams are currently
2761 // active. In that situation the statistics would get stuck at their last
2762 // calculated value (AEC and echo detection need at least one stream in each
2763 // direction), so to avoid that, they should not be set by APM.
2764 stats = apm->GetStatistics(false);
2765 EXPECT_FALSE(stats.residual_echo_likelihood);
2766 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2767 EXPECT_FALSE(stats.echo_return_loss);
2768 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2769 EXPECT_FALSE(stats.divergent_filter_fraction);
2770 EXPECT_FALSE(stats.delay_median_ms);
2771 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2772}
2773
2774TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2775 // Set up APM with AECM and process some audio.
2776 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2777 ASSERT_TRUE(apm);
2778
2779 // Set up an audioframe.
2780 AudioFrame frame;
2781 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002782 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002783
2784 // Fill the audio frame with a sawtooth pattern.
2785 int16_t* ptr = frame.mutable_data();
2786 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2787 ptr[i] = 10000 * ((i % 3) - 1);
2788 }
2789
2790 // Do some processing.
2791 for (int i = 0; i < 200; i++) {
2792 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2793 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2794 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2795 }
2796
2797 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002798 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002799 // We expect only the residual echo detector statistics to be set and have a
2800 // sensible value.
2801 EXPECT_TRUE(stats.residual_echo_likelihood);
2802 if (stats.residual_echo_likelihood) {
2803 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2804 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2805 }
2806 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2807 if (stats.residual_echo_likelihood_recent_max) {
2808 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2809 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2810 }
2811 EXPECT_FALSE(stats.echo_return_loss);
2812 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2813 EXPECT_FALSE(stats.divergent_filter_fraction);
2814 EXPECT_FALSE(stats.delay_median_ms);
2815 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2816
2817 // If there are no receive streams, we expect the stats not to be set.
2818 stats = apm->GetStatistics(false);
2819 EXPECT_FALSE(stats.residual_echo_likelihood);
2820 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2821 EXPECT_FALSE(stats.echo_return_loss);
2822 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2823 EXPECT_FALSE(stats.divergent_filter_fraction);
2824 EXPECT_FALSE(stats.delay_median_ms);
2825 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2826}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002827
2828TEST(ApmStatistics, ReportOutputRmsDbfs) {
2829 ProcessingConfig processing_config = {
2830 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2831 AudioProcessing::Config config;
2832
2833 // Set up an audioframe.
2834 AudioFrame frame;
2835 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002836 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002837
2838 // Fill the audio frame with a sawtooth pattern.
2839 int16_t* ptr = frame.mutable_data();
2840 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2841 ptr[i] = 10000 * ((i % 3) - 1);
2842 }
2843
2844 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2845 apm->Initialize(processing_config);
2846
2847 // If not enabled, no metric should be reported.
2848 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2849 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2850
2851 // If enabled, metrics should be reported.
2852 config.level_estimation.enabled = true;
2853 apm->ApplyConfig(config);
2854 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2855 auto stats = apm->GetStatistics(false);
2856 EXPECT_TRUE(stats.output_rms_dbfs);
2857 EXPECT_GE(*stats.output_rms_dbfs, 0);
2858
2859 // If re-disabled, the value is again not reported.
2860 config.level_estimation.enabled = false;
2861 apm->ApplyConfig(config);
2862 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2863 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2864}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002865
2866TEST(ApmStatistics, ReportHasVoice) {
2867 ProcessingConfig processing_config = {
2868 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2869 AudioProcessing::Config config;
2870
2871 // Set up an audioframe.
2872 AudioFrame frame;
2873 frame.num_channels_ = 1;
2874 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2875
2876 // Fill the audio frame with a sawtooth pattern.
2877 int16_t* ptr = frame.mutable_data();
2878 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2879 ptr[i] = 10000 * ((i % 3) - 1);
2880 }
2881
2882 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2883 apm->Initialize(processing_config);
2884
2885 // If not enabled, no metric should be reported.
2886 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2887 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2888
2889 // If enabled, metrics should be reported.
2890 config.voice_detection.enabled = true;
2891 apm->ApplyConfig(config);
2892 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2893 auto stats = apm->GetStatistics(false);
2894 EXPECT_TRUE(stats.voice_detected);
2895
2896 // If re-disabled, the value is again not reported.
2897 config.voice_detection.enabled = false;
2898 apm->ApplyConfig(config);
2899 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2900 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2901}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002902} // namespace webrtc