blob: 7cd2c95e01d4a0ef005866eaaab8aab9728d7ca3 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000010#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080015#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000016#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/include/push_resampler.h"
20#include "common_audio/resampler/push_sinc_resampler.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
22#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
23#include "modules/audio_processing/audio_processing_impl.h"
24#include "modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
25#include "modules/audio_processing/common.h"
26#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020027#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/level_controller/level_controller_constants.h"
29#include "modules/audio_processing/test/protobuf_utils.h"
30#include "modules/audio_processing/test/test_utils.h"
31#include "modules/include/module_common_types.h"
32#include "rtc_base/arraysize.h"
33#include "rtc_base/checks.h"
34#include "rtc_base/gtest_prod_util.h"
35#include "rtc_base/ignore_wundef.h"
36#include "rtc_base/protobuf_utils.h"
37#include "rtc_base/safe_minmax.h"
38#include "rtc_base/task_queue.h"
39#include "rtc_base/thread.h"
40#include "system_wrappers/include/event_wrapper.h"
Fredrik Solenberg729b9102017-10-03 13:39:39 +000041#include "system_wrappers/include/trace.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/gtest.h"
43#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070044
45RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000046#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000047#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#endif
kwiberg77eab702016-09-28 17:42:01 -070051RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000052
andrew@webrtc.org27c69802014-02-18 20:24:56 +000053namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000054namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000055
ekmeyerson60d9b332015-08-14 10:35:55 -070056// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
57// applicable.
58
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000059// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000060// re-implemented; see comment at the bottom of this file. For now, the user has
61// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000062// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000063// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000064// be set to true with the command-line switch --write_ref_data.
65bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070066const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070067const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000068
aluebseb3603b2016-04-20 15:27:58 -070069#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
70// Android doesn't support 48kHz.
71const int kProcessSampleRates[] = {8000, 16000, 32000};
72#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070073const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070074#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000075
ekmeyerson60d9b332015-08-14 10:35:55 -070076enum StreamDirection { kForward = 0, kReverse };
77
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000078void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000079 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080 cb->num_channels());
81 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000082 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000083 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000084 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080085 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000086 S16ToFloat(cb_int.channels()[i],
87 cb->num_frames(),
88 cb->channels()[i]);
89 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000090}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000091
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000092void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070093 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094}
95
andrew@webrtc.org103657b2014-04-24 18:28:56 +000096// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080097size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098 switch (layout) {
99 case AudioProcessing::kMono:
100 return 1;
101 case AudioProcessing::kMonoAndKeyboard:
102 case AudioProcessing::kStereo:
103 return 2;
104 case AudioProcessing::kStereoAndKeyboard:
105 return 3;
106 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700107 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800108 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000109}
110
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000111int TruncateToMultipleOf10(int value) {
112 return (value / 10) * 10;
113}
114
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800116 size_t samples_per_channel) {
117 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000118 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000119}
120
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800122 size_t samples_per_channel) {
123 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000124 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
125}
126
pkasting25702cb2016-01-08 13:50:27 -0800127void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
128 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000129 stereo[i * 2 + 1] = stereo[i * 2];
130 }
131}
132
yujo36b1a5f2017-06-12 12:45:32 -0700133void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800134 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000135 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
136 }
137}
138
139void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700140 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700141 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
142 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700143 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000144 }
145}
146
147void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800148 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700149 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700150 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700151 frame_data[i] = left;
152 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000153 }
154}
155
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000156void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700157 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700158 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
159 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700160 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000161 }
162}
163
andrew@webrtc.org81865342012-10-27 00:28:27 +0000164bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000165 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166 return false;
167 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000168 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000169 return false;
170 }
yujo36b1a5f2017-06-12 12:45:32 -0700171 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000172 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000173 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000174 return false;
175 }
176 return true;
177}
178
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000179void EnableAllAPComponents(AudioProcessing* ap) {
180#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
181 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
182
183 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
184 EXPECT_NOERR(ap->gain_control()->Enable(true));
185#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
186 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
187 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
188 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
189 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
190
191 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
192 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
193 EXPECT_NOERR(ap->gain_control()->Enable(true));
194#endif
195
peah8271d042016-11-22 07:24:52 -0800196 AudioProcessing::Config apm_config;
197 apm_config.high_pass_filter.enabled = true;
198 ap->ApplyConfig(apm_config);
199
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000200 EXPECT_NOERR(ap->level_estimator()->Enable(true));
201 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
202
203 EXPECT_NOERR(ap->voice_detection()->Enable(true));
204}
205
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000206// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000207template <class T>
208T AbsValue(T a) {
209 return a > 0 ? a: -a;
210}
211
212int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800213 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700214 const int16_t* frame_data = frame.data();
215 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800216 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700217 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000218 }
219
220 return max_data;
221}
222
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000223#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000224void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000225 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700226 EXPECT_EQ(reference.instant(), test.instant);
227 EXPECT_EQ(reference.average(), test.average);
228 EXPECT_EQ(reference.maximum(), test.maximum);
229 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230}
231
232void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 audioproc::Test::Statistic* msg) {
234 msg->set_instant(output.instant);
235 msg->set_average(output.average);
236 msg->set_maximum(output.maximum);
237 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000238}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000239#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240
Alex Loiko890988c2017-08-31 10:25:48 +0200241void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700242 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000244 ASSERT_TRUE(file != NULL);
245
246 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000247 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800248 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000249 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000250
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000251 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000252 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000253 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000254 fclose(file);
255}
256
Alex Loiko890988c2017-08-31 10:25:48 +0200257std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000258 std::ostringstream ss;
259 // Resource files are all stereo.
260 ss << name << sample_rate_hz / 1000 << "_stereo";
261 return test::ResourcePath(ss.str(), "pcm");
262}
263
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000264// Temporary filenames unique to this process. Used to be able to run these
265// tests in parallel as each process needs to be running in isolation they can't
266// have competing filenames.
267std::map<std::string, std::string> temp_filenames;
268
Alex Loiko890988c2017-08-31 10:25:48 +0200269std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000270 int input_rate,
271 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700272 int reverse_input_rate,
273 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800274 size_t num_input_channels,
275 size_t num_output_channels,
276 size_t num_reverse_input_channels,
277 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700280 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
281 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282 if (num_output_channels == 1) {
283 ss << "mono";
284 } else if (num_output_channels == 2) {
285 ss << "stereo";
286 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700287 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000288 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700289 ss << output_rate / 1000;
290 if (num_reverse_output_channels == 1) {
291 ss << "_rmono";
292 } else if (num_reverse_output_channels == 2) {
293 ss << "_rstereo";
294 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700295 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700296 }
297 ss << reverse_output_rate / 1000;
298 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000299
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000300 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700301 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000302 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
303 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000304}
305
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000306void ClearTempFiles() {
307 for (auto& kv : temp_filenames)
308 remove(kv.second.c_str());
309}
310
Alex Loiko890988c2017-08-31 10:25:48 +0200311void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000312 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000313 ASSERT_TRUE(file != NULL);
314 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000315 fclose(file);
316}
317
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000318// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
319// stereo) file, converts to deinterleaved float (optionally downmixing) and
320// returns the result in |cb|. Returns false if the file ended (or on error) and
321// true otherwise.
322//
323// |int_data| and |float_data| are just temporary space that must be
324// sufficiently large to hold the 10 ms chunk.
325bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
326 ChannelBuffer<float>* cb) {
327 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000328 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000329 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
330 if (read_count != frame_size) {
331 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700332 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000333 return false; // This is expected.
334 }
335
336 S16ToFloat(int_data, frame_size, float_data);
337 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000338 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000339 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000340 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000341 cb->channels());
342 }
343
344 return true;
345}
346
niklase@google.com470e71d2011-07-07 08:21:25 +0000347class ApmTest : public ::testing::Test {
348 protected:
349 ApmTest();
350 virtual void SetUp();
351 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000352
353 static void SetUpTestCase() {
Fredrik Solenberg729b9102017-10-03 13:39:39 +0000354 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000355 }
356
357 static void TearDownTestCase() {
Fredrik Solenberg729b9102017-10-03 13:39:39 +0000358 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000359 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000360 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000361
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000362 // Used to select between int and float interface tests.
363 enum Format {
364 kIntFormat,
365 kFloatFormat
366 };
367
368 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000369 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000370 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800371 size_t num_input_channels,
372 size_t num_output_channels,
373 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000374 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000375 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000376 void EnableAllComponents();
377 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000378 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000379 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000380 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
381 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000382 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000383 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
384 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800386 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700387 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800388 void TestChangingForwardChannels(size_t num_in_channels,
389 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800391 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700392 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000393 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
394 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000395 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000396 int ProcessStreamChooser(Format format);
397 int AnalyzeReverseStreamChooser(Format format);
398 void ProcessDebugDump(const std::string& in_filename,
399 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800400 Format format,
401 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000402 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403
404 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000405 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800406 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000407 AudioFrame* frame_;
408 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800409 std::unique_ptr<ChannelBuffer<float> > float_cb_;
410 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800412 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 FILE* far_file_;
414 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000415 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416};
417
418ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000419 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000420#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800421 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
422 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000423#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000424#if defined(WEBRTC_MAC)
425 // A different file for Mac is needed because on this platform the AEC
426 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800427 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
428 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000429#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800430 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
431 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000432#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000433#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000434 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000435 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000436 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000437 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000438 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000439 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000440 out_file_(NULL) {
441 Config config;
442 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
443 apm_.reset(AudioProcessing::Create(config));
444}
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
446void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000447 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000448
449 frame_ = new AudioFrame();
450 revframe_ = new AudioFrame();
451
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000452 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000453}
454
455void ApmTest::TearDown() {
456 if (frame_) {
457 delete frame_;
458 }
459 frame_ = NULL;
460
461 if (revframe_) {
462 delete revframe_;
463 }
464 revframe_ = NULL;
465
466 if (far_file_) {
467 ASSERT_EQ(0, fclose(far_file_));
468 }
469 far_file_ = NULL;
470
471 if (near_file_) {
472 ASSERT_EQ(0, fclose(near_file_));
473 }
474 near_file_ = NULL;
475
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000476 if (out_file_) {
477 ASSERT_EQ(0, fclose(out_file_));
478 }
479 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000480}
481
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000482void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000483 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700484 ap->Initialize(
485 {{{frame_->sample_rate_hz_, frame_->num_channels_},
486 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700487 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700488 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000489}
490
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000491void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000492 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000493 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800494 size_t num_input_channels,
495 size_t num_output_channels,
496 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000497 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000498 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000499 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000500 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000501
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000502 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
503 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000504 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000505
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000506 if (far_file_) {
507 ASSERT_EQ(0, fclose(far_file_));
508 }
509 std::string filename = ResourceFilePath("far", sample_rate_hz);
510 far_file_ = fopen(filename.c_str(), "rb");
511 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
512 filename << "\n";
513
514 if (near_file_) {
515 ASSERT_EQ(0, fclose(near_file_));
516 }
517 filename = ResourceFilePath("near", sample_rate_hz);
518 near_file_ = fopen(filename.c_str(), "rb");
519 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
520 filename << "\n";
521
522 if (open_output_file) {
523 if (out_file_) {
524 ASSERT_EQ(0, fclose(out_file_));
525 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700526 filename = OutputFilePath(
527 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
528 reverse_sample_rate_hz, num_input_channels, num_output_channels,
529 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000530 out_file_ = fopen(filename.c_str(), "wb");
531 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
532 filename << "\n";
533 }
534}
535
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000536void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000537 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000538}
539
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
541 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000542 // The files always contain stereo audio.
543 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700544 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000545 sizeof(int16_t),
546 frame_size,
547 file);
548 if (read_count != frame_size) {
549 // Check that the file really ended.
550 EXPECT_NE(0, feof(file));
551 return false; // This is expected.
552 }
553
554 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700555 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000556 frame->samples_per_channel_);
557 }
558
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000559 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000560 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000562 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000563}
564
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000565bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
566 return ReadFrame(file, frame, NULL);
567}
568
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000569// If the end of the file has been reached, rewind it and attempt to read the
570// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
572 ChannelBuffer<float>* cb) {
573 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000574 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000575 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000576 }
577}
578
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000579void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
580 ReadFrameWithRewind(file, frame, NULL);
581}
582
andrew@webrtc.org81865342012-10-27 00:28:27 +0000583void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
584 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000585 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000586 EXPECT_EQ(apm_->kNoError,
587 apm_->gain_control()->set_stream_analog_level(127));
588 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000589}
590
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000591int ApmTest::ProcessStreamChooser(Format format) {
592 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593 return apm_->ProcessStream(frame_);
594 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000595 return apm_->ProcessStream(float_cb_->channels(),
596 frame_->samples_per_channel_,
597 frame_->sample_rate_hz_,
598 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000599 output_sample_rate_hz_,
600 LayoutFromChannels(num_output_channels_),
601 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000602}
603
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000604int ApmTest::AnalyzeReverseStreamChooser(Format format) {
605 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700606 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000607 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000608 return apm_->AnalyzeReverseStream(
609 revfloat_cb_->channels(),
610 revframe_->samples_per_channel_,
611 revframe_->sample_rate_hz_,
612 LayoutFromChannels(revframe_->num_channels_));
613}
614
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000615void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
616 int delay_min, int delay_max) {
617 // The |revframe_| and |frame_| should include the proper frame information,
618 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000619 AudioFrame tmp_frame;
620 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000621 bool causal = true;
622
623 tmp_frame.CopyFrom(*revframe_);
624 SetFrameTo(&tmp_frame, 0);
625
626 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
627 // Initialize the |frame_queue| with empty frames.
628 int frame_delay = delay_ms / 10;
629 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000630 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631 frame->CopyFrom(tmp_frame);
632 frame_queue.push(frame);
633 frame_delay++;
634 causal = false;
635 }
636 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000637 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000638 frame->CopyFrom(tmp_frame);
639 frame_queue.push(frame);
640 frame_delay--;
641 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000642 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
643 // need enough frames with audio to have reliable estimates, but as few as
644 // possible to keep processing time down. 4.5 seconds seemed to be a good
645 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000646 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000647 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000648 frame->CopyFrom(tmp_frame);
649 // Use the near end recording, since that has more speech in it.
650 ASSERT_TRUE(ReadFrame(near_file_, frame));
651 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000652 AudioFrame* reverse_frame = frame;
653 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000654 if (!causal) {
655 reverse_frame = frame_queue.front();
656 // When we call ProcessStream() the frame is modified, so we can't use the
657 // pointer directly when things are non-causal. Use an intermediate frame
658 // and copy the data.
659 process_frame = &tmp_frame;
660 process_frame->CopyFrom(*frame);
661 }
aluebsb0319552016-03-17 20:39:53 -0700662 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
664 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
665 frame = frame_queue.front();
666 frame_queue.pop();
667 delete frame;
668
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000669 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000670 int median;
671 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000672 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000673 // Discard the first delay metrics to avoid convergence effects.
674 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000675 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
676 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000677 }
678 }
679
680 rewind(near_file_);
681 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000682 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 frame_queue.pop();
684 delete frame;
685 }
686 // Calculate expected delay estimate and acceptable regions. Further,
687 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700688 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700689 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700690 const int expected_median =
691 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
692 const int expected_median_high = 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);
kwiberg07038562017-06-12 11:40:47 -0700695 const int expected_median_low = rtc::SafeClamp<int>(
696 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700697 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000698 // Verify delay metrics.
699 int median;
700 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000701 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000702 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000703 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
704 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000705 EXPECT_GE(expected_median_high, median);
706 EXPECT_LE(expected_median_low, median);
707}
708
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000709void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000710 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000711 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000712
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000715 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000716 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000717
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000718 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 EXPECT_EQ(apm_->kNoError,
720 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000721 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000722 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000724
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725 // Other stream parameters set correctly.
726 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 EXPECT_EQ(apm_->kNoError,
728 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000729 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000730 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000732 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000733 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
734 EXPECT_EQ(apm_->kNoError,
735 apm_->echo_cancellation()->enable_drift_compensation(false));
736
737 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000738 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000740 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000741 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000742
743 // Resets after successful ProcessStream().
744 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000745 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000746 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000747 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000748
749 // Other stream parameters set correctly.
750 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
751 EXPECT_EQ(apm_->kNoError,
752 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000753 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000754 EXPECT_EQ(apm_->kNoError,
755 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000757 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000758 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
759
760 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000761 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000762 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000763
764 // Resets after successful ProcessStream().
765 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000766 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000767 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000768 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000769 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000770
771 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
773 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
774 EXPECT_EQ(apm_->kNoError,
775 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000776 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000777 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000778
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000779 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000782 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000783 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000784
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000785 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000787 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000788 EXPECT_EQ(apm_->kNoError,
789 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000790 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000791}
792
793TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000794 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000795}
796
797TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000798 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000799}
800
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000801TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
802 EXPECT_EQ(0, apm_->delay_offset_ms());
803 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
804 EXPECT_EQ(50, apm_->stream_delay_ms());
805}
806
807TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
808 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000809 apm_->set_delay_offset_ms(100);
810 EXPECT_EQ(100, apm_->delay_offset_ms());
811 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000812 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000813 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
814 EXPECT_EQ(200, apm_->stream_delay_ms());
815
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000816 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000817 apm_->set_delay_offset_ms(-50);
818 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000819 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
820 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000821 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
822 EXPECT_EQ(50, apm_->stream_delay_ms());
823}
824
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800826 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700827 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000828 frame_->num_channels_ = num_channels;
829 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700830 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000831}
832
Michael Graczyk86c6d332015-07-23 11:41:39 -0700833void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800834 size_t num_in_channels,
835 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700836 AudioProcessing::Error expected_return) {
837 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
838 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
839
840 EXPECT_EQ(expected_return,
841 apm_->ProcessStream(float_cb_->channels(), input_stream,
842 output_stream, float_cb_->channels()));
843}
844
845void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800846 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700847 AudioProcessing::Error expected_return) {
848 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700849 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
850 {output_sample_rate_hz_, apm_->num_output_channels()},
851 {frame_->sample_rate_hz_, num_rev_channels},
852 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700853
ekmeyerson60d9b332015-08-14 10:35:55 -0700854 EXPECT_EQ(
855 expected_return,
856 apm_->ProcessReverseStream(
857 float_cb_->channels(), processing_config.reverse_input_stream(),
858 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700859}
860
861TEST_F(ApmTest, ChannelsInt16Interface) {
862 // Testing number of invalid and valid channels.
863 Init(16000, 16000, 16000, 4, 4, 4, false);
864
865 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
866
Peter Kasting69558702016-01-12 16:26:35 -0800867 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700868 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000869 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000870 }
871}
872
Michael Graczyk86c6d332015-07-23 11:41:39 -0700873TEST_F(ApmTest, Channels) {
874 // Testing number of invalid and valid channels.
875 Init(16000, 16000, 16000, 4, 4, 4, false);
876
877 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
878 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
879
Peter Kasting69558702016-01-12 16:26:35 -0800880 for (size_t i = 1; i < 4; ++i) {
881 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700882 // Output channels much be one or match input channels.
883 if (j == 1 || i == j) {
884 TestChangingForwardChannels(i, j, kNoErr);
885 TestChangingReverseChannels(i, kNoErr);
886
887 EXPECT_EQ(i, apm_->num_input_channels());
888 EXPECT_EQ(j, apm_->num_output_channels());
889 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800890 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700891 } else {
892 TestChangingForwardChannels(i, j,
893 AudioProcessing::kBadNumberChannelsError);
894 }
895 }
896 }
897}
898
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000899TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000900 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000901 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000902 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000903 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700904 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800905 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000906 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000907 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000908 }
909}
910
niklase@google.com470e71d2011-07-07 08:21:25 +0000911TEST_F(ApmTest, EchoCancellation) {
912 EXPECT_EQ(apm_->kNoError,
913 apm_->echo_cancellation()->enable_drift_compensation(true));
914 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
915 EXPECT_EQ(apm_->kNoError,
916 apm_->echo_cancellation()->enable_drift_compensation(false));
917 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
918
niklase@google.com470e71d2011-07-07 08:21:25 +0000919 EchoCancellation::SuppressionLevel level[] = {
920 EchoCancellation::kLowSuppression,
921 EchoCancellation::kModerateSuppression,
922 EchoCancellation::kHighSuppression,
923 };
pkasting25702cb2016-01-08 13:50:27 -0800924 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000925 EXPECT_EQ(apm_->kNoError,
926 apm_->echo_cancellation()->set_suppression_level(level[i]));
927 EXPECT_EQ(level[i],
928 apm_->echo_cancellation()->suppression_level());
929 }
930
931 EchoCancellation::Metrics metrics;
932 EXPECT_EQ(apm_->kNotEnabledError,
933 apm_->echo_cancellation()->GetMetrics(&metrics));
934
ivoc3e9a5372016-10-28 07:55:33 -0700935 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
936 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
937
niklase@google.com470e71d2011-07-07 08:21:25 +0000938 EXPECT_EQ(apm_->kNoError,
939 apm_->echo_cancellation()->enable_metrics(true));
940 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
941 EXPECT_EQ(apm_->kNoError,
942 apm_->echo_cancellation()->enable_metrics(false));
943 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
944
ivoc48dfab52016-10-28 03:29:31 -0700945 EXPECT_EQ(apm_->kNoError,
946 apm_->echo_cancellation()->enable_delay_logging(true));
947 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
948 EXPECT_EQ(apm_->kNoError,
949 apm_->echo_cancellation()->enable_delay_logging(false));
950 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
951
ivoc3e9a5372016-10-28 07:55:33 -0700952 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
953 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
954
955 int median = 0;
956 int std = 0;
957 float poor_fraction = 0;
958 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
959 &median, &std, &poor_fraction));
960
niklase@google.com470e71d2011-07-07 08:21:25 +0000961 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
962 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
963 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
964 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000965
966 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
967 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
968 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
969 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
970 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
971 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000972}
973
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000974TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000975 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000976 // Enable AEC only.
977 EXPECT_EQ(apm_->kNoError,
978 apm_->echo_cancellation()->enable_drift_compensation(false));
979 EXPECT_EQ(apm_->kNoError,
980 apm_->echo_cancellation()->enable_metrics(false));
981 EXPECT_EQ(apm_->kNoError,
982 apm_->echo_cancellation()->enable_delay_logging(true));
983 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000984 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700985 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000986 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000987
988 // Internally in the AEC the amount of lookahead the delay estimation can
989 // handle is 15 blocks and the maximum delay is set to 60 blocks.
990 const int kLookaheadBlocks = 15;
991 const int kMaxDelayBlocks = 60;
992 // The AEC has a startup time before it actually starts to process. This
993 // procedure can flush the internal far-end buffer, which of course affects
994 // the delay estimation. Therefore, we set a system_delay high enough to
995 // avoid that. The smallest system_delay you can report without flushing the
996 // buffer is 66 ms in 8 kHz.
997 //
998 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
999 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1000 // delay estimation. This should be noted though. In case of test failure,
1001 // this could be the cause.
1002 const int kSystemDelayMs = 66;
1003 // Test a couple of corner cases and verify that the estimated delay is
1004 // within a valid region (set to +-1.5 blocks). Note that these cases are
1005 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001006 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001007 Init(kProcessSampleRates[i],
1008 kProcessSampleRates[i],
1009 kProcessSampleRates[i],
1010 2,
1011 2,
1012 2,
1013 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001014 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001015 const int num_ms_per_block =
1016 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001017 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1018 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1019
1020 // 1) Verify correct delay estimate at lookahead boundary.
1021 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1022 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1023 delay_max_ms);
1024 // 2) A delay less than maximum lookahead should give an delay estimate at
1025 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1026 delay_ms -= 20;
1027 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1028 delay_max_ms);
1029 // 3) Three values around zero delay. Note that we need to compensate for
1030 // the fake system_delay.
1031 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1032 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1033 delay_max_ms);
1034 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1035 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1036 delay_max_ms);
1037 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1038 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1039 delay_max_ms);
1040 // 4) Verify correct delay estimate at maximum delay boundary.
1041 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1042 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1043 delay_max_ms);
1044 // 5) A delay above the maximum delay should give an estimate at the
1045 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1046 delay_ms += 20;
1047 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1048 delay_max_ms);
1049 }
1050}
1051
niklase@google.com470e71d2011-07-07 08:21:25 +00001052TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001054 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1056 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1057
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 // Toggle routing modes
1059 EchoControlMobile::RoutingMode mode[] = {
1060 EchoControlMobile::kQuietEarpieceOrHeadset,
1061 EchoControlMobile::kEarpiece,
1062 EchoControlMobile::kLoudEarpiece,
1063 EchoControlMobile::kSpeakerphone,
1064 EchoControlMobile::kLoudSpeakerphone,
1065 };
pkasting25702cb2016-01-08 13:50:27 -08001066 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 EXPECT_EQ(apm_->kNoError,
1068 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1069 EXPECT_EQ(mode[i],
1070 apm_->echo_control_mobile()->routing_mode());
1071 }
1072 // Turn comfort noise off/on
1073 EXPECT_EQ(apm_->kNoError,
1074 apm_->echo_control_mobile()->enable_comfort_noise(false));
1075 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1076 EXPECT_EQ(apm_->kNoError,
1077 apm_->echo_control_mobile()->enable_comfort_noise(true));
1078 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001079 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001080 const size_t echo_path_size =
1081 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001082 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1083 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001084 EXPECT_EQ(apm_->kNullPointerError,
1085 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1086 EXPECT_EQ(apm_->kNullPointerError,
1087 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1088 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001089 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001090 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001091 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001092 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001093 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001094 echo_path_in[i] = echo_path_out[i] + 1;
1095 }
1096 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001097 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
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()->SetEchoPath(echo_path_in.get(),
1100 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001101 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001102 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1103 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001104 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001105 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1106 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001107
1108 // Process a few frames with NS in the default disabled state. This exercises
1109 // a different codepath than with it enabled.
1110 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1111 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1112 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1113 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1114
niklase@google.com470e71d2011-07-07 08:21:25 +00001115 // Turn AECM off
1116 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1117 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1118}
1119
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001120TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001121 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001122 EXPECT_EQ(apm_->kNoError,
1123 apm_->gain_control()->set_mode(
1124 apm_->gain_control()->mode()));
1125
1126 GainControl::Mode mode[] = {
1127 GainControl::kAdaptiveAnalog,
1128 GainControl::kAdaptiveDigital,
1129 GainControl::kFixedDigital
1130 };
pkasting25702cb2016-01-08 13:50:27 -08001131 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001132 EXPECT_EQ(apm_->kNoError,
1133 apm_->gain_control()->set_mode(mode[i]));
1134 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1135 }
1136 // Testing invalid target levels
1137 EXPECT_EQ(apm_->kBadParameterError,
1138 apm_->gain_control()->set_target_level_dbfs(-3));
1139 EXPECT_EQ(apm_->kBadParameterError,
1140 apm_->gain_control()->set_target_level_dbfs(-40));
1141 // Testing valid target levels
1142 EXPECT_EQ(apm_->kNoError,
1143 apm_->gain_control()->set_target_level_dbfs(
1144 apm_->gain_control()->target_level_dbfs()));
1145
1146 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001147 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001148 EXPECT_EQ(apm_->kNoError,
1149 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1150 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1151 }
1152
1153 // Testing invalid compression gains
1154 EXPECT_EQ(apm_->kBadParameterError,
1155 apm_->gain_control()->set_compression_gain_db(-1));
1156 EXPECT_EQ(apm_->kBadParameterError,
1157 apm_->gain_control()->set_compression_gain_db(100));
1158
1159 // Testing valid compression gains
1160 EXPECT_EQ(apm_->kNoError,
1161 apm_->gain_control()->set_compression_gain_db(
1162 apm_->gain_control()->compression_gain_db()));
1163
1164 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001165 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001166 EXPECT_EQ(apm_->kNoError,
1167 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1168 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1169 }
1170
1171 // Testing limiter off/on
1172 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1173 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1174 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1175 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1176
1177 // Testing invalid level limits
1178 EXPECT_EQ(apm_->kBadParameterError,
1179 apm_->gain_control()->set_analog_level_limits(-1, 512));
1180 EXPECT_EQ(apm_->kBadParameterError,
1181 apm_->gain_control()->set_analog_level_limits(100000, 512));
1182 EXPECT_EQ(apm_->kBadParameterError,
1183 apm_->gain_control()->set_analog_level_limits(512, -1));
1184 EXPECT_EQ(apm_->kBadParameterError,
1185 apm_->gain_control()->set_analog_level_limits(512, 100000));
1186 EXPECT_EQ(apm_->kBadParameterError,
1187 apm_->gain_control()->set_analog_level_limits(512, 255));
1188
1189 // Testing valid level limits
1190 EXPECT_EQ(apm_->kNoError,
1191 apm_->gain_control()->set_analog_level_limits(
1192 apm_->gain_control()->analog_level_minimum(),
1193 apm_->gain_control()->analog_level_maximum()));
1194
1195 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001196 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001197 EXPECT_EQ(apm_->kNoError,
1198 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1199 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1200 }
1201
1202 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001203 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001204 EXPECT_EQ(apm_->kNoError,
1205 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1206 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1207 }
1208
1209 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1210
1211 // Turn AGC off
1212 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1213 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1214}
1215
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001216void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001217 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001218 EXPECT_EQ(apm_->kNoError,
1219 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1220 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1221
1222 int out_analog_level = 0;
1223 for (int i = 0; i < 2000; ++i) {
1224 ReadFrameWithRewind(near_file_, frame_);
1225 // Ensure the audio is at a low level, so the AGC will try to increase it.
1226 ScaleFrame(frame_, 0.25);
1227
1228 // Always pass in the same volume.
1229 EXPECT_EQ(apm_->kNoError,
1230 apm_->gain_control()->set_stream_analog_level(100));
1231 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1232 out_analog_level = apm_->gain_control()->stream_analog_level();
1233 }
1234
1235 // Ensure the AGC is still able to reach the maximum.
1236 EXPECT_EQ(255, out_analog_level);
1237}
1238
1239// Verifies that despite volume slider quantization, the AGC can continue to
1240// increase its volume.
1241TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001242 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001243 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1244 }
1245}
1246
1247void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001248 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001249 EXPECT_EQ(apm_->kNoError,
1250 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1251 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1252
1253 int out_analog_level = 100;
1254 for (int i = 0; i < 1000; ++i) {
1255 ReadFrameWithRewind(near_file_, frame_);
1256 // Ensure the audio is at a low level, so the AGC will try to increase it.
1257 ScaleFrame(frame_, 0.25);
1258
1259 EXPECT_EQ(apm_->kNoError,
1260 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1261 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1262 out_analog_level = apm_->gain_control()->stream_analog_level();
1263 }
1264
1265 // Ensure the volume was raised.
1266 EXPECT_GT(out_analog_level, 100);
1267 int highest_level_reached = out_analog_level;
1268 // Simulate a user manual volume change.
1269 out_analog_level = 100;
1270
1271 for (int i = 0; i < 300; ++i) {
1272 ReadFrameWithRewind(near_file_, frame_);
1273 ScaleFrame(frame_, 0.25);
1274
1275 EXPECT_EQ(apm_->kNoError,
1276 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1277 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1278 out_analog_level = apm_->gain_control()->stream_analog_level();
1279 // Check that AGC respected the manually adjusted volume.
1280 EXPECT_LT(out_analog_level, highest_level_reached);
1281 }
1282 // Check that the volume was still raised.
1283 EXPECT_GT(out_analog_level, 100);
1284}
1285
1286TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001287 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001288 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1289 }
1290}
1291
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001292#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1293TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1294 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001295 const size_t kSamplesPerChannel =
1296 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001297 const size_t kNumInputChannels = 2;
1298 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001299 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001300 const float kScaleFactor = 0.25f;
1301 Config config;
1302 std::vector<webrtc::Point> geometry;
1303 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1304 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1305 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001306 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001307 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001308 std::unique_ptr<AudioProcessing> apm(
Sam Zackrisson0beac582017-09-25 12:04:02 +02001309 AudioProcessing::Create(config, nullptr, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001310 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1311 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1312 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001313 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1314 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001315 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1316 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001317 std::string filename = ResourceFilePath("far", kSampleRateHz);
1318 FILE* far_file = fopen(filename.c_str(), "rb");
1319 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1320 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1321 const int kDefaultCompressionGain =
1322 apm->gain_control()->compression_gain_db();
1323 bool is_target = false;
1324 EXPECT_CALL(*beamformer, is_target_present())
1325 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001326 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001327 ASSERT_TRUE(ReadChunk(far_file,
1328 int_data.get(),
1329 float_data.get(),
1330 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001331 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001332 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001333 src_buf.channels()[j][k] *= kScaleFactor;
1334 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001335 }
1336 EXPECT_EQ(kNoErr,
1337 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001338 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001339 kSampleRateHz,
1340 LayoutFromChannels(src_buf.num_channels()),
1341 kSampleRateHz,
1342 LayoutFromChannels(dest_buf.num_channels()),
1343 dest_buf.channels()));
1344 }
1345 EXPECT_EQ(kDefaultVolume,
1346 apm->gain_control()->stream_analog_level());
1347 EXPECT_EQ(kDefaultCompressionGain,
1348 apm->gain_control()->compression_gain_db());
1349 rewind(far_file);
1350 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001351 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001352 ASSERT_TRUE(ReadChunk(far_file,
1353 int_data.get(),
1354 float_data.get(),
1355 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001356 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001357 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001358 src_buf.channels()[j][k] *= kScaleFactor;
1359 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001360 }
1361 EXPECT_EQ(kNoErr,
1362 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001363 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001364 kSampleRateHz,
1365 LayoutFromChannels(src_buf.num_channels()),
1366 kSampleRateHz,
1367 LayoutFromChannels(dest_buf.num_channels()),
1368 dest_buf.channels()));
1369 }
1370 EXPECT_LT(kDefaultVolume,
1371 apm->gain_control()->stream_analog_level());
1372 EXPECT_LT(kDefaultCompressionGain,
1373 apm->gain_control()->compression_gain_db());
1374 ASSERT_EQ(0, fclose(far_file));
1375}
1376#endif
1377
niklase@google.com470e71d2011-07-07 08:21:25 +00001378TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001379 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001380 NoiseSuppression::Level level[] = {
1381 NoiseSuppression::kLow,
1382 NoiseSuppression::kModerate,
1383 NoiseSuppression::kHigh,
1384 NoiseSuppression::kVeryHigh
1385 };
pkasting25702cb2016-01-08 13:50:27 -08001386 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001387 EXPECT_EQ(apm_->kNoError,
1388 apm_->noise_suppression()->set_level(level[i]));
1389 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1390 }
1391
andrew@webrtc.org648af742012-02-08 01:57:29 +00001392 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001393 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1394 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1395 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1396 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1397}
1398
1399TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001400 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001401 AudioProcessing::Config apm_config;
1402 apm_config.high_pass_filter.enabled = true;
1403 apm_->ApplyConfig(apm_config);
1404 apm_config.high_pass_filter.enabled = false;
1405 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001406}
1407
1408TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001409 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001410 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001411 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001412
1413 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1414
1415 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1416 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1417
1418 // Run this test in wideband; in super-wb, the splitting filter distorts the
1419 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001420 frame_->samples_per_channel_ = 160;
1421 frame_->num_channels_ = 2;
1422 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001423
1424 // Min value if no frames have been processed.
1425 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1426
1427 // Min value on zero frames.
1428 SetFrameTo(frame_, 0);
1429 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1430 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1431 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1432
1433 // Try a few RMS values.
1434 // (These also test that the value resets after retrieving it.)
1435 SetFrameTo(frame_, 32767);
1436 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1437 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1438 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1439
1440 SetFrameTo(frame_, 30000);
1441 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1442 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1443 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1444
1445 SetFrameTo(frame_, 10000);
1446 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1447 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1448 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1449
1450 SetFrameTo(frame_, 10);
1451 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1452 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1453 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1454
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001455 // Verify reset after enable/disable.
1456 SetFrameTo(frame_, 32767);
1457 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1458 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1459 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1460 SetFrameTo(frame_, 1);
1461 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1462 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1463
1464 // Verify reset after initialize.
1465 SetFrameTo(frame_, 32767);
1466 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1467 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1468 SetFrameTo(frame_, 1);
1469 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1470 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001471}
1472
1473TEST_F(ApmTest, VoiceDetection) {
1474 // Test external VAD
1475 EXPECT_EQ(apm_->kNoError,
1476 apm_->voice_detection()->set_stream_has_voice(true));
1477 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1478 EXPECT_EQ(apm_->kNoError,
1479 apm_->voice_detection()->set_stream_has_voice(false));
1480 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1481
andrew@webrtc.org648af742012-02-08 01:57:29 +00001482 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001483 VoiceDetection::Likelihood likelihood[] = {
1484 VoiceDetection::kVeryLowLikelihood,
1485 VoiceDetection::kLowLikelihood,
1486 VoiceDetection::kModerateLikelihood,
1487 VoiceDetection::kHighLikelihood
1488 };
pkasting25702cb2016-01-08 13:50:27 -08001489 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001490 EXPECT_EQ(apm_->kNoError,
1491 apm_->voice_detection()->set_likelihood(likelihood[i]));
1492 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1493 }
1494
1495 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001496 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001497 EXPECT_EQ(apm_->kBadParameterError,
1498 apm_->voice_detection()->set_frame_size_ms(12));
1499
andrew@webrtc.org648af742012-02-08 01:57:29 +00001500 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001501 for (int i = 10; i <= 30; i += 10) {
1502 EXPECT_EQ(apm_->kNoError,
1503 apm_->voice_detection()->set_frame_size_ms(i));
1504 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1505 }
1506 */
1507
andrew@webrtc.org648af742012-02-08 01:57:29 +00001508 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001509 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1510 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1511 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1512 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1513
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001514 // Test that AudioFrame activity is maintained when VAD is disabled.
1515 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1516 AudioFrame::VADActivity activity[] = {
1517 AudioFrame::kVadActive,
1518 AudioFrame::kVadPassive,
1519 AudioFrame::kVadUnknown
1520 };
pkasting25702cb2016-01-08 13:50:27 -08001521 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001522 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001523 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001524 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001525 }
1526
1527 // Test that AudioFrame activity is set when VAD is enabled.
1528 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001529 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001530 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001531 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001532
niklase@google.com470e71d2011-07-07 08:21:25 +00001533 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1534}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001535
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001536TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1537 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1538 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1539 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1540 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1541 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1542 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1543 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1544}
1545
1546TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001547 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001548 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001549 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001550 AudioFrame frame_copy;
1551 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001552 for (int j = 0; j < 1000; j++) {
1553 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1554 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001555 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1556 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001557 }
1558 }
1559}
1560
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001561TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1562 // Test that ProcessStream copies input to output even with no processing.
1563 const size_t kSamples = 80;
1564 const int sample_rate = 8000;
1565 const float src[kSamples] = {
1566 -1.0f, 0.0f, 1.0f
1567 };
1568 float dest[kSamples] = {};
1569
1570 auto src_channels = &src[0];
1571 auto dest_channels = &dest[0];
1572
1573 apm_.reset(AudioProcessing::Create());
1574 EXPECT_NOERR(apm_->ProcessStream(
1575 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1576 sample_rate, LayoutFromChannels(1), &dest_channels));
1577
1578 for (size_t i = 0; i < kSamples; ++i) {
1579 EXPECT_EQ(src[i], dest[i]);
1580 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001581
1582 // Same for ProcessReverseStream.
1583 float rev_dest[kSamples] = {};
1584 auto rev_dest_channels = &rev_dest[0];
1585
1586 StreamConfig input_stream = {sample_rate, 1};
1587 StreamConfig output_stream = {sample_rate, 1};
1588 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1589 output_stream, &rev_dest_channels));
1590
1591 for (size_t i = 0; i < kSamples; ++i) {
1592 EXPECT_EQ(src[i], rev_dest[i]);
1593 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001594}
1595
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001596TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1597 EnableAllComponents();
1598
pkasting25702cb2016-01-08 13:50:27 -08001599 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001600 Init(kProcessSampleRates[i],
1601 kProcessSampleRates[i],
1602 kProcessSampleRates[i],
1603 2,
1604 2,
1605 2,
1606 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001607 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001608 ASSERT_EQ(0, feof(far_file_));
1609 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001610 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001611 CopyLeftToRightChannel(revframe_->mutable_data(),
1612 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001613
aluebsb0319552016-03-17 20:39:53 -07001614 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001615
yujo36b1a5f2017-06-12 12:45:32 -07001616 CopyLeftToRightChannel(frame_->mutable_data(),
1617 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001618 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1619
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001620 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001621 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001622 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001623 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001624 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001625 analog_level = apm_->gain_control()->stream_analog_level();
1626
yujo36b1a5f2017-06-12 12:45:32 -07001627 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001628 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001629 rewind(far_file_);
1630 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001631 }
1632}
1633
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001634TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001635 // Verify the filter is not active through undistorted audio when:
1636 // 1. No components are enabled...
1637 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001638 AudioFrame frame_copy;
1639 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001640 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1641 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1642 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1643
1644 // 2. Only the level estimator is enabled...
1645 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001646 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001647 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1648 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1649 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1650 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1651 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1652
1653 // 3. Only VAD is enabled...
1654 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001655 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001656 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1658 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1659 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1660 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1661
1662 // 4. Both VAD and the level estimator are enabled...
1663 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001664 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001665 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1666 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1667 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1668 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1669 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1670 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1671 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1672
1673 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001674 frame_->samples_per_channel_ = 160;
1675 frame_->num_channels_ = 2;
1676 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001677 // Enable AEC, which would require the filter in super-wb. We rely on the
1678 // first few frames of data being unaffected by the AEC.
1679 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1680 // behavior of the AEC. Think of something more robust.
1681 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001682 // Make sure we have extended filter enabled. This makes sure nothing is
1683 // touched until we have a farend frame.
1684 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001685 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001686 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001687 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001688 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001689 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_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001693 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001694 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1695 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1696
1697 // Check the test is valid. We should have distortion from the filter
1698 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001699 frame_->samples_per_channel_ = 320;
1700 frame_->num_channels_ = 2;
1701 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001702 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001703 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001704 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001705 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001706 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1707 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1708}
1709
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001710#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1711void ApmTest::ProcessDebugDump(const std::string& in_filename,
1712 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001713 Format format,
1714 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001715 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001716 FILE* in_file = fopen(in_filename.c_str(), "rb");
1717 ASSERT_TRUE(in_file != NULL);
1718 audioproc::Event event_msg;
1719 bool first_init = true;
1720
1721 while (ReadMessageFromFile(in_file, &event_msg)) {
1722 if (event_msg.type() == audioproc::Event::INIT) {
1723 const audioproc::Init msg = event_msg.init();
1724 int reverse_sample_rate = msg.sample_rate();
1725 if (msg.has_reverse_sample_rate()) {
1726 reverse_sample_rate = msg.reverse_sample_rate();
1727 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001728 int output_sample_rate = msg.sample_rate();
1729 if (msg.has_output_sample_rate()) {
1730 output_sample_rate = msg.output_sample_rate();
1731 }
1732
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001733 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001734 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001735 reverse_sample_rate,
1736 msg.num_input_channels(),
1737 msg.num_output_channels(),
1738 msg.num_reverse_channels(),
1739 false);
1740 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001741 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001742 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001743 auto aec_dump =
1744 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1745 EXPECT_TRUE(aec_dump);
1746 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001747 first_init = false;
1748 }
1749
1750 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1751 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1752
1753 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001754 ASSERT_EQ(revframe_->num_channels_,
1755 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001756 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001757 memcpy(revfloat_cb_->channels()[i],
1758 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001759 msg.channel(i).size());
1760 }
1761 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001762 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001763 if (format == kFloatFormat) {
1764 // We're using an int16 input file; convert to float.
1765 ConvertToFloat(*revframe_, revfloat_cb_.get());
1766 }
1767 }
1768 AnalyzeReverseStreamChooser(format);
1769
1770 } else if (event_msg.type() == audioproc::Event::STREAM) {
1771 const audioproc::Stream msg = event_msg.stream();
1772 // ProcessStream could have changed this for the output frame.
1773 frame_->num_channels_ = apm_->num_input_channels();
1774
1775 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1776 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1777 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1778 if (msg.has_keypress()) {
1779 apm_->set_stream_key_pressed(msg.keypress());
1780 } else {
1781 apm_->set_stream_key_pressed(true);
1782 }
1783
1784 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001785 ASSERT_EQ(frame_->num_channels_,
1786 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001787 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001788 memcpy(float_cb_->channels()[i],
1789 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001790 msg.input_channel(i).size());
1791 }
1792 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001793 memcpy(frame_->mutable_data(), msg.input_data().data(),
1794 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001795 if (format == kFloatFormat) {
1796 // We're using an int16 input file; convert to float.
1797 ConvertToFloat(*frame_, float_cb_.get());
1798 }
1799 }
1800 ProcessStreamChooser(format);
1801 }
1802 }
aleloif4dd1912017-06-15 01:55:38 -07001803 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001804 fclose(in_file);
1805}
1806
1807void ApmTest::VerifyDebugDumpTest(Format format) {
1808 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001809 std::string format_string;
1810 switch (format) {
1811 case kIntFormat:
1812 format_string = "_int";
1813 break;
1814 case kFloatFormat:
1815 format_string = "_float";
1816 break;
1817 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001818 const std::string ref_filename = test::TempFilename(
1819 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1820 const std::string out_filename = test::TempFilename(
1821 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001822 const std::string limited_filename = test::TempFilename(
1823 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1824 const size_t logging_limit_bytes = 100000;
1825 // We expect at least this many bytes in the created logfile.
1826 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001827 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001828 ProcessDebugDump(in_filename, ref_filename, format, -1);
1829 ProcessDebugDump(ref_filename, out_filename, format, -1);
1830 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001831
1832 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1833 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001834 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001835 ASSERT_TRUE(ref_file != NULL);
1836 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001837 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001838 std::unique_ptr<uint8_t[]> ref_bytes;
1839 std::unique_ptr<uint8_t[]> out_bytes;
1840 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001841
1842 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1843 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001844 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001845 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001846 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001847 while (ref_size > 0 && out_size > 0) {
1848 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001849 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001850 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001851 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001852 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001853 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001854 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1855 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001856 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001857 }
1858 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001859 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1860 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001861 EXPECT_NE(0, feof(ref_file));
1862 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001863 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001864 ASSERT_EQ(0, fclose(ref_file));
1865 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001866 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001867 remove(ref_filename.c_str());
1868 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001869 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001870}
1871
pbosc7a65692016-05-06 12:50:04 -07001872TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001873 VerifyDebugDumpTest(kIntFormat);
1874}
1875
pbosc7a65692016-05-06 12:50:04 -07001876TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001877 VerifyDebugDumpTest(kFloatFormat);
1878}
1879#endif
1880
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001881// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001882TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001883 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001884 const std::string filename =
1885 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001886 {
1887 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1888 EXPECT_FALSE(aec_dump);
1889 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001890
1891#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1892 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001893 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001894
aleloif4dd1912017-06-15 01:55:38 -07001895 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1896 EXPECT_TRUE(aec_dump);
1897 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001898 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001899 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001900 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001901
1902 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001903 FILE* fid = fopen(filename.c_str(), "r");
1904 ASSERT_TRUE(fid != NULL);
1905
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001906 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001907 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001908 ASSERT_EQ(0, remove(filename.c_str()));
1909#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001910 // Verify the file has NOT been written.
1911 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1912#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1913}
1914
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001915// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001916TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001917 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1918
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001919 const std::string filename =
1920 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001921 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001922 ASSERT_TRUE(fid);
1923
1924#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1925 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001926 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001927
aleloif4dd1912017-06-15 01:55:38 -07001928 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1929 EXPECT_TRUE(aec_dump);
1930 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001931 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001932 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001933 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001934
1935 // Verify the file has been written.
1936 fid = fopen(filename.c_str(), "r");
1937 ASSERT_TRUE(fid != NULL);
1938
1939 // Clean it up.
1940 ASSERT_EQ(0, fclose(fid));
1941 ASSERT_EQ(0, remove(filename.c_str()));
1942#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001943 ASSERT_EQ(0, fclose(fid));
1944#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1945}
1946
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001947TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001948 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001949 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001950
1951 Config config;
1952 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001953 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001954 EnableAllComponents();
1955 EnableAllAPComponents(fapm.get());
1956 for (int i = 0; i < ref_data.test_size(); i++) {
1957 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1958
1959 audioproc::Test* test = ref_data.mutable_test(i);
1960 // TODO(ajm): Restore downmixing test cases.
1961 if (test->num_input_channels() != test->num_output_channels())
1962 continue;
1963
Peter Kasting69558702016-01-12 16:26:35 -08001964 const size_t num_render_channels =
1965 static_cast<size_t>(test->num_reverse_channels());
1966 const size_t num_input_channels =
1967 static_cast<size_t>(test->num_input_channels());
1968 const size_t num_output_channels =
1969 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001970 const size_t samples_per_channel = static_cast<size_t>(
1971 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001972
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001973 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1974 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001975 Init(fapm.get());
1976
1977 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001978 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1979 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001980
1981 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001982 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001983 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1984 ReadFrame(near_file_, frame_, float_cb_.get())) {
1985 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1986
aluebsb0319552016-03-17 20:39:53 -07001987 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001988 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1989 revfloat_cb_->channels(),
1990 samples_per_channel,
1991 test->sample_rate(),
1992 LayoutFromChannels(num_render_channels)));
1993
1994 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1995 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1996 apm_->echo_cancellation()->set_stream_drift_samples(0);
1997 fapm->echo_cancellation()->set_stream_drift_samples(0);
1998 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1999 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
2000
2001 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07002002 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002003 output_int16.channels());
2004
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002005 EXPECT_NOERR(fapm->ProcessStream(
2006 float_cb_->channels(),
2007 samples_per_channel,
2008 test->sample_rate(),
2009 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002010 test->sample_rate(),
2011 LayoutFromChannels(num_output_channels),
2012 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002013 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002014 FloatToS16(float_cb_->channels()[j],
2015 samples_per_channel,
2016 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002017 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002018 float snr = ComputeSNR(output_int16.channels()[j],
2019 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002020 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002021
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002022 const float kVarianceThreshold = 20;
2023 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002024
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002025 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002026 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2027 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002028 }
2029 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002030
2031 analog_level = fapm->gain_control()->stream_analog_level();
2032 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2033 fapm->gain_control()->stream_analog_level());
2034 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2035 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002036 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2037 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002038 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002039
2040 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002041 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002042 }
aluebs776593b2016-03-15 14:04:58 -07002043
2044#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2045 const size_t kMaxNumBadChunks = 0;
2046#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2047 // There are a few chunks in the fixed-point profile that give low SNR.
2048 // Listening confirmed the difference is acceptable.
2049 const size_t kMaxNumBadChunks = 60;
2050#endif
2051 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2052
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002053 rewind(far_file_);
2054 rewind(near_file_);
2055 }
2056}
2057
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002058// TODO(andrew): Add a test to process a few frames with different combinations
2059// of enabled components.
2060
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002061TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002062 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002063 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002064
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002065 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002066 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002067 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002068 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002069 for (size_t i = 0; i < arraysize(kChannels); i++) {
2070 for (size_t j = 0; j < arraysize(kChannels); j++) {
2071 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002072 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002073 test->set_num_reverse_channels(kChannels[i]);
2074 test->set_num_input_channels(kChannels[j]);
2075 test->set_num_output_channels(kChannels[j]);
2076 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002077 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002078 }
2079 }
2080 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002081#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2082 // To test the extended filter mode.
2083 audioproc::Test* test = ref_data.add_test();
2084 test->set_num_reverse_channels(2);
2085 test->set_num_input_channels(2);
2086 test->set_num_output_channels(2);
2087 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2088 test->set_use_aec_extended_filter(true);
2089#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002090 }
2091
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002092 for (int i = 0; i < ref_data.test_size(); i++) {
2093 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002094
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002095 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002096 // TODO(ajm): We no longer allow different input and output channels. Skip
2097 // these tests for now, but they should be removed from the set.
2098 if (test->num_input_channels() != test->num_output_channels())
2099 continue;
2100
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002101 Config config;
2102 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002103 config.Set<ExtendedFilter>(
2104 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002105 apm_.reset(AudioProcessing::Create(config));
2106
2107 EnableAllComponents();
2108
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002109 Init(test->sample_rate(),
2110 test->sample_rate(),
2111 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002112 static_cast<size_t>(test->num_input_channels()),
2113 static_cast<size_t>(test->num_output_channels()),
2114 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002115 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002116
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002117 int frame_count = 0;
2118 int has_echo_count = 0;
2119 int has_voice_count = 0;
2120 int is_saturated_count = 0;
2121 int analog_level = 127;
2122 int analog_level_average = 0;
2123 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002124 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002125#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2126 int stats_index = 0;
2127#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002128
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002129 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002130 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002131
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002132 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2133
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002134 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002135 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002136 EXPECT_EQ(apm_->kNoError,
2137 apm_->gain_control()->set_stream_analog_level(analog_level));
2138
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002139 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002140
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002141 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002142 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2143 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002144
2145 max_output_average += MaxAudioFrame(*frame_);
2146
2147 if (apm_->echo_cancellation()->stream_has_echo()) {
2148 has_echo_count++;
2149 }
2150
2151 analog_level = apm_->gain_control()->stream_analog_level();
2152 analog_level_average += analog_level;
2153 if (apm_->gain_control()->stream_is_saturated()) {
2154 is_saturated_count++;
2155 }
2156 if (apm_->voice_detection()->stream_has_voice()) {
2157 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002158 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002159 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002160 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002161 }
2162
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002163 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2164
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002165 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002166 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002167 sizeof(int16_t),
2168 frame_size,
2169 out_file_);
2170 ASSERT_EQ(frame_size, write_count);
2171
2172 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002173 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002174 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002175
2176#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2177 const int kStatsAggregationFrameNum = 100; // 1 second.
2178 if (frame_count % kStatsAggregationFrameNum == 0) {
2179 // Get echo metrics.
2180 EchoCancellation::Metrics echo_metrics;
2181 EXPECT_EQ(apm_->kNoError,
2182 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2183
2184 // Get delay metrics.
2185 int median = 0;
2186 int std = 0;
2187 float fraction_poor_delays = 0;
2188 EXPECT_EQ(apm_->kNoError,
2189 apm_->echo_cancellation()->GetDelayMetrics(
2190 &median, &std, &fraction_poor_delays));
2191
2192 // Get RMS.
2193 int rms_level = apm_->level_estimator()->RMS();
2194 EXPECT_LE(0, rms_level);
2195 EXPECT_GE(127, rms_level);
2196
2197 if (!write_ref_data) {
2198 const audioproc::Test::EchoMetrics& reference =
2199 test->echo_metrics(stats_index);
2200 TestStats(echo_metrics.residual_echo_return_loss,
2201 reference.residual_echo_return_loss());
2202 TestStats(echo_metrics.echo_return_loss,
2203 reference.echo_return_loss());
2204 TestStats(echo_metrics.echo_return_loss_enhancement,
2205 reference.echo_return_loss_enhancement());
2206 TestStats(echo_metrics.a_nlp,
2207 reference.a_nlp());
2208 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2209 reference.divergent_filter_fraction());
2210
2211 const audioproc::Test::DelayMetrics& reference_delay =
2212 test->delay_metrics(stats_index);
2213 EXPECT_EQ(reference_delay.median(), median);
2214 EXPECT_EQ(reference_delay.std(), std);
2215 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2216 fraction_poor_delays);
2217
2218 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2219
2220 ++stats_index;
2221 } else {
2222 audioproc::Test::EchoMetrics* message =
2223 test->add_echo_metrics();
2224 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2225 message->mutable_residual_echo_return_loss());
2226 WriteStatsMessage(echo_metrics.echo_return_loss,
2227 message->mutable_echo_return_loss());
2228 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2229 message->mutable_echo_return_loss_enhancement());
2230 WriteStatsMessage(echo_metrics.a_nlp,
2231 message->mutable_a_nlp());
2232 message->set_divergent_filter_fraction(
2233 echo_metrics.divergent_filter_fraction);
2234
2235 audioproc::Test::DelayMetrics* message_delay =
2236 test->add_delay_metrics();
2237 message_delay->set_median(median);
2238 message_delay->set_std(std);
2239 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2240
2241 test->add_rms_level(rms_level);
2242 }
2243 }
2244#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002245 }
2246 max_output_average /= frame_count;
2247 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002248 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002249
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002250 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002251 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002252 // When running the test on a N7 we get a {2, 6} difference of
2253 // |has_voice_count| and |max_output_average| is up to 18 higher.
2254 // All numbers being consistently higher on N7 compare to ref_data.
2255 // TODO(bjornv): If we start getting more of these offsets on Android we
2256 // should consider a different approach. Either using one slack for all,
2257 // or generate a separate android reference.
2258#if defined(WEBRTC_ANDROID)
2259 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002260 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002261 const int kMaxOutputAverageOffset = 9;
2262 const int kMaxOutputAverageNear = 9;
2263#else
2264 const int kHasVoiceCountOffset = 0;
2265 const int kHasVoiceCountNear = kIntNear;
2266 const int kMaxOutputAverageOffset = 0;
2267 const int kMaxOutputAverageNear = kIntNear;
2268#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002269 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002270 EXPECT_NEAR(test->has_voice_count(),
2271 has_voice_count - kHasVoiceCountOffset,
2272 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002273 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002274
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002275 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002276 EXPECT_NEAR(test->max_output_average(),
2277 max_output_average - kMaxOutputAverageOffset,
2278 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002279#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002280 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002281 EXPECT_NEAR(test->ns_speech_probability_average(),
2282 ns_speech_prob_average,
2283 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002284#endif
2285 } else {
2286 test->set_has_echo_count(has_echo_count);
2287 test->set_has_voice_count(has_voice_count);
2288 test->set_is_saturated_count(is_saturated_count);
2289
2290 test->set_analog_level_average(analog_level_average);
2291 test->set_max_output_average(max_output_average);
2292
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002293#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002294 EXPECT_LE(0.0f, ns_speech_prob_average);
2295 EXPECT_GE(1.0f, ns_speech_prob_average);
2296 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002297#endif
2298 }
2299
2300 rewind(far_file_);
2301 rewind(near_file_);
2302 }
2303
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002304 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002305 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002306 }
2307}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002308
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002309TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2310 struct ChannelFormat {
2311 AudioProcessing::ChannelLayout in_layout;
2312 AudioProcessing::ChannelLayout out_layout;
2313 };
2314 ChannelFormat cf[] = {
2315 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2316 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2317 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2318 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002319
kwiberg62eaacf2016-02-17 06:39:05 -08002320 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002321 // Enable one component just to ensure some processing takes place.
2322 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002323 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002324 const int in_rate = 44100;
2325 const int out_rate = 48000;
2326 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2327 TotalChannelsFromLayout(cf[i].in_layout));
2328 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2329 ChannelsFromLayout(cf[i].out_layout));
2330
2331 // Run over a few chunks.
2332 for (int j = 0; j < 10; ++j) {
2333 EXPECT_NOERR(ap->ProcessStream(
2334 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002335 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002336 in_rate,
2337 cf[i].in_layout,
2338 out_rate,
2339 cf[i].out_layout,
2340 out_cb.channels()));
2341 }
2342 }
2343}
2344
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002345// Compares the reference and test arrays over a region around the expected
2346// delay. Finds the highest SNR in that region and adds the variance and squared
2347// error results to the supplied accumulators.
2348void UpdateBestSNR(const float* ref,
2349 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002350 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002351 int expected_delay,
2352 double* variance_acc,
2353 double* sq_error_acc) {
2354 double best_snr = std::numeric_limits<double>::min();
2355 double best_variance = 0;
2356 double best_sq_error = 0;
2357 // Search over a region of eight samples around the expected delay.
2358 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2359 ++delay) {
2360 double sq_error = 0;
2361 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002362 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002363 double error = test[i + delay] - ref[i];
2364 sq_error += error * error;
2365 variance += ref[i] * ref[i];
2366 }
2367
2368 if (sq_error == 0) {
2369 *variance_acc += variance;
2370 return;
2371 }
2372 double snr = variance / sq_error;
2373 if (snr > best_snr) {
2374 best_snr = snr;
2375 best_variance = variance;
2376 best_sq_error = sq_error;
2377 }
2378 }
2379
2380 *variance_acc += best_variance;
2381 *sq_error_acc += best_sq_error;
2382}
2383
2384// Used to test a multitude of sample rate and channel combinations. It works
2385// by first producing a set of reference files (in SetUpTestCase) that are
2386// assumed to be correct, as the used parameters are verified by other tests
2387// in this collection. Primarily the reference files are all produced at
2388// "native" rates which do not involve any resampling.
2389
2390// Each test pass produces an output file with a particular format. The output
2391// is matched against the reference file closest to its internal processing
2392// format. If necessary the output is resampled back to its process format.
2393// Due to the resampling distortion, we don't expect identical results, but
2394// enforce SNR thresholds which vary depending on the format. 0 is a special
2395// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002396typedef std::tr1::tuple<int, int, int, int, double, double>
2397 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002398class AudioProcessingTest
2399 : public testing::TestWithParam<AudioProcessingTestData> {
2400 public:
2401 AudioProcessingTest()
2402 : input_rate_(std::tr1::get<0>(GetParam())),
2403 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002404 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2405 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2406 expected_snr_(std::tr1::get<4>(GetParam())),
2407 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002408
2409 virtual ~AudioProcessingTest() {}
2410
2411 static void SetUpTestCase() {
2412 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002413 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002414 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002415 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2416 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2417 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002418 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002419 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2420 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2421 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002422 }
2423 }
2424 }
2425 }
2426
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002427 static void TearDownTestCase() {
2428 ClearTempFiles();
2429 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002430
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002431 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002432 // to a file specified with |output_file_prefix|. Both forward and reverse
2433 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002434 static void ProcessFormat(int input_rate,
2435 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002436 int reverse_input_rate,
2437 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002438 size_t num_input_channels,
2439 size_t num_output_channels,
2440 size_t num_reverse_input_channels,
2441 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002442 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002443 Config config;
2444 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002445 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002446 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002447
ekmeyerson60d9b332015-08-14 10:35:55 -07002448 ProcessingConfig processing_config = {
2449 {{input_rate, num_input_channels},
2450 {output_rate, num_output_channels},
2451 {reverse_input_rate, num_reverse_input_channels},
2452 {reverse_output_rate, num_reverse_output_channels}}};
2453 ap->Initialize(processing_config);
2454
2455 FILE* far_file =
2456 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002457 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002458 FILE* out_file =
2459 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2460 reverse_input_rate, reverse_output_rate,
2461 num_input_channels, num_output_channels,
2462 num_reverse_input_channels,
2463 num_reverse_output_channels, kForward).c_str(),
2464 "wb");
2465 FILE* rev_out_file =
2466 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2467 reverse_input_rate, reverse_output_rate,
2468 num_input_channels, num_output_channels,
2469 num_reverse_input_channels,
2470 num_reverse_output_channels, kReverse).c_str(),
2471 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002472 ASSERT_TRUE(far_file != NULL);
2473 ASSERT_TRUE(near_file != NULL);
2474 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002475 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002476
2477 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2478 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002479 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2480 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002481 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2482 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002483 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2484 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002485
2486 // Temporary buffers.
2487 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002488 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2489 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002490 std::unique_ptr<float[]> float_data(new float[max_length]);
2491 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002492
2493 int analog_level = 127;
2494 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2495 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002496 EXPECT_NOERR(ap->ProcessReverseStream(
2497 rev_cb.channels(), processing_config.reverse_input_stream(),
2498 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002499
2500 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2501 ap->echo_cancellation()->set_stream_drift_samples(0);
2502 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2503
2504 EXPECT_NOERR(ap->ProcessStream(
2505 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002506 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002507 input_rate,
2508 LayoutFromChannels(num_input_channels),
2509 output_rate,
2510 LayoutFromChannels(num_output_channels),
2511 out_cb.channels()));
2512
ekmeyerson60d9b332015-08-14 10:35:55 -07002513 // Dump forward output to file.
2514 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002515 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002516 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002517
pkasting25702cb2016-01-08 13:50:27 -08002518 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002519 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002520 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002521
ekmeyerson60d9b332015-08-14 10:35:55 -07002522 // Dump reverse output to file.
2523 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2524 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002525 size_t rev_out_length =
2526 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002527
pkasting25702cb2016-01-08 13:50:27 -08002528 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002529 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2530 rev_out_file));
2531
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002532 analog_level = ap->gain_control()->stream_analog_level();
2533 }
2534 fclose(far_file);
2535 fclose(near_file);
2536 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002537 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002538 }
2539
2540 protected:
2541 int input_rate_;
2542 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002543 int reverse_input_rate_;
2544 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002545 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002546 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002547};
2548
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002549TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002550 struct ChannelFormat {
2551 int num_input;
2552 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002553 int num_reverse_input;
2554 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002555 };
2556 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002557 {1, 1, 1, 1},
2558 {1, 1, 2, 1},
2559 {2, 1, 1, 1},
2560 {2, 1, 2, 1},
2561 {2, 2, 1, 1},
2562 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002563 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002564
pkasting25702cb2016-01-08 13:50:27 -08002565 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002566 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2567 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2568 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002569
ekmeyerson60d9b332015-08-14 10:35:55 -07002570 // Verify output for both directions.
2571 std::vector<StreamDirection> stream_directions;
2572 stream_directions.push_back(kForward);
2573 stream_directions.push_back(kReverse);
2574 for (StreamDirection file_direction : stream_directions) {
2575 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2576 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2577 const int out_num =
2578 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2579 const double expected_snr =
2580 file_direction ? expected_reverse_snr_ : expected_snr_;
2581
2582 const int min_ref_rate = std::min(in_rate, out_rate);
2583 int ref_rate;
2584
2585 if (min_ref_rate > 32000) {
2586 ref_rate = 48000;
2587 } else if (min_ref_rate > 16000) {
2588 ref_rate = 32000;
2589 } else if (min_ref_rate > 8000) {
2590 ref_rate = 16000;
2591 } else {
2592 ref_rate = 8000;
2593 }
aluebs776593b2016-03-15 14:04:58 -07002594#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002595 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002596 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002597 }
2598#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002599 FILE* out_file = fopen(
2600 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2601 reverse_output_rate_, cf[i].num_input,
2602 cf[i].num_output, cf[i].num_reverse_input,
2603 cf[i].num_reverse_output, file_direction).c_str(),
2604 "rb");
2605 // The reference files always have matching input and output channels.
2606 FILE* ref_file = fopen(
2607 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2608 cf[i].num_output, cf[i].num_output,
2609 cf[i].num_reverse_output, cf[i].num_reverse_output,
2610 file_direction).c_str(),
2611 "rb");
2612 ASSERT_TRUE(out_file != NULL);
2613 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002614
pkasting25702cb2016-01-08 13:50:27 -08002615 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2616 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002617 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002618 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002619 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002620 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002621 // Data from the resampled output, in case the reference and output rates
2622 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002623 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002624
ekmeyerson60d9b332015-08-14 10:35:55 -07002625 PushResampler<float> resampler;
2626 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002627
ekmeyerson60d9b332015-08-14 10:35:55 -07002628 // Compute the resampling delay of the output relative to the reference,
2629 // to find the region over which we should search for the best SNR.
2630 float expected_delay_sec = 0;
2631 if (in_rate != ref_rate) {
2632 // Input resampling delay.
2633 expected_delay_sec +=
2634 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2635 }
2636 if (out_rate != ref_rate) {
2637 // Output resampling delay.
2638 expected_delay_sec +=
2639 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2640 // Delay of converting the output back to its processing rate for
2641 // testing.
2642 expected_delay_sec +=
2643 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2644 }
2645 int expected_delay =
2646 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002647
ekmeyerson60d9b332015-08-14 10:35:55 -07002648 double variance = 0;
2649 double sq_error = 0;
2650 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2651 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2652 float* out_ptr = out_data.get();
2653 if (out_rate != ref_rate) {
2654 // Resample the output back to its internal processing rate if
2655 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002656 ASSERT_EQ(ref_length,
2657 static_cast<size_t>(resampler.Resample(
2658 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002659 out_ptr = cmp_data.get();
2660 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002661
ekmeyerson60d9b332015-08-14 10:35:55 -07002662 // Update the |sq_error| and |variance| accumulators with the highest
2663 // SNR of reference vs output.
2664 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2665 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002666 }
2667
ekmeyerson60d9b332015-08-14 10:35:55 -07002668 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2669 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2670 << cf[i].num_input << ", " << cf[i].num_output << ", "
2671 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2672 << ", " << file_direction << "): ";
2673 if (sq_error > 0) {
2674 double snr = 10 * log10(variance / sq_error);
2675 EXPECT_GE(snr, expected_snr);
2676 EXPECT_NE(0, expected_snr);
2677 std::cout << "SNR=" << snr << " dB" << std::endl;
2678 } else {
aluebs776593b2016-03-15 14:04:58 -07002679 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002680 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002681
ekmeyerson60d9b332015-08-14 10:35:55 -07002682 fclose(out_file);
2683 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002684 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002685 }
2686}
2687
2688#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2689INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002690 CommonFormats,
2691 AudioProcessingTest,
2692 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002693 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2694 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002695 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2696 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2697 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2698 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2699 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2700 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2701 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2702 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2703 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002704
ekmeyerson60d9b332015-08-14 10:35:55 -07002705 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2706 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2707 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2708 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2709 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2710 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2711 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2712 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2713 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2714 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2715 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2716 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002717
ekmeyerson60d9b332015-08-14 10:35:55 -07002718 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2719 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2720 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2721 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2722 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2723 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2724 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2725 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2726 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2727 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2728 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2729 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002730
ekmeyerson60d9b332015-08-14 10:35:55 -07002731 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2732 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2733 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2734 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2735 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2736 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2737 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2738 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2739 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2740 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002741 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002742 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002743
2744#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2745INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002746 CommonFormats,
2747 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002748 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2749 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2750 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2751 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2752 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2753 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002754 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2755 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2756 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2757 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2758 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2759 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002760
aluebs776593b2016-03-15 14:04:58 -07002761 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2762 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2763 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002764 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2765 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2766 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2767 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2768 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2769 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2770 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2771 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2772 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002773
aluebs776593b2016-03-15 14:04:58 -07002774 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2775 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2776 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2777 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2778 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2779 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2780 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2781 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2782 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002783 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2784 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2785 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002786
ekmeyerson60d9b332015-08-14 10:35:55 -07002787 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2788 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2789 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2790 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2791 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2792 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2793 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2794 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2795 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2796 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002797 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002798 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002799#endif
2800
niklase@google.com470e71d2011-07-07 08:21:25 +00002801} // namespace
peahc19f3122016-10-07 14:54:10 -07002802
2803TEST(ApmConfiguration, DefaultBehavior) {
2804 // Verify that the level controller is default off, it can be activated using
2805 // the config, and that the default initial level is maintained after the
2806 // config has been applied.
2807 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002808 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002809 AudioProcessing::Config config;
2810 EXPECT_FALSE(apm->config_.level_controller.enabled);
2811 // TODO(peah): Add test for the existence of the level controller object once
2812 // that is created only when that is specified in the config.
2813 // TODO(peah): Remove the testing for
2814 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2815 // is instead used to activate the level controller.
2816 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2817 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2818 apm->config_.level_controller.initial_peak_level_dbfs,
2819 std::numeric_limits<float>::epsilon());
2820 config.level_controller.enabled = true;
2821 apm->ApplyConfig(config);
2822 EXPECT_TRUE(apm->config_.level_controller.enabled);
2823 // TODO(peah): Add test for the existence of the level controller object once
2824 // that is created only when the that is specified in the config.
2825 // TODO(peah): Remove the testing for
2826 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2827 // is instead used to activate the level controller.
2828 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2829 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2830 apm->config_.level_controller.initial_peak_level_dbfs,
2831 std::numeric_limits<float>::epsilon());
2832}
2833
2834TEST(ApmConfiguration, ValidConfigBehavior) {
2835 // Verify that the initial level can be specified and is retained after the
2836 // config has been applied.
2837 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002838 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002839 AudioProcessing::Config config;
2840 config.level_controller.initial_peak_level_dbfs = -50.f;
2841 apm->ApplyConfig(config);
2842 EXPECT_FALSE(apm->config_.level_controller.enabled);
2843 // TODO(peah): Add test for the existence of the level controller object once
2844 // that is created only when the that is specified in the config.
2845 // TODO(peah): Remove the testing for
2846 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2847 // is instead used to activate the level controller.
2848 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2849 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2850 std::numeric_limits<float>::epsilon());
2851}
2852
2853TEST(ApmConfiguration, InValidConfigBehavior) {
2854 // Verify that the config is properly reset when nonproper values are applied
2855 // for the initial level.
2856
2857 // Verify that the config is properly reset when the specified initial peak
2858 // level is too low.
2859 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002860 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002861 AudioProcessing::Config config;
2862 config.level_controller.enabled = true;
2863 config.level_controller.initial_peak_level_dbfs = -101.f;
2864 apm->ApplyConfig(config);
2865 EXPECT_FALSE(apm->config_.level_controller.enabled);
2866 // TODO(peah): Add test for the existence of the level controller object once
2867 // that is created only when the that is specified in the config.
2868 // TODO(peah): Remove the testing for
2869 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2870 // is instead used to activate the level controller.
2871 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2872 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2873 apm->config_.level_controller.initial_peak_level_dbfs,
2874 std::numeric_limits<float>::epsilon());
2875
2876 // Verify that the config is properly reset when the specified initial peak
2877 // level is too high.
peaha9cc40b2017-06-29 08:32:09 -07002878 apm.reset(new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002879 config = AudioProcessing::Config();
2880 config.level_controller.enabled = true;
2881 config.level_controller.initial_peak_level_dbfs = 1.f;
2882 apm->ApplyConfig(config);
2883 EXPECT_FALSE(apm->config_.level_controller.enabled);
2884 // TODO(peah): Add test for the existence of the level controller object once
2885 // that is created only when that is specified in the config.
2886 // TODO(peah): Remove the testing for
2887 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2888 // is instead used to activate the level controller.
2889 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2890 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2891 apm->config_.level_controller.initial_peak_level_dbfs,
2892 std::numeric_limits<float>::epsilon());
2893}
2894
Sam Zackrisson0beac582017-09-25 12:04:02 +02002895TEST(ApmConfiguration, EnablePostProcessing) {
2896 // Verify that apm uses a capture post processing module if one is provided.
2897 webrtc::Config webrtc_config;
2898 auto mock_post_processor_ptr =
2899 new testing::NiceMock<test::MockPostProcessing>();
2900 auto mock_post_processor =
2901 std::unique_ptr<PostProcessing>(mock_post_processor_ptr);
2902 rtc::scoped_refptr<AudioProcessing> apm = AudioProcessing::Create(
2903 webrtc_config, std::move(mock_post_processor), nullptr);
2904
2905 AudioFrame audio;
2906 audio.num_channels_ = 1;
2907 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2908
2909 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
2910 std::cout << apm->ProcessStream(&audio) << std::endl;
2911}
2912
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002913} // namespace webrtc