blob: 948c5efd93f4a3fc77a5974304445d88d9fa935d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000011#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000012#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080013
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000014#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
pkasting25702cb2016-01-08 13:50:27 -080019#include "webrtc/base/arraysize.h"
andrew@webrtc.org27c69802014-02-18 20:24:56 +000020#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000021#include "webrtc/common_audio/resampler/include/push_resampler.h"
22#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000023#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000024#include "webrtc/modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000025#include "webrtc/modules/audio_processing/common.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000026#include "webrtc/modules/audio_processing/include/audio_processing.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070027#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000028#include "webrtc/modules/audio_processing/test/test_utils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010030#include "webrtc/system_wrappers/include/event_wrapper.h"
31#include "webrtc/system_wrappers/include/trace.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000032#include "webrtc/test/testsupport/fileutils.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000033#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
pbos@webrtc.org8c34cee2013-05-28 09:24:03 +000034#include "gtest/gtest.h"
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000035#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000036#else
pbos@webrtc.org8c34cee2013-05-28 09:24:03 +000037#include "testing/gtest/include/gtest/gtest.h"
kjellander78ddd732016-02-09 08:13:06 -080038#include "webrtc/modules/audio_processing/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000039#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000040
andrew@webrtc.org27c69802014-02-18 20:24:56 +000041namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000042namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000043
ekmeyerson60d9b332015-08-14 10:35:55 -070044// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
45// applicable.
46
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000047// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000048// re-implemented; see comment at the bottom of this file. For now, the user has
49// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000050// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000051// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000052// be set to true with the command-line switch --write_ref_data.
53bool write_ref_data = false;
pkasting25702cb2016-01-08 13:50:27 -080054const google::protobuf::int32 kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070055const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000056
Alejandro Luebs47748742015-05-22 12:00:21 -070057const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000058
ekmeyerson60d9b332015-08-14 10:35:55 -070059enum StreamDirection { kForward = 0, kReverse };
60
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000061void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000062 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000063 cb->num_channels());
64 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000065 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000066 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000067 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080068 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000069 S16ToFloat(cb_int.channels()[i],
70 cb->num_frames(),
71 cb->channels()[i]);
72 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000073}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000074
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
76 ConvertToFloat(frame.data_, cb);
77}
78
andrew@webrtc.org103657b2014-04-24 18:28:56 +000079// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080080size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000081 switch (layout) {
82 case AudioProcessing::kMono:
83 return 1;
84 case AudioProcessing::kMonoAndKeyboard:
85 case AudioProcessing::kStereo:
86 return 2;
87 case AudioProcessing::kStereoAndKeyboard:
88 return 3;
89 }
90 assert(false);
pkasting25702cb2016-01-08 13:50:27 -080091 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +000092}
93
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000094int TruncateToMultipleOf10(int value) {
95 return (value / 10) * 10;
96}
97
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000098void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -080099 size_t samples_per_channel) {
100 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000101 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000102}
103
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000104void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800105 size_t samples_per_channel) {
106 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000107 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
108}
109
pkasting25702cb2016-01-08 13:50:27 -0800110void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
111 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000112 stereo[i * 2 + 1] = stereo[i * 2];
113 }
114}
115
pkasting25702cb2016-01-08 13:50:27 -0800116void VerifyChannelsAreEqual(int16_t* stereo, size_t samples_per_channel) {
117 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000118 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
119 }
120}
121
122void SetFrameTo(AudioFrame* frame, int16_t value) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700123 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
124 ++i) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000125 frame->data_[i] = value;
126 }
127}
128
129void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800130 ASSERT_EQ(2u, frame->num_channels_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700131 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000132 frame->data_[i] = left;
133 frame->data_[i + 1] = right;
134 }
135}
136
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000137void ScaleFrame(AudioFrame* frame, float scale) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
139 ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000140 frame->data_[i] = FloatS16ToS16(frame->data_[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000141 }
142}
143
andrew@webrtc.org81865342012-10-27 00:28:27 +0000144bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000145 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000146 return false;
147 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000148 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000149 return false;
150 }
151 if (memcmp(frame1.data_, frame2.data_,
152 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000153 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000154 return false;
155 }
156 return true;
157}
158
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000159void EnableAllAPComponents(AudioProcessing* ap) {
160#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
161 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
162
163 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
164 EXPECT_NOERR(ap->gain_control()->Enable(true));
165#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
166 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
167 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
168 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
169 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
170
171 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
172 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
173 EXPECT_NOERR(ap->gain_control()->Enable(true));
174#endif
175
176 EXPECT_NOERR(ap->high_pass_filter()->Enable(true));
177 EXPECT_NOERR(ap->level_estimator()->Enable(true));
178 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
179
180 EXPECT_NOERR(ap->voice_detection()->Enable(true));
181}
182
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000183// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000184template <class T>
185T AbsValue(T a) {
186 return a > 0 ? a: -a;
187}
188
189int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800190 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000191 int16_t max_data = AbsValue(frame.data_[0]);
pkasting25702cb2016-01-08 13:50:27 -0800192 for (size_t i = 1; i < length; i++) {
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000193 max_data = std::max(max_data, AbsValue(frame.data_[i]));
194 }
195
196 return max_data;
197}
198
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000199#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000200void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000201 const audioproc::Test::Statistic& reference) {
minyue7b19b082016-03-02 06:56:46 -0800202 EXPECT_NEAR(reference.instant(), test.instant, 2);
203 EXPECT_NEAR(reference.average(), test.average, 2);
204 EXPECT_NEAR(reference.maximum(), test.maximum, 3);
205 EXPECT_NEAR(reference.minimum(), test.minimum, 2);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000206}
207
208void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000209 audioproc::Test::Statistic* msg) {
210 msg->set_instant(output.instant);
211 msg->set_average(output.average);
212 msg->set_maximum(output.maximum);
213 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000214}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000215#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000216
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000217void OpenFileAndWriteMessage(const std::string filename,
218 const ::google::protobuf::MessageLite& msg) {
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +0000219#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000220 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000221 ASSERT_TRUE(file != NULL);
222
223 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000224 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800225 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000226 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000227
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000228 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000230 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000231 fclose(file);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +0000232#else
233 std::cout << "Warning: Writing new reference is only allowed on Linux!"
234 << std::endl;
235#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236}
237
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000238std::string ResourceFilePath(std::string name, int sample_rate_hz) {
239 std::ostringstream ss;
240 // Resource files are all stereo.
241 ss << name << sample_rate_hz / 1000 << "_stereo";
242 return test::ResourcePath(ss.str(), "pcm");
243}
244
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000245// Temporary filenames unique to this process. Used to be able to run these
246// tests in parallel as each process needs to be running in isolation they can't
247// have competing filenames.
248std::map<std::string, std::string> temp_filenames;
249
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000250std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000251 int input_rate,
252 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700253 int reverse_input_rate,
254 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800255 size_t num_input_channels,
256 size_t num_output_channels,
257 size_t num_reverse_input_channels,
258 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700259 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000260 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700261 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
262 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000263 if (num_output_channels == 1) {
264 ss << "mono";
265 } else if (num_output_channels == 2) {
266 ss << "stereo";
267 } else {
268 assert(false);
269 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700270 ss << output_rate / 1000;
271 if (num_reverse_output_channels == 1) {
272 ss << "_rmono";
273 } else if (num_reverse_output_channels == 2) {
274 ss << "_rstereo";
275 } else {
276 assert(false);
277 }
278 ss << reverse_output_rate / 1000;
279 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000280
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000281 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700282 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000283 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
284 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000285}
286
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000287void ClearTempFiles() {
288 for (auto& kv : temp_filenames)
289 remove(kv.second.c_str());
290}
291
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000292void OpenFileAndReadMessage(const std::string filename,
293 ::google::protobuf::MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000294 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000295 ASSERT_TRUE(file != NULL);
296 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000297 fclose(file);
298}
299
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000300// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
301// stereo) file, converts to deinterleaved float (optionally downmixing) and
302// returns the result in |cb|. Returns false if the file ended (or on error) and
303// true otherwise.
304//
305// |int_data| and |float_data| are just temporary space that must be
306// sufficiently large to hold the 10 ms chunk.
307bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
308 ChannelBuffer<float>* cb) {
309 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000310 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000311 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
312 if (read_count != frame_size) {
313 // Check that the file really ended.
314 assert(feof(file));
315 return false; // This is expected.
316 }
317
318 S16ToFloat(int_data, frame_size, float_data);
319 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000320 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000321 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000322 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000323 cb->channels());
324 }
325
326 return true;
327}
328
niklase@google.com470e71d2011-07-07 08:21:25 +0000329class ApmTest : public ::testing::Test {
330 protected:
331 ApmTest();
332 virtual void SetUp();
333 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000334
335 static void SetUpTestCase() {
336 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000337 }
338
339 static void TearDownTestCase() {
340 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000341 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000342 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000343
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000344 // Used to select between int and float interface tests.
345 enum Format {
346 kIntFormat,
347 kFloatFormat
348 };
349
350 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000351 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000352 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800353 size_t num_input_channels,
354 size_t num_output_channels,
355 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000356 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000357 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000358 void EnableAllComponents();
359 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000360 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000361 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000362 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
363 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000364 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000365 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
366 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700367 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800368 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700369 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800370 void TestChangingForwardChannels(size_t num_in_channels,
371 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700372 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800373 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700374 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000375 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
376 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000377 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000378 int ProcessStreamChooser(Format format);
379 int AnalyzeReverseStreamChooser(Format format);
380 void ProcessDebugDump(const std::string& in_filename,
381 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800382 Format format,
383 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000384 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000385
386 const std::string output_path_;
387 const std::string ref_path_;
388 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800389 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000390 AudioFrame* frame_;
391 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800392 std::unique_ptr<ChannelBuffer<float> > float_cb_;
393 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000394 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800395 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396 FILE* far_file_;
397 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000398 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000399};
400
401ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000402 : output_path_(test::OutputPath()),
403 ref_path_(test::ProjectRootPath() + "data/audio_processing/"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000404#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000405 ref_filename_(ref_path_ + "output_data_fixed.pb"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000406#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000407#if defined(WEBRTC_MAC)
408 // A different file for Mac is needed because on this platform the AEC
409 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
410 ref_filename_(ref_path_ + "output_data_mac.pb"),
411#else
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000412 ref_filename_(ref_path_ + "output_data_float.pb"),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000413#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000414#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000415 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000416 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000417 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000418 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000419 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000420 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000421 out_file_(NULL) {
422 Config config;
423 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
424 apm_.reset(AudioProcessing::Create(config));
425}
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
427void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000428 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000429
430 frame_ = new AudioFrame();
431 revframe_ = new AudioFrame();
432
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000433 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434}
435
436void ApmTest::TearDown() {
437 if (frame_) {
438 delete frame_;
439 }
440 frame_ = NULL;
441
442 if (revframe_) {
443 delete revframe_;
444 }
445 revframe_ = NULL;
446
447 if (far_file_) {
448 ASSERT_EQ(0, fclose(far_file_));
449 }
450 far_file_ = NULL;
451
452 if (near_file_) {
453 ASSERT_EQ(0, fclose(near_file_));
454 }
455 near_file_ = NULL;
456
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000457 if (out_file_) {
458 ASSERT_EQ(0, fclose(out_file_));
459 }
460 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000461}
462
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000463void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000464 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700465 ap->Initialize(
466 {{{frame_->sample_rate_hz_, frame_->num_channels_},
467 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700468 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700469 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000470}
471
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000472void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000473 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000474 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800475 size_t num_input_channels,
476 size_t num_output_channels,
477 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000478 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000479 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000482
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000483 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
484 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000485 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000486
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000487 if (far_file_) {
488 ASSERT_EQ(0, fclose(far_file_));
489 }
490 std::string filename = ResourceFilePath("far", sample_rate_hz);
491 far_file_ = fopen(filename.c_str(), "rb");
492 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
493 filename << "\n";
494
495 if (near_file_) {
496 ASSERT_EQ(0, fclose(near_file_));
497 }
498 filename = ResourceFilePath("near", sample_rate_hz);
499 near_file_ = fopen(filename.c_str(), "rb");
500 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
501 filename << "\n";
502
503 if (open_output_file) {
504 if (out_file_) {
505 ASSERT_EQ(0, fclose(out_file_));
506 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700507 filename = OutputFilePath(
508 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
509 reverse_sample_rate_hz, num_input_channels, num_output_channels,
510 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000511 out_file_ = fopen(filename.c_str(), "wb");
512 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
513 filename << "\n";
514 }
515}
516
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000517void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000518 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000519}
520
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000521bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
522 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000523 // The files always contain stereo audio.
524 size_t frame_size = frame->samples_per_channel_ * 2;
525 size_t read_count = fread(frame->data_,
526 sizeof(int16_t),
527 frame_size,
528 file);
529 if (read_count != frame_size) {
530 // Check that the file really ended.
531 EXPECT_NE(0, feof(file));
532 return false; // This is expected.
533 }
534
535 if (frame->num_channels_ == 1) {
536 MixStereoToMono(frame->data_, frame->data_,
537 frame->samples_per_channel_);
538 }
539
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000541 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000542 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000543 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000544}
545
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000546bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
547 return ReadFrame(file, frame, NULL);
548}
549
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000550// If the end of the file has been reached, rewind it and attempt to read the
551// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
553 ChannelBuffer<float>* cb) {
554 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000555 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000557 }
558}
559
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000560void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
561 ReadFrameWithRewind(file, frame, NULL);
562}
563
andrew@webrtc.org81865342012-10-27 00:28:27 +0000564void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
565 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000566 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000567 EXPECT_EQ(apm_->kNoError,
568 apm_->gain_control()->set_stream_analog_level(127));
569 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000570}
571
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000572int ApmTest::ProcessStreamChooser(Format format) {
573 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000574 return apm_->ProcessStream(frame_);
575 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000576 return apm_->ProcessStream(float_cb_->channels(),
577 frame_->samples_per_channel_,
578 frame_->sample_rate_hz_,
579 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000580 output_sample_rate_hz_,
581 LayoutFromChannels(num_output_channels_),
582 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000583}
584
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000585int ApmTest::AnalyzeReverseStreamChooser(Format format) {
586 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700587 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000588 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000589 return apm_->AnalyzeReverseStream(
590 revfloat_cb_->channels(),
591 revframe_->samples_per_channel_,
592 revframe_->sample_rate_hz_,
593 LayoutFromChannels(revframe_->num_channels_));
594}
595
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000596void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
597 int delay_min, int delay_max) {
598 // The |revframe_| and |frame_| should include the proper frame information,
599 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000600 AudioFrame tmp_frame;
601 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000602 bool causal = true;
603
604 tmp_frame.CopyFrom(*revframe_);
605 SetFrameTo(&tmp_frame, 0);
606
607 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
608 // Initialize the |frame_queue| with empty frames.
609 int frame_delay = delay_ms / 10;
610 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000611 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000612 frame->CopyFrom(tmp_frame);
613 frame_queue.push(frame);
614 frame_delay++;
615 causal = false;
616 }
617 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000618 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000619 frame->CopyFrom(tmp_frame);
620 frame_queue.push(frame);
621 frame_delay--;
622 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000623 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
624 // need enough frames with audio to have reliable estimates, but as few as
625 // possible to keep processing time down. 4.5 seconds seemed to be a good
626 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000627 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000628 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000629 frame->CopyFrom(tmp_frame);
630 // Use the near end recording, since that has more speech in it.
631 ASSERT_TRUE(ReadFrame(near_file_, frame));
632 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000633 AudioFrame* reverse_frame = frame;
634 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000635 if (!causal) {
636 reverse_frame = frame_queue.front();
637 // When we call ProcessStream() the frame is modified, so we can't use the
638 // pointer directly when things are non-causal. Use an intermediate frame
639 // and copy the data.
640 process_frame = &tmp_frame;
641 process_frame->CopyFrom(*frame);
642 }
aluebsb0319552016-03-17 20:39:53 -0700643 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000644 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
645 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
646 frame = frame_queue.front();
647 frame_queue.pop();
648 delete frame;
649
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000650 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000651 int median;
652 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000653 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000654 // Discard the first delay metrics to avoid convergence effects.
655 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000656 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
657 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000658 }
659 }
660
661 rewind(near_file_);
662 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000663 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 frame_queue.pop();
665 delete frame;
666 }
667 // Calculate expected delay estimate and acceptable regions. Further,
668 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700669 const size_t samples_per_ms =
670 std::min(static_cast<size_t>(16), frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000671 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
672 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700673 int expected_median_high = std::min(
674 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
675 delay_min),
676 delay_max);
677 int expected_median_low = std::min(
678 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
679 delay_min),
680 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000681 // Verify delay metrics.
682 int median;
683 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000684 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000685 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000686 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
687 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000688 EXPECT_GE(expected_median_high, median);
689 EXPECT_LE(expected_median_low, median);
690}
691
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000692void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000693 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000694 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000695
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000696 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000697 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000698 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000700
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000701 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 EXPECT_EQ(apm_->kNoError,
703 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000704 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000705 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000706 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000707
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708 // Other stream parameters set correctly.
709 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000710 EXPECT_EQ(apm_->kNoError,
711 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000713 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000715 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
717 EXPECT_EQ(apm_->kNoError,
718 apm_->echo_cancellation()->enable_drift_compensation(false));
719
720 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000721 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000723 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725
726 // Resets after successful ProcessStream().
727 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000729 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000731
732 // Other stream parameters set correctly.
733 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
734 EXPECT_EQ(apm_->kNoError,
735 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000736 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000737 EXPECT_EQ(apm_->kNoError,
738 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000739 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000740 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000741 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
742
743 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000744 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000745 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000746
747 // Resets after successful ProcessStream().
748 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000749 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000750 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000751 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000752 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753
754 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000755 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
756 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
757 EXPECT_EQ(apm_->kNoError,
758 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000759 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000760 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000761
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000762 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000763 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000764 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000766 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000767
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000768 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000770 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 EXPECT_EQ(apm_->kNoError,
772 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000773 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000774}
775
776TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000777 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000778}
779
780TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000782}
783
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000784TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
785 EXPECT_EQ(0, apm_->delay_offset_ms());
786 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
787 EXPECT_EQ(50, apm_->stream_delay_ms());
788}
789
790TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
791 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000792 apm_->set_delay_offset_ms(100);
793 EXPECT_EQ(100, apm_->delay_offset_ms());
794 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000795 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000796 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
797 EXPECT_EQ(200, apm_->stream_delay_ms());
798
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000799 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000800 apm_->set_delay_offset_ms(-50);
801 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000802 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
803 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000804 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
805 EXPECT_EQ(50, apm_->stream_delay_ms());
806}
807
Michael Graczyk86c6d332015-07-23 11:41:39 -0700808void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800809 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700810 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000811 frame_->num_channels_ = num_channels;
812 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700813 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000814}
815
Michael Graczyk86c6d332015-07-23 11:41:39 -0700816void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800817 size_t num_in_channels,
818 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700819 AudioProcessing::Error expected_return) {
820 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
821 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
822
823 EXPECT_EQ(expected_return,
824 apm_->ProcessStream(float_cb_->channels(), input_stream,
825 output_stream, float_cb_->channels()));
826}
827
828void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800829 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700830 AudioProcessing::Error expected_return) {
831 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700832 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
833 {output_sample_rate_hz_, apm_->num_output_channels()},
834 {frame_->sample_rate_hz_, num_rev_channels},
835 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700836
ekmeyerson60d9b332015-08-14 10:35:55 -0700837 EXPECT_EQ(
838 expected_return,
839 apm_->ProcessReverseStream(
840 float_cb_->channels(), processing_config.reverse_input_stream(),
841 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700842}
843
844TEST_F(ApmTest, ChannelsInt16Interface) {
845 // Testing number of invalid and valid channels.
846 Init(16000, 16000, 16000, 4, 4, 4, false);
847
848 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
849
Peter Kasting69558702016-01-12 16:26:35 -0800850 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700851 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000852 EXPECT_EQ(i, apm_->num_input_channels());
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000853 // We always force the number of reverse channels used for processing to 1.
Peter Kasting69558702016-01-12 16:26:35 -0800854 EXPECT_EQ(1u, apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000855 }
856}
857
Michael Graczyk86c6d332015-07-23 11:41:39 -0700858TEST_F(ApmTest, Channels) {
859 // Testing number of invalid and valid channels.
860 Init(16000, 16000, 16000, 4, 4, 4, false);
861
862 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
863 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
864
Peter Kasting69558702016-01-12 16:26:35 -0800865 for (size_t i = 1; i < 4; ++i) {
866 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700867 // Output channels much be one or match input channels.
868 if (j == 1 || i == j) {
869 TestChangingForwardChannels(i, j, kNoErr);
870 TestChangingReverseChannels(i, kNoErr);
871
872 EXPECT_EQ(i, apm_->num_input_channels());
873 EXPECT_EQ(j, apm_->num_output_channels());
874 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800875 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700876 } else {
877 TestChangingForwardChannels(i, j,
878 AudioProcessing::kBadNumberChannelsError);
879 }
880 }
881 }
882}
883
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000884TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000885 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000886 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000887 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000888 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700889 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800890 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000891 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000892 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000893 }
894}
895
niklase@google.com470e71d2011-07-07 08:21:25 +0000896TEST_F(ApmTest, EchoCancellation) {
897 EXPECT_EQ(apm_->kNoError,
898 apm_->echo_cancellation()->enable_drift_compensation(true));
899 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
900 EXPECT_EQ(apm_->kNoError,
901 apm_->echo_cancellation()->enable_drift_compensation(false));
902 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
903
niklase@google.com470e71d2011-07-07 08:21:25 +0000904 EchoCancellation::SuppressionLevel level[] = {
905 EchoCancellation::kLowSuppression,
906 EchoCancellation::kModerateSuppression,
907 EchoCancellation::kHighSuppression,
908 };
pkasting25702cb2016-01-08 13:50:27 -0800909 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000910 EXPECT_EQ(apm_->kNoError,
911 apm_->echo_cancellation()->set_suppression_level(level[i]));
912 EXPECT_EQ(level[i],
913 apm_->echo_cancellation()->suppression_level());
914 }
915
916 EchoCancellation::Metrics metrics;
917 EXPECT_EQ(apm_->kNotEnabledError,
918 apm_->echo_cancellation()->GetMetrics(&metrics));
919
920 EXPECT_EQ(apm_->kNoError,
921 apm_->echo_cancellation()->enable_metrics(true));
922 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
923 EXPECT_EQ(apm_->kNoError,
924 apm_->echo_cancellation()->enable_metrics(false));
925 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
926
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000927 int median = 0;
928 int std = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000929 float poor_fraction = 0;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000930 EXPECT_EQ(apm_->kNotEnabledError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000931 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
932 &poor_fraction));
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000933
934 EXPECT_EQ(apm_->kNoError,
935 apm_->echo_cancellation()->enable_delay_logging(true));
936 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
937 EXPECT_EQ(apm_->kNoError,
938 apm_->echo_cancellation()->enable_delay_logging(false));
939 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
940
niklase@google.com470e71d2011-07-07 08:21:25 +0000941 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
942 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
943 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
944 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000945
946 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
947 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
948 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
949 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
950 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
951 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000952}
953
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000954TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000955 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000956 // Enable AEC only.
957 EXPECT_EQ(apm_->kNoError,
958 apm_->echo_cancellation()->enable_drift_compensation(false));
959 EXPECT_EQ(apm_->kNoError,
960 apm_->echo_cancellation()->enable_metrics(false));
961 EXPECT_EQ(apm_->kNoError,
962 apm_->echo_cancellation()->enable_delay_logging(true));
963 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000964 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700965 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000966 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000967
968 // Internally in the AEC the amount of lookahead the delay estimation can
969 // handle is 15 blocks and the maximum delay is set to 60 blocks.
970 const int kLookaheadBlocks = 15;
971 const int kMaxDelayBlocks = 60;
972 // The AEC has a startup time before it actually starts to process. This
973 // procedure can flush the internal far-end buffer, which of course affects
974 // the delay estimation. Therefore, we set a system_delay high enough to
975 // avoid that. The smallest system_delay you can report without flushing the
976 // buffer is 66 ms in 8 kHz.
977 //
978 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
979 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
980 // delay estimation. This should be noted though. In case of test failure,
981 // this could be the cause.
982 const int kSystemDelayMs = 66;
983 // Test a couple of corner cases and verify that the estimated delay is
984 // within a valid region (set to +-1.5 blocks). Note that these cases are
985 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800986 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000987 Init(kProcessSampleRates[i],
988 kProcessSampleRates[i],
989 kProcessSampleRates[i],
990 2,
991 2,
992 2,
993 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000994 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700995 const int num_ms_per_block =
996 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000997 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
998 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
999
1000 // 1) Verify correct delay estimate at lookahead boundary.
1001 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1002 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1003 delay_max_ms);
1004 // 2) A delay less than maximum lookahead should give an delay estimate at
1005 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1006 delay_ms -= 20;
1007 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1008 delay_max_ms);
1009 // 3) Three values around zero delay. Note that we need to compensate for
1010 // the fake system_delay.
1011 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1012 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1013 delay_max_ms);
1014 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1015 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1016 delay_max_ms);
1017 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1018 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1019 delay_max_ms);
1020 // 4) Verify correct delay estimate at maximum delay boundary.
1021 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1022 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1023 delay_max_ms);
1024 // 5) A delay above the maximum delay should give an estimate at the
1025 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1026 delay_ms += 20;
1027 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1028 delay_max_ms);
1029 }
1030}
1031
niklase@google.com470e71d2011-07-07 08:21:25 +00001032TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001033 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001034 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001035 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1036 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1037
niklase@google.com470e71d2011-07-07 08:21:25 +00001038 // Toggle routing modes
1039 EchoControlMobile::RoutingMode mode[] = {
1040 EchoControlMobile::kQuietEarpieceOrHeadset,
1041 EchoControlMobile::kEarpiece,
1042 EchoControlMobile::kLoudEarpiece,
1043 EchoControlMobile::kSpeakerphone,
1044 EchoControlMobile::kLoudSpeakerphone,
1045 };
pkasting25702cb2016-01-08 13:50:27 -08001046 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001047 EXPECT_EQ(apm_->kNoError,
1048 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1049 EXPECT_EQ(mode[i],
1050 apm_->echo_control_mobile()->routing_mode());
1051 }
1052 // Turn comfort noise off/on
1053 EXPECT_EQ(apm_->kNoError,
1054 apm_->echo_control_mobile()->enable_comfort_noise(false));
1055 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1056 EXPECT_EQ(apm_->kNoError,
1057 apm_->echo_control_mobile()->enable_comfort_noise(true));
1058 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001059 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001060 const size_t echo_path_size =
1061 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001062 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1063 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001064 EXPECT_EQ(apm_->kNullPointerError,
1065 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1066 EXPECT_EQ(apm_->kNullPointerError,
1067 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1068 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001069 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001070 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001071 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001072 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001073 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001074 echo_path_in[i] = echo_path_out[i] + 1;
1075 }
1076 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001077 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001078 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001079 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1080 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001081 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001082 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1083 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001084 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001085 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1086 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001087
1088 // Process a few frames with NS in the default disabled state. This exercises
1089 // a different codepath than with it enabled.
1090 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1091 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1092 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1093 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1094
niklase@google.com470e71d2011-07-07 08:21:25 +00001095 // Turn AECM off
1096 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1097 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1098}
1099
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001100TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001101 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001102 EXPECT_EQ(apm_->kNoError,
1103 apm_->gain_control()->set_mode(
1104 apm_->gain_control()->mode()));
1105
1106 GainControl::Mode mode[] = {
1107 GainControl::kAdaptiveAnalog,
1108 GainControl::kAdaptiveDigital,
1109 GainControl::kFixedDigital
1110 };
pkasting25702cb2016-01-08 13:50:27 -08001111 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001112 EXPECT_EQ(apm_->kNoError,
1113 apm_->gain_control()->set_mode(mode[i]));
1114 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1115 }
1116 // Testing invalid target levels
1117 EXPECT_EQ(apm_->kBadParameterError,
1118 apm_->gain_control()->set_target_level_dbfs(-3));
1119 EXPECT_EQ(apm_->kBadParameterError,
1120 apm_->gain_control()->set_target_level_dbfs(-40));
1121 // Testing valid target levels
1122 EXPECT_EQ(apm_->kNoError,
1123 apm_->gain_control()->set_target_level_dbfs(
1124 apm_->gain_control()->target_level_dbfs()));
1125
1126 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001127 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001128 EXPECT_EQ(apm_->kNoError,
1129 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1130 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1131 }
1132
1133 // Testing invalid compression gains
1134 EXPECT_EQ(apm_->kBadParameterError,
1135 apm_->gain_control()->set_compression_gain_db(-1));
1136 EXPECT_EQ(apm_->kBadParameterError,
1137 apm_->gain_control()->set_compression_gain_db(100));
1138
1139 // Testing valid compression gains
1140 EXPECT_EQ(apm_->kNoError,
1141 apm_->gain_control()->set_compression_gain_db(
1142 apm_->gain_control()->compression_gain_db()));
1143
1144 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001145 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001146 EXPECT_EQ(apm_->kNoError,
1147 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1148 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1149 }
1150
1151 // Testing limiter off/on
1152 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1153 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1154 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1155 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1156
1157 // Testing invalid level limits
1158 EXPECT_EQ(apm_->kBadParameterError,
1159 apm_->gain_control()->set_analog_level_limits(-1, 512));
1160 EXPECT_EQ(apm_->kBadParameterError,
1161 apm_->gain_control()->set_analog_level_limits(100000, 512));
1162 EXPECT_EQ(apm_->kBadParameterError,
1163 apm_->gain_control()->set_analog_level_limits(512, -1));
1164 EXPECT_EQ(apm_->kBadParameterError,
1165 apm_->gain_control()->set_analog_level_limits(512, 100000));
1166 EXPECT_EQ(apm_->kBadParameterError,
1167 apm_->gain_control()->set_analog_level_limits(512, 255));
1168
1169 // Testing valid level limits
1170 EXPECT_EQ(apm_->kNoError,
1171 apm_->gain_control()->set_analog_level_limits(
1172 apm_->gain_control()->analog_level_minimum(),
1173 apm_->gain_control()->analog_level_maximum()));
1174
1175 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001176 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 EXPECT_EQ(apm_->kNoError,
1178 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1179 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1180 }
1181
1182 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001183 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001184 EXPECT_EQ(apm_->kNoError,
1185 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1186 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1187 }
1188
1189 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1190
1191 // Turn AGC off
1192 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1193 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1194}
1195
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001196void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001197 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001198 EXPECT_EQ(apm_->kNoError,
1199 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1200 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1201
1202 int out_analog_level = 0;
1203 for (int i = 0; i < 2000; ++i) {
1204 ReadFrameWithRewind(near_file_, frame_);
1205 // Ensure the audio is at a low level, so the AGC will try to increase it.
1206 ScaleFrame(frame_, 0.25);
1207
1208 // Always pass in the same volume.
1209 EXPECT_EQ(apm_->kNoError,
1210 apm_->gain_control()->set_stream_analog_level(100));
1211 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1212 out_analog_level = apm_->gain_control()->stream_analog_level();
1213 }
1214
1215 // Ensure the AGC is still able to reach the maximum.
1216 EXPECT_EQ(255, out_analog_level);
1217}
1218
1219// Verifies that despite volume slider quantization, the AGC can continue to
1220// increase its volume.
1221TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001222 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001223 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1224 }
1225}
1226
1227void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001228 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001229 EXPECT_EQ(apm_->kNoError,
1230 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1231 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1232
1233 int out_analog_level = 100;
1234 for (int i = 0; i < 1000; ++i) {
1235 ReadFrameWithRewind(near_file_, frame_);
1236 // Ensure the audio is at a low level, so the AGC will try to increase it.
1237 ScaleFrame(frame_, 0.25);
1238
1239 EXPECT_EQ(apm_->kNoError,
1240 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1241 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1242 out_analog_level = apm_->gain_control()->stream_analog_level();
1243 }
1244
1245 // Ensure the volume was raised.
1246 EXPECT_GT(out_analog_level, 100);
1247 int highest_level_reached = out_analog_level;
1248 // Simulate a user manual volume change.
1249 out_analog_level = 100;
1250
1251 for (int i = 0; i < 300; ++i) {
1252 ReadFrameWithRewind(near_file_, frame_);
1253 ScaleFrame(frame_, 0.25);
1254
1255 EXPECT_EQ(apm_->kNoError,
1256 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1257 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1258 out_analog_level = apm_->gain_control()->stream_analog_level();
1259 // Check that AGC respected the manually adjusted volume.
1260 EXPECT_LT(out_analog_level, highest_level_reached);
1261 }
1262 // Check that the volume was still raised.
1263 EXPECT_GT(out_analog_level, 100);
1264}
1265
1266TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001267 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001268 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1269 }
1270}
1271
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001272#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1273TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1274 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001275 const size_t kSamplesPerChannel =
1276 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001277 const size_t kNumInputChannels = 2;
1278 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001279 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001280 const float kScaleFactor = 0.25f;
1281 Config config;
1282 std::vector<webrtc::Point> geometry;
1283 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1284 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1285 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001286 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
1287 new testing::NiceMock<MockNonlinearBeamformer>(geometry);
kwiberg62eaacf2016-02-17 06:39:05 -08001288 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001289 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001290 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1291 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1292 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001293 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1294 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001295 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1296 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001297 std::string filename = ResourceFilePath("far", kSampleRateHz);
1298 FILE* far_file = fopen(filename.c_str(), "rb");
1299 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1300 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1301 const int kDefaultCompressionGain =
1302 apm->gain_control()->compression_gain_db();
1303 bool is_target = false;
1304 EXPECT_CALL(*beamformer, is_target_present())
1305 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001306 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001307 ASSERT_TRUE(ReadChunk(far_file,
1308 int_data.get(),
1309 float_data.get(),
1310 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001311 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001312 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001313 src_buf.channels()[j][k] *= kScaleFactor;
1314 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001315 }
1316 EXPECT_EQ(kNoErr,
1317 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001318 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001319 kSampleRateHz,
1320 LayoutFromChannels(src_buf.num_channels()),
1321 kSampleRateHz,
1322 LayoutFromChannels(dest_buf.num_channels()),
1323 dest_buf.channels()));
1324 }
1325 EXPECT_EQ(kDefaultVolume,
1326 apm->gain_control()->stream_analog_level());
1327 EXPECT_EQ(kDefaultCompressionGain,
1328 apm->gain_control()->compression_gain_db());
1329 rewind(far_file);
1330 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001331 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001332 ASSERT_TRUE(ReadChunk(far_file,
1333 int_data.get(),
1334 float_data.get(),
1335 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001336 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001337 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001338 src_buf.channels()[j][k] *= kScaleFactor;
1339 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001340 }
1341 EXPECT_EQ(kNoErr,
1342 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001343 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001344 kSampleRateHz,
1345 LayoutFromChannels(src_buf.num_channels()),
1346 kSampleRateHz,
1347 LayoutFromChannels(dest_buf.num_channels()),
1348 dest_buf.channels()));
1349 }
1350 EXPECT_LT(kDefaultVolume,
1351 apm->gain_control()->stream_analog_level());
1352 EXPECT_LT(kDefaultCompressionGain,
1353 apm->gain_control()->compression_gain_db());
1354 ASSERT_EQ(0, fclose(far_file));
1355}
1356#endif
1357
niklase@google.com470e71d2011-07-07 08:21:25 +00001358TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001359 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001360 NoiseSuppression::Level level[] = {
1361 NoiseSuppression::kLow,
1362 NoiseSuppression::kModerate,
1363 NoiseSuppression::kHigh,
1364 NoiseSuppression::kVeryHigh
1365 };
pkasting25702cb2016-01-08 13:50:27 -08001366 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001367 EXPECT_EQ(apm_->kNoError,
1368 apm_->noise_suppression()->set_level(level[i]));
1369 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1370 }
1371
andrew@webrtc.org648af742012-02-08 01:57:29 +00001372 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1374 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1375 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1376 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1377}
1378
1379TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001380 // Turn HP filter on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001381 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(true));
1382 EXPECT_TRUE(apm_->high_pass_filter()->is_enabled());
1383 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(false));
1384 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1385}
1386
1387TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001388 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001389 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001390 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001391
1392 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1393
1394 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1395 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1396
1397 // Run this test in wideband; in super-wb, the splitting filter distorts the
1398 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001399 frame_->samples_per_channel_ = 160;
1400 frame_->num_channels_ = 2;
1401 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001402
1403 // Min value if no frames have been processed.
1404 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1405
1406 // Min value on zero frames.
1407 SetFrameTo(frame_, 0);
1408 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1409 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1410 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1411
1412 // Try a few RMS values.
1413 // (These also test that the value resets after retrieving it.)
1414 SetFrameTo(frame_, 32767);
1415 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1416 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1417 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1418
1419 SetFrameTo(frame_, 30000);
1420 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1421 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1422 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1423
1424 SetFrameTo(frame_, 10000);
1425 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1426 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1427 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1428
1429 SetFrameTo(frame_, 10);
1430 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1431 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1432 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1433
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001434 // Verify reset after enable/disable.
1435 SetFrameTo(frame_, 32767);
1436 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1437 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1438 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1439 SetFrameTo(frame_, 1);
1440 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1441 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1442
1443 // Verify reset after initialize.
1444 SetFrameTo(frame_, 32767);
1445 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1446 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1447 SetFrameTo(frame_, 1);
1448 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1449 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001450}
1451
1452TEST_F(ApmTest, VoiceDetection) {
1453 // Test external VAD
1454 EXPECT_EQ(apm_->kNoError,
1455 apm_->voice_detection()->set_stream_has_voice(true));
1456 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1457 EXPECT_EQ(apm_->kNoError,
1458 apm_->voice_detection()->set_stream_has_voice(false));
1459 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1460
andrew@webrtc.org648af742012-02-08 01:57:29 +00001461 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001462 VoiceDetection::Likelihood likelihood[] = {
1463 VoiceDetection::kVeryLowLikelihood,
1464 VoiceDetection::kLowLikelihood,
1465 VoiceDetection::kModerateLikelihood,
1466 VoiceDetection::kHighLikelihood
1467 };
pkasting25702cb2016-01-08 13:50:27 -08001468 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001469 EXPECT_EQ(apm_->kNoError,
1470 apm_->voice_detection()->set_likelihood(likelihood[i]));
1471 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1472 }
1473
1474 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001475 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001476 EXPECT_EQ(apm_->kBadParameterError,
1477 apm_->voice_detection()->set_frame_size_ms(12));
1478
andrew@webrtc.org648af742012-02-08 01:57:29 +00001479 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001480 for (int i = 10; i <= 30; i += 10) {
1481 EXPECT_EQ(apm_->kNoError,
1482 apm_->voice_detection()->set_frame_size_ms(i));
1483 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1484 }
1485 */
1486
andrew@webrtc.org648af742012-02-08 01:57:29 +00001487 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001488 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1489 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1490 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1491 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1492
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001493 // Test that AudioFrame activity is maintained when VAD is disabled.
1494 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1495 AudioFrame::VADActivity activity[] = {
1496 AudioFrame::kVadActive,
1497 AudioFrame::kVadPassive,
1498 AudioFrame::kVadUnknown
1499 };
pkasting25702cb2016-01-08 13:50:27 -08001500 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001501 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001502 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001503 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001504 }
1505
1506 // Test that AudioFrame activity is set when VAD is enabled.
1507 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001508 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001509 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001510 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001511
niklase@google.com470e71d2011-07-07 08:21:25 +00001512 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1513}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001514
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001515TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1516 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1517 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1518 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1519 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1520 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1521 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1522 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1523}
1524
1525TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001526 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001527 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001528 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001529 AudioFrame frame_copy;
1530 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001531 for (int j = 0; j < 1000; j++) {
1532 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1533 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001534 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1535 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001536 }
1537 }
1538}
1539
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001540TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1541 // Test that ProcessStream copies input to output even with no processing.
1542 const size_t kSamples = 80;
1543 const int sample_rate = 8000;
1544 const float src[kSamples] = {
1545 -1.0f, 0.0f, 1.0f
1546 };
1547 float dest[kSamples] = {};
1548
1549 auto src_channels = &src[0];
1550 auto dest_channels = &dest[0];
1551
1552 apm_.reset(AudioProcessing::Create());
1553 EXPECT_NOERR(apm_->ProcessStream(
1554 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1555 sample_rate, LayoutFromChannels(1), &dest_channels));
1556
1557 for (size_t i = 0; i < kSamples; ++i) {
1558 EXPECT_EQ(src[i], dest[i]);
1559 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001560
1561 // Same for ProcessReverseStream.
1562 float rev_dest[kSamples] = {};
1563 auto rev_dest_channels = &rev_dest[0];
1564
1565 StreamConfig input_stream = {sample_rate, 1};
1566 StreamConfig output_stream = {sample_rate, 1};
1567 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1568 output_stream, &rev_dest_channels));
1569
1570 for (size_t i = 0; i < kSamples; ++i) {
1571 EXPECT_EQ(src[i], rev_dest[i]);
1572 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001573}
1574
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001575TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1576 EnableAllComponents();
1577
pkasting25702cb2016-01-08 13:50:27 -08001578 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001579 Init(kProcessSampleRates[i],
1580 kProcessSampleRates[i],
1581 kProcessSampleRates[i],
1582 2,
1583 2,
1584 2,
1585 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001586 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001587 ASSERT_EQ(0, feof(far_file_));
1588 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001589 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001590 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1591
aluebsb0319552016-03-17 20:39:53 -07001592 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001593
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001594 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1595 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1596
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001597 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001598 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001599 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001600 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001601 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001602 analog_level = apm_->gain_control()->stream_analog_level();
1603
1604 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1605 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001606 rewind(far_file_);
1607 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001608 }
1609}
1610
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001611TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001612 // Verify the filter is not active through undistorted audio when:
1613 // 1. No components are enabled...
1614 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001615 AudioFrame frame_copy;
1616 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001617 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1618 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1619 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1620
1621 // 2. Only the level estimator is enabled...
1622 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001623 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001624 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1625 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1626 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1627 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1628 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1629
1630 // 3. Only VAD is enabled...
1631 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001632 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001633 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1634 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1635 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1636 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1637 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1638
1639 // 4. Both VAD and the level estimator are enabled...
1640 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001641 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001642 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1643 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1644 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1645 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1646 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1647 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1648 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1649
1650 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001651 frame_->samples_per_channel_ = 160;
1652 frame_->num_channels_ = 2;
1653 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001654 // Enable AEC, which would require the filter in super-wb. We rely on the
1655 // first few frames of data being unaffected by the AEC.
1656 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1657 // behavior of the AEC. Think of something more robust.
1658 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001659 // Make sure we have extended filter enabled. This makes sure nothing is
1660 // touched until we have a farend frame.
1661 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001662 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001663 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001664 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001665 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001666 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001667 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001668 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1669 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001670 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001671 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1672 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1673
1674 // Check the test is valid. We should have distortion from the filter
1675 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001676 frame_->samples_per_channel_ = 320;
1677 frame_->num_channels_ = 2;
1678 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001679 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001680 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001681 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001682 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001683 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1684 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1685}
1686
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001687#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1688void ApmTest::ProcessDebugDump(const std::string& in_filename,
1689 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001690 Format format,
1691 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001692 FILE* in_file = fopen(in_filename.c_str(), "rb");
1693 ASSERT_TRUE(in_file != NULL);
1694 audioproc::Event event_msg;
1695 bool first_init = true;
1696
1697 while (ReadMessageFromFile(in_file, &event_msg)) {
1698 if (event_msg.type() == audioproc::Event::INIT) {
1699 const audioproc::Init msg = event_msg.init();
1700 int reverse_sample_rate = msg.sample_rate();
1701 if (msg.has_reverse_sample_rate()) {
1702 reverse_sample_rate = msg.reverse_sample_rate();
1703 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001704 int output_sample_rate = msg.sample_rate();
1705 if (msg.has_output_sample_rate()) {
1706 output_sample_rate = msg.output_sample_rate();
1707 }
1708
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001709 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001710 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001711 reverse_sample_rate,
1712 msg.num_input_channels(),
1713 msg.num_output_channels(),
1714 msg.num_reverse_channels(),
1715 false);
1716 if (first_init) {
1717 // StartDebugRecording() writes an additional init message. Don't start
1718 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001719 EXPECT_NOERR(
1720 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001721 first_init = false;
1722 }
1723
1724 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1725 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1726
1727 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001728 ASSERT_EQ(revframe_->num_channels_,
1729 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001730 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001731 memcpy(revfloat_cb_->channels()[i],
1732 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001733 msg.channel(i).size());
1734 }
1735 } else {
1736 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1737 if (format == kFloatFormat) {
1738 // We're using an int16 input file; convert to float.
1739 ConvertToFloat(*revframe_, revfloat_cb_.get());
1740 }
1741 }
1742 AnalyzeReverseStreamChooser(format);
1743
1744 } else if (event_msg.type() == audioproc::Event::STREAM) {
1745 const audioproc::Stream msg = event_msg.stream();
1746 // ProcessStream could have changed this for the output frame.
1747 frame_->num_channels_ = apm_->num_input_channels();
1748
1749 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1750 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1751 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1752 if (msg.has_keypress()) {
1753 apm_->set_stream_key_pressed(msg.keypress());
1754 } else {
1755 apm_->set_stream_key_pressed(true);
1756 }
1757
1758 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001759 ASSERT_EQ(frame_->num_channels_,
1760 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001761 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001762 memcpy(float_cb_->channels()[i],
1763 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001764 msg.input_channel(i).size());
1765 }
1766 } else {
1767 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1768 if (format == kFloatFormat) {
1769 // We're using an int16 input file; convert to float.
1770 ConvertToFloat(*frame_, float_cb_.get());
1771 }
1772 }
1773 ProcessStreamChooser(format);
1774 }
1775 }
1776 EXPECT_NOERR(apm_->StopDebugRecording());
1777 fclose(in_file);
1778}
1779
1780void ApmTest::VerifyDebugDumpTest(Format format) {
1781 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001782 std::string format_string;
1783 switch (format) {
1784 case kIntFormat:
1785 format_string = "_int";
1786 break;
1787 case kFloatFormat:
1788 format_string = "_float";
1789 break;
1790 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001791 const std::string ref_filename = test::TempFilename(
1792 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1793 const std::string out_filename = test::TempFilename(
1794 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001795 const std::string limited_filename = test::TempFilename(
1796 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1797 const size_t logging_limit_bytes = 100000;
1798 // We expect at least this many bytes in the created logfile.
1799 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001800 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001801 ProcessDebugDump(in_filename, ref_filename, format, -1);
1802 ProcessDebugDump(ref_filename, out_filename, format, -1);
1803 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001804
1805 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1806 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001807 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001808 ASSERT_TRUE(ref_file != NULL);
1809 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001810 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001811 std::unique_ptr<uint8_t[]> ref_bytes;
1812 std::unique_ptr<uint8_t[]> out_bytes;
1813 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001814
1815 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1816 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001817 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001818 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001819 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001820 while (ref_size > 0 && out_size > 0) {
1821 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001822 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001823 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001824 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001825 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001826 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001827 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1828 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001829 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001830 }
1831 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001832 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1833 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001834 EXPECT_NE(0, feof(ref_file));
1835 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001836 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001837 ASSERT_EQ(0, fclose(ref_file));
1838 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001839 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001840 remove(ref_filename.c_str());
1841 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001842 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001843}
1844
1845TEST_F(ApmTest, VerifyDebugDumpInt) {
1846 VerifyDebugDumpTest(kIntFormat);
1847}
1848
1849TEST_F(ApmTest, VerifyDebugDumpFloat) {
1850 VerifyDebugDumpTest(kFloatFormat);
1851}
1852#endif
1853
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001854// TODO(andrew): expand test to verify output.
1855TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001856 const std::string filename =
1857 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001858 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001859 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001860
1861#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1862 // Stopping without having started should be OK.
1863 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1864
ivocd66b44d2016-01-15 03:06:36 -08001865 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001866 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001867 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001868 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1869
1870 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001871 FILE* fid = fopen(filename.c_str(), "r");
1872 ASSERT_TRUE(fid != NULL);
1873
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001874 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001875 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001876 ASSERT_EQ(0, remove(filename.c_str()));
1877#else
1878 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001879 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001880 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1881
1882 // Verify the file has NOT been written.
1883 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1884#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1885}
1886
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001887// TODO(andrew): expand test to verify output.
1888TEST_F(ApmTest, DebugDumpFromFileHandle) {
1889 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001890 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001891 const std::string filename =
1892 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001893 fid = fopen(filename.c_str(), "w");
1894 ASSERT_TRUE(fid);
1895
1896#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1897 // Stopping without having started should be OK.
1898 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1899
ivocd66b44d2016-01-15 03:06:36 -08001900 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001901 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001902 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1903 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1904
1905 // Verify the file has been written.
1906 fid = fopen(filename.c_str(), "r");
1907 ASSERT_TRUE(fid != NULL);
1908
1909 // Clean it up.
1910 ASSERT_EQ(0, fclose(fid));
1911 ASSERT_EQ(0, remove(filename.c_str()));
1912#else
1913 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001914 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001915 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1916
1917 ASSERT_EQ(0, fclose(fid));
1918#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1919}
1920
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001921TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001922 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001923 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001924
1925 Config config;
1926 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001927 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001928 EnableAllComponents();
1929 EnableAllAPComponents(fapm.get());
1930 for (int i = 0; i < ref_data.test_size(); i++) {
1931 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1932
1933 audioproc::Test* test = ref_data.mutable_test(i);
1934 // TODO(ajm): Restore downmixing test cases.
1935 if (test->num_input_channels() != test->num_output_channels())
1936 continue;
1937
Peter Kasting69558702016-01-12 16:26:35 -08001938 const size_t num_render_channels =
1939 static_cast<size_t>(test->num_reverse_channels());
1940 const size_t num_input_channels =
1941 static_cast<size_t>(test->num_input_channels());
1942 const size_t num_output_channels =
1943 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001944 const size_t samples_per_channel = static_cast<size_t>(
1945 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001946
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001947 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1948 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001949 Init(fapm.get());
1950
1951 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001952 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1953 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001954
1955 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001956 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001957 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1958 ReadFrame(near_file_, frame_, float_cb_.get())) {
1959 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1960
aluebsb0319552016-03-17 20:39:53 -07001961 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001962 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1963 revfloat_cb_->channels(),
1964 samples_per_channel,
1965 test->sample_rate(),
1966 LayoutFromChannels(num_render_channels)));
1967
1968 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1969 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1970 apm_->echo_cancellation()->set_stream_drift_samples(0);
1971 fapm->echo_cancellation()->set_stream_drift_samples(0);
1972 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1973 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1974
1975 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001976 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
1977 output_int16.channels());
1978
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001979 EXPECT_NOERR(fapm->ProcessStream(
1980 float_cb_->channels(),
1981 samples_per_channel,
1982 test->sample_rate(),
1983 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001984 test->sample_rate(),
1985 LayoutFromChannels(num_output_channels),
1986 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001987 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001988 FloatToS16(float_cb_->channels()[j],
1989 samples_per_channel,
1990 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001991 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001992 float snr = ComputeSNR(output_int16.channels()[j],
1993 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001994 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001995
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001996 const float kVarianceThreshold = 20;
1997 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001998
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001999 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002000 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2001 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002002 }
2003 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002004
2005 analog_level = fapm->gain_control()->stream_analog_level();
2006 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2007 fapm->gain_control()->stream_analog_level());
2008 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2009 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002010 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2011 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002012 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002013
2014 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002015 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002016 }
aluebs776593b2016-03-15 14:04:58 -07002017
2018#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2019 const size_t kMaxNumBadChunks = 0;
2020#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2021 // There are a few chunks in the fixed-point profile that give low SNR.
2022 // Listening confirmed the difference is acceptable.
2023 const size_t kMaxNumBadChunks = 60;
2024#endif
2025 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2026
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002027 rewind(far_file_);
2028 rewind(near_file_);
2029 }
2030}
2031
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002032// TODO(andrew): Add a test to process a few frames with different combinations
2033// of enabled components.
2034
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002035TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002036 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002037 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002038
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002039 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002040 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002041 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002042 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002043 for (size_t i = 0; i < arraysize(kChannels); i++) {
2044 for (size_t j = 0; j < arraysize(kChannels); j++) {
2045 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002046 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002047 test->set_num_reverse_channels(kChannels[i]);
2048 test->set_num_input_channels(kChannels[j]);
2049 test->set_num_output_channels(kChannels[j]);
2050 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002051 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002052 }
2053 }
2054 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002055#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2056 // To test the extended filter mode.
2057 audioproc::Test* test = ref_data.add_test();
2058 test->set_num_reverse_channels(2);
2059 test->set_num_input_channels(2);
2060 test->set_num_output_channels(2);
2061 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2062 test->set_use_aec_extended_filter(true);
2063#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002064 }
2065
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002066 for (int i = 0; i < ref_data.test_size(); i++) {
2067 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002068
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002069 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002070 // TODO(ajm): We no longer allow different input and output channels. Skip
2071 // these tests for now, but they should be removed from the set.
2072 if (test->num_input_channels() != test->num_output_channels())
2073 continue;
2074
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002075 Config config;
2076 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002077 config.Set<ExtendedFilter>(
2078 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002079 apm_.reset(AudioProcessing::Create(config));
2080
2081 EnableAllComponents();
2082
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002083 Init(test->sample_rate(),
2084 test->sample_rate(),
2085 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002086 static_cast<size_t>(test->num_input_channels()),
2087 static_cast<size_t>(test->num_output_channels()),
2088 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002089 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002090
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002091 int frame_count = 0;
2092 int has_echo_count = 0;
2093 int has_voice_count = 0;
2094 int is_saturated_count = 0;
2095 int analog_level = 127;
2096 int analog_level_average = 0;
2097 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002098 float ns_speech_prob_average = 0.0f;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002099
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002100 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002101 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002102
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002103 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2104
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002105 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002106 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002107 EXPECT_EQ(apm_->kNoError,
2108 apm_->gain_control()->set_stream_analog_level(analog_level));
2109
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002110 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002111
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002112 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002113 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2114 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002115
2116 max_output_average += MaxAudioFrame(*frame_);
2117
2118 if (apm_->echo_cancellation()->stream_has_echo()) {
2119 has_echo_count++;
2120 }
2121
2122 analog_level = apm_->gain_control()->stream_analog_level();
2123 analog_level_average += analog_level;
2124 if (apm_->gain_control()->stream_is_saturated()) {
2125 is_saturated_count++;
2126 }
2127 if (apm_->voice_detection()->stream_has_voice()) {
2128 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002129 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002130 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002131 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002132 }
2133
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002134 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2135
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002136 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002137 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002138 sizeof(int16_t),
2139 frame_size,
2140 out_file_);
2141 ASSERT_EQ(frame_size, write_count);
2142
2143 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002144 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002145 frame_count++;
2146 }
2147 max_output_average /= frame_count;
2148 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002149 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002150
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002151#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002152 EchoCancellation::Metrics echo_metrics;
2153 EXPECT_EQ(apm_->kNoError,
2154 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2155 int median = 0;
2156 int std = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002157 float fraction_poor_delays = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002158 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002159 apm_->echo_cancellation()->GetDelayMetrics(
2160 &median, &std, &fraction_poor_delays));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002161
2162 int rms_level = apm_->level_estimator()->RMS();
2163 EXPECT_LE(0, rms_level);
2164 EXPECT_GE(127, rms_level);
2165#endif
2166
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002167 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002168 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002169 // When running the test on a N7 we get a {2, 6} difference of
2170 // |has_voice_count| and |max_output_average| is up to 18 higher.
2171 // All numbers being consistently higher on N7 compare to ref_data.
2172 // TODO(bjornv): If we start getting more of these offsets on Android we
2173 // should consider a different approach. Either using one slack for all,
2174 // or generate a separate android reference.
2175#if defined(WEBRTC_ANDROID)
2176 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002177 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002178 const int kMaxOutputAverageOffset = 9;
2179 const int kMaxOutputAverageNear = 9;
2180#else
2181 const int kHasVoiceCountOffset = 0;
2182 const int kHasVoiceCountNear = kIntNear;
2183 const int kMaxOutputAverageOffset = 0;
2184 const int kMaxOutputAverageNear = kIntNear;
2185#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002186 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002187 EXPECT_NEAR(test->has_voice_count(),
2188 has_voice_count - kHasVoiceCountOffset,
2189 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002190 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002191
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002192 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002193 EXPECT_NEAR(test->max_output_average(),
2194 max_output_average - kMaxOutputAverageOffset,
2195 kMaxOutputAverageNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002196
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002197#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002198 audioproc::Test::EchoMetrics reference = test->echo_metrics();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002199 TestStats(echo_metrics.residual_echo_return_loss,
2200 reference.residual_echo_return_loss());
2201 TestStats(echo_metrics.echo_return_loss,
2202 reference.echo_return_loss());
2203 TestStats(echo_metrics.echo_return_loss_enhancement,
2204 reference.echo_return_loss_enhancement());
2205 TestStats(echo_metrics.a_nlp,
2206 reference.a_nlp());
2207
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002208 const double kFloatNear = 0.0005;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002209 audioproc::Test::DelayMetrics reference_delay = test->delay_metrics();
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002210 EXPECT_NEAR(reference_delay.median(), median, kIntNear);
2211 EXPECT_NEAR(reference_delay.std(), std, kIntNear);
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002212 EXPECT_NEAR(reference_delay.fraction_poor_delays(), fraction_poor_delays,
2213 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002214
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002215 EXPECT_NEAR(test->rms_level(), rms_level, kIntNear);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002216
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002217 EXPECT_NEAR(test->ns_speech_probability_average(),
2218 ns_speech_prob_average,
2219 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002220#endif
2221 } else {
2222 test->set_has_echo_count(has_echo_count);
2223 test->set_has_voice_count(has_voice_count);
2224 test->set_is_saturated_count(is_saturated_count);
2225
2226 test->set_analog_level_average(analog_level_average);
2227 test->set_max_output_average(max_output_average);
2228
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002229#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002230 audioproc::Test::EchoMetrics* message = test->mutable_echo_metrics();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002231 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2232 message->mutable_residual_echo_return_loss());
2233 WriteStatsMessage(echo_metrics.echo_return_loss,
2234 message->mutable_echo_return_loss());
2235 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2236 message->mutable_echo_return_loss_enhancement());
2237 WriteStatsMessage(echo_metrics.a_nlp,
2238 message->mutable_a_nlp());
2239
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002240 audioproc::Test::DelayMetrics* message_delay =
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002241 test->mutable_delay_metrics();
2242 message_delay->set_median(median);
2243 message_delay->set_std(std);
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002244 message_delay->set_fraction_poor_delays(fraction_poor_delays);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002245
2246 test->set_rms_level(rms_level);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002247
2248 EXPECT_LE(0.0f, ns_speech_prob_average);
2249 EXPECT_GE(1.0f, ns_speech_prob_average);
2250 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002251#endif
2252 }
2253
2254 rewind(far_file_);
2255 rewind(near_file_);
2256 }
2257
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002258 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002259 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002260 }
2261}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002262
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002263TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2264 struct ChannelFormat {
2265 AudioProcessing::ChannelLayout in_layout;
2266 AudioProcessing::ChannelLayout out_layout;
2267 };
2268 ChannelFormat cf[] = {
2269 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2270 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2271 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2272 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002273
kwiberg62eaacf2016-02-17 06:39:05 -08002274 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002275 // Enable one component just to ensure some processing takes place.
2276 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002277 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002278 const int in_rate = 44100;
2279 const int out_rate = 48000;
2280 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2281 TotalChannelsFromLayout(cf[i].in_layout));
2282 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2283 ChannelsFromLayout(cf[i].out_layout));
2284
2285 // Run over a few chunks.
2286 for (int j = 0; j < 10; ++j) {
2287 EXPECT_NOERR(ap->ProcessStream(
2288 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002289 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002290 in_rate,
2291 cf[i].in_layout,
2292 out_rate,
2293 cf[i].out_layout,
2294 out_cb.channels()));
2295 }
2296 }
2297}
2298
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002299// Compares the reference and test arrays over a region around the expected
2300// delay. Finds the highest SNR in that region and adds the variance and squared
2301// error results to the supplied accumulators.
2302void UpdateBestSNR(const float* ref,
2303 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002304 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002305 int expected_delay,
2306 double* variance_acc,
2307 double* sq_error_acc) {
2308 double best_snr = std::numeric_limits<double>::min();
2309 double best_variance = 0;
2310 double best_sq_error = 0;
2311 // Search over a region of eight samples around the expected delay.
2312 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2313 ++delay) {
2314 double sq_error = 0;
2315 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002316 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002317 double error = test[i + delay] - ref[i];
2318 sq_error += error * error;
2319 variance += ref[i] * ref[i];
2320 }
2321
2322 if (sq_error == 0) {
2323 *variance_acc += variance;
2324 return;
2325 }
2326 double snr = variance / sq_error;
2327 if (snr > best_snr) {
2328 best_snr = snr;
2329 best_variance = variance;
2330 best_sq_error = sq_error;
2331 }
2332 }
2333
2334 *variance_acc += best_variance;
2335 *sq_error_acc += best_sq_error;
2336}
2337
2338// Used to test a multitude of sample rate and channel combinations. It works
2339// by first producing a set of reference files (in SetUpTestCase) that are
2340// assumed to be correct, as the used parameters are verified by other tests
2341// in this collection. Primarily the reference files are all produced at
2342// "native" rates which do not involve any resampling.
2343
2344// Each test pass produces an output file with a particular format. The output
2345// is matched against the reference file closest to its internal processing
2346// format. If necessary the output is resampled back to its process format.
2347// Due to the resampling distortion, we don't expect identical results, but
2348// enforce SNR thresholds which vary depending on the format. 0 is a special
2349// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002350typedef std::tr1::tuple<int, int, int, int, double, double>
2351 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002352class AudioProcessingTest
2353 : public testing::TestWithParam<AudioProcessingTestData> {
2354 public:
2355 AudioProcessingTest()
2356 : input_rate_(std::tr1::get<0>(GetParam())),
2357 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002358 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2359 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2360 expected_snr_(std::tr1::get<4>(GetParam())),
2361 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002362
2363 virtual ~AudioProcessingTest() {}
2364
2365 static void SetUpTestCase() {
2366 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002367 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002368 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002369 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2370 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2371 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002372 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002373 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2374 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2375 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002376 }
2377 }
2378 }
2379 }
2380
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002381 static void TearDownTestCase() {
2382 ClearTempFiles();
2383 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002384
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002385 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002386 // to a file specified with |output_file_prefix|. Both forward and reverse
2387 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002388 static void ProcessFormat(int input_rate,
2389 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002390 int reverse_input_rate,
2391 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002392 size_t num_input_channels,
2393 size_t num_output_channels,
2394 size_t num_reverse_input_channels,
2395 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002396 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002397 Config config;
2398 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002399 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002400 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002401
ekmeyerson60d9b332015-08-14 10:35:55 -07002402 ProcessingConfig processing_config = {
2403 {{input_rate, num_input_channels},
2404 {output_rate, num_output_channels},
2405 {reverse_input_rate, num_reverse_input_channels},
2406 {reverse_output_rate, num_reverse_output_channels}}};
2407 ap->Initialize(processing_config);
2408
2409 FILE* far_file =
2410 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002411 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002412 FILE* out_file =
2413 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2414 reverse_input_rate, reverse_output_rate,
2415 num_input_channels, num_output_channels,
2416 num_reverse_input_channels,
2417 num_reverse_output_channels, kForward).c_str(),
2418 "wb");
2419 FILE* rev_out_file =
2420 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2421 reverse_input_rate, reverse_output_rate,
2422 num_input_channels, num_output_channels,
2423 num_reverse_input_channels,
2424 num_reverse_output_channels, kReverse).c_str(),
2425 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002426 ASSERT_TRUE(far_file != NULL);
2427 ASSERT_TRUE(near_file != NULL);
2428 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002429 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002430
2431 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2432 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002433 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2434 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002435 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2436 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002437 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2438 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002439
2440 // Temporary buffers.
2441 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002442 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2443 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002444 std::unique_ptr<float[]> float_data(new float[max_length]);
2445 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002446
2447 int analog_level = 127;
2448 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2449 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002450 EXPECT_NOERR(ap->ProcessReverseStream(
2451 rev_cb.channels(), processing_config.reverse_input_stream(),
2452 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002453
2454 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2455 ap->echo_cancellation()->set_stream_drift_samples(0);
2456 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2457
2458 EXPECT_NOERR(ap->ProcessStream(
2459 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002460 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002461 input_rate,
2462 LayoutFromChannels(num_input_channels),
2463 output_rate,
2464 LayoutFromChannels(num_output_channels),
2465 out_cb.channels()));
2466
ekmeyerson60d9b332015-08-14 10:35:55 -07002467 // Dump forward output to file.
2468 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002469 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002470 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002471
pkasting25702cb2016-01-08 13:50:27 -08002472 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002473 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002474 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002475
ekmeyerson60d9b332015-08-14 10:35:55 -07002476 // Dump reverse output to file.
2477 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2478 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002479 size_t rev_out_length =
2480 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002481
pkasting25702cb2016-01-08 13:50:27 -08002482 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002483 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2484 rev_out_file));
2485
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002486 analog_level = ap->gain_control()->stream_analog_level();
2487 }
2488 fclose(far_file);
2489 fclose(near_file);
2490 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002491 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002492 }
2493
2494 protected:
2495 int input_rate_;
2496 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002497 int reverse_input_rate_;
2498 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002499 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002500 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002501};
2502
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002503TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002504 struct ChannelFormat {
2505 int num_input;
2506 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002507 int num_reverse_input;
2508 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002509 };
2510 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002511 {1, 1, 1, 1},
2512 {1, 1, 2, 1},
2513 {2, 1, 1, 1},
2514 {2, 1, 2, 1},
2515 {2, 2, 1, 1},
2516 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002517 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002518
pkasting25702cb2016-01-08 13:50:27 -08002519 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002520 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2521 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2522 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002523
ekmeyerson60d9b332015-08-14 10:35:55 -07002524 // Verify output for both directions.
2525 std::vector<StreamDirection> stream_directions;
2526 stream_directions.push_back(kForward);
2527 stream_directions.push_back(kReverse);
2528 for (StreamDirection file_direction : stream_directions) {
2529 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2530 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2531 const int out_num =
2532 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2533 const double expected_snr =
2534 file_direction ? expected_reverse_snr_ : expected_snr_;
2535
2536 const int min_ref_rate = std::min(in_rate, out_rate);
2537 int ref_rate;
2538
2539 if (min_ref_rate > 32000) {
2540 ref_rate = 48000;
2541 } else if (min_ref_rate > 16000) {
2542 ref_rate = 32000;
2543 } else if (min_ref_rate > 8000) {
2544 ref_rate = 16000;
2545 } else {
2546 ref_rate = 8000;
2547 }
aluebs776593b2016-03-15 14:04:58 -07002548#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002549 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002550 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002551 }
2552#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002553 FILE* out_file = fopen(
2554 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2555 reverse_output_rate_, cf[i].num_input,
2556 cf[i].num_output, cf[i].num_reverse_input,
2557 cf[i].num_reverse_output, file_direction).c_str(),
2558 "rb");
2559 // The reference files always have matching input and output channels.
2560 FILE* ref_file = fopen(
2561 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2562 cf[i].num_output, cf[i].num_output,
2563 cf[i].num_reverse_output, cf[i].num_reverse_output,
2564 file_direction).c_str(),
2565 "rb");
2566 ASSERT_TRUE(out_file != NULL);
2567 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002568
pkasting25702cb2016-01-08 13:50:27 -08002569 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2570 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002571 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002572 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002573 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002574 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002575 // Data from the resampled output, in case the reference and output rates
2576 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002577 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002578
ekmeyerson60d9b332015-08-14 10:35:55 -07002579 PushResampler<float> resampler;
2580 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002581
ekmeyerson60d9b332015-08-14 10:35:55 -07002582 // Compute the resampling delay of the output relative to the reference,
2583 // to find the region over which we should search for the best SNR.
2584 float expected_delay_sec = 0;
2585 if (in_rate != ref_rate) {
2586 // Input resampling delay.
2587 expected_delay_sec +=
2588 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2589 }
2590 if (out_rate != ref_rate) {
2591 // Output resampling delay.
2592 expected_delay_sec +=
2593 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2594 // Delay of converting the output back to its processing rate for
2595 // testing.
2596 expected_delay_sec +=
2597 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2598 }
2599 int expected_delay =
2600 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002601
ekmeyerson60d9b332015-08-14 10:35:55 -07002602 double variance = 0;
2603 double sq_error = 0;
2604 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2605 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2606 float* out_ptr = out_data.get();
2607 if (out_rate != ref_rate) {
2608 // Resample the output back to its internal processing rate if
2609 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002610 ASSERT_EQ(ref_length,
2611 static_cast<size_t>(resampler.Resample(
2612 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002613 out_ptr = cmp_data.get();
2614 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002615
ekmeyerson60d9b332015-08-14 10:35:55 -07002616 // Update the |sq_error| and |variance| accumulators with the highest
2617 // SNR of reference vs output.
2618 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2619 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002620 }
2621
ekmeyerson60d9b332015-08-14 10:35:55 -07002622 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2623 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2624 << cf[i].num_input << ", " << cf[i].num_output << ", "
2625 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2626 << ", " << file_direction << "): ";
2627 if (sq_error > 0) {
2628 double snr = 10 * log10(variance / sq_error);
2629 EXPECT_GE(snr, expected_snr);
2630 EXPECT_NE(0, expected_snr);
2631 std::cout << "SNR=" << snr << " dB" << std::endl;
2632 } else {
aluebs776593b2016-03-15 14:04:58 -07002633 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002634 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002635
ekmeyerson60d9b332015-08-14 10:35:55 -07002636 fclose(out_file);
2637 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002638 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002639 }
2640}
2641
2642#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2643INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002644 CommonFormats,
2645 AudioProcessingTest,
2646 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002647 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2648 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002649 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2650 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2651 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2652 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2653 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2654 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2655 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2656 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2657 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002658
ekmeyerson60d9b332015-08-14 10:35:55 -07002659 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2660 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2661 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2662 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2663 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2664 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2665 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2666 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2667 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2668 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2669 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2670 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002671
ekmeyerson60d9b332015-08-14 10:35:55 -07002672 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2673 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2674 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2675 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2676 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2677 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2678 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2679 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2680 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2681 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2682 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2683 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002684
ekmeyerson60d9b332015-08-14 10:35:55 -07002685 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2686 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2687 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2688 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2689 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2690 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2691 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2692 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2693 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2694 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
peah0bf612b2016-04-06 02:47:46 -07002695 std::tr1::make_tuple(16000, 16000, 32000, 16000, 50, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002696 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002697
2698#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2699INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002700 CommonFormats,
2701 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002702 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2703 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2704 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2705 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2706 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2707 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002708 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2709 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2710 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2711 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2712 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2713 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002714
aluebs776593b2016-03-15 14:04:58 -07002715 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2716 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2717 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002718 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2719 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2720 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2721 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2722 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2723 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2724 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2725 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2726 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002727
aluebs776593b2016-03-15 14:04:58 -07002728 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2729 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2730 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2731 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2732 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2733 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2734 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2735 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2736 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002737 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2738 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2739 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002740
ekmeyerson60d9b332015-08-14 10:35:55 -07002741 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2742 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2743 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2744 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2745 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2746 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2747 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2748 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2749 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2750 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
peah0bf612b2016-04-06 02:47:46 -07002751 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002752 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002753#endif
2754
niklase@google.com470e71d2011-07-07 08:21:25 +00002755} // namespace
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002756} // namespace webrtc