blob: 799063dba72661e74f1ff4e4ee8cf50948340662 [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) {
yujo36b1a5f2017-06-12 12:45:32 -070090 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000091}
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
yujo36b1a5f2017-06-12 12:45:32 -0700130void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800131 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) {
yujo36b1a5f2017-06-12 12:45:32 -0700137 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
139 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700140 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000141 }
142}
143
144void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800145 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700146 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700147 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700148 frame_data[i] = left;
149 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000150 }
151}
152
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000153void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700154 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700155 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
156 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700157 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000158 }
159}
160
andrew@webrtc.org81865342012-10-27 00:28:27 +0000161bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000162 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000163 return false;
164 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000165 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166 return false;
167 }
yujo36b1a5f2017-06-12 12:45:32 -0700168 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000169 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 return false;
172 }
173 return true;
174}
175
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000176void EnableAllAPComponents(AudioProcessing* ap) {
177#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
178 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
179
180 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
181 EXPECT_NOERR(ap->gain_control()->Enable(true));
182#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
183 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
184 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
185 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
186 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
187
188 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
189 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
190 EXPECT_NOERR(ap->gain_control()->Enable(true));
191#endif
192
peah8271d042016-11-22 07:24:52 -0800193 AudioProcessing::Config apm_config;
194 apm_config.high_pass_filter.enabled = true;
195 ap->ApplyConfig(apm_config);
196
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000197 EXPECT_NOERR(ap->level_estimator()->Enable(true));
198 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
199
200 EXPECT_NOERR(ap->voice_detection()->Enable(true));
201}
202
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000203// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000204template <class T>
205T AbsValue(T a) {
206 return a > 0 ? a: -a;
207}
208
209int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800210 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700211 const int16_t* frame_data = frame.data();
212 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800213 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700214 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000215 }
216
217 return max_data;
218}
219
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000220#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000221void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000222 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700223 EXPECT_EQ(reference.instant(), test.instant);
224 EXPECT_EQ(reference.average(), test.average);
225 EXPECT_EQ(reference.maximum(), test.maximum);
226 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000227}
228
229void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000230 audioproc::Test::Statistic* msg) {
231 msg->set_instant(output.instant);
232 msg->set_average(output.average);
233 msg->set_maximum(output.maximum);
234 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000235}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000236#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000238void OpenFileAndWriteMessage(const std::string filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700239 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 ASSERT_TRUE(file != NULL);
242
243 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800245 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000246 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000247
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000248 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000249 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000250 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000251 fclose(file);
252}
253
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000254std::string ResourceFilePath(std::string name, int sample_rate_hz) {
255 std::ostringstream ss;
256 // Resource files are all stereo.
257 ss << name << sample_rate_hz / 1000 << "_stereo";
258 return test::ResourcePath(ss.str(), "pcm");
259}
260
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000261// Temporary filenames unique to this process. Used to be able to run these
262// tests in parallel as each process needs to be running in isolation they can't
263// have competing filenames.
264std::map<std::string, std::string> temp_filenames;
265
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000266std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000267 int input_rate,
268 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700269 int reverse_input_rate,
270 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800271 size_t num_input_channels,
272 size_t num_output_channels,
273 size_t num_reverse_input_channels,
274 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700275 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000276 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700277 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
278 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279 if (num_output_channels == 1) {
280 ss << "mono";
281 } else if (num_output_channels == 2) {
282 ss << "stereo";
283 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700284 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000285 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700286 ss << output_rate / 1000;
287 if (num_reverse_output_channels == 1) {
288 ss << "_rmono";
289 } else if (num_reverse_output_channels == 2) {
290 ss << "_rstereo";
291 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700292 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700293 }
294 ss << reverse_output_rate / 1000;
295 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000296
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000297 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700298 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000299 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
300 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000301}
302
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000303void ClearTempFiles() {
304 for (auto& kv : temp_filenames)
305 remove(kv.second.c_str());
306}
307
mbonadei7c2c8432017-04-07 00:59:12 -0700308void OpenFileAndReadMessage(std::string filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000309 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000310 ASSERT_TRUE(file != NULL);
311 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000312 fclose(file);
313}
314
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000315// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
316// stereo) file, converts to deinterleaved float (optionally downmixing) and
317// returns the result in |cb|. Returns false if the file ended (or on error) and
318// true otherwise.
319//
320// |int_data| and |float_data| are just temporary space that must be
321// sufficiently large to hold the 10 ms chunk.
322bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
323 ChannelBuffer<float>* cb) {
324 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000325 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000326 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
327 if (read_count != frame_size) {
328 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700329 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000330 return false; // This is expected.
331 }
332
333 S16ToFloat(int_data, frame_size, float_data);
334 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000335 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000336 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000337 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000338 cb->channels());
339 }
340
341 return true;
342}
343
niklase@google.com470e71d2011-07-07 08:21:25 +0000344class ApmTest : public ::testing::Test {
345 protected:
346 ApmTest();
347 virtual void SetUp();
348 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000349
350 static void SetUpTestCase() {
351 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000352 }
353
354 static void TearDownTestCase() {
355 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000356 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000357 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000358
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000359 // Used to select between int and float interface tests.
360 enum Format {
361 kIntFormat,
362 kFloatFormat
363 };
364
365 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000366 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000367 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800368 size_t num_input_channels,
369 size_t num_output_channels,
370 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000371 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000372 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000373 void EnableAllComponents();
374 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000375 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000376 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000377 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
378 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000379 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000380 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
381 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700382 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800383 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700384 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800385 void TestChangingForwardChannels(size_t num_in_channels,
386 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700387 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800388 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700389 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000390 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
391 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000392 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393 int ProcessStreamChooser(Format format);
394 int AnalyzeReverseStreamChooser(Format format);
395 void ProcessDebugDump(const std::string& in_filename,
396 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800397 Format format,
398 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000399 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000400
401 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000402 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800403 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000404 AudioFrame* frame_;
405 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800406 std::unique_ptr<ChannelBuffer<float> > float_cb_;
407 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000408 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800409 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 FILE* far_file_;
411 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000412 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413};
414
415ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000416 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000417#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800418 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
419 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000420#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000421#if defined(WEBRTC_MAC)
422 // A different file for Mac is needed because on this platform the AEC
423 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800424 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
425 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000426#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800427 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
428 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000429#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000430#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000431 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000432 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000433 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000434 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000435 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000436 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000437 out_file_(NULL) {
438 Config config;
439 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
440 apm_.reset(AudioProcessing::Create(config));
441}
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
443void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000444 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
446 frame_ = new AudioFrame();
447 revframe_ = new AudioFrame();
448
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000449 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000450}
451
452void ApmTest::TearDown() {
453 if (frame_) {
454 delete frame_;
455 }
456 frame_ = NULL;
457
458 if (revframe_) {
459 delete revframe_;
460 }
461 revframe_ = NULL;
462
463 if (far_file_) {
464 ASSERT_EQ(0, fclose(far_file_));
465 }
466 far_file_ = NULL;
467
468 if (near_file_) {
469 ASSERT_EQ(0, fclose(near_file_));
470 }
471 near_file_ = NULL;
472
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000473 if (out_file_) {
474 ASSERT_EQ(0, fclose(out_file_));
475 }
476 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000477}
478
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000479void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700481 ap->Initialize(
482 {{{frame_->sample_rate_hz_, frame_->num_channels_},
483 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700484 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700485 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000486}
487
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000488void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000489 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000490 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800491 size_t num_input_channels,
492 size_t num_output_channels,
493 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000494 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000495 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000496 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000497 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000498
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000499 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
500 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000501 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000502
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000503 if (far_file_) {
504 ASSERT_EQ(0, fclose(far_file_));
505 }
506 std::string filename = ResourceFilePath("far", sample_rate_hz);
507 far_file_ = fopen(filename.c_str(), "rb");
508 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
509 filename << "\n";
510
511 if (near_file_) {
512 ASSERT_EQ(0, fclose(near_file_));
513 }
514 filename = ResourceFilePath("near", sample_rate_hz);
515 near_file_ = fopen(filename.c_str(), "rb");
516 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
517 filename << "\n";
518
519 if (open_output_file) {
520 if (out_file_) {
521 ASSERT_EQ(0, fclose(out_file_));
522 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700523 filename = OutputFilePath(
524 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
525 reverse_sample_rate_hz, num_input_channels, num_output_channels,
526 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000527 out_file_ = fopen(filename.c_str(), "wb");
528 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
529 filename << "\n";
530 }
531}
532
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000533void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000534 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000535}
536
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000537bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
538 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000539 // The files always contain stereo audio.
540 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700541 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000542 sizeof(int16_t),
543 frame_size,
544 file);
545 if (read_count != frame_size) {
546 // Check that the file really ended.
547 EXPECT_NE(0, feof(file));
548 return false; // This is expected.
549 }
550
551 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700552 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000553 frame->samples_per_channel_);
554 }
555
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000557 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000558 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000559 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000560}
561
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000562bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
563 return ReadFrame(file, frame, NULL);
564}
565
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000566// If the end of the file has been reached, rewind it and attempt to read the
567// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000568void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
569 ChannelBuffer<float>* cb) {
570 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000571 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000573 }
574}
575
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000576void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
577 ReadFrameWithRewind(file, frame, NULL);
578}
579
andrew@webrtc.org81865342012-10-27 00:28:27 +0000580void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
581 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000582 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000583 EXPECT_EQ(apm_->kNoError,
584 apm_->gain_control()->set_stream_analog_level(127));
585 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000586}
587
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000588int ApmTest::ProcessStreamChooser(Format format) {
589 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000590 return apm_->ProcessStream(frame_);
591 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000592 return apm_->ProcessStream(float_cb_->channels(),
593 frame_->samples_per_channel_,
594 frame_->sample_rate_hz_,
595 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000596 output_sample_rate_hz_,
597 LayoutFromChannels(num_output_channels_),
598 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000599}
600
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000601int ApmTest::AnalyzeReverseStreamChooser(Format format) {
602 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700603 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000604 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000605 return apm_->AnalyzeReverseStream(
606 revfloat_cb_->channels(),
607 revframe_->samples_per_channel_,
608 revframe_->sample_rate_hz_,
609 LayoutFromChannels(revframe_->num_channels_));
610}
611
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000612void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
613 int delay_min, int delay_max) {
614 // The |revframe_| and |frame_| should include the proper frame information,
615 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000616 AudioFrame tmp_frame;
617 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000618 bool causal = true;
619
620 tmp_frame.CopyFrom(*revframe_);
621 SetFrameTo(&tmp_frame, 0);
622
623 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
624 // Initialize the |frame_queue| with empty frames.
625 int frame_delay = delay_ms / 10;
626 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000627 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000628 frame->CopyFrom(tmp_frame);
629 frame_queue.push(frame);
630 frame_delay++;
631 causal = false;
632 }
633 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000634 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000635 frame->CopyFrom(tmp_frame);
636 frame_queue.push(frame);
637 frame_delay--;
638 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000639 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
640 // need enough frames with audio to have reliable estimates, but as few as
641 // possible to keep processing time down. 4.5 seconds seemed to be a good
642 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000643 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000644 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000645 frame->CopyFrom(tmp_frame);
646 // Use the near end recording, since that has more speech in it.
647 ASSERT_TRUE(ReadFrame(near_file_, frame));
648 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000649 AudioFrame* reverse_frame = frame;
650 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000651 if (!causal) {
652 reverse_frame = frame_queue.front();
653 // When we call ProcessStream() the frame is modified, so we can't use the
654 // pointer directly when things are non-causal. Use an intermediate frame
655 // and copy the data.
656 process_frame = &tmp_frame;
657 process_frame->CopyFrom(*frame);
658 }
aluebsb0319552016-03-17 20:39:53 -0700659 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000660 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
661 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
662 frame = frame_queue.front();
663 frame_queue.pop();
664 delete frame;
665
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000666 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000667 int median;
668 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000669 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000670 // Discard the first delay metrics to avoid convergence effects.
671 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000672 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
673 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000674 }
675 }
676
677 rewind(near_file_);
678 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000679 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000680 frame_queue.pop();
681 delete frame;
682 }
683 // Calculate expected delay estimate and acceptable regions. Further,
684 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700685 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700686 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700687 const int expected_median =
688 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
689 const int expected_median_high = rtc::SafeClamp<int>(
690 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700691 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700692 const int expected_median_low = rtc::SafeClamp<int>(
693 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700694 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000695 // Verify delay metrics.
696 int median;
697 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000698 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000699 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000700 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
701 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000702 EXPECT_GE(expected_median_high, median);
703 EXPECT_LE(expected_median_low, median);
704}
705
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000706void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000707 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000708 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000709
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000710 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000712 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000713 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000714
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000715 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000716 EXPECT_EQ(apm_->kNoError,
717 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000718 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000719 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000720 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000721
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000722 // Other stream parameters set correctly.
723 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 EXPECT_EQ(apm_->kNoError,
725 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000726 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000727 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000728 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000729 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000730 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
731 EXPECT_EQ(apm_->kNoError,
732 apm_->echo_cancellation()->enable_drift_compensation(false));
733
734 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000735 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000736 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000737 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000738 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000739
740 // Resets after successful ProcessStream().
741 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000743 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000744 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000745
746 // Other stream parameters set correctly.
747 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
748 EXPECT_EQ(apm_->kNoError,
749 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000750 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000751 EXPECT_EQ(apm_->kNoError,
752 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000753 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000754 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000755 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
756
757 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000758 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000759 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000760
761 // Resets after successful ProcessStream().
762 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000763 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000764 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000765 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000766 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000767
768 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
770 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
771 EXPECT_EQ(apm_->kNoError,
772 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000773 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000774 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000775
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000776 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000777 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000779 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000780 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000781
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000782 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000784 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 EXPECT_EQ(apm_->kNoError,
786 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000787 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000788}
789
790TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000791 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000792}
793
794TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000795 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000796}
797
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000798TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
799 EXPECT_EQ(0, apm_->delay_offset_ms());
800 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
801 EXPECT_EQ(50, apm_->stream_delay_ms());
802}
803
804TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
805 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000806 apm_->set_delay_offset_ms(100);
807 EXPECT_EQ(100, apm_->delay_offset_ms());
808 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000809 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000810 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
811 EXPECT_EQ(200, apm_->stream_delay_ms());
812
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000813 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000814 apm_->set_delay_offset_ms(-50);
815 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000816 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
817 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000818 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
819 EXPECT_EQ(50, apm_->stream_delay_ms());
820}
821
Michael Graczyk86c6d332015-07-23 11:41:39 -0700822void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800823 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700824 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000825 frame_->num_channels_ = num_channels;
826 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700827 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000828}
829
Michael Graczyk86c6d332015-07-23 11:41:39 -0700830void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800831 size_t num_in_channels,
832 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700833 AudioProcessing::Error expected_return) {
834 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
835 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
836
837 EXPECT_EQ(expected_return,
838 apm_->ProcessStream(float_cb_->channels(), input_stream,
839 output_stream, float_cb_->channels()));
840}
841
842void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800843 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700844 AudioProcessing::Error expected_return) {
845 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700846 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
847 {output_sample_rate_hz_, apm_->num_output_channels()},
848 {frame_->sample_rate_hz_, num_rev_channels},
849 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700850
ekmeyerson60d9b332015-08-14 10:35:55 -0700851 EXPECT_EQ(
852 expected_return,
853 apm_->ProcessReverseStream(
854 float_cb_->channels(), processing_config.reverse_input_stream(),
855 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700856}
857
858TEST_F(ApmTest, ChannelsInt16Interface) {
859 // Testing number of invalid and valid channels.
860 Init(16000, 16000, 16000, 4, 4, 4, false);
861
862 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
863
Peter Kasting69558702016-01-12 16:26:35 -0800864 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700865 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000866 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000867 }
868}
869
Michael Graczyk86c6d332015-07-23 11:41:39 -0700870TEST_F(ApmTest, Channels) {
871 // Testing number of invalid and valid channels.
872 Init(16000, 16000, 16000, 4, 4, 4, false);
873
874 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
875 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
876
Peter Kasting69558702016-01-12 16:26:35 -0800877 for (size_t i = 1; i < 4; ++i) {
878 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700879 // Output channels much be one or match input channels.
880 if (j == 1 || i == j) {
881 TestChangingForwardChannels(i, j, kNoErr);
882 TestChangingReverseChannels(i, kNoErr);
883
884 EXPECT_EQ(i, apm_->num_input_channels());
885 EXPECT_EQ(j, apm_->num_output_channels());
886 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800887 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700888 } else {
889 TestChangingForwardChannels(i, j,
890 AudioProcessing::kBadNumberChannelsError);
891 }
892 }
893 }
894}
895
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000896TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000897 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000898 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000899 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000900 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700901 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800902 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000903 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000904 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000905 }
906}
907
niklase@google.com470e71d2011-07-07 08:21:25 +0000908TEST_F(ApmTest, EchoCancellation) {
909 EXPECT_EQ(apm_->kNoError,
910 apm_->echo_cancellation()->enable_drift_compensation(true));
911 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
912 EXPECT_EQ(apm_->kNoError,
913 apm_->echo_cancellation()->enable_drift_compensation(false));
914 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
915
niklase@google.com470e71d2011-07-07 08:21:25 +0000916 EchoCancellation::SuppressionLevel level[] = {
917 EchoCancellation::kLowSuppression,
918 EchoCancellation::kModerateSuppression,
919 EchoCancellation::kHighSuppression,
920 };
pkasting25702cb2016-01-08 13:50:27 -0800921 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000922 EXPECT_EQ(apm_->kNoError,
923 apm_->echo_cancellation()->set_suppression_level(level[i]));
924 EXPECT_EQ(level[i],
925 apm_->echo_cancellation()->suppression_level());
926 }
927
928 EchoCancellation::Metrics metrics;
929 EXPECT_EQ(apm_->kNotEnabledError,
930 apm_->echo_cancellation()->GetMetrics(&metrics));
931
ivoc3e9a5372016-10-28 07:55:33 -0700932 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
933 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
934
niklase@google.com470e71d2011-07-07 08:21:25 +0000935 EXPECT_EQ(apm_->kNoError,
936 apm_->echo_cancellation()->enable_metrics(true));
937 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
938 EXPECT_EQ(apm_->kNoError,
939 apm_->echo_cancellation()->enable_metrics(false));
940 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
941
ivoc48dfab52016-10-28 03:29:31 -0700942 EXPECT_EQ(apm_->kNoError,
943 apm_->echo_cancellation()->enable_delay_logging(true));
944 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
945 EXPECT_EQ(apm_->kNoError,
946 apm_->echo_cancellation()->enable_delay_logging(false));
947 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
948
ivoc3e9a5372016-10-28 07:55:33 -0700949 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
950 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
951
952 int median = 0;
953 int std = 0;
954 float poor_fraction = 0;
955 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
956 &median, &std, &poor_fraction));
957
niklase@google.com470e71d2011-07-07 08:21:25 +0000958 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
959 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
960 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
961 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000962
963 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
964 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
965 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
966 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
967 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
968 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000969}
970
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000971TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000972 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000973 // Enable AEC only.
974 EXPECT_EQ(apm_->kNoError,
975 apm_->echo_cancellation()->enable_drift_compensation(false));
976 EXPECT_EQ(apm_->kNoError,
977 apm_->echo_cancellation()->enable_metrics(false));
978 EXPECT_EQ(apm_->kNoError,
979 apm_->echo_cancellation()->enable_delay_logging(true));
980 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000981 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700982 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000983 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000984
985 // Internally in the AEC the amount of lookahead the delay estimation can
986 // handle is 15 blocks and the maximum delay is set to 60 blocks.
987 const int kLookaheadBlocks = 15;
988 const int kMaxDelayBlocks = 60;
989 // The AEC has a startup time before it actually starts to process. This
990 // procedure can flush the internal far-end buffer, which of course affects
991 // the delay estimation. Therefore, we set a system_delay high enough to
992 // avoid that. The smallest system_delay you can report without flushing the
993 // buffer is 66 ms in 8 kHz.
994 //
995 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
996 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
997 // delay estimation. This should be noted though. In case of test failure,
998 // this could be the cause.
999 const int kSystemDelayMs = 66;
1000 // Test a couple of corner cases and verify that the estimated delay is
1001 // within a valid region (set to +-1.5 blocks). Note that these cases are
1002 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001003 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001004 Init(kProcessSampleRates[i],
1005 kProcessSampleRates[i],
1006 kProcessSampleRates[i],
1007 2,
1008 2,
1009 2,
1010 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001011 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001012 const int num_ms_per_block =
1013 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001014 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1015 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1016
1017 // 1) Verify correct delay estimate at lookahead boundary.
1018 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1019 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1020 delay_max_ms);
1021 // 2) A delay less than maximum lookahead should give an delay estimate at
1022 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1023 delay_ms -= 20;
1024 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1025 delay_max_ms);
1026 // 3) Three values around zero delay. Note that we need to compensate for
1027 // the fake system_delay.
1028 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1029 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1030 delay_max_ms);
1031 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1032 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1033 delay_max_ms);
1034 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1035 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1036 delay_max_ms);
1037 // 4) Verify correct delay estimate at maximum delay boundary.
1038 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1039 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1040 delay_max_ms);
1041 // 5) A delay above the maximum delay should give an estimate at the
1042 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1043 delay_ms += 20;
1044 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1045 delay_max_ms);
1046 }
1047}
1048
niklase@google.com470e71d2011-07-07 08:21:25 +00001049TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001051 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1053 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1054
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 // Toggle routing modes
1056 EchoControlMobile::RoutingMode mode[] = {
1057 EchoControlMobile::kQuietEarpieceOrHeadset,
1058 EchoControlMobile::kEarpiece,
1059 EchoControlMobile::kLoudEarpiece,
1060 EchoControlMobile::kSpeakerphone,
1061 EchoControlMobile::kLoudSpeakerphone,
1062 };
pkasting25702cb2016-01-08 13:50:27 -08001063 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001064 EXPECT_EQ(apm_->kNoError,
1065 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1066 EXPECT_EQ(mode[i],
1067 apm_->echo_control_mobile()->routing_mode());
1068 }
1069 // Turn comfort noise off/on
1070 EXPECT_EQ(apm_->kNoError,
1071 apm_->echo_control_mobile()->enable_comfort_noise(false));
1072 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1073 EXPECT_EQ(apm_->kNoError,
1074 apm_->echo_control_mobile()->enable_comfort_noise(true));
1075 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001076 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001077 const size_t echo_path_size =
1078 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001079 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1080 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001081 EXPECT_EQ(apm_->kNullPointerError,
1082 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1083 EXPECT_EQ(apm_->kNullPointerError,
1084 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1085 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001086 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001087 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001088 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001089 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001090 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001091 echo_path_in[i] = echo_path_out[i] + 1;
1092 }
1093 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001094 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001095 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001096 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1097 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001098 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001099 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1100 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001101 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001102 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1103 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001104
1105 // Process a few frames with NS in the default disabled state. This exercises
1106 // a different codepath than with it enabled.
1107 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1108 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1109 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1110 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1111
niklase@google.com470e71d2011-07-07 08:21:25 +00001112 // Turn AECM off
1113 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1114 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1115}
1116
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001117TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001118 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001119 EXPECT_EQ(apm_->kNoError,
1120 apm_->gain_control()->set_mode(
1121 apm_->gain_control()->mode()));
1122
1123 GainControl::Mode mode[] = {
1124 GainControl::kAdaptiveAnalog,
1125 GainControl::kAdaptiveDigital,
1126 GainControl::kFixedDigital
1127 };
pkasting25702cb2016-01-08 13:50:27 -08001128 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001129 EXPECT_EQ(apm_->kNoError,
1130 apm_->gain_control()->set_mode(mode[i]));
1131 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1132 }
1133 // Testing invalid target levels
1134 EXPECT_EQ(apm_->kBadParameterError,
1135 apm_->gain_control()->set_target_level_dbfs(-3));
1136 EXPECT_EQ(apm_->kBadParameterError,
1137 apm_->gain_control()->set_target_level_dbfs(-40));
1138 // Testing valid target levels
1139 EXPECT_EQ(apm_->kNoError,
1140 apm_->gain_control()->set_target_level_dbfs(
1141 apm_->gain_control()->target_level_dbfs()));
1142
1143 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001144 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001145 EXPECT_EQ(apm_->kNoError,
1146 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1147 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1148 }
1149
1150 // Testing invalid compression gains
1151 EXPECT_EQ(apm_->kBadParameterError,
1152 apm_->gain_control()->set_compression_gain_db(-1));
1153 EXPECT_EQ(apm_->kBadParameterError,
1154 apm_->gain_control()->set_compression_gain_db(100));
1155
1156 // Testing valid compression gains
1157 EXPECT_EQ(apm_->kNoError,
1158 apm_->gain_control()->set_compression_gain_db(
1159 apm_->gain_control()->compression_gain_db()));
1160
1161 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001162 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001163 EXPECT_EQ(apm_->kNoError,
1164 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1165 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1166 }
1167
1168 // Testing limiter off/on
1169 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1170 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1171 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1172 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1173
1174 // Testing invalid level limits
1175 EXPECT_EQ(apm_->kBadParameterError,
1176 apm_->gain_control()->set_analog_level_limits(-1, 512));
1177 EXPECT_EQ(apm_->kBadParameterError,
1178 apm_->gain_control()->set_analog_level_limits(100000, 512));
1179 EXPECT_EQ(apm_->kBadParameterError,
1180 apm_->gain_control()->set_analog_level_limits(512, -1));
1181 EXPECT_EQ(apm_->kBadParameterError,
1182 apm_->gain_control()->set_analog_level_limits(512, 100000));
1183 EXPECT_EQ(apm_->kBadParameterError,
1184 apm_->gain_control()->set_analog_level_limits(512, 255));
1185
1186 // Testing valid level limits
1187 EXPECT_EQ(apm_->kNoError,
1188 apm_->gain_control()->set_analog_level_limits(
1189 apm_->gain_control()->analog_level_minimum(),
1190 apm_->gain_control()->analog_level_maximum()));
1191
1192 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001193 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001194 EXPECT_EQ(apm_->kNoError,
1195 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1196 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1197 }
1198
1199 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001200 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 EXPECT_EQ(apm_->kNoError,
1202 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1203 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1204 }
1205
1206 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1207
1208 // Turn AGC off
1209 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1210 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1211}
1212
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001213void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001214 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001215 EXPECT_EQ(apm_->kNoError,
1216 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1217 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1218
1219 int out_analog_level = 0;
1220 for (int i = 0; i < 2000; ++i) {
1221 ReadFrameWithRewind(near_file_, frame_);
1222 // Ensure the audio is at a low level, so the AGC will try to increase it.
1223 ScaleFrame(frame_, 0.25);
1224
1225 // Always pass in the same volume.
1226 EXPECT_EQ(apm_->kNoError,
1227 apm_->gain_control()->set_stream_analog_level(100));
1228 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1229 out_analog_level = apm_->gain_control()->stream_analog_level();
1230 }
1231
1232 // Ensure the AGC is still able to reach the maximum.
1233 EXPECT_EQ(255, out_analog_level);
1234}
1235
1236// Verifies that despite volume slider quantization, the AGC can continue to
1237// increase its volume.
1238TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001239 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001240 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1241 }
1242}
1243
1244void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001245 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001246 EXPECT_EQ(apm_->kNoError,
1247 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1248 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1249
1250 int out_analog_level = 100;
1251 for (int i = 0; i < 1000; ++i) {
1252 ReadFrameWithRewind(near_file_, frame_);
1253 // Ensure the audio is at a low level, so the AGC will try to increase it.
1254 ScaleFrame(frame_, 0.25);
1255
1256 EXPECT_EQ(apm_->kNoError,
1257 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1258 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1259 out_analog_level = apm_->gain_control()->stream_analog_level();
1260 }
1261
1262 // Ensure the volume was raised.
1263 EXPECT_GT(out_analog_level, 100);
1264 int highest_level_reached = out_analog_level;
1265 // Simulate a user manual volume change.
1266 out_analog_level = 100;
1267
1268 for (int i = 0; i < 300; ++i) {
1269 ReadFrameWithRewind(near_file_, frame_);
1270 ScaleFrame(frame_, 0.25);
1271
1272 EXPECT_EQ(apm_->kNoError,
1273 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1274 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1275 out_analog_level = apm_->gain_control()->stream_analog_level();
1276 // Check that AGC respected the manually adjusted volume.
1277 EXPECT_LT(out_analog_level, highest_level_reached);
1278 }
1279 // Check that the volume was still raised.
1280 EXPECT_GT(out_analog_level, 100);
1281}
1282
1283TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001284 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001285 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1286 }
1287}
1288
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001289#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1290TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1291 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001292 const size_t kSamplesPerChannel =
1293 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001294 const size_t kNumInputChannels = 2;
1295 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001296 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001297 const float kScaleFactor = 0.25f;
1298 Config config;
1299 std::vector<webrtc::Point> geometry;
1300 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1301 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1302 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001303 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001304 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001305 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001306 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001307 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1308 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1309 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001310 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1311 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001312 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1313 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001314 std::string filename = ResourceFilePath("far", kSampleRateHz);
1315 FILE* far_file = fopen(filename.c_str(), "rb");
1316 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1317 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1318 const int kDefaultCompressionGain =
1319 apm->gain_control()->compression_gain_db();
1320 bool is_target = false;
1321 EXPECT_CALL(*beamformer, is_target_present())
1322 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001323 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001324 ASSERT_TRUE(ReadChunk(far_file,
1325 int_data.get(),
1326 float_data.get(),
1327 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001328 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001329 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001330 src_buf.channels()[j][k] *= kScaleFactor;
1331 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001332 }
1333 EXPECT_EQ(kNoErr,
1334 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001335 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001336 kSampleRateHz,
1337 LayoutFromChannels(src_buf.num_channels()),
1338 kSampleRateHz,
1339 LayoutFromChannels(dest_buf.num_channels()),
1340 dest_buf.channels()));
1341 }
1342 EXPECT_EQ(kDefaultVolume,
1343 apm->gain_control()->stream_analog_level());
1344 EXPECT_EQ(kDefaultCompressionGain,
1345 apm->gain_control()->compression_gain_db());
1346 rewind(far_file);
1347 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001348 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001349 ASSERT_TRUE(ReadChunk(far_file,
1350 int_data.get(),
1351 float_data.get(),
1352 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001353 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001354 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001355 src_buf.channels()[j][k] *= kScaleFactor;
1356 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001357 }
1358 EXPECT_EQ(kNoErr,
1359 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001360 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001361 kSampleRateHz,
1362 LayoutFromChannels(src_buf.num_channels()),
1363 kSampleRateHz,
1364 LayoutFromChannels(dest_buf.num_channels()),
1365 dest_buf.channels()));
1366 }
1367 EXPECT_LT(kDefaultVolume,
1368 apm->gain_control()->stream_analog_level());
1369 EXPECT_LT(kDefaultCompressionGain,
1370 apm->gain_control()->compression_gain_db());
1371 ASSERT_EQ(0, fclose(far_file));
1372}
1373#endif
1374
niklase@google.com470e71d2011-07-07 08:21:25 +00001375TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001376 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001377 NoiseSuppression::Level level[] = {
1378 NoiseSuppression::kLow,
1379 NoiseSuppression::kModerate,
1380 NoiseSuppression::kHigh,
1381 NoiseSuppression::kVeryHigh
1382 };
pkasting25702cb2016-01-08 13:50:27 -08001383 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001384 EXPECT_EQ(apm_->kNoError,
1385 apm_->noise_suppression()->set_level(level[i]));
1386 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1387 }
1388
andrew@webrtc.org648af742012-02-08 01:57:29 +00001389 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001390 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1391 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1392 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1393 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1394}
1395
1396TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001397 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001398 AudioProcessing::Config apm_config;
1399 apm_config.high_pass_filter.enabled = true;
1400 apm_->ApplyConfig(apm_config);
1401 apm_config.high_pass_filter.enabled = false;
1402 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
1405TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001406 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001407 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001408 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001409
1410 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1411
1412 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1413 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1414
1415 // Run this test in wideband; in super-wb, the splitting filter distorts the
1416 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001417 frame_->samples_per_channel_ = 160;
1418 frame_->num_channels_ = 2;
1419 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001420
1421 // Min value if no frames have been processed.
1422 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1423
1424 // Min value on zero frames.
1425 SetFrameTo(frame_, 0);
1426 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1427 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1428 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1429
1430 // Try a few RMS values.
1431 // (These also test that the value resets after retrieving it.)
1432 SetFrameTo(frame_, 32767);
1433 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1434 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1435 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1436
1437 SetFrameTo(frame_, 30000);
1438 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1439 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1440 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1441
1442 SetFrameTo(frame_, 10000);
1443 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1444 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1445 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1446
1447 SetFrameTo(frame_, 10);
1448 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1449 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1450 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1451
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001452 // Verify reset after enable/disable.
1453 SetFrameTo(frame_, 32767);
1454 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1455 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1456 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1457 SetFrameTo(frame_, 1);
1458 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1459 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1460
1461 // Verify reset after initialize.
1462 SetFrameTo(frame_, 32767);
1463 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1464 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1465 SetFrameTo(frame_, 1);
1466 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1467 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001468}
1469
1470TEST_F(ApmTest, VoiceDetection) {
1471 // Test external VAD
1472 EXPECT_EQ(apm_->kNoError,
1473 apm_->voice_detection()->set_stream_has_voice(true));
1474 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1475 EXPECT_EQ(apm_->kNoError,
1476 apm_->voice_detection()->set_stream_has_voice(false));
1477 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1478
andrew@webrtc.org648af742012-02-08 01:57:29 +00001479 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001480 VoiceDetection::Likelihood likelihood[] = {
1481 VoiceDetection::kVeryLowLikelihood,
1482 VoiceDetection::kLowLikelihood,
1483 VoiceDetection::kModerateLikelihood,
1484 VoiceDetection::kHighLikelihood
1485 };
pkasting25702cb2016-01-08 13:50:27 -08001486 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001487 EXPECT_EQ(apm_->kNoError,
1488 apm_->voice_detection()->set_likelihood(likelihood[i]));
1489 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1490 }
1491
1492 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001493 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001494 EXPECT_EQ(apm_->kBadParameterError,
1495 apm_->voice_detection()->set_frame_size_ms(12));
1496
andrew@webrtc.org648af742012-02-08 01:57:29 +00001497 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001498 for (int i = 10; i <= 30; i += 10) {
1499 EXPECT_EQ(apm_->kNoError,
1500 apm_->voice_detection()->set_frame_size_ms(i));
1501 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1502 }
1503 */
1504
andrew@webrtc.org648af742012-02-08 01:57:29 +00001505 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001506 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1507 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1508 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1509 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1510
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001511 // Test that AudioFrame activity is maintained when VAD is disabled.
1512 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1513 AudioFrame::VADActivity activity[] = {
1514 AudioFrame::kVadActive,
1515 AudioFrame::kVadPassive,
1516 AudioFrame::kVadUnknown
1517 };
pkasting25702cb2016-01-08 13:50:27 -08001518 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001519 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001520 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001521 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001522 }
1523
1524 // Test that AudioFrame activity is set when VAD is enabled.
1525 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001526 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001527 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001528 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001529
niklase@google.com470e71d2011-07-07 08:21:25 +00001530 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1531}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001532
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001533TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1534 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1535 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1536 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1537 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1538 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1539 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1540 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1541}
1542
1543TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001544 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001545 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001546 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001547 AudioFrame frame_copy;
1548 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001549 for (int j = 0; j < 1000; j++) {
1550 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1551 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001552 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1553 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001554 }
1555 }
1556}
1557
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001558TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1559 // Test that ProcessStream copies input to output even with no processing.
1560 const size_t kSamples = 80;
1561 const int sample_rate = 8000;
1562 const float src[kSamples] = {
1563 -1.0f, 0.0f, 1.0f
1564 };
1565 float dest[kSamples] = {};
1566
1567 auto src_channels = &src[0];
1568 auto dest_channels = &dest[0];
1569
1570 apm_.reset(AudioProcessing::Create());
1571 EXPECT_NOERR(apm_->ProcessStream(
1572 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1573 sample_rate, LayoutFromChannels(1), &dest_channels));
1574
1575 for (size_t i = 0; i < kSamples; ++i) {
1576 EXPECT_EQ(src[i], dest[i]);
1577 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001578
1579 // Same for ProcessReverseStream.
1580 float rev_dest[kSamples] = {};
1581 auto rev_dest_channels = &rev_dest[0];
1582
1583 StreamConfig input_stream = {sample_rate, 1};
1584 StreamConfig output_stream = {sample_rate, 1};
1585 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1586 output_stream, &rev_dest_channels));
1587
1588 for (size_t i = 0; i < kSamples; ++i) {
1589 EXPECT_EQ(src[i], rev_dest[i]);
1590 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001591}
1592
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001593TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1594 EnableAllComponents();
1595
pkasting25702cb2016-01-08 13:50:27 -08001596 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001597 Init(kProcessSampleRates[i],
1598 kProcessSampleRates[i],
1599 kProcessSampleRates[i],
1600 2,
1601 2,
1602 2,
1603 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001604 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001605 ASSERT_EQ(0, feof(far_file_));
1606 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001607 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001608 CopyLeftToRightChannel(revframe_->mutable_data(),
1609 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001610
aluebsb0319552016-03-17 20:39:53 -07001611 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001612
yujo36b1a5f2017-06-12 12:45:32 -07001613 CopyLeftToRightChannel(frame_->mutable_data(),
1614 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001615 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1616
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001617 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001618 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001619 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001620 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001621 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001622 analog_level = apm_->gain_control()->stream_analog_level();
1623
yujo36b1a5f2017-06-12 12:45:32 -07001624 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001625 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001626 rewind(far_file_);
1627 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001628 }
1629}
1630
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001631TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001632 // Verify the filter is not active through undistorted audio when:
1633 // 1. No components are enabled...
1634 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001635 AudioFrame frame_copy;
1636 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001637 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1638 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1639 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1640
1641 // 2. Only the level estimator is enabled...
1642 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001643 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001644 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1645 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1646 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1647 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1648 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1649
1650 // 3. Only VAD is enabled...
1651 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001652 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001653 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1654 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1655 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1656 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1657 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1658
1659 // 4. Both VAD and the level estimator are enabled...
1660 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001661 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001662 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1663 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1664 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1665 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1666 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1667 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1668 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1669
1670 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001671 frame_->samples_per_channel_ = 160;
1672 frame_->num_channels_ = 2;
1673 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001674 // Enable AEC, which would require the filter in super-wb. We rely on the
1675 // first few frames of data being unaffected by the AEC.
1676 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1677 // behavior of the AEC. Think of something more robust.
1678 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001679 // Make sure we have extended filter enabled. This makes sure nothing is
1680 // touched until we have a farend frame.
1681 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001682 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001683 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001684 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001685 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001686 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001687 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001688 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1689 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001690 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001691 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1692 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1693
1694 // Check the test is valid. We should have distortion from the filter
1695 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001696 frame_->samples_per_channel_ = 320;
1697 frame_->num_channels_ = 2;
1698 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001699 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001700 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001701 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001702 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001703 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1704 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1705}
1706
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001707#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1708void ApmTest::ProcessDebugDump(const std::string& in_filename,
1709 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001710 Format format,
1711 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001712 FILE* in_file = fopen(in_filename.c_str(), "rb");
1713 ASSERT_TRUE(in_file != NULL);
1714 audioproc::Event event_msg;
1715 bool first_init = true;
1716
1717 while (ReadMessageFromFile(in_file, &event_msg)) {
1718 if (event_msg.type() == audioproc::Event::INIT) {
1719 const audioproc::Init msg = event_msg.init();
1720 int reverse_sample_rate = msg.sample_rate();
1721 if (msg.has_reverse_sample_rate()) {
1722 reverse_sample_rate = msg.reverse_sample_rate();
1723 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001724 int output_sample_rate = msg.sample_rate();
1725 if (msg.has_output_sample_rate()) {
1726 output_sample_rate = msg.output_sample_rate();
1727 }
1728
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001729 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001730 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001731 reverse_sample_rate,
1732 msg.num_input_channels(),
1733 msg.num_output_channels(),
1734 msg.num_reverse_channels(),
1735 false);
1736 if (first_init) {
1737 // StartDebugRecording() writes an additional init message. Don't start
1738 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001739 EXPECT_NOERR(
1740 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001741 first_init = false;
1742 }
1743
1744 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1745 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1746
1747 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001748 ASSERT_EQ(revframe_->num_channels_,
1749 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001750 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001751 memcpy(revfloat_cb_->channels()[i],
1752 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001753 msg.channel(i).size());
1754 }
1755 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001756 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001757 if (format == kFloatFormat) {
1758 // We're using an int16 input file; convert to float.
1759 ConvertToFloat(*revframe_, revfloat_cb_.get());
1760 }
1761 }
1762 AnalyzeReverseStreamChooser(format);
1763
1764 } else if (event_msg.type() == audioproc::Event::STREAM) {
1765 const audioproc::Stream msg = event_msg.stream();
1766 // ProcessStream could have changed this for the output frame.
1767 frame_->num_channels_ = apm_->num_input_channels();
1768
1769 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1770 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1771 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1772 if (msg.has_keypress()) {
1773 apm_->set_stream_key_pressed(msg.keypress());
1774 } else {
1775 apm_->set_stream_key_pressed(true);
1776 }
1777
1778 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001779 ASSERT_EQ(frame_->num_channels_,
1780 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001781 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001782 memcpy(float_cb_->channels()[i],
1783 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001784 msg.input_channel(i).size());
1785 }
1786 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001787 memcpy(frame_->mutable_data(), msg.input_data().data(),
1788 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001789 if (format == kFloatFormat) {
1790 // We're using an int16 input file; convert to float.
1791 ConvertToFloat(*frame_, float_cb_.get());
1792 }
1793 }
1794 ProcessStreamChooser(format);
1795 }
1796 }
1797 EXPECT_NOERR(apm_->StopDebugRecording());
1798 fclose(in_file);
1799}
1800
1801void ApmTest::VerifyDebugDumpTest(Format format) {
1802 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001803 std::string format_string;
1804 switch (format) {
1805 case kIntFormat:
1806 format_string = "_int";
1807 break;
1808 case kFloatFormat:
1809 format_string = "_float";
1810 break;
1811 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001812 const std::string ref_filename = test::TempFilename(
1813 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1814 const std::string out_filename = test::TempFilename(
1815 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001816 const std::string limited_filename = test::TempFilename(
1817 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1818 const size_t logging_limit_bytes = 100000;
1819 // We expect at least this many bytes in the created logfile.
1820 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001821 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001822 ProcessDebugDump(in_filename, ref_filename, format, -1);
1823 ProcessDebugDump(ref_filename, out_filename, format, -1);
1824 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001825
1826 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1827 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001828 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001829 ASSERT_TRUE(ref_file != NULL);
1830 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001831 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001832 std::unique_ptr<uint8_t[]> ref_bytes;
1833 std::unique_ptr<uint8_t[]> out_bytes;
1834 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001835
1836 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1837 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001838 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001839 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001840 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001841 while (ref_size > 0 && out_size > 0) {
1842 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001843 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001844 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001845 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001846 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001847 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001848 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1849 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001850 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001851 }
1852 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001853 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1854 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001855 EXPECT_NE(0, feof(ref_file));
1856 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001857 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001858 ASSERT_EQ(0, fclose(ref_file));
1859 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001860 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001861 remove(ref_filename.c_str());
1862 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001863 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001864}
1865
pbosc7a65692016-05-06 12:50:04 -07001866TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001867 VerifyDebugDumpTest(kIntFormat);
1868}
1869
pbosc7a65692016-05-06 12:50:04 -07001870TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001871 VerifyDebugDumpTest(kFloatFormat);
1872}
1873#endif
1874
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001875// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001876TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001877 const std::string filename =
1878 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001879 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001880 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001881
1882#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1883 // Stopping without having started should be OK.
1884 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1885
ivocd66b44d2016-01-15 03:06:36 -08001886 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001887 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001888 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001889 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1890
1891 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001892 FILE* fid = fopen(filename.c_str(), "r");
1893 ASSERT_TRUE(fid != NULL);
1894
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001895 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001896 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001897 ASSERT_EQ(0, remove(filename.c_str()));
1898#else
1899 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001900 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001901 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1902
1903 // Verify the file has NOT been written.
1904 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1905#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1906}
1907
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001908// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001909TEST_F(ApmTest, DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001910 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001911 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001912 const std::string filename =
1913 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001914 fid = fopen(filename.c_str(), "w");
1915 ASSERT_TRUE(fid);
1916
1917#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1918 // Stopping without having started should be OK.
1919 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1920
ivocd66b44d2016-01-15 03:06:36 -08001921 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001922 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001923 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1924 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1925
1926 // Verify the file has been written.
1927 fid = fopen(filename.c_str(), "r");
1928 ASSERT_TRUE(fid != NULL);
1929
1930 // Clean it up.
1931 ASSERT_EQ(0, fclose(fid));
1932 ASSERT_EQ(0, remove(filename.c_str()));
1933#else
1934 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001935 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001936 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1937
1938 ASSERT_EQ(0, fclose(fid));
1939#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1940}
1941
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001942TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001943 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001944 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001945
1946 Config config;
1947 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001948 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001949 EnableAllComponents();
1950 EnableAllAPComponents(fapm.get());
1951 for (int i = 0; i < ref_data.test_size(); i++) {
1952 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1953
1954 audioproc::Test* test = ref_data.mutable_test(i);
1955 // TODO(ajm): Restore downmixing test cases.
1956 if (test->num_input_channels() != test->num_output_channels())
1957 continue;
1958
Peter Kasting69558702016-01-12 16:26:35 -08001959 const size_t num_render_channels =
1960 static_cast<size_t>(test->num_reverse_channels());
1961 const size_t num_input_channels =
1962 static_cast<size_t>(test->num_input_channels());
1963 const size_t num_output_channels =
1964 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001965 const size_t samples_per_channel = static_cast<size_t>(
1966 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001967
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001968 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1969 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001970 Init(fapm.get());
1971
1972 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001973 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1974 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001975
1976 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001977 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001978 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1979 ReadFrame(near_file_, frame_, float_cb_.get())) {
1980 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1981
aluebsb0319552016-03-17 20:39:53 -07001982 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001983 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1984 revfloat_cb_->channels(),
1985 samples_per_channel,
1986 test->sample_rate(),
1987 LayoutFromChannels(num_render_channels)));
1988
1989 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1990 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1991 apm_->echo_cancellation()->set_stream_drift_samples(0);
1992 fapm->echo_cancellation()->set_stream_drift_samples(0);
1993 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1994 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1995
1996 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001997 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001998 output_int16.channels());
1999
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002000 EXPECT_NOERR(fapm->ProcessStream(
2001 float_cb_->channels(),
2002 samples_per_channel,
2003 test->sample_rate(),
2004 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002005 test->sample_rate(),
2006 LayoutFromChannels(num_output_channels),
2007 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002008 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002009 FloatToS16(float_cb_->channels()[j],
2010 samples_per_channel,
2011 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002012 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002013 float snr = ComputeSNR(output_int16.channels()[j],
2014 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002015 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002016
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002017 const float kVarianceThreshold = 20;
2018 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002019
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002020 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002021 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2022 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002023 }
2024 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002025
2026 analog_level = fapm->gain_control()->stream_analog_level();
2027 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2028 fapm->gain_control()->stream_analog_level());
2029 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2030 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002031 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2032 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002033 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002034
2035 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002036 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002037 }
aluebs776593b2016-03-15 14:04:58 -07002038
2039#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2040 const size_t kMaxNumBadChunks = 0;
2041#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2042 // There are a few chunks in the fixed-point profile that give low SNR.
2043 // Listening confirmed the difference is acceptable.
2044 const size_t kMaxNumBadChunks = 60;
2045#endif
2046 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2047
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002048 rewind(far_file_);
2049 rewind(near_file_);
2050 }
2051}
2052
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002053// TODO(andrew): Add a test to process a few frames with different combinations
2054// of enabled components.
2055
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002056TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002057 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002058 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002059
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002060 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002061 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002062 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002063 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002064 for (size_t i = 0; i < arraysize(kChannels); i++) {
2065 for (size_t j = 0; j < arraysize(kChannels); j++) {
2066 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002067 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002068 test->set_num_reverse_channels(kChannels[i]);
2069 test->set_num_input_channels(kChannels[j]);
2070 test->set_num_output_channels(kChannels[j]);
2071 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002072 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002073 }
2074 }
2075 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002076#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2077 // To test the extended filter mode.
2078 audioproc::Test* test = ref_data.add_test();
2079 test->set_num_reverse_channels(2);
2080 test->set_num_input_channels(2);
2081 test->set_num_output_channels(2);
2082 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2083 test->set_use_aec_extended_filter(true);
2084#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002085 }
2086
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002087 for (int i = 0; i < ref_data.test_size(); i++) {
2088 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002089
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002090 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002091 // TODO(ajm): We no longer allow different input and output channels. Skip
2092 // these tests for now, but they should be removed from the set.
2093 if (test->num_input_channels() != test->num_output_channels())
2094 continue;
2095
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002096 Config config;
2097 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002098 config.Set<ExtendedFilter>(
2099 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002100 apm_.reset(AudioProcessing::Create(config));
2101
2102 EnableAllComponents();
2103
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002104 Init(test->sample_rate(),
2105 test->sample_rate(),
2106 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002107 static_cast<size_t>(test->num_input_channels()),
2108 static_cast<size_t>(test->num_output_channels()),
2109 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002110 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002111
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002112 int frame_count = 0;
2113 int has_echo_count = 0;
2114 int has_voice_count = 0;
2115 int is_saturated_count = 0;
2116 int analog_level = 127;
2117 int analog_level_average = 0;
2118 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002119 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002120#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2121 int stats_index = 0;
2122#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002123
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002124 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002125 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002126
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002127 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2128
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002129 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002130 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002131 EXPECT_EQ(apm_->kNoError,
2132 apm_->gain_control()->set_stream_analog_level(analog_level));
2133
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002134 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002135
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002136 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002137 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2138 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002139
2140 max_output_average += MaxAudioFrame(*frame_);
2141
2142 if (apm_->echo_cancellation()->stream_has_echo()) {
2143 has_echo_count++;
2144 }
2145
2146 analog_level = apm_->gain_control()->stream_analog_level();
2147 analog_level_average += analog_level;
2148 if (apm_->gain_control()->stream_is_saturated()) {
2149 is_saturated_count++;
2150 }
2151 if (apm_->voice_detection()->stream_has_voice()) {
2152 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002153 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002154 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002155 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002156 }
2157
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002158 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2159
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002160 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002161 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002162 sizeof(int16_t),
2163 frame_size,
2164 out_file_);
2165 ASSERT_EQ(frame_size, write_count);
2166
2167 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002168 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002169 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002170
2171#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2172 const int kStatsAggregationFrameNum = 100; // 1 second.
2173 if (frame_count % kStatsAggregationFrameNum == 0) {
2174 // Get echo metrics.
2175 EchoCancellation::Metrics echo_metrics;
2176 EXPECT_EQ(apm_->kNoError,
2177 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2178
2179 // Get delay metrics.
2180 int median = 0;
2181 int std = 0;
2182 float fraction_poor_delays = 0;
2183 EXPECT_EQ(apm_->kNoError,
2184 apm_->echo_cancellation()->GetDelayMetrics(
2185 &median, &std, &fraction_poor_delays));
2186
2187 // Get RMS.
2188 int rms_level = apm_->level_estimator()->RMS();
2189 EXPECT_LE(0, rms_level);
2190 EXPECT_GE(127, rms_level);
2191
2192 if (!write_ref_data) {
2193 const audioproc::Test::EchoMetrics& reference =
2194 test->echo_metrics(stats_index);
2195 TestStats(echo_metrics.residual_echo_return_loss,
2196 reference.residual_echo_return_loss());
2197 TestStats(echo_metrics.echo_return_loss,
2198 reference.echo_return_loss());
2199 TestStats(echo_metrics.echo_return_loss_enhancement,
2200 reference.echo_return_loss_enhancement());
2201 TestStats(echo_metrics.a_nlp,
2202 reference.a_nlp());
2203 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2204 reference.divergent_filter_fraction());
2205
2206 const audioproc::Test::DelayMetrics& reference_delay =
2207 test->delay_metrics(stats_index);
2208 EXPECT_EQ(reference_delay.median(), median);
2209 EXPECT_EQ(reference_delay.std(), std);
2210 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2211 fraction_poor_delays);
2212
2213 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2214
2215 ++stats_index;
2216 } else {
2217 audioproc::Test::EchoMetrics* message =
2218 test->add_echo_metrics();
2219 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2220 message->mutable_residual_echo_return_loss());
2221 WriteStatsMessage(echo_metrics.echo_return_loss,
2222 message->mutable_echo_return_loss());
2223 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2224 message->mutable_echo_return_loss_enhancement());
2225 WriteStatsMessage(echo_metrics.a_nlp,
2226 message->mutable_a_nlp());
2227 message->set_divergent_filter_fraction(
2228 echo_metrics.divergent_filter_fraction);
2229
2230 audioproc::Test::DelayMetrics* message_delay =
2231 test->add_delay_metrics();
2232 message_delay->set_median(median);
2233 message_delay->set_std(std);
2234 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2235
2236 test->add_rms_level(rms_level);
2237 }
2238 }
2239#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002240 }
2241 max_output_average /= frame_count;
2242 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002243 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002244
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002245 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002246 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002247 // When running the test on a N7 we get a {2, 6} difference of
2248 // |has_voice_count| and |max_output_average| is up to 18 higher.
2249 // All numbers being consistently higher on N7 compare to ref_data.
2250 // TODO(bjornv): If we start getting more of these offsets on Android we
2251 // should consider a different approach. Either using one slack for all,
2252 // or generate a separate android reference.
2253#if defined(WEBRTC_ANDROID)
2254 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002255 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002256 const int kMaxOutputAverageOffset = 9;
2257 const int kMaxOutputAverageNear = 9;
2258#else
2259 const int kHasVoiceCountOffset = 0;
2260 const int kHasVoiceCountNear = kIntNear;
2261 const int kMaxOutputAverageOffset = 0;
2262 const int kMaxOutputAverageNear = kIntNear;
2263#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002264 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002265 EXPECT_NEAR(test->has_voice_count(),
2266 has_voice_count - kHasVoiceCountOffset,
2267 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002268 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002269
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002270 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002271 EXPECT_NEAR(test->max_output_average(),
2272 max_output_average - kMaxOutputAverageOffset,
2273 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002274#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002275 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002276 EXPECT_NEAR(test->ns_speech_probability_average(),
2277 ns_speech_prob_average,
2278 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002279#endif
2280 } else {
2281 test->set_has_echo_count(has_echo_count);
2282 test->set_has_voice_count(has_voice_count);
2283 test->set_is_saturated_count(is_saturated_count);
2284
2285 test->set_analog_level_average(analog_level_average);
2286 test->set_max_output_average(max_output_average);
2287
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002288#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002289 EXPECT_LE(0.0f, ns_speech_prob_average);
2290 EXPECT_GE(1.0f, ns_speech_prob_average);
2291 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002292#endif
2293 }
2294
2295 rewind(far_file_);
2296 rewind(near_file_);
2297 }
2298
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002299 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002300 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002301 }
2302}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002303
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002304TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2305 struct ChannelFormat {
2306 AudioProcessing::ChannelLayout in_layout;
2307 AudioProcessing::ChannelLayout out_layout;
2308 };
2309 ChannelFormat cf[] = {
2310 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2311 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2312 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2313 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002314
kwiberg62eaacf2016-02-17 06:39:05 -08002315 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002316 // Enable one component just to ensure some processing takes place.
2317 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002318 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002319 const int in_rate = 44100;
2320 const int out_rate = 48000;
2321 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2322 TotalChannelsFromLayout(cf[i].in_layout));
2323 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2324 ChannelsFromLayout(cf[i].out_layout));
2325
2326 // Run over a few chunks.
2327 for (int j = 0; j < 10; ++j) {
2328 EXPECT_NOERR(ap->ProcessStream(
2329 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002330 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002331 in_rate,
2332 cf[i].in_layout,
2333 out_rate,
2334 cf[i].out_layout,
2335 out_cb.channels()));
2336 }
2337 }
2338}
2339
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002340// Compares the reference and test arrays over a region around the expected
2341// delay. Finds the highest SNR in that region and adds the variance and squared
2342// error results to the supplied accumulators.
2343void UpdateBestSNR(const float* ref,
2344 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002345 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002346 int expected_delay,
2347 double* variance_acc,
2348 double* sq_error_acc) {
2349 double best_snr = std::numeric_limits<double>::min();
2350 double best_variance = 0;
2351 double best_sq_error = 0;
2352 // Search over a region of eight samples around the expected delay.
2353 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2354 ++delay) {
2355 double sq_error = 0;
2356 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002357 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002358 double error = test[i + delay] - ref[i];
2359 sq_error += error * error;
2360 variance += ref[i] * ref[i];
2361 }
2362
2363 if (sq_error == 0) {
2364 *variance_acc += variance;
2365 return;
2366 }
2367 double snr = variance / sq_error;
2368 if (snr > best_snr) {
2369 best_snr = snr;
2370 best_variance = variance;
2371 best_sq_error = sq_error;
2372 }
2373 }
2374
2375 *variance_acc += best_variance;
2376 *sq_error_acc += best_sq_error;
2377}
2378
2379// Used to test a multitude of sample rate and channel combinations. It works
2380// by first producing a set of reference files (in SetUpTestCase) that are
2381// assumed to be correct, as the used parameters are verified by other tests
2382// in this collection. Primarily the reference files are all produced at
2383// "native" rates which do not involve any resampling.
2384
2385// Each test pass produces an output file with a particular format. The output
2386// is matched against the reference file closest to its internal processing
2387// format. If necessary the output is resampled back to its process format.
2388// Due to the resampling distortion, we don't expect identical results, but
2389// enforce SNR thresholds which vary depending on the format. 0 is a special
2390// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002391typedef std::tr1::tuple<int, int, int, int, double, double>
2392 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002393class AudioProcessingTest
2394 : public testing::TestWithParam<AudioProcessingTestData> {
2395 public:
2396 AudioProcessingTest()
2397 : input_rate_(std::tr1::get<0>(GetParam())),
2398 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002399 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2400 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2401 expected_snr_(std::tr1::get<4>(GetParam())),
2402 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002403
2404 virtual ~AudioProcessingTest() {}
2405
2406 static void SetUpTestCase() {
2407 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002408 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002409 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002410 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2411 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2412 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002413 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002414 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2415 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2416 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002417 }
2418 }
2419 }
2420 }
2421
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002422 static void TearDownTestCase() {
2423 ClearTempFiles();
2424 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002425
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002426 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002427 // to a file specified with |output_file_prefix|. Both forward and reverse
2428 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429 static void ProcessFormat(int input_rate,
2430 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002431 int reverse_input_rate,
2432 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002433 size_t num_input_channels,
2434 size_t num_output_channels,
2435 size_t num_reverse_input_channels,
2436 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002437 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002438 Config config;
2439 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002440 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002441 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002442
ekmeyerson60d9b332015-08-14 10:35:55 -07002443 ProcessingConfig processing_config = {
2444 {{input_rate, num_input_channels},
2445 {output_rate, num_output_channels},
2446 {reverse_input_rate, num_reverse_input_channels},
2447 {reverse_output_rate, num_reverse_output_channels}}};
2448 ap->Initialize(processing_config);
2449
2450 FILE* far_file =
2451 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002452 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002453 FILE* out_file =
2454 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2455 reverse_input_rate, reverse_output_rate,
2456 num_input_channels, num_output_channels,
2457 num_reverse_input_channels,
2458 num_reverse_output_channels, kForward).c_str(),
2459 "wb");
2460 FILE* rev_out_file =
2461 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2462 reverse_input_rate, reverse_output_rate,
2463 num_input_channels, num_output_channels,
2464 num_reverse_input_channels,
2465 num_reverse_output_channels, kReverse).c_str(),
2466 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002467 ASSERT_TRUE(far_file != NULL);
2468 ASSERT_TRUE(near_file != NULL);
2469 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002470 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002471
2472 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2473 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2475 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002476 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2477 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002478 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2479 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002480
2481 // Temporary buffers.
2482 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002483 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2484 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002485 std::unique_ptr<float[]> float_data(new float[max_length]);
2486 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002487
2488 int analog_level = 127;
2489 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2490 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002491 EXPECT_NOERR(ap->ProcessReverseStream(
2492 rev_cb.channels(), processing_config.reverse_input_stream(),
2493 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002494
2495 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2496 ap->echo_cancellation()->set_stream_drift_samples(0);
2497 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2498
2499 EXPECT_NOERR(ap->ProcessStream(
2500 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002501 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002502 input_rate,
2503 LayoutFromChannels(num_input_channels),
2504 output_rate,
2505 LayoutFromChannels(num_output_channels),
2506 out_cb.channels()));
2507
ekmeyerson60d9b332015-08-14 10:35:55 -07002508 // Dump forward output to file.
2509 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002510 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002511 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002512
pkasting25702cb2016-01-08 13:50:27 -08002513 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002514 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002515 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002516
ekmeyerson60d9b332015-08-14 10:35:55 -07002517 // Dump reverse output to file.
2518 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2519 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002520 size_t rev_out_length =
2521 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002522
pkasting25702cb2016-01-08 13:50:27 -08002523 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002524 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2525 rev_out_file));
2526
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002527 analog_level = ap->gain_control()->stream_analog_level();
2528 }
2529 fclose(far_file);
2530 fclose(near_file);
2531 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002532 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002533 }
2534
2535 protected:
2536 int input_rate_;
2537 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002538 int reverse_input_rate_;
2539 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002540 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002541 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002542};
2543
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002544TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002545 struct ChannelFormat {
2546 int num_input;
2547 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002548 int num_reverse_input;
2549 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002550 };
2551 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002552 {1, 1, 1, 1},
2553 {1, 1, 2, 1},
2554 {2, 1, 1, 1},
2555 {2, 1, 2, 1},
2556 {2, 2, 1, 1},
2557 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002558 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002559
pkasting25702cb2016-01-08 13:50:27 -08002560 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002561 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2562 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2563 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002564
ekmeyerson60d9b332015-08-14 10:35:55 -07002565 // Verify output for both directions.
2566 std::vector<StreamDirection> stream_directions;
2567 stream_directions.push_back(kForward);
2568 stream_directions.push_back(kReverse);
2569 for (StreamDirection file_direction : stream_directions) {
2570 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2571 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2572 const int out_num =
2573 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2574 const double expected_snr =
2575 file_direction ? expected_reverse_snr_ : expected_snr_;
2576
2577 const int min_ref_rate = std::min(in_rate, out_rate);
2578 int ref_rate;
2579
2580 if (min_ref_rate > 32000) {
2581 ref_rate = 48000;
2582 } else if (min_ref_rate > 16000) {
2583 ref_rate = 32000;
2584 } else if (min_ref_rate > 8000) {
2585 ref_rate = 16000;
2586 } else {
2587 ref_rate = 8000;
2588 }
aluebs776593b2016-03-15 14:04:58 -07002589#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002590 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002591 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002592 }
2593#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002594 FILE* out_file = fopen(
2595 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2596 reverse_output_rate_, cf[i].num_input,
2597 cf[i].num_output, cf[i].num_reverse_input,
2598 cf[i].num_reverse_output, file_direction).c_str(),
2599 "rb");
2600 // The reference files always have matching input and output channels.
2601 FILE* ref_file = fopen(
2602 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2603 cf[i].num_output, cf[i].num_output,
2604 cf[i].num_reverse_output, cf[i].num_reverse_output,
2605 file_direction).c_str(),
2606 "rb");
2607 ASSERT_TRUE(out_file != NULL);
2608 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002609
pkasting25702cb2016-01-08 13:50:27 -08002610 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2611 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002612 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002613 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002614 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002615 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002616 // Data from the resampled output, in case the reference and output rates
2617 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002618 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002619
ekmeyerson60d9b332015-08-14 10:35:55 -07002620 PushResampler<float> resampler;
2621 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002622
ekmeyerson60d9b332015-08-14 10:35:55 -07002623 // Compute the resampling delay of the output relative to the reference,
2624 // to find the region over which we should search for the best SNR.
2625 float expected_delay_sec = 0;
2626 if (in_rate != ref_rate) {
2627 // Input resampling delay.
2628 expected_delay_sec +=
2629 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2630 }
2631 if (out_rate != ref_rate) {
2632 // Output resampling delay.
2633 expected_delay_sec +=
2634 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2635 // Delay of converting the output back to its processing rate for
2636 // testing.
2637 expected_delay_sec +=
2638 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2639 }
2640 int expected_delay =
2641 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002642
ekmeyerson60d9b332015-08-14 10:35:55 -07002643 double variance = 0;
2644 double sq_error = 0;
2645 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2646 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2647 float* out_ptr = out_data.get();
2648 if (out_rate != ref_rate) {
2649 // Resample the output back to its internal processing rate if
2650 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002651 ASSERT_EQ(ref_length,
2652 static_cast<size_t>(resampler.Resample(
2653 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002654 out_ptr = cmp_data.get();
2655 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002656
ekmeyerson60d9b332015-08-14 10:35:55 -07002657 // Update the |sq_error| and |variance| accumulators with the highest
2658 // SNR of reference vs output.
2659 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2660 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002661 }
2662
ekmeyerson60d9b332015-08-14 10:35:55 -07002663 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2664 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2665 << cf[i].num_input << ", " << cf[i].num_output << ", "
2666 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2667 << ", " << file_direction << "): ";
2668 if (sq_error > 0) {
2669 double snr = 10 * log10(variance / sq_error);
2670 EXPECT_GE(snr, expected_snr);
2671 EXPECT_NE(0, expected_snr);
2672 std::cout << "SNR=" << snr << " dB" << std::endl;
2673 } else {
aluebs776593b2016-03-15 14:04:58 -07002674 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002675 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002676
ekmeyerson60d9b332015-08-14 10:35:55 -07002677 fclose(out_file);
2678 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002679 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002680 }
2681}
2682
2683#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2684INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002685 CommonFormats,
2686 AudioProcessingTest,
2687 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002688 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2689 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002690 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2691 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2692 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2693 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2694 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2695 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2696 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2697 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2698 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002699
ekmeyerson60d9b332015-08-14 10:35:55 -07002700 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2701 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2702 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2703 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2704 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2705 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2706 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2707 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2708 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2709 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2710 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2711 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002712
ekmeyerson60d9b332015-08-14 10:35:55 -07002713 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2714 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2715 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2716 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2717 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2718 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2719 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2720 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2721 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2722 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2723 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2724 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002725
ekmeyerson60d9b332015-08-14 10:35:55 -07002726 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2727 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2728 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2729 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2730 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2731 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2732 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2733 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2734 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2735 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002736 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002737 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002738
2739#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2740INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002741 CommonFormats,
2742 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002743 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2744 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2745 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2746 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2747 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2748 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002749 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2750 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2751 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2752 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2753 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2754 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002755
aluebs776593b2016-03-15 14:04:58 -07002756 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2757 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2758 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002759 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2760 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2761 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2762 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2763 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2764 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2765 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2766 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2767 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002768
aluebs776593b2016-03-15 14:04:58 -07002769 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2770 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2771 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2772 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2773 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2774 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2775 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2776 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2777 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002778 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2779 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2780 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002781
ekmeyerson60d9b332015-08-14 10:35:55 -07002782 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2783 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2784 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2785 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2786 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2787 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2788 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2789 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2790 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2791 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002792 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002793 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002794#endif
2795
niklase@google.com470e71d2011-07-07 08:21:25 +00002796} // namespace
peahc19f3122016-10-07 14:54:10 -07002797
2798TEST(ApmConfiguration, DefaultBehavior) {
2799 // Verify that the level controller is default off, it can be activated using
2800 // the config, and that the default initial level is maintained after the
2801 // config has been applied.
2802 std::unique_ptr<AudioProcessingImpl> apm(
2803 new AudioProcessingImpl(webrtc::Config()));
2804 AudioProcessing::Config config;
2805 EXPECT_FALSE(apm->config_.level_controller.enabled);
2806 // TODO(peah): Add test for the existence of the level controller object once
2807 // that is created only when that is specified in the config.
2808 // TODO(peah): Remove the testing for
2809 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2810 // is instead used to activate the level controller.
2811 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2812 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2813 apm->config_.level_controller.initial_peak_level_dbfs,
2814 std::numeric_limits<float>::epsilon());
2815 config.level_controller.enabled = true;
2816 apm->ApplyConfig(config);
2817 EXPECT_TRUE(apm->config_.level_controller.enabled);
2818 // TODO(peah): Add test for the existence of the level controller object once
2819 // that is created only when the that is specified in the config.
2820 // TODO(peah): Remove the testing for
2821 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2822 // is instead used to activate the level controller.
2823 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2824 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2825 apm->config_.level_controller.initial_peak_level_dbfs,
2826 std::numeric_limits<float>::epsilon());
2827}
2828
2829TEST(ApmConfiguration, ValidConfigBehavior) {
2830 // Verify that the initial level can be specified and is retained after the
2831 // config has been applied.
2832 std::unique_ptr<AudioProcessingImpl> apm(
2833 new AudioProcessingImpl(webrtc::Config()));
2834 AudioProcessing::Config config;
2835 config.level_controller.initial_peak_level_dbfs = -50.f;
2836 apm->ApplyConfig(config);
2837 EXPECT_FALSE(apm->config_.level_controller.enabled);
2838 // TODO(peah): Add test for the existence of the level controller object once
2839 // that is created only when the that is specified in the config.
2840 // TODO(peah): Remove the testing for
2841 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2842 // is instead used to activate the level controller.
2843 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2844 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2845 std::numeric_limits<float>::epsilon());
2846}
2847
2848TEST(ApmConfiguration, InValidConfigBehavior) {
2849 // Verify that the config is properly reset when nonproper values are applied
2850 // for the initial level.
2851
2852 // Verify that the config is properly reset when the specified initial peak
2853 // level is too low.
2854 std::unique_ptr<AudioProcessingImpl> apm(
2855 new AudioProcessingImpl(webrtc::Config()));
2856 AudioProcessing::Config config;
2857 config.level_controller.enabled = true;
2858 config.level_controller.initial_peak_level_dbfs = -101.f;
2859 apm->ApplyConfig(config);
2860 EXPECT_FALSE(apm->config_.level_controller.enabled);
2861 // TODO(peah): Add test for the existence of the level controller object once
2862 // that is created only when the that is specified in the config.
2863 // TODO(peah): Remove the testing for
2864 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2865 // is instead used to activate the level controller.
2866 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2867 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2868 apm->config_.level_controller.initial_peak_level_dbfs,
2869 std::numeric_limits<float>::epsilon());
2870
2871 // Verify that the config is properly reset when the specified initial peak
2872 // level is too high.
2873 apm.reset(new AudioProcessingImpl(webrtc::Config()));
2874 config = AudioProcessing::Config();
2875 config.level_controller.enabled = true;
2876 config.level_controller.initial_peak_level_dbfs = 1.f;
2877 apm->ApplyConfig(config);
2878 EXPECT_FALSE(apm->config_.level_controller.enabled);
2879 // TODO(peah): Add test for the existence of the level controller object once
2880 // that is created only when that is specified in the config.
2881 // TODO(peah): Remove the testing for
2882 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2883 // is instead used to activate the level controller.
2884 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2885 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2886 apm->config_.level_controller.initial_peak_level_dbfs,
2887 std::numeric_limits<float>::epsilon());
2888}
2889
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002890} // namespace webrtc