blob: 139aa9daafa534d29a11e2e8bf53832609f0e24d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000011#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000012#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080013
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000014#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
pkasting25702cb2016-01-08 13:50:27 -080019#include "webrtc/base/arraysize.h"
kwiberg9e2be5f2016-09-14 05:23:22 -070020#include "webrtc/base/checks.h"
peahc19f3122016-10-07 14:54:10 -070021#include "webrtc/base/gtest_prod_util.h"
kwiberg77eab702016-09-28 17:42:01 -070022#include "webrtc/base/ignore_wundef.h"
andrew@webrtc.org27c69802014-02-18 20:24:56 +000023#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000024#include "webrtc/common_audio/resampler/include/push_resampler.h"
25#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000026#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
peahc19f3122016-10-07 14:54:10 -070027#include "webrtc/modules/audio_processing/audio_processing_impl.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000028#include "webrtc/modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000029#include "webrtc/modules/audio_processing/common.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000030#include "webrtc/modules/audio_processing/include/audio_processing.h"
peahc19f3122016-10-07 14:54:10 -070031#include "webrtc/modules/audio_processing/level_controller/level_controller_constants.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070032#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000033#include "webrtc/modules/audio_processing/test/test_utils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010034#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/event_wrapper.h"
36#include "webrtc/system_wrappers/include/trace.h"
kwiberg77eab702016-09-28 17:42:01 -070037#include "webrtc/test/gtest.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000038#include "webrtc/test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070039
40RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000041#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000042#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000043#else
kjellander78ddd732016-02-09 08:13:06 -080044#include "webrtc/modules/audio_processing/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000045#endif
kwiberg77eab702016-09-28 17:42:01 -070046RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000047
andrew@webrtc.org27c69802014-02-18 20:24:56 +000048namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000049namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000050
ekmeyerson60d9b332015-08-14 10:35:55 -070051// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
52// applicable.
53
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000054// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000055// re-implemented; see comment at the bottom of this file. For now, the user has
56// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000057// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000058// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000059// be set to true with the command-line switch --write_ref_data.
60bool write_ref_data = false;
pkasting25702cb2016-01-08 13:50:27 -080061const google::protobuf::int32 kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070062const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000063
aluebseb3603b2016-04-20 15:27:58 -070064#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
65// Android doesn't support 48kHz.
66const int kProcessSampleRates[] = {8000, 16000, 32000};
67#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070068const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070069#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000070
ekmeyerson60d9b332015-08-14 10:35:55 -070071enum StreamDirection { kForward = 0, kReverse };
72
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000073void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000074 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075 cb->num_channels());
76 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000077 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000078 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000079 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080080 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 S16ToFloat(cb_int.channels()[i],
82 cb->num_frames(),
83 cb->channels()[i]);
84 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000086
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000087void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
88 ConvertToFloat(frame.data_, cb);
89}
90
andrew@webrtc.org103657b2014-04-24 18:28:56 +000091// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080092size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000093 switch (layout) {
94 case AudioProcessing::kMono:
95 return 1;
96 case AudioProcessing::kMonoAndKeyboard:
97 case AudioProcessing::kStereo:
98 return 2;
99 case AudioProcessing::kStereoAndKeyboard:
100 return 3;
101 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700102 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800103 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000104}
105
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000106int TruncateToMultipleOf10(int value) {
107 return (value / 10) * 10;
108}
109
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000110void MixStereoToMono(const float* stereo, float* 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]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000114}
115
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800117 size_t samples_per_channel) {
118 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000119 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
120}
121
pkasting25702cb2016-01-08 13:50:27 -0800122void CopyLeftToRightChannel(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 stereo[i * 2 + 1] = stereo[i * 2];
125 }
126}
127
pkasting25702cb2016-01-08 13:50:27 -0800128void VerifyChannelsAreEqual(int16_t* stereo, size_t samples_per_channel) {
129 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000130 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
131 }
132}
133
134void SetFrameTo(AudioFrame* frame, int16_t value) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700135 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
136 ++i) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000137 frame->data_[i] = value;
138 }
139}
140
141void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800142 ASSERT_EQ(2u, frame->num_channels_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000144 frame->data_[i] = left;
145 frame->data_[i + 1] = right;
146 }
147}
148
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000149void ScaleFrame(AudioFrame* frame, float scale) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700150 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
151 ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000152 frame->data_[i] = FloatS16ToS16(frame->data_[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000153 }
154}
155
andrew@webrtc.org81865342012-10-27 00:28:27 +0000156bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000157 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000158 return false;
159 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000160 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000161 return false;
162 }
163 if (memcmp(frame1.data_, frame2.data_,
164 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000165 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166 return false;
167 }
168 return true;
169}
170
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000171void EnableAllAPComponents(AudioProcessing* ap) {
172#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
173 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
174
175 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
176 EXPECT_NOERR(ap->gain_control()->Enable(true));
177#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
178 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
179 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
180 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
181 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
182
183 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
184 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
185 EXPECT_NOERR(ap->gain_control()->Enable(true));
186#endif
187
188 EXPECT_NOERR(ap->high_pass_filter()->Enable(true));
189 EXPECT_NOERR(ap->level_estimator()->Enable(true));
190 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
191
192 EXPECT_NOERR(ap->voice_detection()->Enable(true));
193}
194
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000195// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000196template <class T>
197T AbsValue(T a) {
198 return a > 0 ? a: -a;
199}
200
201int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800202 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000203 int16_t max_data = AbsValue(frame.data_[0]);
pkasting25702cb2016-01-08 13:50:27 -0800204 for (size_t i = 1; i < length; i++) {
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000205 max_data = std::max(max_data, AbsValue(frame.data_[i]));
206 }
207
208 return max_data;
209}
210
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000211#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000212void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000213 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700214 EXPECT_EQ(reference.instant(), test.instant);
215 EXPECT_EQ(reference.average(), test.average);
216 EXPECT_EQ(reference.maximum(), test.maximum);
217 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000218}
219
220void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000221 audioproc::Test::Statistic* msg) {
222 msg->set_instant(output.instant);
223 msg->set_average(output.average);
224 msg->set_maximum(output.maximum);
225 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000226}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000227#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000228
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000229void OpenFileAndWriteMessage(const std::string filename,
230 const ::google::protobuf::MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000231 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000232 ASSERT_TRUE(file != NULL);
233
234 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000235 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800236 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000237 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000238
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000239 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242 fclose(file);
243}
244
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000245std::string ResourceFilePath(std::string name, int sample_rate_hz) {
246 std::ostringstream ss;
247 // Resource files are all stereo.
248 ss << name << sample_rate_hz / 1000 << "_stereo";
249 return test::ResourcePath(ss.str(), "pcm");
250}
251
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000252// Temporary filenames unique to this process. Used to be able to run these
253// tests in parallel as each process needs to be running in isolation they can't
254// have competing filenames.
255std::map<std::string, std::string> temp_filenames;
256
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000257std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000258 int input_rate,
259 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700260 int reverse_input_rate,
261 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800262 size_t num_input_channels,
263 size_t num_output_channels,
264 size_t num_reverse_input_channels,
265 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700266 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000267 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700268 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
269 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000270 if (num_output_channels == 1) {
271 ss << "mono";
272 } else if (num_output_channels == 2) {
273 ss << "stereo";
274 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700275 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000276 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700277 ss << output_rate / 1000;
278 if (num_reverse_output_channels == 1) {
279 ss << "_rmono";
280 } else if (num_reverse_output_channels == 2) {
281 ss << "_rstereo";
282 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700283 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700284 }
285 ss << reverse_output_rate / 1000;
286 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000287
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000288 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700289 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000290 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
291 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000292}
293
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000294void ClearTempFiles() {
295 for (auto& kv : temp_filenames)
296 remove(kv.second.c_str());
297}
298
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200299void OpenFileAndReadMessage(std::string filename,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000300 ::google::protobuf::MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000301 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000302 ASSERT_TRUE(file != NULL);
303 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000304 fclose(file);
305}
306
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000307// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
308// stereo) file, converts to deinterleaved float (optionally downmixing) and
309// returns the result in |cb|. Returns false if the file ended (or on error) and
310// true otherwise.
311//
312// |int_data| and |float_data| are just temporary space that must be
313// sufficiently large to hold the 10 ms chunk.
314bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
315 ChannelBuffer<float>* cb) {
316 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000317 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000318 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
319 if (read_count != frame_size) {
320 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700321 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000322 return false; // This is expected.
323 }
324
325 S16ToFloat(int_data, frame_size, float_data);
326 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000327 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000328 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000329 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000330 cb->channels());
331 }
332
333 return true;
334}
335
niklase@google.com470e71d2011-07-07 08:21:25 +0000336class ApmTest : public ::testing::Test {
337 protected:
338 ApmTest();
339 virtual void SetUp();
340 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000341
342 static void SetUpTestCase() {
343 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000344 }
345
346 static void TearDownTestCase() {
347 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000348 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000349 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000350
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000351 // Used to select between int and float interface tests.
352 enum Format {
353 kIntFormat,
354 kFloatFormat
355 };
356
357 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000358 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000359 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800360 size_t num_input_channels,
361 size_t num_output_channels,
362 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000363 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000364 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000365 void EnableAllComponents();
366 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000367 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000368 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000369 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
370 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000371 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000372 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
373 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700374 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800375 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700376 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800377 void TestChangingForwardChannels(size_t num_in_channels,
378 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700379 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800380 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700381 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000382 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
383 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000384 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000385 int ProcessStreamChooser(Format format);
386 int AnalyzeReverseStreamChooser(Format format);
387 void ProcessDebugDump(const std::string& in_filename,
388 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800389 Format format,
390 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000391 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000392
393 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000394 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800395 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000396 AudioFrame* frame_;
397 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800398 std::unique_ptr<ChannelBuffer<float> > float_cb_;
399 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000400 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800401 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000402 FILE* far_file_;
403 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000404 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000405};
406
407ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000408 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000409#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800410 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
411 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000412#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000413#if defined(WEBRTC_MAC)
414 // A different file for Mac is needed because on this platform the AEC
415 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800416 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
417 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000418#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800419 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
420 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000421#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000422#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000424 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000425 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000426 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000427 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000428 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000429 out_file_(NULL) {
430 Config config;
431 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
432 apm_.reset(AudioProcessing::Create(config));
433}
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
435void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000436 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000437
438 frame_ = new AudioFrame();
439 revframe_ = new AudioFrame();
440
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000441 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
444void ApmTest::TearDown() {
445 if (frame_) {
446 delete frame_;
447 }
448 frame_ = NULL;
449
450 if (revframe_) {
451 delete revframe_;
452 }
453 revframe_ = NULL;
454
455 if (far_file_) {
456 ASSERT_EQ(0, fclose(far_file_));
457 }
458 far_file_ = NULL;
459
460 if (near_file_) {
461 ASSERT_EQ(0, fclose(near_file_));
462 }
463 near_file_ = NULL;
464
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000465 if (out_file_) {
466 ASSERT_EQ(0, fclose(out_file_));
467 }
468 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000469}
470
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000471void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000472 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700473 ap->Initialize(
474 {{{frame_->sample_rate_hz_, frame_->num_channels_},
475 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700476 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700477 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000478}
479
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000480void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000481 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000482 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800483 size_t num_input_channels,
484 size_t num_output_channels,
485 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000486 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000487 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000488 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000489 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000491 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
492 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000493 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000494
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000495 if (far_file_) {
496 ASSERT_EQ(0, fclose(far_file_));
497 }
498 std::string filename = ResourceFilePath("far", sample_rate_hz);
499 far_file_ = fopen(filename.c_str(), "rb");
500 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
501 filename << "\n";
502
503 if (near_file_) {
504 ASSERT_EQ(0, fclose(near_file_));
505 }
506 filename = ResourceFilePath("near", sample_rate_hz);
507 near_file_ = fopen(filename.c_str(), "rb");
508 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
509 filename << "\n";
510
511 if (open_output_file) {
512 if (out_file_) {
513 ASSERT_EQ(0, fclose(out_file_));
514 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700515 filename = OutputFilePath(
516 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
517 reverse_sample_rate_hz, num_input_channels, num_output_channels,
518 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000519 out_file_ = fopen(filename.c_str(), "wb");
520 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
521 filename << "\n";
522 }
523}
524
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000525void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000526 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000527}
528
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000529bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
530 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000531 // The files always contain stereo audio.
532 size_t frame_size = frame->samples_per_channel_ * 2;
533 size_t read_count = fread(frame->data_,
534 sizeof(int16_t),
535 frame_size,
536 file);
537 if (read_count != frame_size) {
538 // Check that the file really ended.
539 EXPECT_NE(0, feof(file));
540 return false; // This is expected.
541 }
542
543 if (frame->num_channels_ == 1) {
544 MixStereoToMono(frame->data_, frame->data_,
545 frame->samples_per_channel_);
546 }
547
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000549 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000550 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000551 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000552}
553
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
555 return ReadFrame(file, frame, NULL);
556}
557
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000558// If the end of the file has been reached, rewind it and attempt to read the
559// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000560void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
561 ChannelBuffer<float>* cb) {
562 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000563 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000564 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000565 }
566}
567
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000568void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
569 ReadFrameWithRewind(file, frame, NULL);
570}
571
andrew@webrtc.org81865342012-10-27 00:28:27 +0000572void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
573 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000574 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000575 EXPECT_EQ(apm_->kNoError,
576 apm_->gain_control()->set_stream_analog_level(127));
577 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000578}
579
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000580int ApmTest::ProcessStreamChooser(Format format) {
581 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582 return apm_->ProcessStream(frame_);
583 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000584 return apm_->ProcessStream(float_cb_->channels(),
585 frame_->samples_per_channel_,
586 frame_->sample_rate_hz_,
587 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000588 output_sample_rate_hz_,
589 LayoutFromChannels(num_output_channels_),
590 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591}
592
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000593int ApmTest::AnalyzeReverseStreamChooser(Format format) {
594 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700595 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000596 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000597 return apm_->AnalyzeReverseStream(
598 revfloat_cb_->channels(),
599 revframe_->samples_per_channel_,
600 revframe_->sample_rate_hz_,
601 LayoutFromChannels(revframe_->num_channels_));
602}
603
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000604void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
605 int delay_min, int delay_max) {
606 // The |revframe_| and |frame_| should include the proper frame information,
607 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000608 AudioFrame tmp_frame;
609 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000610 bool causal = true;
611
612 tmp_frame.CopyFrom(*revframe_);
613 SetFrameTo(&tmp_frame, 0);
614
615 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
616 // Initialize the |frame_queue| with empty frames.
617 int frame_delay = delay_ms / 10;
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 causal = false;
624 }
625 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000626 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000627 frame->CopyFrom(tmp_frame);
628 frame_queue.push(frame);
629 frame_delay--;
630 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000631 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
632 // need enough frames with audio to have reliable estimates, but as few as
633 // possible to keep processing time down. 4.5 seconds seemed to be a good
634 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000635 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000636 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000637 frame->CopyFrom(tmp_frame);
638 // Use the near end recording, since that has more speech in it.
639 ASSERT_TRUE(ReadFrame(near_file_, frame));
640 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000641 AudioFrame* reverse_frame = frame;
642 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000643 if (!causal) {
644 reverse_frame = frame_queue.front();
645 // When we call ProcessStream() the frame is modified, so we can't use the
646 // pointer directly when things are non-causal. Use an intermediate frame
647 // and copy the data.
648 process_frame = &tmp_frame;
649 process_frame->CopyFrom(*frame);
650 }
aluebsb0319552016-03-17 20:39:53 -0700651 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000652 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
653 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
654 frame = frame_queue.front();
655 frame_queue.pop();
656 delete frame;
657
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000658 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000659 int median;
660 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000661 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 // Discard the first delay metrics to avoid convergence effects.
663 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000664 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
665 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000666 }
667 }
668
669 rewind(near_file_);
670 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000671 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000672 frame_queue.pop();
673 delete frame;
674 }
675 // Calculate expected delay estimate and acceptable regions. Further,
676 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 const size_t samples_per_ms =
678 std::min(static_cast<size_t>(16), frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000679 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
680 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 int expected_median_high = std::min(
682 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
683 delay_min),
684 delay_max);
685 int expected_median_low = std::min(
686 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
687 delay_min),
688 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000689 // Verify delay metrics.
690 int median;
691 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000692 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000693 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000694 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
695 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000696 EXPECT_GE(expected_median_high, median);
697 EXPECT_LE(expected_median_low, median);
698}
699
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000700void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000701 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000702 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000703
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000704 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
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 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000710 EXPECT_EQ(apm_->kNoError,
711 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000713 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000714 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000715
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716 // Other stream parameters set correctly.
717 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000718 EXPECT_EQ(apm_->kNoError,
719 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000720 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000721 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000724 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
725 EXPECT_EQ(apm_->kNoError,
726 apm_->echo_cancellation()->enable_drift_compensation(false));
727
728 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000729 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000731 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000732 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000733
734 // Resets after successful ProcessStream().
735 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000736 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000737 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000738 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000739
740 // Other stream parameters set correctly.
741 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
742 EXPECT_EQ(apm_->kNoError,
743 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000744 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000745 EXPECT_EQ(apm_->kNoError,
746 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000747 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000748 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000749 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
750
751 // -- Missing drift --
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 // Resets after successful ProcessStream().
756 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000757 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000758 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000759 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000760 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000761
762 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000763 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
764 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
765 EXPECT_EQ(apm_->kNoError,
766 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000767 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000768 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000770 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000772 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000774 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000775
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000776 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000777 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000778 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000779 EXPECT_EQ(apm_->kNoError,
780 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000782}
783
784TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000785 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000786}
787
788TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000789 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000790}
791
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000792TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
793 EXPECT_EQ(0, apm_->delay_offset_ms());
794 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
795 EXPECT_EQ(50, apm_->stream_delay_ms());
796}
797
798TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
799 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000800 apm_->set_delay_offset_ms(100);
801 EXPECT_EQ(100, apm_->delay_offset_ms());
802 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000803 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000804 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
805 EXPECT_EQ(200, apm_->stream_delay_ms());
806
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000807 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000808 apm_->set_delay_offset_ms(-50);
809 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000810 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
811 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000812 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
813 EXPECT_EQ(50, apm_->stream_delay_ms());
814}
815
Michael Graczyk86c6d332015-07-23 11:41:39 -0700816void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800817 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700818 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000819 frame_->num_channels_ = num_channels;
820 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700821 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000822}
823
Michael Graczyk86c6d332015-07-23 11:41:39 -0700824void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800825 size_t num_in_channels,
826 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700827 AudioProcessing::Error expected_return) {
828 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
829 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
830
831 EXPECT_EQ(expected_return,
832 apm_->ProcessStream(float_cb_->channels(), input_stream,
833 output_stream, float_cb_->channels()));
834}
835
836void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800837 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700838 AudioProcessing::Error expected_return) {
839 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700840 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
841 {output_sample_rate_hz_, apm_->num_output_channels()},
842 {frame_->sample_rate_hz_, num_rev_channels},
843 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700844
ekmeyerson60d9b332015-08-14 10:35:55 -0700845 EXPECT_EQ(
846 expected_return,
847 apm_->ProcessReverseStream(
848 float_cb_->channels(), processing_config.reverse_input_stream(),
849 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700850}
851
852TEST_F(ApmTest, ChannelsInt16Interface) {
853 // Testing number of invalid and valid channels.
854 Init(16000, 16000, 16000, 4, 4, 4, false);
855
856 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
857
Peter Kasting69558702016-01-12 16:26:35 -0800858 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700859 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000860 EXPECT_EQ(i, apm_->num_input_channels());
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000861 // We always force the number of reverse channels used for processing to 1.
Peter Kasting69558702016-01-12 16:26:35 -0800862 EXPECT_EQ(1u, apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000863 }
864}
865
Michael Graczyk86c6d332015-07-23 11:41:39 -0700866TEST_F(ApmTest, Channels) {
867 // Testing number of invalid and valid channels.
868 Init(16000, 16000, 16000, 4, 4, 4, false);
869
870 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
871 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
872
Peter Kasting69558702016-01-12 16:26:35 -0800873 for (size_t i = 1; i < 4; ++i) {
874 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700875 // Output channels much be one or match input channels.
876 if (j == 1 || i == j) {
877 TestChangingForwardChannels(i, j, kNoErr);
878 TestChangingReverseChannels(i, kNoErr);
879
880 EXPECT_EQ(i, apm_->num_input_channels());
881 EXPECT_EQ(j, apm_->num_output_channels());
882 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800883 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700884 } else {
885 TestChangingForwardChannels(i, j,
886 AudioProcessing::kBadNumberChannelsError);
887 }
888 }
889 }
890}
891
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000892TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000893 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000894 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000895 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000896 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700897 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800898 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000899 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000900 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 }
902}
903
niklase@google.com470e71d2011-07-07 08:21:25 +0000904TEST_F(ApmTest, EchoCancellation) {
905 EXPECT_EQ(apm_->kNoError,
906 apm_->echo_cancellation()->enable_drift_compensation(true));
907 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
908 EXPECT_EQ(apm_->kNoError,
909 apm_->echo_cancellation()->enable_drift_compensation(false));
910 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
911
niklase@google.com470e71d2011-07-07 08:21:25 +0000912 EchoCancellation::SuppressionLevel level[] = {
913 EchoCancellation::kLowSuppression,
914 EchoCancellation::kModerateSuppression,
915 EchoCancellation::kHighSuppression,
916 };
pkasting25702cb2016-01-08 13:50:27 -0800917 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 EXPECT_EQ(apm_->kNoError,
919 apm_->echo_cancellation()->set_suppression_level(level[i]));
920 EXPECT_EQ(level[i],
921 apm_->echo_cancellation()->suppression_level());
922 }
923
924 EchoCancellation::Metrics metrics;
925 EXPECT_EQ(apm_->kNotEnabledError,
926 apm_->echo_cancellation()->GetMetrics(&metrics));
927
ivoc3e9a5372016-10-28 07:55:33 -0700928 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
929 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
930
niklase@google.com470e71d2011-07-07 08:21:25 +0000931 EXPECT_EQ(apm_->kNoError,
932 apm_->echo_cancellation()->enable_metrics(true));
933 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
934 EXPECT_EQ(apm_->kNoError,
935 apm_->echo_cancellation()->enable_metrics(false));
936 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
937
ivoc48dfab52016-10-28 03:29:31 -0700938 EXPECT_EQ(apm_->kNoError,
939 apm_->echo_cancellation()->enable_delay_logging(true));
940 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
941 EXPECT_EQ(apm_->kNoError,
942 apm_->echo_cancellation()->enable_delay_logging(false));
943 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
944
ivoc3e9a5372016-10-28 07:55:33 -0700945 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
946 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
947
948 int median = 0;
949 int std = 0;
950 float poor_fraction = 0;
951 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
952 &median, &std, &poor_fraction));
953
niklase@google.com470e71d2011-07-07 08:21:25 +0000954 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
955 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
956 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
957 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000958
959 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
960 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
961 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
962 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
963 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
964 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000965}
966
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000967TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000968 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000969 // Enable AEC only.
970 EXPECT_EQ(apm_->kNoError,
971 apm_->echo_cancellation()->enable_drift_compensation(false));
972 EXPECT_EQ(apm_->kNoError,
973 apm_->echo_cancellation()->enable_metrics(false));
974 EXPECT_EQ(apm_->kNoError,
975 apm_->echo_cancellation()->enable_delay_logging(true));
976 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000977 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700978 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000979 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000980
981 // Internally in the AEC the amount of lookahead the delay estimation can
982 // handle is 15 blocks and the maximum delay is set to 60 blocks.
983 const int kLookaheadBlocks = 15;
984 const int kMaxDelayBlocks = 60;
985 // The AEC has a startup time before it actually starts to process. This
986 // procedure can flush the internal far-end buffer, which of course affects
987 // the delay estimation. Therefore, we set a system_delay high enough to
988 // avoid that. The smallest system_delay you can report without flushing the
989 // buffer is 66 ms in 8 kHz.
990 //
991 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
992 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
993 // delay estimation. This should be noted though. In case of test failure,
994 // this could be the cause.
995 const int kSystemDelayMs = 66;
996 // Test a couple of corner cases and verify that the estimated delay is
997 // within a valid region (set to +-1.5 blocks). Note that these cases are
998 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800999 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001000 Init(kProcessSampleRates[i],
1001 kProcessSampleRates[i],
1002 kProcessSampleRates[i],
1003 2,
1004 2,
1005 2,
1006 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001007 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001008 const int num_ms_per_block =
1009 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001010 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1011 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1012
1013 // 1) Verify correct delay estimate at lookahead boundary.
1014 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1015 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1016 delay_max_ms);
1017 // 2) A delay less than maximum lookahead should give an delay estimate at
1018 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1019 delay_ms -= 20;
1020 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1021 delay_max_ms);
1022 // 3) Three values around zero delay. Note that we need to compensate for
1023 // the fake system_delay.
1024 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1025 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1026 delay_max_ms);
1027 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1028 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1029 delay_max_ms);
1030 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1031 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1032 delay_max_ms);
1033 // 4) Verify correct delay estimate at maximum delay boundary.
1034 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1035 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1036 delay_max_ms);
1037 // 5) A delay above the maximum delay should give an estimate at the
1038 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1039 delay_ms += 20;
1040 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1041 delay_max_ms);
1042 }
1043}
1044
niklase@google.com470e71d2011-07-07 08:21:25 +00001045TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001046 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001047 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1049 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1050
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 // Toggle routing modes
1052 EchoControlMobile::RoutingMode mode[] = {
1053 EchoControlMobile::kQuietEarpieceOrHeadset,
1054 EchoControlMobile::kEarpiece,
1055 EchoControlMobile::kLoudEarpiece,
1056 EchoControlMobile::kSpeakerphone,
1057 EchoControlMobile::kLoudSpeakerphone,
1058 };
pkasting25702cb2016-01-08 13:50:27 -08001059 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 EXPECT_EQ(apm_->kNoError,
1061 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1062 EXPECT_EQ(mode[i],
1063 apm_->echo_control_mobile()->routing_mode());
1064 }
1065 // Turn comfort noise off/on
1066 EXPECT_EQ(apm_->kNoError,
1067 apm_->echo_control_mobile()->enable_comfort_noise(false));
1068 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1069 EXPECT_EQ(apm_->kNoError,
1070 apm_->echo_control_mobile()->enable_comfort_noise(true));
1071 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001072 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001073 const size_t echo_path_size =
1074 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001075 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1076 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001077 EXPECT_EQ(apm_->kNullPointerError,
1078 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1079 EXPECT_EQ(apm_->kNullPointerError,
1080 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1081 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001082 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001083 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001084 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001085 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001086 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001087 echo_path_in[i] = echo_path_out[i] + 1;
1088 }
1089 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001090 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001091 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001092 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1093 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001094 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001095 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1096 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001097 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001098 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1099 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001100
1101 // Process a few frames with NS in the default disabled state. This exercises
1102 // a different codepath than with it enabled.
1103 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1104 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1105 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1106 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1107
niklase@google.com470e71d2011-07-07 08:21:25 +00001108 // Turn AECM off
1109 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1110 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1111}
1112
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001113TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001114 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001115 EXPECT_EQ(apm_->kNoError,
1116 apm_->gain_control()->set_mode(
1117 apm_->gain_control()->mode()));
1118
1119 GainControl::Mode mode[] = {
1120 GainControl::kAdaptiveAnalog,
1121 GainControl::kAdaptiveDigital,
1122 GainControl::kFixedDigital
1123 };
pkasting25702cb2016-01-08 13:50:27 -08001124 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001125 EXPECT_EQ(apm_->kNoError,
1126 apm_->gain_control()->set_mode(mode[i]));
1127 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1128 }
1129 // Testing invalid target levels
1130 EXPECT_EQ(apm_->kBadParameterError,
1131 apm_->gain_control()->set_target_level_dbfs(-3));
1132 EXPECT_EQ(apm_->kBadParameterError,
1133 apm_->gain_control()->set_target_level_dbfs(-40));
1134 // Testing valid target levels
1135 EXPECT_EQ(apm_->kNoError,
1136 apm_->gain_control()->set_target_level_dbfs(
1137 apm_->gain_control()->target_level_dbfs()));
1138
1139 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001140 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001141 EXPECT_EQ(apm_->kNoError,
1142 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1143 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1144 }
1145
1146 // Testing invalid compression gains
1147 EXPECT_EQ(apm_->kBadParameterError,
1148 apm_->gain_control()->set_compression_gain_db(-1));
1149 EXPECT_EQ(apm_->kBadParameterError,
1150 apm_->gain_control()->set_compression_gain_db(100));
1151
1152 // Testing valid compression gains
1153 EXPECT_EQ(apm_->kNoError,
1154 apm_->gain_control()->set_compression_gain_db(
1155 apm_->gain_control()->compression_gain_db()));
1156
1157 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001158 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001159 EXPECT_EQ(apm_->kNoError,
1160 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1161 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1162 }
1163
1164 // Testing limiter off/on
1165 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1166 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1167 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1168 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1169
1170 // Testing invalid level limits
1171 EXPECT_EQ(apm_->kBadParameterError,
1172 apm_->gain_control()->set_analog_level_limits(-1, 512));
1173 EXPECT_EQ(apm_->kBadParameterError,
1174 apm_->gain_control()->set_analog_level_limits(100000, 512));
1175 EXPECT_EQ(apm_->kBadParameterError,
1176 apm_->gain_control()->set_analog_level_limits(512, -1));
1177 EXPECT_EQ(apm_->kBadParameterError,
1178 apm_->gain_control()->set_analog_level_limits(512, 100000));
1179 EXPECT_EQ(apm_->kBadParameterError,
1180 apm_->gain_control()->set_analog_level_limits(512, 255));
1181
1182 // Testing valid level limits
1183 EXPECT_EQ(apm_->kNoError,
1184 apm_->gain_control()->set_analog_level_limits(
1185 apm_->gain_control()->analog_level_minimum(),
1186 apm_->gain_control()->analog_level_maximum()));
1187
1188 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001189 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001190 EXPECT_EQ(apm_->kNoError,
1191 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1192 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1193 }
1194
1195 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001196 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001197 EXPECT_EQ(apm_->kNoError,
1198 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1199 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1200 }
1201
1202 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1203
1204 // Turn AGC off
1205 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1206 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1207}
1208
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001209void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001210 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001211 EXPECT_EQ(apm_->kNoError,
1212 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1213 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1214
1215 int out_analog_level = 0;
1216 for (int i = 0; i < 2000; ++i) {
1217 ReadFrameWithRewind(near_file_, frame_);
1218 // Ensure the audio is at a low level, so the AGC will try to increase it.
1219 ScaleFrame(frame_, 0.25);
1220
1221 // Always pass in the same volume.
1222 EXPECT_EQ(apm_->kNoError,
1223 apm_->gain_control()->set_stream_analog_level(100));
1224 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1225 out_analog_level = apm_->gain_control()->stream_analog_level();
1226 }
1227
1228 // Ensure the AGC is still able to reach the maximum.
1229 EXPECT_EQ(255, out_analog_level);
1230}
1231
1232// Verifies that despite volume slider quantization, the AGC can continue to
1233// increase its volume.
1234TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001235 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001236 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1237 }
1238}
1239
1240void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001241 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001242 EXPECT_EQ(apm_->kNoError,
1243 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1244 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1245
1246 int out_analog_level = 100;
1247 for (int i = 0; i < 1000; ++i) {
1248 ReadFrameWithRewind(near_file_, frame_);
1249 // Ensure the audio is at a low level, so the AGC will try to increase it.
1250 ScaleFrame(frame_, 0.25);
1251
1252 EXPECT_EQ(apm_->kNoError,
1253 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1254 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1255 out_analog_level = apm_->gain_control()->stream_analog_level();
1256 }
1257
1258 // Ensure the volume was raised.
1259 EXPECT_GT(out_analog_level, 100);
1260 int highest_level_reached = out_analog_level;
1261 // Simulate a user manual volume change.
1262 out_analog_level = 100;
1263
1264 for (int i = 0; i < 300; ++i) {
1265 ReadFrameWithRewind(near_file_, frame_);
1266 ScaleFrame(frame_, 0.25);
1267
1268 EXPECT_EQ(apm_->kNoError,
1269 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1270 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1271 out_analog_level = apm_->gain_control()->stream_analog_level();
1272 // Check that AGC respected the manually adjusted volume.
1273 EXPECT_LT(out_analog_level, highest_level_reached);
1274 }
1275 // Check that the volume was still raised.
1276 EXPECT_GT(out_analog_level, 100);
1277}
1278
1279TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001280 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001281 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1282 }
1283}
1284
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001285#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1286TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1287 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001288 const size_t kSamplesPerChannel =
1289 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001290 const size_t kNumInputChannels = 2;
1291 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001292 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001293 const float kScaleFactor = 0.25f;
1294 Config config;
1295 std::vector<webrtc::Point> geometry;
1296 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1297 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1298 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001299 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001300 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001301 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001302 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001303 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1304 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1305 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001306 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1307 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001308 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1309 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001310 std::string filename = ResourceFilePath("far", kSampleRateHz);
1311 FILE* far_file = fopen(filename.c_str(), "rb");
1312 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1313 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1314 const int kDefaultCompressionGain =
1315 apm->gain_control()->compression_gain_db();
1316 bool is_target = false;
1317 EXPECT_CALL(*beamformer, is_target_present())
1318 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001319 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001320 ASSERT_TRUE(ReadChunk(far_file,
1321 int_data.get(),
1322 float_data.get(),
1323 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001324 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001325 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001326 src_buf.channels()[j][k] *= kScaleFactor;
1327 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001328 }
1329 EXPECT_EQ(kNoErr,
1330 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001331 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001332 kSampleRateHz,
1333 LayoutFromChannels(src_buf.num_channels()),
1334 kSampleRateHz,
1335 LayoutFromChannels(dest_buf.num_channels()),
1336 dest_buf.channels()));
1337 }
1338 EXPECT_EQ(kDefaultVolume,
1339 apm->gain_control()->stream_analog_level());
1340 EXPECT_EQ(kDefaultCompressionGain,
1341 apm->gain_control()->compression_gain_db());
1342 rewind(far_file);
1343 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001344 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001345 ASSERT_TRUE(ReadChunk(far_file,
1346 int_data.get(),
1347 float_data.get(),
1348 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001349 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001350 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001351 src_buf.channels()[j][k] *= kScaleFactor;
1352 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001353 }
1354 EXPECT_EQ(kNoErr,
1355 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001356 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001357 kSampleRateHz,
1358 LayoutFromChannels(src_buf.num_channels()),
1359 kSampleRateHz,
1360 LayoutFromChannels(dest_buf.num_channels()),
1361 dest_buf.channels()));
1362 }
1363 EXPECT_LT(kDefaultVolume,
1364 apm->gain_control()->stream_analog_level());
1365 EXPECT_LT(kDefaultCompressionGain,
1366 apm->gain_control()->compression_gain_db());
1367 ASSERT_EQ(0, fclose(far_file));
1368}
1369#endif
1370
niklase@google.com470e71d2011-07-07 08:21:25 +00001371TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001372 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 NoiseSuppression::Level level[] = {
1374 NoiseSuppression::kLow,
1375 NoiseSuppression::kModerate,
1376 NoiseSuppression::kHigh,
1377 NoiseSuppression::kVeryHigh
1378 };
pkasting25702cb2016-01-08 13:50:27 -08001379 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001380 EXPECT_EQ(apm_->kNoError,
1381 apm_->noise_suppression()->set_level(level[i]));
1382 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1383 }
1384
andrew@webrtc.org648af742012-02-08 01:57:29 +00001385 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001386 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1387 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1388 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1389 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1390}
1391
1392TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001393 // Turn HP filter on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001394 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(true));
1395 EXPECT_TRUE(apm_->high_pass_filter()->is_enabled());
1396 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(false));
1397 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1398}
1399
1400TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001401 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001402 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001403 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001404
1405 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1406
1407 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1408 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1409
1410 // Run this test in wideband; in super-wb, the splitting filter distorts the
1411 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001412 frame_->samples_per_channel_ = 160;
1413 frame_->num_channels_ = 2;
1414 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001415
1416 // Min value if no frames have been processed.
1417 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1418
1419 // Min value on zero frames.
1420 SetFrameTo(frame_, 0);
1421 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1422 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1423 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1424
1425 // Try a few RMS values.
1426 // (These also test that the value resets after retrieving it.)
1427 SetFrameTo(frame_, 32767);
1428 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1429 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1430 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1431
1432 SetFrameTo(frame_, 30000);
1433 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1434 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1435 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1436
1437 SetFrameTo(frame_, 10000);
1438 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1439 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1440 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1441
1442 SetFrameTo(frame_, 10);
1443 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1444 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1445 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1446
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001447 // Verify reset after enable/disable.
1448 SetFrameTo(frame_, 32767);
1449 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1450 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1451 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1452 SetFrameTo(frame_, 1);
1453 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1454 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1455
1456 // Verify reset after initialize.
1457 SetFrameTo(frame_, 32767);
1458 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1459 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1460 SetFrameTo(frame_, 1);
1461 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1462 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001463}
1464
1465TEST_F(ApmTest, VoiceDetection) {
1466 // Test external VAD
1467 EXPECT_EQ(apm_->kNoError,
1468 apm_->voice_detection()->set_stream_has_voice(true));
1469 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1470 EXPECT_EQ(apm_->kNoError,
1471 apm_->voice_detection()->set_stream_has_voice(false));
1472 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1473
andrew@webrtc.org648af742012-02-08 01:57:29 +00001474 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001475 VoiceDetection::Likelihood likelihood[] = {
1476 VoiceDetection::kVeryLowLikelihood,
1477 VoiceDetection::kLowLikelihood,
1478 VoiceDetection::kModerateLikelihood,
1479 VoiceDetection::kHighLikelihood
1480 };
pkasting25702cb2016-01-08 13:50:27 -08001481 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001482 EXPECT_EQ(apm_->kNoError,
1483 apm_->voice_detection()->set_likelihood(likelihood[i]));
1484 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1485 }
1486
1487 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001488 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001489 EXPECT_EQ(apm_->kBadParameterError,
1490 apm_->voice_detection()->set_frame_size_ms(12));
1491
andrew@webrtc.org648af742012-02-08 01:57:29 +00001492 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001493 for (int i = 10; i <= 30; i += 10) {
1494 EXPECT_EQ(apm_->kNoError,
1495 apm_->voice_detection()->set_frame_size_ms(i));
1496 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1497 }
1498 */
1499
andrew@webrtc.org648af742012-02-08 01:57:29 +00001500 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001501 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1502 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1503 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1504 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1505
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001506 // Test that AudioFrame activity is maintained when VAD is disabled.
1507 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1508 AudioFrame::VADActivity activity[] = {
1509 AudioFrame::kVadActive,
1510 AudioFrame::kVadPassive,
1511 AudioFrame::kVadUnknown
1512 };
pkasting25702cb2016-01-08 13:50:27 -08001513 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001514 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001515 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001516 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001517 }
1518
1519 // Test that AudioFrame activity is set when VAD is enabled.
1520 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001521 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001522 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001523 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001524
niklase@google.com470e71d2011-07-07 08:21:25 +00001525 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1526}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001527
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001528TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1529 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1530 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1531 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1532 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1533 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1534 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1535 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1536}
1537
1538TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001539 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001540 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001541 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001542 AudioFrame frame_copy;
1543 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001544 for (int j = 0; j < 1000; j++) {
1545 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1546 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001547 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1548 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001549 }
1550 }
1551}
1552
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001553TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1554 // Test that ProcessStream copies input to output even with no processing.
1555 const size_t kSamples = 80;
1556 const int sample_rate = 8000;
1557 const float src[kSamples] = {
1558 -1.0f, 0.0f, 1.0f
1559 };
1560 float dest[kSamples] = {};
1561
1562 auto src_channels = &src[0];
1563 auto dest_channels = &dest[0];
1564
1565 apm_.reset(AudioProcessing::Create());
1566 EXPECT_NOERR(apm_->ProcessStream(
1567 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1568 sample_rate, LayoutFromChannels(1), &dest_channels));
1569
1570 for (size_t i = 0; i < kSamples; ++i) {
1571 EXPECT_EQ(src[i], dest[i]);
1572 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001573
1574 // Same for ProcessReverseStream.
1575 float rev_dest[kSamples] = {};
1576 auto rev_dest_channels = &rev_dest[0];
1577
1578 StreamConfig input_stream = {sample_rate, 1};
1579 StreamConfig output_stream = {sample_rate, 1};
1580 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1581 output_stream, &rev_dest_channels));
1582
1583 for (size_t i = 0; i < kSamples; ++i) {
1584 EXPECT_EQ(src[i], rev_dest[i]);
1585 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001586}
1587
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001588TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1589 EnableAllComponents();
1590
pkasting25702cb2016-01-08 13:50:27 -08001591 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001592 Init(kProcessSampleRates[i],
1593 kProcessSampleRates[i],
1594 kProcessSampleRates[i],
1595 2,
1596 2,
1597 2,
1598 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001599 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001600 ASSERT_EQ(0, feof(far_file_));
1601 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001602 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001603 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1604
aluebsb0319552016-03-17 20:39:53 -07001605 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001606
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001607 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1608 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1609
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001610 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001611 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001612 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001613 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001614 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001615 analog_level = apm_->gain_control()->stream_analog_level();
1616
1617 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1618 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001619 rewind(far_file_);
1620 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001621 }
1622}
1623
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001624TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001625 // Verify the filter is not active through undistorted audio when:
1626 // 1. No components are enabled...
1627 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001628 AudioFrame frame_copy;
1629 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001630 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1631 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1632 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1633
1634 // 2. Only the level estimator is enabled...
1635 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001636 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001637 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1638 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1639 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1640 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1641 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1642
1643 // 3. Only VAD is enabled...
1644 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001645 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001646 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1647 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1648 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1649 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1650 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1651
1652 // 4. Both VAD and the level estimator are enabled...
1653 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001654 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001655 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1656 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1658 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1659 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1660 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1661 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1662
1663 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001664 frame_->samples_per_channel_ = 160;
1665 frame_->num_channels_ = 2;
1666 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001667 // Enable AEC, which would require the filter in super-wb. We rely on the
1668 // first few frames of data being unaffected by the AEC.
1669 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1670 // behavior of the AEC. Think of something more robust.
1671 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001672 // Make sure we have extended filter enabled. This makes sure nothing is
1673 // touched until we have a farend frame.
1674 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001675 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001676 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001677 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001678 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001679 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001680 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001681 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1682 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_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1686
1687 // Check the test is valid. We should have distortion from the filter
1688 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001689 frame_->samples_per_channel_ = 320;
1690 frame_->num_channels_ = 2;
1691 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001692 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001693 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001694 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001695 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001696 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1697 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1698}
1699
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001700#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1701void ApmTest::ProcessDebugDump(const std::string& in_filename,
1702 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001703 Format format,
1704 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001705 FILE* in_file = fopen(in_filename.c_str(), "rb");
1706 ASSERT_TRUE(in_file != NULL);
1707 audioproc::Event event_msg;
1708 bool first_init = true;
1709
1710 while (ReadMessageFromFile(in_file, &event_msg)) {
1711 if (event_msg.type() == audioproc::Event::INIT) {
1712 const audioproc::Init msg = event_msg.init();
1713 int reverse_sample_rate = msg.sample_rate();
1714 if (msg.has_reverse_sample_rate()) {
1715 reverse_sample_rate = msg.reverse_sample_rate();
1716 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001717 int output_sample_rate = msg.sample_rate();
1718 if (msg.has_output_sample_rate()) {
1719 output_sample_rate = msg.output_sample_rate();
1720 }
1721
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001722 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001723 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001724 reverse_sample_rate,
1725 msg.num_input_channels(),
1726 msg.num_output_channels(),
1727 msg.num_reverse_channels(),
1728 false);
1729 if (first_init) {
1730 // StartDebugRecording() writes an additional init message. Don't start
1731 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001732 EXPECT_NOERR(
1733 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001734 first_init = false;
1735 }
1736
1737 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1738 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1739
1740 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001741 ASSERT_EQ(revframe_->num_channels_,
1742 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001743 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001744 memcpy(revfloat_cb_->channels()[i],
1745 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001746 msg.channel(i).size());
1747 }
1748 } else {
1749 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1750 if (format == kFloatFormat) {
1751 // We're using an int16 input file; convert to float.
1752 ConvertToFloat(*revframe_, revfloat_cb_.get());
1753 }
1754 }
1755 AnalyzeReverseStreamChooser(format);
1756
1757 } else if (event_msg.type() == audioproc::Event::STREAM) {
1758 const audioproc::Stream msg = event_msg.stream();
1759 // ProcessStream could have changed this for the output frame.
1760 frame_->num_channels_ = apm_->num_input_channels();
1761
1762 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1763 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1764 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1765 if (msg.has_keypress()) {
1766 apm_->set_stream_key_pressed(msg.keypress());
1767 } else {
1768 apm_->set_stream_key_pressed(true);
1769 }
1770
1771 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001772 ASSERT_EQ(frame_->num_channels_,
1773 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001774 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001775 memcpy(float_cb_->channels()[i],
1776 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001777 msg.input_channel(i).size());
1778 }
1779 } else {
1780 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1781 if (format == kFloatFormat) {
1782 // We're using an int16 input file; convert to float.
1783 ConvertToFloat(*frame_, float_cb_.get());
1784 }
1785 }
1786 ProcessStreamChooser(format);
1787 }
1788 }
1789 EXPECT_NOERR(apm_->StopDebugRecording());
1790 fclose(in_file);
1791}
1792
1793void ApmTest::VerifyDebugDumpTest(Format format) {
1794 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001795 std::string format_string;
1796 switch (format) {
1797 case kIntFormat:
1798 format_string = "_int";
1799 break;
1800 case kFloatFormat:
1801 format_string = "_float";
1802 break;
1803 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001804 const std::string ref_filename = test::TempFilename(
1805 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1806 const std::string out_filename = test::TempFilename(
1807 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001808 const std::string limited_filename = test::TempFilename(
1809 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1810 const size_t logging_limit_bytes = 100000;
1811 // We expect at least this many bytes in the created logfile.
1812 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001813 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001814 ProcessDebugDump(in_filename, ref_filename, format, -1);
1815 ProcessDebugDump(ref_filename, out_filename, format, -1);
1816 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001817
1818 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1819 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001820 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001821 ASSERT_TRUE(ref_file != NULL);
1822 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001823 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001824 std::unique_ptr<uint8_t[]> ref_bytes;
1825 std::unique_ptr<uint8_t[]> out_bytes;
1826 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001827
1828 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1829 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001830 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001831 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001832 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001833 while (ref_size > 0 && out_size > 0) {
1834 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001835 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001836 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001837 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001838 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001839 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001840 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1841 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001842 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001843 }
1844 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001845 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1846 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001847 EXPECT_NE(0, feof(ref_file));
1848 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001849 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001850 ASSERT_EQ(0, fclose(ref_file));
1851 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001852 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001853 remove(ref_filename.c_str());
1854 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001855 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001856}
1857
pbosc7a65692016-05-06 12:50:04 -07001858TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001859 VerifyDebugDumpTest(kIntFormat);
1860}
1861
pbosc7a65692016-05-06 12:50:04 -07001862TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001863 VerifyDebugDumpTest(kFloatFormat);
1864}
1865#endif
1866
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001867// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001868TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001869 const std::string filename =
1870 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001871 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001872 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001873
1874#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1875 // Stopping without having started should be OK.
1876 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1877
ivocd66b44d2016-01-15 03:06:36 -08001878 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001879 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001880 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001881 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1882
1883 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001884 FILE* fid = fopen(filename.c_str(), "r");
1885 ASSERT_TRUE(fid != NULL);
1886
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001887 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001888 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001889 ASSERT_EQ(0, remove(filename.c_str()));
1890#else
1891 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001892 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001893 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1894
1895 // Verify the file has NOT been written.
1896 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1897#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1898}
1899
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001900// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001901TEST_F(ApmTest, DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001902 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001903 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001904 const std::string filename =
1905 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001906 fid = fopen(filename.c_str(), "w");
1907 ASSERT_TRUE(fid);
1908
1909#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1910 // Stopping without having started should be OK.
1911 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1912
ivocd66b44d2016-01-15 03:06:36 -08001913 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001914 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001915 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1916 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1917
1918 // Verify the file has been written.
1919 fid = fopen(filename.c_str(), "r");
1920 ASSERT_TRUE(fid != NULL);
1921
1922 // Clean it up.
1923 ASSERT_EQ(0, fclose(fid));
1924 ASSERT_EQ(0, remove(filename.c_str()));
1925#else
1926 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001927 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001928 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1929
1930 ASSERT_EQ(0, fclose(fid));
1931#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1932}
1933
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001934TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001935 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001936 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001937
1938 Config config;
1939 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001940 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001941 EnableAllComponents();
1942 EnableAllAPComponents(fapm.get());
1943 for (int i = 0; i < ref_data.test_size(); i++) {
1944 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1945
1946 audioproc::Test* test = ref_data.mutable_test(i);
1947 // TODO(ajm): Restore downmixing test cases.
1948 if (test->num_input_channels() != test->num_output_channels())
1949 continue;
1950
Peter Kasting69558702016-01-12 16:26:35 -08001951 const size_t num_render_channels =
1952 static_cast<size_t>(test->num_reverse_channels());
1953 const size_t num_input_channels =
1954 static_cast<size_t>(test->num_input_channels());
1955 const size_t num_output_channels =
1956 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001957 const size_t samples_per_channel = static_cast<size_t>(
1958 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001959
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001960 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1961 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001962 Init(fapm.get());
1963
1964 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001965 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1966 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001967
1968 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001969 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001970 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1971 ReadFrame(near_file_, frame_, float_cb_.get())) {
1972 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1973
aluebsb0319552016-03-17 20:39:53 -07001974 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001975 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1976 revfloat_cb_->channels(),
1977 samples_per_channel,
1978 test->sample_rate(),
1979 LayoutFromChannels(num_render_channels)));
1980
1981 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1982 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1983 apm_->echo_cancellation()->set_stream_drift_samples(0);
1984 fapm->echo_cancellation()->set_stream_drift_samples(0);
1985 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1986 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1987
1988 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001989 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
1990 output_int16.channels());
1991
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001992 EXPECT_NOERR(fapm->ProcessStream(
1993 float_cb_->channels(),
1994 samples_per_channel,
1995 test->sample_rate(),
1996 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001997 test->sample_rate(),
1998 LayoutFromChannels(num_output_channels),
1999 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002000 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002001 FloatToS16(float_cb_->channels()[j],
2002 samples_per_channel,
2003 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002004 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002005 float snr = ComputeSNR(output_int16.channels()[j],
2006 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002007 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002008
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002009 const float kVarianceThreshold = 20;
2010 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002011
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002012 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002013 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2014 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002015 }
2016 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002017
2018 analog_level = fapm->gain_control()->stream_analog_level();
2019 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2020 fapm->gain_control()->stream_analog_level());
2021 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2022 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002023 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2024 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002025 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002026
2027 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002028 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002029 }
aluebs776593b2016-03-15 14:04:58 -07002030
2031#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2032 const size_t kMaxNumBadChunks = 0;
2033#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2034 // There are a few chunks in the fixed-point profile that give low SNR.
2035 // Listening confirmed the difference is acceptable.
2036 const size_t kMaxNumBadChunks = 60;
2037#endif
2038 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2039
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002040 rewind(far_file_);
2041 rewind(near_file_);
2042 }
2043}
2044
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002045// TODO(andrew): Add a test to process a few frames with different combinations
2046// of enabled components.
2047
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002048TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002049 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002050 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002051
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002052 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002053 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002054 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002055 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002056 for (size_t i = 0; i < arraysize(kChannels); i++) {
2057 for (size_t j = 0; j < arraysize(kChannels); j++) {
2058 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002059 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002060 test->set_num_reverse_channels(kChannels[i]);
2061 test->set_num_input_channels(kChannels[j]);
2062 test->set_num_output_channels(kChannels[j]);
2063 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002064 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002065 }
2066 }
2067 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002068#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2069 // To test the extended filter mode.
2070 audioproc::Test* test = ref_data.add_test();
2071 test->set_num_reverse_channels(2);
2072 test->set_num_input_channels(2);
2073 test->set_num_output_channels(2);
2074 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2075 test->set_use_aec_extended_filter(true);
2076#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002077 }
2078
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002079 for (int i = 0; i < ref_data.test_size(); i++) {
2080 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002081
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002082 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002083 // TODO(ajm): We no longer allow different input and output channels. Skip
2084 // these tests for now, but they should be removed from the set.
2085 if (test->num_input_channels() != test->num_output_channels())
2086 continue;
2087
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002088 Config config;
2089 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002090 config.Set<ExtendedFilter>(
2091 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002092 apm_.reset(AudioProcessing::Create(config));
2093
2094 EnableAllComponents();
2095
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002096 Init(test->sample_rate(),
2097 test->sample_rate(),
2098 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002099 static_cast<size_t>(test->num_input_channels()),
2100 static_cast<size_t>(test->num_output_channels()),
2101 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002102 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002103
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002104 int frame_count = 0;
2105 int has_echo_count = 0;
2106 int has_voice_count = 0;
2107 int is_saturated_count = 0;
2108 int analog_level = 127;
2109 int analog_level_average = 0;
2110 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002111 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002112#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2113 int stats_index = 0;
2114#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002115
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002116 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002117 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002118
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002119 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2120
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002121 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002122 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002123 EXPECT_EQ(apm_->kNoError,
2124 apm_->gain_control()->set_stream_analog_level(analog_level));
2125
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002126 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002127
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002128 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002129 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2130 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002131
2132 max_output_average += MaxAudioFrame(*frame_);
2133
2134 if (apm_->echo_cancellation()->stream_has_echo()) {
2135 has_echo_count++;
2136 }
2137
2138 analog_level = apm_->gain_control()->stream_analog_level();
2139 analog_level_average += analog_level;
2140 if (apm_->gain_control()->stream_is_saturated()) {
2141 is_saturated_count++;
2142 }
2143 if (apm_->voice_detection()->stream_has_voice()) {
2144 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002145 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002146 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002147 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002148 }
2149
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002150 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2151
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002152 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002153 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002154 sizeof(int16_t),
2155 frame_size,
2156 out_file_);
2157 ASSERT_EQ(frame_size, write_count);
2158
2159 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002160 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002161 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002162
2163#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2164 const int kStatsAggregationFrameNum = 100; // 1 second.
2165 if (frame_count % kStatsAggregationFrameNum == 0) {
2166 // Get echo metrics.
2167 EchoCancellation::Metrics echo_metrics;
2168 EXPECT_EQ(apm_->kNoError,
2169 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2170
2171 // Get delay metrics.
2172 int median = 0;
2173 int std = 0;
2174 float fraction_poor_delays = 0;
2175 EXPECT_EQ(apm_->kNoError,
2176 apm_->echo_cancellation()->GetDelayMetrics(
2177 &median, &std, &fraction_poor_delays));
2178
2179 // Get RMS.
2180 int rms_level = apm_->level_estimator()->RMS();
2181 EXPECT_LE(0, rms_level);
2182 EXPECT_GE(127, rms_level);
2183
2184 if (!write_ref_data) {
2185 const audioproc::Test::EchoMetrics& reference =
2186 test->echo_metrics(stats_index);
2187 TestStats(echo_metrics.residual_echo_return_loss,
2188 reference.residual_echo_return_loss());
2189 TestStats(echo_metrics.echo_return_loss,
2190 reference.echo_return_loss());
2191 TestStats(echo_metrics.echo_return_loss_enhancement,
2192 reference.echo_return_loss_enhancement());
2193 TestStats(echo_metrics.a_nlp,
2194 reference.a_nlp());
2195 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2196 reference.divergent_filter_fraction());
2197
2198 const audioproc::Test::DelayMetrics& reference_delay =
2199 test->delay_metrics(stats_index);
2200 EXPECT_EQ(reference_delay.median(), median);
2201 EXPECT_EQ(reference_delay.std(), std);
2202 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2203 fraction_poor_delays);
2204
2205 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2206
2207 ++stats_index;
2208 } else {
2209 audioproc::Test::EchoMetrics* message =
2210 test->add_echo_metrics();
2211 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2212 message->mutable_residual_echo_return_loss());
2213 WriteStatsMessage(echo_metrics.echo_return_loss,
2214 message->mutable_echo_return_loss());
2215 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2216 message->mutable_echo_return_loss_enhancement());
2217 WriteStatsMessage(echo_metrics.a_nlp,
2218 message->mutable_a_nlp());
2219 message->set_divergent_filter_fraction(
2220 echo_metrics.divergent_filter_fraction);
2221
2222 audioproc::Test::DelayMetrics* message_delay =
2223 test->add_delay_metrics();
2224 message_delay->set_median(median);
2225 message_delay->set_std(std);
2226 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2227
2228 test->add_rms_level(rms_level);
2229 }
2230 }
2231#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002232 }
2233 max_output_average /= frame_count;
2234 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002235 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002236
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002237 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002238 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002239 // When running the test on a N7 we get a {2, 6} difference of
2240 // |has_voice_count| and |max_output_average| is up to 18 higher.
2241 // All numbers being consistently higher on N7 compare to ref_data.
2242 // TODO(bjornv): If we start getting more of these offsets on Android we
2243 // should consider a different approach. Either using one slack for all,
2244 // or generate a separate android reference.
2245#if defined(WEBRTC_ANDROID)
2246 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002247 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002248 const int kMaxOutputAverageOffset = 9;
2249 const int kMaxOutputAverageNear = 9;
2250#else
2251 const int kHasVoiceCountOffset = 0;
2252 const int kHasVoiceCountNear = kIntNear;
2253 const int kMaxOutputAverageOffset = 0;
2254 const int kMaxOutputAverageNear = kIntNear;
2255#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002256 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002257 EXPECT_NEAR(test->has_voice_count(),
2258 has_voice_count - kHasVoiceCountOffset,
2259 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002260 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002261
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002262 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002263 EXPECT_NEAR(test->max_output_average(),
2264 max_output_average - kMaxOutputAverageOffset,
2265 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002266#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002267 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002268 EXPECT_NEAR(test->ns_speech_probability_average(),
2269 ns_speech_prob_average,
2270 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002271#endif
2272 } else {
2273 test->set_has_echo_count(has_echo_count);
2274 test->set_has_voice_count(has_voice_count);
2275 test->set_is_saturated_count(is_saturated_count);
2276
2277 test->set_analog_level_average(analog_level_average);
2278 test->set_max_output_average(max_output_average);
2279
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002280#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002281 EXPECT_LE(0.0f, ns_speech_prob_average);
2282 EXPECT_GE(1.0f, ns_speech_prob_average);
2283 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002284#endif
2285 }
2286
2287 rewind(far_file_);
2288 rewind(near_file_);
2289 }
2290
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002291 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002292 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002293 }
2294}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002295
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002296TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2297 struct ChannelFormat {
2298 AudioProcessing::ChannelLayout in_layout;
2299 AudioProcessing::ChannelLayout out_layout;
2300 };
2301 ChannelFormat cf[] = {
2302 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2303 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2304 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2305 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002306
kwiberg62eaacf2016-02-17 06:39:05 -08002307 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002308 // Enable one component just to ensure some processing takes place.
2309 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002310 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002311 const int in_rate = 44100;
2312 const int out_rate = 48000;
2313 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2314 TotalChannelsFromLayout(cf[i].in_layout));
2315 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2316 ChannelsFromLayout(cf[i].out_layout));
2317
2318 // Run over a few chunks.
2319 for (int j = 0; j < 10; ++j) {
2320 EXPECT_NOERR(ap->ProcessStream(
2321 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002322 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002323 in_rate,
2324 cf[i].in_layout,
2325 out_rate,
2326 cf[i].out_layout,
2327 out_cb.channels()));
2328 }
2329 }
2330}
2331
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002332// Compares the reference and test arrays over a region around the expected
2333// delay. Finds the highest SNR in that region and adds the variance and squared
2334// error results to the supplied accumulators.
2335void UpdateBestSNR(const float* ref,
2336 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002337 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002338 int expected_delay,
2339 double* variance_acc,
2340 double* sq_error_acc) {
2341 double best_snr = std::numeric_limits<double>::min();
2342 double best_variance = 0;
2343 double best_sq_error = 0;
2344 // Search over a region of eight samples around the expected delay.
2345 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2346 ++delay) {
2347 double sq_error = 0;
2348 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002349 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002350 double error = test[i + delay] - ref[i];
2351 sq_error += error * error;
2352 variance += ref[i] * ref[i];
2353 }
2354
2355 if (sq_error == 0) {
2356 *variance_acc += variance;
2357 return;
2358 }
2359 double snr = variance / sq_error;
2360 if (snr > best_snr) {
2361 best_snr = snr;
2362 best_variance = variance;
2363 best_sq_error = sq_error;
2364 }
2365 }
2366
2367 *variance_acc += best_variance;
2368 *sq_error_acc += best_sq_error;
2369}
2370
2371// Used to test a multitude of sample rate and channel combinations. It works
2372// by first producing a set of reference files (in SetUpTestCase) that are
2373// assumed to be correct, as the used parameters are verified by other tests
2374// in this collection. Primarily the reference files are all produced at
2375// "native" rates which do not involve any resampling.
2376
2377// Each test pass produces an output file with a particular format. The output
2378// is matched against the reference file closest to its internal processing
2379// format. If necessary the output is resampled back to its process format.
2380// Due to the resampling distortion, we don't expect identical results, but
2381// enforce SNR thresholds which vary depending on the format. 0 is a special
2382// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002383typedef std::tr1::tuple<int, int, int, int, double, double>
2384 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002385class AudioProcessingTest
2386 : public testing::TestWithParam<AudioProcessingTestData> {
2387 public:
2388 AudioProcessingTest()
2389 : input_rate_(std::tr1::get<0>(GetParam())),
2390 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002391 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2392 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2393 expected_snr_(std::tr1::get<4>(GetParam())),
2394 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002395
2396 virtual ~AudioProcessingTest() {}
2397
2398 static void SetUpTestCase() {
2399 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002400 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002401 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002402 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2403 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2404 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002405 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002406 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2407 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2408 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409 }
2410 }
2411 }
2412 }
2413
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002414 static void TearDownTestCase() {
2415 ClearTempFiles();
2416 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002417
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002418 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002419 // to a file specified with |output_file_prefix|. Both forward and reverse
2420 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002421 static void ProcessFormat(int input_rate,
2422 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002423 int reverse_input_rate,
2424 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002425 size_t num_input_channels,
2426 size_t num_output_channels,
2427 size_t num_reverse_input_channels,
2428 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002430 Config config;
2431 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002432 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002433 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002434
ekmeyerson60d9b332015-08-14 10:35:55 -07002435 ProcessingConfig processing_config = {
2436 {{input_rate, num_input_channels},
2437 {output_rate, num_output_channels},
2438 {reverse_input_rate, num_reverse_input_channels},
2439 {reverse_output_rate, num_reverse_output_channels}}};
2440 ap->Initialize(processing_config);
2441
2442 FILE* far_file =
2443 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002444 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002445 FILE* out_file =
2446 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2447 reverse_input_rate, reverse_output_rate,
2448 num_input_channels, num_output_channels,
2449 num_reverse_input_channels,
2450 num_reverse_output_channels, kForward).c_str(),
2451 "wb");
2452 FILE* rev_out_file =
2453 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2454 reverse_input_rate, reverse_output_rate,
2455 num_input_channels, num_output_channels,
2456 num_reverse_input_channels,
2457 num_reverse_output_channels, kReverse).c_str(),
2458 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002459 ASSERT_TRUE(far_file != NULL);
2460 ASSERT_TRUE(near_file != NULL);
2461 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002462 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002463
2464 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2465 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002466 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2467 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002468 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2469 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002470 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2471 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002472
2473 // Temporary buffers.
2474 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002475 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2476 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002477 std::unique_ptr<float[]> float_data(new float[max_length]);
2478 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002479
2480 int analog_level = 127;
2481 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2482 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002483 EXPECT_NOERR(ap->ProcessReverseStream(
2484 rev_cb.channels(), processing_config.reverse_input_stream(),
2485 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002486
2487 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2488 ap->echo_cancellation()->set_stream_drift_samples(0);
2489 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2490
2491 EXPECT_NOERR(ap->ProcessStream(
2492 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002493 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002494 input_rate,
2495 LayoutFromChannels(num_input_channels),
2496 output_rate,
2497 LayoutFromChannels(num_output_channels),
2498 out_cb.channels()));
2499
ekmeyerson60d9b332015-08-14 10:35:55 -07002500 // Dump forward output to file.
2501 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002502 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002503 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002504
pkasting25702cb2016-01-08 13:50:27 -08002505 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002506 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002507 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002508
ekmeyerson60d9b332015-08-14 10:35:55 -07002509 // Dump reverse output to file.
2510 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2511 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002512 size_t rev_out_length =
2513 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002514
pkasting25702cb2016-01-08 13:50:27 -08002515 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002516 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2517 rev_out_file));
2518
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002519 analog_level = ap->gain_control()->stream_analog_level();
2520 }
2521 fclose(far_file);
2522 fclose(near_file);
2523 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002524 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002525 }
2526
2527 protected:
2528 int input_rate_;
2529 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002530 int reverse_input_rate_;
2531 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002532 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002533 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002534};
2535
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002536TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002537 struct ChannelFormat {
2538 int num_input;
2539 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002540 int num_reverse_input;
2541 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002542 };
2543 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002544 {1, 1, 1, 1},
2545 {1, 1, 2, 1},
2546 {2, 1, 1, 1},
2547 {2, 1, 2, 1},
2548 {2, 2, 1, 1},
2549 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002550 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002551
pkasting25702cb2016-01-08 13:50:27 -08002552 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002553 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2554 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2555 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002556
ekmeyerson60d9b332015-08-14 10:35:55 -07002557 // Verify output for both directions.
2558 std::vector<StreamDirection> stream_directions;
2559 stream_directions.push_back(kForward);
2560 stream_directions.push_back(kReverse);
2561 for (StreamDirection file_direction : stream_directions) {
2562 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2563 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2564 const int out_num =
2565 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2566 const double expected_snr =
2567 file_direction ? expected_reverse_snr_ : expected_snr_;
2568
2569 const int min_ref_rate = std::min(in_rate, out_rate);
2570 int ref_rate;
2571
2572 if (min_ref_rate > 32000) {
2573 ref_rate = 48000;
2574 } else if (min_ref_rate > 16000) {
2575 ref_rate = 32000;
2576 } else if (min_ref_rate > 8000) {
2577 ref_rate = 16000;
2578 } else {
2579 ref_rate = 8000;
2580 }
aluebs776593b2016-03-15 14:04:58 -07002581#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002582 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002583 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002584 }
2585#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002586 FILE* out_file = fopen(
2587 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2588 reverse_output_rate_, cf[i].num_input,
2589 cf[i].num_output, cf[i].num_reverse_input,
2590 cf[i].num_reverse_output, file_direction).c_str(),
2591 "rb");
2592 // The reference files always have matching input and output channels.
2593 FILE* ref_file = fopen(
2594 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2595 cf[i].num_output, cf[i].num_output,
2596 cf[i].num_reverse_output, cf[i].num_reverse_output,
2597 file_direction).c_str(),
2598 "rb");
2599 ASSERT_TRUE(out_file != NULL);
2600 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002601
pkasting25702cb2016-01-08 13:50:27 -08002602 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2603 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002604 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002605 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002606 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002607 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002608 // Data from the resampled output, in case the reference and output rates
2609 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002610 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002611
ekmeyerson60d9b332015-08-14 10:35:55 -07002612 PushResampler<float> resampler;
2613 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002614
ekmeyerson60d9b332015-08-14 10:35:55 -07002615 // Compute the resampling delay of the output relative to the reference,
2616 // to find the region over which we should search for the best SNR.
2617 float expected_delay_sec = 0;
2618 if (in_rate != ref_rate) {
2619 // Input resampling delay.
2620 expected_delay_sec +=
2621 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2622 }
2623 if (out_rate != ref_rate) {
2624 // Output resampling delay.
2625 expected_delay_sec +=
2626 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2627 // Delay of converting the output back to its processing rate for
2628 // testing.
2629 expected_delay_sec +=
2630 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2631 }
2632 int expected_delay =
2633 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002634
ekmeyerson60d9b332015-08-14 10:35:55 -07002635 double variance = 0;
2636 double sq_error = 0;
2637 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2638 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2639 float* out_ptr = out_data.get();
2640 if (out_rate != ref_rate) {
2641 // Resample the output back to its internal processing rate if
2642 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002643 ASSERT_EQ(ref_length,
2644 static_cast<size_t>(resampler.Resample(
2645 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002646 out_ptr = cmp_data.get();
2647 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002648
ekmeyerson60d9b332015-08-14 10:35:55 -07002649 // Update the |sq_error| and |variance| accumulators with the highest
2650 // SNR of reference vs output.
2651 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2652 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002653 }
2654
ekmeyerson60d9b332015-08-14 10:35:55 -07002655 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2656 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2657 << cf[i].num_input << ", " << cf[i].num_output << ", "
2658 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2659 << ", " << file_direction << "): ";
2660 if (sq_error > 0) {
2661 double snr = 10 * log10(variance / sq_error);
2662 EXPECT_GE(snr, expected_snr);
2663 EXPECT_NE(0, expected_snr);
2664 std::cout << "SNR=" << snr << " dB" << std::endl;
2665 } else {
aluebs776593b2016-03-15 14:04:58 -07002666 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002667 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002668
ekmeyerson60d9b332015-08-14 10:35:55 -07002669 fclose(out_file);
2670 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002671 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002672 }
2673}
2674
2675#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2676INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002677 CommonFormats,
2678 AudioProcessingTest,
2679 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002680 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2681 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002682 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2683 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2684 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2685 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2686 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2687 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2688 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2689 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2690 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002691
ekmeyerson60d9b332015-08-14 10:35:55 -07002692 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2693 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2694 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2695 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2696 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2697 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2698 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2699 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2700 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2701 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2702 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2703 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002704
ekmeyerson60d9b332015-08-14 10:35:55 -07002705 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2706 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2707 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2708 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2709 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2710 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2711 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2712 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2713 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2714 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2715 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2716 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002717
ekmeyerson60d9b332015-08-14 10:35:55 -07002718 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2719 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2720 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2721 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2722 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2723 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2724 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2725 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2726 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2727 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002728 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002729 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002730
2731#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2732INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002733 CommonFormats,
2734 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002735 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2736 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2737 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2738 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2739 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2740 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002741 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2742 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2743 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2744 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2745 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2746 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002747
aluebs776593b2016-03-15 14:04:58 -07002748 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2749 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2750 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002751 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2752 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2753 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2754 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2755 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2756 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2757 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2758 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2759 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002760
aluebs776593b2016-03-15 14:04:58 -07002761 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2762 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2763 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2764 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2765 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2766 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2767 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2768 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2769 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002770 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2771 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2772 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002773
ekmeyerson60d9b332015-08-14 10:35:55 -07002774 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2775 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2776 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2777 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2778 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2779 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2780 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2781 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2782 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2783 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002784 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002785 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002786#endif
2787
niklase@google.com470e71d2011-07-07 08:21:25 +00002788} // namespace
peahc19f3122016-10-07 14:54:10 -07002789
2790TEST(ApmConfiguration, DefaultBehavior) {
2791 // Verify that the level controller is default off, it can be activated using
2792 // the config, and that the default initial level is maintained after the
2793 // config has been applied.
2794 std::unique_ptr<AudioProcessingImpl> apm(
2795 new AudioProcessingImpl(webrtc::Config()));
2796 AudioProcessing::Config config;
2797 EXPECT_FALSE(apm->config_.level_controller.enabled);
2798 // TODO(peah): Add test for the existence of the level controller object once
2799 // that is created only when that is specified in the config.
2800 // TODO(peah): Remove the testing for
2801 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2802 // is instead used to activate the level controller.
2803 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2804 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2805 apm->config_.level_controller.initial_peak_level_dbfs,
2806 std::numeric_limits<float>::epsilon());
2807 config.level_controller.enabled = true;
2808 apm->ApplyConfig(config);
2809 EXPECT_TRUE(apm->config_.level_controller.enabled);
2810 // TODO(peah): Add test for the existence of the level controller object once
2811 // that is created only when the that is specified in the config.
2812 // TODO(peah): Remove the testing for
2813 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2814 // is instead used to activate the level controller.
2815 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2816 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2817 apm->config_.level_controller.initial_peak_level_dbfs,
2818 std::numeric_limits<float>::epsilon());
2819}
2820
2821TEST(ApmConfiguration, ValidConfigBehavior) {
2822 // Verify that the initial level can be specified and is retained after the
2823 // config has been applied.
2824 std::unique_ptr<AudioProcessingImpl> apm(
2825 new AudioProcessingImpl(webrtc::Config()));
2826 AudioProcessing::Config config;
2827 config.level_controller.initial_peak_level_dbfs = -50.f;
2828 apm->ApplyConfig(config);
2829 EXPECT_FALSE(apm->config_.level_controller.enabled);
2830 // TODO(peah): Add test for the existence of the level controller object once
2831 // that is created only when the that is specified in the config.
2832 // TODO(peah): Remove the testing for
2833 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2834 // is instead used to activate the level controller.
2835 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2836 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2837 std::numeric_limits<float>::epsilon());
2838}
2839
2840TEST(ApmConfiguration, InValidConfigBehavior) {
2841 // Verify that the config is properly reset when nonproper values are applied
2842 // for the initial level.
2843
2844 // Verify that the config is properly reset when the specified initial peak
2845 // level is too low.
2846 std::unique_ptr<AudioProcessingImpl> apm(
2847 new AudioProcessingImpl(webrtc::Config()));
2848 AudioProcessing::Config config;
2849 config.level_controller.enabled = true;
2850 config.level_controller.initial_peak_level_dbfs = -101.f;
2851 apm->ApplyConfig(config);
2852 EXPECT_FALSE(apm->config_.level_controller.enabled);
2853 // TODO(peah): Add test for the existence of the level controller object once
2854 // that is created only when the that is specified in the config.
2855 // TODO(peah): Remove the testing for
2856 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2857 // is instead used to activate the level controller.
2858 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2859 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2860 apm->config_.level_controller.initial_peak_level_dbfs,
2861 std::numeric_limits<float>::epsilon());
2862
2863 // Verify that the config is properly reset when the specified initial peak
2864 // level is too high.
2865 apm.reset(new AudioProcessingImpl(webrtc::Config()));
2866 config = AudioProcessing::Config();
2867 config.level_controller.enabled = true;
2868 config.level_controller.initial_peak_level_dbfs = 1.f;
2869 apm->ApplyConfig(config);
2870 EXPECT_FALSE(apm->config_.level_controller.enabled);
2871 // TODO(peah): Add test for the existence of the level controller object once
2872 // that is created only when that is specified in the config.
2873 // TODO(peah): Remove the testing for
2874 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2875 // is instead used to activate the level controller.
2876 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2877 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2878 apm->config_.level_controller.initial_peak_level_dbfs,
2879 std::numeric_limits<float>::epsilon());
2880}
2881
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002882} // namespace webrtc