2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/modules/audio_processing/utility/delay_estimator.h"
15 #include "webrtc/modules/audio_processing/utility/delay_estimator_internal.h"
16 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h"
18 #include "webrtc/typedefs.h"
22 enum { kSpectrumSize = 65 };
23 // Delay history sizes.
24 enum { kMaxDelay = 100 };
25 enum { kLookahead = 10 };
26 // Length of binary spectrum sequence.
27 enum { kSequenceLength = 400 };
29 const int kEnable[] = { 0, 1 };
30 const size_t kSizeEnable = sizeof(kEnable) / sizeof(*kEnable);
32 class DelayEstimatorTest : public ::testing::Test {
36 virtual void TearDown();
40 void VerifyDelay(BinaryDelayEstimator* binary_handle, int offset, int delay);
41 void RunBinarySpectra(BinaryDelayEstimator* binary1,
42 BinaryDelayEstimator* binary2,
43 int near_offset, int lookahead_offset, int far_offset);
44 void RunBinarySpectraTest(int near_offset, int lookahead_offset,
45 int ref_robust_validation, int robust_validation);
48 DelayEstimator* self_;
50 DelayEstimatorFarend* farend_self_;
51 BinaryDelayEstimator* binary_;
52 BinaryDelayEstimatorFarend* binary_farend_;
54 // Dummy input spectra.
55 float far_f_[kSpectrumSize];
56 float near_f_[kSpectrumSize];
57 uint16_t far_u16_[kSpectrumSize];
58 uint16_t near_u16_[kSpectrumSize];
59 uint32_t binary_spectrum_[kSequenceLength + kMaxDelay + kLookahead];
62 DelayEstimatorTest::DelayEstimatorTest()
69 spectrum_size_(kSpectrumSize) {
70 // Dummy input data are set with more or less arbitrary non-zero values.
71 memset(far_f_, 1, sizeof(far_f_));
72 memset(near_f_, 2, sizeof(near_f_));
73 memset(far_u16_, 1, sizeof(far_u16_));
74 memset(near_u16_, 2, sizeof(near_u16_));
75 // Construct a sequence of binary spectra used to verify delay estimate. The
76 // |kSequenceLength| has to be long enough for the delay estimation to leave
77 // the initialized state.
78 binary_spectrum_[0] = 1;
79 for (int i = 1; i < (kSequenceLength + kMaxDelay + kLookahead); i++) {
80 binary_spectrum_[i] = 3 * binary_spectrum_[i - 1];
84 void DelayEstimatorTest::SetUp() {
85 farend_handle_ = WebRtc_CreateDelayEstimatorFarend(kSpectrumSize,
86 kMaxDelay + kLookahead);
87 ASSERT_TRUE(farend_handle_ != NULL);
88 farend_self_ = reinterpret_cast<DelayEstimatorFarend*>(farend_handle_);
89 handle_ = WebRtc_CreateDelayEstimator(farend_handle_, kLookahead);
90 ASSERT_TRUE(handle_ != NULL);
91 self_ = reinterpret_cast<DelayEstimator*>(handle_);
92 binary_farend_ = WebRtc_CreateBinaryDelayEstimatorFarend(kMaxDelay +
94 ASSERT_TRUE(binary_farend_ != NULL);
95 binary_ = WebRtc_CreateBinaryDelayEstimator(binary_farend_, kLookahead);
96 ASSERT_TRUE(binary_ != NULL);
99 void DelayEstimatorTest::TearDown() {
100 WebRtc_FreeDelayEstimator(handle_);
103 WebRtc_FreeDelayEstimatorFarend(farend_handle_);
104 farend_handle_ = NULL;
106 WebRtc_FreeBinaryDelayEstimator(binary_);
108 WebRtc_FreeBinaryDelayEstimatorFarend(binary_farend_);
109 binary_farend_ = NULL;
112 void DelayEstimatorTest::Init() {
113 // Initialize Delay Estimator
114 EXPECT_EQ(0, WebRtc_InitDelayEstimatorFarend(farend_handle_));
115 EXPECT_EQ(0, WebRtc_InitDelayEstimator(handle_));
116 // Verify initialization.
117 EXPECT_EQ(0, farend_self_->far_spectrum_initialized);
118 EXPECT_EQ(0, self_->near_spectrum_initialized);
119 EXPECT_EQ(-2, WebRtc_last_delay(handle_)); // Delay in initial state.
120 EXPECT_FLOAT_EQ(0, WebRtc_last_delay_quality(handle_)); // Zero quality.
123 void DelayEstimatorTest::InitBinary() {
124 // Initialize Binary Delay Estimator (far-end part).
125 WebRtc_InitBinaryDelayEstimatorFarend(binary_farend_);
126 // Initialize Binary Delay Estimator
127 WebRtc_InitBinaryDelayEstimator(binary_);
128 // Verify initialization. This does not guarantee a complete check, since
129 // |last_delay| may be equal to -2 before initialization if done on the fly.
130 EXPECT_EQ(-2, binary_->last_delay);
133 void DelayEstimatorTest::VerifyDelay(BinaryDelayEstimator* binary_handle,
134 int offset, int delay) {
135 // Verify that we WebRtc_binary_last_delay() returns correct delay.
136 EXPECT_EQ(delay, WebRtc_binary_last_delay(binary_handle));
139 // Verify correct delay estimate. In the non-causal case the true delay
140 // is equivalent with the |offset|.
141 EXPECT_EQ(offset, delay);
145 void DelayEstimatorTest::RunBinarySpectra(BinaryDelayEstimator* binary1,
146 BinaryDelayEstimator* binary2,
148 int lookahead_offset,
150 int different_validations = binary1->robust_validation_enabled ^
151 binary2->robust_validation_enabled;
152 WebRtc_InitBinaryDelayEstimatorFarend(binary_farend_);
153 WebRtc_InitBinaryDelayEstimator(binary1);
154 WebRtc_InitBinaryDelayEstimator(binary2);
155 // Verify initialization. This does not guarantee a complete check, since
156 // |last_delay| may be equal to -2 before initialization if done on the fly.
157 EXPECT_EQ(-2, binary1->last_delay);
158 EXPECT_EQ(-2, binary2->last_delay);
159 for (int i = kLookahead; i < (kSequenceLength + kLookahead); i++) {
160 WebRtc_AddBinaryFarSpectrum(binary_farend_,
161 binary_spectrum_[i + far_offset]);
162 int delay_1 = WebRtc_ProcessBinarySpectrum(binary1, binary_spectrum_[i]);
164 WebRtc_ProcessBinarySpectrum(binary2,
165 binary_spectrum_[i - near_offset]);
167 VerifyDelay(binary1, far_offset + kLookahead, delay_1);
169 far_offset + kLookahead + lookahead_offset + near_offset,
171 // Expect the two delay estimates to be offset by |lookahead_offset| +
172 // |near_offset| when we have left the initial state.
173 if ((delay_1 != -2) && (delay_2 != -2)) {
174 EXPECT_EQ(delay_1, delay_2 - lookahead_offset - near_offset);
176 // For the case of identical signals |delay_1| and |delay_2| should match
177 // all the time, unless one of them has robust validation turned on. In
178 // that case the robust validation leaves the initial state faster.
179 if ((near_offset == 0) && (lookahead_offset == 0)) {
180 if (!different_validations) {
181 EXPECT_EQ(delay_1, delay_2);
183 if (binary1->robust_validation_enabled) {
184 EXPECT_GE(delay_1, delay_2);
186 EXPECT_GE(delay_2, delay_1);
191 // Verify that we have left the initialized state.
192 EXPECT_NE(-2, WebRtc_binary_last_delay(binary1));
193 EXPECT_LT(0, WebRtc_binary_last_delay_quality(binary1));
194 EXPECT_NE(-2, WebRtc_binary_last_delay(binary2));
195 EXPECT_LT(0, WebRtc_binary_last_delay_quality(binary2));
198 void DelayEstimatorTest::RunBinarySpectraTest(int near_offset,
199 int lookahead_offset,
200 int ref_robust_validation,
201 int robust_validation) {
202 BinaryDelayEstimator* binary2 =
203 WebRtc_CreateBinaryDelayEstimator(binary_farend_,
204 kLookahead + lookahead_offset);
205 // Verify the delay for both causal and non-causal systems. For causal systems
206 // the delay is equivalent with a positive |offset| of the far-end sequence.
207 // For non-causal systems the delay is equivalent with a negative |offset| of
208 // the far-end sequence.
209 binary_->robust_validation_enabled = ref_robust_validation;
210 binary2->robust_validation_enabled = robust_validation;
211 for (int offset = -kLookahead;
212 offset < kMaxDelay - lookahead_offset - near_offset;
214 RunBinarySpectra(binary_, binary2, near_offset, lookahead_offset, offset);
216 WebRtc_FreeBinaryDelayEstimator(binary2);
218 binary_->robust_validation_enabled = 0; // Reset reference.
221 TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfWrapper) {
222 // In this test we verify correct error returns on invalid API calls.
224 // WebRtc_CreateDelayEstimatorFarend() and WebRtc_CreateDelayEstimator()
225 // should return a NULL pointer on invalid input values.
226 // Make sure we have a non-NULL value at start, so we can detect NULL after
228 void* handle = farend_handle_;
229 handle = WebRtc_CreateDelayEstimatorFarend(33, kMaxDelay + kLookahead);
230 EXPECT_TRUE(handle == NULL);
231 handle = WebRtc_CreateDelayEstimatorFarend(kSpectrumSize, 1);
232 EXPECT_TRUE(handle == NULL);
235 handle = WebRtc_CreateDelayEstimator(NULL, kLookahead);
236 EXPECT_TRUE(handle == NULL);
237 handle = WebRtc_CreateDelayEstimator(farend_handle_, -1);
238 EXPECT_TRUE(handle == NULL);
240 // WebRtc_InitDelayEstimatorFarend() and WebRtc_InitDelayEstimator() should
241 // return -1 if we have a NULL pointer as |handle|.
242 EXPECT_EQ(-1, WebRtc_InitDelayEstimatorFarend(NULL));
243 EXPECT_EQ(-1, WebRtc_InitDelayEstimator(NULL));
245 // WebRtc_AddFarSpectrumFloat() should return -1 if we have:
246 // 1) NULL pointer as |handle|.
247 // 2) NULL pointer as far-end spectrum.
248 // 3) Incorrect spectrum size.
249 EXPECT_EQ(-1, WebRtc_AddFarSpectrumFloat(NULL, far_f_, spectrum_size_));
250 // Use |farend_handle_| which is properly created at SetUp().
251 EXPECT_EQ(-1, WebRtc_AddFarSpectrumFloat(farend_handle_, NULL,
253 EXPECT_EQ(-1, WebRtc_AddFarSpectrumFloat(farend_handle_, far_f_,
254 spectrum_size_ + 1));
256 // WebRtc_AddFarSpectrumFix() should return -1 if we have:
257 // 1) NULL pointer as |handle|.
258 // 2) NULL pointer as far-end spectrum.
259 // 3) Incorrect spectrum size.
260 // 4) Too high precision in far-end spectrum (Q-domain > 15).
261 EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(NULL, far_u16_, spectrum_size_, 0));
262 EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(farend_handle_, NULL, spectrum_size_,
264 EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_,
265 spectrum_size_ + 1, 0));
266 EXPECT_EQ(-1, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_,
267 spectrum_size_, 16));
269 // WebRtc_set_allowed_offset() should return -1 if we have:
270 // 1) NULL pointer as |handle|.
271 // 2) |allowed_offset| < 0.
272 EXPECT_EQ(-1, WebRtc_set_allowed_offset(NULL, 0));
273 EXPECT_EQ(-1, WebRtc_set_allowed_offset(handle_, -1));
275 EXPECT_EQ(-1, WebRtc_get_allowed_offset(NULL));
277 // WebRtc_enable_robust_validation() should return -1 if we have:
278 // 1) NULL pointer as |handle|.
279 // 2) Incorrect |enable| value (not 0 or 1).
280 EXPECT_EQ(-1, WebRtc_enable_robust_validation(NULL, kEnable[0]));
281 EXPECT_EQ(-1, WebRtc_enable_robust_validation(handle_, -1));
282 EXPECT_EQ(-1, WebRtc_enable_robust_validation(handle_, 2));
284 // WebRtc_is_robust_validation_enabled() should return -1 if we have NULL
285 // pointer as |handle|.
286 EXPECT_EQ(-1, WebRtc_is_robust_validation_enabled(NULL));
288 // WebRtc_DelayEstimatorProcessFloat() should return -1 if we have:
289 // 1) NULL pointer as |handle|.
290 // 2) NULL pointer as near-end spectrum.
291 // 3) Incorrect spectrum size.
292 EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(NULL, near_f_,
294 // Use |handle_| which is properly created at SetUp().
295 EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(handle_, NULL,
297 EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFloat(handle_, near_f_,
298 spectrum_size_ + 1));
300 // WebRtc_DelayEstimatorProcessFix() should return -1 if we have:
301 // 1) NULL pointer as |handle|.
302 // 3) NULL pointer as near-end spectrum.
303 // 4) Incorrect spectrum size.
304 // 6) Too high precision in near-end spectrum (Q-domain > 15).
305 EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(NULL, near_u16_, spectrum_size_,
307 EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, NULL, spectrum_size_,
309 EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, near_u16_,
310 spectrum_size_ + 1, 0));
311 EXPECT_EQ(-1, WebRtc_DelayEstimatorProcessFix(handle_, near_u16_,
312 spectrum_size_, 16));
314 // WebRtc_last_delay() should return -1 if we have a NULL pointer as |handle|.
315 EXPECT_EQ(-1, WebRtc_last_delay(NULL));
317 // Free any local memory if needed.
318 WebRtc_FreeDelayEstimator(handle);
321 TEST_F(DelayEstimatorTest, VerifyAllowedOffset) {
322 // Is set to zero by default.
323 EXPECT_EQ(0, WebRtc_get_allowed_offset(handle_));
324 for (int i = 1; i >= 0; i--) {
325 EXPECT_EQ(0, WebRtc_set_allowed_offset(handle_, i));
326 EXPECT_EQ(i, WebRtc_get_allowed_offset(handle_));
328 // Unaffected over a reset.
329 EXPECT_EQ(i, WebRtc_get_allowed_offset(handle_));
333 TEST_F(DelayEstimatorTest, VerifyEnableRobustValidation) {
334 // Disabled by default.
335 EXPECT_EQ(0, WebRtc_is_robust_validation_enabled(handle_));
336 for (size_t i = 0; i < kSizeEnable; ++i) {
337 EXPECT_EQ(0, WebRtc_enable_robust_validation(handle_, kEnable[i]));
338 EXPECT_EQ(kEnable[i], WebRtc_is_robust_validation_enabled(handle_));
340 // Unaffected over a reset.
341 EXPECT_EQ(kEnable[i], WebRtc_is_robust_validation_enabled(handle_));
345 TEST_F(DelayEstimatorTest, InitializedSpectrumAfterProcess) {
346 // In this test we verify that the mean spectra are initialized after first
347 // time we call WebRtc_AddFarSpectrum() and Process() respectively.
349 // For floating point operations, process one frame and verify initialization
352 EXPECT_EQ(0, WebRtc_AddFarSpectrumFloat(farend_handle_, far_f_,
354 EXPECT_EQ(1, farend_self_->far_spectrum_initialized);
355 EXPECT_EQ(-2, WebRtc_DelayEstimatorProcessFloat(handle_, near_f_,
357 EXPECT_EQ(1, self_->near_spectrum_initialized);
359 // For fixed point operations, process one frame and verify initialization
362 EXPECT_EQ(0, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_,
364 EXPECT_EQ(1, farend_self_->far_spectrum_initialized);
365 EXPECT_EQ(-2, WebRtc_DelayEstimatorProcessFix(handle_, near_u16_,
367 EXPECT_EQ(1, self_->near_spectrum_initialized);
370 TEST_F(DelayEstimatorTest, CorrectLastDelay) {
371 // In this test we verify that we get the correct last delay upon valid call.
372 // We simply process the same data until we leave the initialized state
373 // (|last_delay| = -2). Then we compare the Process() output with the
374 // last_delay() call.
376 // TODO(bjornv): Update quality values for robust validation.
378 // Floating point operations.
380 for (int i = 0; i < 200; i++) {
381 EXPECT_EQ(0, WebRtc_AddFarSpectrumFloat(farend_handle_, far_f_,
383 last_delay = WebRtc_DelayEstimatorProcessFloat(handle_, near_f_,
385 if (last_delay != -2) {
386 EXPECT_EQ(last_delay, WebRtc_last_delay(handle_));
387 if (!WebRtc_is_robust_validation_enabled(handle_)) {
388 EXPECT_FLOAT_EQ(7203.f / kMaxBitCountsQ9,
389 WebRtc_last_delay_quality(handle_));
394 // Verify that we have left the initialized state.
395 EXPECT_NE(-2, WebRtc_last_delay(handle_));
396 EXPECT_LT(0, WebRtc_last_delay_quality(handle_));
398 // Fixed point operations.
400 for (int i = 0; i < 200; i++) {
401 EXPECT_EQ(0, WebRtc_AddFarSpectrumFix(farend_handle_, far_u16_,
403 last_delay = WebRtc_DelayEstimatorProcessFix(handle_, near_u16_,
405 if (last_delay != -2) {
406 EXPECT_EQ(last_delay, WebRtc_last_delay(handle_));
407 if (!WebRtc_is_robust_validation_enabled(handle_)) {
408 EXPECT_FLOAT_EQ(7203.f / kMaxBitCountsQ9,
409 WebRtc_last_delay_quality(handle_));
414 // Verify that we have left the initialized state.
415 EXPECT_NE(-2, WebRtc_last_delay(handle_));
416 EXPECT_LT(0, WebRtc_last_delay_quality(handle_));
419 TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfBinaryEstimatorFarend) {
420 // In this test we verify correct output on invalid API calls to the Binary
421 // Delay Estimator (far-end part).
423 BinaryDelayEstimatorFarend* binary = binary_farend_;
424 // WebRtc_CreateBinaryDelayEstimatorFarend() should return -1 if the input
425 // history size is less than 2. This is to make sure the buffer shifting
427 // Make sure we have a non-NULL value at start, so we can detect NULL after
429 binary = WebRtc_CreateBinaryDelayEstimatorFarend(1);
430 EXPECT_TRUE(binary == NULL);
433 TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfBinaryEstimator) {
434 // In this test we verify correct output on invalid API calls to the Binary
437 BinaryDelayEstimator* binary_handle = binary_;
438 // WebRtc_CreateBinaryDelayEstimator() should return -1 if we have a NULL
439 // pointer as |binary_farend| or invalid input values. Upon failure, the
440 // |binary_handle| should be NULL.
441 // Make sure we have a non-NULL value at start, so we can detect NULL after
443 binary_handle = WebRtc_CreateBinaryDelayEstimator(NULL, kLookahead);
444 EXPECT_TRUE(binary_handle == NULL);
445 binary_handle = WebRtc_CreateBinaryDelayEstimator(binary_farend_, -1);
446 EXPECT_TRUE(binary_handle == NULL);
449 TEST_F(DelayEstimatorTest, MeanEstimatorFix) {
450 // In this test we verify that we update the mean value in correct direction
451 // only. With "direction" we mean increase or decrease.
453 int32_t mean_value = 4000;
454 int32_t mean_value_before = mean_value;
455 int32_t new_mean_value = mean_value * 2;
457 // Increasing |mean_value|.
458 WebRtc_MeanEstimatorFix(new_mean_value, 10, &mean_value);
459 EXPECT_LT(mean_value_before, mean_value);
460 EXPECT_GT(new_mean_value, mean_value);
462 // Decreasing |mean_value|.
463 new_mean_value = mean_value / 2;
464 mean_value_before = mean_value;
465 WebRtc_MeanEstimatorFix(new_mean_value, 10, &mean_value);
466 EXPECT_GT(mean_value_before, mean_value);
467 EXPECT_LT(new_mean_value, mean_value);
470 TEST_F(DelayEstimatorTest, ExactDelayEstimateMultipleNearSameSpectrum) {
471 // In this test we verify that we get the correct delay estimates if we shift
472 // the signal accordingly. We create two Binary Delay Estimators and feed them
473 // with the same signals, so they should output the same results.
474 // We verify both causal and non-causal delays.
475 // For these noise free signals, the robust validation should not have an
476 // impact, hence we turn robust validation on/off for both reference and
479 for (size_t i = 0; i < kSizeEnable; ++i) {
480 for (size_t j = 0; j < kSizeEnable; ++j) {
481 RunBinarySpectraTest(0, 0, kEnable[i], kEnable[j]);
486 TEST_F(DelayEstimatorTest, ExactDelayEstimateMultipleNearDifferentSpectrum) {
487 // In this test we use the same setup as above, but we now feed the two Binary
488 // Delay Estimators with different signals, so they should output different
490 // For these noise free signals, the robust validation should not have an
491 // impact, hence we turn robust validation on/off for both reference and
494 const int kNearOffset = 1;
495 for (size_t i = 0; i < kSizeEnable; ++i) {
496 for (size_t j = 0; j < kSizeEnable; ++j) {
497 RunBinarySpectraTest(kNearOffset, 0, kEnable[i], kEnable[j]);
502 TEST_F(DelayEstimatorTest, ExactDelayEstimateMultipleNearDifferentLookahead) {
503 // In this test we use the same setup as above, feeding the two Binary
504 // Delay Estimators with the same signals. The difference is that we create
505 // them with different lookahead.
506 // For these noise free signals, the robust validation should not have an
507 // impact, hence we turn robust validation on/off for both reference and
510 const int kLookaheadOffset = 1;
511 for (size_t i = 0; i < kSizeEnable; ++i) {
512 for (size_t j = 0; j < kSizeEnable; ++j) {
513 RunBinarySpectraTest(0, kLookaheadOffset, kEnable[i], kEnable[j]);
518 TEST_F(DelayEstimatorTest, AllowedOffsetNoImpactWhenRobustValidationDisabled) {
519 // The same setup as in ExactDelayEstimateMultipleNearSameSpectrum with the
520 // difference that |allowed_offset| is set for the reference binary delay
523 binary_->allowed_offset = 10;
524 RunBinarySpectraTest(0, 0, 0, 0);
525 binary_->allowed_offset = 0; // Reset reference.
528 TEST_F(DelayEstimatorTest, VerifyLookaheadAtCreate) {
529 void* farend_handle = WebRtc_CreateDelayEstimatorFarend(kSpectrumSize,
531 ASSERT_TRUE(farend_handle != NULL);
532 void* handle = WebRtc_CreateDelayEstimator(farend_handle, kLookahead);
533 ASSERT_TRUE(handle != NULL);
534 EXPECT_EQ(kLookahead, WebRtc_lookahead(handle));
535 WebRtc_FreeDelayEstimator(handle);
536 WebRtc_FreeDelayEstimatorFarend(farend_handle);
539 // TODO(bjornv): Add tests for SoftReset...(...).