blob: a5b293eb1820b569ee4f88fb191696209586f7d0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000011#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000012#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080013
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000014#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
pkasting25702cb2016-01-08 13:50:27 -080019#include "webrtc/base/arraysize.h"
andrew@webrtc.org27c69802014-02-18 20:24:56 +000020#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000021#include "webrtc/common_audio/resampler/include/push_resampler.h"
22#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000023#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000024#include "webrtc/modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000025#include "webrtc/modules/audio_processing/common.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000026#include "webrtc/modules/audio_processing/include/audio_processing.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070027#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000028#include "webrtc/modules/audio_processing/test/test_utils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010030#include "webrtc/system_wrappers/include/event_wrapper.h"
31#include "webrtc/system_wrappers/include/trace.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000032#include "webrtc/test/testsupport/fileutils.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000033#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
pbos@webrtc.org8c34cee2013-05-28 09:24:03 +000034#include "gtest/gtest.h"
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000035#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000036#else
pbos@webrtc.org8c34cee2013-05-28 09:24:03 +000037#include "testing/gtest/include/gtest/gtest.h"
kjellander78ddd732016-02-09 08:13:06 -080038#include "webrtc/modules/audio_processing/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000039#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000040
andrew@webrtc.org27c69802014-02-18 20:24:56 +000041namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000042namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000043
ekmeyerson60d9b332015-08-14 10:35:55 -070044// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
45// applicable.
46
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000047// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000048// re-implemented; see comment at the bottom of this file. For now, the user has
49// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000050// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000051// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000052// be set to true with the command-line switch --write_ref_data.
53bool write_ref_data = false;
pkasting25702cb2016-01-08 13:50:27 -080054const google::protobuf::int32 kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070055const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000056
aluebseb3603b2016-04-20 15:27:58 -070057#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
58// Android doesn't support 48kHz.
59const int kProcessSampleRates[] = {8000, 16000, 32000};
60#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070061const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070062#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000063
ekmeyerson60d9b332015-08-14 10:35:55 -070064enum StreamDirection { kForward = 0, kReverse };
65
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000066void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000067 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000068 cb->num_channels());
69 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000070 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000071 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000072 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080073 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000074 S16ToFloat(cb_int.channels()[i],
75 cb->num_frames(),
76 cb->channels()[i]);
77 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000078}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000079
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
81 ConvertToFloat(frame.data_, cb);
82}
83
andrew@webrtc.org103657b2014-04-24 18:28:56 +000084// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080085size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000086 switch (layout) {
87 case AudioProcessing::kMono:
88 return 1;
89 case AudioProcessing::kMonoAndKeyboard:
90 case AudioProcessing::kStereo:
91 return 2;
92 case AudioProcessing::kStereoAndKeyboard:
93 return 3;
94 }
95 assert(false);
pkasting25702cb2016-01-08 13:50:27 -080096 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097}
98
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000099int TruncateToMultipleOf10(int value) {
100 return (value / 10) * 10;
101}
102
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000103void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800104 size_t samples_per_channel) {
105 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000106 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000107}
108
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000109void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800110 size_t samples_per_channel) {
111 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000112 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
113}
114
pkasting25702cb2016-01-08 13:50:27 -0800115void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
116 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000117 stereo[i * 2 + 1] = stereo[i * 2];
118 }
119}
120
pkasting25702cb2016-01-08 13:50:27 -0800121void VerifyChannelsAreEqual(int16_t* stereo, size_t samples_per_channel) {
122 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000123 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
124 }
125}
126
127void SetFrameTo(AudioFrame* frame, int16_t value) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700128 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
129 ++i) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000130 frame->data_[i] = value;
131 }
132}
133
134void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800135 ASSERT_EQ(2u, frame->num_channels_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000137 frame->data_[i] = left;
138 frame->data_[i + 1] = right;
139 }
140}
141
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000142void ScaleFrame(AudioFrame* frame, float scale) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
144 ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000145 frame->data_[i] = FloatS16ToS16(frame->data_[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000146 }
147}
148
andrew@webrtc.org81865342012-10-27 00:28:27 +0000149bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000150 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000151 return false;
152 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000153 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000154 return false;
155 }
156 if (memcmp(frame1.data_, frame2.data_,
157 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000158 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000159 return false;
160 }
161 return true;
162}
163
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000164void EnableAllAPComponents(AudioProcessing* ap) {
165#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
166 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
167
168 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
169 EXPECT_NOERR(ap->gain_control()->Enable(true));
170#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
171 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
172 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
173 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
174 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
175
176 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
177 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
178 EXPECT_NOERR(ap->gain_control()->Enable(true));
179#endif
180
181 EXPECT_NOERR(ap->high_pass_filter()->Enable(true));
182 EXPECT_NOERR(ap->level_estimator()->Enable(true));
183 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
184
185 EXPECT_NOERR(ap->voice_detection()->Enable(true));
186}
187
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000188// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000189template <class T>
190T AbsValue(T a) {
191 return a > 0 ? a: -a;
192}
193
194int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800195 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000196 int16_t max_data = AbsValue(frame.data_[0]);
pkasting25702cb2016-01-08 13:50:27 -0800197 for (size_t i = 1; i < length; i++) {
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000198 max_data = std::max(max_data, AbsValue(frame.data_[i]));
199 }
200
201 return max_data;
202}
203
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000204#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000205void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000206 const audioproc::Test::Statistic& reference) {
minyue7b19b082016-03-02 06:56:46 -0800207 EXPECT_NEAR(reference.instant(), test.instant, 2);
208 EXPECT_NEAR(reference.average(), test.average, 2);
209 EXPECT_NEAR(reference.maximum(), test.maximum, 3);
210 EXPECT_NEAR(reference.minimum(), test.minimum, 2);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000211}
212
213void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000214 audioproc::Test::Statistic* msg) {
215 msg->set_instant(output.instant);
216 msg->set_average(output.average);
217 msg->set_maximum(output.maximum);
218 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000219}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000220#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000221
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000222void OpenFileAndWriteMessage(const std::string filename,
223 const ::google::protobuf::MessageLite& msg) {
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +0000224#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000225 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000226 ASSERT_TRUE(file != NULL);
227
228 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800230 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000231 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000235 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236 fclose(file);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +0000237#else
238 std::cout << "Warning: Writing new reference is only allowed on Linux!"
239 << std::endl;
240#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241}
242
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000243std::string ResourceFilePath(std::string name, int sample_rate_hz) {
244 std::ostringstream ss;
245 // Resource files are all stereo.
246 ss << name << sample_rate_hz / 1000 << "_stereo";
247 return test::ResourcePath(ss.str(), "pcm");
248}
249
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000250// Temporary filenames unique to this process. Used to be able to run these
251// tests in parallel as each process needs to be running in isolation they can't
252// have competing filenames.
253std::map<std::string, std::string> temp_filenames;
254
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000255std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000256 int input_rate,
257 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700258 int reverse_input_rate,
259 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800260 size_t num_input_channels,
261 size_t num_output_channels,
262 size_t num_reverse_input_channels,
263 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700264 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000265 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700266 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
267 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000268 if (num_output_channels == 1) {
269 ss << "mono";
270 } else if (num_output_channels == 2) {
271 ss << "stereo";
272 } else {
273 assert(false);
274 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700275 ss << output_rate / 1000;
276 if (num_reverse_output_channels == 1) {
277 ss << "_rmono";
278 } else if (num_reverse_output_channels == 2) {
279 ss << "_rstereo";
280 } else {
281 assert(false);
282 }
283 ss << reverse_output_rate / 1000;
284 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000285
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000286 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700287 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000288 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
289 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000290}
291
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000292void ClearTempFiles() {
293 for (auto& kv : temp_filenames)
294 remove(kv.second.c_str());
295}
296
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000297void OpenFileAndReadMessage(const std::string filename,
298 ::google::protobuf::MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000299 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000300 ASSERT_TRUE(file != NULL);
301 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000302 fclose(file);
303}
304
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000305// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
306// stereo) file, converts to deinterleaved float (optionally downmixing) and
307// returns the result in |cb|. Returns false if the file ended (or on error) and
308// true otherwise.
309//
310// |int_data| and |float_data| are just temporary space that must be
311// sufficiently large to hold the 10 ms chunk.
312bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
313 ChannelBuffer<float>* cb) {
314 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000315 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000316 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
317 if (read_count != frame_size) {
318 // Check that the file really ended.
319 assert(feof(file));
320 return false; // This is expected.
321 }
322
323 S16ToFloat(int_data, frame_size, float_data);
324 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000325 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000326 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000327 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000328 cb->channels());
329 }
330
331 return true;
332}
333
niklase@google.com470e71d2011-07-07 08:21:25 +0000334class ApmTest : public ::testing::Test {
335 protected:
336 ApmTest();
337 virtual void SetUp();
338 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000339
340 static void SetUpTestCase() {
341 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000342 }
343
344 static void TearDownTestCase() {
345 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000346 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000347 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000348
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000349 // Used to select between int and float interface tests.
350 enum Format {
351 kIntFormat,
352 kFloatFormat
353 };
354
355 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000356 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000357 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800358 size_t num_input_channels,
359 size_t num_output_channels,
360 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000361 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000362 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000363 void EnableAllComponents();
364 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000365 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000366 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000367 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
368 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000369 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000370 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
371 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700372 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800373 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700374 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800375 void TestChangingForwardChannels(size_t num_in_channels,
376 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700377 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800378 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700379 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000380 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
381 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000382 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000383 int ProcessStreamChooser(Format format);
384 int AnalyzeReverseStreamChooser(Format format);
385 void ProcessDebugDump(const std::string& in_filename,
386 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800387 Format format,
388 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000389 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000390
391 const std::string output_path_;
392 const std::string ref_path_;
393 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800394 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000395 AudioFrame* frame_;
396 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800397 std::unique_ptr<ChannelBuffer<float> > float_cb_;
398 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000399 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800400 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000401 FILE* far_file_;
402 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000404};
405
406ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000407 : output_path_(test::OutputPath()),
408 ref_path_(test::ProjectRootPath() + "data/audio_processing/"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000409#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000410 ref_filename_(ref_path_ + "output_data_fixed.pb"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000411#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000412#if defined(WEBRTC_MAC)
413 // A different file for Mac is needed because on this platform the AEC
414 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
415 ref_filename_(ref_path_ + "output_data_mac.pb"),
416#else
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000417 ref_filename_(ref_path_ + "output_data_float.pb"),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000418#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000419#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000420 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000421 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000422 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000423 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000424 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000425 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000426 out_file_(NULL) {
427 Config config;
428 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
429 apm_.reset(AudioProcessing::Create(config));
430}
niklase@google.com470e71d2011-07-07 08:21:25 +0000431
432void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000433 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
435 frame_ = new AudioFrame();
436 revframe_ = new AudioFrame();
437
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000438 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
441void ApmTest::TearDown() {
442 if (frame_) {
443 delete frame_;
444 }
445 frame_ = NULL;
446
447 if (revframe_) {
448 delete revframe_;
449 }
450 revframe_ = NULL;
451
452 if (far_file_) {
453 ASSERT_EQ(0, fclose(far_file_));
454 }
455 far_file_ = NULL;
456
457 if (near_file_) {
458 ASSERT_EQ(0, fclose(near_file_));
459 }
460 near_file_ = NULL;
461
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000462 if (out_file_) {
463 ASSERT_EQ(0, fclose(out_file_));
464 }
465 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000466}
467
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000468void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000469 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700470 ap->Initialize(
471 {{{frame_->sample_rate_hz_, frame_->num_channels_},
472 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700473 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700474 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000475}
476
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000477void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000478 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000479 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800480 size_t num_input_channels,
481 size_t num_output_channels,
482 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000483 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000486 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000487
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000488 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
489 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000490 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000491
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000492 if (far_file_) {
493 ASSERT_EQ(0, fclose(far_file_));
494 }
495 std::string filename = ResourceFilePath("far", sample_rate_hz);
496 far_file_ = fopen(filename.c_str(), "rb");
497 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
498 filename << "\n";
499
500 if (near_file_) {
501 ASSERT_EQ(0, fclose(near_file_));
502 }
503 filename = ResourceFilePath("near", sample_rate_hz);
504 near_file_ = fopen(filename.c_str(), "rb");
505 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
506 filename << "\n";
507
508 if (open_output_file) {
509 if (out_file_) {
510 ASSERT_EQ(0, fclose(out_file_));
511 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700512 filename = OutputFilePath(
513 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
514 reverse_sample_rate_hz, num_input_channels, num_output_channels,
515 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000516 out_file_ = fopen(filename.c_str(), "wb");
517 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
518 filename << "\n";
519 }
520}
521
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000522void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000523 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000524}
525
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000526bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
527 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000528 // The files always contain stereo audio.
529 size_t frame_size = frame->samples_per_channel_ * 2;
530 size_t read_count = fread(frame->data_,
531 sizeof(int16_t),
532 frame_size,
533 file);
534 if (read_count != frame_size) {
535 // Check that the file really ended.
536 EXPECT_NE(0, feof(file));
537 return false; // This is expected.
538 }
539
540 if (frame->num_channels_ == 1) {
541 MixStereoToMono(frame->data_, frame->data_,
542 frame->samples_per_channel_);
543 }
544
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000545 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000546 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000547 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000548 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000549}
550
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000551bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
552 return ReadFrame(file, frame, NULL);
553}
554
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000555// If the end of the file has been reached, rewind it and attempt to read the
556// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
558 ChannelBuffer<float>* cb) {
559 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000560 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000562 }
563}
564
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000565void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
566 ReadFrameWithRewind(file, frame, NULL);
567}
568
andrew@webrtc.org81865342012-10-27 00:28:27 +0000569void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
570 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000571 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000572 EXPECT_EQ(apm_->kNoError,
573 apm_->gain_control()->set_stream_analog_level(127));
574 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000575}
576
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000577int ApmTest::ProcessStreamChooser(Format format) {
578 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000579 return apm_->ProcessStream(frame_);
580 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581 return apm_->ProcessStream(float_cb_->channels(),
582 frame_->samples_per_channel_,
583 frame_->sample_rate_hz_,
584 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000585 output_sample_rate_hz_,
586 LayoutFromChannels(num_output_channels_),
587 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000588}
589
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000590int ApmTest::AnalyzeReverseStreamChooser(Format format) {
591 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700592 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000594 return apm_->AnalyzeReverseStream(
595 revfloat_cb_->channels(),
596 revframe_->samples_per_channel_,
597 revframe_->sample_rate_hz_,
598 LayoutFromChannels(revframe_->num_channels_));
599}
600
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000601void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
602 int delay_min, int delay_max) {
603 // The |revframe_| and |frame_| should include the proper frame information,
604 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000605 AudioFrame tmp_frame;
606 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000607 bool causal = true;
608
609 tmp_frame.CopyFrom(*revframe_);
610 SetFrameTo(&tmp_frame, 0);
611
612 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
613 // Initialize the |frame_queue| with empty frames.
614 int frame_delay = delay_ms / 10;
615 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000616 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000617 frame->CopyFrom(tmp_frame);
618 frame_queue.push(frame);
619 frame_delay++;
620 causal = false;
621 }
622 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000623 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000624 frame->CopyFrom(tmp_frame);
625 frame_queue.push(frame);
626 frame_delay--;
627 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000628 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
629 // need enough frames with audio to have reliable estimates, but as few as
630 // possible to keep processing time down. 4.5 seconds seemed to be a good
631 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000632 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000633 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000634 frame->CopyFrom(tmp_frame);
635 // Use the near end recording, since that has more speech in it.
636 ASSERT_TRUE(ReadFrame(near_file_, frame));
637 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000638 AudioFrame* reverse_frame = frame;
639 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000640 if (!causal) {
641 reverse_frame = frame_queue.front();
642 // When we call ProcessStream() the frame is modified, so we can't use the
643 // pointer directly when things are non-causal. Use an intermediate frame
644 // and copy the data.
645 process_frame = &tmp_frame;
646 process_frame->CopyFrom(*frame);
647 }
aluebsb0319552016-03-17 20:39:53 -0700648 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000649 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
650 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
651 frame = frame_queue.front();
652 frame_queue.pop();
653 delete frame;
654
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000655 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 int median;
657 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000658 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000659 // Discard the first delay metrics to avoid convergence effects.
660 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000661 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
662 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 }
664 }
665
666 rewind(near_file_);
667 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000668 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000669 frame_queue.pop();
670 delete frame;
671 }
672 // Calculate expected delay estimate and acceptable regions. Further,
673 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700674 const size_t samples_per_ms =
675 std::min(static_cast<size_t>(16), frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000676 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
677 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 int expected_median_high = std::min(
679 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
680 delay_min),
681 delay_max);
682 int expected_median_low = std::min(
683 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
684 delay_min),
685 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000686 // Verify delay metrics.
687 int median;
688 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000689 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000690 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000691 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
692 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000693 EXPECT_GE(expected_median_high, median);
694 EXPECT_LE(expected_median_low, median);
695}
696
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000697void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000698 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000700
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000701 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000703 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000704 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000705
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000706 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000707 EXPECT_EQ(apm_->kNoError,
708 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000709 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000710 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000711 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000712
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713 // Other stream parameters set correctly.
714 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 EXPECT_EQ(apm_->kNoError,
716 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000718 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000720 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000721 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
722 EXPECT_EQ(apm_->kNoError,
723 apm_->echo_cancellation()->enable_drift_compensation(false));
724
725 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000726 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000727 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000728 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000729 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000730
731 // Resets after successful ProcessStream().
732 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000733 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000734 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000736
737 // Other stream parameters set correctly.
738 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
739 EXPECT_EQ(apm_->kNoError,
740 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000741 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000742 EXPECT_EQ(apm_->kNoError,
743 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000744 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000745 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000746 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
747
748 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000749 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000750 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000751
752 // Resets after successful ProcessStream().
753 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000754 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000757 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000758
759 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000760 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
761 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
762 EXPECT_EQ(apm_->kNoError,
763 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000764 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000765 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000767 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000768 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000769 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000770 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000771 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000772
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000773 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000775 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 EXPECT_EQ(apm_->kNoError,
777 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000779}
780
781TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000782 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000783}
784
785TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000786 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000789TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
790 EXPECT_EQ(0, apm_->delay_offset_ms());
791 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
792 EXPECT_EQ(50, apm_->stream_delay_ms());
793}
794
795TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
796 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000797 apm_->set_delay_offset_ms(100);
798 EXPECT_EQ(100, apm_->delay_offset_ms());
799 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000800 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000801 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
802 EXPECT_EQ(200, apm_->stream_delay_ms());
803
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000804 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000805 apm_->set_delay_offset_ms(-50);
806 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000807 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
808 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000809 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
810 EXPECT_EQ(50, apm_->stream_delay_ms());
811}
812
Michael Graczyk86c6d332015-07-23 11:41:39 -0700813void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800814 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700815 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000816 frame_->num_channels_ = num_channels;
817 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700818 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000819}
820
Michael Graczyk86c6d332015-07-23 11:41:39 -0700821void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800822 size_t num_in_channels,
823 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700824 AudioProcessing::Error expected_return) {
825 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
826 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
827
828 EXPECT_EQ(expected_return,
829 apm_->ProcessStream(float_cb_->channels(), input_stream,
830 output_stream, float_cb_->channels()));
831}
832
833void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800834 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700835 AudioProcessing::Error expected_return) {
836 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700837 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
838 {output_sample_rate_hz_, apm_->num_output_channels()},
839 {frame_->sample_rate_hz_, num_rev_channels},
840 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700841
ekmeyerson60d9b332015-08-14 10:35:55 -0700842 EXPECT_EQ(
843 expected_return,
844 apm_->ProcessReverseStream(
845 float_cb_->channels(), processing_config.reverse_input_stream(),
846 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700847}
848
849TEST_F(ApmTest, ChannelsInt16Interface) {
850 // Testing number of invalid and valid channels.
851 Init(16000, 16000, 16000, 4, 4, 4, false);
852
853 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
854
Peter Kasting69558702016-01-12 16:26:35 -0800855 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700856 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000857 EXPECT_EQ(i, apm_->num_input_channels());
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000858 // We always force the number of reverse channels used for processing to 1.
Peter Kasting69558702016-01-12 16:26:35 -0800859 EXPECT_EQ(1u, apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000860 }
861}
862
Michael Graczyk86c6d332015-07-23 11:41:39 -0700863TEST_F(ApmTest, Channels) {
864 // Testing number of invalid and valid channels.
865 Init(16000, 16000, 16000, 4, 4, 4, false);
866
867 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
868 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
869
Peter Kasting69558702016-01-12 16:26:35 -0800870 for (size_t i = 1; i < 4; ++i) {
871 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700872 // Output channels much be one or match input channels.
873 if (j == 1 || i == j) {
874 TestChangingForwardChannels(i, j, kNoErr);
875 TestChangingReverseChannels(i, kNoErr);
876
877 EXPECT_EQ(i, apm_->num_input_channels());
878 EXPECT_EQ(j, apm_->num_output_channels());
879 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800880 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700881 } else {
882 TestChangingForwardChannels(i, j,
883 AudioProcessing::kBadNumberChannelsError);
884 }
885 }
886 }
887}
888
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000889TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000890 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000891 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000892 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000893 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700894 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800895 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000896 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000897 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 }
899}
900
niklase@google.com470e71d2011-07-07 08:21:25 +0000901TEST_F(ApmTest, EchoCancellation) {
902 EXPECT_EQ(apm_->kNoError,
903 apm_->echo_cancellation()->enable_drift_compensation(true));
904 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
905 EXPECT_EQ(apm_->kNoError,
906 apm_->echo_cancellation()->enable_drift_compensation(false));
907 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
908
niklase@google.com470e71d2011-07-07 08:21:25 +0000909 EchoCancellation::SuppressionLevel level[] = {
910 EchoCancellation::kLowSuppression,
911 EchoCancellation::kModerateSuppression,
912 EchoCancellation::kHighSuppression,
913 };
pkasting25702cb2016-01-08 13:50:27 -0800914 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 EXPECT_EQ(apm_->kNoError,
916 apm_->echo_cancellation()->set_suppression_level(level[i]));
917 EXPECT_EQ(level[i],
918 apm_->echo_cancellation()->suppression_level());
919 }
920
921 EchoCancellation::Metrics metrics;
922 EXPECT_EQ(apm_->kNotEnabledError,
923 apm_->echo_cancellation()->GetMetrics(&metrics));
924
925 EXPECT_EQ(apm_->kNoError,
926 apm_->echo_cancellation()->enable_metrics(true));
927 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
928 EXPECT_EQ(apm_->kNoError,
929 apm_->echo_cancellation()->enable_metrics(false));
930 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
931
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000932 int median = 0;
933 int std = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000934 float poor_fraction = 0;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000935 EXPECT_EQ(apm_->kNotEnabledError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000936 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
937 &poor_fraction));
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000938
939 EXPECT_EQ(apm_->kNoError,
940 apm_->echo_cancellation()->enable_delay_logging(true));
941 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
942 EXPECT_EQ(apm_->kNoError,
943 apm_->echo_cancellation()->enable_delay_logging(false));
944 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
945
niklase@google.com470e71d2011-07-07 08:21:25 +0000946 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
947 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
948 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
949 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000950
951 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
952 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
953 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
954 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
955 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
956 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000957}
958
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000959TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000960 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000961 // Enable AEC only.
962 EXPECT_EQ(apm_->kNoError,
963 apm_->echo_cancellation()->enable_drift_compensation(false));
964 EXPECT_EQ(apm_->kNoError,
965 apm_->echo_cancellation()->enable_metrics(false));
966 EXPECT_EQ(apm_->kNoError,
967 apm_->echo_cancellation()->enable_delay_logging(true));
968 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000969 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700970 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000971 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000972
973 // Internally in the AEC the amount of lookahead the delay estimation can
974 // handle is 15 blocks and the maximum delay is set to 60 blocks.
975 const int kLookaheadBlocks = 15;
976 const int kMaxDelayBlocks = 60;
977 // The AEC has a startup time before it actually starts to process. This
978 // procedure can flush the internal far-end buffer, which of course affects
979 // the delay estimation. Therefore, we set a system_delay high enough to
980 // avoid that. The smallest system_delay you can report without flushing the
981 // buffer is 66 ms in 8 kHz.
982 //
983 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
984 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
985 // delay estimation. This should be noted though. In case of test failure,
986 // this could be the cause.
987 const int kSystemDelayMs = 66;
988 // Test a couple of corner cases and verify that the estimated delay is
989 // within a valid region (set to +-1.5 blocks). Note that these cases are
990 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800991 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000992 Init(kProcessSampleRates[i],
993 kProcessSampleRates[i],
994 kProcessSampleRates[i],
995 2,
996 2,
997 2,
998 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000999 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001000 const int num_ms_per_block =
1001 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001002 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1003 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1004
1005 // 1) Verify correct delay estimate at lookahead boundary.
1006 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1007 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1008 delay_max_ms);
1009 // 2) A delay less than maximum lookahead should give an delay estimate at
1010 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1011 delay_ms -= 20;
1012 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1013 delay_max_ms);
1014 // 3) Three values around zero delay. Note that we need to compensate for
1015 // the fake system_delay.
1016 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1017 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1018 delay_max_ms);
1019 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1020 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1021 delay_max_ms);
1022 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1023 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1024 delay_max_ms);
1025 // 4) Verify correct delay estimate at maximum delay boundary.
1026 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1027 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1028 delay_max_ms);
1029 // 5) A delay above the maximum delay should give an estimate at the
1030 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1031 delay_ms += 20;
1032 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1033 delay_max_ms);
1034 }
1035}
1036
niklase@google.com470e71d2011-07-07 08:21:25 +00001037TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001038 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001039 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001040 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1041 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1042
niklase@google.com470e71d2011-07-07 08:21:25 +00001043 // Toggle routing modes
1044 EchoControlMobile::RoutingMode mode[] = {
1045 EchoControlMobile::kQuietEarpieceOrHeadset,
1046 EchoControlMobile::kEarpiece,
1047 EchoControlMobile::kLoudEarpiece,
1048 EchoControlMobile::kSpeakerphone,
1049 EchoControlMobile::kLoudSpeakerphone,
1050 };
pkasting25702cb2016-01-08 13:50:27 -08001051 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 EXPECT_EQ(apm_->kNoError,
1053 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1054 EXPECT_EQ(mode[i],
1055 apm_->echo_control_mobile()->routing_mode());
1056 }
1057 // Turn comfort noise off/on
1058 EXPECT_EQ(apm_->kNoError,
1059 apm_->echo_control_mobile()->enable_comfort_noise(false));
1060 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1061 EXPECT_EQ(apm_->kNoError,
1062 apm_->echo_control_mobile()->enable_comfort_noise(true));
1063 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001064 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001065 const size_t echo_path_size =
1066 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001067 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1068 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001069 EXPECT_EQ(apm_->kNullPointerError,
1070 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1071 EXPECT_EQ(apm_->kNullPointerError,
1072 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1073 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001074 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001075 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001076 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001077 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001078 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001079 echo_path_in[i] = echo_path_out[i] + 1;
1080 }
1081 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001082 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001083 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001084 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1085 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001086 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001087 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1088 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001089 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001090 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1091 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001092
1093 // Process a few frames with NS in the default disabled state. This exercises
1094 // a different codepath than with it enabled.
1095 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1096 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1097 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1098 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1099
niklase@google.com470e71d2011-07-07 08:21:25 +00001100 // Turn AECM off
1101 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1102 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1103}
1104
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001105TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001106 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001107 EXPECT_EQ(apm_->kNoError,
1108 apm_->gain_control()->set_mode(
1109 apm_->gain_control()->mode()));
1110
1111 GainControl::Mode mode[] = {
1112 GainControl::kAdaptiveAnalog,
1113 GainControl::kAdaptiveDigital,
1114 GainControl::kFixedDigital
1115 };
pkasting25702cb2016-01-08 13:50:27 -08001116 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001117 EXPECT_EQ(apm_->kNoError,
1118 apm_->gain_control()->set_mode(mode[i]));
1119 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1120 }
1121 // Testing invalid target levels
1122 EXPECT_EQ(apm_->kBadParameterError,
1123 apm_->gain_control()->set_target_level_dbfs(-3));
1124 EXPECT_EQ(apm_->kBadParameterError,
1125 apm_->gain_control()->set_target_level_dbfs(-40));
1126 // Testing valid target levels
1127 EXPECT_EQ(apm_->kNoError,
1128 apm_->gain_control()->set_target_level_dbfs(
1129 apm_->gain_control()->target_level_dbfs()));
1130
1131 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001132 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 EXPECT_EQ(apm_->kNoError,
1134 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1135 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1136 }
1137
1138 // Testing invalid compression gains
1139 EXPECT_EQ(apm_->kBadParameterError,
1140 apm_->gain_control()->set_compression_gain_db(-1));
1141 EXPECT_EQ(apm_->kBadParameterError,
1142 apm_->gain_control()->set_compression_gain_db(100));
1143
1144 // Testing valid compression gains
1145 EXPECT_EQ(apm_->kNoError,
1146 apm_->gain_control()->set_compression_gain_db(
1147 apm_->gain_control()->compression_gain_db()));
1148
1149 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001150 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001151 EXPECT_EQ(apm_->kNoError,
1152 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1153 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1154 }
1155
1156 // Testing limiter off/on
1157 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1158 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1159 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1160 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1161
1162 // Testing invalid level limits
1163 EXPECT_EQ(apm_->kBadParameterError,
1164 apm_->gain_control()->set_analog_level_limits(-1, 512));
1165 EXPECT_EQ(apm_->kBadParameterError,
1166 apm_->gain_control()->set_analog_level_limits(100000, 512));
1167 EXPECT_EQ(apm_->kBadParameterError,
1168 apm_->gain_control()->set_analog_level_limits(512, -1));
1169 EXPECT_EQ(apm_->kBadParameterError,
1170 apm_->gain_control()->set_analog_level_limits(512, 100000));
1171 EXPECT_EQ(apm_->kBadParameterError,
1172 apm_->gain_control()->set_analog_level_limits(512, 255));
1173
1174 // Testing valid level limits
1175 EXPECT_EQ(apm_->kNoError,
1176 apm_->gain_control()->set_analog_level_limits(
1177 apm_->gain_control()->analog_level_minimum(),
1178 apm_->gain_control()->analog_level_maximum()));
1179
1180 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001181 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001182 EXPECT_EQ(apm_->kNoError,
1183 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1184 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1185 }
1186
1187 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001188 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 EXPECT_EQ(apm_->kNoError,
1190 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1191 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1192 }
1193
1194 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1195
1196 // Turn AGC off
1197 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1198 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1199}
1200
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001201void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001202 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001203 EXPECT_EQ(apm_->kNoError,
1204 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1205 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1206
1207 int out_analog_level = 0;
1208 for (int i = 0; i < 2000; ++i) {
1209 ReadFrameWithRewind(near_file_, frame_);
1210 // Ensure the audio is at a low level, so the AGC will try to increase it.
1211 ScaleFrame(frame_, 0.25);
1212
1213 // Always pass in the same volume.
1214 EXPECT_EQ(apm_->kNoError,
1215 apm_->gain_control()->set_stream_analog_level(100));
1216 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1217 out_analog_level = apm_->gain_control()->stream_analog_level();
1218 }
1219
1220 // Ensure the AGC is still able to reach the maximum.
1221 EXPECT_EQ(255, out_analog_level);
1222}
1223
1224// Verifies that despite volume slider quantization, the AGC can continue to
1225// increase its volume.
1226TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001227 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001228 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1229 }
1230}
1231
1232void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001233 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001234 EXPECT_EQ(apm_->kNoError,
1235 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1236 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1237
1238 int out_analog_level = 100;
1239 for (int i = 0; i < 1000; ++i) {
1240 ReadFrameWithRewind(near_file_, frame_);
1241 // Ensure the audio is at a low level, so the AGC will try to increase it.
1242 ScaleFrame(frame_, 0.25);
1243
1244 EXPECT_EQ(apm_->kNoError,
1245 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1246 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1247 out_analog_level = apm_->gain_control()->stream_analog_level();
1248 }
1249
1250 // Ensure the volume was raised.
1251 EXPECT_GT(out_analog_level, 100);
1252 int highest_level_reached = out_analog_level;
1253 // Simulate a user manual volume change.
1254 out_analog_level = 100;
1255
1256 for (int i = 0; i < 300; ++i) {
1257 ReadFrameWithRewind(near_file_, frame_);
1258 ScaleFrame(frame_, 0.25);
1259
1260 EXPECT_EQ(apm_->kNoError,
1261 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1262 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1263 out_analog_level = apm_->gain_control()->stream_analog_level();
1264 // Check that AGC respected the manually adjusted volume.
1265 EXPECT_LT(out_analog_level, highest_level_reached);
1266 }
1267 // Check that the volume was still raised.
1268 EXPECT_GT(out_analog_level, 100);
1269}
1270
1271TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001272 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001273 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1274 }
1275}
1276
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001277#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1278TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1279 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001280 const size_t kSamplesPerChannel =
1281 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001282 const size_t kNumInputChannels = 2;
1283 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001284 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001285 const float kScaleFactor = 0.25f;
1286 Config config;
1287 std::vector<webrtc::Point> geometry;
1288 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1289 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1290 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001291 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
1292 new testing::NiceMock<MockNonlinearBeamformer>(geometry);
kwiberg62eaacf2016-02-17 06:39:05 -08001293 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001294 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001295 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1296 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1297 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001298 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1299 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001300 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1301 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001302 std::string filename = ResourceFilePath("far", kSampleRateHz);
1303 FILE* far_file = fopen(filename.c_str(), "rb");
1304 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1305 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1306 const int kDefaultCompressionGain =
1307 apm->gain_control()->compression_gain_db();
1308 bool is_target = false;
1309 EXPECT_CALL(*beamformer, is_target_present())
1310 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001311 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001312 ASSERT_TRUE(ReadChunk(far_file,
1313 int_data.get(),
1314 float_data.get(),
1315 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001316 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001317 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001318 src_buf.channels()[j][k] *= kScaleFactor;
1319 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001320 }
1321 EXPECT_EQ(kNoErr,
1322 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001323 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001324 kSampleRateHz,
1325 LayoutFromChannels(src_buf.num_channels()),
1326 kSampleRateHz,
1327 LayoutFromChannels(dest_buf.num_channels()),
1328 dest_buf.channels()));
1329 }
1330 EXPECT_EQ(kDefaultVolume,
1331 apm->gain_control()->stream_analog_level());
1332 EXPECT_EQ(kDefaultCompressionGain,
1333 apm->gain_control()->compression_gain_db());
1334 rewind(far_file);
1335 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001336 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001337 ASSERT_TRUE(ReadChunk(far_file,
1338 int_data.get(),
1339 float_data.get(),
1340 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001341 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001342 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001343 src_buf.channels()[j][k] *= kScaleFactor;
1344 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001345 }
1346 EXPECT_EQ(kNoErr,
1347 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001348 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001349 kSampleRateHz,
1350 LayoutFromChannels(src_buf.num_channels()),
1351 kSampleRateHz,
1352 LayoutFromChannels(dest_buf.num_channels()),
1353 dest_buf.channels()));
1354 }
1355 EXPECT_LT(kDefaultVolume,
1356 apm->gain_control()->stream_analog_level());
1357 EXPECT_LT(kDefaultCompressionGain,
1358 apm->gain_control()->compression_gain_db());
1359 ASSERT_EQ(0, fclose(far_file));
1360}
1361#endif
1362
niklase@google.com470e71d2011-07-07 08:21:25 +00001363TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001364 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001365 NoiseSuppression::Level level[] = {
1366 NoiseSuppression::kLow,
1367 NoiseSuppression::kModerate,
1368 NoiseSuppression::kHigh,
1369 NoiseSuppression::kVeryHigh
1370 };
pkasting25702cb2016-01-08 13:50:27 -08001371 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001372 EXPECT_EQ(apm_->kNoError,
1373 apm_->noise_suppression()->set_level(level[i]));
1374 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1375 }
1376
andrew@webrtc.org648af742012-02-08 01:57:29 +00001377 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001378 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1379 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1380 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1381 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1382}
1383
1384TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001385 // Turn HP filter on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001386 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(true));
1387 EXPECT_TRUE(apm_->high_pass_filter()->is_enabled());
1388 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(false));
1389 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1390}
1391
1392TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001393 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001394 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001395 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001396
1397 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1398
1399 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1400 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1401
1402 // Run this test in wideband; in super-wb, the splitting filter distorts the
1403 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001404 frame_->samples_per_channel_ = 160;
1405 frame_->num_channels_ = 2;
1406 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001407
1408 // Min value if no frames have been processed.
1409 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1410
1411 // Min value on zero frames.
1412 SetFrameTo(frame_, 0);
1413 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1414 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1415 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1416
1417 // Try a few RMS values.
1418 // (These also test that the value resets after retrieving it.)
1419 SetFrameTo(frame_, 32767);
1420 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1421 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1422 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1423
1424 SetFrameTo(frame_, 30000);
1425 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1426 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1427 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1428
1429 SetFrameTo(frame_, 10000);
1430 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1431 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1432 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1433
1434 SetFrameTo(frame_, 10);
1435 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1436 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1437 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1438
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001439 // Verify reset after enable/disable.
1440 SetFrameTo(frame_, 32767);
1441 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1442 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1443 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1444 SetFrameTo(frame_, 1);
1445 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1446 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1447
1448 // Verify reset after initialize.
1449 SetFrameTo(frame_, 32767);
1450 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1451 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1452 SetFrameTo(frame_, 1);
1453 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1454 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001455}
1456
1457TEST_F(ApmTest, VoiceDetection) {
1458 // Test external VAD
1459 EXPECT_EQ(apm_->kNoError,
1460 apm_->voice_detection()->set_stream_has_voice(true));
1461 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1462 EXPECT_EQ(apm_->kNoError,
1463 apm_->voice_detection()->set_stream_has_voice(false));
1464 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1465
andrew@webrtc.org648af742012-02-08 01:57:29 +00001466 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001467 VoiceDetection::Likelihood likelihood[] = {
1468 VoiceDetection::kVeryLowLikelihood,
1469 VoiceDetection::kLowLikelihood,
1470 VoiceDetection::kModerateLikelihood,
1471 VoiceDetection::kHighLikelihood
1472 };
pkasting25702cb2016-01-08 13:50:27 -08001473 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001474 EXPECT_EQ(apm_->kNoError,
1475 apm_->voice_detection()->set_likelihood(likelihood[i]));
1476 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1477 }
1478
1479 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001480 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001481 EXPECT_EQ(apm_->kBadParameterError,
1482 apm_->voice_detection()->set_frame_size_ms(12));
1483
andrew@webrtc.org648af742012-02-08 01:57:29 +00001484 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001485 for (int i = 10; i <= 30; i += 10) {
1486 EXPECT_EQ(apm_->kNoError,
1487 apm_->voice_detection()->set_frame_size_ms(i));
1488 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1489 }
1490 */
1491
andrew@webrtc.org648af742012-02-08 01:57:29 +00001492 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001493 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1494 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1495 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1496 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1497
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001498 // Test that AudioFrame activity is maintained when VAD is disabled.
1499 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1500 AudioFrame::VADActivity activity[] = {
1501 AudioFrame::kVadActive,
1502 AudioFrame::kVadPassive,
1503 AudioFrame::kVadUnknown
1504 };
pkasting25702cb2016-01-08 13:50:27 -08001505 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001506 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001507 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001508 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001509 }
1510
1511 // Test that AudioFrame activity is set when VAD is enabled.
1512 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001513 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001514 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001515 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001516
niklase@google.com470e71d2011-07-07 08:21:25 +00001517 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1518}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001519
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001520TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1521 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1522 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1523 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1524 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1525 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1526 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1527 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1528}
1529
1530TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001531 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001532 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001533 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001534 AudioFrame frame_copy;
1535 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001536 for (int j = 0; j < 1000; j++) {
1537 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1538 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001539 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1540 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001541 }
1542 }
1543}
1544
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001545TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1546 // Test that ProcessStream copies input to output even with no processing.
1547 const size_t kSamples = 80;
1548 const int sample_rate = 8000;
1549 const float src[kSamples] = {
1550 -1.0f, 0.0f, 1.0f
1551 };
1552 float dest[kSamples] = {};
1553
1554 auto src_channels = &src[0];
1555 auto dest_channels = &dest[0];
1556
1557 apm_.reset(AudioProcessing::Create());
1558 EXPECT_NOERR(apm_->ProcessStream(
1559 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1560 sample_rate, LayoutFromChannels(1), &dest_channels));
1561
1562 for (size_t i = 0; i < kSamples; ++i) {
1563 EXPECT_EQ(src[i], dest[i]);
1564 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001565
1566 // Same for ProcessReverseStream.
1567 float rev_dest[kSamples] = {};
1568 auto rev_dest_channels = &rev_dest[0];
1569
1570 StreamConfig input_stream = {sample_rate, 1};
1571 StreamConfig output_stream = {sample_rate, 1};
1572 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1573 output_stream, &rev_dest_channels));
1574
1575 for (size_t i = 0; i < kSamples; ++i) {
1576 EXPECT_EQ(src[i], rev_dest[i]);
1577 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001578}
1579
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001580TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1581 EnableAllComponents();
1582
pkasting25702cb2016-01-08 13:50:27 -08001583 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001584 Init(kProcessSampleRates[i],
1585 kProcessSampleRates[i],
1586 kProcessSampleRates[i],
1587 2,
1588 2,
1589 2,
1590 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001591 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001592 ASSERT_EQ(0, feof(far_file_));
1593 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001594 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001595 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1596
aluebsb0319552016-03-17 20:39:53 -07001597 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001598
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001599 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1600 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1601
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001602 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001603 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001604 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001605 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001606 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001607 analog_level = apm_->gain_control()->stream_analog_level();
1608
1609 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1610 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001611 rewind(far_file_);
1612 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001613 }
1614}
1615
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001616TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001617 // Verify the filter is not active through undistorted audio when:
1618 // 1. No components are enabled...
1619 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001620 AudioFrame frame_copy;
1621 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001622 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1623 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1624 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1625
1626 // 2. Only the level estimator is enabled...
1627 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001628 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001629 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1630 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1631 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1632 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1633 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1634
1635 // 3. Only VAD is enabled...
1636 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001637 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001638 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1639 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1640 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1641 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1642 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1643
1644 // 4. Both VAD and the level estimator are enabled...
1645 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001646 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001647 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1648 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1649 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1650 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1651 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1652 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1653 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1654
1655 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001656 frame_->samples_per_channel_ = 160;
1657 frame_->num_channels_ = 2;
1658 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001659 // Enable AEC, which would require the filter in super-wb. We rely on the
1660 // first few frames of data being unaffected by the AEC.
1661 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1662 // behavior of the AEC. Think of something more robust.
1663 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001664 // Make sure we have extended filter enabled. This makes sure nothing is
1665 // touched until we have a farend frame.
1666 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001667 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001668 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001669 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001670 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001671 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001672 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001673 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1674 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001675 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001676 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1677 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1678
1679 // Check the test is valid. We should have distortion from the filter
1680 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001681 frame_->samples_per_channel_ = 320;
1682 frame_->num_channels_ = 2;
1683 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001684 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001685 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001686 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001687 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001688 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1689 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1690}
1691
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001692#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1693void ApmTest::ProcessDebugDump(const std::string& in_filename,
1694 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001695 Format format,
1696 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001697 FILE* in_file = fopen(in_filename.c_str(), "rb");
1698 ASSERT_TRUE(in_file != NULL);
1699 audioproc::Event event_msg;
1700 bool first_init = true;
1701
1702 while (ReadMessageFromFile(in_file, &event_msg)) {
1703 if (event_msg.type() == audioproc::Event::INIT) {
1704 const audioproc::Init msg = event_msg.init();
1705 int reverse_sample_rate = msg.sample_rate();
1706 if (msg.has_reverse_sample_rate()) {
1707 reverse_sample_rate = msg.reverse_sample_rate();
1708 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001709 int output_sample_rate = msg.sample_rate();
1710 if (msg.has_output_sample_rate()) {
1711 output_sample_rate = msg.output_sample_rate();
1712 }
1713
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001714 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001715 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001716 reverse_sample_rate,
1717 msg.num_input_channels(),
1718 msg.num_output_channels(),
1719 msg.num_reverse_channels(),
1720 false);
1721 if (first_init) {
1722 // StartDebugRecording() writes an additional init message. Don't start
1723 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001724 EXPECT_NOERR(
1725 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001726 first_init = false;
1727 }
1728
1729 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1730 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1731
1732 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001733 ASSERT_EQ(revframe_->num_channels_,
1734 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001735 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001736 memcpy(revfloat_cb_->channels()[i],
1737 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001738 msg.channel(i).size());
1739 }
1740 } else {
1741 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1742 if (format == kFloatFormat) {
1743 // We're using an int16 input file; convert to float.
1744 ConvertToFloat(*revframe_, revfloat_cb_.get());
1745 }
1746 }
1747 AnalyzeReverseStreamChooser(format);
1748
1749 } else if (event_msg.type() == audioproc::Event::STREAM) {
1750 const audioproc::Stream msg = event_msg.stream();
1751 // ProcessStream could have changed this for the output frame.
1752 frame_->num_channels_ = apm_->num_input_channels();
1753
1754 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1755 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1756 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1757 if (msg.has_keypress()) {
1758 apm_->set_stream_key_pressed(msg.keypress());
1759 } else {
1760 apm_->set_stream_key_pressed(true);
1761 }
1762
1763 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001764 ASSERT_EQ(frame_->num_channels_,
1765 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001766 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001767 memcpy(float_cb_->channels()[i],
1768 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001769 msg.input_channel(i).size());
1770 }
1771 } else {
1772 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1773 if (format == kFloatFormat) {
1774 // We're using an int16 input file; convert to float.
1775 ConvertToFloat(*frame_, float_cb_.get());
1776 }
1777 }
1778 ProcessStreamChooser(format);
1779 }
1780 }
1781 EXPECT_NOERR(apm_->StopDebugRecording());
1782 fclose(in_file);
1783}
1784
1785void ApmTest::VerifyDebugDumpTest(Format format) {
1786 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001787 std::string format_string;
1788 switch (format) {
1789 case kIntFormat:
1790 format_string = "_int";
1791 break;
1792 case kFloatFormat:
1793 format_string = "_float";
1794 break;
1795 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001796 const std::string ref_filename = test::TempFilename(
1797 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1798 const std::string out_filename = test::TempFilename(
1799 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001800 const std::string limited_filename = test::TempFilename(
1801 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1802 const size_t logging_limit_bytes = 100000;
1803 // We expect at least this many bytes in the created logfile.
1804 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001805 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001806 ProcessDebugDump(in_filename, ref_filename, format, -1);
1807 ProcessDebugDump(ref_filename, out_filename, format, -1);
1808 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001809
1810 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1811 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001812 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001813 ASSERT_TRUE(ref_file != NULL);
1814 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001815 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001816 std::unique_ptr<uint8_t[]> ref_bytes;
1817 std::unique_ptr<uint8_t[]> out_bytes;
1818 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001819
1820 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1821 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001822 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001823 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001824 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001825 while (ref_size > 0 && out_size > 0) {
1826 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001827 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001828 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001829 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001830 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001831 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001832 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1833 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001834 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001835 }
1836 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001837 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1838 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001839 EXPECT_NE(0, feof(ref_file));
1840 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001841 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001842 ASSERT_EQ(0, fclose(ref_file));
1843 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001844 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001845 remove(ref_filename.c_str());
1846 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001847 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001848}
1849
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +02001850// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
1851#ifdef UNDEFINED_SANITIZER
1852#define MAYBE_VerifyDebugDumpInt DISABLED_VerifyDebugDumpInt
1853#else
1854#define MAYBE_VerifyDebugDumpInt VerifyDebugDumpInt
1855#endif
1856TEST_F(ApmTest, MAYBE_VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001857 VerifyDebugDumpTest(kIntFormat);
1858}
1859
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +02001860// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
1861#ifdef UNDEFINED_SANITIZER
1862#define MAYBE_VerifyDebugDumpFloat DISABLED_VerifyDebugDumpFloat
1863#else
1864#define MAYBE_VerifyDebugDumpFloat VerifyDebugDumpFloat
1865#endif
1866TEST_F(ApmTest, MAYBE_VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001867 VerifyDebugDumpTest(kFloatFormat);
1868}
1869#endif
1870
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001871// TODO(andrew): expand test to verify output.
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +02001872// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
1873#ifdef UNDEFINED_SANITIZER
1874#define MAYBE_DebugDump DISABLED_DebugDump
1875#else
1876#define MAYBE_DebugDump DebugDump
1877#endif
1878TEST_F(ApmTest, MAYBE_DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001879 const std::string filename =
1880 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001881 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001882 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001883
1884#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1885 // Stopping without having started should be OK.
1886 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1887
ivocd66b44d2016-01-15 03:06:36 -08001888 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001889 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001890 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001891 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1892
1893 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001894 FILE* fid = fopen(filename.c_str(), "r");
1895 ASSERT_TRUE(fid != NULL);
1896
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001897 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001898 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001899 ASSERT_EQ(0, remove(filename.c_str()));
1900#else
1901 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001902 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001903 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1904
1905 // Verify the file has NOT been written.
1906 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1907#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1908}
1909
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001910// TODO(andrew): expand test to verify output.
kjellander@webrtc.orgc23bf2e2016-04-25 06:43:43 +02001911// Disabled for UBSan: https://bugs.chromium.org/p/webrtc/issues/detail?id=5820
1912#ifdef UNDEFINED_SANITIZER
1913#define MAYBE_DebugDumpFromFileHandle DISABLED_DebugDumpFromFileHandle
1914#else
1915#define MAYBE_DebugDumpFromFileHandle DebugDumpFromFileHandle
1916#endif
1917TEST_F(ApmTest, MAYBE_DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001918 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001919 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001920 const std::string filename =
1921 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001922 fid = fopen(filename.c_str(), "w");
1923 ASSERT_TRUE(fid);
1924
1925#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1926 // Stopping without having started should be OK.
1927 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1928
ivocd66b44d2016-01-15 03:06:36 -08001929 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
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_));
1932 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1933
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
1942 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001943 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001944 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1945
1946 ASSERT_EQ(0, fclose(fid));
1947#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1948}
1949
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001950TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001951 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001952 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001953
1954 Config config;
1955 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001956 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001957 EnableAllComponents();
1958 EnableAllAPComponents(fapm.get());
1959 for (int i = 0; i < ref_data.test_size(); i++) {
1960 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1961
1962 audioproc::Test* test = ref_data.mutable_test(i);
1963 // TODO(ajm): Restore downmixing test cases.
1964 if (test->num_input_channels() != test->num_output_channels())
1965 continue;
1966
Peter Kasting69558702016-01-12 16:26:35 -08001967 const size_t num_render_channels =
1968 static_cast<size_t>(test->num_reverse_channels());
1969 const size_t num_input_channels =
1970 static_cast<size_t>(test->num_input_channels());
1971 const size_t num_output_channels =
1972 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001973 const size_t samples_per_channel = static_cast<size_t>(
1974 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001975
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001976 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1977 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001978 Init(fapm.get());
1979
1980 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001981 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1982 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001983
1984 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001985 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001986 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1987 ReadFrame(near_file_, frame_, float_cb_.get())) {
1988 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1989
aluebsb0319552016-03-17 20:39:53 -07001990 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001991 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1992 revfloat_cb_->channels(),
1993 samples_per_channel,
1994 test->sample_rate(),
1995 LayoutFromChannels(num_render_channels)));
1996
1997 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1998 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1999 apm_->echo_cancellation()->set_stream_drift_samples(0);
2000 fapm->echo_cancellation()->set_stream_drift_samples(0);
2001 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
2002 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
2003
2004 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002005 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
2006 output_int16.channels());
2007
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002008 EXPECT_NOERR(fapm->ProcessStream(
2009 float_cb_->channels(),
2010 samples_per_channel,
2011 test->sample_rate(),
2012 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002013 test->sample_rate(),
2014 LayoutFromChannels(num_output_channels),
2015 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002016 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002017 FloatToS16(float_cb_->channels()[j],
2018 samples_per_channel,
2019 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002020 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002021 float snr = ComputeSNR(output_int16.channels()[j],
2022 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002023 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002024
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002025 const float kVarianceThreshold = 20;
2026 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002027
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002028 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002029 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2030 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002031 }
2032 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002033
2034 analog_level = fapm->gain_control()->stream_analog_level();
2035 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2036 fapm->gain_control()->stream_analog_level());
2037 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2038 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002039 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2040 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002041 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002042
2043 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002044 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002045 }
aluebs776593b2016-03-15 14:04:58 -07002046
2047#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2048 const size_t kMaxNumBadChunks = 0;
2049#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2050 // There are a few chunks in the fixed-point profile that give low SNR.
2051 // Listening confirmed the difference is acceptable.
2052 const size_t kMaxNumBadChunks = 60;
2053#endif
2054 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2055
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002056 rewind(far_file_);
2057 rewind(near_file_);
2058 }
2059}
2060
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002061// TODO(andrew): Add a test to process a few frames with different combinations
2062// of enabled components.
2063
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002064TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002065 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002066 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002067
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002068 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002069 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002070 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002071 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002072 for (size_t i = 0; i < arraysize(kChannels); i++) {
2073 for (size_t j = 0; j < arraysize(kChannels); j++) {
2074 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002075 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002076 test->set_num_reverse_channels(kChannels[i]);
2077 test->set_num_input_channels(kChannels[j]);
2078 test->set_num_output_channels(kChannels[j]);
2079 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002080 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002081 }
2082 }
2083 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002084#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2085 // To test the extended filter mode.
2086 audioproc::Test* test = ref_data.add_test();
2087 test->set_num_reverse_channels(2);
2088 test->set_num_input_channels(2);
2089 test->set_num_output_channels(2);
2090 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2091 test->set_use_aec_extended_filter(true);
2092#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002093 }
2094
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002095 for (int i = 0; i < ref_data.test_size(); i++) {
2096 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002097
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002098 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002099 // TODO(ajm): We no longer allow different input and output channels. Skip
2100 // these tests for now, but they should be removed from the set.
2101 if (test->num_input_channels() != test->num_output_channels())
2102 continue;
2103
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002104 Config config;
2105 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002106 config.Set<ExtendedFilter>(
2107 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002108 apm_.reset(AudioProcessing::Create(config));
2109
2110 EnableAllComponents();
2111
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002112 Init(test->sample_rate(),
2113 test->sample_rate(),
2114 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002115 static_cast<size_t>(test->num_input_channels()),
2116 static_cast<size_t>(test->num_output_channels()),
2117 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002118 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002119
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002120 int frame_count = 0;
2121 int has_echo_count = 0;
2122 int has_voice_count = 0;
2123 int is_saturated_count = 0;
2124 int analog_level = 127;
2125 int analog_level_average = 0;
2126 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002127 float ns_speech_prob_average = 0.0f;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002128
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002129 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002130 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002131
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002132 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2133
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002134 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002135 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002136 EXPECT_EQ(apm_->kNoError,
2137 apm_->gain_control()->set_stream_analog_level(analog_level));
2138
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002139 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002140
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002141 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002142 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2143 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002144
2145 max_output_average += MaxAudioFrame(*frame_);
2146
2147 if (apm_->echo_cancellation()->stream_has_echo()) {
2148 has_echo_count++;
2149 }
2150
2151 analog_level = apm_->gain_control()->stream_analog_level();
2152 analog_level_average += analog_level;
2153 if (apm_->gain_control()->stream_is_saturated()) {
2154 is_saturated_count++;
2155 }
2156 if (apm_->voice_detection()->stream_has_voice()) {
2157 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002158 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002159 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002160 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002161 }
2162
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002163 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2164
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002165 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002166 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002167 sizeof(int16_t),
2168 frame_size,
2169 out_file_);
2170 ASSERT_EQ(frame_size, write_count);
2171
2172 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002173 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002174 frame_count++;
2175 }
2176 max_output_average /= frame_count;
2177 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002178 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002179
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002180#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002181 EchoCancellation::Metrics echo_metrics;
2182 EXPECT_EQ(apm_->kNoError,
2183 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2184 int median = 0;
2185 int std = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002186 float fraction_poor_delays = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002187 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002188 apm_->echo_cancellation()->GetDelayMetrics(
2189 &median, &std, &fraction_poor_delays));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002190
2191 int rms_level = apm_->level_estimator()->RMS();
2192 EXPECT_LE(0, rms_level);
2193 EXPECT_GE(127, rms_level);
2194#endif
2195
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002196 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002197 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002198 // When running the test on a N7 we get a {2, 6} difference of
2199 // |has_voice_count| and |max_output_average| is up to 18 higher.
2200 // All numbers being consistently higher on N7 compare to ref_data.
2201 // TODO(bjornv): If we start getting more of these offsets on Android we
2202 // should consider a different approach. Either using one slack for all,
2203 // or generate a separate android reference.
2204#if defined(WEBRTC_ANDROID)
2205 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002206 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002207 const int kMaxOutputAverageOffset = 9;
2208 const int kMaxOutputAverageNear = 9;
2209#else
2210 const int kHasVoiceCountOffset = 0;
2211 const int kHasVoiceCountNear = kIntNear;
2212 const int kMaxOutputAverageOffset = 0;
2213 const int kMaxOutputAverageNear = kIntNear;
2214#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002215 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002216 EXPECT_NEAR(test->has_voice_count(),
2217 has_voice_count - kHasVoiceCountOffset,
2218 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002219 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002220
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002221 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002222 EXPECT_NEAR(test->max_output_average(),
2223 max_output_average - kMaxOutputAverageOffset,
2224 kMaxOutputAverageNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002225
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002226#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002227 audioproc::Test::EchoMetrics reference = test->echo_metrics();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002228 TestStats(echo_metrics.residual_echo_return_loss,
2229 reference.residual_echo_return_loss());
2230 TestStats(echo_metrics.echo_return_loss,
2231 reference.echo_return_loss());
2232 TestStats(echo_metrics.echo_return_loss_enhancement,
2233 reference.echo_return_loss_enhancement());
2234 TestStats(echo_metrics.a_nlp,
2235 reference.a_nlp());
2236
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002237 const double kFloatNear = 0.0005;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002238 audioproc::Test::DelayMetrics reference_delay = test->delay_metrics();
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002239 EXPECT_NEAR(reference_delay.median(), median, kIntNear);
2240 EXPECT_NEAR(reference_delay.std(), std, kIntNear);
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002241 EXPECT_NEAR(reference_delay.fraction_poor_delays(), fraction_poor_delays,
2242 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002243
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002244 EXPECT_NEAR(test->rms_level(), rms_level, kIntNear);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002245
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002246 EXPECT_NEAR(test->ns_speech_probability_average(),
2247 ns_speech_prob_average,
2248 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002249#endif
2250 } else {
2251 test->set_has_echo_count(has_echo_count);
2252 test->set_has_voice_count(has_voice_count);
2253 test->set_is_saturated_count(is_saturated_count);
2254
2255 test->set_analog_level_average(analog_level_average);
2256 test->set_max_output_average(max_output_average);
2257
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002258#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002259 audioproc::Test::EchoMetrics* message = test->mutable_echo_metrics();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002260 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2261 message->mutable_residual_echo_return_loss());
2262 WriteStatsMessage(echo_metrics.echo_return_loss,
2263 message->mutable_echo_return_loss());
2264 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2265 message->mutable_echo_return_loss_enhancement());
2266 WriteStatsMessage(echo_metrics.a_nlp,
2267 message->mutable_a_nlp());
2268
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002269 audioproc::Test::DelayMetrics* message_delay =
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002270 test->mutable_delay_metrics();
2271 message_delay->set_median(median);
2272 message_delay->set_std(std);
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +00002273 message_delay->set_fraction_poor_delays(fraction_poor_delays);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002274
2275 test->set_rms_level(rms_level);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002276
2277 EXPECT_LE(0.0f, ns_speech_prob_average);
2278 EXPECT_GE(1.0f, ns_speech_prob_average);
2279 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002280#endif
2281 }
2282
2283 rewind(far_file_);
2284 rewind(near_file_);
2285 }
2286
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002287 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002288 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002289 }
2290}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002291
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002292TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2293 struct ChannelFormat {
2294 AudioProcessing::ChannelLayout in_layout;
2295 AudioProcessing::ChannelLayout out_layout;
2296 };
2297 ChannelFormat cf[] = {
2298 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2299 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2300 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2301 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002302
kwiberg62eaacf2016-02-17 06:39:05 -08002303 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002304 // Enable one component just to ensure some processing takes place.
2305 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002306 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002307 const int in_rate = 44100;
2308 const int out_rate = 48000;
2309 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2310 TotalChannelsFromLayout(cf[i].in_layout));
2311 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2312 ChannelsFromLayout(cf[i].out_layout));
2313
2314 // Run over a few chunks.
2315 for (int j = 0; j < 10; ++j) {
2316 EXPECT_NOERR(ap->ProcessStream(
2317 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002318 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002319 in_rate,
2320 cf[i].in_layout,
2321 out_rate,
2322 cf[i].out_layout,
2323 out_cb.channels()));
2324 }
2325 }
2326}
2327
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002328// Compares the reference and test arrays over a region around the expected
2329// delay. Finds the highest SNR in that region and adds the variance and squared
2330// error results to the supplied accumulators.
2331void UpdateBestSNR(const float* ref,
2332 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002333 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002334 int expected_delay,
2335 double* variance_acc,
2336 double* sq_error_acc) {
2337 double best_snr = std::numeric_limits<double>::min();
2338 double best_variance = 0;
2339 double best_sq_error = 0;
2340 // Search over a region of eight samples around the expected delay.
2341 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2342 ++delay) {
2343 double sq_error = 0;
2344 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002345 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002346 double error = test[i + delay] - ref[i];
2347 sq_error += error * error;
2348 variance += ref[i] * ref[i];
2349 }
2350
2351 if (sq_error == 0) {
2352 *variance_acc += variance;
2353 return;
2354 }
2355 double snr = variance / sq_error;
2356 if (snr > best_snr) {
2357 best_snr = snr;
2358 best_variance = variance;
2359 best_sq_error = sq_error;
2360 }
2361 }
2362
2363 *variance_acc += best_variance;
2364 *sq_error_acc += best_sq_error;
2365}
2366
2367// Used to test a multitude of sample rate and channel combinations. It works
2368// by first producing a set of reference files (in SetUpTestCase) that are
2369// assumed to be correct, as the used parameters are verified by other tests
2370// in this collection. Primarily the reference files are all produced at
2371// "native" rates which do not involve any resampling.
2372
2373// Each test pass produces an output file with a particular format. The output
2374// is matched against the reference file closest to its internal processing
2375// format. If necessary the output is resampled back to its process format.
2376// Due to the resampling distortion, we don't expect identical results, but
2377// enforce SNR thresholds which vary depending on the format. 0 is a special
2378// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002379typedef std::tr1::tuple<int, int, int, int, double, double>
2380 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002381class AudioProcessingTest
2382 : public testing::TestWithParam<AudioProcessingTestData> {
2383 public:
2384 AudioProcessingTest()
2385 : input_rate_(std::tr1::get<0>(GetParam())),
2386 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002387 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2388 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2389 expected_snr_(std::tr1::get<4>(GetParam())),
2390 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002391
2392 virtual ~AudioProcessingTest() {}
2393
2394 static void SetUpTestCase() {
2395 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002396 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002397 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002398 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2399 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2400 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002401 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002402 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2403 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2404 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002405 }
2406 }
2407 }
2408 }
2409
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002410 static void TearDownTestCase() {
2411 ClearTempFiles();
2412 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002413
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002414 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002415 // to a file specified with |output_file_prefix|. Both forward and reverse
2416 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002417 static void ProcessFormat(int input_rate,
2418 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002419 int reverse_input_rate,
2420 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002421 size_t num_input_channels,
2422 size_t num_output_channels,
2423 size_t num_reverse_input_channels,
2424 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002425 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002426 Config config;
2427 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002428 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002430
ekmeyerson60d9b332015-08-14 10:35:55 -07002431 ProcessingConfig processing_config = {
2432 {{input_rate, num_input_channels},
2433 {output_rate, num_output_channels},
2434 {reverse_input_rate, num_reverse_input_channels},
2435 {reverse_output_rate, num_reverse_output_channels}}};
2436 ap->Initialize(processing_config);
2437
2438 FILE* far_file =
2439 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002440 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002441 FILE* out_file =
2442 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2443 reverse_input_rate, reverse_output_rate,
2444 num_input_channels, num_output_channels,
2445 num_reverse_input_channels,
2446 num_reverse_output_channels, kForward).c_str(),
2447 "wb");
2448 FILE* rev_out_file =
2449 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2450 reverse_input_rate, reverse_output_rate,
2451 num_input_channels, num_output_channels,
2452 num_reverse_input_channels,
2453 num_reverse_output_channels, kReverse).c_str(),
2454 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002455 ASSERT_TRUE(far_file != NULL);
2456 ASSERT_TRUE(near_file != NULL);
2457 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002458 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002459
2460 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2461 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002462 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2463 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002464 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2465 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002466 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2467 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002468
2469 // Temporary buffers.
2470 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002471 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2472 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002473 std::unique_ptr<float[]> float_data(new float[max_length]);
2474 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002475
2476 int analog_level = 127;
2477 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2478 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002479 EXPECT_NOERR(ap->ProcessReverseStream(
2480 rev_cb.channels(), processing_config.reverse_input_stream(),
2481 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002482
2483 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2484 ap->echo_cancellation()->set_stream_drift_samples(0);
2485 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2486
2487 EXPECT_NOERR(ap->ProcessStream(
2488 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002489 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002490 input_rate,
2491 LayoutFromChannels(num_input_channels),
2492 output_rate,
2493 LayoutFromChannels(num_output_channels),
2494 out_cb.channels()));
2495
ekmeyerson60d9b332015-08-14 10:35:55 -07002496 // Dump forward output to file.
2497 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002498 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002499 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002500
pkasting25702cb2016-01-08 13:50:27 -08002501 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002502 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002503 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002504
ekmeyerson60d9b332015-08-14 10:35:55 -07002505 // Dump reverse output to file.
2506 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2507 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002508 size_t rev_out_length =
2509 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002510
pkasting25702cb2016-01-08 13:50:27 -08002511 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002512 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2513 rev_out_file));
2514
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002515 analog_level = ap->gain_control()->stream_analog_level();
2516 }
2517 fclose(far_file);
2518 fclose(near_file);
2519 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002520 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002521 }
2522
2523 protected:
2524 int input_rate_;
2525 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002526 int reverse_input_rate_;
2527 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002528 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002529 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002530};
2531
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002532TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002533 struct ChannelFormat {
2534 int num_input;
2535 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002536 int num_reverse_input;
2537 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002538 };
2539 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002540 {1, 1, 1, 1},
2541 {1, 1, 2, 1},
2542 {2, 1, 1, 1},
2543 {2, 1, 2, 1},
2544 {2, 2, 1, 1},
2545 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002546 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002547
pkasting25702cb2016-01-08 13:50:27 -08002548 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002549 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2550 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2551 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002552
ekmeyerson60d9b332015-08-14 10:35:55 -07002553 // Verify output for both directions.
2554 std::vector<StreamDirection> stream_directions;
2555 stream_directions.push_back(kForward);
2556 stream_directions.push_back(kReverse);
2557 for (StreamDirection file_direction : stream_directions) {
2558 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2559 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2560 const int out_num =
2561 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2562 const double expected_snr =
2563 file_direction ? expected_reverse_snr_ : expected_snr_;
2564
2565 const int min_ref_rate = std::min(in_rate, out_rate);
2566 int ref_rate;
2567
2568 if (min_ref_rate > 32000) {
2569 ref_rate = 48000;
2570 } else if (min_ref_rate > 16000) {
2571 ref_rate = 32000;
2572 } else if (min_ref_rate > 8000) {
2573 ref_rate = 16000;
2574 } else {
2575 ref_rate = 8000;
2576 }
aluebs776593b2016-03-15 14:04:58 -07002577#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002578 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002579 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002580 }
2581#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002582 FILE* out_file = fopen(
2583 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2584 reverse_output_rate_, cf[i].num_input,
2585 cf[i].num_output, cf[i].num_reverse_input,
2586 cf[i].num_reverse_output, file_direction).c_str(),
2587 "rb");
2588 // The reference files always have matching input and output channels.
2589 FILE* ref_file = fopen(
2590 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2591 cf[i].num_output, cf[i].num_output,
2592 cf[i].num_reverse_output, cf[i].num_reverse_output,
2593 file_direction).c_str(),
2594 "rb");
2595 ASSERT_TRUE(out_file != NULL);
2596 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002597
pkasting25702cb2016-01-08 13:50:27 -08002598 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2599 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002600 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002601 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002602 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002603 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002604 // Data from the resampled output, in case the reference and output rates
2605 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002606 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002607
ekmeyerson60d9b332015-08-14 10:35:55 -07002608 PushResampler<float> resampler;
2609 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002610
ekmeyerson60d9b332015-08-14 10:35:55 -07002611 // Compute the resampling delay of the output relative to the reference,
2612 // to find the region over which we should search for the best SNR.
2613 float expected_delay_sec = 0;
2614 if (in_rate != ref_rate) {
2615 // Input resampling delay.
2616 expected_delay_sec +=
2617 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2618 }
2619 if (out_rate != ref_rate) {
2620 // Output resampling delay.
2621 expected_delay_sec +=
2622 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2623 // Delay of converting the output back to its processing rate for
2624 // testing.
2625 expected_delay_sec +=
2626 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2627 }
2628 int expected_delay =
2629 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002630
ekmeyerson60d9b332015-08-14 10:35:55 -07002631 double variance = 0;
2632 double sq_error = 0;
2633 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2634 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2635 float* out_ptr = out_data.get();
2636 if (out_rate != ref_rate) {
2637 // Resample the output back to its internal processing rate if
2638 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002639 ASSERT_EQ(ref_length,
2640 static_cast<size_t>(resampler.Resample(
2641 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002642 out_ptr = cmp_data.get();
2643 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002644
ekmeyerson60d9b332015-08-14 10:35:55 -07002645 // Update the |sq_error| and |variance| accumulators with the highest
2646 // SNR of reference vs output.
2647 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2648 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002649 }
2650
ekmeyerson60d9b332015-08-14 10:35:55 -07002651 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2652 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2653 << cf[i].num_input << ", " << cf[i].num_output << ", "
2654 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2655 << ", " << file_direction << "): ";
2656 if (sq_error > 0) {
2657 double snr = 10 * log10(variance / sq_error);
2658 EXPECT_GE(snr, expected_snr);
2659 EXPECT_NE(0, expected_snr);
2660 std::cout << "SNR=" << snr << " dB" << std::endl;
2661 } else {
aluebs776593b2016-03-15 14:04:58 -07002662 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002663 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002664
ekmeyerson60d9b332015-08-14 10:35:55 -07002665 fclose(out_file);
2666 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002667 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002668 }
2669}
2670
2671#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2672INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002673 CommonFormats,
2674 AudioProcessingTest,
2675 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002676 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2677 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002678 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2679 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2680 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2681 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2682 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2683 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2684 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2685 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2686 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002687
ekmeyerson60d9b332015-08-14 10:35:55 -07002688 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2689 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2690 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2691 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2692 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2693 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2694 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2695 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2696 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2697 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2698 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2699 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002700
ekmeyerson60d9b332015-08-14 10:35:55 -07002701 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2702 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2703 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2704 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2705 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2706 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2707 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2708 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2709 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2710 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2711 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2712 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002713
ekmeyerson60d9b332015-08-14 10:35:55 -07002714 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2715 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2716 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2717 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2718 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2719 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2720 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2721 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2722 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2723 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002724 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002725 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002726
2727#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2728INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002729 CommonFormats,
2730 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002731 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2732 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2733 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2734 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2735 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2736 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002737 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2738 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2739 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2740 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2741 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2742 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002743
aluebs776593b2016-03-15 14:04:58 -07002744 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2745 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2746 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002747 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2748 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2749 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2750 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2751 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2752 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2753 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2754 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2755 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002756
aluebs776593b2016-03-15 14:04:58 -07002757 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2758 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2759 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2760 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2761 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2762 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2763 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2764 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2765 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002766 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2767 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2768 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002769
ekmeyerson60d9b332015-08-14 10:35:55 -07002770 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2771 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2772 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2773 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2774 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2775 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2776 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2777 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2778 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2779 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002780 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002781 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002782#endif
2783
niklase@google.com470e71d2011-07-07 08:21:25 +00002784} // namespace
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002785} // namespace webrtc