blob: 4d97904c6869c25febb77933fc73fbf1760ddf7d [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 */
10
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000011#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000012#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080013
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000014#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
pkasting25702cb2016-01-08 13:50:27 -080019#include "webrtc/base/arraysize.h"
kwiberg9e2be5f2016-09-14 05:23:22 -070020#include "webrtc/base/checks.h"
peahc19f3122016-10-07 14:54:10 -070021#include "webrtc/base/gtest_prod_util.h"
kwiberg77eab702016-09-28 17:42:01 -070022#include "webrtc/base/ignore_wundef.h"
mbonadei7c2c8432017-04-07 00:59:12 -070023#include "webrtc/base/protobuf_utils.h"
kwiberg7885d3f2017-04-25 12:35:07 -070024#include "webrtc/base/safe_minmax.h"
andrew@webrtc.org27c69802014-02-18 20:24:56 +000025#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000026#include "webrtc/common_audio/resampler/include/push_resampler.h"
27#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000028#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
peahc19f3122016-10-07 14:54:10 -070029#include "webrtc/modules/audio_processing/audio_processing_impl.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000030#include "webrtc/modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000031#include "webrtc/modules/audio_processing/common.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000032#include "webrtc/modules/audio_processing/include/audio_processing.h"
peahc19f3122016-10-07 14:54:10 -070033#include "webrtc/modules/audio_processing/level_controller/level_controller_constants.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070034#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000035#include "webrtc/modules/audio_processing/test/test_utils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010036#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010037#include "webrtc/system_wrappers/include/event_wrapper.h"
38#include "webrtc/system_wrappers/include/trace.h"
kwiberg77eab702016-09-28 17:42:01 -070039#include "webrtc/test/gtest.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000040#include "webrtc/test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070041
42RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000043#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000044#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000045#else
kjellandere3e902e2017-02-28 08:01:46 -080046#include "webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#endif
kwiberg77eab702016-09-28 17:42:01 -070048RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000049
andrew@webrtc.org27c69802014-02-18 20:24:56 +000050namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000051namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000052
ekmeyerson60d9b332015-08-14 10:35:55 -070053// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
54// applicable.
55
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000056// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000057// re-implemented; see comment at the bottom of this file. For now, the user has
58// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000059// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000060// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000061// be set to true with the command-line switch --write_ref_data.
62bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070063const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070064const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000065
aluebseb3603b2016-04-20 15:27:58 -070066#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
67// Android doesn't support 48kHz.
68const int kProcessSampleRates[] = {8000, 16000, 32000};
69#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070070const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070071#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000072
ekmeyerson60d9b332015-08-14 10:35:55 -070073enum StreamDirection { kForward = 0, kReverse };
74
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000076 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000077 cb->num_channels());
78 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000079 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000081 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080082 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000083 S16ToFloat(cb_int.channels()[i],
84 cb->num_frames(),
85 cb->channels()[i]);
86 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000087}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000088
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000089void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
90 ConvertToFloat(frame.data_, cb);
91}
92
andrew@webrtc.org103657b2014-04-24 18:28:56 +000093// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080094size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000095 switch (layout) {
96 case AudioProcessing::kMono:
97 return 1;
98 case AudioProcessing::kMonoAndKeyboard:
99 case AudioProcessing::kStereo:
100 return 2;
101 case AudioProcessing::kStereoAndKeyboard:
102 return 3;
103 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700104 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800105 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000106}
107
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000108int TruncateToMultipleOf10(int value) {
109 return (value / 10) * 10;
110}
111
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000112void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800113 size_t samples_per_channel) {
114 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000116}
117
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000118void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800119 size_t samples_per_channel) {
120 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
122}
123
pkasting25702cb2016-01-08 13:50:27 -0800124void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
125 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000126 stereo[i * 2 + 1] = stereo[i * 2];
127 }
128}
129
pkasting25702cb2016-01-08 13:50:27 -0800130void VerifyChannelsAreEqual(int16_t* stereo, size_t samples_per_channel) {
131 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000132 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
133 }
134}
135
136void SetFrameTo(AudioFrame* frame, int16_t value) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
138 ++i) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000139 frame->data_[i] = value;
140 }
141}
142
143void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800144 ASSERT_EQ(2u, frame->num_channels_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000146 frame->data_[i] = left;
147 frame->data_[i + 1] = right;
148 }
149}
150
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000151void ScaleFrame(AudioFrame* frame, float scale) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
153 ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000154 frame->data_[i] = FloatS16ToS16(frame->data_[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000155 }
156}
157
andrew@webrtc.org81865342012-10-27 00:28:27 +0000158bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000159 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000160 return false;
161 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000162 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000163 return false;
164 }
165 if (memcmp(frame1.data_, frame2.data_,
166 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 return false;
169 }
170 return true;
171}
172
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000173void EnableAllAPComponents(AudioProcessing* ap) {
174#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
175 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
176
177 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
178 EXPECT_NOERR(ap->gain_control()->Enable(true));
179#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
180 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
181 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
182 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
183 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
184
185 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
186 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
187 EXPECT_NOERR(ap->gain_control()->Enable(true));
188#endif
189
peah8271d042016-11-22 07:24:52 -0800190 AudioProcessing::Config apm_config;
191 apm_config.high_pass_filter.enabled = true;
192 ap->ApplyConfig(apm_config);
193
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000194 EXPECT_NOERR(ap->level_estimator()->Enable(true));
195 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
196
197 EXPECT_NOERR(ap->voice_detection()->Enable(true));
198}
199
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000200// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000201template <class T>
202T AbsValue(T a) {
203 return a > 0 ? a: -a;
204}
205
206int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800207 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000208 int16_t max_data = AbsValue(frame.data_[0]);
pkasting25702cb2016-01-08 13:50:27 -0800209 for (size_t i = 1; i < length; i++) {
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000210 max_data = std::max(max_data, AbsValue(frame.data_[i]));
211 }
212
213 return max_data;
214}
215
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000216#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000217void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000218 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700219 EXPECT_EQ(reference.instant(), test.instant);
220 EXPECT_EQ(reference.average(), test.average);
221 EXPECT_EQ(reference.maximum(), test.maximum);
222 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000223}
224
225void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000226 audioproc::Test::Statistic* msg) {
227 msg->set_instant(output.instant);
228 msg->set_average(output.average);
229 msg->set_maximum(output.maximum);
230 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000231}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000232#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000233
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234void OpenFileAndWriteMessage(const std::string filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700235 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000237 ASSERT_TRUE(file != NULL);
238
239 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800241 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000242 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000244 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000245 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000246 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000247 fclose(file);
248}
249
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000250std::string ResourceFilePath(std::string name, int sample_rate_hz) {
251 std::ostringstream ss;
252 // Resource files are all stereo.
253 ss << name << sample_rate_hz / 1000 << "_stereo";
254 return test::ResourcePath(ss.str(), "pcm");
255}
256
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000257// Temporary filenames unique to this process. Used to be able to run these
258// tests in parallel as each process needs to be running in isolation they can't
259// have competing filenames.
260std::map<std::string, std::string> temp_filenames;
261
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000262std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000263 int input_rate,
264 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700265 int reverse_input_rate,
266 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800267 size_t num_input_channels,
268 size_t num_output_channels,
269 size_t num_reverse_input_channels,
270 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700271 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000272 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700273 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
274 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000275 if (num_output_channels == 1) {
276 ss << "mono";
277 } else if (num_output_channels == 2) {
278 ss << "stereo";
279 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700280 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000281 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700282 ss << output_rate / 1000;
283 if (num_reverse_output_channels == 1) {
284 ss << "_rmono";
285 } else if (num_reverse_output_channels == 2) {
286 ss << "_rstereo";
287 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700288 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700289 }
290 ss << reverse_output_rate / 1000;
291 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000292
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000293 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700294 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000295 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
296 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000297}
298
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000299void ClearTempFiles() {
300 for (auto& kv : temp_filenames)
301 remove(kv.second.c_str());
302}
303
mbonadei7c2c8432017-04-07 00:59:12 -0700304void OpenFileAndReadMessage(std::string filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000305 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000306 ASSERT_TRUE(file != NULL);
307 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000308 fclose(file);
309}
310
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000311// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
312// stereo) file, converts to deinterleaved float (optionally downmixing) and
313// returns the result in |cb|. Returns false if the file ended (or on error) and
314// true otherwise.
315//
316// |int_data| and |float_data| are just temporary space that must be
317// sufficiently large to hold the 10 ms chunk.
318bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
319 ChannelBuffer<float>* cb) {
320 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000321 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000322 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
323 if (read_count != frame_size) {
324 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700325 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000326 return false; // This is expected.
327 }
328
329 S16ToFloat(int_data, frame_size, float_data);
330 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000331 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000332 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000333 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000334 cb->channels());
335 }
336
337 return true;
338}
339
niklase@google.com470e71d2011-07-07 08:21:25 +0000340class ApmTest : public ::testing::Test {
341 protected:
342 ApmTest();
343 virtual void SetUp();
344 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000345
346 static void SetUpTestCase() {
347 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000348 }
349
350 static void TearDownTestCase() {
351 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000352 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000353 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000354
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000355 // Used to select between int and float interface tests.
356 enum Format {
357 kIntFormat,
358 kFloatFormat
359 };
360
361 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000362 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800364 size_t num_input_channels,
365 size_t num_output_channels,
366 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000367 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000368 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000369 void EnableAllComponents();
370 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000371 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000372 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000373 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
374 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000375 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000376 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
377 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700378 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800379 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700380 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800381 void TestChangingForwardChannels(size_t num_in_channels,
382 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700383 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800384 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000386 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
387 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000388 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000389 int ProcessStreamChooser(Format format);
390 int AnalyzeReverseStreamChooser(Format format);
391 void ProcessDebugDump(const std::string& in_filename,
392 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800393 Format format,
394 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000395 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000396
397 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000398 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800399 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000400 AudioFrame* frame_;
401 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800402 std::unique_ptr<ChannelBuffer<float> > float_cb_;
403 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000404 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800405 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406 FILE* far_file_;
407 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000408 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000409};
410
411ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000412 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000413#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800414 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
415 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000416#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000417#if defined(WEBRTC_MAC)
418 // A different file for Mac is needed because on this platform the AEC
419 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800420 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
421 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000422#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800423 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
424 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000425#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000426#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000428 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000429 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000430 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000431 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000432 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000433 out_file_(NULL) {
434 Config config;
435 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
436 apm_.reset(AudioProcessing::Create(config));
437}
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
439void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000440 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
442 frame_ = new AudioFrame();
443 revframe_ = new AudioFrame();
444
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000445 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
448void ApmTest::TearDown() {
449 if (frame_) {
450 delete frame_;
451 }
452 frame_ = NULL;
453
454 if (revframe_) {
455 delete revframe_;
456 }
457 revframe_ = NULL;
458
459 if (far_file_) {
460 ASSERT_EQ(0, fclose(far_file_));
461 }
462 far_file_ = NULL;
463
464 if (near_file_) {
465 ASSERT_EQ(0, fclose(near_file_));
466 }
467 near_file_ = NULL;
468
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000469 if (out_file_) {
470 ASSERT_EQ(0, fclose(out_file_));
471 }
472 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000475void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000476 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700477 ap->Initialize(
478 {{{frame_->sample_rate_hz_, frame_->num_channels_},
479 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700480 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700481 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000482}
483
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000486 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800487 size_t num_input_channels,
488 size_t num_output_channels,
489 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000491 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000492 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000493 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000494
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000495 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
496 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000497 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000498
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000499 if (far_file_) {
500 ASSERT_EQ(0, fclose(far_file_));
501 }
502 std::string filename = ResourceFilePath("far", sample_rate_hz);
503 far_file_ = fopen(filename.c_str(), "rb");
504 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
505 filename << "\n";
506
507 if (near_file_) {
508 ASSERT_EQ(0, fclose(near_file_));
509 }
510 filename = ResourceFilePath("near", sample_rate_hz);
511 near_file_ = fopen(filename.c_str(), "rb");
512 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
513 filename << "\n";
514
515 if (open_output_file) {
516 if (out_file_) {
517 ASSERT_EQ(0, fclose(out_file_));
518 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700519 filename = OutputFilePath(
520 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
521 reverse_sample_rate_hz, num_input_channels, num_output_channels,
522 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000523 out_file_ = fopen(filename.c_str(), "wb");
524 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
525 filename << "\n";
526 }
527}
528
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000529void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000530 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000531}
532
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000533bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
534 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000535 // The files always contain stereo audio.
536 size_t frame_size = frame->samples_per_channel_ * 2;
537 size_t read_count = fread(frame->data_,
538 sizeof(int16_t),
539 frame_size,
540 file);
541 if (read_count != frame_size) {
542 // Check that the file really ended.
543 EXPECT_NE(0, feof(file));
544 return false; // This is expected.
545 }
546
547 if (frame->num_channels_ == 1) {
548 MixStereoToMono(frame->data_, frame->data_,
549 frame->samples_per_channel_);
550 }
551
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000553 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000555 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000556}
557
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000558bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
559 return ReadFrame(file, frame, NULL);
560}
561
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000562// If the end of the file has been reached, rewind it and attempt to read the
563// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000564void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
565 ChannelBuffer<float>* cb) {
566 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000567 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000568 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000569 }
570}
571
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
573 ReadFrameWithRewind(file, frame, NULL);
574}
575
andrew@webrtc.org81865342012-10-27 00:28:27 +0000576void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
577 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000578 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000579 EXPECT_EQ(apm_->kNoError,
580 apm_->gain_control()->set_stream_analog_level(127));
581 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000582}
583
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000584int ApmTest::ProcessStreamChooser(Format format) {
585 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000586 return apm_->ProcessStream(frame_);
587 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000588 return apm_->ProcessStream(float_cb_->channels(),
589 frame_->samples_per_channel_,
590 frame_->sample_rate_hz_,
591 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000592 output_sample_rate_hz_,
593 LayoutFromChannels(num_output_channels_),
594 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000595}
596
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000597int ApmTest::AnalyzeReverseStreamChooser(Format format) {
598 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700599 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000600 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000601 return apm_->AnalyzeReverseStream(
602 revfloat_cb_->channels(),
603 revframe_->samples_per_channel_,
604 revframe_->sample_rate_hz_,
605 LayoutFromChannels(revframe_->num_channels_));
606}
607
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000608void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
609 int delay_min, int delay_max) {
610 // The |revframe_| and |frame_| should include the proper frame information,
611 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000612 AudioFrame tmp_frame;
613 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000614 bool causal = true;
615
616 tmp_frame.CopyFrom(*revframe_);
617 SetFrameTo(&tmp_frame, 0);
618
619 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
620 // Initialize the |frame_queue| with empty frames.
621 int frame_delay = delay_ms / 10;
622 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000623 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000624 frame->CopyFrom(tmp_frame);
625 frame_queue.push(frame);
626 frame_delay++;
627 causal = false;
628 }
629 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000630 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631 frame->CopyFrom(tmp_frame);
632 frame_queue.push(frame);
633 frame_delay--;
634 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000635 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
636 // need enough frames with audio to have reliable estimates, but as few as
637 // possible to keep processing time down. 4.5 seconds seemed to be a good
638 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000639 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000640 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000641 frame->CopyFrom(tmp_frame);
642 // Use the near end recording, since that has more speech in it.
643 ASSERT_TRUE(ReadFrame(near_file_, frame));
644 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000645 AudioFrame* reverse_frame = frame;
646 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 if (!causal) {
648 reverse_frame = frame_queue.front();
649 // When we call ProcessStream() the frame is modified, so we can't use the
650 // pointer directly when things are non-causal. Use an intermediate frame
651 // and copy the data.
652 process_frame = &tmp_frame;
653 process_frame->CopyFrom(*frame);
654 }
aluebsb0319552016-03-17 20:39:53 -0700655 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
658 frame = frame_queue.front();
659 frame_queue.pop();
660 delete frame;
661
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000662 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 int median;
664 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000665 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000666 // Discard the first delay metrics to avoid convergence effects.
667 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000668 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
669 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000670 }
671 }
672
673 rewind(near_file_);
674 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000675 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000676 frame_queue.pop();
677 delete frame;
678 }
679 // Calculate expected delay estimate and acceptable regions. Further,
680 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700682 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
684 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700685 int expected_median_high = std::min(
686 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
687 delay_min),
688 delay_max);
689 int expected_median_low = std::min(
690 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
691 delay_min),
692 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000693 // Verify delay metrics.
694 int median;
695 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000696 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000697 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000698 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
699 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000700 EXPECT_GE(expected_median_high, median);
701 EXPECT_LE(expected_median_low, median);
702}
703
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000704void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000706 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000707
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000710 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000711 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000712
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 EXPECT_EQ(apm_->kNoError,
715 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000716 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000717 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000718 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000719
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000720 // Other stream parameters set correctly.
721 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 EXPECT_EQ(apm_->kNoError,
723 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000724 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000725 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000727 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
729 EXPECT_EQ(apm_->kNoError,
730 apm_->echo_cancellation()->enable_drift_compensation(false));
731
732 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000733 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000734 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000735 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000736 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000737
738 // Resets after successful ProcessStream().
739 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000740 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000741 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000743
744 // Other stream parameters set correctly.
745 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
746 EXPECT_EQ(apm_->kNoError,
747 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000748 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000749 EXPECT_EQ(apm_->kNoError,
750 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000751 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000752 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
754
755 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000757 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000758
759 // Resets after successful ProcessStream().
760 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000761 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000762 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000763 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000764 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000765
766 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000767 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
768 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
769 EXPECT_EQ(apm_->kNoError,
770 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000771 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000772 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000773
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000774 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000775 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000776 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000777 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000779
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000780 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000781 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000782 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 EXPECT_EQ(apm_->kNoError,
784 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000785 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000786}
787
788TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000789 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000790}
791
792TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000793 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000794}
795
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000796TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
797 EXPECT_EQ(0, apm_->delay_offset_ms());
798 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
799 EXPECT_EQ(50, apm_->stream_delay_ms());
800}
801
802TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
803 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000804 apm_->set_delay_offset_ms(100);
805 EXPECT_EQ(100, apm_->delay_offset_ms());
806 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000807 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000808 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
809 EXPECT_EQ(200, apm_->stream_delay_ms());
810
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000811 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000812 apm_->set_delay_offset_ms(-50);
813 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000814 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
815 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000816 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
817 EXPECT_EQ(50, apm_->stream_delay_ms());
818}
819
Michael Graczyk86c6d332015-07-23 11:41:39 -0700820void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800821 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700822 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000823 frame_->num_channels_ = num_channels;
824 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700825 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000826}
827
Michael Graczyk86c6d332015-07-23 11:41:39 -0700828void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800829 size_t num_in_channels,
830 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700831 AudioProcessing::Error expected_return) {
832 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
833 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
834
835 EXPECT_EQ(expected_return,
836 apm_->ProcessStream(float_cb_->channels(), input_stream,
837 output_stream, float_cb_->channels()));
838}
839
840void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800841 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700842 AudioProcessing::Error expected_return) {
843 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700844 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
845 {output_sample_rate_hz_, apm_->num_output_channels()},
846 {frame_->sample_rate_hz_, num_rev_channels},
847 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700848
ekmeyerson60d9b332015-08-14 10:35:55 -0700849 EXPECT_EQ(
850 expected_return,
851 apm_->ProcessReverseStream(
852 float_cb_->channels(), processing_config.reverse_input_stream(),
853 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700854}
855
856TEST_F(ApmTest, ChannelsInt16Interface) {
857 // Testing number of invalid and valid channels.
858 Init(16000, 16000, 16000, 4, 4, 4, false);
859
860 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
861
Peter Kasting69558702016-01-12 16:26:35 -0800862 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700863 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000864 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 }
866}
867
Michael Graczyk86c6d332015-07-23 11:41:39 -0700868TEST_F(ApmTest, Channels) {
869 // Testing number of invalid and valid channels.
870 Init(16000, 16000, 16000, 4, 4, 4, false);
871
872 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
873 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
874
Peter Kasting69558702016-01-12 16:26:35 -0800875 for (size_t i = 1; i < 4; ++i) {
876 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700877 // Output channels much be one or match input channels.
878 if (j == 1 || i == j) {
879 TestChangingForwardChannels(i, j, kNoErr);
880 TestChangingReverseChannels(i, kNoErr);
881
882 EXPECT_EQ(i, apm_->num_input_channels());
883 EXPECT_EQ(j, apm_->num_output_channels());
884 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800885 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700886 } else {
887 TestChangingForwardChannels(i, j,
888 AudioProcessing::kBadNumberChannelsError);
889 }
890 }
891 }
892}
893
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000894TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000895 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000896 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000897 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700899 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800900 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000901 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000902 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000903 }
904}
905
niklase@google.com470e71d2011-07-07 08:21:25 +0000906TEST_F(ApmTest, EchoCancellation) {
907 EXPECT_EQ(apm_->kNoError,
908 apm_->echo_cancellation()->enable_drift_compensation(true));
909 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
910 EXPECT_EQ(apm_->kNoError,
911 apm_->echo_cancellation()->enable_drift_compensation(false));
912 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
913
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 EchoCancellation::SuppressionLevel level[] = {
915 EchoCancellation::kLowSuppression,
916 EchoCancellation::kModerateSuppression,
917 EchoCancellation::kHighSuppression,
918 };
pkasting25702cb2016-01-08 13:50:27 -0800919 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000920 EXPECT_EQ(apm_->kNoError,
921 apm_->echo_cancellation()->set_suppression_level(level[i]));
922 EXPECT_EQ(level[i],
923 apm_->echo_cancellation()->suppression_level());
924 }
925
926 EchoCancellation::Metrics metrics;
927 EXPECT_EQ(apm_->kNotEnabledError,
928 apm_->echo_cancellation()->GetMetrics(&metrics));
929
ivoc3e9a5372016-10-28 07:55:33 -0700930 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
931 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
932
niklase@google.com470e71d2011-07-07 08:21:25 +0000933 EXPECT_EQ(apm_->kNoError,
934 apm_->echo_cancellation()->enable_metrics(true));
935 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
936 EXPECT_EQ(apm_->kNoError,
937 apm_->echo_cancellation()->enable_metrics(false));
938 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
939
ivoc48dfab52016-10-28 03:29:31 -0700940 EXPECT_EQ(apm_->kNoError,
941 apm_->echo_cancellation()->enable_delay_logging(true));
942 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
943 EXPECT_EQ(apm_->kNoError,
944 apm_->echo_cancellation()->enable_delay_logging(false));
945 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
946
ivoc3e9a5372016-10-28 07:55:33 -0700947 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
948 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
949
950 int median = 0;
951 int std = 0;
952 float poor_fraction = 0;
953 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
954 &median, &std, &poor_fraction));
955
niklase@google.com470e71d2011-07-07 08:21:25 +0000956 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
957 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
958 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
959 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000960
961 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
962 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
963 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
964 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
965 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
966 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000967}
968
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000969TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000970 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000971 // Enable AEC only.
972 EXPECT_EQ(apm_->kNoError,
973 apm_->echo_cancellation()->enable_drift_compensation(false));
974 EXPECT_EQ(apm_->kNoError,
975 apm_->echo_cancellation()->enable_metrics(false));
976 EXPECT_EQ(apm_->kNoError,
977 apm_->echo_cancellation()->enable_delay_logging(true));
978 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000979 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700980 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000981 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000982
983 // Internally in the AEC the amount of lookahead the delay estimation can
984 // handle is 15 blocks and the maximum delay is set to 60 blocks.
985 const int kLookaheadBlocks = 15;
986 const int kMaxDelayBlocks = 60;
987 // The AEC has a startup time before it actually starts to process. This
988 // procedure can flush the internal far-end buffer, which of course affects
989 // the delay estimation. Therefore, we set a system_delay high enough to
990 // avoid that. The smallest system_delay you can report without flushing the
991 // buffer is 66 ms in 8 kHz.
992 //
993 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
994 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
995 // delay estimation. This should be noted though. In case of test failure,
996 // this could be the cause.
997 const int kSystemDelayMs = 66;
998 // Test a couple of corner cases and verify that the estimated delay is
999 // within a valid region (set to +-1.5 blocks). Note that these cases are
1000 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001001 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001002 Init(kProcessSampleRates[i],
1003 kProcessSampleRates[i],
1004 kProcessSampleRates[i],
1005 2,
1006 2,
1007 2,
1008 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001009 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001010 const int num_ms_per_block =
1011 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001012 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1013 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1014
1015 // 1) Verify correct delay estimate at lookahead boundary.
1016 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1017 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1018 delay_max_ms);
1019 // 2) A delay less than maximum lookahead should give an delay estimate at
1020 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1021 delay_ms -= 20;
1022 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1023 delay_max_ms);
1024 // 3) Three values around zero delay. Note that we need to compensate for
1025 // the fake system_delay.
1026 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1027 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1028 delay_max_ms);
1029 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1030 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1031 delay_max_ms);
1032 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1033 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1034 delay_max_ms);
1035 // 4) Verify correct delay estimate at maximum delay boundary.
1036 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1037 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1038 delay_max_ms);
1039 // 5) A delay above the maximum delay should give an estimate at the
1040 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1041 delay_ms += 20;
1042 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1043 delay_max_ms);
1044 }
1045}
1046
niklase@google.com470e71d2011-07-07 08:21:25 +00001047TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001049 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1051 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1052
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 // Toggle routing modes
1054 EchoControlMobile::RoutingMode mode[] = {
1055 EchoControlMobile::kQuietEarpieceOrHeadset,
1056 EchoControlMobile::kEarpiece,
1057 EchoControlMobile::kLoudEarpiece,
1058 EchoControlMobile::kSpeakerphone,
1059 EchoControlMobile::kLoudSpeakerphone,
1060 };
pkasting25702cb2016-01-08 13:50:27 -08001061 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001062 EXPECT_EQ(apm_->kNoError,
1063 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1064 EXPECT_EQ(mode[i],
1065 apm_->echo_control_mobile()->routing_mode());
1066 }
1067 // Turn comfort noise off/on
1068 EXPECT_EQ(apm_->kNoError,
1069 apm_->echo_control_mobile()->enable_comfort_noise(false));
1070 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1071 EXPECT_EQ(apm_->kNoError,
1072 apm_->echo_control_mobile()->enable_comfort_noise(true));
1073 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001074 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001075 const size_t echo_path_size =
1076 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001077 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1078 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001079 EXPECT_EQ(apm_->kNullPointerError,
1080 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1081 EXPECT_EQ(apm_->kNullPointerError,
1082 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1083 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001084 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001085 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001086 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001087 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001088 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001089 echo_path_in[i] = echo_path_out[i] + 1;
1090 }
1091 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001092 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001093 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001094 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1095 echo_path_size));
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()->GetEchoPath(echo_path_out.get(),
1098 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001099 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001100 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1101 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001102
1103 // Process a few frames with NS in the default disabled state. This exercises
1104 // a different codepath than with it enabled.
1105 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1106 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1107 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1108 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1109
niklase@google.com470e71d2011-07-07 08:21:25 +00001110 // Turn AECM off
1111 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1112 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1113}
1114
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001115TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001116 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001117 EXPECT_EQ(apm_->kNoError,
1118 apm_->gain_control()->set_mode(
1119 apm_->gain_control()->mode()));
1120
1121 GainControl::Mode mode[] = {
1122 GainControl::kAdaptiveAnalog,
1123 GainControl::kAdaptiveDigital,
1124 GainControl::kFixedDigital
1125 };
pkasting25702cb2016-01-08 13:50:27 -08001126 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001127 EXPECT_EQ(apm_->kNoError,
1128 apm_->gain_control()->set_mode(mode[i]));
1129 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1130 }
1131 // Testing invalid target levels
1132 EXPECT_EQ(apm_->kBadParameterError,
1133 apm_->gain_control()->set_target_level_dbfs(-3));
1134 EXPECT_EQ(apm_->kBadParameterError,
1135 apm_->gain_control()->set_target_level_dbfs(-40));
1136 // Testing valid target levels
1137 EXPECT_EQ(apm_->kNoError,
1138 apm_->gain_control()->set_target_level_dbfs(
1139 apm_->gain_control()->target_level_dbfs()));
1140
1141 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001142 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001143 EXPECT_EQ(apm_->kNoError,
1144 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1145 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1146 }
1147
1148 // Testing invalid compression gains
1149 EXPECT_EQ(apm_->kBadParameterError,
1150 apm_->gain_control()->set_compression_gain_db(-1));
1151 EXPECT_EQ(apm_->kBadParameterError,
1152 apm_->gain_control()->set_compression_gain_db(100));
1153
1154 // Testing valid compression gains
1155 EXPECT_EQ(apm_->kNoError,
1156 apm_->gain_control()->set_compression_gain_db(
1157 apm_->gain_control()->compression_gain_db()));
1158
1159 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001160 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001161 EXPECT_EQ(apm_->kNoError,
1162 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1163 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1164 }
1165
1166 // Testing limiter off/on
1167 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1168 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1169 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1170 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1171
1172 // Testing invalid level limits
1173 EXPECT_EQ(apm_->kBadParameterError,
1174 apm_->gain_control()->set_analog_level_limits(-1, 512));
1175 EXPECT_EQ(apm_->kBadParameterError,
1176 apm_->gain_control()->set_analog_level_limits(100000, 512));
1177 EXPECT_EQ(apm_->kBadParameterError,
1178 apm_->gain_control()->set_analog_level_limits(512, -1));
1179 EXPECT_EQ(apm_->kBadParameterError,
1180 apm_->gain_control()->set_analog_level_limits(512, 100000));
1181 EXPECT_EQ(apm_->kBadParameterError,
1182 apm_->gain_control()->set_analog_level_limits(512, 255));
1183
1184 // Testing valid level limits
1185 EXPECT_EQ(apm_->kNoError,
1186 apm_->gain_control()->set_analog_level_limits(
1187 apm_->gain_control()->analog_level_minimum(),
1188 apm_->gain_control()->analog_level_maximum()));
1189
1190 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001191 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001192 EXPECT_EQ(apm_->kNoError,
1193 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1194 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1195 }
1196
1197 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001198 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001199 EXPECT_EQ(apm_->kNoError,
1200 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1201 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1202 }
1203
1204 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1205
1206 // Turn AGC off
1207 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1208 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1209}
1210
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001211void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001212 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001213 EXPECT_EQ(apm_->kNoError,
1214 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1215 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1216
1217 int out_analog_level = 0;
1218 for (int i = 0; i < 2000; ++i) {
1219 ReadFrameWithRewind(near_file_, frame_);
1220 // Ensure the audio is at a low level, so the AGC will try to increase it.
1221 ScaleFrame(frame_, 0.25);
1222
1223 // Always pass in the same volume.
1224 EXPECT_EQ(apm_->kNoError,
1225 apm_->gain_control()->set_stream_analog_level(100));
1226 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1227 out_analog_level = apm_->gain_control()->stream_analog_level();
1228 }
1229
1230 // Ensure the AGC is still able to reach the maximum.
1231 EXPECT_EQ(255, out_analog_level);
1232}
1233
1234// Verifies that despite volume slider quantization, the AGC can continue to
1235// increase its volume.
1236TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001237 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001238 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1239 }
1240}
1241
1242void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001243 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001244 EXPECT_EQ(apm_->kNoError,
1245 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1246 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1247
1248 int out_analog_level = 100;
1249 for (int i = 0; i < 1000; ++i) {
1250 ReadFrameWithRewind(near_file_, frame_);
1251 // Ensure the audio is at a low level, so the AGC will try to increase it.
1252 ScaleFrame(frame_, 0.25);
1253
1254 EXPECT_EQ(apm_->kNoError,
1255 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1256 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1257 out_analog_level = apm_->gain_control()->stream_analog_level();
1258 }
1259
1260 // Ensure the volume was raised.
1261 EXPECT_GT(out_analog_level, 100);
1262 int highest_level_reached = out_analog_level;
1263 // Simulate a user manual volume change.
1264 out_analog_level = 100;
1265
1266 for (int i = 0; i < 300; ++i) {
1267 ReadFrameWithRewind(near_file_, frame_);
1268 ScaleFrame(frame_, 0.25);
1269
1270 EXPECT_EQ(apm_->kNoError,
1271 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1272 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1273 out_analog_level = apm_->gain_control()->stream_analog_level();
1274 // Check that AGC respected the manually adjusted volume.
1275 EXPECT_LT(out_analog_level, highest_level_reached);
1276 }
1277 // Check that the volume was still raised.
1278 EXPECT_GT(out_analog_level, 100);
1279}
1280
1281TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001282 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001283 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1284 }
1285}
1286
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001287#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1288TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1289 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001290 const size_t kSamplesPerChannel =
1291 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001292 const size_t kNumInputChannels = 2;
1293 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001294 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001295 const float kScaleFactor = 0.25f;
1296 Config config;
1297 std::vector<webrtc::Point> geometry;
1298 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1299 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1300 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001301 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001302 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001303 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001304 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001305 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1306 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1307 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001308 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1309 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001310 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1311 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001312 std::string filename = ResourceFilePath("far", kSampleRateHz);
1313 FILE* far_file = fopen(filename.c_str(), "rb");
1314 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1315 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1316 const int kDefaultCompressionGain =
1317 apm->gain_control()->compression_gain_db();
1318 bool is_target = false;
1319 EXPECT_CALL(*beamformer, is_target_present())
1320 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001321 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001322 ASSERT_TRUE(ReadChunk(far_file,
1323 int_data.get(),
1324 float_data.get(),
1325 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001326 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001327 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001328 src_buf.channels()[j][k] *= kScaleFactor;
1329 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001330 }
1331 EXPECT_EQ(kNoErr,
1332 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001333 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001334 kSampleRateHz,
1335 LayoutFromChannels(src_buf.num_channels()),
1336 kSampleRateHz,
1337 LayoutFromChannels(dest_buf.num_channels()),
1338 dest_buf.channels()));
1339 }
1340 EXPECT_EQ(kDefaultVolume,
1341 apm->gain_control()->stream_analog_level());
1342 EXPECT_EQ(kDefaultCompressionGain,
1343 apm->gain_control()->compression_gain_db());
1344 rewind(far_file);
1345 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001346 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001347 ASSERT_TRUE(ReadChunk(far_file,
1348 int_data.get(),
1349 float_data.get(),
1350 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001351 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001352 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001353 src_buf.channels()[j][k] *= kScaleFactor;
1354 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001355 }
1356 EXPECT_EQ(kNoErr,
1357 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001358 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001359 kSampleRateHz,
1360 LayoutFromChannels(src_buf.num_channels()),
1361 kSampleRateHz,
1362 LayoutFromChannels(dest_buf.num_channels()),
1363 dest_buf.channels()));
1364 }
1365 EXPECT_LT(kDefaultVolume,
1366 apm->gain_control()->stream_analog_level());
1367 EXPECT_LT(kDefaultCompressionGain,
1368 apm->gain_control()->compression_gain_db());
1369 ASSERT_EQ(0, fclose(far_file));
1370}
1371#endif
1372
niklase@google.com470e71d2011-07-07 08:21:25 +00001373TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001374 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001375 NoiseSuppression::Level level[] = {
1376 NoiseSuppression::kLow,
1377 NoiseSuppression::kModerate,
1378 NoiseSuppression::kHigh,
1379 NoiseSuppression::kVeryHigh
1380 };
pkasting25702cb2016-01-08 13:50:27 -08001381 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001382 EXPECT_EQ(apm_->kNoError,
1383 apm_->noise_suppression()->set_level(level[i]));
1384 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1385 }
1386
andrew@webrtc.org648af742012-02-08 01:57:29 +00001387 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001388 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1389 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1390 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1391 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1392}
1393
1394TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001395 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001396 AudioProcessing::Config apm_config;
1397 apm_config.high_pass_filter.enabled = true;
1398 apm_->ApplyConfig(apm_config);
1399 apm_config.high_pass_filter.enabled = false;
1400 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001401}
1402
1403TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001404 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001405 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001406 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001407
1408 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1409
1410 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1411 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1412
1413 // Run this test in wideband; in super-wb, the splitting filter distorts the
1414 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001415 frame_->samples_per_channel_ = 160;
1416 frame_->num_channels_ = 2;
1417 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001418
1419 // Min value if no frames have been processed.
1420 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1421
1422 // Min value on zero frames.
1423 SetFrameTo(frame_, 0);
1424 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1425 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1426 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1427
1428 // Try a few RMS values.
1429 // (These also test that the value resets after retrieving it.)
1430 SetFrameTo(frame_, 32767);
1431 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1432 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1433 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1434
1435 SetFrameTo(frame_, 30000);
1436 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1437 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1438 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1439
1440 SetFrameTo(frame_, 10000);
1441 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1442 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1443 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1444
1445 SetFrameTo(frame_, 10);
1446 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1447 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1448 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1449
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001450 // Verify reset after enable/disable.
1451 SetFrameTo(frame_, 32767);
1452 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1453 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1454 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1455 SetFrameTo(frame_, 1);
1456 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1457 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1458
1459 // Verify reset after initialize.
1460 SetFrameTo(frame_, 32767);
1461 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1462 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1463 SetFrameTo(frame_, 1);
1464 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1465 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001466}
1467
1468TEST_F(ApmTest, VoiceDetection) {
1469 // Test external VAD
1470 EXPECT_EQ(apm_->kNoError,
1471 apm_->voice_detection()->set_stream_has_voice(true));
1472 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1473 EXPECT_EQ(apm_->kNoError,
1474 apm_->voice_detection()->set_stream_has_voice(false));
1475 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1476
andrew@webrtc.org648af742012-02-08 01:57:29 +00001477 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001478 VoiceDetection::Likelihood likelihood[] = {
1479 VoiceDetection::kVeryLowLikelihood,
1480 VoiceDetection::kLowLikelihood,
1481 VoiceDetection::kModerateLikelihood,
1482 VoiceDetection::kHighLikelihood
1483 };
pkasting25702cb2016-01-08 13:50:27 -08001484 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001485 EXPECT_EQ(apm_->kNoError,
1486 apm_->voice_detection()->set_likelihood(likelihood[i]));
1487 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1488 }
1489
1490 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001491 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001492 EXPECT_EQ(apm_->kBadParameterError,
1493 apm_->voice_detection()->set_frame_size_ms(12));
1494
andrew@webrtc.org648af742012-02-08 01:57:29 +00001495 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001496 for (int i = 10; i <= 30; i += 10) {
1497 EXPECT_EQ(apm_->kNoError,
1498 apm_->voice_detection()->set_frame_size_ms(i));
1499 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1500 }
1501 */
1502
andrew@webrtc.org648af742012-02-08 01:57:29 +00001503 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001504 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1505 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1506 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1507 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1508
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001509 // Test that AudioFrame activity is maintained when VAD is disabled.
1510 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1511 AudioFrame::VADActivity activity[] = {
1512 AudioFrame::kVadActive,
1513 AudioFrame::kVadPassive,
1514 AudioFrame::kVadUnknown
1515 };
pkasting25702cb2016-01-08 13:50:27 -08001516 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001517 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001518 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001519 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001520 }
1521
1522 // Test that AudioFrame activity is set when VAD is enabled.
1523 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001524 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001525 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001526 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001527
niklase@google.com470e71d2011-07-07 08:21:25 +00001528 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1529}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001530
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001531TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1532 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1533 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1534 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1535 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1536 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1537 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1538 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1539}
1540
1541TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001542 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001543 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001544 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001545 AudioFrame frame_copy;
1546 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001547 for (int j = 0; j < 1000; j++) {
1548 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1549 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001550 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1551 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001552 }
1553 }
1554}
1555
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001556TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1557 // Test that ProcessStream copies input to output even with no processing.
1558 const size_t kSamples = 80;
1559 const int sample_rate = 8000;
1560 const float src[kSamples] = {
1561 -1.0f, 0.0f, 1.0f
1562 };
1563 float dest[kSamples] = {};
1564
1565 auto src_channels = &src[0];
1566 auto dest_channels = &dest[0];
1567
1568 apm_.reset(AudioProcessing::Create());
1569 EXPECT_NOERR(apm_->ProcessStream(
1570 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1571 sample_rate, LayoutFromChannels(1), &dest_channels));
1572
1573 for (size_t i = 0; i < kSamples; ++i) {
1574 EXPECT_EQ(src[i], dest[i]);
1575 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001576
1577 // Same for ProcessReverseStream.
1578 float rev_dest[kSamples] = {};
1579 auto rev_dest_channels = &rev_dest[0];
1580
1581 StreamConfig input_stream = {sample_rate, 1};
1582 StreamConfig output_stream = {sample_rate, 1};
1583 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1584 output_stream, &rev_dest_channels));
1585
1586 for (size_t i = 0; i < kSamples; ++i) {
1587 EXPECT_EQ(src[i], rev_dest[i]);
1588 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001589}
1590
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001591TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1592 EnableAllComponents();
1593
pkasting25702cb2016-01-08 13:50:27 -08001594 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001595 Init(kProcessSampleRates[i],
1596 kProcessSampleRates[i],
1597 kProcessSampleRates[i],
1598 2,
1599 2,
1600 2,
1601 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001602 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001603 ASSERT_EQ(0, feof(far_file_));
1604 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001605 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001606 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1607
aluebsb0319552016-03-17 20:39:53 -07001608 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001609
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001610 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1611 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1612
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001613 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001614 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001615 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001616 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001617 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001618 analog_level = apm_->gain_control()->stream_analog_level();
1619
1620 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1621 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001622 rewind(far_file_);
1623 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001624 }
1625}
1626
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001627TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001628 // Verify the filter is not active through undistorted audio when:
1629 // 1. No components are enabled...
1630 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001631 AudioFrame frame_copy;
1632 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001633 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1634 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1635 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1636
1637 // 2. Only the level estimator is enabled...
1638 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001639 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001640 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1641 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1642 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1643 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1644 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1645
1646 // 3. Only VAD is enabled...
1647 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001648 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001649 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1650 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1651 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1652 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1653 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1654
1655 // 4. Both VAD and the level estimator are enabled...
1656 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001657 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001658 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1659 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1660 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1661 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1662 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1663 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1664 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1665
1666 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001667 frame_->samples_per_channel_ = 160;
1668 frame_->num_channels_ = 2;
1669 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001670 // Enable AEC, which would require the filter in super-wb. We rely on the
1671 // first few frames of data being unaffected by the AEC.
1672 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1673 // behavior of the AEC. Think of something more robust.
1674 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001675 // Make sure we have extended filter enabled. This makes sure nothing is
1676 // touched until we have a farend frame.
1677 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001678 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001679 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001680 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001681 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001682 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001683 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001684 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1685 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001686 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001687 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1688 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1689
1690 // Check the test is valid. We should have distortion from the filter
1691 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001692 frame_->samples_per_channel_ = 320;
1693 frame_->num_channels_ = 2;
1694 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001695 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001696 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001697 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001698 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001699 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1700 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1701}
1702
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001703#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1704void ApmTest::ProcessDebugDump(const std::string& in_filename,
1705 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001706 Format format,
1707 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001708 FILE* in_file = fopen(in_filename.c_str(), "rb");
1709 ASSERT_TRUE(in_file != NULL);
1710 audioproc::Event event_msg;
1711 bool first_init = true;
1712
1713 while (ReadMessageFromFile(in_file, &event_msg)) {
1714 if (event_msg.type() == audioproc::Event::INIT) {
1715 const audioproc::Init msg = event_msg.init();
1716 int reverse_sample_rate = msg.sample_rate();
1717 if (msg.has_reverse_sample_rate()) {
1718 reverse_sample_rate = msg.reverse_sample_rate();
1719 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001720 int output_sample_rate = msg.sample_rate();
1721 if (msg.has_output_sample_rate()) {
1722 output_sample_rate = msg.output_sample_rate();
1723 }
1724
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001725 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001726 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001727 reverse_sample_rate,
1728 msg.num_input_channels(),
1729 msg.num_output_channels(),
1730 msg.num_reverse_channels(),
1731 false);
1732 if (first_init) {
1733 // StartDebugRecording() writes an additional init message. Don't start
1734 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001735 EXPECT_NOERR(
1736 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001737 first_init = false;
1738 }
1739
1740 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1741 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1742
1743 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001744 ASSERT_EQ(revframe_->num_channels_,
1745 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001746 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001747 memcpy(revfloat_cb_->channels()[i],
1748 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001749 msg.channel(i).size());
1750 }
1751 } else {
1752 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1753 if (format == kFloatFormat) {
1754 // We're using an int16 input file; convert to float.
1755 ConvertToFloat(*revframe_, revfloat_cb_.get());
1756 }
1757 }
1758 AnalyzeReverseStreamChooser(format);
1759
1760 } else if (event_msg.type() == audioproc::Event::STREAM) {
1761 const audioproc::Stream msg = event_msg.stream();
1762 // ProcessStream could have changed this for the output frame.
1763 frame_->num_channels_ = apm_->num_input_channels();
1764
1765 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1766 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1767 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1768 if (msg.has_keypress()) {
1769 apm_->set_stream_key_pressed(msg.keypress());
1770 } else {
1771 apm_->set_stream_key_pressed(true);
1772 }
1773
1774 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001775 ASSERT_EQ(frame_->num_channels_,
1776 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001777 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001778 memcpy(float_cb_->channels()[i],
1779 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001780 msg.input_channel(i).size());
1781 }
1782 } else {
1783 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1784 if (format == kFloatFormat) {
1785 // We're using an int16 input file; convert to float.
1786 ConvertToFloat(*frame_, float_cb_.get());
1787 }
1788 }
1789 ProcessStreamChooser(format);
1790 }
1791 }
1792 EXPECT_NOERR(apm_->StopDebugRecording());
1793 fclose(in_file);
1794}
1795
1796void ApmTest::VerifyDebugDumpTest(Format format) {
1797 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001798 std::string format_string;
1799 switch (format) {
1800 case kIntFormat:
1801 format_string = "_int";
1802 break;
1803 case kFloatFormat:
1804 format_string = "_float";
1805 break;
1806 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001807 const std::string ref_filename = test::TempFilename(
1808 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1809 const std::string out_filename = test::TempFilename(
1810 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001811 const std::string limited_filename = test::TempFilename(
1812 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1813 const size_t logging_limit_bytes = 100000;
1814 // We expect at least this many bytes in the created logfile.
1815 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001816 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001817 ProcessDebugDump(in_filename, ref_filename, format, -1);
1818 ProcessDebugDump(ref_filename, out_filename, format, -1);
1819 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001820
1821 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1822 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001823 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001824 ASSERT_TRUE(ref_file != NULL);
1825 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001826 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001827 std::unique_ptr<uint8_t[]> ref_bytes;
1828 std::unique_ptr<uint8_t[]> out_bytes;
1829 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001830
1831 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1832 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001833 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001834 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001835 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001836 while (ref_size > 0 && out_size > 0) {
1837 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001838 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001839 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001840 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001841 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001842 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001843 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1844 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001845 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001846 }
1847 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001848 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1849 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001850 EXPECT_NE(0, feof(ref_file));
1851 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001852 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001853 ASSERT_EQ(0, fclose(ref_file));
1854 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001855 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001856 remove(ref_filename.c_str());
1857 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001858 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001859}
1860
pbosc7a65692016-05-06 12:50:04 -07001861TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001862 VerifyDebugDumpTest(kIntFormat);
1863}
1864
pbosc7a65692016-05-06 12:50:04 -07001865TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001866 VerifyDebugDumpTest(kFloatFormat);
1867}
1868#endif
1869
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001870// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001871TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001872 const std::string filename =
1873 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001874 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001875 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001876
1877#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1878 // Stopping without having started should be OK.
1879 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1880
ivocd66b44d2016-01-15 03:06:36 -08001881 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001882 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001883 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001884 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1885
1886 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001887 FILE* fid = fopen(filename.c_str(), "r");
1888 ASSERT_TRUE(fid != NULL);
1889
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001890 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001891 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001892 ASSERT_EQ(0, remove(filename.c_str()));
1893#else
1894 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001895 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001896 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1897
1898 // Verify the file has NOT been written.
1899 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1900#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1901}
1902
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001903// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001904TEST_F(ApmTest, DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001905 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001906 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001907 const std::string filename =
1908 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001909 fid = fopen(filename.c_str(), "w");
1910 ASSERT_TRUE(fid);
1911
1912#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1913 // Stopping without having started should be OK.
1914 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1915
ivocd66b44d2016-01-15 03:06:36 -08001916 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001917 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001918 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1919 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1920
1921 // Verify the file has been written.
1922 fid = fopen(filename.c_str(), "r");
1923 ASSERT_TRUE(fid != NULL);
1924
1925 // Clean it up.
1926 ASSERT_EQ(0, fclose(fid));
1927 ASSERT_EQ(0, remove(filename.c_str()));
1928#else
1929 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001930 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001931 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1932
1933 ASSERT_EQ(0, fclose(fid));
1934#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1935}
1936
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001937TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001938 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001939 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001940
1941 Config config;
1942 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001943 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001944 EnableAllComponents();
1945 EnableAllAPComponents(fapm.get());
1946 for (int i = 0; i < ref_data.test_size(); i++) {
1947 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1948
1949 audioproc::Test* test = ref_data.mutable_test(i);
1950 // TODO(ajm): Restore downmixing test cases.
1951 if (test->num_input_channels() != test->num_output_channels())
1952 continue;
1953
Peter Kasting69558702016-01-12 16:26:35 -08001954 const size_t num_render_channels =
1955 static_cast<size_t>(test->num_reverse_channels());
1956 const size_t num_input_channels =
1957 static_cast<size_t>(test->num_input_channels());
1958 const size_t num_output_channels =
1959 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001960 const size_t samples_per_channel = static_cast<size_t>(
1961 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001962
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001963 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1964 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001965 Init(fapm.get());
1966
1967 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001968 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1969 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001970
1971 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001972 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001973 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1974 ReadFrame(near_file_, frame_, float_cb_.get())) {
1975 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1976
aluebsb0319552016-03-17 20:39:53 -07001977 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001978 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1979 revfloat_cb_->channels(),
1980 samples_per_channel,
1981 test->sample_rate(),
1982 LayoutFromChannels(num_render_channels)));
1983
1984 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1985 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1986 apm_->echo_cancellation()->set_stream_drift_samples(0);
1987 fapm->echo_cancellation()->set_stream_drift_samples(0);
1988 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1989 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1990
1991 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001992 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
1993 output_int16.channels());
1994
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001995 EXPECT_NOERR(fapm->ProcessStream(
1996 float_cb_->channels(),
1997 samples_per_channel,
1998 test->sample_rate(),
1999 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002000 test->sample_rate(),
2001 LayoutFromChannels(num_output_channels),
2002 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002003 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002004 FloatToS16(float_cb_->channels()[j],
2005 samples_per_channel,
2006 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002007 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002008 float snr = ComputeSNR(output_int16.channels()[j],
2009 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002010 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002011
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002012 const float kVarianceThreshold = 20;
2013 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002014
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002015 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002016 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2017 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002018 }
2019 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002020
2021 analog_level = fapm->gain_control()->stream_analog_level();
2022 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2023 fapm->gain_control()->stream_analog_level());
2024 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2025 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002026 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2027 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002028 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002029
2030 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002031 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002032 }
aluebs776593b2016-03-15 14:04:58 -07002033
2034#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2035 const size_t kMaxNumBadChunks = 0;
2036#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2037 // There are a few chunks in the fixed-point profile that give low SNR.
2038 // Listening confirmed the difference is acceptable.
2039 const size_t kMaxNumBadChunks = 60;
2040#endif
2041 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2042
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002043 rewind(far_file_);
2044 rewind(near_file_);
2045 }
2046}
2047
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002048// TODO(andrew): Add a test to process a few frames with different combinations
2049// of enabled components.
2050
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002051TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002052 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002053 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002054
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002055 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002056 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002057 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002058 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002059 for (size_t i = 0; i < arraysize(kChannels); i++) {
2060 for (size_t j = 0; j < arraysize(kChannels); j++) {
2061 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002062 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002063 test->set_num_reverse_channels(kChannels[i]);
2064 test->set_num_input_channels(kChannels[j]);
2065 test->set_num_output_channels(kChannels[j]);
2066 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002067 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002068 }
2069 }
2070 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002071#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2072 // To test the extended filter mode.
2073 audioproc::Test* test = ref_data.add_test();
2074 test->set_num_reverse_channels(2);
2075 test->set_num_input_channels(2);
2076 test->set_num_output_channels(2);
2077 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2078 test->set_use_aec_extended_filter(true);
2079#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002080 }
2081
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002082 for (int i = 0; i < ref_data.test_size(); i++) {
2083 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002084
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002085 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002086 // TODO(ajm): We no longer allow different input and output channels. Skip
2087 // these tests for now, but they should be removed from the set.
2088 if (test->num_input_channels() != test->num_output_channels())
2089 continue;
2090
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002091 Config config;
2092 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002093 config.Set<ExtendedFilter>(
2094 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002095 apm_.reset(AudioProcessing::Create(config));
2096
2097 EnableAllComponents();
2098
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002099 Init(test->sample_rate(),
2100 test->sample_rate(),
2101 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002102 static_cast<size_t>(test->num_input_channels()),
2103 static_cast<size_t>(test->num_output_channels()),
2104 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002105 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002106
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002107 int frame_count = 0;
2108 int has_echo_count = 0;
2109 int has_voice_count = 0;
2110 int is_saturated_count = 0;
2111 int analog_level = 127;
2112 int analog_level_average = 0;
2113 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002114 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002115#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2116 int stats_index = 0;
2117#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002118
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002119 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002120 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002121
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002122 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2123
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002124 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002125 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002126 EXPECT_EQ(apm_->kNoError,
2127 apm_->gain_control()->set_stream_analog_level(analog_level));
2128
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002129 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002130
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002131 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002132 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2133 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002134
2135 max_output_average += MaxAudioFrame(*frame_);
2136
2137 if (apm_->echo_cancellation()->stream_has_echo()) {
2138 has_echo_count++;
2139 }
2140
2141 analog_level = apm_->gain_control()->stream_analog_level();
2142 analog_level_average += analog_level;
2143 if (apm_->gain_control()->stream_is_saturated()) {
2144 is_saturated_count++;
2145 }
2146 if (apm_->voice_detection()->stream_has_voice()) {
2147 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002148 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002149 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002150 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002151 }
2152
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002153 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2154
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002155 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002156 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002157 sizeof(int16_t),
2158 frame_size,
2159 out_file_);
2160 ASSERT_EQ(frame_size, write_count);
2161
2162 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002163 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002164 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002165
2166#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2167 const int kStatsAggregationFrameNum = 100; // 1 second.
2168 if (frame_count % kStatsAggregationFrameNum == 0) {
2169 // Get echo metrics.
2170 EchoCancellation::Metrics echo_metrics;
2171 EXPECT_EQ(apm_->kNoError,
2172 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2173
2174 // Get delay metrics.
2175 int median = 0;
2176 int std = 0;
2177 float fraction_poor_delays = 0;
2178 EXPECT_EQ(apm_->kNoError,
2179 apm_->echo_cancellation()->GetDelayMetrics(
2180 &median, &std, &fraction_poor_delays));
2181
2182 // Get RMS.
2183 int rms_level = apm_->level_estimator()->RMS();
2184 EXPECT_LE(0, rms_level);
2185 EXPECT_GE(127, rms_level);
2186
2187 if (!write_ref_data) {
2188 const audioproc::Test::EchoMetrics& reference =
2189 test->echo_metrics(stats_index);
2190 TestStats(echo_metrics.residual_echo_return_loss,
2191 reference.residual_echo_return_loss());
2192 TestStats(echo_metrics.echo_return_loss,
2193 reference.echo_return_loss());
2194 TestStats(echo_metrics.echo_return_loss_enhancement,
2195 reference.echo_return_loss_enhancement());
2196 TestStats(echo_metrics.a_nlp,
2197 reference.a_nlp());
2198 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2199 reference.divergent_filter_fraction());
2200
2201 const audioproc::Test::DelayMetrics& reference_delay =
2202 test->delay_metrics(stats_index);
2203 EXPECT_EQ(reference_delay.median(), median);
2204 EXPECT_EQ(reference_delay.std(), std);
2205 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2206 fraction_poor_delays);
2207
2208 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2209
2210 ++stats_index;
2211 } else {
2212 audioproc::Test::EchoMetrics* message =
2213 test->add_echo_metrics();
2214 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2215 message->mutable_residual_echo_return_loss());
2216 WriteStatsMessage(echo_metrics.echo_return_loss,
2217 message->mutable_echo_return_loss());
2218 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2219 message->mutable_echo_return_loss_enhancement());
2220 WriteStatsMessage(echo_metrics.a_nlp,
2221 message->mutable_a_nlp());
2222 message->set_divergent_filter_fraction(
2223 echo_metrics.divergent_filter_fraction);
2224
2225 audioproc::Test::DelayMetrics* message_delay =
2226 test->add_delay_metrics();
2227 message_delay->set_median(median);
2228 message_delay->set_std(std);
2229 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2230
2231 test->add_rms_level(rms_level);
2232 }
2233 }
2234#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002235 }
2236 max_output_average /= frame_count;
2237 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002238 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002239
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002240 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002241 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002242 // When running the test on a N7 we get a {2, 6} difference of
2243 // |has_voice_count| and |max_output_average| is up to 18 higher.
2244 // All numbers being consistently higher on N7 compare to ref_data.
2245 // TODO(bjornv): If we start getting more of these offsets on Android we
2246 // should consider a different approach. Either using one slack for all,
2247 // or generate a separate android reference.
2248#if defined(WEBRTC_ANDROID)
2249 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002250 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002251 const int kMaxOutputAverageOffset = 9;
2252 const int kMaxOutputAverageNear = 9;
2253#else
2254 const int kHasVoiceCountOffset = 0;
2255 const int kHasVoiceCountNear = kIntNear;
2256 const int kMaxOutputAverageOffset = 0;
2257 const int kMaxOutputAverageNear = kIntNear;
2258#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002259 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002260 EXPECT_NEAR(test->has_voice_count(),
2261 has_voice_count - kHasVoiceCountOffset,
2262 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002263 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002264
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002265 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002266 EXPECT_NEAR(test->max_output_average(),
2267 max_output_average - kMaxOutputAverageOffset,
2268 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002269#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002270 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002271 EXPECT_NEAR(test->ns_speech_probability_average(),
2272 ns_speech_prob_average,
2273 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002274#endif
2275 } else {
2276 test->set_has_echo_count(has_echo_count);
2277 test->set_has_voice_count(has_voice_count);
2278 test->set_is_saturated_count(is_saturated_count);
2279
2280 test->set_analog_level_average(analog_level_average);
2281 test->set_max_output_average(max_output_average);
2282
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002283#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002284 EXPECT_LE(0.0f, ns_speech_prob_average);
2285 EXPECT_GE(1.0f, ns_speech_prob_average);
2286 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002287#endif
2288 }
2289
2290 rewind(far_file_);
2291 rewind(near_file_);
2292 }
2293
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002294 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002295 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002296 }
2297}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002298
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002299TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2300 struct ChannelFormat {
2301 AudioProcessing::ChannelLayout in_layout;
2302 AudioProcessing::ChannelLayout out_layout;
2303 };
2304 ChannelFormat cf[] = {
2305 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2306 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2307 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2308 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002309
kwiberg62eaacf2016-02-17 06:39:05 -08002310 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002311 // Enable one component just to ensure some processing takes place.
2312 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002313 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002314 const int in_rate = 44100;
2315 const int out_rate = 48000;
2316 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2317 TotalChannelsFromLayout(cf[i].in_layout));
2318 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2319 ChannelsFromLayout(cf[i].out_layout));
2320
2321 // Run over a few chunks.
2322 for (int j = 0; j < 10; ++j) {
2323 EXPECT_NOERR(ap->ProcessStream(
2324 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002325 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002326 in_rate,
2327 cf[i].in_layout,
2328 out_rate,
2329 cf[i].out_layout,
2330 out_cb.channels()));
2331 }
2332 }
2333}
2334
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002335// Compares the reference and test arrays over a region around the expected
2336// delay. Finds the highest SNR in that region and adds the variance and squared
2337// error results to the supplied accumulators.
2338void UpdateBestSNR(const float* ref,
2339 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002340 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002341 int expected_delay,
2342 double* variance_acc,
2343 double* sq_error_acc) {
2344 double best_snr = std::numeric_limits<double>::min();
2345 double best_variance = 0;
2346 double best_sq_error = 0;
2347 // Search over a region of eight samples around the expected delay.
2348 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2349 ++delay) {
2350 double sq_error = 0;
2351 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002352 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002353 double error = test[i + delay] - ref[i];
2354 sq_error += error * error;
2355 variance += ref[i] * ref[i];
2356 }
2357
2358 if (sq_error == 0) {
2359 *variance_acc += variance;
2360 return;
2361 }
2362 double snr = variance / sq_error;
2363 if (snr > best_snr) {
2364 best_snr = snr;
2365 best_variance = variance;
2366 best_sq_error = sq_error;
2367 }
2368 }
2369
2370 *variance_acc += best_variance;
2371 *sq_error_acc += best_sq_error;
2372}
2373
2374// Used to test a multitude of sample rate and channel combinations. It works
2375// by first producing a set of reference files (in SetUpTestCase) that are
2376// assumed to be correct, as the used parameters are verified by other tests
2377// in this collection. Primarily the reference files are all produced at
2378// "native" rates which do not involve any resampling.
2379
2380// Each test pass produces an output file with a particular format. The output
2381// is matched against the reference file closest to its internal processing
2382// format. If necessary the output is resampled back to its process format.
2383// Due to the resampling distortion, we don't expect identical results, but
2384// enforce SNR thresholds which vary depending on the format. 0 is a special
2385// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002386typedef std::tr1::tuple<int, int, int, int, double, double>
2387 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002388class AudioProcessingTest
2389 : public testing::TestWithParam<AudioProcessingTestData> {
2390 public:
2391 AudioProcessingTest()
2392 : input_rate_(std::tr1::get<0>(GetParam())),
2393 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002394 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2395 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2396 expected_snr_(std::tr1::get<4>(GetParam())),
2397 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002398
2399 virtual ~AudioProcessingTest() {}
2400
2401 static void SetUpTestCase() {
2402 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002403 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002404 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002405 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2406 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2407 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002408 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002409 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2410 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2411 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002412 }
2413 }
2414 }
2415 }
2416
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002417 static void TearDownTestCase() {
2418 ClearTempFiles();
2419 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002420
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002421 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002422 // to a file specified with |output_file_prefix|. Both forward and reverse
2423 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002424 static void ProcessFormat(int input_rate,
2425 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002426 int reverse_input_rate,
2427 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002428 size_t num_input_channels,
2429 size_t num_output_channels,
2430 size_t num_reverse_input_channels,
2431 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002433 Config config;
2434 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002435 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002436 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002437
ekmeyerson60d9b332015-08-14 10:35:55 -07002438 ProcessingConfig processing_config = {
2439 {{input_rate, num_input_channels},
2440 {output_rate, num_output_channels},
2441 {reverse_input_rate, num_reverse_input_channels},
2442 {reverse_output_rate, num_reverse_output_channels}}};
2443 ap->Initialize(processing_config);
2444
2445 FILE* far_file =
2446 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002447 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002448 FILE* out_file =
2449 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2450 reverse_input_rate, reverse_output_rate,
2451 num_input_channels, num_output_channels,
2452 num_reverse_input_channels,
2453 num_reverse_output_channels, kForward).c_str(),
2454 "wb");
2455 FILE* rev_out_file =
2456 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2457 reverse_input_rate, reverse_output_rate,
2458 num_input_channels, num_output_channels,
2459 num_reverse_input_channels,
2460 num_reverse_output_channels, kReverse).c_str(),
2461 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002462 ASSERT_TRUE(far_file != NULL);
2463 ASSERT_TRUE(near_file != NULL);
2464 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002465 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002466
2467 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2468 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002469 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2470 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002471 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2472 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002473 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2474 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002475
2476 // Temporary buffers.
2477 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002478 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2479 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002480 std::unique_ptr<float[]> float_data(new float[max_length]);
2481 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002482
2483 int analog_level = 127;
2484 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2485 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002486 EXPECT_NOERR(ap->ProcessReverseStream(
2487 rev_cb.channels(), processing_config.reverse_input_stream(),
2488 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002489
2490 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2491 ap->echo_cancellation()->set_stream_drift_samples(0);
2492 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2493
2494 EXPECT_NOERR(ap->ProcessStream(
2495 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002496 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002497 input_rate,
2498 LayoutFromChannels(num_input_channels),
2499 output_rate,
2500 LayoutFromChannels(num_output_channels),
2501 out_cb.channels()));
2502
ekmeyerson60d9b332015-08-14 10:35:55 -07002503 // Dump forward output to file.
2504 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002505 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002506 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002507
pkasting25702cb2016-01-08 13:50:27 -08002508 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002509 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002510 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002511
ekmeyerson60d9b332015-08-14 10:35:55 -07002512 // Dump reverse output to file.
2513 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2514 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002515 size_t rev_out_length =
2516 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002517
pkasting25702cb2016-01-08 13:50:27 -08002518 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002519 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2520 rev_out_file));
2521
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002522 analog_level = ap->gain_control()->stream_analog_level();
2523 }
2524 fclose(far_file);
2525 fclose(near_file);
2526 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002527 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002528 }
2529
2530 protected:
2531 int input_rate_;
2532 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002533 int reverse_input_rate_;
2534 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002535 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002536 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002537};
2538
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002539TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002540 struct ChannelFormat {
2541 int num_input;
2542 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002543 int num_reverse_input;
2544 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002545 };
2546 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002547 {1, 1, 1, 1},
2548 {1, 1, 2, 1},
2549 {2, 1, 1, 1},
2550 {2, 1, 2, 1},
2551 {2, 2, 1, 1},
2552 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002553 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002554
pkasting25702cb2016-01-08 13:50:27 -08002555 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002556 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2557 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2558 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002559
ekmeyerson60d9b332015-08-14 10:35:55 -07002560 // Verify output for both directions.
2561 std::vector<StreamDirection> stream_directions;
2562 stream_directions.push_back(kForward);
2563 stream_directions.push_back(kReverse);
2564 for (StreamDirection file_direction : stream_directions) {
2565 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2566 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2567 const int out_num =
2568 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2569 const double expected_snr =
2570 file_direction ? expected_reverse_snr_ : expected_snr_;
2571
2572 const int min_ref_rate = std::min(in_rate, out_rate);
2573 int ref_rate;
2574
2575 if (min_ref_rate > 32000) {
2576 ref_rate = 48000;
2577 } else if (min_ref_rate > 16000) {
2578 ref_rate = 32000;
2579 } else if (min_ref_rate > 8000) {
2580 ref_rate = 16000;
2581 } else {
2582 ref_rate = 8000;
2583 }
aluebs776593b2016-03-15 14:04:58 -07002584#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002585 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002586 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002587 }
2588#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002589 FILE* out_file = fopen(
2590 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2591 reverse_output_rate_, cf[i].num_input,
2592 cf[i].num_output, cf[i].num_reverse_input,
2593 cf[i].num_reverse_output, file_direction).c_str(),
2594 "rb");
2595 // The reference files always have matching input and output channels.
2596 FILE* ref_file = fopen(
2597 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2598 cf[i].num_output, cf[i].num_output,
2599 cf[i].num_reverse_output, cf[i].num_reverse_output,
2600 file_direction).c_str(),
2601 "rb");
2602 ASSERT_TRUE(out_file != NULL);
2603 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002604
pkasting25702cb2016-01-08 13:50:27 -08002605 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2606 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002607 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002608 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002609 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002610 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002611 // Data from the resampled output, in case the reference and output rates
2612 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002613 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002614
ekmeyerson60d9b332015-08-14 10:35:55 -07002615 PushResampler<float> resampler;
2616 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002617
ekmeyerson60d9b332015-08-14 10:35:55 -07002618 // Compute the resampling delay of the output relative to the reference,
2619 // to find the region over which we should search for the best SNR.
2620 float expected_delay_sec = 0;
2621 if (in_rate != ref_rate) {
2622 // Input resampling delay.
2623 expected_delay_sec +=
2624 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2625 }
2626 if (out_rate != ref_rate) {
2627 // Output resampling delay.
2628 expected_delay_sec +=
2629 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2630 // Delay of converting the output back to its processing rate for
2631 // testing.
2632 expected_delay_sec +=
2633 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2634 }
2635 int expected_delay =
2636 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002637
ekmeyerson60d9b332015-08-14 10:35:55 -07002638 double variance = 0;
2639 double sq_error = 0;
2640 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2641 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2642 float* out_ptr = out_data.get();
2643 if (out_rate != ref_rate) {
2644 // Resample the output back to its internal processing rate if
2645 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002646 ASSERT_EQ(ref_length,
2647 static_cast<size_t>(resampler.Resample(
2648 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002649 out_ptr = cmp_data.get();
2650 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002651
ekmeyerson60d9b332015-08-14 10:35:55 -07002652 // Update the |sq_error| and |variance| accumulators with the highest
2653 // SNR of reference vs output.
2654 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2655 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002656 }
2657
ekmeyerson60d9b332015-08-14 10:35:55 -07002658 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2659 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2660 << cf[i].num_input << ", " << cf[i].num_output << ", "
2661 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2662 << ", " << file_direction << "): ";
2663 if (sq_error > 0) {
2664 double snr = 10 * log10(variance / sq_error);
2665 EXPECT_GE(snr, expected_snr);
2666 EXPECT_NE(0, expected_snr);
2667 std::cout << "SNR=" << snr << " dB" << std::endl;
2668 } else {
aluebs776593b2016-03-15 14:04:58 -07002669 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002670 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002671
ekmeyerson60d9b332015-08-14 10:35:55 -07002672 fclose(out_file);
2673 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002674 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002675 }
2676}
2677
2678#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2679INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002680 CommonFormats,
2681 AudioProcessingTest,
2682 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002683 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2684 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002685 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2686 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2687 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2688 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2689 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2690 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2691 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2692 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2693 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002694
ekmeyerson60d9b332015-08-14 10:35:55 -07002695 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2696 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2697 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2698 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2699 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2700 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2701 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2702 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2703 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2704 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2705 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2706 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002707
ekmeyerson60d9b332015-08-14 10:35:55 -07002708 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2709 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2710 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2711 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2712 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2713 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2714 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2715 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2716 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2717 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2718 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2719 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002720
ekmeyerson60d9b332015-08-14 10:35:55 -07002721 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2722 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2723 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2724 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2725 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2726 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2727 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2728 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2729 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2730 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002731 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002732 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002733
2734#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2735INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002736 CommonFormats,
2737 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002738 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2739 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2740 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2741 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2742 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2743 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002744 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2745 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2746 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2747 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2748 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2749 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002750
aluebs776593b2016-03-15 14:04:58 -07002751 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2752 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2753 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002754 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2755 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2756 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2757 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2758 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2759 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2760 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2761 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2762 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002763
aluebs776593b2016-03-15 14:04:58 -07002764 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2765 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2766 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2767 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2768 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2769 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2770 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2771 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2772 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002773 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2774 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2775 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002776
ekmeyerson60d9b332015-08-14 10:35:55 -07002777 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2778 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2779 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2780 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2781 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2782 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2783 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2784 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2785 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2786 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002787 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002788 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002789#endif
2790
niklase@google.com470e71d2011-07-07 08:21:25 +00002791} // namespace
peahc19f3122016-10-07 14:54:10 -07002792
2793TEST(ApmConfiguration, DefaultBehavior) {
2794 // Verify that the level controller is default off, it can be activated using
2795 // the config, and that the default initial level is maintained after the
2796 // config has been applied.
2797 std::unique_ptr<AudioProcessingImpl> apm(
2798 new AudioProcessingImpl(webrtc::Config()));
2799 AudioProcessing::Config config;
2800 EXPECT_FALSE(apm->config_.level_controller.enabled);
2801 // TODO(peah): Add test for the existence of the level controller object once
2802 // that is created only when that is specified in the config.
2803 // TODO(peah): Remove the testing for
2804 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2805 // is instead used to activate the level controller.
2806 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2807 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2808 apm->config_.level_controller.initial_peak_level_dbfs,
2809 std::numeric_limits<float>::epsilon());
2810 config.level_controller.enabled = true;
2811 apm->ApplyConfig(config);
2812 EXPECT_TRUE(apm->config_.level_controller.enabled);
2813 // TODO(peah): Add test for the existence of the level controller object once
2814 // that is created only when the that is specified in the config.
2815 // TODO(peah): Remove the testing for
2816 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2817 // is instead used to activate the level controller.
2818 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2819 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2820 apm->config_.level_controller.initial_peak_level_dbfs,
2821 std::numeric_limits<float>::epsilon());
2822}
2823
2824TEST(ApmConfiguration, ValidConfigBehavior) {
2825 // Verify that the initial level can be specified and is retained after the
2826 // config has been applied.
2827 std::unique_ptr<AudioProcessingImpl> apm(
2828 new AudioProcessingImpl(webrtc::Config()));
2829 AudioProcessing::Config config;
2830 config.level_controller.initial_peak_level_dbfs = -50.f;
2831 apm->ApplyConfig(config);
2832 EXPECT_FALSE(apm->config_.level_controller.enabled);
2833 // TODO(peah): Add test for the existence of the level controller object once
2834 // that is created only when the that is specified in the config.
2835 // TODO(peah): Remove the testing for
2836 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2837 // is instead used to activate the level controller.
2838 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2839 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2840 std::numeric_limits<float>::epsilon());
2841}
2842
2843TEST(ApmConfiguration, InValidConfigBehavior) {
2844 // Verify that the config is properly reset when nonproper values are applied
2845 // for the initial level.
2846
2847 // Verify that the config is properly reset when the specified initial peak
2848 // level is too low.
2849 std::unique_ptr<AudioProcessingImpl> apm(
2850 new AudioProcessingImpl(webrtc::Config()));
2851 AudioProcessing::Config config;
2852 config.level_controller.enabled = true;
2853 config.level_controller.initial_peak_level_dbfs = -101.f;
2854 apm->ApplyConfig(config);
2855 EXPECT_FALSE(apm->config_.level_controller.enabled);
2856 // TODO(peah): Add test for the existence of the level controller object once
2857 // that is created only when the that is specified in the config.
2858 // TODO(peah): Remove the testing for
2859 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2860 // is instead used to activate the level controller.
2861 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2862 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2863 apm->config_.level_controller.initial_peak_level_dbfs,
2864 std::numeric_limits<float>::epsilon());
2865
2866 // Verify that the config is properly reset when the specified initial peak
2867 // level is too high.
2868 apm.reset(new AudioProcessingImpl(webrtc::Config()));
2869 config = AudioProcessing::Config();
2870 config.level_controller.enabled = true;
2871 config.level_controller.initial_peak_level_dbfs = 1.f;
2872 apm->ApplyConfig(config);
2873 EXPECT_FALSE(apm->config_.level_controller.enabled);
2874 // TODO(peah): Add test for the existence of the level controller object once
2875 // that is created only when that is specified in the config.
2876 // TODO(peah): Remove the testing for
2877 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2878 // is instead used to activate the level controller.
2879 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2880 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2881 apm->config_.level_controller.initial_peak_level_dbfs,
2882 std::numeric_limits<float>::epsilon());
2883}
2884
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002885} // namespace webrtc