blob: b6e56c5fad2df73b9d91240743aabfa6185d8b3e [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 */
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000010#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080015#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000016#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/include/push_resampler.h"
20#include "common_audio/resampler/push_sinc_resampler.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
22#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
23#include "modules/audio_processing/audio_processing_impl.h"
24#include "modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
25#include "modules/audio_processing/common.h"
26#include "modules/audio_processing/include/audio_processing.h"
27#include "modules/audio_processing/level_controller/level_controller_constants.h"
28#include "modules/audio_processing/test/protobuf_utils.h"
29#include "modules/audio_processing/test/test_utils.h"
30#include "modules/include/module_common_types.h"
31#include "rtc_base/arraysize.h"
32#include "rtc_base/checks.h"
33#include "rtc_base/gtest_prod_util.h"
34#include "rtc_base/ignore_wundef.h"
35#include "rtc_base/protobuf_utils.h"
36#include "rtc_base/safe_minmax.h"
37#include "rtc_base/task_queue.h"
38#include "rtc_base/thread.h"
39#include "system_wrappers/include/event_wrapper.h"
40#include "system_wrappers/include/trace.h"
41#include "test/gtest.h"
42#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070043
44RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000045#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000046#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000049#endif
kwiberg77eab702016-09-28 17:42:01 -070050RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000051
andrew@webrtc.org27c69802014-02-18 20:24:56 +000052namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000053namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000054
ekmeyerson60d9b332015-08-14 10:35:55 -070055// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
56// applicable.
57
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000058// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000059// re-implemented; see comment at the bottom of this file. For now, the user has
60// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000061// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000062// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000063// be set to true with the command-line switch --write_ref_data.
64bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070065const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070066const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000067
aluebseb3603b2016-04-20 15:27:58 -070068#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
69// Android doesn't support 48kHz.
70const int kProcessSampleRates[] = {8000, 16000, 32000};
71#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070072const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070073#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000074
ekmeyerson60d9b332015-08-14 10:35:55 -070075enum StreamDirection { kForward = 0, kReverse };
76
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000077void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000078 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000079 cb->num_channels());
80 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000083 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080084 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000085 S16ToFloat(cb_int.channels()[i],
86 cb->num_frames(),
87 cb->channels()[i]);
88 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000089}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000090
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000091void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070092 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093}
94
andrew@webrtc.org103657b2014-04-24 18:28:56 +000095// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080096size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097 switch (layout) {
98 case AudioProcessing::kMono:
99 return 1;
100 case AudioProcessing::kMonoAndKeyboard:
101 case AudioProcessing::kStereo:
102 return 2;
103 case AudioProcessing::kStereoAndKeyboard:
104 return 3;
105 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700106 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800107 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000108}
109
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000110int TruncateToMultipleOf10(int value) {
111 return (value / 10) * 10;
112}
113
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800115 size_t samples_per_channel) {
116 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000118}
119
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800121 size_t samples_per_channel) {
122 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
124}
125
pkasting25702cb2016-01-08 13:50:27 -0800126void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
127 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000128 stereo[i * 2 + 1] = stereo[i * 2];
129 }
130}
131
yujo36b1a5f2017-06-12 12:45:32 -0700132void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800133 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000134 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
135 }
136}
137
138void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700139 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700140 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
141 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700142 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000143 }
144}
145
146void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800147 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700148 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700149 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700150 frame_data[i] = left;
151 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000152 }
153}
154
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000155void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700156 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700157 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
158 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700159 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000160 }
161}
162
andrew@webrtc.org81865342012-10-27 00:28:27 +0000163bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000164 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165 return false;
166 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 return false;
169 }
yujo36b1a5f2017-06-12 12:45:32 -0700170 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000172 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000173 return false;
174 }
175 return true;
176}
177
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000178void EnableAllAPComponents(AudioProcessing* ap) {
179#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
180 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
181
182 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
183 EXPECT_NOERR(ap->gain_control()->Enable(true));
184#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
185 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
186 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
187 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
188 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
189
190 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
191 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
192 EXPECT_NOERR(ap->gain_control()->Enable(true));
193#endif
194
peah8271d042016-11-22 07:24:52 -0800195 AudioProcessing::Config apm_config;
196 apm_config.high_pass_filter.enabled = true;
197 ap->ApplyConfig(apm_config);
198
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000199 EXPECT_NOERR(ap->level_estimator()->Enable(true));
200 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
201
202 EXPECT_NOERR(ap->voice_detection()->Enable(true));
203}
204
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000205// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000206template <class T>
207T AbsValue(T a) {
208 return a > 0 ? a: -a;
209}
210
211int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800212 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700213 const int16_t* frame_data = frame.data();
214 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800215 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700216 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000217 }
218
219 return max_data;
220}
221
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000222#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000223void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000224 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700225 EXPECT_EQ(reference.instant(), test.instant);
226 EXPECT_EQ(reference.average(), test.average);
227 EXPECT_EQ(reference.maximum(), test.maximum);
228 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229}
230
231void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000232 audioproc::Test::Statistic* msg) {
233 msg->set_instant(output.instant);
234 msg->set_average(output.average);
235 msg->set_maximum(output.maximum);
236 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000238#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239
Alex Loiko890988c2017-08-31 10:25:48 +0200240void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700241 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000243 ASSERT_TRUE(file != NULL);
244
245 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000246 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800247 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000248 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000249
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000250 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000251 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000252 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000253 fclose(file);
254}
255
Alex Loiko890988c2017-08-31 10:25:48 +0200256std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000257 std::ostringstream ss;
258 // Resource files are all stereo.
259 ss << name << sample_rate_hz / 1000 << "_stereo";
260 return test::ResourcePath(ss.str(), "pcm");
261}
262
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000263// Temporary filenames unique to this process. Used to be able to run these
264// tests in parallel as each process needs to be running in isolation they can't
265// have competing filenames.
266std::map<std::string, std::string> temp_filenames;
267
Alex Loiko890988c2017-08-31 10:25:48 +0200268std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000269 int input_rate,
270 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700271 int reverse_input_rate,
272 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800273 size_t num_input_channels,
274 size_t num_output_channels,
275 size_t num_reverse_input_channels,
276 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700277 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000278 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
280 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000281 if (num_output_channels == 1) {
282 ss << "mono";
283 } else if (num_output_channels == 2) {
284 ss << "stereo";
285 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700286 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000287 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700288 ss << output_rate / 1000;
289 if (num_reverse_output_channels == 1) {
290 ss << "_rmono";
291 } else if (num_reverse_output_channels == 2) {
292 ss << "_rstereo";
293 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700294 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700295 }
296 ss << reverse_output_rate / 1000;
297 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000298
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000299 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700300 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000301 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
302 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000303}
304
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000305void ClearTempFiles() {
306 for (auto& kv : temp_filenames)
307 remove(kv.second.c_str());
308}
309
Alex Loiko890988c2017-08-31 10:25:48 +0200310void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000311 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000312 ASSERT_TRUE(file != NULL);
313 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000314 fclose(file);
315}
316
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000317// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
318// stereo) file, converts to deinterleaved float (optionally downmixing) and
319// returns the result in |cb|. Returns false if the file ended (or on error) and
320// true otherwise.
321//
322// |int_data| and |float_data| are just temporary space that must be
323// sufficiently large to hold the 10 ms chunk.
324bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
325 ChannelBuffer<float>* cb) {
326 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000327 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000328 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
329 if (read_count != frame_size) {
330 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700331 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000332 return false; // This is expected.
333 }
334
335 S16ToFloat(int_data, frame_size, float_data);
336 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000337 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000338 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000339 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000340 cb->channels());
341 }
342
343 return true;
344}
345
niklase@google.com470e71d2011-07-07 08:21:25 +0000346class ApmTest : public ::testing::Test {
347 protected:
348 ApmTest();
349 virtual void SetUp();
350 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000351
352 static void SetUpTestCase() {
353 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000354 }
355
356 static void TearDownTestCase() {
357 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000358 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000359 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000360
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000361 // Used to select between int and float interface tests.
362 enum Format {
363 kIntFormat,
364 kFloatFormat
365 };
366
367 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000368 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000369 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800370 size_t num_input_channels,
371 size_t num_output_channels,
372 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000373 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000374 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000375 void EnableAllComponents();
376 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000377 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000378 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000379 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
380 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000381 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000382 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
383 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700384 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800385 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700386 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800387 void TestChangingForwardChannels(size_t num_in_channels,
388 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700389 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800390 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700391 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000392 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
393 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000394 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000395 int ProcessStreamChooser(Format format);
396 int AnalyzeReverseStreamChooser(Format format);
397 void ProcessDebugDump(const std::string& in_filename,
398 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800399 Format format,
400 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000401 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000402
403 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000404 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800405 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000406 AudioFrame* frame_;
407 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800408 std::unique_ptr<ChannelBuffer<float> > float_cb_;
409 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000410 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800411 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000412 FILE* far_file_;
413 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000414 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000415};
416
417ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000418 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000419#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800420 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
421 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000422#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000423#if defined(WEBRTC_MAC)
424 // A different file for Mac is needed because on this platform the AEC
425 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800426 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
427 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000428#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800429 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
430 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000431#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000432#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000433 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000434 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000435 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000436 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000437 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000438 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000439 out_file_(NULL) {
440 Config config;
441 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
442 apm_.reset(AudioProcessing::Create(config));
443}
niklase@google.com470e71d2011-07-07 08:21:25 +0000444
445void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000446 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000447
448 frame_ = new AudioFrame();
449 revframe_ = new AudioFrame();
450
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000451 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452}
453
454void ApmTest::TearDown() {
455 if (frame_) {
456 delete frame_;
457 }
458 frame_ = NULL;
459
460 if (revframe_) {
461 delete revframe_;
462 }
463 revframe_ = NULL;
464
465 if (far_file_) {
466 ASSERT_EQ(0, fclose(far_file_));
467 }
468 far_file_ = NULL;
469
470 if (near_file_) {
471 ASSERT_EQ(0, fclose(near_file_));
472 }
473 near_file_ = NULL;
474
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000475 if (out_file_) {
476 ASSERT_EQ(0, fclose(out_file_));
477 }
478 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000479}
480
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000481void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000482 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700483 ap->Initialize(
484 {{{frame_->sample_rate_hz_, frame_->num_channels_},
485 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700486 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700487 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000488}
489
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000490void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000491 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000492 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800493 size_t num_input_channels,
494 size_t num_output_channels,
495 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000496 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000497 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000498 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000499 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000500
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000501 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
502 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000503 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000504
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000505 if (far_file_) {
506 ASSERT_EQ(0, fclose(far_file_));
507 }
508 std::string filename = ResourceFilePath("far", sample_rate_hz);
509 far_file_ = fopen(filename.c_str(), "rb");
510 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
511 filename << "\n";
512
513 if (near_file_) {
514 ASSERT_EQ(0, fclose(near_file_));
515 }
516 filename = ResourceFilePath("near", sample_rate_hz);
517 near_file_ = fopen(filename.c_str(), "rb");
518 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
519 filename << "\n";
520
521 if (open_output_file) {
522 if (out_file_) {
523 ASSERT_EQ(0, fclose(out_file_));
524 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700525 filename = OutputFilePath(
526 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
527 reverse_sample_rate_hz, num_input_channels, num_output_channels,
528 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000529 out_file_ = fopen(filename.c_str(), "wb");
530 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
531 filename << "\n";
532 }
533}
534
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000535void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000536 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000537}
538
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000539bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
540 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000541 // The files always contain stereo audio.
542 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700543 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000544 sizeof(int16_t),
545 frame_size,
546 file);
547 if (read_count != frame_size) {
548 // Check that the file really ended.
549 EXPECT_NE(0, feof(file));
550 return false; // This is expected.
551 }
552
553 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700554 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000555 frame->samples_per_channel_);
556 }
557
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000558 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000559 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000560 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000561 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000562}
563
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000564bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
565 return ReadFrame(file, frame, NULL);
566}
567
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000568// If the end of the file has been reached, rewind it and attempt to read the
569// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000570void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
571 ChannelBuffer<float>* cb) {
572 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000573 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000574 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000575 }
576}
577
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000578void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
579 ReadFrameWithRewind(file, frame, NULL);
580}
581
andrew@webrtc.org81865342012-10-27 00:28:27 +0000582void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
583 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000584 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000585 EXPECT_EQ(apm_->kNoError,
586 apm_->gain_control()->set_stream_analog_level(127));
587 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000588}
589
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000590int ApmTest::ProcessStreamChooser(Format format) {
591 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000592 return apm_->ProcessStream(frame_);
593 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000594 return apm_->ProcessStream(float_cb_->channels(),
595 frame_->samples_per_channel_,
596 frame_->sample_rate_hz_,
597 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000598 output_sample_rate_hz_,
599 LayoutFromChannels(num_output_channels_),
600 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000601}
602
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000603int ApmTest::AnalyzeReverseStreamChooser(Format format) {
604 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700605 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000606 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000607 return apm_->AnalyzeReverseStream(
608 revfloat_cb_->channels(),
609 revframe_->samples_per_channel_,
610 revframe_->sample_rate_hz_,
611 LayoutFromChannels(revframe_->num_channels_));
612}
613
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000614void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
615 int delay_min, int delay_max) {
616 // The |revframe_| and |frame_| should include the proper frame information,
617 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000618 AudioFrame tmp_frame;
619 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 bool causal = true;
621
622 tmp_frame.CopyFrom(*revframe_);
623 SetFrameTo(&tmp_frame, 0);
624
625 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
626 // Initialize the |frame_queue| with empty frames.
627 int frame_delay = delay_ms / 10;
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 causal = false;
634 }
635 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000636 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000637 frame->CopyFrom(tmp_frame);
638 frame_queue.push(frame);
639 frame_delay--;
640 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000641 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
642 // need enough frames with audio to have reliable estimates, but as few as
643 // possible to keep processing time down. 4.5 seconds seemed to be a good
644 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000645 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000646 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 frame->CopyFrom(tmp_frame);
648 // Use the near end recording, since that has more speech in it.
649 ASSERT_TRUE(ReadFrame(near_file_, frame));
650 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000651 AudioFrame* reverse_frame = frame;
652 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000653 if (!causal) {
654 reverse_frame = frame_queue.front();
655 // When we call ProcessStream() the frame is modified, so we can't use the
656 // pointer directly when things are non-causal. Use an intermediate frame
657 // and copy the data.
658 process_frame = &tmp_frame;
659 process_frame->CopyFrom(*frame);
660 }
aluebsb0319552016-03-17 20:39:53 -0700661 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
663 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
664 frame = frame_queue.front();
665 frame_queue.pop();
666 delete frame;
667
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000668 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000669 int median;
670 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000671 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000672 // Discard the first delay metrics to avoid convergence effects.
673 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000674 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
675 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000676 }
677 }
678
679 rewind(near_file_);
680 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000681 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000682 frame_queue.pop();
683 delete frame;
684 }
685 // Calculate expected delay estimate and acceptable regions. Further,
686 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700687 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700688 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700689 const int expected_median =
690 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
691 const int expected_median_high = rtc::SafeClamp<int>(
692 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700693 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700694 const int expected_median_low = rtc::SafeClamp<int>(
695 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700696 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000697 // Verify delay metrics.
698 int median;
699 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000700 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000701 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000702 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
703 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000704 EXPECT_GE(expected_median_high, median);
705 EXPECT_LE(expected_median_low, median);
706}
707
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000708void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000710 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000714 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000715 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000716
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000718 EXPECT_EQ(apm_->kNoError,
719 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000720 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000721 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000723
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000724 // Other stream parameters set correctly.
725 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 EXPECT_EQ(apm_->kNoError,
727 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000729 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000731 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000732 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
733 EXPECT_EQ(apm_->kNoError,
734 apm_->echo_cancellation()->enable_drift_compensation(false));
735
736 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000737 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000738 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000739 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000740 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000741
742 // Resets after successful ProcessStream().
743 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000744 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000745 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000746 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000747
748 // Other stream parameters set correctly.
749 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
750 EXPECT_EQ(apm_->kNoError,
751 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000752 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753 EXPECT_EQ(apm_->kNoError,
754 apm_->gain_control()->set_stream_analog_level(127));
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 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
758
759 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000760 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000761 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000762
763 // Resets after successful ProcessStream().
764 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000765 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000766 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000767 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000768 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000769
770 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
772 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
773 EXPECT_EQ(apm_->kNoError,
774 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000775 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000776 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000777
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000778 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000779 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000780 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000781 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000782 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000783
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000784 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000786 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000787 EXPECT_EQ(apm_->kNoError,
788 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000789 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000790}
791
792TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000793 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000794}
795
796TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000797 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000798}
799
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000800TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
801 EXPECT_EQ(0, apm_->delay_offset_ms());
802 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
803 EXPECT_EQ(50, apm_->stream_delay_ms());
804}
805
806TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
807 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000808 apm_->set_delay_offset_ms(100);
809 EXPECT_EQ(100, apm_->delay_offset_ms());
810 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000811 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000812 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
813 EXPECT_EQ(200, apm_->stream_delay_ms());
814
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000815 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000816 apm_->set_delay_offset_ms(-50);
817 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000818 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
819 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000820 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
821 EXPECT_EQ(50, apm_->stream_delay_ms());
822}
823
Michael Graczyk86c6d332015-07-23 11:41:39 -0700824void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800825 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700826 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000827 frame_->num_channels_ = num_channels;
828 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700829 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000830}
831
Michael Graczyk86c6d332015-07-23 11:41:39 -0700832void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800833 size_t num_in_channels,
834 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700835 AudioProcessing::Error expected_return) {
836 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
837 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
838
839 EXPECT_EQ(expected_return,
840 apm_->ProcessStream(float_cb_->channels(), input_stream,
841 output_stream, float_cb_->channels()));
842}
843
844void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800845 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700846 AudioProcessing::Error expected_return) {
847 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700848 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
849 {output_sample_rate_hz_, apm_->num_output_channels()},
850 {frame_->sample_rate_hz_, num_rev_channels},
851 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700852
ekmeyerson60d9b332015-08-14 10:35:55 -0700853 EXPECT_EQ(
854 expected_return,
855 apm_->ProcessReverseStream(
856 float_cb_->channels(), processing_config.reverse_input_stream(),
857 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700858}
859
860TEST_F(ApmTest, ChannelsInt16Interface) {
861 // Testing number of invalid and valid channels.
862 Init(16000, 16000, 16000, 4, 4, 4, false);
863
864 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
865
Peter Kasting69558702016-01-12 16:26:35 -0800866 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700867 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000868 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000869 }
870}
871
Michael Graczyk86c6d332015-07-23 11:41:39 -0700872TEST_F(ApmTest, Channels) {
873 // Testing number of invalid and valid channels.
874 Init(16000, 16000, 16000, 4, 4, 4, false);
875
876 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
877 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
878
Peter Kasting69558702016-01-12 16:26:35 -0800879 for (size_t i = 1; i < 4; ++i) {
880 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700881 // Output channels much be one or match input channels.
882 if (j == 1 || i == j) {
883 TestChangingForwardChannels(i, j, kNoErr);
884 TestChangingReverseChannels(i, kNoErr);
885
886 EXPECT_EQ(i, apm_->num_input_channels());
887 EXPECT_EQ(j, apm_->num_output_channels());
888 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800889 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700890 } else {
891 TestChangingForwardChannels(i, j,
892 AudioProcessing::kBadNumberChannelsError);
893 }
894 }
895 }
896}
897
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000898TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000899 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000900 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000901 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000902 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700903 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800904 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000905 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000906 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000907 }
908}
909
niklase@google.com470e71d2011-07-07 08:21:25 +0000910TEST_F(ApmTest, EchoCancellation) {
911 EXPECT_EQ(apm_->kNoError,
912 apm_->echo_cancellation()->enable_drift_compensation(true));
913 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
914 EXPECT_EQ(apm_->kNoError,
915 apm_->echo_cancellation()->enable_drift_compensation(false));
916 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
917
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 EchoCancellation::SuppressionLevel level[] = {
919 EchoCancellation::kLowSuppression,
920 EchoCancellation::kModerateSuppression,
921 EchoCancellation::kHighSuppression,
922 };
pkasting25702cb2016-01-08 13:50:27 -0800923 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 EXPECT_EQ(apm_->kNoError,
925 apm_->echo_cancellation()->set_suppression_level(level[i]));
926 EXPECT_EQ(level[i],
927 apm_->echo_cancellation()->suppression_level());
928 }
929
930 EchoCancellation::Metrics metrics;
931 EXPECT_EQ(apm_->kNotEnabledError,
932 apm_->echo_cancellation()->GetMetrics(&metrics));
933
ivoc3e9a5372016-10-28 07:55:33 -0700934 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
935 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
936
niklase@google.com470e71d2011-07-07 08:21:25 +0000937 EXPECT_EQ(apm_->kNoError,
938 apm_->echo_cancellation()->enable_metrics(true));
939 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
940 EXPECT_EQ(apm_->kNoError,
941 apm_->echo_cancellation()->enable_metrics(false));
942 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
943
ivoc48dfab52016-10-28 03:29:31 -0700944 EXPECT_EQ(apm_->kNoError,
945 apm_->echo_cancellation()->enable_delay_logging(true));
946 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
947 EXPECT_EQ(apm_->kNoError,
948 apm_->echo_cancellation()->enable_delay_logging(false));
949 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
950
ivoc3e9a5372016-10-28 07:55:33 -0700951 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
952 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
953
954 int median = 0;
955 int std = 0;
956 float poor_fraction = 0;
957 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
958 &median, &std, &poor_fraction));
959
niklase@google.com470e71d2011-07-07 08:21:25 +0000960 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
961 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
962 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
963 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000964
965 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
966 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
967 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
968 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
969 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
970 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000971}
972
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000973TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000974 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000975 // Enable AEC only.
976 EXPECT_EQ(apm_->kNoError,
977 apm_->echo_cancellation()->enable_drift_compensation(false));
978 EXPECT_EQ(apm_->kNoError,
979 apm_->echo_cancellation()->enable_metrics(false));
980 EXPECT_EQ(apm_->kNoError,
981 apm_->echo_cancellation()->enable_delay_logging(true));
982 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000983 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700984 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000985 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000986
987 // Internally in the AEC the amount of lookahead the delay estimation can
988 // handle is 15 blocks and the maximum delay is set to 60 blocks.
989 const int kLookaheadBlocks = 15;
990 const int kMaxDelayBlocks = 60;
991 // The AEC has a startup time before it actually starts to process. This
992 // procedure can flush the internal far-end buffer, which of course affects
993 // the delay estimation. Therefore, we set a system_delay high enough to
994 // avoid that. The smallest system_delay you can report without flushing the
995 // buffer is 66 ms in 8 kHz.
996 //
997 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
998 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
999 // delay estimation. This should be noted though. In case of test failure,
1000 // this could be the cause.
1001 const int kSystemDelayMs = 66;
1002 // Test a couple of corner cases and verify that the estimated delay is
1003 // within a valid region (set to +-1.5 blocks). Note that these cases are
1004 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001005 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001006 Init(kProcessSampleRates[i],
1007 kProcessSampleRates[i],
1008 kProcessSampleRates[i],
1009 2,
1010 2,
1011 2,
1012 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001013 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001014 const int num_ms_per_block =
1015 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001016 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1017 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1018
1019 // 1) Verify correct delay estimate at lookahead boundary.
1020 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1021 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1022 delay_max_ms);
1023 // 2) A delay less than maximum lookahead should give an delay estimate at
1024 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1025 delay_ms -= 20;
1026 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1027 delay_max_ms);
1028 // 3) Three values around zero delay. Note that we need to compensate for
1029 // the fake system_delay.
1030 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1031 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1032 delay_max_ms);
1033 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1034 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1035 delay_max_ms);
1036 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1037 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1038 delay_max_ms);
1039 // 4) Verify correct delay estimate at maximum delay boundary.
1040 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1041 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1042 delay_max_ms);
1043 // 5) A delay above the maximum delay should give an estimate at the
1044 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1045 delay_ms += 20;
1046 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1047 delay_max_ms);
1048 }
1049}
1050
niklase@google.com470e71d2011-07-07 08:21:25 +00001051TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001053 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001054 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1055 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1056
niklase@google.com470e71d2011-07-07 08:21:25 +00001057 // Toggle routing modes
1058 EchoControlMobile::RoutingMode mode[] = {
1059 EchoControlMobile::kQuietEarpieceOrHeadset,
1060 EchoControlMobile::kEarpiece,
1061 EchoControlMobile::kLoudEarpiece,
1062 EchoControlMobile::kSpeakerphone,
1063 EchoControlMobile::kLoudSpeakerphone,
1064 };
pkasting25702cb2016-01-08 13:50:27 -08001065 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 EXPECT_EQ(apm_->kNoError,
1067 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1068 EXPECT_EQ(mode[i],
1069 apm_->echo_control_mobile()->routing_mode());
1070 }
1071 // Turn comfort noise off/on
1072 EXPECT_EQ(apm_->kNoError,
1073 apm_->echo_control_mobile()->enable_comfort_noise(false));
1074 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1075 EXPECT_EQ(apm_->kNoError,
1076 apm_->echo_control_mobile()->enable_comfort_noise(true));
1077 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001078 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001079 const size_t echo_path_size =
1080 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001081 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1082 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001083 EXPECT_EQ(apm_->kNullPointerError,
1084 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1085 EXPECT_EQ(apm_->kNullPointerError,
1086 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1087 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001088 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001089 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001090 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001091 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001092 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001093 echo_path_in[i] = echo_path_out[i] + 1;
1094 }
1095 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001096 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
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()->SetEchoPath(echo_path_in.get(),
1099 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001100 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001101 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1102 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001103 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001104 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1105 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001106
1107 // Process a few frames with NS in the default disabled state. This exercises
1108 // a different codepath than with it enabled.
1109 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1110 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1111 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1112 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1113
niklase@google.com470e71d2011-07-07 08:21:25 +00001114 // Turn AECM off
1115 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1116 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1117}
1118
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001119TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001120 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001121 EXPECT_EQ(apm_->kNoError,
1122 apm_->gain_control()->set_mode(
1123 apm_->gain_control()->mode()));
1124
1125 GainControl::Mode mode[] = {
1126 GainControl::kAdaptiveAnalog,
1127 GainControl::kAdaptiveDigital,
1128 GainControl::kFixedDigital
1129 };
pkasting25702cb2016-01-08 13:50:27 -08001130 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 EXPECT_EQ(apm_->kNoError,
1132 apm_->gain_control()->set_mode(mode[i]));
1133 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1134 }
1135 // Testing invalid target levels
1136 EXPECT_EQ(apm_->kBadParameterError,
1137 apm_->gain_control()->set_target_level_dbfs(-3));
1138 EXPECT_EQ(apm_->kBadParameterError,
1139 apm_->gain_control()->set_target_level_dbfs(-40));
1140 // Testing valid target levels
1141 EXPECT_EQ(apm_->kNoError,
1142 apm_->gain_control()->set_target_level_dbfs(
1143 apm_->gain_control()->target_level_dbfs()));
1144
1145 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001146 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001147 EXPECT_EQ(apm_->kNoError,
1148 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1149 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1150 }
1151
1152 // Testing invalid compression gains
1153 EXPECT_EQ(apm_->kBadParameterError,
1154 apm_->gain_control()->set_compression_gain_db(-1));
1155 EXPECT_EQ(apm_->kBadParameterError,
1156 apm_->gain_control()->set_compression_gain_db(100));
1157
1158 // Testing valid compression gains
1159 EXPECT_EQ(apm_->kNoError,
1160 apm_->gain_control()->set_compression_gain_db(
1161 apm_->gain_control()->compression_gain_db()));
1162
1163 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001164 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 EXPECT_EQ(apm_->kNoError,
1166 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1167 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1168 }
1169
1170 // Testing limiter off/on
1171 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1172 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1173 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1174 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1175
1176 // Testing invalid level limits
1177 EXPECT_EQ(apm_->kBadParameterError,
1178 apm_->gain_control()->set_analog_level_limits(-1, 512));
1179 EXPECT_EQ(apm_->kBadParameterError,
1180 apm_->gain_control()->set_analog_level_limits(100000, 512));
1181 EXPECT_EQ(apm_->kBadParameterError,
1182 apm_->gain_control()->set_analog_level_limits(512, -1));
1183 EXPECT_EQ(apm_->kBadParameterError,
1184 apm_->gain_control()->set_analog_level_limits(512, 100000));
1185 EXPECT_EQ(apm_->kBadParameterError,
1186 apm_->gain_control()->set_analog_level_limits(512, 255));
1187
1188 // Testing valid level limits
1189 EXPECT_EQ(apm_->kNoError,
1190 apm_->gain_control()->set_analog_level_limits(
1191 apm_->gain_control()->analog_level_minimum(),
1192 apm_->gain_control()->analog_level_maximum()));
1193
1194 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001195 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001196 EXPECT_EQ(apm_->kNoError,
1197 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1198 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1199 }
1200
1201 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001202 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 EXPECT_EQ(apm_->kNoError,
1204 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1205 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1206 }
1207
1208 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1209
1210 // Turn AGC off
1211 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1212 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1213}
1214
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001215void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001216 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001217 EXPECT_EQ(apm_->kNoError,
1218 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1219 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1220
1221 int out_analog_level = 0;
1222 for (int i = 0; i < 2000; ++i) {
1223 ReadFrameWithRewind(near_file_, frame_);
1224 // Ensure the audio is at a low level, so the AGC will try to increase it.
1225 ScaleFrame(frame_, 0.25);
1226
1227 // Always pass in the same volume.
1228 EXPECT_EQ(apm_->kNoError,
1229 apm_->gain_control()->set_stream_analog_level(100));
1230 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1231 out_analog_level = apm_->gain_control()->stream_analog_level();
1232 }
1233
1234 // Ensure the AGC is still able to reach the maximum.
1235 EXPECT_EQ(255, out_analog_level);
1236}
1237
1238// Verifies that despite volume slider quantization, the AGC can continue to
1239// increase its volume.
1240TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001241 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001242 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1243 }
1244}
1245
1246void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001247 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001248 EXPECT_EQ(apm_->kNoError,
1249 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1250 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1251
1252 int out_analog_level = 100;
1253 for (int i = 0; i < 1000; ++i) {
1254 ReadFrameWithRewind(near_file_, frame_);
1255 // Ensure the audio is at a low level, so the AGC will try to increase it.
1256 ScaleFrame(frame_, 0.25);
1257
1258 EXPECT_EQ(apm_->kNoError,
1259 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1260 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1261 out_analog_level = apm_->gain_control()->stream_analog_level();
1262 }
1263
1264 // Ensure the volume was raised.
1265 EXPECT_GT(out_analog_level, 100);
1266 int highest_level_reached = out_analog_level;
1267 // Simulate a user manual volume change.
1268 out_analog_level = 100;
1269
1270 for (int i = 0; i < 300; ++i) {
1271 ReadFrameWithRewind(near_file_, frame_);
1272 ScaleFrame(frame_, 0.25);
1273
1274 EXPECT_EQ(apm_->kNoError,
1275 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1276 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1277 out_analog_level = apm_->gain_control()->stream_analog_level();
1278 // Check that AGC respected the manually adjusted volume.
1279 EXPECT_LT(out_analog_level, highest_level_reached);
1280 }
1281 // Check that the volume was still raised.
1282 EXPECT_GT(out_analog_level, 100);
1283}
1284
1285TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001286 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001287 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1288 }
1289}
1290
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001291#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1292TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1293 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001294 const size_t kSamplesPerChannel =
1295 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001296 const size_t kNumInputChannels = 2;
1297 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001298 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001299 const float kScaleFactor = 0.25f;
1300 Config config;
1301 std::vector<webrtc::Point> geometry;
1302 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1303 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1304 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001305 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001306 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001307 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001308 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001309 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1310 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1311 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001312 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1313 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001314 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1315 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001316 std::string filename = ResourceFilePath("far", kSampleRateHz);
1317 FILE* far_file = fopen(filename.c_str(), "rb");
1318 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1319 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1320 const int kDefaultCompressionGain =
1321 apm->gain_control()->compression_gain_db();
1322 bool is_target = false;
1323 EXPECT_CALL(*beamformer, is_target_present())
1324 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001325 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001326 ASSERT_TRUE(ReadChunk(far_file,
1327 int_data.get(),
1328 float_data.get(),
1329 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001330 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001331 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001332 src_buf.channels()[j][k] *= kScaleFactor;
1333 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001334 }
1335 EXPECT_EQ(kNoErr,
1336 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001337 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001338 kSampleRateHz,
1339 LayoutFromChannels(src_buf.num_channels()),
1340 kSampleRateHz,
1341 LayoutFromChannels(dest_buf.num_channels()),
1342 dest_buf.channels()));
1343 }
1344 EXPECT_EQ(kDefaultVolume,
1345 apm->gain_control()->stream_analog_level());
1346 EXPECT_EQ(kDefaultCompressionGain,
1347 apm->gain_control()->compression_gain_db());
1348 rewind(far_file);
1349 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001350 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001351 ASSERT_TRUE(ReadChunk(far_file,
1352 int_data.get(),
1353 float_data.get(),
1354 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001355 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001356 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001357 src_buf.channels()[j][k] *= kScaleFactor;
1358 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001359 }
1360 EXPECT_EQ(kNoErr,
1361 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001362 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001363 kSampleRateHz,
1364 LayoutFromChannels(src_buf.num_channels()),
1365 kSampleRateHz,
1366 LayoutFromChannels(dest_buf.num_channels()),
1367 dest_buf.channels()));
1368 }
1369 EXPECT_LT(kDefaultVolume,
1370 apm->gain_control()->stream_analog_level());
1371 EXPECT_LT(kDefaultCompressionGain,
1372 apm->gain_control()->compression_gain_db());
1373 ASSERT_EQ(0, fclose(far_file));
1374}
1375#endif
1376
niklase@google.com470e71d2011-07-07 08:21:25 +00001377TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001378 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001379 NoiseSuppression::Level level[] = {
1380 NoiseSuppression::kLow,
1381 NoiseSuppression::kModerate,
1382 NoiseSuppression::kHigh,
1383 NoiseSuppression::kVeryHigh
1384 };
pkasting25702cb2016-01-08 13:50:27 -08001385 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001386 EXPECT_EQ(apm_->kNoError,
1387 apm_->noise_suppression()->set_level(level[i]));
1388 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1389 }
1390
andrew@webrtc.org648af742012-02-08 01:57:29 +00001391 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001392 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1393 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1394 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1395 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1396}
1397
1398TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001399 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001400 AudioProcessing::Config apm_config;
1401 apm_config.high_pass_filter.enabled = true;
1402 apm_->ApplyConfig(apm_config);
1403 apm_config.high_pass_filter.enabled = false;
1404 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001405}
1406
1407TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001408 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001409 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001411
1412 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1413
1414 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1415 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1416
1417 // Run this test in wideband; in super-wb, the splitting filter distorts the
1418 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001419 frame_->samples_per_channel_ = 160;
1420 frame_->num_channels_ = 2;
1421 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001422
1423 // Min value if no frames have been processed.
1424 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1425
1426 // Min value on zero frames.
1427 SetFrameTo(frame_, 0);
1428 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1429 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1430 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1431
1432 // Try a few RMS values.
1433 // (These also test that the value resets after retrieving it.)
1434 SetFrameTo(frame_, 32767);
1435 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1436 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1437 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1438
1439 SetFrameTo(frame_, 30000);
1440 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1441 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1442 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1443
1444 SetFrameTo(frame_, 10000);
1445 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1446 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1447 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1448
1449 SetFrameTo(frame_, 10);
1450 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1451 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1452 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1453
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001454 // Verify reset after enable/disable.
1455 SetFrameTo(frame_, 32767);
1456 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1457 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1458 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1459 SetFrameTo(frame_, 1);
1460 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1461 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1462
1463 // Verify reset after initialize.
1464 SetFrameTo(frame_, 32767);
1465 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1466 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1467 SetFrameTo(frame_, 1);
1468 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1469 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001470}
1471
1472TEST_F(ApmTest, VoiceDetection) {
1473 // Test external VAD
1474 EXPECT_EQ(apm_->kNoError,
1475 apm_->voice_detection()->set_stream_has_voice(true));
1476 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1477 EXPECT_EQ(apm_->kNoError,
1478 apm_->voice_detection()->set_stream_has_voice(false));
1479 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1480
andrew@webrtc.org648af742012-02-08 01:57:29 +00001481 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001482 VoiceDetection::Likelihood likelihood[] = {
1483 VoiceDetection::kVeryLowLikelihood,
1484 VoiceDetection::kLowLikelihood,
1485 VoiceDetection::kModerateLikelihood,
1486 VoiceDetection::kHighLikelihood
1487 };
pkasting25702cb2016-01-08 13:50:27 -08001488 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001489 EXPECT_EQ(apm_->kNoError,
1490 apm_->voice_detection()->set_likelihood(likelihood[i]));
1491 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1492 }
1493
1494 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001495 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001496 EXPECT_EQ(apm_->kBadParameterError,
1497 apm_->voice_detection()->set_frame_size_ms(12));
1498
andrew@webrtc.org648af742012-02-08 01:57:29 +00001499 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001500 for (int i = 10; i <= 30; i += 10) {
1501 EXPECT_EQ(apm_->kNoError,
1502 apm_->voice_detection()->set_frame_size_ms(i));
1503 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1504 }
1505 */
1506
andrew@webrtc.org648af742012-02-08 01:57:29 +00001507 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001508 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1509 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1510 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1511 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1512
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001513 // Test that AudioFrame activity is maintained when VAD is disabled.
1514 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1515 AudioFrame::VADActivity activity[] = {
1516 AudioFrame::kVadActive,
1517 AudioFrame::kVadPassive,
1518 AudioFrame::kVadUnknown
1519 };
pkasting25702cb2016-01-08 13:50:27 -08001520 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001521 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001522 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001523 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001524 }
1525
1526 // Test that AudioFrame activity is set when VAD is enabled.
1527 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001528 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001529 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001530 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001531
niklase@google.com470e71d2011-07-07 08:21:25 +00001532 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1533}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001534
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001535TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1536 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1537 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1538 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1539 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1540 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1541 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1542 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1543}
1544
1545TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001546 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001547 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001548 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001549 AudioFrame frame_copy;
1550 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001551 for (int j = 0; j < 1000; j++) {
1552 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1553 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001554 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1555 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001556 }
1557 }
1558}
1559
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001560TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1561 // Test that ProcessStream copies input to output even with no processing.
1562 const size_t kSamples = 80;
1563 const int sample_rate = 8000;
1564 const float src[kSamples] = {
1565 -1.0f, 0.0f, 1.0f
1566 };
1567 float dest[kSamples] = {};
1568
1569 auto src_channels = &src[0];
1570 auto dest_channels = &dest[0];
1571
1572 apm_.reset(AudioProcessing::Create());
1573 EXPECT_NOERR(apm_->ProcessStream(
1574 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1575 sample_rate, LayoutFromChannels(1), &dest_channels));
1576
1577 for (size_t i = 0; i < kSamples; ++i) {
1578 EXPECT_EQ(src[i], dest[i]);
1579 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001580
1581 // Same for ProcessReverseStream.
1582 float rev_dest[kSamples] = {};
1583 auto rev_dest_channels = &rev_dest[0];
1584
1585 StreamConfig input_stream = {sample_rate, 1};
1586 StreamConfig output_stream = {sample_rate, 1};
1587 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1588 output_stream, &rev_dest_channels));
1589
1590 for (size_t i = 0; i < kSamples; ++i) {
1591 EXPECT_EQ(src[i], rev_dest[i]);
1592 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001593}
1594
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001595TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1596 EnableAllComponents();
1597
pkasting25702cb2016-01-08 13:50:27 -08001598 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001599 Init(kProcessSampleRates[i],
1600 kProcessSampleRates[i],
1601 kProcessSampleRates[i],
1602 2,
1603 2,
1604 2,
1605 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001606 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001607 ASSERT_EQ(0, feof(far_file_));
1608 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001609 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001610 CopyLeftToRightChannel(revframe_->mutable_data(),
1611 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001612
aluebsb0319552016-03-17 20:39:53 -07001613 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001614
yujo36b1a5f2017-06-12 12:45:32 -07001615 CopyLeftToRightChannel(frame_->mutable_data(),
1616 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001617 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1618
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001619 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001620 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001621 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001622 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001623 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001624 analog_level = apm_->gain_control()->stream_analog_level();
1625
yujo36b1a5f2017-06-12 12:45:32 -07001626 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001627 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001628 rewind(far_file_);
1629 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001630 }
1631}
1632
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001633TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001634 // Verify the filter is not active through undistorted audio when:
1635 // 1. No components are enabled...
1636 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001637 AudioFrame frame_copy;
1638 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001639 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1640 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1641 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1642
1643 // 2. Only the level estimator is enabled...
1644 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001645 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001646 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1647 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1648 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1649 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1650 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1651
1652 // 3. Only VAD is enabled...
1653 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001654 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001655 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1656 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1658 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1659 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1660
1661 // 4. Both VAD and the level estimator are enabled...
1662 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001663 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001664 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1665 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1666 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1667 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1668 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1669 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1670 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1671
1672 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001673 frame_->samples_per_channel_ = 160;
1674 frame_->num_channels_ = 2;
1675 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001676 // Enable AEC, which would require the filter in super-wb. We rely on the
1677 // first few frames of data being unaffected by the AEC.
1678 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1679 // behavior of the AEC. Think of something more robust.
1680 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001681 // Make sure we have extended filter enabled. This makes sure nothing is
1682 // touched until we have a farend frame.
1683 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001684 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001685 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001686 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001687 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001688 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001689 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001690 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1691 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001692 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001693 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1694 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1695
1696 // Check the test is valid. We should have distortion from the filter
1697 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001698 frame_->samples_per_channel_ = 320;
1699 frame_->num_channels_ = 2;
1700 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001701 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001702 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001703 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001704 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001705 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1706 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1707}
1708
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001709#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1710void ApmTest::ProcessDebugDump(const std::string& in_filename,
1711 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001712 Format format,
1713 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001714 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001715 FILE* in_file = fopen(in_filename.c_str(), "rb");
1716 ASSERT_TRUE(in_file != NULL);
1717 audioproc::Event event_msg;
1718 bool first_init = true;
1719
1720 while (ReadMessageFromFile(in_file, &event_msg)) {
1721 if (event_msg.type() == audioproc::Event::INIT) {
1722 const audioproc::Init msg = event_msg.init();
1723 int reverse_sample_rate = msg.sample_rate();
1724 if (msg.has_reverse_sample_rate()) {
1725 reverse_sample_rate = msg.reverse_sample_rate();
1726 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001727 int output_sample_rate = msg.sample_rate();
1728 if (msg.has_output_sample_rate()) {
1729 output_sample_rate = msg.output_sample_rate();
1730 }
1731
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001732 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001733 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001734 reverse_sample_rate,
1735 msg.num_input_channels(),
1736 msg.num_output_channels(),
1737 msg.num_reverse_channels(),
1738 false);
1739 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001740 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001741 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001742 auto aec_dump =
1743 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1744 EXPECT_TRUE(aec_dump);
1745 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001746 first_init = false;
1747 }
1748
1749 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1750 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1751
1752 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001753 ASSERT_EQ(revframe_->num_channels_,
1754 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001755 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001756 memcpy(revfloat_cb_->channels()[i],
1757 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001758 msg.channel(i).size());
1759 }
1760 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001761 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001762 if (format == kFloatFormat) {
1763 // We're using an int16 input file; convert to float.
1764 ConvertToFloat(*revframe_, revfloat_cb_.get());
1765 }
1766 }
1767 AnalyzeReverseStreamChooser(format);
1768
1769 } else if (event_msg.type() == audioproc::Event::STREAM) {
1770 const audioproc::Stream msg = event_msg.stream();
1771 // ProcessStream could have changed this for the output frame.
1772 frame_->num_channels_ = apm_->num_input_channels();
1773
1774 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1775 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1776 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1777 if (msg.has_keypress()) {
1778 apm_->set_stream_key_pressed(msg.keypress());
1779 } else {
1780 apm_->set_stream_key_pressed(true);
1781 }
1782
1783 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001784 ASSERT_EQ(frame_->num_channels_,
1785 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001786 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001787 memcpy(float_cb_->channels()[i],
1788 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001789 msg.input_channel(i).size());
1790 }
1791 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001792 memcpy(frame_->mutable_data(), msg.input_data().data(),
1793 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001794 if (format == kFloatFormat) {
1795 // We're using an int16 input file; convert to float.
1796 ConvertToFloat(*frame_, float_cb_.get());
1797 }
1798 }
1799 ProcessStreamChooser(format);
1800 }
1801 }
aleloif4dd1912017-06-15 01:55:38 -07001802 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001803 fclose(in_file);
1804}
1805
1806void ApmTest::VerifyDebugDumpTest(Format format) {
1807 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001808 std::string format_string;
1809 switch (format) {
1810 case kIntFormat:
1811 format_string = "_int";
1812 break;
1813 case kFloatFormat:
1814 format_string = "_float";
1815 break;
1816 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001817 const std::string ref_filename = test::TempFilename(
1818 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1819 const std::string out_filename = test::TempFilename(
1820 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001821 const std::string limited_filename = test::TempFilename(
1822 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1823 const size_t logging_limit_bytes = 100000;
1824 // We expect at least this many bytes in the created logfile.
1825 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001826 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001827 ProcessDebugDump(in_filename, ref_filename, format, -1);
1828 ProcessDebugDump(ref_filename, out_filename, format, -1);
1829 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001830
1831 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1832 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001833 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001834 ASSERT_TRUE(ref_file != NULL);
1835 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001836 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001837 std::unique_ptr<uint8_t[]> ref_bytes;
1838 std::unique_ptr<uint8_t[]> out_bytes;
1839 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001840
1841 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1842 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001843 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001844 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001845 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001846 while (ref_size > 0 && out_size > 0) {
1847 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001848 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001849 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001850 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001851 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001852 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001853 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1854 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001855 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001856 }
1857 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001858 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1859 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001860 EXPECT_NE(0, feof(ref_file));
1861 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001862 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001863 ASSERT_EQ(0, fclose(ref_file));
1864 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001865 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001866 remove(ref_filename.c_str());
1867 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001868 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001869}
1870
pbosc7a65692016-05-06 12:50:04 -07001871TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001872 VerifyDebugDumpTest(kIntFormat);
1873}
1874
pbosc7a65692016-05-06 12:50:04 -07001875TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001876 VerifyDebugDumpTest(kFloatFormat);
1877}
1878#endif
1879
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001880// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001881TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001882 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001883 const std::string filename =
1884 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001885 {
1886 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1887 EXPECT_FALSE(aec_dump);
1888 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001889
1890#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1891 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001892 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001893
aleloif4dd1912017-06-15 01:55:38 -07001894 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1895 EXPECT_TRUE(aec_dump);
1896 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001897 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001898 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001899 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001900
1901 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001902 FILE* fid = fopen(filename.c_str(), "r");
1903 ASSERT_TRUE(fid != NULL);
1904
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001905 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001906 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001907 ASSERT_EQ(0, remove(filename.c_str()));
1908#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001909 // Verify the file has NOT been written.
1910 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1911#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1912}
1913
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001914// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001915TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001916 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1917
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001918 const std::string filename =
1919 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001920 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001921 ASSERT_TRUE(fid);
1922
1923#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1924 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001925 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001926
aleloif4dd1912017-06-15 01:55:38 -07001927 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1928 EXPECT_TRUE(aec_dump);
1929 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001930 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001931 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001932 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001933
1934 // Verify the file has been written.
1935 fid = fopen(filename.c_str(), "r");
1936 ASSERT_TRUE(fid != NULL);
1937
1938 // Clean it up.
1939 ASSERT_EQ(0, fclose(fid));
1940 ASSERT_EQ(0, remove(filename.c_str()));
1941#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001942 ASSERT_EQ(0, fclose(fid));
1943#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1944}
1945
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001946TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001947 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001948 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001949
1950 Config config;
1951 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001952 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001953 EnableAllComponents();
1954 EnableAllAPComponents(fapm.get());
1955 for (int i = 0; i < ref_data.test_size(); i++) {
1956 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1957
1958 audioproc::Test* test = ref_data.mutable_test(i);
1959 // TODO(ajm): Restore downmixing test cases.
1960 if (test->num_input_channels() != test->num_output_channels())
1961 continue;
1962
Peter Kasting69558702016-01-12 16:26:35 -08001963 const size_t num_render_channels =
1964 static_cast<size_t>(test->num_reverse_channels());
1965 const size_t num_input_channels =
1966 static_cast<size_t>(test->num_input_channels());
1967 const size_t num_output_channels =
1968 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001969 const size_t samples_per_channel = static_cast<size_t>(
1970 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001971
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001972 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1973 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001974 Init(fapm.get());
1975
1976 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001977 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1978 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001979
1980 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001981 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001982 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1983 ReadFrame(near_file_, frame_, float_cb_.get())) {
1984 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1985
aluebsb0319552016-03-17 20:39:53 -07001986 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001987 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1988 revfloat_cb_->channels(),
1989 samples_per_channel,
1990 test->sample_rate(),
1991 LayoutFromChannels(num_render_channels)));
1992
1993 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1994 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1995 apm_->echo_cancellation()->set_stream_drift_samples(0);
1996 fapm->echo_cancellation()->set_stream_drift_samples(0);
1997 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1998 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1999
2000 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07002001 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002002 output_int16.channels());
2003
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002004 EXPECT_NOERR(fapm->ProcessStream(
2005 float_cb_->channels(),
2006 samples_per_channel,
2007 test->sample_rate(),
2008 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002009 test->sample_rate(),
2010 LayoutFromChannels(num_output_channels),
2011 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002012 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002013 FloatToS16(float_cb_->channels()[j],
2014 samples_per_channel,
2015 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002016 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002017 float snr = ComputeSNR(output_int16.channels()[j],
2018 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002019 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002020
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002021 const float kVarianceThreshold = 20;
2022 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002023
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002024 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002025 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2026 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002027 }
2028 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002029
2030 analog_level = fapm->gain_control()->stream_analog_level();
2031 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2032 fapm->gain_control()->stream_analog_level());
2033 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2034 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002035 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2036 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002037 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002038
2039 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002040 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002041 }
aluebs776593b2016-03-15 14:04:58 -07002042
2043#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2044 const size_t kMaxNumBadChunks = 0;
2045#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2046 // There are a few chunks in the fixed-point profile that give low SNR.
2047 // Listening confirmed the difference is acceptable.
2048 const size_t kMaxNumBadChunks = 60;
2049#endif
2050 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2051
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002052 rewind(far_file_);
2053 rewind(near_file_);
2054 }
2055}
2056
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002057// TODO(andrew): Add a test to process a few frames with different combinations
2058// of enabled components.
2059
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002060TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002061 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002062 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002063
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002064 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002065 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002066 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002067 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002068 for (size_t i = 0; i < arraysize(kChannels); i++) {
2069 for (size_t j = 0; j < arraysize(kChannels); j++) {
2070 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002071 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002072 test->set_num_reverse_channels(kChannels[i]);
2073 test->set_num_input_channels(kChannels[j]);
2074 test->set_num_output_channels(kChannels[j]);
2075 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002076 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002077 }
2078 }
2079 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002080#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2081 // To test the extended filter mode.
2082 audioproc::Test* test = ref_data.add_test();
2083 test->set_num_reverse_channels(2);
2084 test->set_num_input_channels(2);
2085 test->set_num_output_channels(2);
2086 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2087 test->set_use_aec_extended_filter(true);
2088#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002089 }
2090
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002091 for (int i = 0; i < ref_data.test_size(); i++) {
2092 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002093
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002094 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002095 // TODO(ajm): We no longer allow different input and output channels. Skip
2096 // these tests for now, but they should be removed from the set.
2097 if (test->num_input_channels() != test->num_output_channels())
2098 continue;
2099
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002100 Config config;
2101 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002102 config.Set<ExtendedFilter>(
2103 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002104 apm_.reset(AudioProcessing::Create(config));
2105
2106 EnableAllComponents();
2107
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002108 Init(test->sample_rate(),
2109 test->sample_rate(),
2110 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002111 static_cast<size_t>(test->num_input_channels()),
2112 static_cast<size_t>(test->num_output_channels()),
2113 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002114 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002115
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002116 int frame_count = 0;
2117 int has_echo_count = 0;
2118 int has_voice_count = 0;
2119 int is_saturated_count = 0;
2120 int analog_level = 127;
2121 int analog_level_average = 0;
2122 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002123 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002124#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2125 int stats_index = 0;
2126#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002127
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002128 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002129 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002130
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002131 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2132
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002133 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002134 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002135 EXPECT_EQ(apm_->kNoError,
2136 apm_->gain_control()->set_stream_analog_level(analog_level));
2137
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002138 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002139
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002140 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002141 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2142 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002143
2144 max_output_average += MaxAudioFrame(*frame_);
2145
2146 if (apm_->echo_cancellation()->stream_has_echo()) {
2147 has_echo_count++;
2148 }
2149
2150 analog_level = apm_->gain_control()->stream_analog_level();
2151 analog_level_average += analog_level;
2152 if (apm_->gain_control()->stream_is_saturated()) {
2153 is_saturated_count++;
2154 }
2155 if (apm_->voice_detection()->stream_has_voice()) {
2156 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002157 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002158 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002159 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002160 }
2161
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002162 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2163
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002164 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002165 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002166 sizeof(int16_t),
2167 frame_size,
2168 out_file_);
2169 ASSERT_EQ(frame_size, write_count);
2170
2171 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002172 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002173 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002174
2175#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2176 const int kStatsAggregationFrameNum = 100; // 1 second.
2177 if (frame_count % kStatsAggregationFrameNum == 0) {
2178 // Get echo metrics.
2179 EchoCancellation::Metrics echo_metrics;
2180 EXPECT_EQ(apm_->kNoError,
2181 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2182
2183 // Get delay metrics.
2184 int median = 0;
2185 int std = 0;
2186 float fraction_poor_delays = 0;
2187 EXPECT_EQ(apm_->kNoError,
2188 apm_->echo_cancellation()->GetDelayMetrics(
2189 &median, &std, &fraction_poor_delays));
2190
2191 // Get RMS.
2192 int rms_level = apm_->level_estimator()->RMS();
2193 EXPECT_LE(0, rms_level);
2194 EXPECT_GE(127, rms_level);
2195
2196 if (!write_ref_data) {
2197 const audioproc::Test::EchoMetrics& reference =
2198 test->echo_metrics(stats_index);
2199 TestStats(echo_metrics.residual_echo_return_loss,
2200 reference.residual_echo_return_loss());
2201 TestStats(echo_metrics.echo_return_loss,
2202 reference.echo_return_loss());
2203 TestStats(echo_metrics.echo_return_loss_enhancement,
2204 reference.echo_return_loss_enhancement());
2205 TestStats(echo_metrics.a_nlp,
2206 reference.a_nlp());
2207 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2208 reference.divergent_filter_fraction());
2209
2210 const audioproc::Test::DelayMetrics& reference_delay =
2211 test->delay_metrics(stats_index);
2212 EXPECT_EQ(reference_delay.median(), median);
2213 EXPECT_EQ(reference_delay.std(), std);
2214 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2215 fraction_poor_delays);
2216
2217 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2218
2219 ++stats_index;
2220 } else {
2221 audioproc::Test::EchoMetrics* message =
2222 test->add_echo_metrics();
2223 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2224 message->mutable_residual_echo_return_loss());
2225 WriteStatsMessage(echo_metrics.echo_return_loss,
2226 message->mutable_echo_return_loss());
2227 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2228 message->mutable_echo_return_loss_enhancement());
2229 WriteStatsMessage(echo_metrics.a_nlp,
2230 message->mutable_a_nlp());
2231 message->set_divergent_filter_fraction(
2232 echo_metrics.divergent_filter_fraction);
2233
2234 audioproc::Test::DelayMetrics* message_delay =
2235 test->add_delay_metrics();
2236 message_delay->set_median(median);
2237 message_delay->set_std(std);
2238 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2239
2240 test->add_rms_level(rms_level);
2241 }
2242 }
2243#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002244 }
2245 max_output_average /= frame_count;
2246 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002247 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002248
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002249 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002250 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002251 // When running the test on a N7 we get a {2, 6} difference of
2252 // |has_voice_count| and |max_output_average| is up to 18 higher.
2253 // All numbers being consistently higher on N7 compare to ref_data.
2254 // TODO(bjornv): If we start getting more of these offsets on Android we
2255 // should consider a different approach. Either using one slack for all,
2256 // or generate a separate android reference.
2257#if defined(WEBRTC_ANDROID)
2258 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002259 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002260 const int kMaxOutputAverageOffset = 9;
2261 const int kMaxOutputAverageNear = 9;
2262#else
2263 const int kHasVoiceCountOffset = 0;
2264 const int kHasVoiceCountNear = kIntNear;
2265 const int kMaxOutputAverageOffset = 0;
2266 const int kMaxOutputAverageNear = kIntNear;
2267#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002268 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002269 EXPECT_NEAR(test->has_voice_count(),
2270 has_voice_count - kHasVoiceCountOffset,
2271 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002272 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002273
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002274 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002275 EXPECT_NEAR(test->max_output_average(),
2276 max_output_average - kMaxOutputAverageOffset,
2277 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002278#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002279 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002280 EXPECT_NEAR(test->ns_speech_probability_average(),
2281 ns_speech_prob_average,
2282 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002283#endif
2284 } else {
2285 test->set_has_echo_count(has_echo_count);
2286 test->set_has_voice_count(has_voice_count);
2287 test->set_is_saturated_count(is_saturated_count);
2288
2289 test->set_analog_level_average(analog_level_average);
2290 test->set_max_output_average(max_output_average);
2291
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002292#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002293 EXPECT_LE(0.0f, ns_speech_prob_average);
2294 EXPECT_GE(1.0f, ns_speech_prob_average);
2295 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002296#endif
2297 }
2298
2299 rewind(far_file_);
2300 rewind(near_file_);
2301 }
2302
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002303 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002304 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002305 }
2306}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002307
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002308TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2309 struct ChannelFormat {
2310 AudioProcessing::ChannelLayout in_layout;
2311 AudioProcessing::ChannelLayout out_layout;
2312 };
2313 ChannelFormat cf[] = {
2314 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2315 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2316 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2317 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002318
kwiberg62eaacf2016-02-17 06:39:05 -08002319 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002320 // Enable one component just to ensure some processing takes place.
2321 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002322 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002323 const int in_rate = 44100;
2324 const int out_rate = 48000;
2325 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2326 TotalChannelsFromLayout(cf[i].in_layout));
2327 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2328 ChannelsFromLayout(cf[i].out_layout));
2329
2330 // Run over a few chunks.
2331 for (int j = 0; j < 10; ++j) {
2332 EXPECT_NOERR(ap->ProcessStream(
2333 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002334 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002335 in_rate,
2336 cf[i].in_layout,
2337 out_rate,
2338 cf[i].out_layout,
2339 out_cb.channels()));
2340 }
2341 }
2342}
2343
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002344// Compares the reference and test arrays over a region around the expected
2345// delay. Finds the highest SNR in that region and adds the variance and squared
2346// error results to the supplied accumulators.
2347void UpdateBestSNR(const float* ref,
2348 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002349 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002350 int expected_delay,
2351 double* variance_acc,
2352 double* sq_error_acc) {
2353 double best_snr = std::numeric_limits<double>::min();
2354 double best_variance = 0;
2355 double best_sq_error = 0;
2356 // Search over a region of eight samples around the expected delay.
2357 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2358 ++delay) {
2359 double sq_error = 0;
2360 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002361 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002362 double error = test[i + delay] - ref[i];
2363 sq_error += error * error;
2364 variance += ref[i] * ref[i];
2365 }
2366
2367 if (sq_error == 0) {
2368 *variance_acc += variance;
2369 return;
2370 }
2371 double snr = variance / sq_error;
2372 if (snr > best_snr) {
2373 best_snr = snr;
2374 best_variance = variance;
2375 best_sq_error = sq_error;
2376 }
2377 }
2378
2379 *variance_acc += best_variance;
2380 *sq_error_acc += best_sq_error;
2381}
2382
2383// Used to test a multitude of sample rate and channel combinations. It works
2384// by first producing a set of reference files (in SetUpTestCase) that are
2385// assumed to be correct, as the used parameters are verified by other tests
2386// in this collection. Primarily the reference files are all produced at
2387// "native" rates which do not involve any resampling.
2388
2389// Each test pass produces an output file with a particular format. The output
2390// is matched against the reference file closest to its internal processing
2391// format. If necessary the output is resampled back to its process format.
2392// Due to the resampling distortion, we don't expect identical results, but
2393// enforce SNR thresholds which vary depending on the format. 0 is a special
2394// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002395typedef std::tr1::tuple<int, int, int, int, double, double>
2396 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002397class AudioProcessingTest
2398 : public testing::TestWithParam<AudioProcessingTestData> {
2399 public:
2400 AudioProcessingTest()
2401 : input_rate_(std::tr1::get<0>(GetParam())),
2402 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002403 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2404 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2405 expected_snr_(std::tr1::get<4>(GetParam())),
2406 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002407
2408 virtual ~AudioProcessingTest() {}
2409
2410 static void SetUpTestCase() {
2411 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002412 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002413 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002414 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2415 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2416 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002417 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002418 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2419 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2420 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002421 }
2422 }
2423 }
2424 }
2425
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002426 static void TearDownTestCase() {
2427 ClearTempFiles();
2428 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002429
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002430 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002431 // to a file specified with |output_file_prefix|. Both forward and reverse
2432 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002433 static void ProcessFormat(int input_rate,
2434 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002435 int reverse_input_rate,
2436 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002437 size_t num_input_channels,
2438 size_t num_output_channels,
2439 size_t num_reverse_input_channels,
2440 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002441 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002442 Config config;
2443 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002444 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002445 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002446
ekmeyerson60d9b332015-08-14 10:35:55 -07002447 ProcessingConfig processing_config = {
2448 {{input_rate, num_input_channels},
2449 {output_rate, num_output_channels},
2450 {reverse_input_rate, num_reverse_input_channels},
2451 {reverse_output_rate, num_reverse_output_channels}}};
2452 ap->Initialize(processing_config);
2453
2454 FILE* far_file =
2455 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002456 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002457 FILE* out_file =
2458 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2459 reverse_input_rate, reverse_output_rate,
2460 num_input_channels, num_output_channels,
2461 num_reverse_input_channels,
2462 num_reverse_output_channels, kForward).c_str(),
2463 "wb");
2464 FILE* rev_out_file =
2465 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2466 reverse_input_rate, reverse_output_rate,
2467 num_input_channels, num_output_channels,
2468 num_reverse_input_channels,
2469 num_reverse_output_channels, kReverse).c_str(),
2470 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002471 ASSERT_TRUE(far_file != NULL);
2472 ASSERT_TRUE(near_file != NULL);
2473 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002475
2476 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2477 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002478 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2479 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002480 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2481 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002482 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2483 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002484
2485 // Temporary buffers.
2486 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002487 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2488 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002489 std::unique_ptr<float[]> float_data(new float[max_length]);
2490 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002491
2492 int analog_level = 127;
2493 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2494 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002495 EXPECT_NOERR(ap->ProcessReverseStream(
2496 rev_cb.channels(), processing_config.reverse_input_stream(),
2497 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002498
2499 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2500 ap->echo_cancellation()->set_stream_drift_samples(0);
2501 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2502
2503 EXPECT_NOERR(ap->ProcessStream(
2504 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002505 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002506 input_rate,
2507 LayoutFromChannels(num_input_channels),
2508 output_rate,
2509 LayoutFromChannels(num_output_channels),
2510 out_cb.channels()));
2511
ekmeyerson60d9b332015-08-14 10:35:55 -07002512 // Dump forward output to file.
2513 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002514 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002515 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002516
pkasting25702cb2016-01-08 13:50:27 -08002517 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002518 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002519 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002520
ekmeyerson60d9b332015-08-14 10:35:55 -07002521 // Dump reverse output to file.
2522 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2523 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002524 size_t rev_out_length =
2525 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002526
pkasting25702cb2016-01-08 13:50:27 -08002527 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002528 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2529 rev_out_file));
2530
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002531 analog_level = ap->gain_control()->stream_analog_level();
2532 }
2533 fclose(far_file);
2534 fclose(near_file);
2535 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002536 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002537 }
2538
2539 protected:
2540 int input_rate_;
2541 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002542 int reverse_input_rate_;
2543 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002544 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002545 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002546};
2547
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002548TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002549 struct ChannelFormat {
2550 int num_input;
2551 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002552 int num_reverse_input;
2553 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002554 };
2555 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002556 {1, 1, 1, 1},
2557 {1, 1, 2, 1},
2558 {2, 1, 1, 1},
2559 {2, 1, 2, 1},
2560 {2, 2, 1, 1},
2561 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002562 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002563
pkasting25702cb2016-01-08 13:50:27 -08002564 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002565 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2566 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2567 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002568
ekmeyerson60d9b332015-08-14 10:35:55 -07002569 // Verify output for both directions.
2570 std::vector<StreamDirection> stream_directions;
2571 stream_directions.push_back(kForward);
2572 stream_directions.push_back(kReverse);
2573 for (StreamDirection file_direction : stream_directions) {
2574 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2575 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2576 const int out_num =
2577 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2578 const double expected_snr =
2579 file_direction ? expected_reverse_snr_ : expected_snr_;
2580
2581 const int min_ref_rate = std::min(in_rate, out_rate);
2582 int ref_rate;
2583
2584 if (min_ref_rate > 32000) {
2585 ref_rate = 48000;
2586 } else if (min_ref_rate > 16000) {
2587 ref_rate = 32000;
2588 } else if (min_ref_rate > 8000) {
2589 ref_rate = 16000;
2590 } else {
2591 ref_rate = 8000;
2592 }
aluebs776593b2016-03-15 14:04:58 -07002593#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002594 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002595 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002596 }
2597#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002598 FILE* out_file = fopen(
2599 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2600 reverse_output_rate_, cf[i].num_input,
2601 cf[i].num_output, cf[i].num_reverse_input,
2602 cf[i].num_reverse_output, file_direction).c_str(),
2603 "rb");
2604 // The reference files always have matching input and output channels.
2605 FILE* ref_file = fopen(
2606 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2607 cf[i].num_output, cf[i].num_output,
2608 cf[i].num_reverse_output, cf[i].num_reverse_output,
2609 file_direction).c_str(),
2610 "rb");
2611 ASSERT_TRUE(out_file != NULL);
2612 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002613
pkasting25702cb2016-01-08 13:50:27 -08002614 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2615 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002616 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002617 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002618 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002619 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002620 // Data from the resampled output, in case the reference and output rates
2621 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002622 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002623
ekmeyerson60d9b332015-08-14 10:35:55 -07002624 PushResampler<float> resampler;
2625 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002626
ekmeyerson60d9b332015-08-14 10:35:55 -07002627 // Compute the resampling delay of the output relative to the reference,
2628 // to find the region over which we should search for the best SNR.
2629 float expected_delay_sec = 0;
2630 if (in_rate != ref_rate) {
2631 // Input resampling delay.
2632 expected_delay_sec +=
2633 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2634 }
2635 if (out_rate != ref_rate) {
2636 // Output resampling delay.
2637 expected_delay_sec +=
2638 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2639 // Delay of converting the output back to its processing rate for
2640 // testing.
2641 expected_delay_sec +=
2642 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2643 }
2644 int expected_delay =
2645 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002646
ekmeyerson60d9b332015-08-14 10:35:55 -07002647 double variance = 0;
2648 double sq_error = 0;
2649 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2650 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2651 float* out_ptr = out_data.get();
2652 if (out_rate != ref_rate) {
2653 // Resample the output back to its internal processing rate if
2654 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002655 ASSERT_EQ(ref_length,
2656 static_cast<size_t>(resampler.Resample(
2657 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002658 out_ptr = cmp_data.get();
2659 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002660
ekmeyerson60d9b332015-08-14 10:35:55 -07002661 // Update the |sq_error| and |variance| accumulators with the highest
2662 // SNR of reference vs output.
2663 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2664 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002665 }
2666
ekmeyerson60d9b332015-08-14 10:35:55 -07002667 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2668 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2669 << cf[i].num_input << ", " << cf[i].num_output << ", "
2670 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2671 << ", " << file_direction << "): ";
2672 if (sq_error > 0) {
2673 double snr = 10 * log10(variance / sq_error);
2674 EXPECT_GE(snr, expected_snr);
2675 EXPECT_NE(0, expected_snr);
2676 std::cout << "SNR=" << snr << " dB" << std::endl;
2677 } else {
aluebs776593b2016-03-15 14:04:58 -07002678 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002679 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002680
ekmeyerson60d9b332015-08-14 10:35:55 -07002681 fclose(out_file);
2682 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002683 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002684 }
2685}
2686
2687#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2688INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002689 CommonFormats,
2690 AudioProcessingTest,
2691 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002692 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2693 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002694 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2695 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2696 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2697 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2698 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2699 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2700 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2701 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2702 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002703
ekmeyerson60d9b332015-08-14 10:35:55 -07002704 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2705 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2706 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2707 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2708 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2709 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2710 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2711 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2712 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2713 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2714 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2715 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002716
ekmeyerson60d9b332015-08-14 10:35:55 -07002717 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2718 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2719 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2720 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2721 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2722 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2723 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2724 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2725 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2726 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2727 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2728 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002729
ekmeyerson60d9b332015-08-14 10:35:55 -07002730 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2731 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2732 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2733 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2734 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2735 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2736 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2737 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2738 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2739 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002740 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002741 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002742
2743#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2744INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002745 CommonFormats,
2746 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002747 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2748 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2749 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2750 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2751 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2752 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002753 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2754 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2755 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2756 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2757 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2758 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002759
aluebs776593b2016-03-15 14:04:58 -07002760 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2761 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2762 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002763 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2764 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2765 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2766 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2767 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2768 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2769 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2770 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2771 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002772
aluebs776593b2016-03-15 14:04:58 -07002773 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2774 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2775 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2776 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2777 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2778 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2779 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2780 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2781 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002782 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2783 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2784 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002785
ekmeyerson60d9b332015-08-14 10:35:55 -07002786 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2787 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2788 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2789 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2790 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2791 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2792 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2793 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2794 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2795 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002796 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002797 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002798#endif
2799
niklase@google.com470e71d2011-07-07 08:21:25 +00002800} // namespace
peahc19f3122016-10-07 14:54:10 -07002801
2802TEST(ApmConfiguration, DefaultBehavior) {
2803 // Verify that the level controller is default off, it can be activated using
2804 // the config, and that the default initial level is maintained after the
2805 // config has been applied.
2806 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002807 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002808 AudioProcessing::Config config;
2809 EXPECT_FALSE(apm->config_.level_controller.enabled);
2810 // TODO(peah): Add test for the existence of the level controller object once
2811 // that is created only when that is specified in the config.
2812 // TODO(peah): Remove the testing for
2813 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2814 // is instead used to activate the level controller.
2815 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2816 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2817 apm->config_.level_controller.initial_peak_level_dbfs,
2818 std::numeric_limits<float>::epsilon());
2819 config.level_controller.enabled = true;
2820 apm->ApplyConfig(config);
2821 EXPECT_TRUE(apm->config_.level_controller.enabled);
2822 // TODO(peah): Add test for the existence of the level controller object once
2823 // that is created only when the that is specified in the config.
2824 // TODO(peah): Remove the testing for
2825 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2826 // is instead used to activate the level controller.
2827 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2828 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2829 apm->config_.level_controller.initial_peak_level_dbfs,
2830 std::numeric_limits<float>::epsilon());
2831}
2832
2833TEST(ApmConfiguration, ValidConfigBehavior) {
2834 // Verify that the initial level can be specified and is retained after the
2835 // config has been applied.
2836 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002837 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002838 AudioProcessing::Config config;
2839 config.level_controller.initial_peak_level_dbfs = -50.f;
2840 apm->ApplyConfig(config);
2841 EXPECT_FALSE(apm->config_.level_controller.enabled);
2842 // TODO(peah): Add test for the existence of the level controller object once
2843 // that is created only when the that is specified in the config.
2844 // TODO(peah): Remove the testing for
2845 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2846 // is instead used to activate the level controller.
2847 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2848 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2849 std::numeric_limits<float>::epsilon());
2850}
2851
2852TEST(ApmConfiguration, InValidConfigBehavior) {
2853 // Verify that the config is properly reset when nonproper values are applied
2854 // for the initial level.
2855
2856 // Verify that the config is properly reset when the specified initial peak
2857 // level is too low.
2858 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002859 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002860 AudioProcessing::Config config;
2861 config.level_controller.enabled = true;
2862 config.level_controller.initial_peak_level_dbfs = -101.f;
2863 apm->ApplyConfig(config);
2864 EXPECT_FALSE(apm->config_.level_controller.enabled);
2865 // TODO(peah): Add test for the existence of the level controller object once
2866 // that is created only when the that is specified in the config.
2867 // TODO(peah): Remove the testing for
2868 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2869 // is instead used to activate the level controller.
2870 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2871 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2872 apm->config_.level_controller.initial_peak_level_dbfs,
2873 std::numeric_limits<float>::epsilon());
2874
2875 // Verify that the config is properly reset when the specified initial peak
2876 // level is too high.
peaha9cc40b2017-06-29 08:32:09 -07002877 apm.reset(new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002878 config = AudioProcessing::Config();
2879 config.level_controller.enabled = true;
2880 config.level_controller.initial_peak_level_dbfs = 1.f;
2881 apm->ApplyConfig(config);
2882 EXPECT_FALSE(apm->config_.level_controller.enabled);
2883 // TODO(peah): Add test for the existence of the level controller object once
2884 // that is created only when that is specified in the config.
2885 // TODO(peah): Remove the testing for
2886 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2887 // is instead used to activate the level controller.
2888 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2889 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2890 apm->config_.level_controller.initial_peak_level_dbfs,
2891 std::numeric_limits<float>::epsilon());
2892}
2893
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002894} // namespace webrtc