blob: d4faa812055eac5a0f9808a89a3e06f7aa2a9763 [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_;
394 const std::string ref_path_;
395 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800396 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000397 AudioFrame* frame_;
398 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800399 std::unique_ptr<ChannelBuffer<float> > float_cb_;
400 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000401 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800402 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403 FILE* far_file_;
404 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000405 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406};
407
408ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000409 : output_path_(test::OutputPath()),
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200410#ifndef WEBRTC_IOS
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000411 ref_path_(test::ProjectRootPath() + "data/audio_processing/"),
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200412#else
413 // On iOS test data is flat in the project root dir
414 ref_path_(test::ProjectRootPath()),
415#endif
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000416#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000417 ref_filename_(ref_path_ + "output_data_fixed.pb"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000418#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000419#if defined(WEBRTC_MAC)
420 // A different file for Mac is needed because on this platform the AEC
421 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
422 ref_filename_(ref_path_ + "output_data_mac.pb"),
423#else
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000424 ref_filename_(ref_path_ + "output_data_float.pb"),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000425#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000426#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000428 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000429 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000430 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000431 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000432 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000433 out_file_(NULL) {
434 Config config;
435 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
436 apm_.reset(AudioProcessing::Create(config));
437}
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
439void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000440 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
442 frame_ = new AudioFrame();
443 revframe_ = new AudioFrame();
444
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000445 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
448void ApmTest::TearDown() {
449 if (frame_) {
450 delete frame_;
451 }
452 frame_ = NULL;
453
454 if (revframe_) {
455 delete revframe_;
456 }
457 revframe_ = NULL;
458
459 if (far_file_) {
460 ASSERT_EQ(0, fclose(far_file_));
461 }
462 far_file_ = NULL;
463
464 if (near_file_) {
465 ASSERT_EQ(0, fclose(near_file_));
466 }
467 near_file_ = NULL;
468
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000469 if (out_file_) {
470 ASSERT_EQ(0, fclose(out_file_));
471 }
472 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000475void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000476 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700477 ap->Initialize(
478 {{{frame_->sample_rate_hz_, frame_->num_channels_},
479 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700480 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700481 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000482}
483
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000486 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800487 size_t num_input_channels,
488 size_t num_output_channels,
489 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000491 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000492 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000493 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000494
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000495 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
496 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000497 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000498
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000499 if (far_file_) {
500 ASSERT_EQ(0, fclose(far_file_));
501 }
502 std::string filename = ResourceFilePath("far", sample_rate_hz);
503 far_file_ = fopen(filename.c_str(), "rb");
504 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
505 filename << "\n";
506
507 if (near_file_) {
508 ASSERT_EQ(0, fclose(near_file_));
509 }
510 filename = ResourceFilePath("near", sample_rate_hz);
511 near_file_ = fopen(filename.c_str(), "rb");
512 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
513 filename << "\n";
514
515 if (open_output_file) {
516 if (out_file_) {
517 ASSERT_EQ(0, fclose(out_file_));
518 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700519 filename = OutputFilePath(
520 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
521 reverse_sample_rate_hz, num_input_channels, num_output_channels,
522 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000523 out_file_ = fopen(filename.c_str(), "wb");
524 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
525 filename << "\n";
526 }
527}
528
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000529void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000530 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000531}
532
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000533bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
534 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000535 // The files always contain stereo audio.
536 size_t frame_size = frame->samples_per_channel_ * 2;
537 size_t read_count = fread(frame->data_,
538 sizeof(int16_t),
539 frame_size,
540 file);
541 if (read_count != frame_size) {
542 // Check that the file really ended.
543 EXPECT_NE(0, feof(file));
544 return false; // This is expected.
545 }
546
547 if (frame->num_channels_ == 1) {
548 MixStereoToMono(frame->data_, frame->data_,
549 frame->samples_per_channel_);
550 }
551
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000553 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000555 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000556}
557
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000558bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
559 return ReadFrame(file, frame, NULL);
560}
561
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000562// If the end of the file has been reached, rewind it and attempt to read the
563// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000564void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
565 ChannelBuffer<float>* cb) {
566 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000567 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000568 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000569 }
570}
571
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
573 ReadFrameWithRewind(file, frame, NULL);
574}
575
andrew@webrtc.org81865342012-10-27 00:28:27 +0000576void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
577 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000578 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000579 EXPECT_EQ(apm_->kNoError,
580 apm_->gain_control()->set_stream_analog_level(127));
581 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000582}
583
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000584int ApmTest::ProcessStreamChooser(Format format) {
585 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000586 return apm_->ProcessStream(frame_);
587 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000588 return apm_->ProcessStream(float_cb_->channels(),
589 frame_->samples_per_channel_,
590 frame_->sample_rate_hz_,
591 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000592 output_sample_rate_hz_,
593 LayoutFromChannels(num_output_channels_),
594 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000595}
596
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000597int ApmTest::AnalyzeReverseStreamChooser(Format format) {
598 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700599 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000600 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000601 return apm_->AnalyzeReverseStream(
602 revfloat_cb_->channels(),
603 revframe_->samples_per_channel_,
604 revframe_->sample_rate_hz_,
605 LayoutFromChannels(revframe_->num_channels_));
606}
607
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000608void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
609 int delay_min, int delay_max) {
610 // The |revframe_| and |frame_| should include the proper frame information,
611 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000612 AudioFrame tmp_frame;
613 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000614 bool causal = true;
615
616 tmp_frame.CopyFrom(*revframe_);
617 SetFrameTo(&tmp_frame, 0);
618
619 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
620 // Initialize the |frame_queue| with empty frames.
621 int frame_delay = delay_ms / 10;
622 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000623 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000624 frame->CopyFrom(tmp_frame);
625 frame_queue.push(frame);
626 frame_delay++;
627 causal = false;
628 }
629 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000630 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631 frame->CopyFrom(tmp_frame);
632 frame_queue.push(frame);
633 frame_delay--;
634 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000635 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
636 // need enough frames with audio to have reliable estimates, but as few as
637 // possible to keep processing time down. 4.5 seconds seemed to be a good
638 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000639 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000640 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000641 frame->CopyFrom(tmp_frame);
642 // Use the near end recording, since that has more speech in it.
643 ASSERT_TRUE(ReadFrame(near_file_, frame));
644 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000645 AudioFrame* reverse_frame = frame;
646 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 if (!causal) {
648 reverse_frame = frame_queue.front();
649 // When we call ProcessStream() the frame is modified, so we can't use the
650 // pointer directly when things are non-causal. Use an intermediate frame
651 // and copy the data.
652 process_frame = &tmp_frame;
653 process_frame->CopyFrom(*frame);
654 }
aluebsb0319552016-03-17 20:39:53 -0700655 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
658 frame = frame_queue.front();
659 frame_queue.pop();
660 delete frame;
661
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000662 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 int median;
664 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000665 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000666 // Discard the first delay metrics to avoid convergence effects.
667 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000668 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
669 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000670 }
671 }
672
673 rewind(near_file_);
674 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000675 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000676 frame_queue.pop();
677 delete frame;
678 }
679 // Calculate expected delay estimate and acceptable regions. Further,
680 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 const size_t samples_per_ms =
682 std::min(static_cast<size_t>(16), frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
684 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700685 int expected_median_high = std::min(
686 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
687 delay_min),
688 delay_max);
689 int expected_median_low = std::min(
690 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
691 delay_min),
692 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000693 // Verify delay metrics.
694 int median;
695 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000696 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000697 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000698 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
699 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000700 EXPECT_GE(expected_median_high, median);
701 EXPECT_LE(expected_median_low, median);
702}
703
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000704void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000706 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000707
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000710 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000711 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000712
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 EXPECT_EQ(apm_->kNoError,
715 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000716 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000717 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000718 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000719
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000720 // Other stream parameters set correctly.
721 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 EXPECT_EQ(apm_->kNoError,
723 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000724 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000725 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000727 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
729 EXPECT_EQ(apm_->kNoError,
730 apm_->echo_cancellation()->enable_drift_compensation(false));
731
732 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000733 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000734 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000735 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000736 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000737
738 // Resets after successful ProcessStream().
739 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000740 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000741 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000743
744 // Other stream parameters set correctly.
745 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
746 EXPECT_EQ(apm_->kNoError,
747 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000748 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000749 EXPECT_EQ(apm_->kNoError,
750 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000751 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000752 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
754
755 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000757 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000758
759 // Resets after successful ProcessStream().
760 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000761 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000762 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000763 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000764 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000765
766 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000767 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
768 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
769 EXPECT_EQ(apm_->kNoError,
770 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000771 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000772 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000773
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000774 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000775 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000776 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000777 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000779
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000780 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000781 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000782 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 EXPECT_EQ(apm_->kNoError,
784 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000785 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000786}
787
788TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000789 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000790}
791
792TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000793 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000794}
795
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000796TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
797 EXPECT_EQ(0, apm_->delay_offset_ms());
798 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
799 EXPECT_EQ(50, apm_->stream_delay_ms());
800}
801
802TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
803 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000804 apm_->set_delay_offset_ms(100);
805 EXPECT_EQ(100, apm_->delay_offset_ms());
806 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000807 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000808 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
809 EXPECT_EQ(200, apm_->stream_delay_ms());
810
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000811 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000812 apm_->set_delay_offset_ms(-50);
813 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000814 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
815 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000816 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
817 EXPECT_EQ(50, apm_->stream_delay_ms());
818}
819
Michael Graczyk86c6d332015-07-23 11:41:39 -0700820void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800821 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700822 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000823 frame_->num_channels_ = num_channels;
824 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700825 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000826}
827
Michael Graczyk86c6d332015-07-23 11:41:39 -0700828void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800829 size_t num_in_channels,
830 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700831 AudioProcessing::Error expected_return) {
832 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
833 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
834
835 EXPECT_EQ(expected_return,
836 apm_->ProcessStream(float_cb_->channels(), input_stream,
837 output_stream, float_cb_->channels()));
838}
839
840void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800841 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700842 AudioProcessing::Error expected_return) {
843 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700844 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
845 {output_sample_rate_hz_, apm_->num_output_channels()},
846 {frame_->sample_rate_hz_, num_rev_channels},
847 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700848
ekmeyerson60d9b332015-08-14 10:35:55 -0700849 EXPECT_EQ(
850 expected_return,
851 apm_->ProcessReverseStream(
852 float_cb_->channels(), processing_config.reverse_input_stream(),
853 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700854}
855
856TEST_F(ApmTest, ChannelsInt16Interface) {
857 // Testing number of invalid and valid channels.
858 Init(16000, 16000, 16000, 4, 4, 4, false);
859
860 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
861
Peter Kasting69558702016-01-12 16:26:35 -0800862 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700863 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000864 EXPECT_EQ(i, apm_->num_input_channels());
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000865 // We always force the number of reverse channels used for processing to 1.
Peter Kasting69558702016-01-12 16:26:35 -0800866 EXPECT_EQ(1u, apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000867 }
868}
869
Michael Graczyk86c6d332015-07-23 11:41:39 -0700870TEST_F(ApmTest, Channels) {
871 // Testing number of invalid and valid channels.
872 Init(16000, 16000, 16000, 4, 4, 4, false);
873
874 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
875 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
876
Peter Kasting69558702016-01-12 16:26:35 -0800877 for (size_t i = 1; i < 4; ++i) {
878 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700879 // Output channels much be one or match input channels.
880 if (j == 1 || i == j) {
881 TestChangingForwardChannels(i, j, kNoErr);
882 TestChangingReverseChannels(i, kNoErr);
883
884 EXPECT_EQ(i, apm_->num_input_channels());
885 EXPECT_EQ(j, apm_->num_output_channels());
886 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800887 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700888 } else {
889 TestChangingForwardChannels(i, j,
890 AudioProcessing::kBadNumberChannelsError);
891 }
892 }
893 }
894}
895
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000896TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000897 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000898 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000899 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000900 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700901 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800902 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000903 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000904 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000905 }
906}
907
niklase@google.com470e71d2011-07-07 08:21:25 +0000908TEST_F(ApmTest, EchoCancellation) {
909 EXPECT_EQ(apm_->kNoError,
910 apm_->echo_cancellation()->enable_drift_compensation(true));
911 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
912 EXPECT_EQ(apm_->kNoError,
913 apm_->echo_cancellation()->enable_drift_compensation(false));
914 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
915
niklase@google.com470e71d2011-07-07 08:21:25 +0000916 EchoCancellation::SuppressionLevel level[] = {
917 EchoCancellation::kLowSuppression,
918 EchoCancellation::kModerateSuppression,
919 EchoCancellation::kHighSuppression,
920 };
pkasting25702cb2016-01-08 13:50:27 -0800921 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000922 EXPECT_EQ(apm_->kNoError,
923 apm_->echo_cancellation()->set_suppression_level(level[i]));
924 EXPECT_EQ(level[i],
925 apm_->echo_cancellation()->suppression_level());
926 }
927
928 EchoCancellation::Metrics metrics;
929 EXPECT_EQ(apm_->kNotEnabledError,
930 apm_->echo_cancellation()->GetMetrics(&metrics));
931
932 EXPECT_EQ(apm_->kNoError,
933 apm_->echo_cancellation()->enable_metrics(true));
934 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
935 EXPECT_EQ(apm_->kNoError,
936 apm_->echo_cancellation()->enable_metrics(false));
937 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
938
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000939 int median = 0;
940 int std = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000941 float poor_fraction = 0;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000942 EXPECT_EQ(apm_->kNotEnabledError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000943 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
944 &poor_fraction));
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000945
946 EXPECT_EQ(apm_->kNoError,
947 apm_->echo_cancellation()->enable_delay_logging(true));
948 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
949 EXPECT_EQ(apm_->kNoError,
950 apm_->echo_cancellation()->enable_delay_logging(false));
951 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
952
niklase@google.com470e71d2011-07-07 08:21:25 +0000953 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
954 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
955 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
956 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000957
958 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
959 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
960 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
961 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
962 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
963 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000964}
965
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000966TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000967 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000968 // Enable AEC only.
969 EXPECT_EQ(apm_->kNoError,
970 apm_->echo_cancellation()->enable_drift_compensation(false));
971 EXPECT_EQ(apm_->kNoError,
972 apm_->echo_cancellation()->enable_metrics(false));
973 EXPECT_EQ(apm_->kNoError,
974 apm_->echo_cancellation()->enable_delay_logging(true));
975 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000976 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700977 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000978 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000979
980 // Internally in the AEC the amount of lookahead the delay estimation can
981 // handle is 15 blocks and the maximum delay is set to 60 blocks.
982 const int kLookaheadBlocks = 15;
983 const int kMaxDelayBlocks = 60;
984 // The AEC has a startup time before it actually starts to process. This
985 // procedure can flush the internal far-end buffer, which of course affects
986 // the delay estimation. Therefore, we set a system_delay high enough to
987 // avoid that. The smallest system_delay you can report without flushing the
988 // buffer is 66 ms in 8 kHz.
989 //
990 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
991 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
992 // delay estimation. This should be noted though. In case of test failure,
993 // this could be the cause.
994 const int kSystemDelayMs = 66;
995 // Test a couple of corner cases and verify that the estimated delay is
996 // within a valid region (set to +-1.5 blocks). Note that these cases are
997 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800998 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000999 Init(kProcessSampleRates[i],
1000 kProcessSampleRates[i],
1001 kProcessSampleRates[i],
1002 2,
1003 2,
1004 2,
1005 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001006 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001007 const int num_ms_per_block =
1008 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001009 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1010 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1011
1012 // 1) Verify correct delay estimate at lookahead boundary.
1013 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1014 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1015 delay_max_ms);
1016 // 2) A delay less than maximum lookahead should give an delay estimate at
1017 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1018 delay_ms -= 20;
1019 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1020 delay_max_ms);
1021 // 3) Three values around zero delay. Note that we need to compensate for
1022 // the fake system_delay.
1023 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1024 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1025 delay_max_ms);
1026 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1027 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1028 delay_max_ms);
1029 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1030 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1031 delay_max_ms);
1032 // 4) Verify correct delay estimate at maximum delay boundary.
1033 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1034 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1035 delay_max_ms);
1036 // 5) A delay above the maximum delay should give an estimate at the
1037 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1038 delay_ms += 20;
1039 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1040 delay_max_ms);
1041 }
1042}
1043
niklase@google.com470e71d2011-07-07 08:21:25 +00001044TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001045 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001046 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001047 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1048 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1049
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 // Toggle routing modes
1051 EchoControlMobile::RoutingMode mode[] = {
1052 EchoControlMobile::kQuietEarpieceOrHeadset,
1053 EchoControlMobile::kEarpiece,
1054 EchoControlMobile::kLoudEarpiece,
1055 EchoControlMobile::kSpeakerphone,
1056 EchoControlMobile::kLoudSpeakerphone,
1057 };
pkasting25702cb2016-01-08 13:50:27 -08001058 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001059 EXPECT_EQ(apm_->kNoError,
1060 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1061 EXPECT_EQ(mode[i],
1062 apm_->echo_control_mobile()->routing_mode());
1063 }
1064 // Turn comfort noise off/on
1065 EXPECT_EQ(apm_->kNoError,
1066 apm_->echo_control_mobile()->enable_comfort_noise(false));
1067 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1068 EXPECT_EQ(apm_->kNoError,
1069 apm_->echo_control_mobile()->enable_comfort_noise(true));
1070 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001071 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001072 const size_t echo_path_size =
1073 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001074 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1075 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001076 EXPECT_EQ(apm_->kNullPointerError,
1077 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1078 EXPECT_EQ(apm_->kNullPointerError,
1079 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1080 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001081 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001082 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001083 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001084 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001085 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001086 echo_path_in[i] = echo_path_out[i] + 1;
1087 }
1088 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001089 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001090 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001091 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1092 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001093 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001094 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1095 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001096 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001097 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1098 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001099
1100 // Process a few frames with NS in the default disabled state. This exercises
1101 // a different codepath than with it enabled.
1102 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1103 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1104 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1105 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1106
niklase@google.com470e71d2011-07-07 08:21:25 +00001107 // Turn AECM off
1108 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1109 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1110}
1111
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001112TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001113 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001114 EXPECT_EQ(apm_->kNoError,
1115 apm_->gain_control()->set_mode(
1116 apm_->gain_control()->mode()));
1117
1118 GainControl::Mode mode[] = {
1119 GainControl::kAdaptiveAnalog,
1120 GainControl::kAdaptiveDigital,
1121 GainControl::kFixedDigital
1122 };
pkasting25702cb2016-01-08 13:50:27 -08001123 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001124 EXPECT_EQ(apm_->kNoError,
1125 apm_->gain_control()->set_mode(mode[i]));
1126 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1127 }
1128 // Testing invalid target levels
1129 EXPECT_EQ(apm_->kBadParameterError,
1130 apm_->gain_control()->set_target_level_dbfs(-3));
1131 EXPECT_EQ(apm_->kBadParameterError,
1132 apm_->gain_control()->set_target_level_dbfs(-40));
1133 // Testing valid target levels
1134 EXPECT_EQ(apm_->kNoError,
1135 apm_->gain_control()->set_target_level_dbfs(
1136 apm_->gain_control()->target_level_dbfs()));
1137
1138 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001139 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001140 EXPECT_EQ(apm_->kNoError,
1141 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1142 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1143 }
1144
1145 // Testing invalid compression gains
1146 EXPECT_EQ(apm_->kBadParameterError,
1147 apm_->gain_control()->set_compression_gain_db(-1));
1148 EXPECT_EQ(apm_->kBadParameterError,
1149 apm_->gain_control()->set_compression_gain_db(100));
1150
1151 // Testing valid compression gains
1152 EXPECT_EQ(apm_->kNoError,
1153 apm_->gain_control()->set_compression_gain_db(
1154 apm_->gain_control()->compression_gain_db()));
1155
1156 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001157 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 EXPECT_EQ(apm_->kNoError,
1159 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1160 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1161 }
1162
1163 // Testing limiter off/on
1164 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1165 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1166 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1167 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1168
1169 // Testing invalid level limits
1170 EXPECT_EQ(apm_->kBadParameterError,
1171 apm_->gain_control()->set_analog_level_limits(-1, 512));
1172 EXPECT_EQ(apm_->kBadParameterError,
1173 apm_->gain_control()->set_analog_level_limits(100000, 512));
1174 EXPECT_EQ(apm_->kBadParameterError,
1175 apm_->gain_control()->set_analog_level_limits(512, -1));
1176 EXPECT_EQ(apm_->kBadParameterError,
1177 apm_->gain_control()->set_analog_level_limits(512, 100000));
1178 EXPECT_EQ(apm_->kBadParameterError,
1179 apm_->gain_control()->set_analog_level_limits(512, 255));
1180
1181 // Testing valid level limits
1182 EXPECT_EQ(apm_->kNoError,
1183 apm_->gain_control()->set_analog_level_limits(
1184 apm_->gain_control()->analog_level_minimum(),
1185 apm_->gain_control()->analog_level_maximum()));
1186
1187 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001188 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 EXPECT_EQ(apm_->kNoError,
1190 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1191 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1192 }
1193
1194 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001195 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001196 EXPECT_EQ(apm_->kNoError,
1197 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1198 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1199 }
1200
1201 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1202
1203 // Turn AGC off
1204 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1205 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1206}
1207
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001208void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001209 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001210 EXPECT_EQ(apm_->kNoError,
1211 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1212 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1213
1214 int out_analog_level = 0;
1215 for (int i = 0; i < 2000; ++i) {
1216 ReadFrameWithRewind(near_file_, frame_);
1217 // Ensure the audio is at a low level, so the AGC will try to increase it.
1218 ScaleFrame(frame_, 0.25);
1219
1220 // Always pass in the same volume.
1221 EXPECT_EQ(apm_->kNoError,
1222 apm_->gain_control()->set_stream_analog_level(100));
1223 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1224 out_analog_level = apm_->gain_control()->stream_analog_level();
1225 }
1226
1227 // Ensure the AGC is still able to reach the maximum.
1228 EXPECT_EQ(255, out_analog_level);
1229}
1230
1231// Verifies that despite volume slider quantization, the AGC can continue to
1232// increase its volume.
1233TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001234 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001235 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1236 }
1237}
1238
1239void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001240 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001241 EXPECT_EQ(apm_->kNoError,
1242 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1243 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1244
1245 int out_analog_level = 100;
1246 for (int i = 0; i < 1000; ++i) {
1247 ReadFrameWithRewind(near_file_, frame_);
1248 // Ensure the audio is at a low level, so the AGC will try to increase it.
1249 ScaleFrame(frame_, 0.25);
1250
1251 EXPECT_EQ(apm_->kNoError,
1252 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1253 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1254 out_analog_level = apm_->gain_control()->stream_analog_level();
1255 }
1256
1257 // Ensure the volume was raised.
1258 EXPECT_GT(out_analog_level, 100);
1259 int highest_level_reached = out_analog_level;
1260 // Simulate a user manual volume change.
1261 out_analog_level = 100;
1262
1263 for (int i = 0; i < 300; ++i) {
1264 ReadFrameWithRewind(near_file_, frame_);
1265 ScaleFrame(frame_, 0.25);
1266
1267 EXPECT_EQ(apm_->kNoError,
1268 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1269 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1270 out_analog_level = apm_->gain_control()->stream_analog_level();
1271 // Check that AGC respected the manually adjusted volume.
1272 EXPECT_LT(out_analog_level, highest_level_reached);
1273 }
1274 // Check that the volume was still raised.
1275 EXPECT_GT(out_analog_level, 100);
1276}
1277
1278TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001279 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001280 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1281 }
1282}
1283
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001284#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1285TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1286 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001287 const size_t kSamplesPerChannel =
1288 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001289 const size_t kNumInputChannels = 2;
1290 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001291 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001292 const float kScaleFactor = 0.25f;
1293 Config config;
1294 std::vector<webrtc::Point> geometry;
1295 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1296 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1297 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001298 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001299 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001300 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001301 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001302 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1303 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1304 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001305 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1306 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001307 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1308 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001309 std::string filename = ResourceFilePath("far", kSampleRateHz);
1310 FILE* far_file = fopen(filename.c_str(), "rb");
1311 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1312 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1313 const int kDefaultCompressionGain =
1314 apm->gain_control()->compression_gain_db();
1315 bool is_target = false;
1316 EXPECT_CALL(*beamformer, is_target_present())
1317 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001318 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001319 ASSERT_TRUE(ReadChunk(far_file,
1320 int_data.get(),
1321 float_data.get(),
1322 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001323 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001324 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001325 src_buf.channels()[j][k] *= kScaleFactor;
1326 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001327 }
1328 EXPECT_EQ(kNoErr,
1329 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001330 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001331 kSampleRateHz,
1332 LayoutFromChannels(src_buf.num_channels()),
1333 kSampleRateHz,
1334 LayoutFromChannels(dest_buf.num_channels()),
1335 dest_buf.channels()));
1336 }
1337 EXPECT_EQ(kDefaultVolume,
1338 apm->gain_control()->stream_analog_level());
1339 EXPECT_EQ(kDefaultCompressionGain,
1340 apm->gain_control()->compression_gain_db());
1341 rewind(far_file);
1342 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001343 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001344 ASSERT_TRUE(ReadChunk(far_file,
1345 int_data.get(),
1346 float_data.get(),
1347 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001348 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001349 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001350 src_buf.channels()[j][k] *= kScaleFactor;
1351 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001352 }
1353 EXPECT_EQ(kNoErr,
1354 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001355 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001356 kSampleRateHz,
1357 LayoutFromChannels(src_buf.num_channels()),
1358 kSampleRateHz,
1359 LayoutFromChannels(dest_buf.num_channels()),
1360 dest_buf.channels()));
1361 }
1362 EXPECT_LT(kDefaultVolume,
1363 apm->gain_control()->stream_analog_level());
1364 EXPECT_LT(kDefaultCompressionGain,
1365 apm->gain_control()->compression_gain_db());
1366 ASSERT_EQ(0, fclose(far_file));
1367}
1368#endif
1369
niklase@google.com470e71d2011-07-07 08:21:25 +00001370TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001371 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001372 NoiseSuppression::Level level[] = {
1373 NoiseSuppression::kLow,
1374 NoiseSuppression::kModerate,
1375 NoiseSuppression::kHigh,
1376 NoiseSuppression::kVeryHigh
1377 };
pkasting25702cb2016-01-08 13:50:27 -08001378 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001379 EXPECT_EQ(apm_->kNoError,
1380 apm_->noise_suppression()->set_level(level[i]));
1381 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1382 }
1383
andrew@webrtc.org648af742012-02-08 01:57:29 +00001384 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001385 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1386 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1387 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1388 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1389}
1390
1391TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001392 // Turn HP filter on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001393 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(true));
1394 EXPECT_TRUE(apm_->high_pass_filter()->is_enabled());
1395 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(false));
1396 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1397}
1398
1399TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001400 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001401 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001402 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001403
1404 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1405
1406 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1407 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1408
1409 // Run this test in wideband; in super-wb, the splitting filter distorts the
1410 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001411 frame_->samples_per_channel_ = 160;
1412 frame_->num_channels_ = 2;
1413 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001414
1415 // Min value if no frames have been processed.
1416 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1417
1418 // Min value on zero frames.
1419 SetFrameTo(frame_, 0);
1420 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1421 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1422 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1423
1424 // Try a few RMS values.
1425 // (These also test that the value resets after retrieving it.)
1426 SetFrameTo(frame_, 32767);
1427 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1428 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1429 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1430
1431 SetFrameTo(frame_, 30000);
1432 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1433 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1434 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1435
1436 SetFrameTo(frame_, 10000);
1437 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1438 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1439 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1440
1441 SetFrameTo(frame_, 10);
1442 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1443 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1444 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1445
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001446 // Verify reset after enable/disable.
1447 SetFrameTo(frame_, 32767);
1448 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1449 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1450 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1451 SetFrameTo(frame_, 1);
1452 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1453 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1454
1455 // Verify reset after initialize.
1456 SetFrameTo(frame_, 32767);
1457 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1458 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1459 SetFrameTo(frame_, 1);
1460 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1461 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001462}
1463
1464TEST_F(ApmTest, VoiceDetection) {
1465 // Test external VAD
1466 EXPECT_EQ(apm_->kNoError,
1467 apm_->voice_detection()->set_stream_has_voice(true));
1468 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1469 EXPECT_EQ(apm_->kNoError,
1470 apm_->voice_detection()->set_stream_has_voice(false));
1471 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1472
andrew@webrtc.org648af742012-02-08 01:57:29 +00001473 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001474 VoiceDetection::Likelihood likelihood[] = {
1475 VoiceDetection::kVeryLowLikelihood,
1476 VoiceDetection::kLowLikelihood,
1477 VoiceDetection::kModerateLikelihood,
1478 VoiceDetection::kHighLikelihood
1479 };
pkasting25702cb2016-01-08 13:50:27 -08001480 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001481 EXPECT_EQ(apm_->kNoError,
1482 apm_->voice_detection()->set_likelihood(likelihood[i]));
1483 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1484 }
1485
1486 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001487 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001488 EXPECT_EQ(apm_->kBadParameterError,
1489 apm_->voice_detection()->set_frame_size_ms(12));
1490
andrew@webrtc.org648af742012-02-08 01:57:29 +00001491 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001492 for (int i = 10; i <= 30; i += 10) {
1493 EXPECT_EQ(apm_->kNoError,
1494 apm_->voice_detection()->set_frame_size_ms(i));
1495 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1496 }
1497 */
1498
andrew@webrtc.org648af742012-02-08 01:57:29 +00001499 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001500 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1501 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1502 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1503 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1504
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001505 // Test that AudioFrame activity is maintained when VAD is disabled.
1506 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1507 AudioFrame::VADActivity activity[] = {
1508 AudioFrame::kVadActive,
1509 AudioFrame::kVadPassive,
1510 AudioFrame::kVadUnknown
1511 };
pkasting25702cb2016-01-08 13:50:27 -08001512 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001513 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001514 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001515 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001516 }
1517
1518 // Test that AudioFrame activity is set when VAD is enabled.
1519 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001520 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001521 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001522 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001523
niklase@google.com470e71d2011-07-07 08:21:25 +00001524 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1525}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001526
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001527TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1528 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1529 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1530 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1531 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1532 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1533 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1534 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1535}
1536
1537TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001538 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001539 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001540 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001541 AudioFrame frame_copy;
1542 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001543 for (int j = 0; j < 1000; j++) {
1544 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1545 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001546 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1547 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001548 }
1549 }
1550}
1551
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001552TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1553 // Test that ProcessStream copies input to output even with no processing.
1554 const size_t kSamples = 80;
1555 const int sample_rate = 8000;
1556 const float src[kSamples] = {
1557 -1.0f, 0.0f, 1.0f
1558 };
1559 float dest[kSamples] = {};
1560
1561 auto src_channels = &src[0];
1562 auto dest_channels = &dest[0];
1563
1564 apm_.reset(AudioProcessing::Create());
1565 EXPECT_NOERR(apm_->ProcessStream(
1566 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1567 sample_rate, LayoutFromChannels(1), &dest_channels));
1568
1569 for (size_t i = 0; i < kSamples; ++i) {
1570 EXPECT_EQ(src[i], dest[i]);
1571 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001572
1573 // Same for ProcessReverseStream.
1574 float rev_dest[kSamples] = {};
1575 auto rev_dest_channels = &rev_dest[0];
1576
1577 StreamConfig input_stream = {sample_rate, 1};
1578 StreamConfig output_stream = {sample_rate, 1};
1579 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1580 output_stream, &rev_dest_channels));
1581
1582 for (size_t i = 0; i < kSamples; ++i) {
1583 EXPECT_EQ(src[i], rev_dest[i]);
1584 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001585}
1586
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001587TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1588 EnableAllComponents();
1589
pkasting25702cb2016-01-08 13:50:27 -08001590 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001591 Init(kProcessSampleRates[i],
1592 kProcessSampleRates[i],
1593 kProcessSampleRates[i],
1594 2,
1595 2,
1596 2,
1597 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001598 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001599 ASSERT_EQ(0, feof(far_file_));
1600 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001601 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001602 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1603
aluebsb0319552016-03-17 20:39:53 -07001604 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001605
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001606 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1607 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1608
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001609 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001610 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001611 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001612 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001613 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001614 analog_level = apm_->gain_control()->stream_analog_level();
1615
1616 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1617 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001618 rewind(far_file_);
1619 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001620 }
1621}
1622
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001623TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001624 // Verify the filter is not active through undistorted audio when:
1625 // 1. No components are enabled...
1626 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001627 AudioFrame frame_copy;
1628 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001629 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1630 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1631 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1632
1633 // 2. Only the level estimator is enabled...
1634 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001635 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001636 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1637 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1638 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1639 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1640 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1641
1642 // 3. Only VAD is enabled...
1643 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001644 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001645 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1646 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1647 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1648 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1649 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1650
1651 // 4. Both VAD and the level estimator are enabled...
1652 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001653 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001654 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1655 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1656 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1658 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1659 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1660 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1661
1662 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001663 frame_->samples_per_channel_ = 160;
1664 frame_->num_channels_ = 2;
1665 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001666 // Enable AEC, which would require the filter in super-wb. We rely on the
1667 // first few frames of data being unaffected by the AEC.
1668 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1669 // behavior of the AEC. Think of something more robust.
1670 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001671 // Make sure we have extended filter enabled. This makes sure nothing is
1672 // touched until we have a farend frame.
1673 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001674 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001675 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001676 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001677 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001678 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001679 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001680 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1681 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001682 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001683 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1684 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1685
1686 // Check the test is valid. We should have distortion from the filter
1687 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001688 frame_->samples_per_channel_ = 320;
1689 frame_->num_channels_ = 2;
1690 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001691 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001692 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001693 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001694 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001695 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1696 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1697}
1698
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001699#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1700void ApmTest::ProcessDebugDump(const std::string& in_filename,
1701 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001702 Format format,
1703 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001704 FILE* in_file = fopen(in_filename.c_str(), "rb");
1705 ASSERT_TRUE(in_file != NULL);
1706 audioproc::Event event_msg;
1707 bool first_init = true;
1708
1709 while (ReadMessageFromFile(in_file, &event_msg)) {
1710 if (event_msg.type() == audioproc::Event::INIT) {
1711 const audioproc::Init msg = event_msg.init();
1712 int reverse_sample_rate = msg.sample_rate();
1713 if (msg.has_reverse_sample_rate()) {
1714 reverse_sample_rate = msg.reverse_sample_rate();
1715 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001716 int output_sample_rate = msg.sample_rate();
1717 if (msg.has_output_sample_rate()) {
1718 output_sample_rate = msg.output_sample_rate();
1719 }
1720
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001721 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001722 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001723 reverse_sample_rate,
1724 msg.num_input_channels(),
1725 msg.num_output_channels(),
1726 msg.num_reverse_channels(),
1727 false);
1728 if (first_init) {
1729 // StartDebugRecording() writes an additional init message. Don't start
1730 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001731 EXPECT_NOERR(
1732 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001733 first_init = false;
1734 }
1735
1736 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1737 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1738
1739 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001740 ASSERT_EQ(revframe_->num_channels_,
1741 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001742 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001743 memcpy(revfloat_cb_->channels()[i],
1744 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001745 msg.channel(i).size());
1746 }
1747 } else {
1748 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1749 if (format == kFloatFormat) {
1750 // We're using an int16 input file; convert to float.
1751 ConvertToFloat(*revframe_, revfloat_cb_.get());
1752 }
1753 }
1754 AnalyzeReverseStreamChooser(format);
1755
1756 } else if (event_msg.type() == audioproc::Event::STREAM) {
1757 const audioproc::Stream msg = event_msg.stream();
1758 // ProcessStream could have changed this for the output frame.
1759 frame_->num_channels_ = apm_->num_input_channels();
1760
1761 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1762 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1763 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1764 if (msg.has_keypress()) {
1765 apm_->set_stream_key_pressed(msg.keypress());
1766 } else {
1767 apm_->set_stream_key_pressed(true);
1768 }
1769
1770 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001771 ASSERT_EQ(frame_->num_channels_,
1772 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001773 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001774 memcpy(float_cb_->channels()[i],
1775 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001776 msg.input_channel(i).size());
1777 }
1778 } else {
1779 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1780 if (format == kFloatFormat) {
1781 // We're using an int16 input file; convert to float.
1782 ConvertToFloat(*frame_, float_cb_.get());
1783 }
1784 }
1785 ProcessStreamChooser(format);
1786 }
1787 }
1788 EXPECT_NOERR(apm_->StopDebugRecording());
1789 fclose(in_file);
1790}
1791
1792void ApmTest::VerifyDebugDumpTest(Format format) {
1793 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001794 std::string format_string;
1795 switch (format) {
1796 case kIntFormat:
1797 format_string = "_int";
1798 break;
1799 case kFloatFormat:
1800 format_string = "_float";
1801 break;
1802 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001803 const std::string ref_filename = test::TempFilename(
1804 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1805 const std::string out_filename = test::TempFilename(
1806 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001807 const std::string limited_filename = test::TempFilename(
1808 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1809 const size_t logging_limit_bytes = 100000;
1810 // We expect at least this many bytes in the created logfile.
1811 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001812 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001813 ProcessDebugDump(in_filename, ref_filename, format, -1);
1814 ProcessDebugDump(ref_filename, out_filename, format, -1);
1815 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001816
1817 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1818 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001819 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001820 ASSERT_TRUE(ref_file != NULL);
1821 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001822 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001823 std::unique_ptr<uint8_t[]> ref_bytes;
1824 std::unique_ptr<uint8_t[]> out_bytes;
1825 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001826
1827 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1828 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001829 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001830 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001831 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001832 while (ref_size > 0 && out_size > 0) {
1833 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001834 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001835 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001836 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001837 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001838 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001839 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1840 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001841 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001842 }
1843 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001844 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1845 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001846 EXPECT_NE(0, feof(ref_file));
1847 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001848 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001849 ASSERT_EQ(0, fclose(ref_file));
1850 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001851 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001852 remove(ref_filename.c_str());
1853 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001854 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001855}
1856
pbosc7a65692016-05-06 12:50:04 -07001857TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001858 VerifyDebugDumpTest(kIntFormat);
1859}
1860
pbosc7a65692016-05-06 12:50:04 -07001861TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001862 VerifyDebugDumpTest(kFloatFormat);
1863}
1864#endif
1865
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001866// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001867TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001868 const std::string filename =
1869 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001870 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001871 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001872
1873#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1874 // Stopping without having started should be OK.
1875 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1876
ivocd66b44d2016-01-15 03:06:36 -08001877 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001878 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001879 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001880 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1881
1882 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001883 FILE* fid = fopen(filename.c_str(), "r");
1884 ASSERT_TRUE(fid != NULL);
1885
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001886 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001887 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001888 ASSERT_EQ(0, remove(filename.c_str()));
1889#else
1890 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001891 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001892 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1893
1894 // Verify the file has NOT been written.
1895 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1896#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1897}
1898
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001899// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001900TEST_F(ApmTest, DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001901 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001902 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001903 const std::string filename =
1904 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001905 fid = fopen(filename.c_str(), "w");
1906 ASSERT_TRUE(fid);
1907
1908#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1909 // Stopping without having started should be OK.
1910 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1911
ivocd66b44d2016-01-15 03:06:36 -08001912 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001913 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001914 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1915 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1916
1917 // Verify the file has been written.
1918 fid = fopen(filename.c_str(), "r");
1919 ASSERT_TRUE(fid != NULL);
1920
1921 // Clean it up.
1922 ASSERT_EQ(0, fclose(fid));
1923 ASSERT_EQ(0, remove(filename.c_str()));
1924#else
1925 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001926 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001927 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1928
1929 ASSERT_EQ(0, fclose(fid));
1930#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1931}
1932
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001933TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001934 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001935 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001936
1937 Config config;
1938 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001939 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001940 EnableAllComponents();
1941 EnableAllAPComponents(fapm.get());
1942 for (int i = 0; i < ref_data.test_size(); i++) {
1943 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1944
1945 audioproc::Test* test = ref_data.mutable_test(i);
1946 // TODO(ajm): Restore downmixing test cases.
1947 if (test->num_input_channels() != test->num_output_channels())
1948 continue;
1949
Peter Kasting69558702016-01-12 16:26:35 -08001950 const size_t num_render_channels =
1951 static_cast<size_t>(test->num_reverse_channels());
1952 const size_t num_input_channels =
1953 static_cast<size_t>(test->num_input_channels());
1954 const size_t num_output_channels =
1955 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001956 const size_t samples_per_channel = static_cast<size_t>(
1957 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001958
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001959 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1960 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001961 Init(fapm.get());
1962
1963 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001964 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1965 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001966
1967 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001968 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001969 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1970 ReadFrame(near_file_, frame_, float_cb_.get())) {
1971 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1972
aluebsb0319552016-03-17 20:39:53 -07001973 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001974 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1975 revfloat_cb_->channels(),
1976 samples_per_channel,
1977 test->sample_rate(),
1978 LayoutFromChannels(num_render_channels)));
1979
1980 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1981 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1982 apm_->echo_cancellation()->set_stream_drift_samples(0);
1983 fapm->echo_cancellation()->set_stream_drift_samples(0);
1984 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1985 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1986
1987 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001988 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
1989 output_int16.channels());
1990
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001991 EXPECT_NOERR(fapm->ProcessStream(
1992 float_cb_->channels(),
1993 samples_per_channel,
1994 test->sample_rate(),
1995 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001996 test->sample_rate(),
1997 LayoutFromChannels(num_output_channels),
1998 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001999 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002000 FloatToS16(float_cb_->channels()[j],
2001 samples_per_channel,
2002 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002003 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002004 float snr = ComputeSNR(output_int16.channels()[j],
2005 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002006 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002007
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002008 const float kVarianceThreshold = 20;
2009 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002010
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002011 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002012 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2013 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002014 }
2015 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002016
2017 analog_level = fapm->gain_control()->stream_analog_level();
2018 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2019 fapm->gain_control()->stream_analog_level());
2020 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2021 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002022 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2023 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002024 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002025
2026 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002027 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002028 }
aluebs776593b2016-03-15 14:04:58 -07002029
2030#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2031 const size_t kMaxNumBadChunks = 0;
2032#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2033 // There are a few chunks in the fixed-point profile that give low SNR.
2034 // Listening confirmed the difference is acceptable.
2035 const size_t kMaxNumBadChunks = 60;
2036#endif
2037 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2038
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002039 rewind(far_file_);
2040 rewind(near_file_);
2041 }
2042}
2043
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002044// TODO(andrew): Add a test to process a few frames with different combinations
2045// of enabled components.
2046
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002047TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002048 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002049 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002050
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002051 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002052 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002053 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002054 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002055 for (size_t i = 0; i < arraysize(kChannels); i++) {
2056 for (size_t j = 0; j < arraysize(kChannels); j++) {
2057 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002058 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002059 test->set_num_reverse_channels(kChannels[i]);
2060 test->set_num_input_channels(kChannels[j]);
2061 test->set_num_output_channels(kChannels[j]);
2062 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002063 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002064 }
2065 }
2066 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002067#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2068 // To test the extended filter mode.
2069 audioproc::Test* test = ref_data.add_test();
2070 test->set_num_reverse_channels(2);
2071 test->set_num_input_channels(2);
2072 test->set_num_output_channels(2);
2073 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2074 test->set_use_aec_extended_filter(true);
2075#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002076 }
2077
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002078 for (int i = 0; i < ref_data.test_size(); i++) {
2079 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002080
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002081 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002082 // TODO(ajm): We no longer allow different input and output channels. Skip
2083 // these tests for now, but they should be removed from the set.
2084 if (test->num_input_channels() != test->num_output_channels())
2085 continue;
2086
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002087 Config config;
2088 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002089 config.Set<ExtendedFilter>(
2090 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002091 apm_.reset(AudioProcessing::Create(config));
2092
2093 EnableAllComponents();
2094
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002095 Init(test->sample_rate(),
2096 test->sample_rate(),
2097 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002098 static_cast<size_t>(test->num_input_channels()),
2099 static_cast<size_t>(test->num_output_channels()),
2100 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002101 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002102
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002103 int frame_count = 0;
2104 int has_echo_count = 0;
2105 int has_voice_count = 0;
2106 int is_saturated_count = 0;
2107 int analog_level = 127;
2108 int analog_level_average = 0;
2109 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002110 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002111#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2112 int stats_index = 0;
2113#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002114
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002115 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002116 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002117
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002118 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2119
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002120 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002121 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002122 EXPECT_EQ(apm_->kNoError,
2123 apm_->gain_control()->set_stream_analog_level(analog_level));
2124
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002125 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002126
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002127 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002128 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2129 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002130
2131 max_output_average += MaxAudioFrame(*frame_);
2132
2133 if (apm_->echo_cancellation()->stream_has_echo()) {
2134 has_echo_count++;
2135 }
2136
2137 analog_level = apm_->gain_control()->stream_analog_level();
2138 analog_level_average += analog_level;
2139 if (apm_->gain_control()->stream_is_saturated()) {
2140 is_saturated_count++;
2141 }
2142 if (apm_->voice_detection()->stream_has_voice()) {
2143 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002144 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002145 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002146 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002147 }
2148
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002149 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2150
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002151 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002152 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002153 sizeof(int16_t),
2154 frame_size,
2155 out_file_);
2156 ASSERT_EQ(frame_size, write_count);
2157
2158 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002159 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002160 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002161
2162#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2163 const int kStatsAggregationFrameNum = 100; // 1 second.
2164 if (frame_count % kStatsAggregationFrameNum == 0) {
2165 // Get echo metrics.
2166 EchoCancellation::Metrics echo_metrics;
2167 EXPECT_EQ(apm_->kNoError,
2168 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2169
2170 // Get delay metrics.
2171 int median = 0;
2172 int std = 0;
2173 float fraction_poor_delays = 0;
2174 EXPECT_EQ(apm_->kNoError,
2175 apm_->echo_cancellation()->GetDelayMetrics(
2176 &median, &std, &fraction_poor_delays));
2177
2178 // Get RMS.
2179 int rms_level = apm_->level_estimator()->RMS();
2180 EXPECT_LE(0, rms_level);
2181 EXPECT_GE(127, rms_level);
2182
2183 if (!write_ref_data) {
2184 const audioproc::Test::EchoMetrics& reference =
2185 test->echo_metrics(stats_index);
2186 TestStats(echo_metrics.residual_echo_return_loss,
2187 reference.residual_echo_return_loss());
2188 TestStats(echo_metrics.echo_return_loss,
2189 reference.echo_return_loss());
2190 TestStats(echo_metrics.echo_return_loss_enhancement,
2191 reference.echo_return_loss_enhancement());
2192 TestStats(echo_metrics.a_nlp,
2193 reference.a_nlp());
2194 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2195 reference.divergent_filter_fraction());
2196
2197 const audioproc::Test::DelayMetrics& reference_delay =
2198 test->delay_metrics(stats_index);
2199 EXPECT_EQ(reference_delay.median(), median);
2200 EXPECT_EQ(reference_delay.std(), std);
2201 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2202 fraction_poor_delays);
2203
2204 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2205
2206 ++stats_index;
2207 } else {
2208 audioproc::Test::EchoMetrics* message =
2209 test->add_echo_metrics();
2210 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2211 message->mutable_residual_echo_return_loss());
2212 WriteStatsMessage(echo_metrics.echo_return_loss,
2213 message->mutable_echo_return_loss());
2214 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2215 message->mutable_echo_return_loss_enhancement());
2216 WriteStatsMessage(echo_metrics.a_nlp,
2217 message->mutable_a_nlp());
2218 message->set_divergent_filter_fraction(
2219 echo_metrics.divergent_filter_fraction);
2220
2221 audioproc::Test::DelayMetrics* message_delay =
2222 test->add_delay_metrics();
2223 message_delay->set_median(median);
2224 message_delay->set_std(std);
2225 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2226
2227 test->add_rms_level(rms_level);
2228 }
2229 }
2230#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002231 }
2232 max_output_average /= frame_count;
2233 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002234 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002235
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002236 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002237 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002238 // When running the test on a N7 we get a {2, 6} difference of
2239 // |has_voice_count| and |max_output_average| is up to 18 higher.
2240 // All numbers being consistently higher on N7 compare to ref_data.
2241 // TODO(bjornv): If we start getting more of these offsets on Android we
2242 // should consider a different approach. Either using one slack for all,
2243 // or generate a separate android reference.
2244#if defined(WEBRTC_ANDROID)
2245 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002246 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002247 const int kMaxOutputAverageOffset = 9;
2248 const int kMaxOutputAverageNear = 9;
2249#else
2250 const int kHasVoiceCountOffset = 0;
2251 const int kHasVoiceCountNear = kIntNear;
2252 const int kMaxOutputAverageOffset = 0;
2253 const int kMaxOutputAverageNear = kIntNear;
2254#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002255 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002256 EXPECT_NEAR(test->has_voice_count(),
2257 has_voice_count - kHasVoiceCountOffset,
2258 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002259 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002260
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002261 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002262 EXPECT_NEAR(test->max_output_average(),
2263 max_output_average - kMaxOutputAverageOffset,
2264 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002265#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002266 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002267 EXPECT_NEAR(test->ns_speech_probability_average(),
2268 ns_speech_prob_average,
2269 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002270#endif
2271 } else {
2272 test->set_has_echo_count(has_echo_count);
2273 test->set_has_voice_count(has_voice_count);
2274 test->set_is_saturated_count(is_saturated_count);
2275
2276 test->set_analog_level_average(analog_level_average);
2277 test->set_max_output_average(max_output_average);
2278
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002279#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002280 EXPECT_LE(0.0f, ns_speech_prob_average);
2281 EXPECT_GE(1.0f, ns_speech_prob_average);
2282 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002283#endif
2284 }
2285
2286 rewind(far_file_);
2287 rewind(near_file_);
2288 }
2289
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002290 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002291 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002292 }
2293}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002294
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002295TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2296 struct ChannelFormat {
2297 AudioProcessing::ChannelLayout in_layout;
2298 AudioProcessing::ChannelLayout out_layout;
2299 };
2300 ChannelFormat cf[] = {
2301 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2302 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2303 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2304 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002305
kwiberg62eaacf2016-02-17 06:39:05 -08002306 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002307 // Enable one component just to ensure some processing takes place.
2308 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002309 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002310 const int in_rate = 44100;
2311 const int out_rate = 48000;
2312 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2313 TotalChannelsFromLayout(cf[i].in_layout));
2314 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2315 ChannelsFromLayout(cf[i].out_layout));
2316
2317 // Run over a few chunks.
2318 for (int j = 0; j < 10; ++j) {
2319 EXPECT_NOERR(ap->ProcessStream(
2320 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002321 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002322 in_rate,
2323 cf[i].in_layout,
2324 out_rate,
2325 cf[i].out_layout,
2326 out_cb.channels()));
2327 }
2328 }
2329}
2330
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002331// Compares the reference and test arrays over a region around the expected
2332// delay. Finds the highest SNR in that region and adds the variance and squared
2333// error results to the supplied accumulators.
2334void UpdateBestSNR(const float* ref,
2335 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002336 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002337 int expected_delay,
2338 double* variance_acc,
2339 double* sq_error_acc) {
2340 double best_snr = std::numeric_limits<double>::min();
2341 double best_variance = 0;
2342 double best_sq_error = 0;
2343 // Search over a region of eight samples around the expected delay.
2344 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2345 ++delay) {
2346 double sq_error = 0;
2347 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002348 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002349 double error = test[i + delay] - ref[i];
2350 sq_error += error * error;
2351 variance += ref[i] * ref[i];
2352 }
2353
2354 if (sq_error == 0) {
2355 *variance_acc += variance;
2356 return;
2357 }
2358 double snr = variance / sq_error;
2359 if (snr > best_snr) {
2360 best_snr = snr;
2361 best_variance = variance;
2362 best_sq_error = sq_error;
2363 }
2364 }
2365
2366 *variance_acc += best_variance;
2367 *sq_error_acc += best_sq_error;
2368}
2369
2370// Used to test a multitude of sample rate and channel combinations. It works
2371// by first producing a set of reference files (in SetUpTestCase) that are
2372// assumed to be correct, as the used parameters are verified by other tests
2373// in this collection. Primarily the reference files are all produced at
2374// "native" rates which do not involve any resampling.
2375
2376// Each test pass produces an output file with a particular format. The output
2377// is matched against the reference file closest to its internal processing
2378// format. If necessary the output is resampled back to its process format.
2379// Due to the resampling distortion, we don't expect identical results, but
2380// enforce SNR thresholds which vary depending on the format. 0 is a special
2381// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002382typedef std::tr1::tuple<int, int, int, int, double, double>
2383 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002384class AudioProcessingTest
2385 : public testing::TestWithParam<AudioProcessingTestData> {
2386 public:
2387 AudioProcessingTest()
2388 : input_rate_(std::tr1::get<0>(GetParam())),
2389 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002390 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2391 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2392 expected_snr_(std::tr1::get<4>(GetParam())),
2393 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002394
2395 virtual ~AudioProcessingTest() {}
2396
2397 static void SetUpTestCase() {
2398 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002399 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002400 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002401 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2402 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2403 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002404 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002405 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2406 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2407 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002408 }
2409 }
2410 }
2411 }
2412
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002413 static void TearDownTestCase() {
2414 ClearTempFiles();
2415 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002416
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002417 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002418 // to a file specified with |output_file_prefix|. Both forward and reverse
2419 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002420 static void ProcessFormat(int input_rate,
2421 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002422 int reverse_input_rate,
2423 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002424 size_t num_input_channels,
2425 size_t num_output_channels,
2426 size_t num_reverse_input_channels,
2427 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002428 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002429 Config config;
2430 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002431 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002433
ekmeyerson60d9b332015-08-14 10:35:55 -07002434 ProcessingConfig processing_config = {
2435 {{input_rate, num_input_channels},
2436 {output_rate, num_output_channels},
2437 {reverse_input_rate, num_reverse_input_channels},
2438 {reverse_output_rate, num_reverse_output_channels}}};
2439 ap->Initialize(processing_config);
2440
2441 FILE* far_file =
2442 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002443 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002444 FILE* out_file =
2445 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2446 reverse_input_rate, reverse_output_rate,
2447 num_input_channels, num_output_channels,
2448 num_reverse_input_channels,
2449 num_reverse_output_channels, kForward).c_str(),
2450 "wb");
2451 FILE* rev_out_file =
2452 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2453 reverse_input_rate, reverse_output_rate,
2454 num_input_channels, num_output_channels,
2455 num_reverse_input_channels,
2456 num_reverse_output_channels, kReverse).c_str(),
2457 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002458 ASSERT_TRUE(far_file != NULL);
2459 ASSERT_TRUE(near_file != NULL);
2460 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002461 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002462
2463 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2464 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002465 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2466 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002467 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2468 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002469 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2470 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002471
2472 // Temporary buffers.
2473 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2475 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002476 std::unique_ptr<float[]> float_data(new float[max_length]);
2477 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002478
2479 int analog_level = 127;
2480 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2481 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002482 EXPECT_NOERR(ap->ProcessReverseStream(
2483 rev_cb.channels(), processing_config.reverse_input_stream(),
2484 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002485
2486 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2487 ap->echo_cancellation()->set_stream_drift_samples(0);
2488 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2489
2490 EXPECT_NOERR(ap->ProcessStream(
2491 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002492 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002493 input_rate,
2494 LayoutFromChannels(num_input_channels),
2495 output_rate,
2496 LayoutFromChannels(num_output_channels),
2497 out_cb.channels()));
2498
ekmeyerson60d9b332015-08-14 10:35:55 -07002499 // Dump forward output to file.
2500 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002501 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002502 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002503
pkasting25702cb2016-01-08 13:50:27 -08002504 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002505 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002506 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002507
ekmeyerson60d9b332015-08-14 10:35:55 -07002508 // Dump reverse output to file.
2509 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2510 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002511 size_t rev_out_length =
2512 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002513
pkasting25702cb2016-01-08 13:50:27 -08002514 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002515 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2516 rev_out_file));
2517
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002518 analog_level = ap->gain_control()->stream_analog_level();
2519 }
2520 fclose(far_file);
2521 fclose(near_file);
2522 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002523 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002524 }
2525
2526 protected:
2527 int input_rate_;
2528 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002529 int reverse_input_rate_;
2530 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002531 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002532 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002533};
2534
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002535TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002536 struct ChannelFormat {
2537 int num_input;
2538 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002539 int num_reverse_input;
2540 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002541 };
2542 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002543 {1, 1, 1, 1},
2544 {1, 1, 2, 1},
2545 {2, 1, 1, 1},
2546 {2, 1, 2, 1},
2547 {2, 2, 1, 1},
2548 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002549 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002550
pkasting25702cb2016-01-08 13:50:27 -08002551 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002552 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2553 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2554 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002555
ekmeyerson60d9b332015-08-14 10:35:55 -07002556 // Verify output for both directions.
2557 std::vector<StreamDirection> stream_directions;
2558 stream_directions.push_back(kForward);
2559 stream_directions.push_back(kReverse);
2560 for (StreamDirection file_direction : stream_directions) {
2561 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2562 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2563 const int out_num =
2564 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2565 const double expected_snr =
2566 file_direction ? expected_reverse_snr_ : expected_snr_;
2567
2568 const int min_ref_rate = std::min(in_rate, out_rate);
2569 int ref_rate;
2570
2571 if (min_ref_rate > 32000) {
2572 ref_rate = 48000;
2573 } else if (min_ref_rate > 16000) {
2574 ref_rate = 32000;
2575 } else if (min_ref_rate > 8000) {
2576 ref_rate = 16000;
2577 } else {
2578 ref_rate = 8000;
2579 }
aluebs776593b2016-03-15 14:04:58 -07002580#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002581 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002582 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002583 }
2584#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002585 FILE* out_file = fopen(
2586 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2587 reverse_output_rate_, cf[i].num_input,
2588 cf[i].num_output, cf[i].num_reverse_input,
2589 cf[i].num_reverse_output, file_direction).c_str(),
2590 "rb");
2591 // The reference files always have matching input and output channels.
2592 FILE* ref_file = fopen(
2593 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2594 cf[i].num_output, cf[i].num_output,
2595 cf[i].num_reverse_output, cf[i].num_reverse_output,
2596 file_direction).c_str(),
2597 "rb");
2598 ASSERT_TRUE(out_file != NULL);
2599 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002600
pkasting25702cb2016-01-08 13:50:27 -08002601 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2602 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002603 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002604 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002605 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002606 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002607 // Data from the resampled output, in case the reference and output rates
2608 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002609 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002610
ekmeyerson60d9b332015-08-14 10:35:55 -07002611 PushResampler<float> resampler;
2612 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002613
ekmeyerson60d9b332015-08-14 10:35:55 -07002614 // Compute the resampling delay of the output relative to the reference,
2615 // to find the region over which we should search for the best SNR.
2616 float expected_delay_sec = 0;
2617 if (in_rate != ref_rate) {
2618 // Input resampling delay.
2619 expected_delay_sec +=
2620 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2621 }
2622 if (out_rate != ref_rate) {
2623 // Output resampling delay.
2624 expected_delay_sec +=
2625 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2626 // Delay of converting the output back to its processing rate for
2627 // testing.
2628 expected_delay_sec +=
2629 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2630 }
2631 int expected_delay =
2632 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002633
ekmeyerson60d9b332015-08-14 10:35:55 -07002634 double variance = 0;
2635 double sq_error = 0;
2636 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2637 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2638 float* out_ptr = out_data.get();
2639 if (out_rate != ref_rate) {
2640 // Resample the output back to its internal processing rate if
2641 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002642 ASSERT_EQ(ref_length,
2643 static_cast<size_t>(resampler.Resample(
2644 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002645 out_ptr = cmp_data.get();
2646 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002647
ekmeyerson60d9b332015-08-14 10:35:55 -07002648 // Update the |sq_error| and |variance| accumulators with the highest
2649 // SNR of reference vs output.
2650 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2651 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002652 }
2653
ekmeyerson60d9b332015-08-14 10:35:55 -07002654 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2655 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2656 << cf[i].num_input << ", " << cf[i].num_output << ", "
2657 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2658 << ", " << file_direction << "): ";
2659 if (sq_error > 0) {
2660 double snr = 10 * log10(variance / sq_error);
2661 EXPECT_GE(snr, expected_snr);
2662 EXPECT_NE(0, expected_snr);
2663 std::cout << "SNR=" << snr << " dB" << std::endl;
2664 } else {
aluebs776593b2016-03-15 14:04:58 -07002665 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002666 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002667
ekmeyerson60d9b332015-08-14 10:35:55 -07002668 fclose(out_file);
2669 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002670 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002671 }
2672}
2673
2674#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2675INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002676 CommonFormats,
2677 AudioProcessingTest,
2678 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002679 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2680 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002681 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2682 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2683 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2684 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2685 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2686 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2687 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2688 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2689 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002690
ekmeyerson60d9b332015-08-14 10:35:55 -07002691 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2692 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2693 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2694 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2695 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2696 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2697 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2698 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2699 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2700 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2701 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2702 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002703
ekmeyerson60d9b332015-08-14 10:35:55 -07002704 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2705 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2706 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2707 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2708 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2709 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2710 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2711 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2712 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2713 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2714 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2715 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002716
ekmeyerson60d9b332015-08-14 10:35:55 -07002717 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2718 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2719 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2720 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2721 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2722 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2723 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2724 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2725 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2726 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002727 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002728 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002729
2730#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2731INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002732 CommonFormats,
2733 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002734 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2735 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2736 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2737 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2738 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2739 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002740 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2741 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2742 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2743 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2744 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2745 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002746
aluebs776593b2016-03-15 14:04:58 -07002747 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2748 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2749 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002750 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2751 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2752 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2753 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2754 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2755 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2756 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2757 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2758 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002759
aluebs776593b2016-03-15 14:04:58 -07002760 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2761 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2762 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2763 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2764 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2765 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2766 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2767 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2768 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002769 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2770 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2771 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002772
ekmeyerson60d9b332015-08-14 10:35:55 -07002773 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2774 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2775 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2776 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2777 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2778 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2779 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2780 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2781 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2782 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002783 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002784 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002785#endif
2786
niklase@google.com470e71d2011-07-07 08:21:25 +00002787} // namespace
peahc19f3122016-10-07 14:54:10 -07002788
2789TEST(ApmConfiguration, DefaultBehavior) {
2790 // Verify that the level controller is default off, it can be activated using
2791 // the config, and that the default initial level is maintained after the
2792 // config has been applied.
2793 std::unique_ptr<AudioProcessingImpl> apm(
2794 new AudioProcessingImpl(webrtc::Config()));
2795 AudioProcessing::Config config;
2796 EXPECT_FALSE(apm->config_.level_controller.enabled);
2797 // TODO(peah): Add test for the existence of the level controller object once
2798 // that is created only when that is specified in the config.
2799 // TODO(peah): Remove the testing for
2800 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2801 // is instead used to activate the level controller.
2802 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2803 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2804 apm->config_.level_controller.initial_peak_level_dbfs,
2805 std::numeric_limits<float>::epsilon());
2806 config.level_controller.enabled = true;
2807 apm->ApplyConfig(config);
2808 EXPECT_TRUE(apm->config_.level_controller.enabled);
2809 // TODO(peah): Add test for the existence of the level controller object once
2810 // that is created only when the that is specified in the config.
2811 // TODO(peah): Remove the testing for
2812 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2813 // is instead used to activate the level controller.
2814 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2815 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2816 apm->config_.level_controller.initial_peak_level_dbfs,
2817 std::numeric_limits<float>::epsilon());
2818}
2819
2820TEST(ApmConfiguration, ValidConfigBehavior) {
2821 // Verify that the initial level can be specified and is retained after the
2822 // config has been applied.
2823 std::unique_ptr<AudioProcessingImpl> apm(
2824 new AudioProcessingImpl(webrtc::Config()));
2825 AudioProcessing::Config config;
2826 config.level_controller.initial_peak_level_dbfs = -50.f;
2827 apm->ApplyConfig(config);
2828 EXPECT_FALSE(apm->config_.level_controller.enabled);
2829 // TODO(peah): Add test for the existence of the level controller object once
2830 // that is created only when the that is specified in the config.
2831 // TODO(peah): Remove the testing for
2832 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2833 // is instead used to activate the level controller.
2834 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2835 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2836 std::numeric_limits<float>::epsilon());
2837}
2838
2839TEST(ApmConfiguration, InValidConfigBehavior) {
2840 // Verify that the config is properly reset when nonproper values are applied
2841 // for the initial level.
2842
2843 // Verify that the config is properly reset when the specified initial peak
2844 // level is too low.
2845 std::unique_ptr<AudioProcessingImpl> apm(
2846 new AudioProcessingImpl(webrtc::Config()));
2847 AudioProcessing::Config config;
2848 config.level_controller.enabled = true;
2849 config.level_controller.initial_peak_level_dbfs = -101.f;
2850 apm->ApplyConfig(config);
2851 EXPECT_FALSE(apm->config_.level_controller.enabled);
2852 // TODO(peah): Add test for the existence of the level controller object once
2853 // that is created only when the that is specified in the config.
2854 // TODO(peah): Remove the testing for
2855 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2856 // is instead used to activate the level controller.
2857 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2858 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2859 apm->config_.level_controller.initial_peak_level_dbfs,
2860 std::numeric_limits<float>::epsilon());
2861
2862 // Verify that the config is properly reset when the specified initial peak
2863 // level is too high.
2864 apm.reset(new AudioProcessingImpl(webrtc::Config()));
2865 config = AudioProcessing::Config();
2866 config.level_controller.enabled = true;
2867 config.level_controller.initial_peak_level_dbfs = 1.f;
2868 apm->ApplyConfig(config);
2869 EXPECT_FALSE(apm->config_.level_controller.enabled);
2870 // TODO(peah): Add test for the existence of the level controller object once
2871 // that is created only when that is specified in the config.
2872 // TODO(peah): Remove the testing for
2873 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2874 // is instead used to activate the level controller.
2875 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2876 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2877 apm->config_.level_controller.initial_peak_level_dbfs,
2878 std::numeric_limits<float>::epsilon());
2879}
2880
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002881} // namespace webrtc