Allow same src and dst in InputAudioFile::DuplicateInterleaved

This change allows the input and output to the static method
InputAudioFile::DuplicateInterleaved to be the same array. That is,
in-place manipulation is now possible. A unit test is also added.

R=tina.legrand@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/20239004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7008 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrik.lundin@webrtc.org 2014-08-29 07:26:40 +00:00
parent 44010f3e52
commit f554d75288
4 changed files with 66 additions and 3 deletions

View File

@ -40,8 +40,11 @@ bool InputAudioFile::Read(size_t samples, int16_t* destination) {
void InputAudioFile::DuplicateInterleaved(const int16_t* source, size_t samples,
size_t channels,
int16_t* destination) {
for (size_t i = 0; i < samples; ++i) {
for (size_t j = 0; j < channels; ++j) {
// Start from the end of |source| and |destination|, and work towards the
// beginning. This is to allow in-place interleaving of the same array (i.e.,
// |source| and |destination| are the same array).
for (int i = static_cast<int>(samples - 1); i >= 0; --i) {
for (int j = static_cast<int>(channels - 1); j >= 0; --j) {
destination[i * channels + j] = source[i];
}
}

View File

@ -37,7 +37,8 @@ class InputAudioFile {
// Creates a multi-channel signal from a mono signal. Each sample is repeated
// |channels| times to create an interleaved multi-channel signal where all
// channels are identical. The output |destination| must have the capacity to
// hold samples * channels elements.
// hold samples * channels elements. Note that |source| and |destination| can
// be the same array (i.e., point to the same address).
static void DuplicateInterleaved(const int16_t* source, size_t samples,
size_t channels, int16_t* destination);

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Unit tests for test InputAudioFile class.
#include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h"
#include "gtest/gtest.h"
namespace webrtc {
namespace test {
TEST(TestInputAudioFile, DuplicateInterleaveSeparateSrcDst) {
static const size_t kSamples = 10;
static const size_t kChannels = 2;
int16_t input[kSamples];
for (size_t i = 0; i < kSamples; ++i) {
input[i] = i;
}
int16_t output[kSamples * kChannels];
InputAudioFile::DuplicateInterleaved(input, kSamples, kChannels, output);
// Verify output
int16_t* output_ptr = output;
for (size_t i = 0; i < kSamples; ++i) {
for (size_t j = 0; j < kChannels; ++j) {
EXPECT_EQ(static_cast<int16_t>(i), *output_ptr++);
}
}
}
TEST(TestInputAudioFile, DuplicateInterleaveSameSrcDst) {
static const size_t kSamples = 10;
static const size_t kChannels = 5;
int16_t input[kSamples * kChannels];
for (size_t i = 0; i < kSamples; ++i) {
input[i] = i;
}
InputAudioFile::DuplicateInterleaved(input, kSamples, kChannels, input);
// Verify output
int16_t* output_ptr = input;
for (size_t i = 0; i < kSamples; ++i) {
for (size_t j = 0; j < kChannels; ++j) {
EXPECT_EQ(static_cast<int16_t>(i), *output_ptr++);
}
}
}
} // namespace test
} // namespace webrtc

View File

@ -153,6 +153,7 @@
'audio_coding/neteq/mock/mock_external_decoder_pcm16b.h',
'audio_coding/neteq/mock/mock_packet_buffer.h',
'audio_coding/neteq/mock/mock_payload_splitter.h',
'audio_coding/neteq/tools/input_audio_file_unittest.cc',
'audio_coding/neteq/tools/packet_unittest.cc',
'audio_processing/aec/system_delay_unittest.cc',
'audio_processing/aec/echo_cancellation_unittest.cc',