blob: 646e8b7b874eb1c0baba34c12cb926503d3a5d1a [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000011#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000012#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080013
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000014#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
pkasting25702cb2016-01-08 13:50:27 -080019#include "webrtc/base/arraysize.h"
kwiberg9e2be5f2016-09-14 05:23:22 -070020#include "webrtc/base/checks.h"
andrew@webrtc.org27c69802014-02-18 20:24:56 +000021#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000022#include "webrtc/common_audio/resampler/include/push_resampler.h"
23#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000024#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000025#include "webrtc/modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000026#include "webrtc/modules/audio_processing/common.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000027#include "webrtc/modules/audio_processing/include/audio_processing.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070028#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000029#include "webrtc/modules/audio_processing/test/test_utils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010031#include "webrtc/system_wrappers/include/event_wrapper.h"
32#include "webrtc/system_wrappers/include/trace.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000033#include "webrtc/test/testsupport/fileutils.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000034#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
pbos@webrtc.org8c34cee2013-05-28 09:24:03 +000035#include "gtest/gtest.h"
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000036#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000037#else
pbos@webrtc.org8c34cee2013-05-28 09:24:03 +000038#include "testing/gtest/include/gtest/gtest.h"
kjellander78ddd732016-02-09 08:13:06 -080039#include "webrtc/modules/audio_processing/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000040#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000041
andrew@webrtc.org27c69802014-02-18 20:24:56 +000042namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000043namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000044
ekmeyerson60d9b332015-08-14 10:35:55 -070045// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
46// applicable.
47
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000048// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000049// re-implemented; see comment at the bottom of this file. For now, the user has
50// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000051// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000052// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000053// be set to true with the command-line switch --write_ref_data.
54bool write_ref_data = false;
pkasting25702cb2016-01-08 13:50:27 -080055const google::protobuf::int32 kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070056const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000057
aluebseb3603b2016-04-20 15:27:58 -070058#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
59// Android doesn't support 48kHz.
60const int kProcessSampleRates[] = {8000, 16000, 32000};
61#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070062const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070063#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000064
ekmeyerson60d9b332015-08-14 10:35:55 -070065enum StreamDirection { kForward = 0, kReverse };
66
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000067void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000068 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000069 cb->num_channels());
70 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000071 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000072 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000073 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080074 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000075 S16ToFloat(cb_int.channels()[i],
76 cb->num_frames(),
77 cb->channels()[i]);
78 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000079}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000080
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
82 ConvertToFloat(frame.data_, cb);
83}
84
andrew@webrtc.org103657b2014-04-24 18:28:56 +000085// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080086size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000087 switch (layout) {
88 case AudioProcessing::kMono:
89 return 1;
90 case AudioProcessing::kMonoAndKeyboard:
91 case AudioProcessing::kStereo:
92 return 2;
93 case AudioProcessing::kStereoAndKeyboard:
94 return 3;
95 }
kwiberg9e2be5f2016-09-14 05:23:22 -070096 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -080097 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098}
99
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000100int TruncateToMultipleOf10(int value) {
101 return (value / 10) * 10;
102}
103
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000104void MixStereoToMono(const float* stereo, float* 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]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000108}
109
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000110void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800111 size_t samples_per_channel) {
112 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
114}
115
pkasting25702cb2016-01-08 13:50:27 -0800116void CopyLeftToRightChannel(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 stereo[i * 2 + 1] = stereo[i * 2];
119 }
120}
121
pkasting25702cb2016-01-08 13:50:27 -0800122void VerifyChannelsAreEqual(int16_t* stereo, size_t samples_per_channel) {
123 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000124 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
125 }
126}
127
128void SetFrameTo(AudioFrame* frame, int16_t value) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700129 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
130 ++i) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 frame->data_[i] = value;
132 }
133}
134
135void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800136 ASSERT_EQ(2u, frame->num_channels_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000138 frame->data_[i] = left;
139 frame->data_[i + 1] = right;
140 }
141}
142
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000143void ScaleFrame(AudioFrame* frame, float scale) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700144 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
145 ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000146 frame->data_[i] = FloatS16ToS16(frame->data_[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000147 }
148}
149
andrew@webrtc.org81865342012-10-27 00:28:27 +0000150bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000151 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000152 return false;
153 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000154 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000155 return false;
156 }
157 if (memcmp(frame1.data_, frame2.data_,
158 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000159 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000160 return false;
161 }
162 return true;
163}
164
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000165void EnableAllAPComponents(AudioProcessing* ap) {
166#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
167 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
168
169 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
170 EXPECT_NOERR(ap->gain_control()->Enable(true));
171#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
172 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
173 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
174 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
175 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
176
177 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
178 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
179 EXPECT_NOERR(ap->gain_control()->Enable(true));
180#endif
181
182 EXPECT_NOERR(ap->high_pass_filter()->Enable(true));
183 EXPECT_NOERR(ap->level_estimator()->Enable(true));
184 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
185
186 EXPECT_NOERR(ap->voice_detection()->Enable(true));
187}
188
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000189// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000190template <class T>
191T AbsValue(T a) {
192 return a > 0 ? a: -a;
193}
194
195int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800196 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000197 int16_t max_data = AbsValue(frame.data_[0]);
pkasting25702cb2016-01-08 13:50:27 -0800198 for (size_t i = 1; i < length; i++) {
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000199 max_data = std::max(max_data, AbsValue(frame.data_[i]));
200 }
201
202 return max_data;
203}
204
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000205#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000206void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000207 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700208 EXPECT_EQ(reference.instant(), test.instant);
209 EXPECT_EQ(reference.average(), test.average);
210 EXPECT_EQ(reference.maximum(), test.maximum);
211 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000212}
213
214void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000215 audioproc::Test::Statistic* msg) {
216 msg->set_instant(output.instant);
217 msg->set_average(output.average);
218 msg->set_maximum(output.maximum);
219 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000220}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000221#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000222
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000223void OpenFileAndWriteMessage(const std::string filename,
224 const ::google::protobuf::MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000225 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000226 ASSERT_TRUE(file != NULL);
227
228 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800230 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000231 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000235 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236 fclose(file);
237}
238
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000239std::string ResourceFilePath(std::string name, int sample_rate_hz) {
240 std::ostringstream ss;
241 // Resource files are all stereo.
242 ss << name << sample_rate_hz / 1000 << "_stereo";
243 return test::ResourcePath(ss.str(), "pcm");
244}
245
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000246// Temporary filenames unique to this process. Used to be able to run these
247// tests in parallel as each process needs to be running in isolation they can't
248// have competing filenames.
249std::map<std::string, std::string> temp_filenames;
250
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000251std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000252 int input_rate,
253 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700254 int reverse_input_rate,
255 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800256 size_t num_input_channels,
257 size_t num_output_channels,
258 size_t num_reverse_input_channels,
259 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700260 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000261 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700262 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
263 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000264 if (num_output_channels == 1) {
265 ss << "mono";
266 } else if (num_output_channels == 2) {
267 ss << "stereo";
268 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700269 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000270 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700271 ss << output_rate / 1000;
272 if (num_reverse_output_channels == 1) {
273 ss << "_rmono";
274 } else if (num_reverse_output_channels == 2) {
275 ss << "_rstereo";
276 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700277 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 }
279 ss << reverse_output_rate / 1000;
280 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000281
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000282 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700283 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000284 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
285 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000286}
287
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000288void ClearTempFiles() {
289 for (auto& kv : temp_filenames)
290 remove(kv.second.c_str());
291}
292
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000293void OpenFileAndReadMessage(const std::string filename,
294 ::google::protobuf::MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000295 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000296 ASSERT_TRUE(file != NULL);
297 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000298 fclose(file);
299}
300
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000301// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
302// stereo) file, converts to deinterleaved float (optionally downmixing) and
303// returns the result in |cb|. Returns false if the file ended (or on error) and
304// true otherwise.
305//
306// |int_data| and |float_data| are just temporary space that must be
307// sufficiently large to hold the 10 ms chunk.
308bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
309 ChannelBuffer<float>* cb) {
310 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000311 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000312 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
313 if (read_count != frame_size) {
314 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700315 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000316 return false; // This is expected.
317 }
318
319 S16ToFloat(int_data, frame_size, float_data);
320 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000321 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000322 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000323 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000324 cb->channels());
325 }
326
327 return true;
328}
329
niklase@google.com470e71d2011-07-07 08:21:25 +0000330class ApmTest : public ::testing::Test {
331 protected:
332 ApmTest();
333 virtual void SetUp();
334 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000335
336 static void SetUpTestCase() {
337 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000338 }
339
340 static void TearDownTestCase() {
341 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000342 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000343 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000344
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000345 // Used to select between int and float interface tests.
346 enum Format {
347 kIntFormat,
348 kFloatFormat
349 };
350
351 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000352 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000353 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800354 size_t num_input_channels,
355 size_t num_output_channels,
356 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000357 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000358 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000359 void EnableAllComponents();
360 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000361 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000362 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000363 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
364 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000365 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000366 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
367 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700368 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800369 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700370 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800371 void TestChangingForwardChannels(size_t num_in_channels,
372 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700373 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800374 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700375 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000376 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
377 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000378 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000379 int ProcessStreamChooser(Format format);
380 int AnalyzeReverseStreamChooser(Format format);
381 void ProcessDebugDump(const std::string& in_filename,
382 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800383 Format format,
384 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000385 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000386
387 const std::string output_path_;
388 const std::string ref_path_;
389 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800390 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000391 AudioFrame* frame_;
392 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800393 std::unique_ptr<ChannelBuffer<float> > float_cb_;
394 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000395 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800396 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000397 FILE* far_file_;
398 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000399 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400};
401
402ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000403 : output_path_(test::OutputPath()),
404 ref_path_(test::ProjectRootPath() + "data/audio_processing/"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000405#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000406 ref_filename_(ref_path_ + "output_data_fixed.pb"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000407#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000408#if defined(WEBRTC_MAC)
409 // A different file for Mac is needed because on this platform the AEC
410 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
411 ref_filename_(ref_path_ + "output_data_mac.pb"),
412#else
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000413 ref_filename_(ref_path_ + "output_data_float.pb"),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000414#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000415#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000416 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000417 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000418 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000419 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000420 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000421 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000422 out_file_(NULL) {
423 Config config;
424 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
425 apm_.reset(AudioProcessing::Create(config));
426}
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
428void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000429 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
431 frame_ = new AudioFrame();
432 revframe_ = new AudioFrame();
433
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000434 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
437void ApmTest::TearDown() {
438 if (frame_) {
439 delete frame_;
440 }
441 frame_ = NULL;
442
443 if (revframe_) {
444 delete revframe_;
445 }
446 revframe_ = NULL;
447
448 if (far_file_) {
449 ASSERT_EQ(0, fclose(far_file_));
450 }
451 far_file_ = NULL;
452
453 if (near_file_) {
454 ASSERT_EQ(0, fclose(near_file_));
455 }
456 near_file_ = NULL;
457
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000458 if (out_file_) {
459 ASSERT_EQ(0, fclose(out_file_));
460 }
461 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000464void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000465 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700466 ap->Initialize(
467 {{{frame_->sample_rate_hz_, frame_->num_channels_},
468 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700469 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700470 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000471}
472
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000473void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000474 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000475 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800476 size_t num_input_channels,
477 size_t num_output_channels,
478 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000479 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000480 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000481 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000482 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000483
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
485 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000486 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000487
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000488 if (far_file_) {
489 ASSERT_EQ(0, fclose(far_file_));
490 }
491 std::string filename = ResourceFilePath("far", sample_rate_hz);
492 far_file_ = fopen(filename.c_str(), "rb");
493 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
494 filename << "\n";
495
496 if (near_file_) {
497 ASSERT_EQ(0, fclose(near_file_));
498 }
499 filename = ResourceFilePath("near", sample_rate_hz);
500 near_file_ = fopen(filename.c_str(), "rb");
501 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
502 filename << "\n";
503
504 if (open_output_file) {
505 if (out_file_) {
506 ASSERT_EQ(0, fclose(out_file_));
507 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700508 filename = OutputFilePath(
509 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
510 reverse_sample_rate_hz, num_input_channels, num_output_channels,
511 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000512 out_file_ = fopen(filename.c_str(), "wb");
513 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
514 filename << "\n";
515 }
516}
517
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000518void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000519 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000520}
521
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000522bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
523 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000524 // The files always contain stereo audio.
525 size_t frame_size = frame->samples_per_channel_ * 2;
526 size_t read_count = fread(frame->data_,
527 sizeof(int16_t),
528 frame_size,
529 file);
530 if (read_count != frame_size) {
531 // Check that the file really ended.
532 EXPECT_NE(0, feof(file));
533 return false; // This is expected.
534 }
535
536 if (frame->num_channels_ == 1) {
537 MixStereoToMono(frame->data_, frame->data_,
538 frame->samples_per_channel_);
539 }
540
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000541 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000542 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000543 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000544 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000545}
546
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000547bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
548 return ReadFrame(file, frame, NULL);
549}
550
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000551// If the end of the file has been reached, rewind it and attempt to read the
552// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000553void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
554 ChannelBuffer<float>* cb) {
555 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000556 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000558 }
559}
560
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
562 ReadFrameWithRewind(file, frame, NULL);
563}
564
andrew@webrtc.org81865342012-10-27 00:28:27 +0000565void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
566 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000567 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000568 EXPECT_EQ(apm_->kNoError,
569 apm_->gain_control()->set_stream_analog_level(127));
570 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000571}
572
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000573int ApmTest::ProcessStreamChooser(Format format) {
574 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000575 return apm_->ProcessStream(frame_);
576 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577 return apm_->ProcessStream(float_cb_->channels(),
578 frame_->samples_per_channel_,
579 frame_->sample_rate_hz_,
580 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000581 output_sample_rate_hz_,
582 LayoutFromChannels(num_output_channels_),
583 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000584}
585
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000586int ApmTest::AnalyzeReverseStreamChooser(Format format) {
587 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700588 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000589 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000590 return apm_->AnalyzeReverseStream(
591 revfloat_cb_->channels(),
592 revframe_->samples_per_channel_,
593 revframe_->sample_rate_hz_,
594 LayoutFromChannels(revframe_->num_channels_));
595}
596
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000597void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
598 int delay_min, int delay_max) {
599 // The |revframe_| and |frame_| should include the proper frame information,
600 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000601 AudioFrame tmp_frame;
602 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000603 bool causal = true;
604
605 tmp_frame.CopyFrom(*revframe_);
606 SetFrameTo(&tmp_frame, 0);
607
608 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
609 // Initialize the |frame_queue| with empty frames.
610 int frame_delay = delay_ms / 10;
611 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000612 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000613 frame->CopyFrom(tmp_frame);
614 frame_queue.push(frame);
615 frame_delay++;
616 causal = false;
617 }
618 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000619 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 frame->CopyFrom(tmp_frame);
621 frame_queue.push(frame);
622 frame_delay--;
623 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000624 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
625 // need enough frames with audio to have reliable estimates, but as few as
626 // possible to keep processing time down. 4.5 seconds seemed to be a good
627 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000628 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000629 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000630 frame->CopyFrom(tmp_frame);
631 // Use the near end recording, since that has more speech in it.
632 ASSERT_TRUE(ReadFrame(near_file_, frame));
633 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000634 AudioFrame* reverse_frame = frame;
635 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000636 if (!causal) {
637 reverse_frame = frame_queue.front();
638 // When we call ProcessStream() the frame is modified, so we can't use the
639 // pointer directly when things are non-causal. Use an intermediate frame
640 // and copy the data.
641 process_frame = &tmp_frame;
642 process_frame->CopyFrom(*frame);
643 }
aluebsb0319552016-03-17 20:39:53 -0700644 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000645 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
646 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
647 frame = frame_queue.front();
648 frame_queue.pop();
649 delete frame;
650
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000651 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000652 int median;
653 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000654 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000655 // Discard the first delay metrics to avoid convergence effects.
656 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000657 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
658 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000659 }
660 }
661
662 rewind(near_file_);
663 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000664 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000665 frame_queue.pop();
666 delete frame;
667 }
668 // Calculate expected delay estimate and acceptable regions. Further,
669 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700670 const size_t samples_per_ms =
671 std::min(static_cast<size_t>(16), frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000672 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
673 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700674 int expected_median_high = std::min(
675 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
676 delay_min),
677 delay_max);
678 int expected_median_low = std::min(
679 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
680 delay_min),
681 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000682 // Verify delay metrics.
683 int median;
684 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000685 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000686 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000687 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
688 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000689 EXPECT_GE(expected_median_high, median);
690 EXPECT_LE(expected_median_low, median);
691}
692
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000693void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000695 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000696
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000697 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000698 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000699 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000700 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000702 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000703 EXPECT_EQ(apm_->kNoError,
704 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000705 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000706 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000707 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000708
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000709 // Other stream parameters set correctly.
710 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 EXPECT_EQ(apm_->kNoError,
712 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000714 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000716 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
718 EXPECT_EQ(apm_->kNoError,
719 apm_->echo_cancellation()->enable_drift_compensation(false));
720
721 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000722 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000724 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000725 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000726
727 // Resets after successful ProcessStream().
728 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000729 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000730 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000731 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000732
733 // Other stream parameters set correctly.
734 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
735 EXPECT_EQ(apm_->kNoError,
736 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000737 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000738 EXPECT_EQ(apm_->kNoError,
739 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000740 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000741 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000742 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
743
744 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000745 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000746 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000747
748 // Resets after successful ProcessStream().
749 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000750 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000751 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000752 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000753 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000754
755 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000756 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
757 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
758 EXPECT_EQ(apm_->kNoError,
759 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000760 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000761 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000762
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000763 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000765 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000767 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000768
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000769 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000770 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000771 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 EXPECT_EQ(apm_->kNoError,
773 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000774 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000775}
776
777TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000779}
780
781TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000782 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000783}
784
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000785TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
786 EXPECT_EQ(0, apm_->delay_offset_ms());
787 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
788 EXPECT_EQ(50, apm_->stream_delay_ms());
789}
790
791TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
792 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000793 apm_->set_delay_offset_ms(100);
794 EXPECT_EQ(100, apm_->delay_offset_ms());
795 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000796 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000797 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
798 EXPECT_EQ(200, apm_->stream_delay_ms());
799
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000800 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000801 apm_->set_delay_offset_ms(-50);
802 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000803 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
804 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000805 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
806 EXPECT_EQ(50, apm_->stream_delay_ms());
807}
808
Michael Graczyk86c6d332015-07-23 11:41:39 -0700809void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800810 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700811 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000812 frame_->num_channels_ = num_channels;
813 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700814 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000815}
816
Michael Graczyk86c6d332015-07-23 11:41:39 -0700817void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800818 size_t num_in_channels,
819 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700820 AudioProcessing::Error expected_return) {
821 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
822 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
823
824 EXPECT_EQ(expected_return,
825 apm_->ProcessStream(float_cb_->channels(), input_stream,
826 output_stream, float_cb_->channels()));
827}
828
829void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800830 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700831 AudioProcessing::Error expected_return) {
832 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700833 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
834 {output_sample_rate_hz_, apm_->num_output_channels()},
835 {frame_->sample_rate_hz_, num_rev_channels},
836 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700837
ekmeyerson60d9b332015-08-14 10:35:55 -0700838 EXPECT_EQ(
839 expected_return,
840 apm_->ProcessReverseStream(
841 float_cb_->channels(), processing_config.reverse_input_stream(),
842 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700843}
844
845TEST_F(ApmTest, ChannelsInt16Interface) {
846 // Testing number of invalid and valid channels.
847 Init(16000, 16000, 16000, 4, 4, 4, false);
848
849 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
850
Peter Kasting69558702016-01-12 16:26:35 -0800851 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700852 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000853 EXPECT_EQ(i, apm_->num_input_channels());
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000854 // We always force the number of reverse channels used for processing to 1.
Peter Kasting69558702016-01-12 16:26:35 -0800855 EXPECT_EQ(1u, apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000856 }
857}
858
Michael Graczyk86c6d332015-07-23 11:41:39 -0700859TEST_F(ApmTest, Channels) {
860 // Testing number of invalid and valid channels.
861 Init(16000, 16000, 16000, 4, 4, 4, false);
862
863 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
864 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
865
Peter Kasting69558702016-01-12 16:26:35 -0800866 for (size_t i = 1; i < 4; ++i) {
867 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700868 // Output channels much be one or match input channels.
869 if (j == 1 || i == j) {
870 TestChangingForwardChannels(i, j, kNoErr);
871 TestChangingReverseChannels(i, kNoErr);
872
873 EXPECT_EQ(i, apm_->num_input_channels());
874 EXPECT_EQ(j, apm_->num_output_channels());
875 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800876 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700877 } else {
878 TestChangingForwardChannels(i, j,
879 AudioProcessing::kBadNumberChannelsError);
880 }
881 }
882 }
883}
884
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000885TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000887 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000888 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000889 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700890 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800891 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000892 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000893 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 }
895}
896
niklase@google.com470e71d2011-07-07 08:21:25 +0000897TEST_F(ApmTest, EchoCancellation) {
898 EXPECT_EQ(apm_->kNoError,
899 apm_->echo_cancellation()->enable_drift_compensation(true));
900 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
901 EXPECT_EQ(apm_->kNoError,
902 apm_->echo_cancellation()->enable_drift_compensation(false));
903 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
904
niklase@google.com470e71d2011-07-07 08:21:25 +0000905 EchoCancellation::SuppressionLevel level[] = {
906 EchoCancellation::kLowSuppression,
907 EchoCancellation::kModerateSuppression,
908 EchoCancellation::kHighSuppression,
909 };
pkasting25702cb2016-01-08 13:50:27 -0800910 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000911 EXPECT_EQ(apm_->kNoError,
912 apm_->echo_cancellation()->set_suppression_level(level[i]));
913 EXPECT_EQ(level[i],
914 apm_->echo_cancellation()->suppression_level());
915 }
916
917 EchoCancellation::Metrics metrics;
918 EXPECT_EQ(apm_->kNotEnabledError,
919 apm_->echo_cancellation()->GetMetrics(&metrics));
920
921 EXPECT_EQ(apm_->kNoError,
922 apm_->echo_cancellation()->enable_metrics(true));
923 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
924 EXPECT_EQ(apm_->kNoError,
925 apm_->echo_cancellation()->enable_metrics(false));
926 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
927
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000928 int median = 0;
929 int std = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000930 float poor_fraction = 0;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000931 EXPECT_EQ(apm_->kNotEnabledError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000932 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
933 &poor_fraction));
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000934
935 EXPECT_EQ(apm_->kNoError,
936 apm_->echo_cancellation()->enable_delay_logging(true));
937 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
938 EXPECT_EQ(apm_->kNoError,
939 apm_->echo_cancellation()->enable_delay_logging(false));
940 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
941
niklase@google.com470e71d2011-07-07 08:21:25 +0000942 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
943 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
944 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
945 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000946
947 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
948 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
949 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
950 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
951 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
952 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000953}
954
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000955TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000956 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000957 // Enable AEC only.
958 EXPECT_EQ(apm_->kNoError,
959 apm_->echo_cancellation()->enable_drift_compensation(false));
960 EXPECT_EQ(apm_->kNoError,
961 apm_->echo_cancellation()->enable_metrics(false));
962 EXPECT_EQ(apm_->kNoError,
963 apm_->echo_cancellation()->enable_delay_logging(true));
964 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000965 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700966 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000967 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000968
969 // Internally in the AEC the amount of lookahead the delay estimation can
970 // handle is 15 blocks and the maximum delay is set to 60 blocks.
971 const int kLookaheadBlocks = 15;
972 const int kMaxDelayBlocks = 60;
973 // The AEC has a startup time before it actually starts to process. This
974 // procedure can flush the internal far-end buffer, which of course affects
975 // the delay estimation. Therefore, we set a system_delay high enough to
976 // avoid that. The smallest system_delay you can report without flushing the
977 // buffer is 66 ms in 8 kHz.
978 //
979 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
980 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
981 // delay estimation. This should be noted though. In case of test failure,
982 // this could be the cause.
983 const int kSystemDelayMs = 66;
984 // Test a couple of corner cases and verify that the estimated delay is
985 // within a valid region (set to +-1.5 blocks). Note that these cases are
986 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800987 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000988 Init(kProcessSampleRates[i],
989 kProcessSampleRates[i],
990 kProcessSampleRates[i],
991 2,
992 2,
993 2,
994 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000995 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700996 const int num_ms_per_block =
997 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000998 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
999 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1000
1001 // 1) Verify correct delay estimate at lookahead boundary.
1002 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1003 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1004 delay_max_ms);
1005 // 2) A delay less than maximum lookahead should give an delay estimate at
1006 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1007 delay_ms -= 20;
1008 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1009 delay_max_ms);
1010 // 3) Three values around zero delay. Note that we need to compensate for
1011 // the fake system_delay.
1012 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1013 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1014 delay_max_ms);
1015 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1016 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1017 delay_max_ms);
1018 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1019 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1020 delay_max_ms);
1021 // 4) Verify correct delay estimate at maximum delay boundary.
1022 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1023 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1024 delay_max_ms);
1025 // 5) A delay above the maximum delay should give an estimate at the
1026 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1027 delay_ms += 20;
1028 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1029 delay_max_ms);
1030 }
1031}
1032
niklase@google.com470e71d2011-07-07 08:21:25 +00001033TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001034 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001035 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001036 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1037 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1038
niklase@google.com470e71d2011-07-07 08:21:25 +00001039 // Toggle routing modes
1040 EchoControlMobile::RoutingMode mode[] = {
1041 EchoControlMobile::kQuietEarpieceOrHeadset,
1042 EchoControlMobile::kEarpiece,
1043 EchoControlMobile::kLoudEarpiece,
1044 EchoControlMobile::kSpeakerphone,
1045 EchoControlMobile::kLoudSpeakerphone,
1046 };
pkasting25702cb2016-01-08 13:50:27 -08001047 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 EXPECT_EQ(apm_->kNoError,
1049 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1050 EXPECT_EQ(mode[i],
1051 apm_->echo_control_mobile()->routing_mode());
1052 }
1053 // Turn comfort noise off/on
1054 EXPECT_EQ(apm_->kNoError,
1055 apm_->echo_control_mobile()->enable_comfort_noise(false));
1056 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1057 EXPECT_EQ(apm_->kNoError,
1058 apm_->echo_control_mobile()->enable_comfort_noise(true));
1059 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001060 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001061 const size_t echo_path_size =
1062 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001063 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1064 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001065 EXPECT_EQ(apm_->kNullPointerError,
1066 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1067 EXPECT_EQ(apm_->kNullPointerError,
1068 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1069 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001070 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001071 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001072 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001073 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001074 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001075 echo_path_in[i] = echo_path_out[i] + 1;
1076 }
1077 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001078 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001079 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001080 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1081 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001082 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001083 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1084 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001085 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001086 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1087 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001088
1089 // Process a few frames with NS in the default disabled state. This exercises
1090 // a different codepath than with it enabled.
1091 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1092 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1093 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1094 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1095
niklase@google.com470e71d2011-07-07 08:21:25 +00001096 // Turn AECM off
1097 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1098 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1099}
1100
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001101TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001102 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001103 EXPECT_EQ(apm_->kNoError,
1104 apm_->gain_control()->set_mode(
1105 apm_->gain_control()->mode()));
1106
1107 GainControl::Mode mode[] = {
1108 GainControl::kAdaptiveAnalog,
1109 GainControl::kAdaptiveDigital,
1110 GainControl::kFixedDigital
1111 };
pkasting25702cb2016-01-08 13:50:27 -08001112 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001113 EXPECT_EQ(apm_->kNoError,
1114 apm_->gain_control()->set_mode(mode[i]));
1115 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1116 }
1117 // Testing invalid target levels
1118 EXPECT_EQ(apm_->kBadParameterError,
1119 apm_->gain_control()->set_target_level_dbfs(-3));
1120 EXPECT_EQ(apm_->kBadParameterError,
1121 apm_->gain_control()->set_target_level_dbfs(-40));
1122 // Testing valid target levels
1123 EXPECT_EQ(apm_->kNoError,
1124 apm_->gain_control()->set_target_level_dbfs(
1125 apm_->gain_control()->target_level_dbfs()));
1126
1127 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001128 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001129 EXPECT_EQ(apm_->kNoError,
1130 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1131 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1132 }
1133
1134 // Testing invalid compression gains
1135 EXPECT_EQ(apm_->kBadParameterError,
1136 apm_->gain_control()->set_compression_gain_db(-1));
1137 EXPECT_EQ(apm_->kBadParameterError,
1138 apm_->gain_control()->set_compression_gain_db(100));
1139
1140 // Testing valid compression gains
1141 EXPECT_EQ(apm_->kNoError,
1142 apm_->gain_control()->set_compression_gain_db(
1143 apm_->gain_control()->compression_gain_db()));
1144
1145 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001146 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001147 EXPECT_EQ(apm_->kNoError,
1148 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1149 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1150 }
1151
1152 // Testing limiter off/on
1153 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1154 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1155 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1156 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1157
1158 // Testing invalid level limits
1159 EXPECT_EQ(apm_->kBadParameterError,
1160 apm_->gain_control()->set_analog_level_limits(-1, 512));
1161 EXPECT_EQ(apm_->kBadParameterError,
1162 apm_->gain_control()->set_analog_level_limits(100000, 512));
1163 EXPECT_EQ(apm_->kBadParameterError,
1164 apm_->gain_control()->set_analog_level_limits(512, -1));
1165 EXPECT_EQ(apm_->kBadParameterError,
1166 apm_->gain_control()->set_analog_level_limits(512, 100000));
1167 EXPECT_EQ(apm_->kBadParameterError,
1168 apm_->gain_control()->set_analog_level_limits(512, 255));
1169
1170 // Testing valid level limits
1171 EXPECT_EQ(apm_->kNoError,
1172 apm_->gain_control()->set_analog_level_limits(
1173 apm_->gain_control()->analog_level_minimum(),
1174 apm_->gain_control()->analog_level_maximum()));
1175
1176 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001177 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001178 EXPECT_EQ(apm_->kNoError,
1179 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1180 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1181 }
1182
1183 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001184 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001185 EXPECT_EQ(apm_->kNoError,
1186 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1187 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1188 }
1189
1190 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1191
1192 // Turn AGC off
1193 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1194 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1195}
1196
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001197void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001198 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001199 EXPECT_EQ(apm_->kNoError,
1200 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1201 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1202
1203 int out_analog_level = 0;
1204 for (int i = 0; i < 2000; ++i) {
1205 ReadFrameWithRewind(near_file_, frame_);
1206 // Ensure the audio is at a low level, so the AGC will try to increase it.
1207 ScaleFrame(frame_, 0.25);
1208
1209 // Always pass in the same volume.
1210 EXPECT_EQ(apm_->kNoError,
1211 apm_->gain_control()->set_stream_analog_level(100));
1212 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1213 out_analog_level = apm_->gain_control()->stream_analog_level();
1214 }
1215
1216 // Ensure the AGC is still able to reach the maximum.
1217 EXPECT_EQ(255, out_analog_level);
1218}
1219
1220// Verifies that despite volume slider quantization, the AGC can continue to
1221// increase its volume.
1222TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001223 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001224 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1225 }
1226}
1227
1228void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001229 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001230 EXPECT_EQ(apm_->kNoError,
1231 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1232 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1233
1234 int out_analog_level = 100;
1235 for (int i = 0; i < 1000; ++i) {
1236 ReadFrameWithRewind(near_file_, frame_);
1237 // Ensure the audio is at a low level, so the AGC will try to increase it.
1238 ScaleFrame(frame_, 0.25);
1239
1240 EXPECT_EQ(apm_->kNoError,
1241 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1242 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1243 out_analog_level = apm_->gain_control()->stream_analog_level();
1244 }
1245
1246 // Ensure the volume was raised.
1247 EXPECT_GT(out_analog_level, 100);
1248 int highest_level_reached = out_analog_level;
1249 // Simulate a user manual volume change.
1250 out_analog_level = 100;
1251
1252 for (int i = 0; i < 300; ++i) {
1253 ReadFrameWithRewind(near_file_, frame_);
1254 ScaleFrame(frame_, 0.25);
1255
1256 EXPECT_EQ(apm_->kNoError,
1257 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1258 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1259 out_analog_level = apm_->gain_control()->stream_analog_level();
1260 // Check that AGC respected the manually adjusted volume.
1261 EXPECT_LT(out_analog_level, highest_level_reached);
1262 }
1263 // Check that the volume was still raised.
1264 EXPECT_GT(out_analog_level, 100);
1265}
1266
1267TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001268 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001269 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1270 }
1271}
1272
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001273#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1274TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1275 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001276 const size_t kSamplesPerChannel =
1277 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001278 const size_t kNumInputChannels = 2;
1279 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001280 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001281 const float kScaleFactor = 0.25f;
1282 Config config;
1283 std::vector<webrtc::Point> geometry;
1284 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1285 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1286 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001287 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001288 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001289 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001290 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001291 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1292 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1293 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001294 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1295 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001296 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1297 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001298 std::string filename = ResourceFilePath("far", kSampleRateHz);
1299 FILE* far_file = fopen(filename.c_str(), "rb");
1300 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1301 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1302 const int kDefaultCompressionGain =
1303 apm->gain_control()->compression_gain_db();
1304 bool is_target = false;
1305 EXPECT_CALL(*beamformer, is_target_present())
1306 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001307 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001308 ASSERT_TRUE(ReadChunk(far_file,
1309 int_data.get(),
1310 float_data.get(),
1311 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001312 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001313 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001314 src_buf.channels()[j][k] *= kScaleFactor;
1315 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001316 }
1317 EXPECT_EQ(kNoErr,
1318 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001319 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001320 kSampleRateHz,
1321 LayoutFromChannels(src_buf.num_channels()),
1322 kSampleRateHz,
1323 LayoutFromChannels(dest_buf.num_channels()),
1324 dest_buf.channels()));
1325 }
1326 EXPECT_EQ(kDefaultVolume,
1327 apm->gain_control()->stream_analog_level());
1328 EXPECT_EQ(kDefaultCompressionGain,
1329 apm->gain_control()->compression_gain_db());
1330 rewind(far_file);
1331 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001332 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001333 ASSERT_TRUE(ReadChunk(far_file,
1334 int_data.get(),
1335 float_data.get(),
1336 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001337 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001338 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001339 src_buf.channels()[j][k] *= kScaleFactor;
1340 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001341 }
1342 EXPECT_EQ(kNoErr,
1343 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001344 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001345 kSampleRateHz,
1346 LayoutFromChannels(src_buf.num_channels()),
1347 kSampleRateHz,
1348 LayoutFromChannels(dest_buf.num_channels()),
1349 dest_buf.channels()));
1350 }
1351 EXPECT_LT(kDefaultVolume,
1352 apm->gain_control()->stream_analog_level());
1353 EXPECT_LT(kDefaultCompressionGain,
1354 apm->gain_control()->compression_gain_db());
1355 ASSERT_EQ(0, fclose(far_file));
1356}
1357#endif
1358
niklase@google.com470e71d2011-07-07 08:21:25 +00001359TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001360 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001361 NoiseSuppression::Level level[] = {
1362 NoiseSuppression::kLow,
1363 NoiseSuppression::kModerate,
1364 NoiseSuppression::kHigh,
1365 NoiseSuppression::kVeryHigh
1366 };
pkasting25702cb2016-01-08 13:50:27 -08001367 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001368 EXPECT_EQ(apm_->kNoError,
1369 apm_->noise_suppression()->set_level(level[i]));
1370 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1371 }
1372
andrew@webrtc.org648af742012-02-08 01:57:29 +00001373 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001374 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1375 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1376 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1377 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1378}
1379
1380TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001381 // Turn HP filter on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001382 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(true));
1383 EXPECT_TRUE(apm_->high_pass_filter()->is_enabled());
1384 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(false));
1385 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1386}
1387
1388TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001389 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001390 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001392
1393 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1394
1395 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1396 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1397
1398 // Run this test in wideband; in super-wb, the splitting filter distorts the
1399 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001400 frame_->samples_per_channel_ = 160;
1401 frame_->num_channels_ = 2;
1402 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001403
1404 // Min value if no frames have been processed.
1405 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1406
1407 // Min value on zero frames.
1408 SetFrameTo(frame_, 0);
1409 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1410 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1411 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1412
1413 // Try a few RMS values.
1414 // (These also test that the value resets after retrieving it.)
1415 SetFrameTo(frame_, 32767);
1416 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1417 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1418 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1419
1420 SetFrameTo(frame_, 30000);
1421 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1422 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1423 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1424
1425 SetFrameTo(frame_, 10000);
1426 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1427 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1428 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1429
1430 SetFrameTo(frame_, 10);
1431 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1432 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1433 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1434
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001435 // Verify reset after enable/disable.
1436 SetFrameTo(frame_, 32767);
1437 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1438 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1439 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1440 SetFrameTo(frame_, 1);
1441 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1442 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1443
1444 // Verify reset after initialize.
1445 SetFrameTo(frame_, 32767);
1446 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1447 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1448 SetFrameTo(frame_, 1);
1449 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1450 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001451}
1452
1453TEST_F(ApmTest, VoiceDetection) {
1454 // Test external VAD
1455 EXPECT_EQ(apm_->kNoError,
1456 apm_->voice_detection()->set_stream_has_voice(true));
1457 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1458 EXPECT_EQ(apm_->kNoError,
1459 apm_->voice_detection()->set_stream_has_voice(false));
1460 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1461
andrew@webrtc.org648af742012-02-08 01:57:29 +00001462 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001463 VoiceDetection::Likelihood likelihood[] = {
1464 VoiceDetection::kVeryLowLikelihood,
1465 VoiceDetection::kLowLikelihood,
1466 VoiceDetection::kModerateLikelihood,
1467 VoiceDetection::kHighLikelihood
1468 };
pkasting25702cb2016-01-08 13:50:27 -08001469 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001470 EXPECT_EQ(apm_->kNoError,
1471 apm_->voice_detection()->set_likelihood(likelihood[i]));
1472 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1473 }
1474
1475 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001476 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001477 EXPECT_EQ(apm_->kBadParameterError,
1478 apm_->voice_detection()->set_frame_size_ms(12));
1479
andrew@webrtc.org648af742012-02-08 01:57:29 +00001480 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001481 for (int i = 10; i <= 30; i += 10) {
1482 EXPECT_EQ(apm_->kNoError,
1483 apm_->voice_detection()->set_frame_size_ms(i));
1484 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1485 }
1486 */
1487
andrew@webrtc.org648af742012-02-08 01:57:29 +00001488 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001489 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1490 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1491 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1492 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1493
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001494 // Test that AudioFrame activity is maintained when VAD is disabled.
1495 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1496 AudioFrame::VADActivity activity[] = {
1497 AudioFrame::kVadActive,
1498 AudioFrame::kVadPassive,
1499 AudioFrame::kVadUnknown
1500 };
pkasting25702cb2016-01-08 13:50:27 -08001501 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001502 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001503 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001504 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001505 }
1506
1507 // Test that AudioFrame activity is set when VAD is enabled.
1508 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001509 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001510 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001511 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001512
niklase@google.com470e71d2011-07-07 08:21:25 +00001513 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1514}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001515
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001516TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1517 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1518 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1519 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1520 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1521 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1522 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1523 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1524}
1525
1526TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001527 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001528 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001529 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001530 AudioFrame frame_copy;
1531 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001532 for (int j = 0; j < 1000; j++) {
1533 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1534 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001535 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1536 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001537 }
1538 }
1539}
1540
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001541TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1542 // Test that ProcessStream copies input to output even with no processing.
1543 const size_t kSamples = 80;
1544 const int sample_rate = 8000;
1545 const float src[kSamples] = {
1546 -1.0f, 0.0f, 1.0f
1547 };
1548 float dest[kSamples] = {};
1549
1550 auto src_channels = &src[0];
1551 auto dest_channels = &dest[0];
1552
1553 apm_.reset(AudioProcessing::Create());
1554 EXPECT_NOERR(apm_->ProcessStream(
1555 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1556 sample_rate, LayoutFromChannels(1), &dest_channels));
1557
1558 for (size_t i = 0; i < kSamples; ++i) {
1559 EXPECT_EQ(src[i], dest[i]);
1560 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001561
1562 // Same for ProcessReverseStream.
1563 float rev_dest[kSamples] = {};
1564 auto rev_dest_channels = &rev_dest[0];
1565
1566 StreamConfig input_stream = {sample_rate, 1};
1567 StreamConfig output_stream = {sample_rate, 1};
1568 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1569 output_stream, &rev_dest_channels));
1570
1571 for (size_t i = 0; i < kSamples; ++i) {
1572 EXPECT_EQ(src[i], rev_dest[i]);
1573 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001574}
1575
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001576TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1577 EnableAllComponents();
1578
pkasting25702cb2016-01-08 13:50:27 -08001579 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001580 Init(kProcessSampleRates[i],
1581 kProcessSampleRates[i],
1582 kProcessSampleRates[i],
1583 2,
1584 2,
1585 2,
1586 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001587 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001588 ASSERT_EQ(0, feof(far_file_));
1589 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001590 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001591 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1592
aluebsb0319552016-03-17 20:39:53 -07001593 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001594
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001595 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1596 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1597
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001598 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001599 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001600 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001601 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001602 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001603 analog_level = apm_->gain_control()->stream_analog_level();
1604
1605 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1606 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001607 rewind(far_file_);
1608 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001609 }
1610}
1611
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001612TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001613 // Verify the filter is not active through undistorted audio when:
1614 // 1. No components are enabled...
1615 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001616 AudioFrame frame_copy;
1617 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001618 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1619 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1620 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1621
1622 // 2. Only the level estimator is enabled...
1623 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001624 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001625 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1626 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1627 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1628 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1629 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1630
1631 // 3. Only VAD is enabled...
1632 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001633 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001634 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1635 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1636 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1637 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1638 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1639
1640 // 4. Both VAD and the level estimator are enabled...
1641 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001642 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001643 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1644 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1645 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1646 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1647 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1648 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1649 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1650
1651 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001652 frame_->samples_per_channel_ = 160;
1653 frame_->num_channels_ = 2;
1654 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001655 // Enable AEC, which would require the filter in super-wb. We rely on the
1656 // first few frames of data being unaffected by the AEC.
1657 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1658 // behavior of the AEC. Think of something more robust.
1659 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001660 // Make sure we have extended filter enabled. This makes sure nothing is
1661 // touched until we have a farend frame.
1662 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001663 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001664 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001665 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001666 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001667 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001668 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001669 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1670 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001671 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001672 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1673 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1674
1675 // Check the test is valid. We should have distortion from the filter
1676 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001677 frame_->samples_per_channel_ = 320;
1678 frame_->num_channels_ = 2;
1679 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001680 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001681 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001682 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001683 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001684 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1685 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1686}
1687
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001688#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1689void ApmTest::ProcessDebugDump(const std::string& in_filename,
1690 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001691 Format format,
1692 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001693 FILE* in_file = fopen(in_filename.c_str(), "rb");
1694 ASSERT_TRUE(in_file != NULL);
1695 audioproc::Event event_msg;
1696 bool first_init = true;
1697
1698 while (ReadMessageFromFile(in_file, &event_msg)) {
1699 if (event_msg.type() == audioproc::Event::INIT) {
1700 const audioproc::Init msg = event_msg.init();
1701 int reverse_sample_rate = msg.sample_rate();
1702 if (msg.has_reverse_sample_rate()) {
1703 reverse_sample_rate = msg.reverse_sample_rate();
1704 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001705 int output_sample_rate = msg.sample_rate();
1706 if (msg.has_output_sample_rate()) {
1707 output_sample_rate = msg.output_sample_rate();
1708 }
1709
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001710 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001711 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001712 reverse_sample_rate,
1713 msg.num_input_channels(),
1714 msg.num_output_channels(),
1715 msg.num_reverse_channels(),
1716 false);
1717 if (first_init) {
1718 // StartDebugRecording() writes an additional init message. Don't start
1719 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001720 EXPECT_NOERR(
1721 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001722 first_init = false;
1723 }
1724
1725 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1726 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1727
1728 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001729 ASSERT_EQ(revframe_->num_channels_,
1730 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001731 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001732 memcpy(revfloat_cb_->channels()[i],
1733 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001734 msg.channel(i).size());
1735 }
1736 } else {
1737 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1738 if (format == kFloatFormat) {
1739 // We're using an int16 input file; convert to float.
1740 ConvertToFloat(*revframe_, revfloat_cb_.get());
1741 }
1742 }
1743 AnalyzeReverseStreamChooser(format);
1744
1745 } else if (event_msg.type() == audioproc::Event::STREAM) {
1746 const audioproc::Stream msg = event_msg.stream();
1747 // ProcessStream could have changed this for the output frame.
1748 frame_->num_channels_ = apm_->num_input_channels();
1749
1750 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1751 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1752 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1753 if (msg.has_keypress()) {
1754 apm_->set_stream_key_pressed(msg.keypress());
1755 } else {
1756 apm_->set_stream_key_pressed(true);
1757 }
1758
1759 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001760 ASSERT_EQ(frame_->num_channels_,
1761 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001762 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001763 memcpy(float_cb_->channels()[i],
1764 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001765 msg.input_channel(i).size());
1766 }
1767 } else {
1768 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1769 if (format == kFloatFormat) {
1770 // We're using an int16 input file; convert to float.
1771 ConvertToFloat(*frame_, float_cb_.get());
1772 }
1773 }
1774 ProcessStreamChooser(format);
1775 }
1776 }
1777 EXPECT_NOERR(apm_->StopDebugRecording());
1778 fclose(in_file);
1779}
1780
1781void ApmTest::VerifyDebugDumpTest(Format format) {
1782 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001783 std::string format_string;
1784 switch (format) {
1785 case kIntFormat:
1786 format_string = "_int";
1787 break;
1788 case kFloatFormat:
1789 format_string = "_float";
1790 break;
1791 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001792 const std::string ref_filename = test::TempFilename(
1793 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1794 const std::string out_filename = test::TempFilename(
1795 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001796 const std::string limited_filename = test::TempFilename(
1797 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1798 const size_t logging_limit_bytes = 100000;
1799 // We expect at least this many bytes in the created logfile.
1800 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001801 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001802 ProcessDebugDump(in_filename, ref_filename, format, -1);
1803 ProcessDebugDump(ref_filename, out_filename, format, -1);
1804 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001805
1806 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1807 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001808 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001809 ASSERT_TRUE(ref_file != NULL);
1810 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001811 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001812 std::unique_ptr<uint8_t[]> ref_bytes;
1813 std::unique_ptr<uint8_t[]> out_bytes;
1814 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001815
1816 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1817 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001818 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001819 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001820 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001821 while (ref_size > 0 && out_size > 0) {
1822 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001823 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001824 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001825 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001826 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001827 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001828 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1829 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001830 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001831 }
1832 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001833 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1834 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001835 EXPECT_NE(0, feof(ref_file));
1836 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001837 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001838 ASSERT_EQ(0, fclose(ref_file));
1839 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001840 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001841 remove(ref_filename.c_str());
1842 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001843 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001844}
1845
pbosc7a65692016-05-06 12:50:04 -07001846TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001847 VerifyDebugDumpTest(kIntFormat);
1848}
1849
pbosc7a65692016-05-06 12:50:04 -07001850TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001851 VerifyDebugDumpTest(kFloatFormat);
1852}
1853#endif
1854
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001855// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001856TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001857 const std::string filename =
1858 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001859 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001860 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001861
1862#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1863 // Stopping without having started should be OK.
1864 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1865
ivocd66b44d2016-01-15 03:06:36 -08001866 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001867 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001868 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001869 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1870
1871 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001872 FILE* fid = fopen(filename.c_str(), "r");
1873 ASSERT_TRUE(fid != NULL);
1874
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001875 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001876 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001877 ASSERT_EQ(0, remove(filename.c_str()));
1878#else
1879 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001880 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001881 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1882
1883 // Verify the file has NOT been written.
1884 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1885#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1886}
1887
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001888// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001889TEST_F(ApmTest, DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001890 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001891 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001892 const std::string filename =
1893 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001894 fid = fopen(filename.c_str(), "w");
1895 ASSERT_TRUE(fid);
1896
1897#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1898 // Stopping without having started should be OK.
1899 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1900
ivocd66b44d2016-01-15 03:06:36 -08001901 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001902 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001903 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1904 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1905
1906 // Verify the file has been written.
1907 fid = fopen(filename.c_str(), "r");
1908 ASSERT_TRUE(fid != NULL);
1909
1910 // Clean it up.
1911 ASSERT_EQ(0, fclose(fid));
1912 ASSERT_EQ(0, remove(filename.c_str()));
1913#else
1914 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001915 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001916 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1917
1918 ASSERT_EQ(0, fclose(fid));
1919#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1920}
1921
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001922TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001923 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001924 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001925
1926 Config config;
1927 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001928 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001929 EnableAllComponents();
1930 EnableAllAPComponents(fapm.get());
1931 for (int i = 0; i < ref_data.test_size(); i++) {
1932 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1933
1934 audioproc::Test* test = ref_data.mutable_test(i);
1935 // TODO(ajm): Restore downmixing test cases.
1936 if (test->num_input_channels() != test->num_output_channels())
1937 continue;
1938
Peter Kasting69558702016-01-12 16:26:35 -08001939 const size_t num_render_channels =
1940 static_cast<size_t>(test->num_reverse_channels());
1941 const size_t num_input_channels =
1942 static_cast<size_t>(test->num_input_channels());
1943 const size_t num_output_channels =
1944 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001945 const size_t samples_per_channel = static_cast<size_t>(
1946 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001947
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001948 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1949 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001950 Init(fapm.get());
1951
1952 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001953 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1954 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001955
1956 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001957 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001958 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1959 ReadFrame(near_file_, frame_, float_cb_.get())) {
1960 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1961
aluebsb0319552016-03-17 20:39:53 -07001962 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001963 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1964 revfloat_cb_->channels(),
1965 samples_per_channel,
1966 test->sample_rate(),
1967 LayoutFromChannels(num_render_channels)));
1968
1969 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1970 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1971 apm_->echo_cancellation()->set_stream_drift_samples(0);
1972 fapm->echo_cancellation()->set_stream_drift_samples(0);
1973 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1974 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1975
1976 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001977 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
1978 output_int16.channels());
1979
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001980 EXPECT_NOERR(fapm->ProcessStream(
1981 float_cb_->channels(),
1982 samples_per_channel,
1983 test->sample_rate(),
1984 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001985 test->sample_rate(),
1986 LayoutFromChannels(num_output_channels),
1987 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001988 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001989 FloatToS16(float_cb_->channels()[j],
1990 samples_per_channel,
1991 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001992 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001993 float snr = ComputeSNR(output_int16.channels()[j],
1994 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001995 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001996
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001997 const float kVarianceThreshold = 20;
1998 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001999
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002000 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002001 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2002 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002003 }
2004 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002005
2006 analog_level = fapm->gain_control()->stream_analog_level();
2007 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2008 fapm->gain_control()->stream_analog_level());
2009 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2010 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002011 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2012 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002013 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002014
2015 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002016 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002017 }
aluebs776593b2016-03-15 14:04:58 -07002018
2019#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2020 const size_t kMaxNumBadChunks = 0;
2021#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2022 // There are a few chunks in the fixed-point profile that give low SNR.
2023 // Listening confirmed the difference is acceptable.
2024 const size_t kMaxNumBadChunks = 60;
2025#endif
2026 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2027
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002028 rewind(far_file_);
2029 rewind(near_file_);
2030 }
2031}
2032
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002033// TODO(andrew): Add a test to process a few frames with different combinations
2034// of enabled components.
2035
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002036TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002037 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002038 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002039
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002040 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002041 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002042 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002043 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002044 for (size_t i = 0; i < arraysize(kChannels); i++) {
2045 for (size_t j = 0; j < arraysize(kChannels); j++) {
2046 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002047 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002048 test->set_num_reverse_channels(kChannels[i]);
2049 test->set_num_input_channels(kChannels[j]);
2050 test->set_num_output_channels(kChannels[j]);
2051 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002052 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002053 }
2054 }
2055 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002056#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2057 // To test the extended filter mode.
2058 audioproc::Test* test = ref_data.add_test();
2059 test->set_num_reverse_channels(2);
2060 test->set_num_input_channels(2);
2061 test->set_num_output_channels(2);
2062 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2063 test->set_use_aec_extended_filter(true);
2064#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002065 }
2066
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002067 for (int i = 0; i < ref_data.test_size(); i++) {
2068 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002069
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002070 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002071 // TODO(ajm): We no longer allow different input and output channels. Skip
2072 // these tests for now, but they should be removed from the set.
2073 if (test->num_input_channels() != test->num_output_channels())
2074 continue;
2075
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002076 Config config;
2077 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002078 config.Set<ExtendedFilter>(
2079 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002080 apm_.reset(AudioProcessing::Create(config));
2081
2082 EnableAllComponents();
2083
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002084 Init(test->sample_rate(),
2085 test->sample_rate(),
2086 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002087 static_cast<size_t>(test->num_input_channels()),
2088 static_cast<size_t>(test->num_output_channels()),
2089 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002090 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002091
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002092 int frame_count = 0;
2093 int has_echo_count = 0;
2094 int has_voice_count = 0;
2095 int is_saturated_count = 0;
2096 int analog_level = 127;
2097 int analog_level_average = 0;
2098 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002099 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002100#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2101 int stats_index = 0;
2102#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002103
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002104 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002105 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002106
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002107 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2108
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002109 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002110 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002111 EXPECT_EQ(apm_->kNoError,
2112 apm_->gain_control()->set_stream_analog_level(analog_level));
2113
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002114 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002115
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002116 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002117 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2118 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002119
2120 max_output_average += MaxAudioFrame(*frame_);
2121
2122 if (apm_->echo_cancellation()->stream_has_echo()) {
2123 has_echo_count++;
2124 }
2125
2126 analog_level = apm_->gain_control()->stream_analog_level();
2127 analog_level_average += analog_level;
2128 if (apm_->gain_control()->stream_is_saturated()) {
2129 is_saturated_count++;
2130 }
2131 if (apm_->voice_detection()->stream_has_voice()) {
2132 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002133 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002134 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002135 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002136 }
2137
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002138 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2139
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002140 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002141 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002142 sizeof(int16_t),
2143 frame_size,
2144 out_file_);
2145 ASSERT_EQ(frame_size, write_count);
2146
2147 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002148 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002149 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002150
2151#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2152 const int kStatsAggregationFrameNum = 100; // 1 second.
2153 if (frame_count % kStatsAggregationFrameNum == 0) {
2154 // Get echo metrics.
2155 EchoCancellation::Metrics echo_metrics;
2156 EXPECT_EQ(apm_->kNoError,
2157 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2158
2159 // Get delay metrics.
2160 int median = 0;
2161 int std = 0;
2162 float fraction_poor_delays = 0;
2163 EXPECT_EQ(apm_->kNoError,
2164 apm_->echo_cancellation()->GetDelayMetrics(
2165 &median, &std, &fraction_poor_delays));
2166
2167 // Get RMS.
2168 int rms_level = apm_->level_estimator()->RMS();
2169 EXPECT_LE(0, rms_level);
2170 EXPECT_GE(127, rms_level);
2171
2172 if (!write_ref_data) {
2173 const audioproc::Test::EchoMetrics& reference =
2174 test->echo_metrics(stats_index);
2175 TestStats(echo_metrics.residual_echo_return_loss,
2176 reference.residual_echo_return_loss());
2177 TestStats(echo_metrics.echo_return_loss,
2178 reference.echo_return_loss());
2179 TestStats(echo_metrics.echo_return_loss_enhancement,
2180 reference.echo_return_loss_enhancement());
2181 TestStats(echo_metrics.a_nlp,
2182 reference.a_nlp());
2183 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2184 reference.divergent_filter_fraction());
2185
2186 const audioproc::Test::DelayMetrics& reference_delay =
2187 test->delay_metrics(stats_index);
2188 EXPECT_EQ(reference_delay.median(), median);
2189 EXPECT_EQ(reference_delay.std(), std);
2190 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2191 fraction_poor_delays);
2192
2193 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2194
2195 ++stats_index;
2196 } else {
2197 audioproc::Test::EchoMetrics* message =
2198 test->add_echo_metrics();
2199 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2200 message->mutable_residual_echo_return_loss());
2201 WriteStatsMessage(echo_metrics.echo_return_loss,
2202 message->mutable_echo_return_loss());
2203 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2204 message->mutable_echo_return_loss_enhancement());
2205 WriteStatsMessage(echo_metrics.a_nlp,
2206 message->mutable_a_nlp());
2207 message->set_divergent_filter_fraction(
2208 echo_metrics.divergent_filter_fraction);
2209
2210 audioproc::Test::DelayMetrics* message_delay =
2211 test->add_delay_metrics();
2212 message_delay->set_median(median);
2213 message_delay->set_std(std);
2214 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2215
2216 test->add_rms_level(rms_level);
2217 }
2218 }
2219#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002220 }
2221 max_output_average /= frame_count;
2222 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002223 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002224
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002225 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002226 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002227 // When running the test on a N7 we get a {2, 6} difference of
2228 // |has_voice_count| and |max_output_average| is up to 18 higher.
2229 // All numbers being consistently higher on N7 compare to ref_data.
2230 // TODO(bjornv): If we start getting more of these offsets on Android we
2231 // should consider a different approach. Either using one slack for all,
2232 // or generate a separate android reference.
2233#if defined(WEBRTC_ANDROID)
2234 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002235 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002236 const int kMaxOutputAverageOffset = 9;
2237 const int kMaxOutputAverageNear = 9;
2238#else
2239 const int kHasVoiceCountOffset = 0;
2240 const int kHasVoiceCountNear = kIntNear;
2241 const int kMaxOutputAverageOffset = 0;
2242 const int kMaxOutputAverageNear = kIntNear;
2243#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002244 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002245 EXPECT_NEAR(test->has_voice_count(),
2246 has_voice_count - kHasVoiceCountOffset,
2247 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002248 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002249
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002250 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002251 EXPECT_NEAR(test->max_output_average(),
2252 max_output_average - kMaxOutputAverageOffset,
2253 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002254#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002255 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002256 EXPECT_NEAR(test->ns_speech_probability_average(),
2257 ns_speech_prob_average,
2258 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002259#endif
2260 } else {
2261 test->set_has_echo_count(has_echo_count);
2262 test->set_has_voice_count(has_voice_count);
2263 test->set_is_saturated_count(is_saturated_count);
2264
2265 test->set_analog_level_average(analog_level_average);
2266 test->set_max_output_average(max_output_average);
2267
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002268#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002269 EXPECT_LE(0.0f, ns_speech_prob_average);
2270 EXPECT_GE(1.0f, ns_speech_prob_average);
2271 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002272#endif
2273 }
2274
2275 rewind(far_file_);
2276 rewind(near_file_);
2277 }
2278
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002279 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002280 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002281 }
2282}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002283
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002284TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2285 struct ChannelFormat {
2286 AudioProcessing::ChannelLayout in_layout;
2287 AudioProcessing::ChannelLayout out_layout;
2288 };
2289 ChannelFormat cf[] = {
2290 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2291 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2292 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2293 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002294
kwiberg62eaacf2016-02-17 06:39:05 -08002295 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002296 // Enable one component just to ensure some processing takes place.
2297 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002298 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002299 const int in_rate = 44100;
2300 const int out_rate = 48000;
2301 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2302 TotalChannelsFromLayout(cf[i].in_layout));
2303 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2304 ChannelsFromLayout(cf[i].out_layout));
2305
2306 // Run over a few chunks.
2307 for (int j = 0; j < 10; ++j) {
2308 EXPECT_NOERR(ap->ProcessStream(
2309 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002310 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002311 in_rate,
2312 cf[i].in_layout,
2313 out_rate,
2314 cf[i].out_layout,
2315 out_cb.channels()));
2316 }
2317 }
2318}
2319
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002320// Compares the reference and test arrays over a region around the expected
2321// delay. Finds the highest SNR in that region and adds the variance and squared
2322// error results to the supplied accumulators.
2323void UpdateBestSNR(const float* ref,
2324 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002325 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002326 int expected_delay,
2327 double* variance_acc,
2328 double* sq_error_acc) {
2329 double best_snr = std::numeric_limits<double>::min();
2330 double best_variance = 0;
2331 double best_sq_error = 0;
2332 // Search over a region of eight samples around the expected delay.
2333 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2334 ++delay) {
2335 double sq_error = 0;
2336 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002337 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002338 double error = test[i + delay] - ref[i];
2339 sq_error += error * error;
2340 variance += ref[i] * ref[i];
2341 }
2342
2343 if (sq_error == 0) {
2344 *variance_acc += variance;
2345 return;
2346 }
2347 double snr = variance / sq_error;
2348 if (snr > best_snr) {
2349 best_snr = snr;
2350 best_variance = variance;
2351 best_sq_error = sq_error;
2352 }
2353 }
2354
2355 *variance_acc += best_variance;
2356 *sq_error_acc += best_sq_error;
2357}
2358
2359// Used to test a multitude of sample rate and channel combinations. It works
2360// by first producing a set of reference files (in SetUpTestCase) that are
2361// assumed to be correct, as the used parameters are verified by other tests
2362// in this collection. Primarily the reference files are all produced at
2363// "native" rates which do not involve any resampling.
2364
2365// Each test pass produces an output file with a particular format. The output
2366// is matched against the reference file closest to its internal processing
2367// format. If necessary the output is resampled back to its process format.
2368// Due to the resampling distortion, we don't expect identical results, but
2369// enforce SNR thresholds which vary depending on the format. 0 is a special
2370// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002371typedef std::tr1::tuple<int, int, int, int, double, double>
2372 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002373class AudioProcessingTest
2374 : public testing::TestWithParam<AudioProcessingTestData> {
2375 public:
2376 AudioProcessingTest()
2377 : input_rate_(std::tr1::get<0>(GetParam())),
2378 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002379 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2380 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2381 expected_snr_(std::tr1::get<4>(GetParam())),
2382 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002383
2384 virtual ~AudioProcessingTest() {}
2385
2386 static void SetUpTestCase() {
2387 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002388 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002389 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002390 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2391 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2392 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002393 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002394 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2395 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2396 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002397 }
2398 }
2399 }
2400 }
2401
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002402 static void TearDownTestCase() {
2403 ClearTempFiles();
2404 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002405
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002406 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002407 // to a file specified with |output_file_prefix|. Both forward and reverse
2408 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409 static void ProcessFormat(int input_rate,
2410 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002411 int reverse_input_rate,
2412 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002413 size_t num_input_channels,
2414 size_t num_output_channels,
2415 size_t num_reverse_input_channels,
2416 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002417 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002418 Config config;
2419 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002420 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002421 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002422
ekmeyerson60d9b332015-08-14 10:35:55 -07002423 ProcessingConfig processing_config = {
2424 {{input_rate, num_input_channels},
2425 {output_rate, num_output_channels},
2426 {reverse_input_rate, num_reverse_input_channels},
2427 {reverse_output_rate, num_reverse_output_channels}}};
2428 ap->Initialize(processing_config);
2429
2430 FILE* far_file =
2431 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002433 FILE* out_file =
2434 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2435 reverse_input_rate, reverse_output_rate,
2436 num_input_channels, num_output_channels,
2437 num_reverse_input_channels,
2438 num_reverse_output_channels, kForward).c_str(),
2439 "wb");
2440 FILE* rev_out_file =
2441 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2442 reverse_input_rate, reverse_output_rate,
2443 num_input_channels, num_output_channels,
2444 num_reverse_input_channels,
2445 num_reverse_output_channels, kReverse).c_str(),
2446 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002447 ASSERT_TRUE(far_file != NULL);
2448 ASSERT_TRUE(near_file != NULL);
2449 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002450 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002451
2452 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2453 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002454 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2455 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002456 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2457 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002458 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2459 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002460
2461 // Temporary buffers.
2462 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002463 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2464 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002465 std::unique_ptr<float[]> float_data(new float[max_length]);
2466 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002467
2468 int analog_level = 127;
2469 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2470 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002471 EXPECT_NOERR(ap->ProcessReverseStream(
2472 rev_cb.channels(), processing_config.reverse_input_stream(),
2473 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002474
2475 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2476 ap->echo_cancellation()->set_stream_drift_samples(0);
2477 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2478
2479 EXPECT_NOERR(ap->ProcessStream(
2480 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002481 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002482 input_rate,
2483 LayoutFromChannels(num_input_channels),
2484 output_rate,
2485 LayoutFromChannels(num_output_channels),
2486 out_cb.channels()));
2487
ekmeyerson60d9b332015-08-14 10:35:55 -07002488 // Dump forward output to file.
2489 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002490 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002491 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002492
pkasting25702cb2016-01-08 13:50:27 -08002493 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002494 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002495 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002496
ekmeyerson60d9b332015-08-14 10:35:55 -07002497 // Dump reverse output to file.
2498 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2499 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002500 size_t rev_out_length =
2501 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002502
pkasting25702cb2016-01-08 13:50:27 -08002503 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002504 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2505 rev_out_file));
2506
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002507 analog_level = ap->gain_control()->stream_analog_level();
2508 }
2509 fclose(far_file);
2510 fclose(near_file);
2511 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002512 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002513 }
2514
2515 protected:
2516 int input_rate_;
2517 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002518 int reverse_input_rate_;
2519 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002520 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002521 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002522};
2523
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002524TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002525 struct ChannelFormat {
2526 int num_input;
2527 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002528 int num_reverse_input;
2529 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002530 };
2531 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002532 {1, 1, 1, 1},
2533 {1, 1, 2, 1},
2534 {2, 1, 1, 1},
2535 {2, 1, 2, 1},
2536 {2, 2, 1, 1},
2537 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002538 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002539
pkasting25702cb2016-01-08 13:50:27 -08002540 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002541 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2542 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2543 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002544
ekmeyerson60d9b332015-08-14 10:35:55 -07002545 // Verify output for both directions.
2546 std::vector<StreamDirection> stream_directions;
2547 stream_directions.push_back(kForward);
2548 stream_directions.push_back(kReverse);
2549 for (StreamDirection file_direction : stream_directions) {
2550 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2551 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2552 const int out_num =
2553 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2554 const double expected_snr =
2555 file_direction ? expected_reverse_snr_ : expected_snr_;
2556
2557 const int min_ref_rate = std::min(in_rate, out_rate);
2558 int ref_rate;
2559
2560 if (min_ref_rate > 32000) {
2561 ref_rate = 48000;
2562 } else if (min_ref_rate > 16000) {
2563 ref_rate = 32000;
2564 } else if (min_ref_rate > 8000) {
2565 ref_rate = 16000;
2566 } else {
2567 ref_rate = 8000;
2568 }
aluebs776593b2016-03-15 14:04:58 -07002569#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002570 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002571 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002572 }
2573#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002574 FILE* out_file = fopen(
2575 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2576 reverse_output_rate_, cf[i].num_input,
2577 cf[i].num_output, cf[i].num_reverse_input,
2578 cf[i].num_reverse_output, file_direction).c_str(),
2579 "rb");
2580 // The reference files always have matching input and output channels.
2581 FILE* ref_file = fopen(
2582 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2583 cf[i].num_output, cf[i].num_output,
2584 cf[i].num_reverse_output, cf[i].num_reverse_output,
2585 file_direction).c_str(),
2586 "rb");
2587 ASSERT_TRUE(out_file != NULL);
2588 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002589
pkasting25702cb2016-01-08 13:50:27 -08002590 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2591 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002592 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002593 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002594 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002595 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002596 // Data from the resampled output, in case the reference and output rates
2597 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002598 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002599
ekmeyerson60d9b332015-08-14 10:35:55 -07002600 PushResampler<float> resampler;
2601 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002602
ekmeyerson60d9b332015-08-14 10:35:55 -07002603 // Compute the resampling delay of the output relative to the reference,
2604 // to find the region over which we should search for the best SNR.
2605 float expected_delay_sec = 0;
2606 if (in_rate != ref_rate) {
2607 // Input resampling delay.
2608 expected_delay_sec +=
2609 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2610 }
2611 if (out_rate != ref_rate) {
2612 // Output resampling delay.
2613 expected_delay_sec +=
2614 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2615 // Delay of converting the output back to its processing rate for
2616 // testing.
2617 expected_delay_sec +=
2618 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2619 }
2620 int expected_delay =
2621 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002622
ekmeyerson60d9b332015-08-14 10:35:55 -07002623 double variance = 0;
2624 double sq_error = 0;
2625 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2626 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2627 float* out_ptr = out_data.get();
2628 if (out_rate != ref_rate) {
2629 // Resample the output back to its internal processing rate if
2630 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002631 ASSERT_EQ(ref_length,
2632 static_cast<size_t>(resampler.Resample(
2633 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002634 out_ptr = cmp_data.get();
2635 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002636
ekmeyerson60d9b332015-08-14 10:35:55 -07002637 // Update the |sq_error| and |variance| accumulators with the highest
2638 // SNR of reference vs output.
2639 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2640 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002641 }
2642
ekmeyerson60d9b332015-08-14 10:35:55 -07002643 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2644 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2645 << cf[i].num_input << ", " << cf[i].num_output << ", "
2646 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2647 << ", " << file_direction << "): ";
2648 if (sq_error > 0) {
2649 double snr = 10 * log10(variance / sq_error);
2650 EXPECT_GE(snr, expected_snr);
2651 EXPECT_NE(0, expected_snr);
2652 std::cout << "SNR=" << snr << " dB" << std::endl;
2653 } else {
aluebs776593b2016-03-15 14:04:58 -07002654 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002655 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002656
ekmeyerson60d9b332015-08-14 10:35:55 -07002657 fclose(out_file);
2658 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002659 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002660 }
2661}
2662
2663#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2664INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002665 CommonFormats,
2666 AudioProcessingTest,
2667 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002668 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2669 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002670 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2671 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2672 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2673 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2674 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2675 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2676 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2677 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2678 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002679
ekmeyerson60d9b332015-08-14 10:35:55 -07002680 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2681 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2682 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2683 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2684 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2685 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2686 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2687 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2688 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2689 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2690 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2691 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002692
ekmeyerson60d9b332015-08-14 10:35:55 -07002693 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2694 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2695 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2696 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2697 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2698 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2699 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2700 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2701 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2702 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2703 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2704 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002705
ekmeyerson60d9b332015-08-14 10:35:55 -07002706 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2707 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2708 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2709 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2710 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2711 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2712 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2713 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2714 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2715 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002716 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002717 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002718
2719#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2720INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002721 CommonFormats,
2722 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002723 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2724 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2725 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2726 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2727 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2728 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002729 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2730 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2731 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2732 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2733 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2734 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002735
aluebs776593b2016-03-15 14:04:58 -07002736 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2737 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2738 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002739 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2740 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2741 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2742 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2743 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2744 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2745 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2746 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2747 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002748
aluebs776593b2016-03-15 14:04:58 -07002749 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2750 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2751 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2752 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2753 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2754 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2755 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2756 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2757 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002758 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2759 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2760 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002761
ekmeyerson60d9b332015-08-14 10:35:55 -07002762 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2763 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2764 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2765 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2766 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2767 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2768 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2769 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2770 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2771 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002772 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002773 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002774#endif
2775
niklase@google.com470e71d2011-07-07 08:21:25 +00002776} // namespace
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002777} // namespace webrtc