blob: 814eea996745de865adbf5aed4a65a4fe5e820d7 [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"
mbonadei16ab93b2017-03-30 01:24:20 -070023#include "webrtc/base/protobuf_utils.h"
andrew@webrtc.org27c69802014-02-18 20:24:56 +000024#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000025#include "webrtc/common_audio/resampler/include/push_resampler.h"
26#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000027#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
peahc19f3122016-10-07 14:54:10 -070028#include "webrtc/modules/audio_processing/audio_processing_impl.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000029#include "webrtc/modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000030#include "webrtc/modules/audio_processing/common.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000031#include "webrtc/modules/audio_processing/include/audio_processing.h"
peahc19f3122016-10-07 14:54:10 -070032#include "webrtc/modules/audio_processing/level_controller/level_controller_constants.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070033#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000034#include "webrtc/modules/audio_processing/test/test_utils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010035#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010036#include "webrtc/system_wrappers/include/event_wrapper.h"
37#include "webrtc/system_wrappers/include/trace.h"
kwiberg77eab702016-09-28 17:42:01 -070038#include "webrtc/test/gtest.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000039#include "webrtc/test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070040
41RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000042#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000043#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000044#else
kjellandere3e902e2017-02-28 08:01:46 -080045#include "webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000046#endif
kwiberg77eab702016-09-28 17:42:01 -070047RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000048
andrew@webrtc.org27c69802014-02-18 20:24:56 +000049namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000050namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000051
ekmeyerson60d9b332015-08-14 10:35:55 -070052// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
53// applicable.
54
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000055// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000056// re-implemented; see comment at the bottom of this file. For now, the user has
57// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000058// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000059// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000060// be set to true with the command-line switch --write_ref_data.
61bool write_ref_data = false;
mbonadei16ab93b2017-03-30 01:24:20 -070062const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070063const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000064
aluebseb3603b2016-04-20 15:27:58 -070065#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
66// Android doesn't support 48kHz.
67const int kProcessSampleRates[] = {8000, 16000, 32000};
68#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070069const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070070#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000071
ekmeyerson60d9b332015-08-14 10:35:55 -070072enum StreamDirection { kForward = 0, kReverse };
73
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000074void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000075 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000076 cb->num_channels());
77 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000078 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000079 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000080 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080081 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000082 S16ToFloat(cb_int.channels()[i],
83 cb->num_frames(),
84 cb->channels()[i]);
85 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000086}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000087
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000088void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
89 ConvertToFloat(frame.data_, cb);
90}
91
andrew@webrtc.org103657b2014-04-24 18:28:56 +000092// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080093size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000094 switch (layout) {
95 case AudioProcessing::kMono:
96 return 1;
97 case AudioProcessing::kMonoAndKeyboard:
98 case AudioProcessing::kStereo:
99 return 2;
100 case AudioProcessing::kStereoAndKeyboard:
101 return 3;
102 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700103 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800104 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000105}
106
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000107int TruncateToMultipleOf10(int value) {
108 return (value / 10) * 10;
109}
110
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000111void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800112 size_t samples_per_channel) {
113 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000115}
116
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800118 size_t samples_per_channel) {
119 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
121}
122
pkasting25702cb2016-01-08 13:50:27 -0800123void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
124 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000125 stereo[i * 2 + 1] = stereo[i * 2];
126 }
127}
128
pkasting25702cb2016-01-08 13:50:27 -0800129void VerifyChannelsAreEqual(int16_t* stereo, size_t samples_per_channel) {
130 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
132 }
133}
134
135void SetFrameTo(AudioFrame* frame, int16_t value) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
137 ++i) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000138 frame->data_[i] = value;
139 }
140}
141
142void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800143 ASSERT_EQ(2u, frame->num_channels_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700144 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000145 frame->data_[i] = left;
146 frame->data_[i + 1] = right;
147 }
148}
149
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000150void ScaleFrame(AudioFrame* frame, float scale) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
152 ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000153 frame->data_[i] = FloatS16ToS16(frame->data_[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000154 }
155}
156
andrew@webrtc.org81865342012-10-27 00:28:27 +0000157bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000158 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000159 return false;
160 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000161 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000162 return false;
163 }
164 if (memcmp(frame1.data_, frame2.data_,
165 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000166 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000167 return false;
168 }
169 return true;
170}
171
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000172void EnableAllAPComponents(AudioProcessing* ap) {
173#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
174 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
175
176 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
177 EXPECT_NOERR(ap->gain_control()->Enable(true));
178#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
179 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
180 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
181 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
182 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
183
184 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
185 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
186 EXPECT_NOERR(ap->gain_control()->Enable(true));
187#endif
188
peah8271d042016-11-22 07:24:52 -0800189 AudioProcessing::Config apm_config;
190 apm_config.high_pass_filter.enabled = true;
191 ap->ApplyConfig(apm_config);
192
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000193 EXPECT_NOERR(ap->level_estimator()->Enable(true));
194 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
195
196 EXPECT_NOERR(ap->voice_detection()->Enable(true));
197}
198
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000199// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000200template <class T>
201T AbsValue(T a) {
202 return a > 0 ? a: -a;
203}
204
205int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800206 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000207 int16_t max_data = AbsValue(frame.data_[0]);
pkasting25702cb2016-01-08 13:50:27 -0800208 for (size_t i = 1; i < length; i++) {
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000209 max_data = std::max(max_data, AbsValue(frame.data_[i]));
210 }
211
212 return max_data;
213}
214
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000215#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000216void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000217 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700218 EXPECT_EQ(reference.instant(), test.instant);
219 EXPECT_EQ(reference.average(), test.average);
220 EXPECT_EQ(reference.maximum(), test.maximum);
221 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000222}
223
224void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000225 audioproc::Test::Statistic* msg) {
226 msg->set_instant(output.instant);
227 msg->set_average(output.average);
228 msg->set_maximum(output.maximum);
229 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000231#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233void OpenFileAndWriteMessage(const std::string filename,
mbonadei16ab93b2017-03-30 01:24:20 -0700234 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000235 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000236 ASSERT_TRUE(file != NULL);
237
238 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800240 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000243 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000245 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000246 fclose(file);
247}
248
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000249std::string ResourceFilePath(std::string name, int sample_rate_hz) {
250 std::ostringstream ss;
251 // Resource files are all stereo.
252 ss << name << sample_rate_hz / 1000 << "_stereo";
253 return test::ResourcePath(ss.str(), "pcm");
254}
255
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000256// Temporary filenames unique to this process. Used to be able to run these
257// tests in parallel as each process needs to be running in isolation they can't
258// have competing filenames.
259std::map<std::string, std::string> temp_filenames;
260
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000261std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000262 int input_rate,
263 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700264 int reverse_input_rate,
265 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800266 size_t num_input_channels,
267 size_t num_output_channels,
268 size_t num_reverse_input_channels,
269 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700270 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000271 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700272 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
273 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000274 if (num_output_channels == 1) {
275 ss << "mono";
276 } else if (num_output_channels == 2) {
277 ss << "stereo";
278 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700279 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000280 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700281 ss << output_rate / 1000;
282 if (num_reverse_output_channels == 1) {
283 ss << "_rmono";
284 } else if (num_reverse_output_channels == 2) {
285 ss << "_rstereo";
286 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700287 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700288 }
289 ss << reverse_output_rate / 1000;
290 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000291
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000292 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700293 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000294 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
295 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000296}
297
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000298void ClearTempFiles() {
299 for (auto& kv : temp_filenames)
300 remove(kv.second.c_str());
301}
302
mbonadei16ab93b2017-03-30 01:24:20 -0700303void OpenFileAndReadMessage(std::string filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000304 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000305 ASSERT_TRUE(file != NULL);
306 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000307 fclose(file);
308}
309
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000310// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
311// stereo) file, converts to deinterleaved float (optionally downmixing) and
312// returns the result in |cb|. Returns false if the file ended (or on error) and
313// true otherwise.
314//
315// |int_data| and |float_data| are just temporary space that must be
316// sufficiently large to hold the 10 ms chunk.
317bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
318 ChannelBuffer<float>* cb) {
319 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000320 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000321 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
322 if (read_count != frame_size) {
323 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700324 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000325 return false; // This is expected.
326 }
327
328 S16ToFloat(int_data, frame_size, float_data);
329 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000330 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000331 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000332 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000333 cb->channels());
334 }
335
336 return true;
337}
338
niklase@google.com470e71d2011-07-07 08:21:25 +0000339class ApmTest : public ::testing::Test {
340 protected:
341 ApmTest();
342 virtual void SetUp();
343 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000344
345 static void SetUpTestCase() {
346 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000347 }
348
349 static void TearDownTestCase() {
350 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000351 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000352 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000353
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000354 // Used to select between int and float interface tests.
355 enum Format {
356 kIntFormat,
357 kFloatFormat
358 };
359
360 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000361 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000362 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800363 size_t num_input_channels,
364 size_t num_output_channels,
365 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000366 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000367 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000368 void EnableAllComponents();
369 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000370 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000371 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000372 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
373 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000374 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000375 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
376 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700377 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800378 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700379 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800380 void TestChangingForwardChannels(size_t num_in_channels,
381 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700382 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800383 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700384 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000385 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
386 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000387 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000388 int ProcessStreamChooser(Format format);
389 int AnalyzeReverseStreamChooser(Format format);
390 void ProcessDebugDump(const std::string& in_filename,
391 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800392 Format format,
393 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000394 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000395
396 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000397 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800398 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000399 AudioFrame* frame_;
400 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800401 std::unique_ptr<ChannelBuffer<float> > float_cb_;
402 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000403 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800404 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000405 FILE* far_file_;
406 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000407 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408};
409
410ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000411 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000412#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800413 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
414 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000415#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000416#if defined(WEBRTC_MAC)
417 // A different file for Mac is needed because on this platform the AEC
418 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800419 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
420 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000421#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800422 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
423 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000424#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000425#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000426 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000427 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000428 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000429 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000430 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000431 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000432 out_file_(NULL) {
433 Config config;
434 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
435 apm_.reset(AudioProcessing::Create(config));
436}
niklase@google.com470e71d2011-07-07 08:21:25 +0000437
438void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000439 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
441 frame_ = new AudioFrame();
442 revframe_ = new AudioFrame();
443
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000444 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000445}
446
447void ApmTest::TearDown() {
448 if (frame_) {
449 delete frame_;
450 }
451 frame_ = NULL;
452
453 if (revframe_) {
454 delete revframe_;
455 }
456 revframe_ = NULL;
457
458 if (far_file_) {
459 ASSERT_EQ(0, fclose(far_file_));
460 }
461 far_file_ = NULL;
462
463 if (near_file_) {
464 ASSERT_EQ(0, fclose(near_file_));
465 }
466 near_file_ = NULL;
467
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000468 if (out_file_) {
469 ASSERT_EQ(0, fclose(out_file_));
470 }
471 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000472}
473
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000474void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000475 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700476 ap->Initialize(
477 {{{frame_->sample_rate_hz_, frame_->num_channels_},
478 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700479 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700480 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000481}
482
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000483void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000484 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000485 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800486 size_t num_input_channels,
487 size_t num_output_channels,
488 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000489 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000490 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000491 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000492 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000493
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000494 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
495 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000496 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000497
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000498 if (far_file_) {
499 ASSERT_EQ(0, fclose(far_file_));
500 }
501 std::string filename = ResourceFilePath("far", sample_rate_hz);
502 far_file_ = fopen(filename.c_str(), "rb");
503 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
504 filename << "\n";
505
506 if (near_file_) {
507 ASSERT_EQ(0, fclose(near_file_));
508 }
509 filename = ResourceFilePath("near", sample_rate_hz);
510 near_file_ = fopen(filename.c_str(), "rb");
511 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
512 filename << "\n";
513
514 if (open_output_file) {
515 if (out_file_) {
516 ASSERT_EQ(0, fclose(out_file_));
517 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700518 filename = OutputFilePath(
519 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
520 reverse_sample_rate_hz, num_input_channels, num_output_channels,
521 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000522 out_file_ = fopen(filename.c_str(), "wb");
523 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
524 filename << "\n";
525 }
526}
527
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000528void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000529 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000530}
531
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000532bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
533 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000534 // The files always contain stereo audio.
535 size_t frame_size = frame->samples_per_channel_ * 2;
536 size_t read_count = fread(frame->data_,
537 sizeof(int16_t),
538 frame_size,
539 file);
540 if (read_count != frame_size) {
541 // Check that the file really ended.
542 EXPECT_NE(0, feof(file));
543 return false; // This is expected.
544 }
545
546 if (frame->num_channels_ == 1) {
547 MixStereoToMono(frame->data_, frame->data_,
548 frame->samples_per_channel_);
549 }
550
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000551 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000552 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000553 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000554 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000555}
556
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
558 return ReadFrame(file, frame, NULL);
559}
560
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000561// If the end of the file has been reached, rewind it and attempt to read the
562// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000563void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
564 ChannelBuffer<float>* cb) {
565 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000566 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000567 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000568 }
569}
570
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
572 ReadFrameWithRewind(file, frame, NULL);
573}
574
andrew@webrtc.org81865342012-10-27 00:28:27 +0000575void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
576 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000577 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000578 EXPECT_EQ(apm_->kNoError,
579 apm_->gain_control()->set_stream_analog_level(127));
580 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000581}
582
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000583int ApmTest::ProcessStreamChooser(Format format) {
584 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000585 return apm_->ProcessStream(frame_);
586 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587 return apm_->ProcessStream(float_cb_->channels(),
588 frame_->samples_per_channel_,
589 frame_->sample_rate_hz_,
590 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000591 output_sample_rate_hz_,
592 LayoutFromChannels(num_output_channels_),
593 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000594}
595
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000596int ApmTest::AnalyzeReverseStreamChooser(Format format) {
597 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700598 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000599 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000600 return apm_->AnalyzeReverseStream(
601 revfloat_cb_->channels(),
602 revframe_->samples_per_channel_,
603 revframe_->sample_rate_hz_,
604 LayoutFromChannels(revframe_->num_channels_));
605}
606
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000607void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
608 int delay_min, int delay_max) {
609 // The |revframe_| and |frame_| should include the proper frame information,
610 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000611 AudioFrame tmp_frame;
612 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000613 bool causal = true;
614
615 tmp_frame.CopyFrom(*revframe_);
616 SetFrameTo(&tmp_frame, 0);
617
618 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
619 // Initialize the |frame_queue| with empty frames.
620 int frame_delay = delay_ms / 10;
621 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000622 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000623 frame->CopyFrom(tmp_frame);
624 frame_queue.push(frame);
625 frame_delay++;
626 causal = false;
627 }
628 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000629 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000630 frame->CopyFrom(tmp_frame);
631 frame_queue.push(frame);
632 frame_delay--;
633 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000634 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
635 // need enough frames with audio to have reliable estimates, but as few as
636 // possible to keep processing time down. 4.5 seconds seemed to be a good
637 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000638 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000639 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000640 frame->CopyFrom(tmp_frame);
641 // Use the near end recording, since that has more speech in it.
642 ASSERT_TRUE(ReadFrame(near_file_, frame));
643 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000644 AudioFrame* reverse_frame = frame;
645 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000646 if (!causal) {
647 reverse_frame = frame_queue.front();
648 // When we call ProcessStream() the frame is modified, so we can't use the
649 // pointer directly when things are non-causal. Use an intermediate frame
650 // and copy the data.
651 process_frame = &tmp_frame;
652 process_frame->CopyFrom(*frame);
653 }
aluebsb0319552016-03-17 20:39:53 -0700654 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000655 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
656 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
657 frame = frame_queue.front();
658 frame_queue.pop();
659 delete frame;
660
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000661 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 int median;
663 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000664 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000665 // Discard the first delay metrics to avoid convergence effects.
666 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000667 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
668 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000669 }
670 }
671
672 rewind(near_file_);
673 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000674 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000675 frame_queue.pop();
676 delete frame;
677 }
678 // Calculate expected delay estimate and acceptable regions. Further,
679 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700680 const size_t samples_per_ms =
681 std::min(static_cast<size_t>(16), frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000682 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
683 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700684 int expected_median_high = std::min(
685 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
686 delay_min),
687 delay_max);
688 int expected_median_low = std::min(
689 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
690 delay_min),
691 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000692 // Verify delay metrics.
693 int median;
694 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000695 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000696 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000697 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
698 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000699 EXPECT_GE(expected_median_high, median);
700 EXPECT_LE(expected_median_low, median);
701}
702
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000703void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000704 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000705 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000706
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000707 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000708 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000709 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000710 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 EXPECT_EQ(apm_->kNoError,
714 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000715 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000716 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000717 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000718
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719 // Other stream parameters set correctly.
720 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000721 EXPECT_EQ(apm_->kNoError,
722 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000723 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000724 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000726 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
728 EXPECT_EQ(apm_->kNoError,
729 apm_->echo_cancellation()->enable_drift_compensation(false));
730
731 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000732 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000733 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000734 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000736
737 // Resets after successful ProcessStream().
738 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000740 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000741 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000742
743 // Other stream parameters set correctly.
744 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
745 EXPECT_EQ(apm_->kNoError,
746 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000747 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000748 EXPECT_EQ(apm_->kNoError,
749 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000750 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000751 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000752 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
753
754 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000755 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000756 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000757
758 // Resets after successful ProcessStream().
759 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000760 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000761 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000762 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000763 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000764
765 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
767 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
768 EXPECT_EQ(apm_->kNoError,
769 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000770 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000771 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000772
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000773 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000775 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000777 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000778
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000779 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000781 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000782 EXPECT_EQ(apm_->kNoError,
783 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000784 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000785}
786
787TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000788 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000789}
790
791TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000792 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000793}
794
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000795TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
796 EXPECT_EQ(0, apm_->delay_offset_ms());
797 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
798 EXPECT_EQ(50, apm_->stream_delay_ms());
799}
800
801TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
802 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000803 apm_->set_delay_offset_ms(100);
804 EXPECT_EQ(100, apm_->delay_offset_ms());
805 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000806 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000807 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
808 EXPECT_EQ(200, apm_->stream_delay_ms());
809
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000810 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000811 apm_->set_delay_offset_ms(-50);
812 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000813 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
814 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000815 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
816 EXPECT_EQ(50, apm_->stream_delay_ms());
817}
818
Michael Graczyk86c6d332015-07-23 11:41:39 -0700819void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800820 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700821 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000822 frame_->num_channels_ = num_channels;
823 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700824 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000825}
826
Michael Graczyk86c6d332015-07-23 11:41:39 -0700827void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800828 size_t num_in_channels,
829 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700830 AudioProcessing::Error expected_return) {
831 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
832 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
833
834 EXPECT_EQ(expected_return,
835 apm_->ProcessStream(float_cb_->channels(), input_stream,
836 output_stream, float_cb_->channels()));
837}
838
839void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800840 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700841 AudioProcessing::Error expected_return) {
842 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700843 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
844 {output_sample_rate_hz_, apm_->num_output_channels()},
845 {frame_->sample_rate_hz_, num_rev_channels},
846 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700847
ekmeyerson60d9b332015-08-14 10:35:55 -0700848 EXPECT_EQ(
849 expected_return,
850 apm_->ProcessReverseStream(
851 float_cb_->channels(), processing_config.reverse_input_stream(),
852 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700853}
854
855TEST_F(ApmTest, ChannelsInt16Interface) {
856 // Testing number of invalid and valid channels.
857 Init(16000, 16000, 16000, 4, 4, 4, false);
858
859 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
860
Peter Kasting69558702016-01-12 16:26:35 -0800861 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700862 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000863 EXPECT_EQ(i, apm_->num_input_channels());
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000864 // We always force the number of reverse channels used for processing to 1.
Peter Kasting69558702016-01-12 16:26:35 -0800865 EXPECT_EQ(1u, apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000866 }
867}
868
Michael Graczyk86c6d332015-07-23 11:41:39 -0700869TEST_F(ApmTest, Channels) {
870 // Testing number of invalid and valid channels.
871 Init(16000, 16000, 16000, 4, 4, 4, false);
872
873 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
874 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
875
Peter Kasting69558702016-01-12 16:26:35 -0800876 for (size_t i = 1; i < 4; ++i) {
877 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700878 // Output channels much be one or match input channels.
879 if (j == 1 || i == j) {
880 TestChangingForwardChannels(i, j, kNoErr);
881 TestChangingReverseChannels(i, kNoErr);
882
883 EXPECT_EQ(i, apm_->num_input_channels());
884 EXPECT_EQ(j, apm_->num_output_channels());
885 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800886 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700887 } else {
888 TestChangingForwardChannels(i, j,
889 AudioProcessing::kBadNumberChannelsError);
890 }
891 }
892 }
893}
894
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000895TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000896 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000897 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000898 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000899 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700900 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800901 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000902 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000903 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000904 }
905}
906
niklase@google.com470e71d2011-07-07 08:21:25 +0000907TEST_F(ApmTest, EchoCancellation) {
908 EXPECT_EQ(apm_->kNoError,
909 apm_->echo_cancellation()->enable_drift_compensation(true));
910 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
911 EXPECT_EQ(apm_->kNoError,
912 apm_->echo_cancellation()->enable_drift_compensation(false));
913 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
914
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 EchoCancellation::SuppressionLevel level[] = {
916 EchoCancellation::kLowSuppression,
917 EchoCancellation::kModerateSuppression,
918 EchoCancellation::kHighSuppression,
919 };
pkasting25702cb2016-01-08 13:50:27 -0800920 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000921 EXPECT_EQ(apm_->kNoError,
922 apm_->echo_cancellation()->set_suppression_level(level[i]));
923 EXPECT_EQ(level[i],
924 apm_->echo_cancellation()->suppression_level());
925 }
926
927 EchoCancellation::Metrics metrics;
928 EXPECT_EQ(apm_->kNotEnabledError,
929 apm_->echo_cancellation()->GetMetrics(&metrics));
930
ivoc3e9a5372016-10-28 07:55:33 -0700931 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
932 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
933
niklase@google.com470e71d2011-07-07 08:21:25 +0000934 EXPECT_EQ(apm_->kNoError,
935 apm_->echo_cancellation()->enable_metrics(true));
936 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
937 EXPECT_EQ(apm_->kNoError,
938 apm_->echo_cancellation()->enable_metrics(false));
939 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
940
ivoc48dfab52016-10-28 03:29:31 -0700941 EXPECT_EQ(apm_->kNoError,
942 apm_->echo_cancellation()->enable_delay_logging(true));
943 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
944 EXPECT_EQ(apm_->kNoError,
945 apm_->echo_cancellation()->enable_delay_logging(false));
946 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
947
ivoc3e9a5372016-10-28 07:55:33 -0700948 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
949 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
950
951 int median = 0;
952 int std = 0;
953 float poor_fraction = 0;
954 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
955 &median, &std, &poor_fraction));
956
niklase@google.com470e71d2011-07-07 08:21:25 +0000957 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
958 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
959 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
960 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000961
962 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
963 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
964 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
965 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
966 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
967 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000968}
969
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000970TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000971 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000972 // Enable AEC only.
973 EXPECT_EQ(apm_->kNoError,
974 apm_->echo_cancellation()->enable_drift_compensation(false));
975 EXPECT_EQ(apm_->kNoError,
976 apm_->echo_cancellation()->enable_metrics(false));
977 EXPECT_EQ(apm_->kNoError,
978 apm_->echo_cancellation()->enable_delay_logging(true));
979 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000980 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700981 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000982 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000983
984 // Internally in the AEC the amount of lookahead the delay estimation can
985 // handle is 15 blocks and the maximum delay is set to 60 blocks.
986 const int kLookaheadBlocks = 15;
987 const int kMaxDelayBlocks = 60;
988 // The AEC has a startup time before it actually starts to process. This
989 // procedure can flush the internal far-end buffer, which of course affects
990 // the delay estimation. Therefore, we set a system_delay high enough to
991 // avoid that. The smallest system_delay you can report without flushing the
992 // buffer is 66 ms in 8 kHz.
993 //
994 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
995 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
996 // delay estimation. This should be noted though. In case of test failure,
997 // this could be the cause.
998 const int kSystemDelayMs = 66;
999 // Test a couple of corner cases and verify that the estimated delay is
1000 // within a valid region (set to +-1.5 blocks). Note that these cases are
1001 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001002 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001003 Init(kProcessSampleRates[i],
1004 kProcessSampleRates[i],
1005 kProcessSampleRates[i],
1006 2,
1007 2,
1008 2,
1009 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001010 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001011 const int num_ms_per_block =
1012 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001013 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1014 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1015
1016 // 1) Verify correct delay estimate at lookahead boundary.
1017 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1018 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1019 delay_max_ms);
1020 // 2) A delay less than maximum lookahead should give an delay estimate at
1021 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1022 delay_ms -= 20;
1023 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1024 delay_max_ms);
1025 // 3) Three values around zero delay. Note that we need to compensate for
1026 // the fake system_delay.
1027 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1028 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1029 delay_max_ms);
1030 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1031 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1032 delay_max_ms);
1033 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1034 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1035 delay_max_ms);
1036 // 4) Verify correct delay estimate at maximum delay boundary.
1037 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1038 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1039 delay_max_ms);
1040 // 5) A delay above the maximum delay should give an estimate at the
1041 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1042 delay_ms += 20;
1043 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1044 delay_max_ms);
1045 }
1046}
1047
niklase@google.com470e71d2011-07-07 08:21:25 +00001048TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001049 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001050 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1052 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1053
niklase@google.com470e71d2011-07-07 08:21:25 +00001054 // Toggle routing modes
1055 EchoControlMobile::RoutingMode mode[] = {
1056 EchoControlMobile::kQuietEarpieceOrHeadset,
1057 EchoControlMobile::kEarpiece,
1058 EchoControlMobile::kLoudEarpiece,
1059 EchoControlMobile::kSpeakerphone,
1060 EchoControlMobile::kLoudSpeakerphone,
1061 };
pkasting25702cb2016-01-08 13:50:27 -08001062 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001063 EXPECT_EQ(apm_->kNoError,
1064 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1065 EXPECT_EQ(mode[i],
1066 apm_->echo_control_mobile()->routing_mode());
1067 }
1068 // Turn comfort noise off/on
1069 EXPECT_EQ(apm_->kNoError,
1070 apm_->echo_control_mobile()->enable_comfort_noise(false));
1071 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1072 EXPECT_EQ(apm_->kNoError,
1073 apm_->echo_control_mobile()->enable_comfort_noise(true));
1074 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001075 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001076 const size_t echo_path_size =
1077 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001078 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1079 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001080 EXPECT_EQ(apm_->kNullPointerError,
1081 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1082 EXPECT_EQ(apm_->kNullPointerError,
1083 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1084 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001085 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001086 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001087 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001088 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001089 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001090 echo_path_in[i] = echo_path_out[i] + 1;
1091 }
1092 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001093 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001094 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001095 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1096 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001097 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001098 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1099 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001100 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001101 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1102 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001103
1104 // Process a few frames with NS in the default disabled state. This exercises
1105 // a different codepath than with it enabled.
1106 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1107 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1108 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1109 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1110
niklase@google.com470e71d2011-07-07 08:21:25 +00001111 // Turn AECM off
1112 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1113 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1114}
1115
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001116TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001117 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001118 EXPECT_EQ(apm_->kNoError,
1119 apm_->gain_control()->set_mode(
1120 apm_->gain_control()->mode()));
1121
1122 GainControl::Mode mode[] = {
1123 GainControl::kAdaptiveAnalog,
1124 GainControl::kAdaptiveDigital,
1125 GainControl::kFixedDigital
1126 };
pkasting25702cb2016-01-08 13:50:27 -08001127 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001128 EXPECT_EQ(apm_->kNoError,
1129 apm_->gain_control()->set_mode(mode[i]));
1130 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1131 }
1132 // Testing invalid target levels
1133 EXPECT_EQ(apm_->kBadParameterError,
1134 apm_->gain_control()->set_target_level_dbfs(-3));
1135 EXPECT_EQ(apm_->kBadParameterError,
1136 apm_->gain_control()->set_target_level_dbfs(-40));
1137 // Testing valid target levels
1138 EXPECT_EQ(apm_->kNoError,
1139 apm_->gain_control()->set_target_level_dbfs(
1140 apm_->gain_control()->target_level_dbfs()));
1141
1142 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001143 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001144 EXPECT_EQ(apm_->kNoError,
1145 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1146 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1147 }
1148
1149 // Testing invalid compression gains
1150 EXPECT_EQ(apm_->kBadParameterError,
1151 apm_->gain_control()->set_compression_gain_db(-1));
1152 EXPECT_EQ(apm_->kBadParameterError,
1153 apm_->gain_control()->set_compression_gain_db(100));
1154
1155 // Testing valid compression gains
1156 EXPECT_EQ(apm_->kNoError,
1157 apm_->gain_control()->set_compression_gain_db(
1158 apm_->gain_control()->compression_gain_db()));
1159
1160 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001161 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001162 EXPECT_EQ(apm_->kNoError,
1163 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1164 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1165 }
1166
1167 // Testing limiter off/on
1168 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1169 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1170 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1171 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1172
1173 // Testing invalid level limits
1174 EXPECT_EQ(apm_->kBadParameterError,
1175 apm_->gain_control()->set_analog_level_limits(-1, 512));
1176 EXPECT_EQ(apm_->kBadParameterError,
1177 apm_->gain_control()->set_analog_level_limits(100000, 512));
1178 EXPECT_EQ(apm_->kBadParameterError,
1179 apm_->gain_control()->set_analog_level_limits(512, -1));
1180 EXPECT_EQ(apm_->kBadParameterError,
1181 apm_->gain_control()->set_analog_level_limits(512, 100000));
1182 EXPECT_EQ(apm_->kBadParameterError,
1183 apm_->gain_control()->set_analog_level_limits(512, 255));
1184
1185 // Testing valid level limits
1186 EXPECT_EQ(apm_->kNoError,
1187 apm_->gain_control()->set_analog_level_limits(
1188 apm_->gain_control()->analog_level_minimum(),
1189 apm_->gain_control()->analog_level_maximum()));
1190
1191 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001192 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001193 EXPECT_EQ(apm_->kNoError,
1194 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1195 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1196 }
1197
1198 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001199 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001200 EXPECT_EQ(apm_->kNoError,
1201 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1202 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1203 }
1204
1205 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1206
1207 // Turn AGC off
1208 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1209 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1210}
1211
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001212void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001213 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001214 EXPECT_EQ(apm_->kNoError,
1215 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1216 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1217
1218 int out_analog_level = 0;
1219 for (int i = 0; i < 2000; ++i) {
1220 ReadFrameWithRewind(near_file_, frame_);
1221 // Ensure the audio is at a low level, so the AGC will try to increase it.
1222 ScaleFrame(frame_, 0.25);
1223
1224 // Always pass in the same volume.
1225 EXPECT_EQ(apm_->kNoError,
1226 apm_->gain_control()->set_stream_analog_level(100));
1227 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1228 out_analog_level = apm_->gain_control()->stream_analog_level();
1229 }
1230
1231 // Ensure the AGC is still able to reach the maximum.
1232 EXPECT_EQ(255, out_analog_level);
1233}
1234
1235// Verifies that despite volume slider quantization, the AGC can continue to
1236// increase its volume.
1237TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001238 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001239 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1240 }
1241}
1242
1243void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001244 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001245 EXPECT_EQ(apm_->kNoError,
1246 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1247 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1248
1249 int out_analog_level = 100;
1250 for (int i = 0; i < 1000; ++i) {
1251 ReadFrameWithRewind(near_file_, frame_);
1252 // Ensure the audio is at a low level, so the AGC will try to increase it.
1253 ScaleFrame(frame_, 0.25);
1254
1255 EXPECT_EQ(apm_->kNoError,
1256 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1257 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1258 out_analog_level = apm_->gain_control()->stream_analog_level();
1259 }
1260
1261 // Ensure the volume was raised.
1262 EXPECT_GT(out_analog_level, 100);
1263 int highest_level_reached = out_analog_level;
1264 // Simulate a user manual volume change.
1265 out_analog_level = 100;
1266
1267 for (int i = 0; i < 300; ++i) {
1268 ReadFrameWithRewind(near_file_, frame_);
1269 ScaleFrame(frame_, 0.25);
1270
1271 EXPECT_EQ(apm_->kNoError,
1272 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1273 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1274 out_analog_level = apm_->gain_control()->stream_analog_level();
1275 // Check that AGC respected the manually adjusted volume.
1276 EXPECT_LT(out_analog_level, highest_level_reached);
1277 }
1278 // Check that the volume was still raised.
1279 EXPECT_GT(out_analog_level, 100);
1280}
1281
1282TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001283 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001284 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1285 }
1286}
1287
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001288#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1289TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1290 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001291 const size_t kSamplesPerChannel =
1292 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001293 const size_t kNumInputChannels = 2;
1294 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001295 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001296 const float kScaleFactor = 0.25f;
1297 Config config;
1298 std::vector<webrtc::Point> geometry;
1299 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1300 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1301 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001302 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001303 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001304 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001305 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001306 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1307 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1308 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001309 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1310 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001311 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1312 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001313 std::string filename = ResourceFilePath("far", kSampleRateHz);
1314 FILE* far_file = fopen(filename.c_str(), "rb");
1315 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1316 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1317 const int kDefaultCompressionGain =
1318 apm->gain_control()->compression_gain_db();
1319 bool is_target = false;
1320 EXPECT_CALL(*beamformer, is_target_present())
1321 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001322 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001323 ASSERT_TRUE(ReadChunk(far_file,
1324 int_data.get(),
1325 float_data.get(),
1326 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001327 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001328 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001329 src_buf.channels()[j][k] *= kScaleFactor;
1330 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001331 }
1332 EXPECT_EQ(kNoErr,
1333 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001334 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001335 kSampleRateHz,
1336 LayoutFromChannels(src_buf.num_channels()),
1337 kSampleRateHz,
1338 LayoutFromChannels(dest_buf.num_channels()),
1339 dest_buf.channels()));
1340 }
1341 EXPECT_EQ(kDefaultVolume,
1342 apm->gain_control()->stream_analog_level());
1343 EXPECT_EQ(kDefaultCompressionGain,
1344 apm->gain_control()->compression_gain_db());
1345 rewind(far_file);
1346 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001347 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001348 ASSERT_TRUE(ReadChunk(far_file,
1349 int_data.get(),
1350 float_data.get(),
1351 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001352 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001353 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001354 src_buf.channels()[j][k] *= kScaleFactor;
1355 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001356 }
1357 EXPECT_EQ(kNoErr,
1358 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001359 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001360 kSampleRateHz,
1361 LayoutFromChannels(src_buf.num_channels()),
1362 kSampleRateHz,
1363 LayoutFromChannels(dest_buf.num_channels()),
1364 dest_buf.channels()));
1365 }
1366 EXPECT_LT(kDefaultVolume,
1367 apm->gain_control()->stream_analog_level());
1368 EXPECT_LT(kDefaultCompressionGain,
1369 apm->gain_control()->compression_gain_db());
1370 ASSERT_EQ(0, fclose(far_file));
1371}
1372#endif
1373
niklase@google.com470e71d2011-07-07 08:21:25 +00001374TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001375 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001376 NoiseSuppression::Level level[] = {
1377 NoiseSuppression::kLow,
1378 NoiseSuppression::kModerate,
1379 NoiseSuppression::kHigh,
1380 NoiseSuppression::kVeryHigh
1381 };
pkasting25702cb2016-01-08 13:50:27 -08001382 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001383 EXPECT_EQ(apm_->kNoError,
1384 apm_->noise_suppression()->set_level(level[i]));
1385 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1386 }
1387
andrew@webrtc.org648af742012-02-08 01:57:29 +00001388 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001389 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1390 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1391 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1392 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1393}
1394
1395TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001396 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001397 AudioProcessing::Config apm_config;
1398 apm_config.high_pass_filter.enabled = true;
1399 apm_->ApplyConfig(apm_config);
1400 apm_config.high_pass_filter.enabled = false;
1401 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001402}
1403
1404TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001405 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001406 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001407 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001408
1409 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1410
1411 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1412 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1413
1414 // Run this test in wideband; in super-wb, the splitting filter distorts the
1415 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001416 frame_->samples_per_channel_ = 160;
1417 frame_->num_channels_ = 2;
1418 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001419
1420 // Min value if no frames have been processed.
1421 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1422
1423 // Min value on zero frames.
1424 SetFrameTo(frame_, 0);
1425 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1426 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1427 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1428
1429 // Try a few RMS values.
1430 // (These also test that the value resets after retrieving it.)
1431 SetFrameTo(frame_, 32767);
1432 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1433 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1434 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1435
1436 SetFrameTo(frame_, 30000);
1437 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1438 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1439 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1440
1441 SetFrameTo(frame_, 10000);
1442 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1443 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1444 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1445
1446 SetFrameTo(frame_, 10);
1447 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1448 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1449 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1450
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001451 // Verify reset after enable/disable.
1452 SetFrameTo(frame_, 32767);
1453 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1454 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1455 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1456 SetFrameTo(frame_, 1);
1457 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1458 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1459
1460 // Verify reset after initialize.
1461 SetFrameTo(frame_, 32767);
1462 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1463 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1464 SetFrameTo(frame_, 1);
1465 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1466 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001467}
1468
1469TEST_F(ApmTest, VoiceDetection) {
1470 // Test external VAD
1471 EXPECT_EQ(apm_->kNoError,
1472 apm_->voice_detection()->set_stream_has_voice(true));
1473 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1474 EXPECT_EQ(apm_->kNoError,
1475 apm_->voice_detection()->set_stream_has_voice(false));
1476 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1477
andrew@webrtc.org648af742012-02-08 01:57:29 +00001478 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001479 VoiceDetection::Likelihood likelihood[] = {
1480 VoiceDetection::kVeryLowLikelihood,
1481 VoiceDetection::kLowLikelihood,
1482 VoiceDetection::kModerateLikelihood,
1483 VoiceDetection::kHighLikelihood
1484 };
pkasting25702cb2016-01-08 13:50:27 -08001485 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001486 EXPECT_EQ(apm_->kNoError,
1487 apm_->voice_detection()->set_likelihood(likelihood[i]));
1488 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1489 }
1490
1491 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001492 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001493 EXPECT_EQ(apm_->kBadParameterError,
1494 apm_->voice_detection()->set_frame_size_ms(12));
1495
andrew@webrtc.org648af742012-02-08 01:57:29 +00001496 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001497 for (int i = 10; i <= 30; i += 10) {
1498 EXPECT_EQ(apm_->kNoError,
1499 apm_->voice_detection()->set_frame_size_ms(i));
1500 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1501 }
1502 */
1503
andrew@webrtc.org648af742012-02-08 01:57:29 +00001504 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001505 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1506 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1507 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1508 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1509
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001510 // Test that AudioFrame activity is maintained when VAD is disabled.
1511 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1512 AudioFrame::VADActivity activity[] = {
1513 AudioFrame::kVadActive,
1514 AudioFrame::kVadPassive,
1515 AudioFrame::kVadUnknown
1516 };
pkasting25702cb2016-01-08 13:50:27 -08001517 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001518 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001519 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001520 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001521 }
1522
1523 // Test that AudioFrame activity is set when VAD is enabled.
1524 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001525 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001526 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001527 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001528
niklase@google.com470e71d2011-07-07 08:21:25 +00001529 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1530}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001531
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001532TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1533 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1534 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1535 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1536 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1537 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1538 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1539 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1540}
1541
1542TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001543 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001544 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001545 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001546 AudioFrame frame_copy;
1547 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001548 for (int j = 0; j < 1000; j++) {
1549 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1550 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001551 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1552 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001553 }
1554 }
1555}
1556
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001557TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1558 // Test that ProcessStream copies input to output even with no processing.
1559 const size_t kSamples = 80;
1560 const int sample_rate = 8000;
1561 const float src[kSamples] = {
1562 -1.0f, 0.0f, 1.0f
1563 };
1564 float dest[kSamples] = {};
1565
1566 auto src_channels = &src[0];
1567 auto dest_channels = &dest[0];
1568
1569 apm_.reset(AudioProcessing::Create());
1570 EXPECT_NOERR(apm_->ProcessStream(
1571 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1572 sample_rate, LayoutFromChannels(1), &dest_channels));
1573
1574 for (size_t i = 0; i < kSamples; ++i) {
1575 EXPECT_EQ(src[i], dest[i]);
1576 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001577
1578 // Same for ProcessReverseStream.
1579 float rev_dest[kSamples] = {};
1580 auto rev_dest_channels = &rev_dest[0];
1581
1582 StreamConfig input_stream = {sample_rate, 1};
1583 StreamConfig output_stream = {sample_rate, 1};
1584 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1585 output_stream, &rev_dest_channels));
1586
1587 for (size_t i = 0; i < kSamples; ++i) {
1588 EXPECT_EQ(src[i], rev_dest[i]);
1589 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001590}
1591
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001592TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1593 EnableAllComponents();
1594
pkasting25702cb2016-01-08 13:50:27 -08001595 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001596 Init(kProcessSampleRates[i],
1597 kProcessSampleRates[i],
1598 kProcessSampleRates[i],
1599 2,
1600 2,
1601 2,
1602 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001603 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001604 ASSERT_EQ(0, feof(far_file_));
1605 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001606 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001607 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1608
aluebsb0319552016-03-17 20:39:53 -07001609 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001610
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001611 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1612 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1613
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001614 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001615 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001616 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001617 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001618 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001619 analog_level = apm_->gain_control()->stream_analog_level();
1620
1621 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1622 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001623 rewind(far_file_);
1624 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001625 }
1626}
1627
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001628TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001629 // Verify the filter is not active through undistorted audio when:
1630 // 1. No components are enabled...
1631 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001632 AudioFrame frame_copy;
1633 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001634 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1635 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1636 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1637
1638 // 2. Only the level estimator is enabled...
1639 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001640 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001641 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1642 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1643 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1644 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1645 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1646
1647 // 3. Only VAD is enabled...
1648 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001649 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001650 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1651 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1652 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1653 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1654 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1655
1656 // 4. Both VAD and the level estimator are enabled...
1657 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001658 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001659 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1660 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1661 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1662 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1663 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1664 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1665 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1666
1667 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001668 frame_->samples_per_channel_ = 160;
1669 frame_->num_channels_ = 2;
1670 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001671 // Enable AEC, which would require the filter in super-wb. We rely on the
1672 // first few frames of data being unaffected by the AEC.
1673 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1674 // behavior of the AEC. Think of something more robust.
1675 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001676 // Make sure we have extended filter enabled. This makes sure nothing is
1677 // touched until we have a farend frame.
1678 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001679 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001680 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001681 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001682 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001683 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001684 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001685 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1686 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001687 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001688 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1689 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1690
1691 // Check the test is valid. We should have distortion from the filter
1692 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001693 frame_->samples_per_channel_ = 320;
1694 frame_->num_channels_ = 2;
1695 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001696 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001697 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001698 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001699 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001700 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1701 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1702}
1703
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001704#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1705void ApmTest::ProcessDebugDump(const std::string& in_filename,
1706 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001707 Format format,
1708 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001709 FILE* in_file = fopen(in_filename.c_str(), "rb");
1710 ASSERT_TRUE(in_file != NULL);
1711 audioproc::Event event_msg;
1712 bool first_init = true;
1713
1714 while (ReadMessageFromFile(in_file, &event_msg)) {
1715 if (event_msg.type() == audioproc::Event::INIT) {
1716 const audioproc::Init msg = event_msg.init();
1717 int reverse_sample_rate = msg.sample_rate();
1718 if (msg.has_reverse_sample_rate()) {
1719 reverse_sample_rate = msg.reverse_sample_rate();
1720 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001721 int output_sample_rate = msg.sample_rate();
1722 if (msg.has_output_sample_rate()) {
1723 output_sample_rate = msg.output_sample_rate();
1724 }
1725
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001726 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001727 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001728 reverse_sample_rate,
1729 msg.num_input_channels(),
1730 msg.num_output_channels(),
1731 msg.num_reverse_channels(),
1732 false);
1733 if (first_init) {
1734 // StartDebugRecording() writes an additional init message. Don't start
1735 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001736 EXPECT_NOERR(
1737 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001738 first_init = false;
1739 }
1740
1741 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1742 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1743
1744 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001745 ASSERT_EQ(revframe_->num_channels_,
1746 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001747 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001748 memcpy(revfloat_cb_->channels()[i],
1749 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001750 msg.channel(i).size());
1751 }
1752 } else {
1753 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1754 if (format == kFloatFormat) {
1755 // We're using an int16 input file; convert to float.
1756 ConvertToFloat(*revframe_, revfloat_cb_.get());
1757 }
1758 }
1759 AnalyzeReverseStreamChooser(format);
1760
1761 } else if (event_msg.type() == audioproc::Event::STREAM) {
1762 const audioproc::Stream msg = event_msg.stream();
1763 // ProcessStream could have changed this for the output frame.
1764 frame_->num_channels_ = apm_->num_input_channels();
1765
1766 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1767 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1768 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1769 if (msg.has_keypress()) {
1770 apm_->set_stream_key_pressed(msg.keypress());
1771 } else {
1772 apm_->set_stream_key_pressed(true);
1773 }
1774
1775 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001776 ASSERT_EQ(frame_->num_channels_,
1777 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001778 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001779 memcpy(float_cb_->channels()[i],
1780 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001781 msg.input_channel(i).size());
1782 }
1783 } else {
1784 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1785 if (format == kFloatFormat) {
1786 // We're using an int16 input file; convert to float.
1787 ConvertToFloat(*frame_, float_cb_.get());
1788 }
1789 }
1790 ProcessStreamChooser(format);
1791 }
1792 }
1793 EXPECT_NOERR(apm_->StopDebugRecording());
1794 fclose(in_file);
1795}
1796
1797void ApmTest::VerifyDebugDumpTest(Format format) {
1798 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001799 std::string format_string;
1800 switch (format) {
1801 case kIntFormat:
1802 format_string = "_int";
1803 break;
1804 case kFloatFormat:
1805 format_string = "_float";
1806 break;
1807 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001808 const std::string ref_filename = test::TempFilename(
1809 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1810 const std::string out_filename = test::TempFilename(
1811 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001812 const std::string limited_filename = test::TempFilename(
1813 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1814 const size_t logging_limit_bytes = 100000;
1815 // We expect at least this many bytes in the created logfile.
1816 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001817 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001818 ProcessDebugDump(in_filename, ref_filename, format, -1);
1819 ProcessDebugDump(ref_filename, out_filename, format, -1);
1820 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001821
1822 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1823 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001824 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001825 ASSERT_TRUE(ref_file != NULL);
1826 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001827 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001828 std::unique_ptr<uint8_t[]> ref_bytes;
1829 std::unique_ptr<uint8_t[]> out_bytes;
1830 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001831
1832 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1833 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001834 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001835 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001836 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001837 while (ref_size > 0 && out_size > 0) {
1838 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001839 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001840 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001841 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001842 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001843 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001844 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1845 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001846 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001847 }
1848 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001849 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1850 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001851 EXPECT_NE(0, feof(ref_file));
1852 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001853 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001854 ASSERT_EQ(0, fclose(ref_file));
1855 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001856 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001857 remove(ref_filename.c_str());
1858 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001859 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001860}
1861
pbosc7a65692016-05-06 12:50:04 -07001862TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001863 VerifyDebugDumpTest(kIntFormat);
1864}
1865
pbosc7a65692016-05-06 12:50:04 -07001866TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001867 VerifyDebugDumpTest(kFloatFormat);
1868}
1869#endif
1870
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001871// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001872TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001873 const std::string filename =
1874 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001875 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001876 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001877
1878#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1879 // Stopping without having started should be OK.
1880 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1881
ivocd66b44d2016-01-15 03:06:36 -08001882 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001883 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001884 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001885 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1886
1887 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001888 FILE* fid = fopen(filename.c_str(), "r");
1889 ASSERT_TRUE(fid != NULL);
1890
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001891 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001892 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001893 ASSERT_EQ(0, remove(filename.c_str()));
1894#else
1895 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001896 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001897 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1898
1899 // Verify the file has NOT been written.
1900 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1901#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1902}
1903
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001904// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001905TEST_F(ApmTest, DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001906 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001907 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001908 const std::string filename =
1909 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001910 fid = fopen(filename.c_str(), "w");
1911 ASSERT_TRUE(fid);
1912
1913#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1914 // Stopping without having started should be OK.
1915 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1916
ivocd66b44d2016-01-15 03:06:36 -08001917 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001918 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001919 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1920 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1921
1922 // Verify the file has been written.
1923 fid = fopen(filename.c_str(), "r");
1924 ASSERT_TRUE(fid != NULL);
1925
1926 // Clean it up.
1927 ASSERT_EQ(0, fclose(fid));
1928 ASSERT_EQ(0, remove(filename.c_str()));
1929#else
1930 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001931 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001932 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1933
1934 ASSERT_EQ(0, fclose(fid));
1935#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1936}
1937
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001938TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001939 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001940 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001941
1942 Config config;
1943 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001944 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001945 EnableAllComponents();
1946 EnableAllAPComponents(fapm.get());
1947 for (int i = 0; i < ref_data.test_size(); i++) {
1948 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1949
1950 audioproc::Test* test = ref_data.mutable_test(i);
1951 // TODO(ajm): Restore downmixing test cases.
1952 if (test->num_input_channels() != test->num_output_channels())
1953 continue;
1954
Peter Kasting69558702016-01-12 16:26:35 -08001955 const size_t num_render_channels =
1956 static_cast<size_t>(test->num_reverse_channels());
1957 const size_t num_input_channels =
1958 static_cast<size_t>(test->num_input_channels());
1959 const size_t num_output_channels =
1960 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001961 const size_t samples_per_channel = static_cast<size_t>(
1962 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001963
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001964 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1965 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001966 Init(fapm.get());
1967
1968 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001969 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1970 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001971
1972 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001973 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001974 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1975 ReadFrame(near_file_, frame_, float_cb_.get())) {
1976 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1977
aluebsb0319552016-03-17 20:39:53 -07001978 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001979 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1980 revfloat_cb_->channels(),
1981 samples_per_channel,
1982 test->sample_rate(),
1983 LayoutFromChannels(num_render_channels)));
1984
1985 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1986 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1987 apm_->echo_cancellation()->set_stream_drift_samples(0);
1988 fapm->echo_cancellation()->set_stream_drift_samples(0);
1989 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1990 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1991
1992 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001993 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
1994 output_int16.channels());
1995
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001996 EXPECT_NOERR(fapm->ProcessStream(
1997 float_cb_->channels(),
1998 samples_per_channel,
1999 test->sample_rate(),
2000 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002001 test->sample_rate(),
2002 LayoutFromChannels(num_output_channels),
2003 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002004 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002005 FloatToS16(float_cb_->channels()[j],
2006 samples_per_channel,
2007 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002008 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002009 float snr = ComputeSNR(output_int16.channels()[j],
2010 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002011 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002012
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002013 const float kVarianceThreshold = 20;
2014 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002015
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002016 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002017 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2018 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002019 }
2020 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002021
2022 analog_level = fapm->gain_control()->stream_analog_level();
2023 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2024 fapm->gain_control()->stream_analog_level());
2025 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2026 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002027 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2028 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002029 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002030
2031 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002032 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002033 }
aluebs776593b2016-03-15 14:04:58 -07002034
2035#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2036 const size_t kMaxNumBadChunks = 0;
2037#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2038 // There are a few chunks in the fixed-point profile that give low SNR.
2039 // Listening confirmed the difference is acceptable.
2040 const size_t kMaxNumBadChunks = 60;
2041#endif
2042 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2043
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002044 rewind(far_file_);
2045 rewind(near_file_);
2046 }
2047}
2048
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002049// TODO(andrew): Add a test to process a few frames with different combinations
2050// of enabled components.
2051
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002052TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002053 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002054 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002055
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002056 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002057 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002058 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002059 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002060 for (size_t i = 0; i < arraysize(kChannels); i++) {
2061 for (size_t j = 0; j < arraysize(kChannels); j++) {
2062 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002063 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002064 test->set_num_reverse_channels(kChannels[i]);
2065 test->set_num_input_channels(kChannels[j]);
2066 test->set_num_output_channels(kChannels[j]);
2067 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002068 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002069 }
2070 }
2071 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002072#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2073 // To test the extended filter mode.
2074 audioproc::Test* test = ref_data.add_test();
2075 test->set_num_reverse_channels(2);
2076 test->set_num_input_channels(2);
2077 test->set_num_output_channels(2);
2078 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2079 test->set_use_aec_extended_filter(true);
2080#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002081 }
2082
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002083 for (int i = 0; i < ref_data.test_size(); i++) {
2084 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002085
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002086 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002087 // TODO(ajm): We no longer allow different input and output channels. Skip
2088 // these tests for now, but they should be removed from the set.
2089 if (test->num_input_channels() != test->num_output_channels())
2090 continue;
2091
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002092 Config config;
2093 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002094 config.Set<ExtendedFilter>(
2095 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002096 apm_.reset(AudioProcessing::Create(config));
2097
2098 EnableAllComponents();
2099
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002100 Init(test->sample_rate(),
2101 test->sample_rate(),
2102 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002103 static_cast<size_t>(test->num_input_channels()),
2104 static_cast<size_t>(test->num_output_channels()),
2105 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002106 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002107
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002108 int frame_count = 0;
2109 int has_echo_count = 0;
2110 int has_voice_count = 0;
2111 int is_saturated_count = 0;
2112 int analog_level = 127;
2113 int analog_level_average = 0;
2114 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002115 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002116#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2117 int stats_index = 0;
2118#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002119
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002120 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002121 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002122
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002123 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2124
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002125 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002126 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002127 EXPECT_EQ(apm_->kNoError,
2128 apm_->gain_control()->set_stream_analog_level(analog_level));
2129
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002130 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002131
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002132 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002133 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2134 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002135
2136 max_output_average += MaxAudioFrame(*frame_);
2137
2138 if (apm_->echo_cancellation()->stream_has_echo()) {
2139 has_echo_count++;
2140 }
2141
2142 analog_level = apm_->gain_control()->stream_analog_level();
2143 analog_level_average += analog_level;
2144 if (apm_->gain_control()->stream_is_saturated()) {
2145 is_saturated_count++;
2146 }
2147 if (apm_->voice_detection()->stream_has_voice()) {
2148 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002149 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002150 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002151 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002152 }
2153
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002154 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2155
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002156 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002157 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002158 sizeof(int16_t),
2159 frame_size,
2160 out_file_);
2161 ASSERT_EQ(frame_size, write_count);
2162
2163 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002164 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002165 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002166
2167#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2168 const int kStatsAggregationFrameNum = 100; // 1 second.
2169 if (frame_count % kStatsAggregationFrameNum == 0) {
2170 // Get echo metrics.
2171 EchoCancellation::Metrics echo_metrics;
2172 EXPECT_EQ(apm_->kNoError,
2173 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2174
2175 // Get delay metrics.
2176 int median = 0;
2177 int std = 0;
2178 float fraction_poor_delays = 0;
2179 EXPECT_EQ(apm_->kNoError,
2180 apm_->echo_cancellation()->GetDelayMetrics(
2181 &median, &std, &fraction_poor_delays));
2182
2183 // Get RMS.
2184 int rms_level = apm_->level_estimator()->RMS();
2185 EXPECT_LE(0, rms_level);
2186 EXPECT_GE(127, rms_level);
2187
2188 if (!write_ref_data) {
2189 const audioproc::Test::EchoMetrics& reference =
2190 test->echo_metrics(stats_index);
2191 TestStats(echo_metrics.residual_echo_return_loss,
2192 reference.residual_echo_return_loss());
2193 TestStats(echo_metrics.echo_return_loss,
2194 reference.echo_return_loss());
2195 TestStats(echo_metrics.echo_return_loss_enhancement,
2196 reference.echo_return_loss_enhancement());
2197 TestStats(echo_metrics.a_nlp,
2198 reference.a_nlp());
2199 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2200 reference.divergent_filter_fraction());
2201
2202 const audioproc::Test::DelayMetrics& reference_delay =
2203 test->delay_metrics(stats_index);
2204 EXPECT_EQ(reference_delay.median(), median);
2205 EXPECT_EQ(reference_delay.std(), std);
2206 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2207 fraction_poor_delays);
2208
2209 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2210
2211 ++stats_index;
2212 } else {
2213 audioproc::Test::EchoMetrics* message =
2214 test->add_echo_metrics();
2215 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2216 message->mutable_residual_echo_return_loss());
2217 WriteStatsMessage(echo_metrics.echo_return_loss,
2218 message->mutable_echo_return_loss());
2219 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2220 message->mutable_echo_return_loss_enhancement());
2221 WriteStatsMessage(echo_metrics.a_nlp,
2222 message->mutable_a_nlp());
2223 message->set_divergent_filter_fraction(
2224 echo_metrics.divergent_filter_fraction);
2225
2226 audioproc::Test::DelayMetrics* message_delay =
2227 test->add_delay_metrics();
2228 message_delay->set_median(median);
2229 message_delay->set_std(std);
2230 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2231
2232 test->add_rms_level(rms_level);
2233 }
2234 }
2235#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002236 }
2237 max_output_average /= frame_count;
2238 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002239 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002240
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002241 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002242 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002243 // When running the test on a N7 we get a {2, 6} difference of
2244 // |has_voice_count| and |max_output_average| is up to 18 higher.
2245 // All numbers being consistently higher on N7 compare to ref_data.
2246 // TODO(bjornv): If we start getting more of these offsets on Android we
2247 // should consider a different approach. Either using one slack for all,
2248 // or generate a separate android reference.
2249#if defined(WEBRTC_ANDROID)
2250 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002251 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002252 const int kMaxOutputAverageOffset = 9;
2253 const int kMaxOutputAverageNear = 9;
2254#else
2255 const int kHasVoiceCountOffset = 0;
2256 const int kHasVoiceCountNear = kIntNear;
2257 const int kMaxOutputAverageOffset = 0;
2258 const int kMaxOutputAverageNear = kIntNear;
2259#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002260 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002261 EXPECT_NEAR(test->has_voice_count(),
2262 has_voice_count - kHasVoiceCountOffset,
2263 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002264 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002265
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002266 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002267 EXPECT_NEAR(test->max_output_average(),
2268 max_output_average - kMaxOutputAverageOffset,
2269 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002270#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002271 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002272 EXPECT_NEAR(test->ns_speech_probability_average(),
2273 ns_speech_prob_average,
2274 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002275#endif
2276 } else {
2277 test->set_has_echo_count(has_echo_count);
2278 test->set_has_voice_count(has_voice_count);
2279 test->set_is_saturated_count(is_saturated_count);
2280
2281 test->set_analog_level_average(analog_level_average);
2282 test->set_max_output_average(max_output_average);
2283
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002284#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002285 EXPECT_LE(0.0f, ns_speech_prob_average);
2286 EXPECT_GE(1.0f, ns_speech_prob_average);
2287 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002288#endif
2289 }
2290
2291 rewind(far_file_);
2292 rewind(near_file_);
2293 }
2294
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002295 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002296 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002297 }
2298}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002299
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002300TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2301 struct ChannelFormat {
2302 AudioProcessing::ChannelLayout in_layout;
2303 AudioProcessing::ChannelLayout out_layout;
2304 };
2305 ChannelFormat cf[] = {
2306 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2307 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2308 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2309 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002310
kwiberg62eaacf2016-02-17 06:39:05 -08002311 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002312 // Enable one component just to ensure some processing takes place.
2313 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002314 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002315 const int in_rate = 44100;
2316 const int out_rate = 48000;
2317 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2318 TotalChannelsFromLayout(cf[i].in_layout));
2319 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2320 ChannelsFromLayout(cf[i].out_layout));
2321
2322 // Run over a few chunks.
2323 for (int j = 0; j < 10; ++j) {
2324 EXPECT_NOERR(ap->ProcessStream(
2325 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002326 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002327 in_rate,
2328 cf[i].in_layout,
2329 out_rate,
2330 cf[i].out_layout,
2331 out_cb.channels()));
2332 }
2333 }
2334}
2335
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002336// Compares the reference and test arrays over a region around the expected
2337// delay. Finds the highest SNR in that region and adds the variance and squared
2338// error results to the supplied accumulators.
2339void UpdateBestSNR(const float* ref,
2340 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002341 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002342 int expected_delay,
2343 double* variance_acc,
2344 double* sq_error_acc) {
2345 double best_snr = std::numeric_limits<double>::min();
2346 double best_variance = 0;
2347 double best_sq_error = 0;
2348 // Search over a region of eight samples around the expected delay.
2349 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2350 ++delay) {
2351 double sq_error = 0;
2352 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002353 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002354 double error = test[i + delay] - ref[i];
2355 sq_error += error * error;
2356 variance += ref[i] * ref[i];
2357 }
2358
2359 if (sq_error == 0) {
2360 *variance_acc += variance;
2361 return;
2362 }
2363 double snr = variance / sq_error;
2364 if (snr > best_snr) {
2365 best_snr = snr;
2366 best_variance = variance;
2367 best_sq_error = sq_error;
2368 }
2369 }
2370
2371 *variance_acc += best_variance;
2372 *sq_error_acc += best_sq_error;
2373}
2374
2375// Used to test a multitude of sample rate and channel combinations. It works
2376// by first producing a set of reference files (in SetUpTestCase) that are
2377// assumed to be correct, as the used parameters are verified by other tests
2378// in this collection. Primarily the reference files are all produced at
2379// "native" rates which do not involve any resampling.
2380
2381// Each test pass produces an output file with a particular format. The output
2382// is matched against the reference file closest to its internal processing
2383// format. If necessary the output is resampled back to its process format.
2384// Due to the resampling distortion, we don't expect identical results, but
2385// enforce SNR thresholds which vary depending on the format. 0 is a special
2386// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002387typedef std::tr1::tuple<int, int, int, int, double, double>
2388 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002389class AudioProcessingTest
2390 : public testing::TestWithParam<AudioProcessingTestData> {
2391 public:
2392 AudioProcessingTest()
2393 : input_rate_(std::tr1::get<0>(GetParam())),
2394 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002395 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2396 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2397 expected_snr_(std::tr1::get<4>(GetParam())),
2398 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002399
2400 virtual ~AudioProcessingTest() {}
2401
2402 static void SetUpTestCase() {
2403 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002404 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002405 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002406 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2407 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2408 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002410 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2411 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2412 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002413 }
2414 }
2415 }
2416 }
2417
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002418 static void TearDownTestCase() {
2419 ClearTempFiles();
2420 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002421
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002422 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002423 // to a file specified with |output_file_prefix|. Both forward and reverse
2424 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002425 static void ProcessFormat(int input_rate,
2426 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002427 int reverse_input_rate,
2428 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002429 size_t num_input_channels,
2430 size_t num_output_channels,
2431 size_t num_reverse_input_channels,
2432 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002433 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002434 Config config;
2435 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002436 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002437 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002438
ekmeyerson60d9b332015-08-14 10:35:55 -07002439 ProcessingConfig processing_config = {
2440 {{input_rate, num_input_channels},
2441 {output_rate, num_output_channels},
2442 {reverse_input_rate, num_reverse_input_channels},
2443 {reverse_output_rate, num_reverse_output_channels}}};
2444 ap->Initialize(processing_config);
2445
2446 FILE* far_file =
2447 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002448 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002449 FILE* out_file =
2450 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2451 reverse_input_rate, reverse_output_rate,
2452 num_input_channels, num_output_channels,
2453 num_reverse_input_channels,
2454 num_reverse_output_channels, kForward).c_str(),
2455 "wb");
2456 FILE* rev_out_file =
2457 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2458 reverse_input_rate, reverse_output_rate,
2459 num_input_channels, num_output_channels,
2460 num_reverse_input_channels,
2461 num_reverse_output_channels, kReverse).c_str(),
2462 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002463 ASSERT_TRUE(far_file != NULL);
2464 ASSERT_TRUE(near_file != NULL);
2465 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002466 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002467
2468 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2469 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002470 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2471 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002472 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2473 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2475 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002476
2477 // Temporary buffers.
2478 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002479 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2480 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002481 std::unique_ptr<float[]> float_data(new float[max_length]);
2482 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002483
2484 int analog_level = 127;
2485 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2486 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002487 EXPECT_NOERR(ap->ProcessReverseStream(
2488 rev_cb.channels(), processing_config.reverse_input_stream(),
2489 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002490
2491 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2492 ap->echo_cancellation()->set_stream_drift_samples(0);
2493 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2494
2495 EXPECT_NOERR(ap->ProcessStream(
2496 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002497 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002498 input_rate,
2499 LayoutFromChannels(num_input_channels),
2500 output_rate,
2501 LayoutFromChannels(num_output_channels),
2502 out_cb.channels()));
2503
ekmeyerson60d9b332015-08-14 10:35:55 -07002504 // Dump forward output to file.
2505 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002506 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002507 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002508
pkasting25702cb2016-01-08 13:50:27 -08002509 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002510 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002511 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002512
ekmeyerson60d9b332015-08-14 10:35:55 -07002513 // Dump reverse output to file.
2514 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2515 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002516 size_t rev_out_length =
2517 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002518
pkasting25702cb2016-01-08 13:50:27 -08002519 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002520 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2521 rev_out_file));
2522
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002523 analog_level = ap->gain_control()->stream_analog_level();
2524 }
2525 fclose(far_file);
2526 fclose(near_file);
2527 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002528 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002529 }
2530
2531 protected:
2532 int input_rate_;
2533 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002534 int reverse_input_rate_;
2535 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002536 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002537 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002538};
2539
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002540TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002541 struct ChannelFormat {
2542 int num_input;
2543 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002544 int num_reverse_input;
2545 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002546 };
2547 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002548 {1, 1, 1, 1},
2549 {1, 1, 2, 1},
2550 {2, 1, 1, 1},
2551 {2, 1, 2, 1},
2552 {2, 2, 1, 1},
2553 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002554 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002555
pkasting25702cb2016-01-08 13:50:27 -08002556 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002557 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2558 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2559 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002560
ekmeyerson60d9b332015-08-14 10:35:55 -07002561 // Verify output for both directions.
2562 std::vector<StreamDirection> stream_directions;
2563 stream_directions.push_back(kForward);
2564 stream_directions.push_back(kReverse);
2565 for (StreamDirection file_direction : stream_directions) {
2566 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2567 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2568 const int out_num =
2569 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2570 const double expected_snr =
2571 file_direction ? expected_reverse_snr_ : expected_snr_;
2572
2573 const int min_ref_rate = std::min(in_rate, out_rate);
2574 int ref_rate;
2575
2576 if (min_ref_rate > 32000) {
2577 ref_rate = 48000;
2578 } else if (min_ref_rate > 16000) {
2579 ref_rate = 32000;
2580 } else if (min_ref_rate > 8000) {
2581 ref_rate = 16000;
2582 } else {
2583 ref_rate = 8000;
2584 }
aluebs776593b2016-03-15 14:04:58 -07002585#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002586 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002587 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002588 }
2589#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002590 FILE* out_file = fopen(
2591 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2592 reverse_output_rate_, cf[i].num_input,
2593 cf[i].num_output, cf[i].num_reverse_input,
2594 cf[i].num_reverse_output, file_direction).c_str(),
2595 "rb");
2596 // The reference files always have matching input and output channels.
2597 FILE* ref_file = fopen(
2598 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2599 cf[i].num_output, cf[i].num_output,
2600 cf[i].num_reverse_output, cf[i].num_reverse_output,
2601 file_direction).c_str(),
2602 "rb");
2603 ASSERT_TRUE(out_file != NULL);
2604 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002605
pkasting25702cb2016-01-08 13:50:27 -08002606 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2607 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002608 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002609 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002610 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002611 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002612 // Data from the resampled output, in case the reference and output rates
2613 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002614 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002615
ekmeyerson60d9b332015-08-14 10:35:55 -07002616 PushResampler<float> resampler;
2617 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002618
ekmeyerson60d9b332015-08-14 10:35:55 -07002619 // Compute the resampling delay of the output relative to the reference,
2620 // to find the region over which we should search for the best SNR.
2621 float expected_delay_sec = 0;
2622 if (in_rate != ref_rate) {
2623 // Input resampling delay.
2624 expected_delay_sec +=
2625 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2626 }
2627 if (out_rate != ref_rate) {
2628 // Output resampling delay.
2629 expected_delay_sec +=
2630 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2631 // Delay of converting the output back to its processing rate for
2632 // testing.
2633 expected_delay_sec +=
2634 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2635 }
2636 int expected_delay =
2637 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002638
ekmeyerson60d9b332015-08-14 10:35:55 -07002639 double variance = 0;
2640 double sq_error = 0;
2641 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2642 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2643 float* out_ptr = out_data.get();
2644 if (out_rate != ref_rate) {
2645 // Resample the output back to its internal processing rate if
2646 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002647 ASSERT_EQ(ref_length,
2648 static_cast<size_t>(resampler.Resample(
2649 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002650 out_ptr = cmp_data.get();
2651 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002652
ekmeyerson60d9b332015-08-14 10:35:55 -07002653 // Update the |sq_error| and |variance| accumulators with the highest
2654 // SNR of reference vs output.
2655 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2656 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002657 }
2658
ekmeyerson60d9b332015-08-14 10:35:55 -07002659 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2660 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2661 << cf[i].num_input << ", " << cf[i].num_output << ", "
2662 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2663 << ", " << file_direction << "): ";
2664 if (sq_error > 0) {
2665 double snr = 10 * log10(variance / sq_error);
2666 EXPECT_GE(snr, expected_snr);
2667 EXPECT_NE(0, expected_snr);
2668 std::cout << "SNR=" << snr << " dB" << std::endl;
2669 } else {
aluebs776593b2016-03-15 14:04:58 -07002670 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002671 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002672
ekmeyerson60d9b332015-08-14 10:35:55 -07002673 fclose(out_file);
2674 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002675 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002676 }
2677}
2678
2679#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2680INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002681 CommonFormats,
2682 AudioProcessingTest,
2683 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002684 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2685 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002686 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2687 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2688 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2689 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2690 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2691 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2692 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2693 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2694 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002695
ekmeyerson60d9b332015-08-14 10:35:55 -07002696 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2697 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2698 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2699 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2700 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2701 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2702 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2703 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2704 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2705 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2706 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2707 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002708
ekmeyerson60d9b332015-08-14 10:35:55 -07002709 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2710 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2711 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2712 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2713 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2714 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2715 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2716 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2717 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2718 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2719 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2720 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002721
ekmeyerson60d9b332015-08-14 10:35:55 -07002722 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2723 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2724 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2725 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2726 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2727 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2728 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2729 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2730 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2731 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002732 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002733 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002734
2735#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2736INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002737 CommonFormats,
2738 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002739 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2740 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2741 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2742 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2743 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2744 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002745 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2746 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2747 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2748 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2749 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2750 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002751
aluebs776593b2016-03-15 14:04:58 -07002752 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2753 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2754 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002755 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2756 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2757 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2758 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2759 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2760 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2761 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2762 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2763 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002764
aluebs776593b2016-03-15 14:04:58 -07002765 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2766 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2767 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2768 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2769 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2770 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2771 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2772 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2773 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002774 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2775 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2776 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002777
ekmeyerson60d9b332015-08-14 10:35:55 -07002778 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2779 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2780 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2781 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2782 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2783 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2784 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2785 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2786 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2787 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002788 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002789 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002790#endif
2791
niklase@google.com470e71d2011-07-07 08:21:25 +00002792} // namespace
peahc19f3122016-10-07 14:54:10 -07002793
2794TEST(ApmConfiguration, DefaultBehavior) {
2795 // Verify that the level controller is default off, it can be activated using
2796 // the config, and that the default initial level is maintained after the
2797 // config has been applied.
2798 std::unique_ptr<AudioProcessingImpl> apm(
2799 new AudioProcessingImpl(webrtc::Config()));
2800 AudioProcessing::Config config;
2801 EXPECT_FALSE(apm->config_.level_controller.enabled);
2802 // TODO(peah): Add test for the existence of the level controller object once
2803 // that is created only when that is specified in the config.
2804 // TODO(peah): Remove the testing for
2805 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2806 // is instead used to activate the level controller.
2807 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2808 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2809 apm->config_.level_controller.initial_peak_level_dbfs,
2810 std::numeric_limits<float>::epsilon());
2811 config.level_controller.enabled = true;
2812 apm->ApplyConfig(config);
2813 EXPECT_TRUE(apm->config_.level_controller.enabled);
2814 // TODO(peah): Add test for the existence of the level controller object once
2815 // that is created only when the that is specified in the config.
2816 // TODO(peah): Remove the testing for
2817 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2818 // is instead used to activate the level controller.
2819 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2820 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2821 apm->config_.level_controller.initial_peak_level_dbfs,
2822 std::numeric_limits<float>::epsilon());
2823}
2824
2825TEST(ApmConfiguration, ValidConfigBehavior) {
2826 // Verify that the initial level can be specified and is retained after the
2827 // config has been applied.
2828 std::unique_ptr<AudioProcessingImpl> apm(
2829 new AudioProcessingImpl(webrtc::Config()));
2830 AudioProcessing::Config config;
2831 config.level_controller.initial_peak_level_dbfs = -50.f;
2832 apm->ApplyConfig(config);
2833 EXPECT_FALSE(apm->config_.level_controller.enabled);
2834 // TODO(peah): Add test for the existence of the level controller object once
2835 // that is created only when the that is specified in the config.
2836 // TODO(peah): Remove the testing for
2837 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2838 // is instead used to activate the level controller.
2839 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2840 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2841 std::numeric_limits<float>::epsilon());
2842}
2843
2844TEST(ApmConfiguration, InValidConfigBehavior) {
2845 // Verify that the config is properly reset when nonproper values are applied
2846 // for the initial level.
2847
2848 // Verify that the config is properly reset when the specified initial peak
2849 // level is too low.
2850 std::unique_ptr<AudioProcessingImpl> apm(
2851 new AudioProcessingImpl(webrtc::Config()));
2852 AudioProcessing::Config config;
2853 config.level_controller.enabled = true;
2854 config.level_controller.initial_peak_level_dbfs = -101.f;
2855 apm->ApplyConfig(config);
2856 EXPECT_FALSE(apm->config_.level_controller.enabled);
2857 // TODO(peah): Add test for the existence of the level controller object once
2858 // that is created only when the that is specified in the config.
2859 // TODO(peah): Remove the testing for
2860 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2861 // is instead used to activate the level controller.
2862 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2863 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2864 apm->config_.level_controller.initial_peak_level_dbfs,
2865 std::numeric_limits<float>::epsilon());
2866
2867 // Verify that the config is properly reset when the specified initial peak
2868 // level is too high.
2869 apm.reset(new AudioProcessingImpl(webrtc::Config()));
2870 config = AudioProcessing::Config();
2871 config.level_controller.enabled = true;
2872 config.level_controller.initial_peak_level_dbfs = 1.f;
2873 apm->ApplyConfig(config);
2874 EXPECT_FALSE(apm->config_.level_controller.enabled);
2875 // TODO(peah): Add test for the existence of the level controller object once
2876 // that is created only when that is specified in the config.
2877 // TODO(peah): Remove the testing for
2878 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2879 // is instead used to activate the level controller.
2880 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2881 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2882 apm->config_.level_controller.initial_peak_level_dbfs,
2883 std::numeric_limits<float>::epsilon());
2884}
2885
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002886} // namespace webrtc