aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_processing/utility
diff options
context:
space:
mode:
authorbjornv@webrtc.org <bjornv@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d>2013-01-25 15:53:41 +0000
committerbjornv@webrtc.org <bjornv@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d>2013-01-25 15:53:41 +0000
commit94c213af1af7fc545ed49cb4854446d267bb5417 (patch)
tree86f88b122e062bc3a56c813fe0dec69981c40f7f /webrtc/modules/audio_processing/utility
parent59d209562ff18fdaa9ccc55ea630d7c7d4c91dca (diff)
downloadwebrtc-94c213af1af7fc545ed49cb4854446d267bb5417.tar.gz
Separated far-end handling in BinaryDelayEstimator.
This CL is one step in a larger change of the DelayEstimator where we will open up for multiple near-end signals. This particular CL separates the low level far-end parts without affecting the usage externally. This is a first step towards separating the far-end and near-end parts giving the user the control. BUG=None TEST=audioproc_unittests, trybots Review URL: https://webrtc-codereview.appspot.com/1068005 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3415 4adac7df-926f-26a2-2b94-8c16560cd09d
Diffstat (limited to 'webrtc/modules/audio_processing/utility')
-rw-r--r--webrtc/modules/audio_processing/utility/delay_estimator.c129
-rw-r--r--webrtc/modules/audio_processing/utility/delay_estimator.h126
-rw-r--r--webrtc/modules/audio_processing/utility/delay_estimator_internal.h3
-rw-r--r--webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc46
-rw-r--r--webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c18
5 files changed, 222 insertions, 100 deletions
diff --git a/webrtc/modules/audio_processing/utility/delay_estimator.c b/webrtc/modules/audio_processing/utility/delay_estimator.c
index 59909c12b7..6bc594e993 100644
--- a/webrtc/modules/audio_processing/utility/delay_estimator.c
+++ b/webrtc/modules/audio_processing/utility/delay_estimator.c
@@ -59,6 +59,68 @@ static void BitCountComparison(uint32_t binary_vector,
}
}
+void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
+
+ if (self == NULL) {
+ return;
+ }
+
+ free(self->binary_far_history);
+ self->binary_far_history = NULL;
+
+ free(self->far_bit_counts);
+ self->far_bit_counts = NULL;
+
+ free(self);
+}
+
+BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
+ int history_size) {
+ BinaryDelayEstimatorFarend* self = malloc(sizeof(BinaryDelayEstimatorFarend));
+
+ assert(history_size > 1);
+ if (self != NULL) {
+ int malloc_fail = 0;
+
+ self->history_size = history_size;
+
+ // Allocate memory for history buffers.
+ self->binary_far_history = malloc(history_size * sizeof(uint32_t));
+ malloc_fail |= (self->binary_far_history == NULL);
+
+ self->far_bit_counts = malloc(history_size * sizeof(int));
+ malloc_fail |= (self->far_bit_counts == NULL);
+
+ if (malloc_fail) {
+ WebRtc_FreeBinaryDelayEstimatorFarend(self);
+ self = NULL;
+ }
+ }
+
+ return self;
+}
+
+void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self) {
+ assert(self != NULL);
+ memset(self->binary_far_history, 0, sizeof(uint32_t) * self->history_size);
+ memset(self->far_bit_counts, 0, sizeof(int) * self->history_size);
+}
+
+void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* handle,
+ uint32_t binary_far_spectrum) {
+ assert(handle != NULL);
+ // Shift binary spectrum history and insert current |binary_far_spectrum|.
+ memmove(&(handle->binary_far_history[1]), &(handle->binary_far_history[0]),
+ (handle->history_size - 1) * sizeof(uint32_t));
+ handle->binary_far_history[0] = binary_far_spectrum;
+
+ // Shift history of far-end binary spectrum bit counts and insert bit count
+ // of current |binary_far_spectrum|.
+ memmove(&(handle->far_bit_counts[1]), &(handle->far_bit_counts[0]),
+ (handle->history_size - 1) * sizeof(int));
+ handle->far_bit_counts[0] = BitCount(binary_far_spectrum);
+}
+
void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* handle) {
if (handle == NULL) {
@@ -71,24 +133,21 @@ void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* handle) {
free(handle->bit_counts);
handle->bit_counts = NULL;
- free(handle->binary_far_history);
- handle->binary_far_history = NULL;
-
free(handle->binary_near_history);
handle->binary_near_history = NULL;
- free(handle->far_bit_counts);
- handle->far_bit_counts = NULL;
+ // BinaryDelayEstimator does not have ownership of |farend|, hence we do not
+ // free the memory here. That should be handled separately by the user.
+ handle->farend = NULL;
free(handle);
}
-BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(int max_delay,
- int lookahead) {
+BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
+ BinaryDelayEstimatorFarend* farend, int lookahead) {
BinaryDelayEstimator* self = NULL;
- int history_size = max_delay + lookahead; // Must be > 1 for buffer shifting.
- if ((max_delay >= 0) && (lookahead >= 0) && (history_size > 1)) {
+ if ((farend != NULL) && (lookahead >= 0)) {
// Sanity conditions fulfilled.
self = malloc(sizeof(BinaryDelayEstimator));
}
@@ -96,32 +155,20 @@ BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(int max_delay,
if (self != NULL) {
int malloc_fail = 0;
- self->mean_bit_counts = NULL;
- self->bit_counts = NULL;
- self->binary_far_history = NULL;
- self->far_bit_counts = NULL;
- self->binary_near_history = NULL;
-
- self->history_size = history_size;
+ self->farend = farend;
self->near_history_size = lookahead + 1;
// Allocate memory for spectrum buffers.
- self->mean_bit_counts = malloc(history_size * sizeof(int32_t));
+ self->mean_bit_counts = malloc(farend->history_size * sizeof(int32_t));
malloc_fail |= (self->mean_bit_counts == NULL);
- self->bit_counts = malloc(history_size * sizeof(int32_t));
+ self->bit_counts = malloc(farend->history_size * sizeof(int32_t));
malloc_fail |= (self->bit_counts == NULL);
// Allocate memory for history buffers.
- self->binary_far_history = malloc(history_size * sizeof(uint32_t));
- malloc_fail |= (self->binary_far_history == NULL);
-
self->binary_near_history = malloc((lookahead + 1) * sizeof(uint32_t));
malloc_fail |= (self->binary_near_history == NULL);
- self->far_bit_counts = malloc(history_size * sizeof(int));
- malloc_fail |= (self->far_bit_counts == NULL);
-
if (malloc_fail) {
WebRtc_FreeBinaryDelayEstimator(self);
self = NULL;
@@ -135,13 +182,10 @@ void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* handle) {
int i = 0;
assert(handle != NULL);
- memset(handle->bit_counts, 0, sizeof(int32_t) * handle->history_size);
- memset(handle->binary_far_history, 0,
- sizeof(uint32_t) * handle->history_size);
+ memset(handle->bit_counts, 0, sizeof(int32_t) * handle->farend->history_size);
memset(handle->binary_near_history, 0,
sizeof(uint32_t) * handle->near_history_size);
- memset(handle->far_bit_counts, 0, sizeof(int) * handle->history_size);
- for (i = 0; i < handle->history_size; ++i) {
+ for (i = 0; i < handle->farend->history_size; ++i) {
handle->mean_bit_counts[i] = (20 << 9); // 20 in Q9.
}
handle->minimum_probability = (32 << 9); // 32 in Q9.
@@ -151,21 +195,6 @@ void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* handle) {
handle->last_delay = -2;
}
-void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimator* handle,
- uint32_t binary_far_spectrum) {
- assert(handle != NULL);
- // Shift binary spectrum history and insert current |binary_far_spectrum|.
- memmove(&(handle->binary_far_history[1]), &(handle->binary_far_history[0]),
- (handle->history_size - 1) * sizeof(uint32_t));
- handle->binary_far_history[0] = binary_far_spectrum;
-
- // Shift history of far-end binary spectrum bit counts and insert bit count
- // of current |binary_far_spectrum|.
- memmove(&(handle->far_bit_counts[1]), &(handle->far_bit_counts[0]),
- (handle->history_size - 1) * sizeof(int));
- handle->far_bit_counts[0] = BitCount(binary_far_spectrum);
-}
-
int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* handle,
uint32_t binary_near_spectrum) {
int i = 0;
@@ -188,12 +217,12 @@ int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* handle,
// Compare with delayed spectra and store the |bit_counts| for each delay.
BitCountComparison(binary_near_spectrum,
- handle->binary_far_history,
- handle->history_size,
+ handle->farend->binary_far_history,
+ handle->farend->history_size,
handle->bit_counts);
// Update |mean_bit_counts|, which is the smoothed version of |bit_counts|.
- for (i = 0; i < handle->history_size; i++) {
+ for (i = 0; i < handle->farend->history_size; i++) {
// |bit_counts| is constrained to [0, 32], meaning we can smooth with a
// factor up to 2^26. We use Q9.
int32_t bit_count = (handle->bit_counts[i] << 9); // Q9.
@@ -201,17 +230,17 @@ int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* handle,
// Update |mean_bit_counts| only when far-end signal has something to
// contribute. If |far_bit_counts| is zero the far-end signal is weak and
// we likely have a poor echo condition, hence don't update.
- if (handle->far_bit_counts[i] > 0) {
+ if (handle->farend->far_bit_counts[i] > 0) {
// Make number of right shifts piecewise linear w.r.t. |far_bit_counts|.
int shifts = kShiftsAtZero;
- shifts -= (kShiftsLinearSlope * handle->far_bit_counts[i]) >> 4;
+ shifts -= (kShiftsLinearSlope * handle->farend->far_bit_counts[i]) >> 4;
WebRtc_MeanEstimatorFix(bit_count, shifts, &(handle->mean_bit_counts[i]));
}
}
// Find |candidate_delay|, |value_best_candidate| and |value_worst_candidate|
// of |mean_bit_counts|.
- for (i = 0; i < handle->history_size; i++) {
+ for (i = 0; i < handle->farend->history_size; i++) {
if (handle->mean_bit_counts[i] < value_best_candidate) {
value_best_candidate = handle->mean_bit_counts[i];
candidate_delay = i;
diff --git a/webrtc/modules/audio_processing/utility/delay_estimator.h b/webrtc/modules/audio_processing/utility/delay_estimator.h
index da46e72863..94929b602b 100644
--- a/webrtc/modules/audio_processing/utility/delay_estimator.h
+++ b/webrtc/modules/audio_processing/utility/delay_estimator.h
@@ -18,15 +18,21 @@
typedef struct {
// Pointer to bit counts.
- int32_t* mean_bit_counts;
int* far_bit_counts;
+ // Binary history variables.
+ uint32_t* binary_far_history;
+ // Buffer size.
+ int history_size;
+} BinaryDelayEstimatorFarend;
+typedef struct {
+ // Pointer to bit counts.
+ int32_t* mean_bit_counts;
// Array only used locally in ProcessBinarySpectrum() but whose size is
// determined at run-time.
int32_t* bit_counts;
// Binary history variables.
- uint32_t* binary_far_history;
uint32_t* binary_near_history;
// Delay estimation variables.
@@ -36,14 +42,69 @@ typedef struct {
// Delay memory.
int last_delay;
- // Buffer size.
- int history_size;
-
// Near-end buffer size.
int near_history_size;
+ // Far-end binary spectrum history buffer etc.
+ BinaryDelayEstimatorFarend* farend;
} BinaryDelayEstimator;
+// Releases the memory allocated by
+// WebRtc_CreateBinaryDelayEstimatorFarend(...).
+// Input:
+// - self : Pointer to the binary delay estimation far-end
+// instance which is the return value of
+// WebRtc_CreateBinaryDelayEstimatorFarend().
+//
+void WebRtc_FreeBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self);
+
+// Allocates the memory needed by the far-end part of the binary delay
+// estimation. The memory needs to be initialized separately through
+// WebRtc_InitBinaryDelayEstimatorFarend(...).
+//
+// Inputs:
+// - history_size : Size of the far-end binary spectrum history.
+//
+// Return value:
+// - BinaryDelayEstimatorFarend*
+// : Created |handle|. If the memory can't be allocated
+// or if any of the input parameters are invalid NULL
+// is returned.
+//
+BinaryDelayEstimatorFarend* WebRtc_CreateBinaryDelayEstimatorFarend(
+ int history_size);
+
+// Initializes the delay estimation far-end instance created with
+// WebRtc_CreateBinaryDelayEstimatorFarend(...).
+//
+// Input:
+// - self : Pointer to the delay estimation far-end instance.
+//
+// Output:
+// - self : Initialized far-end instance.
+//
+void WebRtc_InitBinaryDelayEstimatorFarend(BinaryDelayEstimatorFarend* self);
+
+// Adds the binary far-end spectrum to the internal far-end history buffer. This
+// spectrum is used as reference when calculating the delay using
+// WebRtc_ProcessBinarySpectrum().
+//
+// Inputs:
+// - handle : Pointer to the delay estimation far-end
+// instance.
+// - binary_far_spectrum : Far-end binary spectrum.
+//
+// Output:
+// - handle : Updated far-end instance.
+//
+void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimatorFarend* handle,
+ uint32_t binary_far_spectrum);
+
// Releases the memory allocated by WebRtc_CreateBinaryDelayEstimator(...).
+//
+// Note that BinaryDelayEstimator utilizes BinaryDelayEstimatorFarend, but does
+// not take ownership of it, hence the BinaryDelayEstimator has to be torn down
+// before the far-end.
+//
// Input:
// - handle : Pointer to the binary delay estimation instance
// which is the return value of
@@ -51,12 +112,45 @@ typedef struct {
//
void WebRtc_FreeBinaryDelayEstimator(BinaryDelayEstimator* handle);
-// Refer to WebRtc_CreateDelayEstimator() in delay_estimator_wrapper.h.
-BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(int max_delay,
- int lookahead);
+// Allocates the memory needed by the binary delay estimation. The memory needs
+// to be initialized separately through WebRtc_InitBinaryDelayEstimator(...).
+//
+// Inputs:
+// - farend : Pointer to the far-end part of the Binary Delay
+// Estimator. This memory has to be created separately
+// prior to this call using
+// WebRtc_CreateBinaryDelayEstimatorFarend().
+//
+// Note that BinaryDelayEstimator does not take
+// ownership of |farend|.
+//
+// - lookahead : Amount of non-causal lookahead to use. This can
+// detect cases in which a near-end signal occurs before
+// the corresponding far-end signal. It will delay the
+// estimate for the current block by an equal amount,
+// and the returned values will be offset by it.
+//
+// A value of zero is the typical no-lookahead case.
+// This also represents the minimum delay which can be
+// estimated.
+//
+// Note that the effective range of delay estimates is
+// [-|lookahead|,... ,|history_size|-|lookahead|)
+// where |history_size| was set upon creating the far-end
+// history buffer size.
+//
+// Return value:
+// - BinaryDelayEstimator*
+// : Created |handle|. If the memory can't be allocated
+// or if any of the input parameters are invalid NULL
+// is returned.
+//
+BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(
+ BinaryDelayEstimatorFarend* farend, int lookahead);
// Initializes the delay estimation instance created with
// WebRtc_CreateBinaryDelayEstimator(...).
+//
// Input:
// - handle : Pointer to the delay estimation instance.
//
@@ -65,24 +159,12 @@ BinaryDelayEstimator* WebRtc_CreateBinaryDelayEstimator(int max_delay,
//
void WebRtc_InitBinaryDelayEstimator(BinaryDelayEstimator* handle);
-// Adds the binary far-end spectrum to the internal buffer. This spectrum is
-// used as reference when calculating the delay using
-// WebRtc_ProcessBinarySpectrum().
-// Inputs:
-// - handle : Pointer to the delay estimation instance.
-// - binary_far_spectrum : Far-end binary spectrum.
-//
-// Output:
-// - handle : Updated instance.
-//
-void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimator* handle,
- uint32_t binary_far_spectrum);
-
// Estimates and returns the delay between the binary far-end and binary near-
// end spectra. It is assumed the binary far-end spectrum has been added using
// WebRtc_AddBinaryFarSpectrum() prior to this call. The value will be offset by
// the lookahead (i.e. the lookahead should be subtracted from the returned
// value).
+//
// Inputs:
// - handle : Pointer to the delay estimation instance.
// - binary_near_spectrum : Near-end binary spectrum of the current block.
@@ -92,7 +174,6 @@ void WebRtc_AddBinaryFarSpectrum(BinaryDelayEstimator* handle,
//
// Return value:
// - delay : >= 0 - Calculated delay value.
-// -1 - Error.
// -2 - Insufficient data for estimation.
//
int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* handle,
@@ -106,7 +187,6 @@ int WebRtc_ProcessBinarySpectrum(BinaryDelayEstimator* handle,
//
// Return value:
// - delay : >= 0 - Last calculated delay value
-// -1 - Error
// -2 - Insufficient data for estimation.
//
int WebRtc_binary_last_delay(BinaryDelayEstimator* handle);
diff --git a/webrtc/modules/audio_processing/utility/delay_estimator_internal.h b/webrtc/modules/audio_processing/utility/delay_estimator_internal.h
index 46b19cae26..43bbc4d012 100644
--- a/webrtc/modules/audio_processing/utility/delay_estimator_internal.h
+++ b/webrtc/modules/audio_processing/utility/delay_estimator_internal.h
@@ -33,6 +33,9 @@ typedef struct {
// Binary spectrum based delay estimator
BinaryDelayEstimator* binary_handle;
+ // TODO(bjornv): This is an intermediate member variable. To be removed when
+ // we complete full support.
+ BinaryDelayEstimatorFarend* binary_farend;
} DelayEstimator;
#endif // WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_DELAY_ESTIMATOR_INTERNAL_H_
diff --git a/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc b/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc
index 43504effbb..28e6c4739b 100644
--- a/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc
+++ b/webrtc/modules/audio_processing/utility/delay_estimator_unittest.cc
@@ -23,6 +23,8 @@ enum { kSpectrumSize = 65 };
// Delay history sizes.
enum { kMaxDelay = 100 };
enum { kLookahead = 10 };
+// Length of binary spectrum sequence.
+enum { kSequenceLength = 400 };
class DelayEstimatorTest : public ::testing::Test {
protected:
@@ -31,12 +33,12 @@ class DelayEstimatorTest : public ::testing::Test {
virtual void TearDown();
void Init();
-
void InitBinary();
void* handle_;
DelayEstimator* self_;
BinaryDelayEstimator* binary_handle_;
+ BinaryDelayEstimatorFarend* binary_farend_handle_;
int spectrum_size_;
// Dummy input spectra.
float far_f_[kSpectrumSize];
@@ -49,6 +51,7 @@ DelayEstimatorTest::DelayEstimatorTest()
: handle_(NULL),
self_(NULL),
binary_handle_(NULL),
+ binary_farend_handle_(NULL),
spectrum_size_(kSpectrumSize) {
// Dummy input data are set with more or less arbitrary non-zero values.
memset(far_f_, 1, sizeof(far_f_));
@@ -61,14 +64,22 @@ void DelayEstimatorTest::SetUp() {
handle_ = WebRtc_CreateDelayEstimator(kSpectrumSize, kMaxDelay, kLookahead);
ASSERT_TRUE(handle_ != NULL);
self_ = reinterpret_cast<DelayEstimator*>(handle_);
- binary_handle_ = self_->binary_handle;
+ binary_farend_handle_ = WebRtc_CreateBinaryDelayEstimatorFarend(kMaxDelay +
+ kLookahead);
+ ASSERT_TRUE(binary_farend_handle_ != NULL);
+ binary_handle_ = WebRtc_CreateBinaryDelayEstimator(binary_farend_handle_,
+ kLookahead);
+ ASSERT_TRUE(binary_handle_ != NULL);
}
void DelayEstimatorTest::TearDown() {
WebRtc_FreeDelayEstimator(handle_);
handle_ = NULL;
self_ = NULL;
+ WebRtc_FreeBinaryDelayEstimator(binary_handle_);
binary_handle_ = NULL;
+ WebRtc_FreeBinaryDelayEstimatorFarend(binary_farend_handle_);
+ binary_farend_handle_ = NULL;
}
void DelayEstimatorTest::Init() {
@@ -82,6 +93,8 @@ void DelayEstimatorTest::Init() {
}
void DelayEstimatorTest::InitBinary() {
+ // Initialize Binary Delay Estimator (far-end part).
+ WebRtc_InitBinaryDelayEstimatorFarend(binary_farend_handle_);
// Initialize Binary Delay Estimator
WebRtc_InitBinaryDelayEstimator(binary_handle_);
// Verify initialization. This does not guarantee a complete check, since
@@ -100,9 +113,6 @@ TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfWrapper) {
handle = WebRtc_CreateDelayEstimator(33, kMaxDelay, kLookahead);
EXPECT_TRUE(handle == NULL);
handle = handle_;
- handle = WebRtc_CreateDelayEstimator(kSpectrumSize, -1, kLookahead);
- EXPECT_TRUE(handle == NULL);
- handle = handle_;
handle = WebRtc_CreateDelayEstimator(kSpectrumSize, kMaxDelay, -1);
EXPECT_TRUE(handle == NULL);
handle = handle_;
@@ -229,30 +239,20 @@ TEST_F(DelayEstimatorTest, CorrectErrorReturnsOfBinaryEstimator) {
// |binary_handle| should be NULL.
// Make sure we have a non-NULL value at start, so we can detect NULL after
// create failure.
- binary_handle = WebRtc_CreateBinaryDelayEstimator(-1, kLookahead);
+ binary_handle = WebRtc_CreateBinaryDelayEstimator(NULL, kLookahead);
EXPECT_TRUE(binary_handle == NULL);
binary_handle = binary_handle_;
- binary_handle = WebRtc_CreateBinaryDelayEstimator(kMaxDelay, -1);
+ binary_handle = WebRtc_CreateBinaryDelayEstimator(binary_farend_handle_, -1);
EXPECT_TRUE(binary_handle == NULL);
binary_handle = binary_handle_;
binary_handle = WebRtc_CreateBinaryDelayEstimator(0, 0);
EXPECT_TRUE(binary_handle == NULL);
-
- // TODO(bjornv): It is not feasible to force an error of
- // WebRtc_ProcessBinarySpectrum(). This can only happen if we have more than
- // 32 bits in our binary spectrum comparison, which by definition can't
- // happen.
- // We should therefore remove that option from the code.
-
- // WebRtc_binary_last_delay() can't return -1 either.
}
TEST_F(DelayEstimatorTest, MeanEstimatorFix) {
// In this test we verify that we update the mean value in correct direction
// only. With "direction" we mean increase or decrease.
- InitBinary();
-
int32_t mean_value = 4000;
int32_t mean_value_before = mean_value;
int32_t new_mean_value = mean_value * 2;
@@ -275,12 +275,11 @@ TEST_F(DelayEstimatorTest, ExactDelayEstimate) {
// the signal accordingly. We verify both causal and non-causal delays.
// Construct a sequence of binary spectra used to verify delay estimate. The
- // |sequence_length| has to be long enough for the delay estimation to leave
+ // |kSequenceLength| has to be long enough for the delay estimation to leave
// the initialized state.
- const int sequence_length = 400;
- uint32_t binary_spectrum[sequence_length + kMaxDelay + kLookahead];
+ uint32_t binary_spectrum[kSequenceLength + kMaxDelay + kLookahead];
binary_spectrum[0] = 1;
- for (int i = 1; i < (sequence_length + kMaxDelay + kLookahead); i++) {
+ for (int i = 1; i < (kSequenceLength + kMaxDelay + kLookahead); i++) {
binary_spectrum[i] = 3 * binary_spectrum[i - 1];
}
@@ -290,8 +289,9 @@ TEST_F(DelayEstimatorTest, ExactDelayEstimate) {
// the far-end sequence.
for (int offset = -kLookahead; offset < kMaxDelay; offset++) {
InitBinary();
- for (int i = kLookahead; i < (sequence_length + kLookahead); i++) {
- WebRtc_AddBinaryFarSpectrum(binary_handle_, binary_spectrum[i + offset]);
+ for (int i = kLookahead; i < (kSequenceLength + kLookahead); i++) {
+ WebRtc_AddBinaryFarSpectrum(binary_farend_handle_,
+ binary_spectrum[i + offset]);
int delay = WebRtc_ProcessBinarySpectrum(binary_handle_,
binary_spectrum[i]);
diff --git a/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c b/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c
index 3a6f5b11e9..a00923f667 100644
--- a/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c
+++ b/webrtc/modules/audio_processing/utility/delay_estimator_wrapper.c
@@ -138,19 +138,23 @@ void WebRtc_FreeDelayEstimator(void* handle) {
WebRtc_FreeBinaryDelayEstimator(self->binary_handle);
self->binary_handle = NULL;
+ WebRtc_FreeBinaryDelayEstimatorFarend(self->binary_farend);
+ self->binary_farend = NULL;
+
free(self);
}
void* WebRtc_CreateDelayEstimator(int spectrum_size, int max_delay,
int lookahead) {
DelayEstimator* self = NULL;
+ const int history_size = max_delay + lookahead; // For buffer shifting: > 1
// TODO(bjornv): Make this a static assert.
// Check if the sub band used in the delay estimation is small enough to fit
// the binary spectra in a uint32_t.
assert(kBandLast - kBandFirst < 32);
- if (spectrum_size >= kBandLast) {
+ if ((spectrum_size >= kBandLast) && (history_size > 1)) {
self = malloc(sizeof(DelayEstimator));
}
@@ -159,8 +163,12 @@ void* WebRtc_CreateDelayEstimator(int spectrum_size, int max_delay,
self->mean_far_spectrum = NULL;
self->mean_near_spectrum = NULL;
+ self->binary_farend = NULL;
+
+ // Allocate memory for the farend spectrum handling.
+ self->binary_farend = WebRtc_CreateBinaryDelayEstimatorFarend(history_size);
- self->binary_handle = WebRtc_CreateBinaryDelayEstimator(max_delay,
+ self->binary_handle = WebRtc_CreateBinaryDelayEstimator(self->binary_farend,
lookahead);
memory_fail |= (self->binary_handle == NULL);
@@ -189,6 +197,8 @@ int WebRtc_InitDelayEstimator(void* handle) {
return -1;
}
+ // Initialize far-end part of binary delay estimator.
+ WebRtc_InitBinaryDelayEstimatorFarend(self->binary_farend);
// Initialize binary delay estimator.
WebRtc_InitBinaryDelayEstimator(self->binary_handle);
@@ -248,7 +258,7 @@ int WebRtc_DelayEstimatorProcessFix(void* handle,
near_q,
&(self->near_spectrum_initialized));
- WebRtc_AddBinaryFarSpectrum(self->binary_handle, binary_far_spectrum);
+ WebRtc_AddBinaryFarSpectrum(self->binary_handle->farend, binary_far_spectrum);
return WebRtc_ProcessBinarySpectrum(self->binary_handle,
binary_near_spectrum);
@@ -286,7 +296,7 @@ int WebRtc_DelayEstimatorProcessFloat(void* handle,
BinarySpectrumFloat(near_spectrum, self->mean_near_spectrum,
&(self->near_spectrum_initialized));
- WebRtc_AddBinaryFarSpectrum(self->binary_handle, binary_far_spectrum);
+ WebRtc_AddBinaryFarSpectrum(self->binary_handle->farend, binary_far_spectrum);
return WebRtc_ProcessBinarySpectrum(self->binary_handle,
binary_near_spectrum);