blob: 3bf6fd263eb348aad1561ed6567259b1412d5315 [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"
24#include "modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
25#include "modules/audio_processing/common.h"
26#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020027#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/level_controller/level_controller_constants.h"
29#include "modules/audio_processing/test/protobuf_utils.h"
30#include "modules/audio_processing/test/test_utils.h"
31#include "modules/include/module_common_types.h"
32#include "rtc_base/arraysize.h"
33#include "rtc_base/checks.h"
34#include "rtc_base/gtest_prod_util.h"
35#include "rtc_base/ignore_wundef.h"
36#include "rtc_base/protobuf_utils.h"
Niels Möllerbf6937a2017-10-03 10:42:04 +020037#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/safe_minmax.h"
39#include "rtc_base/task_queue.h"
40#include "rtc_base/thread.h"
41#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/gtest.h"
43#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070044
45RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000046#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000047#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#endif
kwiberg77eab702016-09-28 17:42:01 -070051RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000052
andrew@webrtc.org27c69802014-02-18 20:24:56 +000053namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000054namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000055
ekmeyerson60d9b332015-08-14 10:35:55 -070056// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
57// applicable.
58
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000059// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000060// re-implemented; see comment at the bottom of this file. For now, the user has
61// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000062// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000063// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000064// be set to true with the command-line switch --write_ref_data.
65bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070066const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070067const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000068
aluebseb3603b2016-04-20 15:27:58 -070069#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
70// Android doesn't support 48kHz.
71const int kProcessSampleRates[] = {8000, 16000, 32000};
72#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070073const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070074#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000075
ekmeyerson60d9b332015-08-14 10:35:55 -070076enum StreamDirection { kForward = 0, kReverse };
77
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000078void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000079 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080 cb->num_channels());
81 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000082 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000083 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000084 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080085 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000086 S16ToFloat(cb_int.channels()[i],
87 cb->num_frames(),
88 cb->channels()[i]);
89 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000090}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000091
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000092void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070093 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094}
95
andrew@webrtc.org103657b2014-04-24 18:28:56 +000096// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080097size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098 switch (layout) {
99 case AudioProcessing::kMono:
100 return 1;
101 case AudioProcessing::kMonoAndKeyboard:
102 case AudioProcessing::kStereo:
103 return 2;
104 case AudioProcessing::kStereoAndKeyboard:
105 return 3;
106 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700107 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800108 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000109}
110
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000111int TruncateToMultipleOf10(int value) {
112 return (value / 10) * 10;
113}
114
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800116 size_t samples_per_channel) {
117 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000118 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000119}
120
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800122 size_t samples_per_channel) {
123 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000124 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
125}
126
pkasting25702cb2016-01-08 13:50:27 -0800127void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
128 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000129 stereo[i * 2 + 1] = stereo[i * 2];
130 }
131}
132
yujo36b1a5f2017-06-12 12:45:32 -0700133void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800134 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000135 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
136 }
137}
138
139void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700140 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700141 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
142 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700143 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000144 }
145}
146
147void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800148 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700149 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700150 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700151 frame_data[i] = left;
152 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000153 }
154}
155
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000156void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700157 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700158 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
159 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700160 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000161 }
162}
163
andrew@webrtc.org81865342012-10-27 00:28:27 +0000164bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000165 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166 return false;
167 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000168 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000169 return false;
170 }
yujo36b1a5f2017-06-12 12:45:32 -0700171 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000172 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000173 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000174 return false;
175 }
176 return true;
177}
178
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000179void EnableAllAPComponents(AudioProcessing* ap) {
180#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
181 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
182
183 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
184 EXPECT_NOERR(ap->gain_control()->Enable(true));
185#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
186 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
187 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
188 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
189 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
190
191 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
192 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
193 EXPECT_NOERR(ap->gain_control()->Enable(true));
194#endif
195
peah8271d042016-11-22 07:24:52 -0800196 AudioProcessing::Config apm_config;
197 apm_config.high_pass_filter.enabled = true;
198 ap->ApplyConfig(apm_config);
199
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000200 EXPECT_NOERR(ap->level_estimator()->Enable(true));
201 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
202
203 EXPECT_NOERR(ap->voice_detection()->Enable(true));
204}
205
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000206// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000207template <class T>
208T AbsValue(T a) {
209 return a > 0 ? a: -a;
210}
211
212int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800213 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700214 const int16_t* frame_data = frame.data();
215 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800216 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700217 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000218 }
219
220 return max_data;
221}
222
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000223#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000224void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000225 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700226 EXPECT_EQ(reference.instant(), test.instant);
227 EXPECT_EQ(reference.average(), test.average);
228 EXPECT_EQ(reference.maximum(), test.maximum);
229 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230}
231
232void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 audioproc::Test::Statistic* msg) {
234 msg->set_instant(output.instant);
235 msg->set_average(output.average);
236 msg->set_maximum(output.maximum);
237 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000238}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000239#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240
Alex Loiko890988c2017-08-31 10:25:48 +0200241void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700242 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000244 ASSERT_TRUE(file != NULL);
245
246 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000247 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800248 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000249 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000250
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000251 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000252 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000253 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000254 fclose(file);
255}
256
Alex Loiko890988c2017-08-31 10:25:48 +0200257std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000258 std::ostringstream ss;
259 // Resource files are all stereo.
260 ss << name << sample_rate_hz / 1000 << "_stereo";
261 return test::ResourcePath(ss.str(), "pcm");
262}
263
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000264// Temporary filenames unique to this process. Used to be able to run these
265// tests in parallel as each process needs to be running in isolation they can't
266// have competing filenames.
267std::map<std::string, std::string> temp_filenames;
268
Alex Loiko890988c2017-08-31 10:25:48 +0200269std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000270 int input_rate,
271 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700272 int reverse_input_rate,
273 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800274 size_t num_input_channels,
275 size_t num_output_channels,
276 size_t num_reverse_input_channels,
277 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700280 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
281 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282 if (num_output_channels == 1) {
283 ss << "mono";
284 } else if (num_output_channels == 2) {
285 ss << "stereo";
286 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700287 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000288 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700289 ss << output_rate / 1000;
290 if (num_reverse_output_channels == 1) {
291 ss << "_rmono";
292 } else if (num_reverse_output_channels == 2) {
293 ss << "_rstereo";
294 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700295 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700296 }
297 ss << reverse_output_rate / 1000;
298 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000299
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000300 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700301 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000302 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
303 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000304}
305
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000306void ClearTempFiles() {
307 for (auto& kv : temp_filenames)
308 remove(kv.second.c_str());
309}
310
Alex Loiko890988c2017-08-31 10:25:48 +0200311void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000312 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000313 ASSERT_TRUE(file != NULL);
314 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000315 fclose(file);
316}
317
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000318// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
319// stereo) file, converts to deinterleaved float (optionally downmixing) and
320// returns the result in |cb|. Returns false if the file ended (or on error) and
321// true otherwise.
322//
323// |int_data| and |float_data| are just temporary space that must be
324// sufficiently large to hold the 10 ms chunk.
325bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
326 ChannelBuffer<float>* cb) {
327 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000328 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000329 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
330 if (read_count != frame_size) {
331 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700332 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000333 return false; // This is expected.
334 }
335
336 S16ToFloat(int_data, frame_size, float_data);
337 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000338 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000339 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000340 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000341 cb->channels());
342 }
343
344 return true;
345}
346
niklase@google.com470e71d2011-07-07 08:21:25 +0000347class ApmTest : public ::testing::Test {
348 protected:
349 ApmTest();
350 virtual void SetUp();
351 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000352
353 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000354 }
355
356 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000357 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000358 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000359
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000360 // Used to select between int and float interface tests.
361 enum Format {
362 kIntFormat,
363 kFloatFormat
364 };
365
366 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000367 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000368 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800369 size_t num_input_channels,
370 size_t num_output_channels,
371 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000372 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000373 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000374 void EnableAllComponents();
375 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000376 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000377 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000378 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
379 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000380 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000381 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
382 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700383 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800384 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800386 void TestChangingForwardChannels(size_t num_in_channels,
387 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700388 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800389 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000391 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
392 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000394 int ProcessStreamChooser(Format format);
395 int AnalyzeReverseStreamChooser(Format format);
396 void ProcessDebugDump(const std::string& in_filename,
397 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800398 Format format,
399 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000400 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000401
402 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800404 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000405 AudioFrame* frame_;
406 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800407 std::unique_ptr<ChannelBuffer<float> > float_cb_;
408 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800410 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 FILE* far_file_;
412 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000413 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414};
415
416ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000417 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000418#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800419 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
420 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000421#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000422#if defined(WEBRTC_MAC)
423 // A different file for Mac is needed because on this platform the AEC
424 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800425 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
426 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000427#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800428 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
429 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000430#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000431#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000433 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000434 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000435 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000436 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000437 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000438 out_file_(NULL) {
439 Config config;
440 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
441 apm_.reset(AudioProcessing::Create(config));
442}
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
444void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000445 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
447 frame_ = new AudioFrame();
448 revframe_ = new AudioFrame();
449
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000450 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
453void ApmTest::TearDown() {
454 if (frame_) {
455 delete frame_;
456 }
457 frame_ = NULL;
458
459 if (revframe_) {
460 delete revframe_;
461 }
462 revframe_ = NULL;
463
464 if (far_file_) {
465 ASSERT_EQ(0, fclose(far_file_));
466 }
467 far_file_ = NULL;
468
469 if (near_file_) {
470 ASSERT_EQ(0, fclose(near_file_));
471 }
472 near_file_ = NULL;
473
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000474 if (out_file_) {
475 ASSERT_EQ(0, fclose(out_file_));
476 }
477 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478}
479
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000480void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000481 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700482 ap->Initialize(
483 {{{frame_->sample_rate_hz_, frame_->num_channels_},
484 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700485 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700486 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000487}
488
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000489void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000490 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000491 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800492 size_t num_input_channels,
493 size_t num_output_channels,
494 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000495 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000496 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000497 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000498 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000499
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000500 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
501 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000502 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000503
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000504 if (far_file_) {
505 ASSERT_EQ(0, fclose(far_file_));
506 }
507 std::string filename = ResourceFilePath("far", sample_rate_hz);
508 far_file_ = fopen(filename.c_str(), "rb");
509 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
510 filename << "\n";
511
512 if (near_file_) {
513 ASSERT_EQ(0, fclose(near_file_));
514 }
515 filename = ResourceFilePath("near", sample_rate_hz);
516 near_file_ = fopen(filename.c_str(), "rb");
517 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
518 filename << "\n";
519
520 if (open_output_file) {
521 if (out_file_) {
522 ASSERT_EQ(0, fclose(out_file_));
523 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700524 filename = OutputFilePath(
525 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
526 reverse_sample_rate_hz, num_input_channels, num_output_channels,
527 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000528 out_file_ = fopen(filename.c_str(), "wb");
529 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
530 filename << "\n";
531 }
532}
533
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000534void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000535 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000536}
537
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000538bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
539 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000540 // The files always contain stereo audio.
541 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700542 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000543 sizeof(int16_t),
544 frame_size,
545 file);
546 if (read_count != frame_size) {
547 // Check that the file really ended.
548 EXPECT_NE(0, feof(file));
549 return false; // This is expected.
550 }
551
552 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700553 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000554 frame->samples_per_channel_);
555 }
556
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000558 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000559 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000560 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000561}
562
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000563bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
564 return ReadFrame(file, frame, NULL);
565}
566
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000567// If the end of the file has been reached, rewind it and attempt to read the
568// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000569void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
570 ChannelBuffer<float>* cb) {
571 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000572 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000574 }
575}
576
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
578 ReadFrameWithRewind(file, frame, NULL);
579}
580
andrew@webrtc.org81865342012-10-27 00:28:27 +0000581void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
582 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000583 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000584 EXPECT_EQ(apm_->kNoError,
585 apm_->gain_control()->set_stream_analog_level(127));
586 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000587}
588
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000589int ApmTest::ProcessStreamChooser(Format format) {
590 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591 return apm_->ProcessStream(frame_);
592 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593 return apm_->ProcessStream(float_cb_->channels(),
594 frame_->samples_per_channel_,
595 frame_->sample_rate_hz_,
596 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000597 output_sample_rate_hz_,
598 LayoutFromChannels(num_output_channels_),
599 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000600}
601
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000602int ApmTest::AnalyzeReverseStreamChooser(Format format) {
603 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700604 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000605 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000606 return apm_->AnalyzeReverseStream(
607 revfloat_cb_->channels(),
608 revframe_->samples_per_channel_,
609 revframe_->sample_rate_hz_,
610 LayoutFromChannels(revframe_->num_channels_));
611}
612
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000613void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
614 int delay_min, int delay_max) {
615 // The |revframe_| and |frame_| should include the proper frame information,
616 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000617 AudioFrame tmp_frame;
618 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000619 bool causal = true;
620
621 tmp_frame.CopyFrom(*revframe_);
622 SetFrameTo(&tmp_frame, 0);
623
624 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
625 // Initialize the |frame_queue| with empty frames.
626 int frame_delay = delay_ms / 10;
627 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000628 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000629 frame->CopyFrom(tmp_frame);
630 frame_queue.push(frame);
631 frame_delay++;
632 causal = false;
633 }
634 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000635 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000636 frame->CopyFrom(tmp_frame);
637 frame_queue.push(frame);
638 frame_delay--;
639 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000640 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
641 // need enough frames with audio to have reliable estimates, but as few as
642 // possible to keep processing time down. 4.5 seconds seemed to be a good
643 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000644 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000645 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000646 frame->CopyFrom(tmp_frame);
647 // Use the near end recording, since that has more speech in it.
648 ASSERT_TRUE(ReadFrame(near_file_, frame));
649 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000650 AudioFrame* reverse_frame = frame;
651 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000652 if (!causal) {
653 reverse_frame = frame_queue.front();
654 // When we call ProcessStream() the frame is modified, so we can't use the
655 // pointer directly when things are non-causal. Use an intermediate frame
656 // and copy the data.
657 process_frame = &tmp_frame;
658 process_frame->CopyFrom(*frame);
659 }
aluebsb0319552016-03-17 20:39:53 -0700660 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000661 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
662 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
663 frame = frame_queue.front();
664 frame_queue.pop();
665 delete frame;
666
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000667 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000668 int median;
669 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000670 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000671 // Discard the first delay metrics to avoid convergence effects.
672 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000673 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
674 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000675 }
676 }
677
678 rewind(near_file_);
679 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000680 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000681 frame_queue.pop();
682 delete frame;
683 }
684 // Calculate expected delay estimate and acceptable regions. Further,
685 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700686 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700687 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700688 const int expected_median =
689 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
690 const int expected_median_high = rtc::SafeClamp<int>(
691 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700692 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700693 const int expected_median_low = rtc::SafeClamp<int>(
694 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700695 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000696 // Verify delay metrics.
697 int median;
698 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000699 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000700 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000701 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
702 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000703 EXPECT_GE(expected_median_high, median);
704 EXPECT_LE(expected_median_low, median);
705}
706
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000707void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000708 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000709 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000710
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000711 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000713 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000714 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000715
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 EXPECT_EQ(apm_->kNoError,
718 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000719 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000720 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000721 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000723 // Other stream parameters set correctly.
724 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 EXPECT_EQ(apm_->kNoError,
726 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000728 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000731 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
732 EXPECT_EQ(apm_->kNoError,
733 apm_->echo_cancellation()->enable_drift_compensation(false));
734
735 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000736 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000737 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000738 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000740
741 // Resets after successful ProcessStream().
742 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000743 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000744 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000745 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000746
747 // Other stream parameters set correctly.
748 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
749 EXPECT_EQ(apm_->kNoError,
750 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000751 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000752 EXPECT_EQ(apm_->kNoError,
753 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000754 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000756 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
757
758 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000759 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000760 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000761
762 // Resets after successful ProcessStream().
763 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000764 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000765 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000766 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000767 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000768
769 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000770 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
771 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
772 EXPECT_EQ(apm_->kNoError,
773 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000774 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000775 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000776
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000777 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000778 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000779 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000783 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000784 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000785 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 EXPECT_EQ(apm_->kNoError,
787 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000788 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000789}
790
791TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000792 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000793}
794
795TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000796 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000799TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
800 EXPECT_EQ(0, apm_->delay_offset_ms());
801 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
802 EXPECT_EQ(50, apm_->stream_delay_ms());
803}
804
805TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
806 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000807 apm_->set_delay_offset_ms(100);
808 EXPECT_EQ(100, apm_->delay_offset_ms());
809 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000810 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000811 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
812 EXPECT_EQ(200, apm_->stream_delay_ms());
813
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000814 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000815 apm_->set_delay_offset_ms(-50);
816 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000817 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
818 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000819 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
820 EXPECT_EQ(50, apm_->stream_delay_ms());
821}
822
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800824 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000826 frame_->num_channels_ = num_channels;
827 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700828 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000829}
830
Michael Graczyk86c6d332015-07-23 11:41:39 -0700831void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800832 size_t num_in_channels,
833 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700834 AudioProcessing::Error expected_return) {
835 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
836 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
837
838 EXPECT_EQ(expected_return,
839 apm_->ProcessStream(float_cb_->channels(), input_stream,
840 output_stream, float_cb_->channels()));
841}
842
843void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800844 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700845 AudioProcessing::Error expected_return) {
846 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700847 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
848 {output_sample_rate_hz_, apm_->num_output_channels()},
849 {frame_->sample_rate_hz_, num_rev_channels},
850 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700851
ekmeyerson60d9b332015-08-14 10:35:55 -0700852 EXPECT_EQ(
853 expected_return,
854 apm_->ProcessReverseStream(
855 float_cb_->channels(), processing_config.reverse_input_stream(),
856 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700857}
858
859TEST_F(ApmTest, ChannelsInt16Interface) {
860 // Testing number of invalid and valid channels.
861 Init(16000, 16000, 16000, 4, 4, 4, false);
862
863 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
864
Peter Kasting69558702016-01-12 16:26:35 -0800865 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700866 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000867 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000868 }
869}
870
Michael Graczyk86c6d332015-07-23 11:41:39 -0700871TEST_F(ApmTest, Channels) {
872 // Testing number of invalid and valid channels.
873 Init(16000, 16000, 16000, 4, 4, 4, false);
874
875 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
876 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
877
Peter Kasting69558702016-01-12 16:26:35 -0800878 for (size_t i = 1; i < 4; ++i) {
879 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700880 // Output channels much be one or match input channels.
881 if (j == 1 || i == j) {
882 TestChangingForwardChannels(i, j, kNoErr);
883 TestChangingReverseChannels(i, kNoErr);
884
885 EXPECT_EQ(i, apm_->num_input_channels());
886 EXPECT_EQ(j, apm_->num_output_channels());
887 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800888 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700889 } else {
890 TestChangingForwardChannels(i, j,
891 AudioProcessing::kBadNumberChannelsError);
892 }
893 }
894 }
895}
896
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000897TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000899 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000900 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700902 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800903 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000904 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000905 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000906 }
907}
908
niklase@google.com470e71d2011-07-07 08:21:25 +0000909TEST_F(ApmTest, EchoCancellation) {
910 EXPECT_EQ(apm_->kNoError,
911 apm_->echo_cancellation()->enable_drift_compensation(true));
912 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
913 EXPECT_EQ(apm_->kNoError,
914 apm_->echo_cancellation()->enable_drift_compensation(false));
915 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
916
niklase@google.com470e71d2011-07-07 08:21:25 +0000917 EchoCancellation::SuppressionLevel level[] = {
918 EchoCancellation::kLowSuppression,
919 EchoCancellation::kModerateSuppression,
920 EchoCancellation::kHighSuppression,
921 };
pkasting25702cb2016-01-08 13:50:27 -0800922 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000923 EXPECT_EQ(apm_->kNoError,
924 apm_->echo_cancellation()->set_suppression_level(level[i]));
925 EXPECT_EQ(level[i],
926 apm_->echo_cancellation()->suppression_level());
927 }
928
929 EchoCancellation::Metrics metrics;
930 EXPECT_EQ(apm_->kNotEnabledError,
931 apm_->echo_cancellation()->GetMetrics(&metrics));
932
ivoc3e9a5372016-10-28 07:55:33 -0700933 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
934 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
935
niklase@google.com470e71d2011-07-07 08:21:25 +0000936 EXPECT_EQ(apm_->kNoError,
937 apm_->echo_cancellation()->enable_metrics(true));
938 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
939 EXPECT_EQ(apm_->kNoError,
940 apm_->echo_cancellation()->enable_metrics(false));
941 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
942
ivoc48dfab52016-10-28 03:29:31 -0700943 EXPECT_EQ(apm_->kNoError,
944 apm_->echo_cancellation()->enable_delay_logging(true));
945 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
946 EXPECT_EQ(apm_->kNoError,
947 apm_->echo_cancellation()->enable_delay_logging(false));
948 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
949
ivoc3e9a5372016-10-28 07:55:33 -0700950 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
951 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
952
953 int median = 0;
954 int std = 0;
955 float poor_fraction = 0;
956 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
957 &median, &std, &poor_fraction));
958
niklase@google.com470e71d2011-07-07 08:21:25 +0000959 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
960 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
961 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
962 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000963
964 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
965 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
966 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
967 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
968 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
969 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000970}
971
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000972TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000973 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000974 // Enable AEC only.
975 EXPECT_EQ(apm_->kNoError,
976 apm_->echo_cancellation()->enable_drift_compensation(false));
977 EXPECT_EQ(apm_->kNoError,
978 apm_->echo_cancellation()->enable_metrics(false));
979 EXPECT_EQ(apm_->kNoError,
980 apm_->echo_cancellation()->enable_delay_logging(true));
981 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000982 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700983 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000984 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000985
986 // Internally in the AEC the amount of lookahead the delay estimation can
987 // handle is 15 blocks and the maximum delay is set to 60 blocks.
988 const int kLookaheadBlocks = 15;
989 const int kMaxDelayBlocks = 60;
990 // The AEC has a startup time before it actually starts to process. This
991 // procedure can flush the internal far-end buffer, which of course affects
992 // the delay estimation. Therefore, we set a system_delay high enough to
993 // avoid that. The smallest system_delay you can report without flushing the
994 // buffer is 66 ms in 8 kHz.
995 //
996 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
997 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
998 // delay estimation. This should be noted though. In case of test failure,
999 // this could be the cause.
1000 const int kSystemDelayMs = 66;
1001 // Test a couple of corner cases and verify that the estimated delay is
1002 // within a valid region (set to +-1.5 blocks). Note that these cases are
1003 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001004 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001005 Init(kProcessSampleRates[i],
1006 kProcessSampleRates[i],
1007 kProcessSampleRates[i],
1008 2,
1009 2,
1010 2,
1011 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001012 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001013 const int num_ms_per_block =
1014 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001015 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1016 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1017
1018 // 1) Verify correct delay estimate at lookahead boundary.
1019 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1020 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1021 delay_max_ms);
1022 // 2) A delay less than maximum lookahead should give an delay estimate at
1023 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1024 delay_ms -= 20;
1025 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1026 delay_max_ms);
1027 // 3) Three values around zero delay. Note that we need to compensate for
1028 // the fake system_delay.
1029 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1030 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1031 delay_max_ms);
1032 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1033 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1034 delay_max_ms);
1035 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1036 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1037 delay_max_ms);
1038 // 4) Verify correct delay estimate at maximum delay boundary.
1039 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1040 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1041 delay_max_ms);
1042 // 5) A delay above the maximum delay should give an estimate at the
1043 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1044 delay_ms += 20;
1045 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1046 delay_max_ms);
1047 }
1048}
1049
niklase@google.com470e71d2011-07-07 08:21:25 +00001050TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001052 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1054 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1055
niklase@google.com470e71d2011-07-07 08:21:25 +00001056 // Toggle routing modes
1057 EchoControlMobile::RoutingMode mode[] = {
1058 EchoControlMobile::kQuietEarpieceOrHeadset,
1059 EchoControlMobile::kEarpiece,
1060 EchoControlMobile::kLoudEarpiece,
1061 EchoControlMobile::kSpeakerphone,
1062 EchoControlMobile::kLoudSpeakerphone,
1063 };
pkasting25702cb2016-01-08 13:50:27 -08001064 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 EXPECT_EQ(apm_->kNoError,
1066 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1067 EXPECT_EQ(mode[i],
1068 apm_->echo_control_mobile()->routing_mode());
1069 }
1070 // Turn comfort noise off/on
1071 EXPECT_EQ(apm_->kNoError,
1072 apm_->echo_control_mobile()->enable_comfort_noise(false));
1073 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1074 EXPECT_EQ(apm_->kNoError,
1075 apm_->echo_control_mobile()->enable_comfort_noise(true));
1076 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001077 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001078 const size_t echo_path_size =
1079 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001080 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1081 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001082 EXPECT_EQ(apm_->kNullPointerError,
1083 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1084 EXPECT_EQ(apm_->kNullPointerError,
1085 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1086 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001087 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001088 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001089 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001090 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001091 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001092 echo_path_in[i] = echo_path_out[i] + 1;
1093 }
1094 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001095 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001096 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001097 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1098 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001099 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001100 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1101 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001102 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001103 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1104 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001105
1106 // Process a few frames with NS in the default disabled state. This exercises
1107 // a different codepath than with it enabled.
1108 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1109 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1110 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1111 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1112
niklase@google.com470e71d2011-07-07 08:21:25 +00001113 // Turn AECM off
1114 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1115 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1116}
1117
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001118TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001119 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001120 EXPECT_EQ(apm_->kNoError,
1121 apm_->gain_control()->set_mode(
1122 apm_->gain_control()->mode()));
1123
1124 GainControl::Mode mode[] = {
1125 GainControl::kAdaptiveAnalog,
1126 GainControl::kAdaptiveDigital,
1127 GainControl::kFixedDigital
1128 };
pkasting25702cb2016-01-08 13:50:27 -08001129 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001130 EXPECT_EQ(apm_->kNoError,
1131 apm_->gain_control()->set_mode(mode[i]));
1132 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1133 }
1134 // Testing invalid target levels
1135 EXPECT_EQ(apm_->kBadParameterError,
1136 apm_->gain_control()->set_target_level_dbfs(-3));
1137 EXPECT_EQ(apm_->kBadParameterError,
1138 apm_->gain_control()->set_target_level_dbfs(-40));
1139 // Testing valid target levels
1140 EXPECT_EQ(apm_->kNoError,
1141 apm_->gain_control()->set_target_level_dbfs(
1142 apm_->gain_control()->target_level_dbfs()));
1143
1144 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001145 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001146 EXPECT_EQ(apm_->kNoError,
1147 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1148 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1149 }
1150
1151 // Testing invalid compression gains
1152 EXPECT_EQ(apm_->kBadParameterError,
1153 apm_->gain_control()->set_compression_gain_db(-1));
1154 EXPECT_EQ(apm_->kBadParameterError,
1155 apm_->gain_control()->set_compression_gain_db(100));
1156
1157 // Testing valid compression gains
1158 EXPECT_EQ(apm_->kNoError,
1159 apm_->gain_control()->set_compression_gain_db(
1160 apm_->gain_control()->compression_gain_db()));
1161
1162 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001163 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001164 EXPECT_EQ(apm_->kNoError,
1165 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1166 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1167 }
1168
1169 // Testing limiter off/on
1170 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1171 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1172 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1173 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1174
1175 // Testing invalid level limits
1176 EXPECT_EQ(apm_->kBadParameterError,
1177 apm_->gain_control()->set_analog_level_limits(-1, 512));
1178 EXPECT_EQ(apm_->kBadParameterError,
1179 apm_->gain_control()->set_analog_level_limits(100000, 512));
1180 EXPECT_EQ(apm_->kBadParameterError,
1181 apm_->gain_control()->set_analog_level_limits(512, -1));
1182 EXPECT_EQ(apm_->kBadParameterError,
1183 apm_->gain_control()->set_analog_level_limits(512, 100000));
1184 EXPECT_EQ(apm_->kBadParameterError,
1185 apm_->gain_control()->set_analog_level_limits(512, 255));
1186
1187 // Testing valid level limits
1188 EXPECT_EQ(apm_->kNoError,
1189 apm_->gain_control()->set_analog_level_limits(
1190 apm_->gain_control()->analog_level_minimum(),
1191 apm_->gain_control()->analog_level_maximum()));
1192
1193 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001194 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001195 EXPECT_EQ(apm_->kNoError,
1196 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1197 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1198 }
1199
1200 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001201 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001202 EXPECT_EQ(apm_->kNoError,
1203 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1204 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1205 }
1206
1207 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1208
1209 // Turn AGC off
1210 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1211 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1212}
1213
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001214void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001215 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001216 EXPECT_EQ(apm_->kNoError,
1217 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1218 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1219
1220 int out_analog_level = 0;
1221 for (int i = 0; i < 2000; ++i) {
1222 ReadFrameWithRewind(near_file_, frame_);
1223 // Ensure the audio is at a low level, so the AGC will try to increase it.
1224 ScaleFrame(frame_, 0.25);
1225
1226 // Always pass in the same volume.
1227 EXPECT_EQ(apm_->kNoError,
1228 apm_->gain_control()->set_stream_analog_level(100));
1229 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1230 out_analog_level = apm_->gain_control()->stream_analog_level();
1231 }
1232
1233 // Ensure the AGC is still able to reach the maximum.
1234 EXPECT_EQ(255, out_analog_level);
1235}
1236
1237// Verifies that despite volume slider quantization, the AGC can continue to
1238// increase its volume.
1239TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001240 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001241 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1242 }
1243}
1244
1245void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001246 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001247 EXPECT_EQ(apm_->kNoError,
1248 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1249 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1250
1251 int out_analog_level = 100;
1252 for (int i = 0; i < 1000; ++i) {
1253 ReadFrameWithRewind(near_file_, frame_);
1254 // Ensure the audio is at a low level, so the AGC will try to increase it.
1255 ScaleFrame(frame_, 0.25);
1256
1257 EXPECT_EQ(apm_->kNoError,
1258 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1259 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1260 out_analog_level = apm_->gain_control()->stream_analog_level();
1261 }
1262
1263 // Ensure the volume was raised.
1264 EXPECT_GT(out_analog_level, 100);
1265 int highest_level_reached = out_analog_level;
1266 // Simulate a user manual volume change.
1267 out_analog_level = 100;
1268
1269 for (int i = 0; i < 300; ++i) {
1270 ReadFrameWithRewind(near_file_, frame_);
1271 ScaleFrame(frame_, 0.25);
1272
1273 EXPECT_EQ(apm_->kNoError,
1274 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1275 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1276 out_analog_level = apm_->gain_control()->stream_analog_level();
1277 // Check that AGC respected the manually adjusted volume.
1278 EXPECT_LT(out_analog_level, highest_level_reached);
1279 }
1280 // Check that the volume was still raised.
1281 EXPECT_GT(out_analog_level, 100);
1282}
1283
1284TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001285 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001286 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1287 }
1288}
1289
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001290#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1291TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1292 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001293 const size_t kSamplesPerChannel =
1294 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001295 const size_t kNumInputChannels = 2;
1296 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001297 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001298 const float kScaleFactor = 0.25f;
1299 Config config;
1300 std::vector<webrtc::Point> geometry;
1301 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1302 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1303 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001304 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001305 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001306 std::unique_ptr<AudioProcessing> apm(
Sam Zackrisson0beac582017-09-25 12:04:02 +02001307 AudioProcessing::Create(config, nullptr, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001308 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1309 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1310 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001311 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1312 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001313 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1314 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001315 std::string filename = ResourceFilePath("far", kSampleRateHz);
1316 FILE* far_file = fopen(filename.c_str(), "rb");
1317 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1318 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1319 const int kDefaultCompressionGain =
1320 apm->gain_control()->compression_gain_db();
1321 bool is_target = false;
1322 EXPECT_CALL(*beamformer, is_target_present())
1323 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001324 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001325 ASSERT_TRUE(ReadChunk(far_file,
1326 int_data.get(),
1327 float_data.get(),
1328 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001329 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001330 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001331 src_buf.channels()[j][k] *= kScaleFactor;
1332 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001333 }
1334 EXPECT_EQ(kNoErr,
1335 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001336 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001337 kSampleRateHz,
1338 LayoutFromChannels(src_buf.num_channels()),
1339 kSampleRateHz,
1340 LayoutFromChannels(dest_buf.num_channels()),
1341 dest_buf.channels()));
1342 }
1343 EXPECT_EQ(kDefaultVolume,
1344 apm->gain_control()->stream_analog_level());
1345 EXPECT_EQ(kDefaultCompressionGain,
1346 apm->gain_control()->compression_gain_db());
1347 rewind(far_file);
1348 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001349 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001350 ASSERT_TRUE(ReadChunk(far_file,
1351 int_data.get(),
1352 float_data.get(),
1353 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001354 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001355 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001356 src_buf.channels()[j][k] *= kScaleFactor;
1357 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001358 }
1359 EXPECT_EQ(kNoErr,
1360 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001361 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001362 kSampleRateHz,
1363 LayoutFromChannels(src_buf.num_channels()),
1364 kSampleRateHz,
1365 LayoutFromChannels(dest_buf.num_channels()),
1366 dest_buf.channels()));
1367 }
1368 EXPECT_LT(kDefaultVolume,
1369 apm->gain_control()->stream_analog_level());
1370 EXPECT_LT(kDefaultCompressionGain,
1371 apm->gain_control()->compression_gain_db());
1372 ASSERT_EQ(0, fclose(far_file));
1373}
1374#endif
1375
niklase@google.com470e71d2011-07-07 08:21:25 +00001376TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001377 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001378 NoiseSuppression::Level level[] = {
1379 NoiseSuppression::kLow,
1380 NoiseSuppression::kModerate,
1381 NoiseSuppression::kHigh,
1382 NoiseSuppression::kVeryHigh
1383 };
pkasting25702cb2016-01-08 13:50:27 -08001384 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001385 EXPECT_EQ(apm_->kNoError,
1386 apm_->noise_suppression()->set_level(level[i]));
1387 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1388 }
1389
andrew@webrtc.org648af742012-02-08 01:57:29 +00001390 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1392 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1393 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1394 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1395}
1396
1397TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001398 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001399 AudioProcessing::Config apm_config;
1400 apm_config.high_pass_filter.enabled = true;
1401 apm_->ApplyConfig(apm_config);
1402 apm_config.high_pass_filter.enabled = false;
1403 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001404}
1405
1406TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001407 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001408 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001409 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001410
1411 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1412
1413 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1414 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1415
1416 // Run this test in wideband; in super-wb, the splitting filter distorts the
1417 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001418 frame_->samples_per_channel_ = 160;
1419 frame_->num_channels_ = 2;
1420 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001421
1422 // Min value if no frames have been processed.
1423 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1424
1425 // Min value on zero frames.
1426 SetFrameTo(frame_, 0);
1427 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1428 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1429 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1430
1431 // Try a few RMS values.
1432 // (These also test that the value resets after retrieving it.)
1433 SetFrameTo(frame_, 32767);
1434 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1435 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1436 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1437
1438 SetFrameTo(frame_, 30000);
1439 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1440 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1441 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1442
1443 SetFrameTo(frame_, 10000);
1444 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1445 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1446 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1447
1448 SetFrameTo(frame_, 10);
1449 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1450 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1451 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1452
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001453 // Verify reset after enable/disable.
1454 SetFrameTo(frame_, 32767);
1455 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1456 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1457 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1458 SetFrameTo(frame_, 1);
1459 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1460 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1461
1462 // Verify reset after initialize.
1463 SetFrameTo(frame_, 32767);
1464 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1465 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1466 SetFrameTo(frame_, 1);
1467 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1468 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001469}
1470
1471TEST_F(ApmTest, VoiceDetection) {
1472 // Test external VAD
1473 EXPECT_EQ(apm_->kNoError,
1474 apm_->voice_detection()->set_stream_has_voice(true));
1475 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1476 EXPECT_EQ(apm_->kNoError,
1477 apm_->voice_detection()->set_stream_has_voice(false));
1478 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1479
andrew@webrtc.org648af742012-02-08 01:57:29 +00001480 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001481 VoiceDetection::Likelihood likelihood[] = {
1482 VoiceDetection::kVeryLowLikelihood,
1483 VoiceDetection::kLowLikelihood,
1484 VoiceDetection::kModerateLikelihood,
1485 VoiceDetection::kHighLikelihood
1486 };
pkasting25702cb2016-01-08 13:50:27 -08001487 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001488 EXPECT_EQ(apm_->kNoError,
1489 apm_->voice_detection()->set_likelihood(likelihood[i]));
1490 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1491 }
1492
1493 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001494 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001495 EXPECT_EQ(apm_->kBadParameterError,
1496 apm_->voice_detection()->set_frame_size_ms(12));
1497
andrew@webrtc.org648af742012-02-08 01:57:29 +00001498 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001499 for (int i = 10; i <= 30; i += 10) {
1500 EXPECT_EQ(apm_->kNoError,
1501 apm_->voice_detection()->set_frame_size_ms(i));
1502 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1503 }
1504 */
1505
andrew@webrtc.org648af742012-02-08 01:57:29 +00001506 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001507 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1508 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1509 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1510 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1511
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001512 // Test that AudioFrame activity is maintained when VAD is disabled.
1513 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1514 AudioFrame::VADActivity activity[] = {
1515 AudioFrame::kVadActive,
1516 AudioFrame::kVadPassive,
1517 AudioFrame::kVadUnknown
1518 };
pkasting25702cb2016-01-08 13:50:27 -08001519 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001520 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001521 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001522 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001523 }
1524
1525 // Test that AudioFrame activity is set when VAD is enabled.
1526 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001527 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001528 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001529 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001530
niklase@google.com470e71d2011-07-07 08:21:25 +00001531 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1532}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001533
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001534TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1535 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1536 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1537 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1538 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1539 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1540 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1541 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1542}
1543
1544TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001545 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001546 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001547 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001548 AudioFrame frame_copy;
1549 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001550 for (int j = 0; j < 1000; j++) {
1551 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1552 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001553 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1554 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001555 }
1556 }
1557}
1558
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001559TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1560 // Test that ProcessStream copies input to output even with no processing.
1561 const size_t kSamples = 80;
1562 const int sample_rate = 8000;
1563 const float src[kSamples] = {
1564 -1.0f, 0.0f, 1.0f
1565 };
1566 float dest[kSamples] = {};
1567
1568 auto src_channels = &src[0];
1569 auto dest_channels = &dest[0];
1570
1571 apm_.reset(AudioProcessing::Create());
1572 EXPECT_NOERR(apm_->ProcessStream(
1573 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1574 sample_rate, LayoutFromChannels(1), &dest_channels));
1575
1576 for (size_t i = 0; i < kSamples; ++i) {
1577 EXPECT_EQ(src[i], dest[i]);
1578 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001579
1580 // Same for ProcessReverseStream.
1581 float rev_dest[kSamples] = {};
1582 auto rev_dest_channels = &rev_dest[0];
1583
1584 StreamConfig input_stream = {sample_rate, 1};
1585 StreamConfig output_stream = {sample_rate, 1};
1586 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1587 output_stream, &rev_dest_channels));
1588
1589 for (size_t i = 0; i < kSamples; ++i) {
1590 EXPECT_EQ(src[i], rev_dest[i]);
1591 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001592}
1593
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001594TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1595 EnableAllComponents();
1596
pkasting25702cb2016-01-08 13:50:27 -08001597 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001598 Init(kProcessSampleRates[i],
1599 kProcessSampleRates[i],
1600 kProcessSampleRates[i],
1601 2,
1602 2,
1603 2,
1604 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001605 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001606 ASSERT_EQ(0, feof(far_file_));
1607 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001608 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001609 CopyLeftToRightChannel(revframe_->mutable_data(),
1610 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001611
aluebsb0319552016-03-17 20:39:53 -07001612 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001613
yujo36b1a5f2017-06-12 12:45:32 -07001614 CopyLeftToRightChannel(frame_->mutable_data(),
1615 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001616 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1617
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001618 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001619 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001620 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001621 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001622 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001623 analog_level = apm_->gain_control()->stream_analog_level();
1624
yujo36b1a5f2017-06-12 12:45:32 -07001625 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001626 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001627 rewind(far_file_);
1628 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001629 }
1630}
1631
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001632TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001633 // Verify the filter is not active through undistorted audio when:
1634 // 1. No components are enabled...
1635 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001636 AudioFrame frame_copy;
1637 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001638 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1639 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1640 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1641
1642 // 2. Only the level estimator is enabled...
1643 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001644 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001645 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1646 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1647 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1648 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1649 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1650
1651 // 3. Only VAD is enabled...
1652 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001653 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001654 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1655 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1656 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1657 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1658 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1659
1660 // 4. Both VAD and the level estimator are enabled...
1661 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001662 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001663 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1664 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1665 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1666 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1667 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1668 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1669 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1670
1671 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001672 frame_->samples_per_channel_ = 160;
1673 frame_->num_channels_ = 2;
1674 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001675 // Enable AEC, which would require the filter in super-wb. We rely on the
1676 // first few frames of data being unaffected by the AEC.
1677 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1678 // behavior of the AEC. Think of something more robust.
1679 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001680 // Make sure we have extended filter enabled. This makes sure nothing is
1681 // touched until we have a farend frame.
1682 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001683 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001684 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001685 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001686 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001687 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001688 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001689 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1690 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001691 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001692 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1693 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1694
1695 // Check the test is valid. We should have distortion from the filter
1696 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001697 frame_->samples_per_channel_ = 320;
1698 frame_->num_channels_ = 2;
1699 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001700 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001701 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001702 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001703 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001704 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1705 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1706}
1707
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001708#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1709void ApmTest::ProcessDebugDump(const std::string& in_filename,
1710 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001711 Format format,
1712 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001713 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001714 FILE* in_file = fopen(in_filename.c_str(), "rb");
1715 ASSERT_TRUE(in_file != NULL);
1716 audioproc::Event event_msg;
1717 bool first_init = true;
1718
1719 while (ReadMessageFromFile(in_file, &event_msg)) {
1720 if (event_msg.type() == audioproc::Event::INIT) {
1721 const audioproc::Init msg = event_msg.init();
1722 int reverse_sample_rate = msg.sample_rate();
1723 if (msg.has_reverse_sample_rate()) {
1724 reverse_sample_rate = msg.reverse_sample_rate();
1725 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001726 int output_sample_rate = msg.sample_rate();
1727 if (msg.has_output_sample_rate()) {
1728 output_sample_rate = msg.output_sample_rate();
1729 }
1730
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001731 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001732 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001733 reverse_sample_rate,
1734 msg.num_input_channels(),
1735 msg.num_output_channels(),
1736 msg.num_reverse_channels(),
1737 false);
1738 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001739 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001740 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001741 auto aec_dump =
1742 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1743 EXPECT_TRUE(aec_dump);
1744 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001745 first_init = false;
1746 }
1747
1748 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1749 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1750
1751 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001752 ASSERT_EQ(revframe_->num_channels_,
1753 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001754 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001755 memcpy(revfloat_cb_->channels()[i],
1756 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001757 msg.channel(i).size());
1758 }
1759 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001760 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001761 if (format == kFloatFormat) {
1762 // We're using an int16 input file; convert to float.
1763 ConvertToFloat(*revframe_, revfloat_cb_.get());
1764 }
1765 }
1766 AnalyzeReverseStreamChooser(format);
1767
1768 } else if (event_msg.type() == audioproc::Event::STREAM) {
1769 const audioproc::Stream msg = event_msg.stream();
1770 // ProcessStream could have changed this for the output frame.
1771 frame_->num_channels_ = apm_->num_input_channels();
1772
1773 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1774 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1775 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1776 if (msg.has_keypress()) {
1777 apm_->set_stream_key_pressed(msg.keypress());
1778 } else {
1779 apm_->set_stream_key_pressed(true);
1780 }
1781
1782 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001783 ASSERT_EQ(frame_->num_channels_,
1784 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001785 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001786 memcpy(float_cb_->channels()[i],
1787 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001788 msg.input_channel(i).size());
1789 }
1790 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001791 memcpy(frame_->mutable_data(), msg.input_data().data(),
1792 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001793 if (format == kFloatFormat) {
1794 // We're using an int16 input file; convert to float.
1795 ConvertToFloat(*frame_, float_cb_.get());
1796 }
1797 }
1798 ProcessStreamChooser(format);
1799 }
1800 }
aleloif4dd1912017-06-15 01:55:38 -07001801 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001802 fclose(in_file);
1803}
1804
1805void ApmTest::VerifyDebugDumpTest(Format format) {
1806 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001807 std::string format_string;
1808 switch (format) {
1809 case kIntFormat:
1810 format_string = "_int";
1811 break;
1812 case kFloatFormat:
1813 format_string = "_float";
1814 break;
1815 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001816 const std::string ref_filename = test::TempFilename(
1817 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1818 const std::string out_filename = test::TempFilename(
1819 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001820 const std::string limited_filename = test::TempFilename(
1821 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1822 const size_t logging_limit_bytes = 100000;
1823 // We expect at least this many bytes in the created logfile.
1824 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001825 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001826 ProcessDebugDump(in_filename, ref_filename, format, -1);
1827 ProcessDebugDump(ref_filename, out_filename, format, -1);
1828 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001829
1830 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1831 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001832 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001833 ASSERT_TRUE(ref_file != NULL);
1834 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001835 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001836 std::unique_ptr<uint8_t[]> ref_bytes;
1837 std::unique_ptr<uint8_t[]> out_bytes;
1838 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001839
1840 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1841 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001842 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001843 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001844 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001845 while (ref_size > 0 && out_size > 0) {
1846 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001847 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001848 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001849 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001850 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001851 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001852 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1853 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001854 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001855 }
1856 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001857 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1858 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001859 EXPECT_NE(0, feof(ref_file));
1860 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001861 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001862 ASSERT_EQ(0, fclose(ref_file));
1863 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001864 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001865 remove(ref_filename.c_str());
1866 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001867 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001868}
1869
pbosc7a65692016-05-06 12:50:04 -07001870TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001871 VerifyDebugDumpTest(kIntFormat);
1872}
1873
pbosc7a65692016-05-06 12:50:04 -07001874TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001875 VerifyDebugDumpTest(kFloatFormat);
1876}
1877#endif
1878
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001879// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001880TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001881 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001882 const std::string filename =
1883 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001884 {
1885 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1886 EXPECT_FALSE(aec_dump);
1887 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001888
1889#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1890 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001891 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001892
aleloif4dd1912017-06-15 01:55:38 -07001893 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1894 EXPECT_TRUE(aec_dump);
1895 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001896 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001897 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001898 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001899
1900 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001901 FILE* fid = fopen(filename.c_str(), "r");
1902 ASSERT_TRUE(fid != NULL);
1903
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001904 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001905 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001906 ASSERT_EQ(0, remove(filename.c_str()));
1907#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001908 // Verify the file has NOT been written.
1909 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1910#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1911}
1912
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001913// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001914TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001915 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1916
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001917 const std::string filename =
1918 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001919 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001920 ASSERT_TRUE(fid);
1921
1922#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1923 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001924 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001925
aleloif4dd1912017-06-15 01:55:38 -07001926 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1927 EXPECT_TRUE(aec_dump);
1928 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001929 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001930 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001931 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001932
1933 // Verify the file has been written.
1934 fid = fopen(filename.c_str(), "r");
1935 ASSERT_TRUE(fid != NULL);
1936
1937 // Clean it up.
1938 ASSERT_EQ(0, fclose(fid));
1939 ASSERT_EQ(0, remove(filename.c_str()));
1940#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001941 ASSERT_EQ(0, fclose(fid));
1942#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1943}
1944
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001945TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001946 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001947 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001948
1949 Config config;
1950 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001951 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001952 EnableAllComponents();
1953 EnableAllAPComponents(fapm.get());
1954 for (int i = 0; i < ref_data.test_size(); i++) {
1955 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1956
1957 audioproc::Test* test = ref_data.mutable_test(i);
1958 // TODO(ajm): Restore downmixing test cases.
1959 if (test->num_input_channels() != test->num_output_channels())
1960 continue;
1961
Peter Kasting69558702016-01-12 16:26:35 -08001962 const size_t num_render_channels =
1963 static_cast<size_t>(test->num_reverse_channels());
1964 const size_t num_input_channels =
1965 static_cast<size_t>(test->num_input_channels());
1966 const size_t num_output_channels =
1967 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001968 const size_t samples_per_channel = static_cast<size_t>(
1969 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001970
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001971 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1972 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001973 Init(fapm.get());
1974
1975 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001976 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1977 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001978
1979 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001980 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001981 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1982 ReadFrame(near_file_, frame_, float_cb_.get())) {
1983 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1984
aluebsb0319552016-03-17 20:39:53 -07001985 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001986 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1987 revfloat_cb_->channels(),
1988 samples_per_channel,
1989 test->sample_rate(),
1990 LayoutFromChannels(num_render_channels)));
1991
1992 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1993 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1994 apm_->echo_cancellation()->set_stream_drift_samples(0);
1995 fapm->echo_cancellation()->set_stream_drift_samples(0);
1996 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1997 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1998
1999 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07002000 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002001 output_int16.channels());
2002
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002003 EXPECT_NOERR(fapm->ProcessStream(
2004 float_cb_->channels(),
2005 samples_per_channel,
2006 test->sample_rate(),
2007 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002008 test->sample_rate(),
2009 LayoutFromChannels(num_output_channels),
2010 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002011 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002012 FloatToS16(float_cb_->channels()[j],
2013 samples_per_channel,
2014 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002015 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002016 float snr = ComputeSNR(output_int16.channels()[j],
2017 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002018 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002019
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002020 const float kVarianceThreshold = 20;
2021 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002022
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002023 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002024 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2025 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002026 }
2027 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002028
2029 analog_level = fapm->gain_control()->stream_analog_level();
2030 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2031 fapm->gain_control()->stream_analog_level());
2032 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2033 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002034 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2035 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002036 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002037
2038 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002039 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002040 }
aluebs776593b2016-03-15 14:04:58 -07002041
2042#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2043 const size_t kMaxNumBadChunks = 0;
2044#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2045 // There are a few chunks in the fixed-point profile that give low SNR.
2046 // Listening confirmed the difference is acceptable.
2047 const size_t kMaxNumBadChunks = 60;
2048#endif
2049 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2050
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002051 rewind(far_file_);
2052 rewind(near_file_);
2053 }
2054}
2055
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002056// TODO(andrew): Add a test to process a few frames with different combinations
2057// of enabled components.
2058
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002059TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002060 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002061 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002062
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002063 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002064 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002065 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002066 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002067 for (size_t i = 0; i < arraysize(kChannels); i++) {
2068 for (size_t j = 0; j < arraysize(kChannels); j++) {
2069 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002070 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002071 test->set_num_reverse_channels(kChannels[i]);
2072 test->set_num_input_channels(kChannels[j]);
2073 test->set_num_output_channels(kChannels[j]);
2074 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002075 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002076 }
2077 }
2078 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002079#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2080 // To test the extended filter mode.
2081 audioproc::Test* test = ref_data.add_test();
2082 test->set_num_reverse_channels(2);
2083 test->set_num_input_channels(2);
2084 test->set_num_output_channels(2);
2085 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2086 test->set_use_aec_extended_filter(true);
2087#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002088 }
2089
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002090 for (int i = 0; i < ref_data.test_size(); i++) {
2091 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002092
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002093 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002094 // TODO(ajm): We no longer allow different input and output channels. Skip
2095 // these tests for now, but they should be removed from the set.
2096 if (test->num_input_channels() != test->num_output_channels())
2097 continue;
2098
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002099 Config config;
2100 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002101 config.Set<ExtendedFilter>(
2102 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002103 apm_.reset(AudioProcessing::Create(config));
2104
2105 EnableAllComponents();
2106
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002107 Init(test->sample_rate(),
2108 test->sample_rate(),
2109 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002110 static_cast<size_t>(test->num_input_channels()),
2111 static_cast<size_t>(test->num_output_channels()),
2112 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002113 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002114
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002115 int frame_count = 0;
2116 int has_echo_count = 0;
2117 int has_voice_count = 0;
2118 int is_saturated_count = 0;
2119 int analog_level = 127;
2120 int analog_level_average = 0;
2121 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002122 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002123#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2124 int stats_index = 0;
2125#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002126
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002127 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002128 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002129
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002130 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2131
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002132 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002133 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002134 EXPECT_EQ(apm_->kNoError,
2135 apm_->gain_control()->set_stream_analog_level(analog_level));
2136
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002137 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002138
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002139 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002140 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2141 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002142
2143 max_output_average += MaxAudioFrame(*frame_);
2144
2145 if (apm_->echo_cancellation()->stream_has_echo()) {
2146 has_echo_count++;
2147 }
2148
2149 analog_level = apm_->gain_control()->stream_analog_level();
2150 analog_level_average += analog_level;
2151 if (apm_->gain_control()->stream_is_saturated()) {
2152 is_saturated_count++;
2153 }
2154 if (apm_->voice_detection()->stream_has_voice()) {
2155 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002156 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002157 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002158 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002159 }
2160
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002161 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2162
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002163 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002164 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002165 sizeof(int16_t),
2166 frame_size,
2167 out_file_);
2168 ASSERT_EQ(frame_size, write_count);
2169
2170 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002171 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002172 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002173
2174#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2175 const int kStatsAggregationFrameNum = 100; // 1 second.
2176 if (frame_count % kStatsAggregationFrameNum == 0) {
2177 // Get echo metrics.
2178 EchoCancellation::Metrics echo_metrics;
2179 EXPECT_EQ(apm_->kNoError,
2180 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2181
2182 // Get delay metrics.
2183 int median = 0;
2184 int std = 0;
2185 float fraction_poor_delays = 0;
2186 EXPECT_EQ(apm_->kNoError,
2187 apm_->echo_cancellation()->GetDelayMetrics(
2188 &median, &std, &fraction_poor_delays));
2189
2190 // Get RMS.
2191 int rms_level = apm_->level_estimator()->RMS();
2192 EXPECT_LE(0, rms_level);
2193 EXPECT_GE(127, rms_level);
2194
2195 if (!write_ref_data) {
2196 const audioproc::Test::EchoMetrics& reference =
2197 test->echo_metrics(stats_index);
2198 TestStats(echo_metrics.residual_echo_return_loss,
2199 reference.residual_echo_return_loss());
2200 TestStats(echo_metrics.echo_return_loss,
2201 reference.echo_return_loss());
2202 TestStats(echo_metrics.echo_return_loss_enhancement,
2203 reference.echo_return_loss_enhancement());
2204 TestStats(echo_metrics.a_nlp,
2205 reference.a_nlp());
2206 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2207 reference.divergent_filter_fraction());
2208
2209 const audioproc::Test::DelayMetrics& reference_delay =
2210 test->delay_metrics(stats_index);
2211 EXPECT_EQ(reference_delay.median(), median);
2212 EXPECT_EQ(reference_delay.std(), std);
2213 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2214 fraction_poor_delays);
2215
2216 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2217
2218 ++stats_index;
2219 } else {
2220 audioproc::Test::EchoMetrics* message =
2221 test->add_echo_metrics();
2222 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2223 message->mutable_residual_echo_return_loss());
2224 WriteStatsMessage(echo_metrics.echo_return_loss,
2225 message->mutable_echo_return_loss());
2226 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2227 message->mutable_echo_return_loss_enhancement());
2228 WriteStatsMessage(echo_metrics.a_nlp,
2229 message->mutable_a_nlp());
2230 message->set_divergent_filter_fraction(
2231 echo_metrics.divergent_filter_fraction);
2232
2233 audioproc::Test::DelayMetrics* message_delay =
2234 test->add_delay_metrics();
2235 message_delay->set_median(median);
2236 message_delay->set_std(std);
2237 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2238
2239 test->add_rms_level(rms_level);
2240 }
2241 }
2242#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002243 }
2244 max_output_average /= frame_count;
2245 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002246 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002247
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002248 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002249 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002250 // When running the test on a N7 we get a {2, 6} difference of
2251 // |has_voice_count| and |max_output_average| is up to 18 higher.
2252 // All numbers being consistently higher on N7 compare to ref_data.
2253 // TODO(bjornv): If we start getting more of these offsets on Android we
2254 // should consider a different approach. Either using one slack for all,
2255 // or generate a separate android reference.
2256#if defined(WEBRTC_ANDROID)
2257 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002258 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002259 const int kMaxOutputAverageOffset = 9;
2260 const int kMaxOutputAverageNear = 9;
2261#else
2262 const int kHasVoiceCountOffset = 0;
2263 const int kHasVoiceCountNear = kIntNear;
2264 const int kMaxOutputAverageOffset = 0;
2265 const int kMaxOutputAverageNear = kIntNear;
2266#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002267 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002268 EXPECT_NEAR(test->has_voice_count(),
2269 has_voice_count - kHasVoiceCountOffset,
2270 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002271 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002272
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002273 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002274 EXPECT_NEAR(test->max_output_average(),
2275 max_output_average - kMaxOutputAverageOffset,
2276 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002277#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002278 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002279 EXPECT_NEAR(test->ns_speech_probability_average(),
2280 ns_speech_prob_average,
2281 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002282#endif
2283 } else {
2284 test->set_has_echo_count(has_echo_count);
2285 test->set_has_voice_count(has_voice_count);
2286 test->set_is_saturated_count(is_saturated_count);
2287
2288 test->set_analog_level_average(analog_level_average);
2289 test->set_max_output_average(max_output_average);
2290
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002291#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002292 EXPECT_LE(0.0f, ns_speech_prob_average);
2293 EXPECT_GE(1.0f, ns_speech_prob_average);
2294 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002295#endif
2296 }
2297
2298 rewind(far_file_);
2299 rewind(near_file_);
2300 }
2301
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002302 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002303 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002304 }
2305}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002306
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002307TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2308 struct ChannelFormat {
2309 AudioProcessing::ChannelLayout in_layout;
2310 AudioProcessing::ChannelLayout out_layout;
2311 };
2312 ChannelFormat cf[] = {
2313 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2314 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2315 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2316 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002317
kwiberg62eaacf2016-02-17 06:39:05 -08002318 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002319 // Enable one component just to ensure some processing takes place.
2320 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002321 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002322 const int in_rate = 44100;
2323 const int out_rate = 48000;
2324 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2325 TotalChannelsFromLayout(cf[i].in_layout));
2326 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2327 ChannelsFromLayout(cf[i].out_layout));
2328
2329 // Run over a few chunks.
2330 for (int j = 0; j < 10; ++j) {
2331 EXPECT_NOERR(ap->ProcessStream(
2332 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002333 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002334 in_rate,
2335 cf[i].in_layout,
2336 out_rate,
2337 cf[i].out_layout,
2338 out_cb.channels()));
2339 }
2340 }
2341}
2342
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002343// Compares the reference and test arrays over a region around the expected
2344// delay. Finds the highest SNR in that region and adds the variance and squared
2345// error results to the supplied accumulators.
2346void UpdateBestSNR(const float* ref,
2347 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002348 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002349 int expected_delay,
2350 double* variance_acc,
2351 double* sq_error_acc) {
2352 double best_snr = std::numeric_limits<double>::min();
2353 double best_variance = 0;
2354 double best_sq_error = 0;
2355 // Search over a region of eight samples around the expected delay.
2356 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2357 ++delay) {
2358 double sq_error = 0;
2359 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002360 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002361 double error = test[i + delay] - ref[i];
2362 sq_error += error * error;
2363 variance += ref[i] * ref[i];
2364 }
2365
2366 if (sq_error == 0) {
2367 *variance_acc += variance;
2368 return;
2369 }
2370 double snr = variance / sq_error;
2371 if (snr > best_snr) {
2372 best_snr = snr;
2373 best_variance = variance;
2374 best_sq_error = sq_error;
2375 }
2376 }
2377
2378 *variance_acc += best_variance;
2379 *sq_error_acc += best_sq_error;
2380}
2381
2382// Used to test a multitude of sample rate and channel combinations. It works
2383// by first producing a set of reference files (in SetUpTestCase) that are
2384// assumed to be correct, as the used parameters are verified by other tests
2385// in this collection. Primarily the reference files are all produced at
2386// "native" rates which do not involve any resampling.
2387
2388// Each test pass produces an output file with a particular format. The output
2389// is matched against the reference file closest to its internal processing
2390// format. If necessary the output is resampled back to its process format.
2391// Due to the resampling distortion, we don't expect identical results, but
2392// enforce SNR thresholds which vary depending on the format. 0 is a special
2393// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002394typedef std::tr1::tuple<int, int, int, int, double, double>
2395 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002396class AudioProcessingTest
2397 : public testing::TestWithParam<AudioProcessingTestData> {
2398 public:
2399 AudioProcessingTest()
2400 : input_rate_(std::tr1::get<0>(GetParam())),
2401 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002402 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2403 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2404 expected_snr_(std::tr1::get<4>(GetParam())),
2405 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002406
2407 virtual ~AudioProcessingTest() {}
2408
2409 static void SetUpTestCase() {
2410 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002411 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002412 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002413 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2414 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2415 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002416 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002417 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2418 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2419 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002420 }
2421 }
2422 }
2423 }
2424
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002425 static void TearDownTestCase() {
2426 ClearTempFiles();
2427 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002428
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002430 // to a file specified with |output_file_prefix|. Both forward and reverse
2431 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432 static void ProcessFormat(int input_rate,
2433 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002434 int reverse_input_rate,
2435 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002436 size_t num_input_channels,
2437 size_t num_output_channels,
2438 size_t num_reverse_input_channels,
2439 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002440 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002441 Config config;
2442 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002443 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002444 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002445
ekmeyerson60d9b332015-08-14 10:35:55 -07002446 ProcessingConfig processing_config = {
2447 {{input_rate, num_input_channels},
2448 {output_rate, num_output_channels},
2449 {reverse_input_rate, num_reverse_input_channels},
2450 {reverse_output_rate, num_reverse_output_channels}}};
2451 ap->Initialize(processing_config);
2452
2453 FILE* far_file =
2454 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002455 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002456 FILE* out_file =
2457 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2458 reverse_input_rate, reverse_output_rate,
2459 num_input_channels, num_output_channels,
2460 num_reverse_input_channels,
2461 num_reverse_output_channels, kForward).c_str(),
2462 "wb");
2463 FILE* rev_out_file =
2464 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2465 reverse_input_rate, reverse_output_rate,
2466 num_input_channels, num_output_channels,
2467 num_reverse_input_channels,
2468 num_reverse_output_channels, kReverse).c_str(),
2469 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002470 ASSERT_TRUE(far_file != NULL);
2471 ASSERT_TRUE(near_file != NULL);
2472 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002473 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002474
2475 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2476 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002477 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2478 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002479 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2480 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002481 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2482 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002483
2484 // Temporary buffers.
2485 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002486 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2487 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002488 std::unique_ptr<float[]> float_data(new float[max_length]);
2489 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002490
2491 int analog_level = 127;
2492 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2493 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002494 EXPECT_NOERR(ap->ProcessReverseStream(
2495 rev_cb.channels(), processing_config.reverse_input_stream(),
2496 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002497
2498 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2499 ap->echo_cancellation()->set_stream_drift_samples(0);
2500 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2501
2502 EXPECT_NOERR(ap->ProcessStream(
2503 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002504 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002505 input_rate,
2506 LayoutFromChannels(num_input_channels),
2507 output_rate,
2508 LayoutFromChannels(num_output_channels),
2509 out_cb.channels()));
2510
ekmeyerson60d9b332015-08-14 10:35:55 -07002511 // Dump forward output to file.
2512 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002513 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002514 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002515
pkasting25702cb2016-01-08 13:50:27 -08002516 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002517 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002518 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002519
ekmeyerson60d9b332015-08-14 10:35:55 -07002520 // Dump reverse output to file.
2521 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2522 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002523 size_t rev_out_length =
2524 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002525
pkasting25702cb2016-01-08 13:50:27 -08002526 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002527 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2528 rev_out_file));
2529
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002530 analog_level = ap->gain_control()->stream_analog_level();
2531 }
2532 fclose(far_file);
2533 fclose(near_file);
2534 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002535 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002536 }
2537
2538 protected:
2539 int input_rate_;
2540 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002541 int reverse_input_rate_;
2542 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002543 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002544 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002545};
2546
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002547TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002548 struct ChannelFormat {
2549 int num_input;
2550 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002551 int num_reverse_input;
2552 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002553 };
2554 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002555 {1, 1, 1, 1},
2556 {1, 1, 2, 1},
2557 {2, 1, 1, 1},
2558 {2, 1, 2, 1},
2559 {2, 2, 1, 1},
2560 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002561 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002562
pkasting25702cb2016-01-08 13:50:27 -08002563 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002564 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2565 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2566 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002567
ekmeyerson60d9b332015-08-14 10:35:55 -07002568 // Verify output for both directions.
2569 std::vector<StreamDirection> stream_directions;
2570 stream_directions.push_back(kForward);
2571 stream_directions.push_back(kReverse);
2572 for (StreamDirection file_direction : stream_directions) {
2573 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2574 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2575 const int out_num =
2576 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2577 const double expected_snr =
2578 file_direction ? expected_reverse_snr_ : expected_snr_;
2579
2580 const int min_ref_rate = std::min(in_rate, out_rate);
2581 int ref_rate;
2582
2583 if (min_ref_rate > 32000) {
2584 ref_rate = 48000;
2585 } else if (min_ref_rate > 16000) {
2586 ref_rate = 32000;
2587 } else if (min_ref_rate > 8000) {
2588 ref_rate = 16000;
2589 } else {
2590 ref_rate = 8000;
2591 }
aluebs776593b2016-03-15 14:04:58 -07002592#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002593 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002594 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002595 }
2596#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002597 FILE* out_file = fopen(
2598 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2599 reverse_output_rate_, cf[i].num_input,
2600 cf[i].num_output, cf[i].num_reverse_input,
2601 cf[i].num_reverse_output, file_direction).c_str(),
2602 "rb");
2603 // The reference files always have matching input and output channels.
2604 FILE* ref_file = fopen(
2605 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2606 cf[i].num_output, cf[i].num_output,
2607 cf[i].num_reverse_output, cf[i].num_reverse_output,
2608 file_direction).c_str(),
2609 "rb");
2610 ASSERT_TRUE(out_file != NULL);
2611 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002612
pkasting25702cb2016-01-08 13:50:27 -08002613 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2614 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002615 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002616 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002617 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002618 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002619 // Data from the resampled output, in case the reference and output rates
2620 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002621 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002622
ekmeyerson60d9b332015-08-14 10:35:55 -07002623 PushResampler<float> resampler;
2624 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002625
ekmeyerson60d9b332015-08-14 10:35:55 -07002626 // Compute the resampling delay of the output relative to the reference,
2627 // to find the region over which we should search for the best SNR.
2628 float expected_delay_sec = 0;
2629 if (in_rate != ref_rate) {
2630 // Input resampling delay.
2631 expected_delay_sec +=
2632 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2633 }
2634 if (out_rate != ref_rate) {
2635 // Output resampling delay.
2636 expected_delay_sec +=
2637 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2638 // Delay of converting the output back to its processing rate for
2639 // testing.
2640 expected_delay_sec +=
2641 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2642 }
2643 int expected_delay =
2644 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002645
ekmeyerson60d9b332015-08-14 10:35:55 -07002646 double variance = 0;
2647 double sq_error = 0;
2648 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2649 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2650 float* out_ptr = out_data.get();
2651 if (out_rate != ref_rate) {
2652 // Resample the output back to its internal processing rate if
2653 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002654 ASSERT_EQ(ref_length,
2655 static_cast<size_t>(resampler.Resample(
2656 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002657 out_ptr = cmp_data.get();
2658 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002659
ekmeyerson60d9b332015-08-14 10:35:55 -07002660 // Update the |sq_error| and |variance| accumulators with the highest
2661 // SNR of reference vs output.
2662 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2663 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002664 }
2665
ekmeyerson60d9b332015-08-14 10:35:55 -07002666 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2667 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2668 << cf[i].num_input << ", " << cf[i].num_output << ", "
2669 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2670 << ", " << file_direction << "): ";
2671 if (sq_error > 0) {
2672 double snr = 10 * log10(variance / sq_error);
2673 EXPECT_GE(snr, expected_snr);
2674 EXPECT_NE(0, expected_snr);
2675 std::cout << "SNR=" << snr << " dB" << std::endl;
2676 } else {
aluebs776593b2016-03-15 14:04:58 -07002677 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002678 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002679
ekmeyerson60d9b332015-08-14 10:35:55 -07002680 fclose(out_file);
2681 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002682 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002683 }
2684}
2685
2686#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2687INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002688 CommonFormats,
2689 AudioProcessingTest,
2690 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002691 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2692 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002693 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2694 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2695 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2696 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2697 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2698 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2699 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2700 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2701 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002702
ekmeyerson60d9b332015-08-14 10:35:55 -07002703 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2704 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2705 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2706 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2707 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2708 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2709 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2710 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2711 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2712 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2713 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2714 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002715
ekmeyerson60d9b332015-08-14 10:35:55 -07002716 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2717 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2718 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2719 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2720 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2721 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2722 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2723 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2724 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2725 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2726 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2727 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002728
ekmeyerson60d9b332015-08-14 10:35:55 -07002729 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2730 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2731 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2732 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2733 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2734 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2735 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2736 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2737 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2738 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002739 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002740 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002741
2742#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2743INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002744 CommonFormats,
2745 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002746 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2747 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2748 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2749 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2750 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2751 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002752 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2753 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2754 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2755 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2756 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2757 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002758
aluebs776593b2016-03-15 14:04:58 -07002759 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2760 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2761 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002762 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2763 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2764 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2765 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2766 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2767 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2768 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2769 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2770 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002771
aluebs776593b2016-03-15 14:04:58 -07002772 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2773 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2774 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2775 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2776 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2777 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2778 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2779 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2780 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002781 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2782 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2783 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002784
ekmeyerson60d9b332015-08-14 10:35:55 -07002785 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2786 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2787 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2788 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2789 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2790 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2791 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2792 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2793 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2794 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002795 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002796 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002797#endif
2798
niklase@google.com470e71d2011-07-07 08:21:25 +00002799} // namespace
peahc19f3122016-10-07 14:54:10 -07002800
2801TEST(ApmConfiguration, DefaultBehavior) {
2802 // Verify that the level controller is default off, it can be activated using
2803 // the config, and that the default initial level is maintained after the
2804 // config has been applied.
2805 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002806 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002807 AudioProcessing::Config config;
2808 EXPECT_FALSE(apm->config_.level_controller.enabled);
2809 // TODO(peah): Add test for the existence of the level controller object once
2810 // that is created only when that is specified in the config.
2811 // TODO(peah): Remove the testing for
2812 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2813 // is instead used to activate the level controller.
2814 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2815 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2816 apm->config_.level_controller.initial_peak_level_dbfs,
2817 std::numeric_limits<float>::epsilon());
2818 config.level_controller.enabled = true;
2819 apm->ApplyConfig(config);
2820 EXPECT_TRUE(apm->config_.level_controller.enabled);
2821 // TODO(peah): Add test for the existence of the level controller object once
2822 // that is created only when the that is specified in the config.
2823 // TODO(peah): Remove the testing for
2824 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2825 // is instead used to activate the level controller.
2826 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2827 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2828 apm->config_.level_controller.initial_peak_level_dbfs,
2829 std::numeric_limits<float>::epsilon());
2830}
2831
2832TEST(ApmConfiguration, ValidConfigBehavior) {
2833 // Verify that the initial level can be specified and is retained after the
2834 // config has been applied.
2835 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002836 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002837 AudioProcessing::Config config;
2838 config.level_controller.initial_peak_level_dbfs = -50.f;
2839 apm->ApplyConfig(config);
2840 EXPECT_FALSE(apm->config_.level_controller.enabled);
2841 // TODO(peah): Add test for the existence of the level controller object once
2842 // that is created only when the that is specified in the config.
2843 // TODO(peah): Remove the testing for
2844 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2845 // is instead used to activate the level controller.
2846 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2847 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2848 std::numeric_limits<float>::epsilon());
2849}
2850
2851TEST(ApmConfiguration, InValidConfigBehavior) {
2852 // Verify that the config is properly reset when nonproper values are applied
2853 // for the initial level.
2854
2855 // Verify that the config is properly reset when the specified initial peak
2856 // level is too low.
2857 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002858 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002859 AudioProcessing::Config config;
2860 config.level_controller.enabled = true;
2861 config.level_controller.initial_peak_level_dbfs = -101.f;
2862 apm->ApplyConfig(config);
2863 EXPECT_FALSE(apm->config_.level_controller.enabled);
2864 // TODO(peah): Add test for the existence of the level controller object once
2865 // that is created only when the that is specified in the config.
2866 // TODO(peah): Remove the testing for
2867 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2868 // is instead used to activate the level controller.
2869 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2870 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2871 apm->config_.level_controller.initial_peak_level_dbfs,
2872 std::numeric_limits<float>::epsilon());
2873
2874 // Verify that the config is properly reset when the specified initial peak
2875 // level is too high.
peaha9cc40b2017-06-29 08:32:09 -07002876 apm.reset(new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002877 config = AudioProcessing::Config();
2878 config.level_controller.enabled = true;
2879 config.level_controller.initial_peak_level_dbfs = 1.f;
2880 apm->ApplyConfig(config);
2881 EXPECT_FALSE(apm->config_.level_controller.enabled);
2882 // TODO(peah): Add test for the existence of the level controller object once
2883 // that is created only when that is specified in the config.
2884 // TODO(peah): Remove the testing for
2885 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2886 // is instead used to activate the level controller.
2887 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2888 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2889 apm->config_.level_controller.initial_peak_level_dbfs,
2890 std::numeric_limits<float>::epsilon());
2891}
2892
Sam Zackrisson0beac582017-09-25 12:04:02 +02002893TEST(ApmConfiguration, EnablePostProcessing) {
2894 // Verify that apm uses a capture post processing module if one is provided.
2895 webrtc::Config webrtc_config;
2896 auto mock_post_processor_ptr =
2897 new testing::NiceMock<test::MockPostProcessing>();
2898 auto mock_post_processor =
2899 std::unique_ptr<PostProcessing>(mock_post_processor_ptr);
2900 rtc::scoped_refptr<AudioProcessing> apm = AudioProcessing::Create(
2901 webrtc_config, std::move(mock_post_processor), nullptr);
2902
2903 AudioFrame audio;
2904 audio.num_channels_ = 1;
2905 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2906
2907 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
2908 std::cout << apm->ProcessStream(&audio) << std::endl;
2909}
2910
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002911} // namespace webrtc