| Index: media/base/audio_bus_unittest.cc
|
| diff --git a/media/base/audio_bus_unittest.cc b/media/base/audio_bus_unittest.cc
|
| index 02809db72379db083e913e5fe79b29a32a399e96..31080db38b698e7611f4ae99aa41fc1e74b0e2ad 100644
|
| --- a/media/base/audio_bus_unittest.cc
|
| +++ b/media/base/audio_bus_unittest.cc
|
| @@ -34,21 +34,21 @@ class AudioBusTest : public testing::Test {
|
| base::AlignedFree(data_[i]);
|
| }
|
|
|
| - // Validate parameters returned by AudioBus v.s. the constructed parameters.
|
| - void VerifyParams(AudioBus* bus) {
|
| + void VerifyChannelAndFrameCount(AudioBus* bus) {
|
| EXPECT_EQ(kChannels, bus->channels());
|
| EXPECT_EQ(kFrameCount, bus->frames());
|
| }
|
|
|
| - void VerifyValue(const float data[], int size, float value) {
|
| + void VerifyArrayIsFilledWithValue(const float data[], int size, float value) {
|
| for (int i = 0; i < size; ++i)
|
| ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i;
|
| }
|
|
|
| // Verify values for each channel in |result| are within |epsilon| of
|
| // |expected|. If |epsilon| exactly equals 0, uses FLOAT_EQ macro.
|
| - void VerifyBusWithEpsilon(const AudioBus* result, const AudioBus* expected,
|
| - float epsilon) {
|
| + void VerifyAreEqualWithEpsilon(const AudioBus* result,
|
| + const AudioBus* expected,
|
| + float epsilon) {
|
| ASSERT_EQ(expected->channels(), result->channels());
|
| ASSERT_EQ(expected->frames(), result->frames());
|
| for (int ch = 0; ch < result->channels(); ++ch) {
|
| @@ -65,26 +65,30 @@ class AudioBusTest : public testing::Test {
|
| }
|
|
|
| // Verify values for each channel in |result| against |expected|.
|
| - void VerifyBus(const AudioBus* result, const AudioBus* expected) {
|
| - VerifyBusWithEpsilon(result, expected, 0);
|
| + void VerifyAreEqual(const AudioBus* result, const AudioBus* expected) {
|
| + VerifyAreEqualWithEpsilon(result, expected, 0);
|
| }
|
|
|
| // Read and write to the full extent of the allocated channel data. Also test
|
| // the Zero() method and verify it does as advertised. Also test data if data
|
| // is 16-byte aligned as advertised (see kChannelAlignment in audio_bus.h).
|
| - void VerifyChannelData(AudioBus* bus) {
|
| + void VerifyReadWriteAndAlignment(AudioBus* bus) {
|
| for (int i = 0; i < bus->channels(); ++i) {
|
| + // Verify that the address returned by channel(i) is a multiple of
|
| + // AudioBus::kChannelAlignment.
|
| ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(
|
| bus->channel(i)) & (AudioBus::kChannelAlignment - 1));
|
| +
|
| + // Write into the channel buffer.
|
| std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i);
|
| }
|
|
|
| for (int i = 0; i < bus->channels(); ++i)
|
| - VerifyValue(bus->channel(i), bus->frames(), i);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), i);
|
|
|
| bus->Zero();
|
| for (int i = 0; i < bus->channels(); ++i)
|
| - VerifyValue(bus->channel(i), bus->frames(), 0);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), 0);
|
| }
|
|
|
| // Verify copying to and from |bus1| and |bus2|.
|
| @@ -96,12 +100,12 @@ class AudioBusTest : public testing::Test {
|
| // Verify copy from |bus1| to |bus2|.
|
| bus2->Zero();
|
| bus1->CopyTo(bus2);
|
| - VerifyBus(bus1, bus2);
|
| + VerifyAreEqual(bus1, bus2);
|
|
|
| // Verify copy from |bus2| to |bus1|.
|
| bus1->Zero();
|
| bus2->CopyTo(bus1);
|
| - VerifyBus(bus2, bus1);
|
| + VerifyAreEqual(bus2, bus1);
|
| }
|
|
|
| protected:
|
| @@ -113,8 +117,8 @@ class AudioBusTest : public testing::Test {
|
| // Verify basic Create(...) method works as advertised.
|
| TEST_F(AudioBusTest, Create) {
|
| std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
|
| - VerifyParams(bus.get());
|
| - VerifyChannelData(bus.get());
|
| + VerifyChannelAndFrameCount(bus.get());
|
| + VerifyReadWriteAndAlignment(bus.get());
|
| }
|
|
|
| // Verify Create(...) using AudioParameters works as advertised.
|
| @@ -122,8 +126,8 @@ TEST_F(AudioBusTest, CreateUsingAudioParameters) {
|
| std::unique_ptr<AudioBus> bus = AudioBus::Create(
|
| AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
|
| kSampleRate, 32, kFrameCount));
|
| - VerifyParams(bus.get());
|
| - VerifyChannelData(bus.get());
|
| + VerifyChannelAndFrameCount(bus.get());
|
| + VerifyReadWriteAndAlignment(bus.get());
|
| }
|
|
|
| // Verify an AudioBus created via wrapping a vector works as advertised.
|
| @@ -135,8 +139,8 @@ TEST_F(AudioBusTest, WrapVector) {
|
| }
|
|
|
| std::unique_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_);
|
| - VerifyParams(bus.get());
|
| - VerifyChannelData(bus.get());
|
| + VerifyChannelAndFrameCount(bus.get());
|
| + VerifyReadWriteAndAlignment(bus.get());
|
| }
|
|
|
| // Verify an AudioBus created via wrapping a memory block works as advertised.
|
| @@ -156,9 +160,9 @@ TEST_F(AudioBusTest, WrapMemory) {
|
| std::unique_ptr<AudioBus> bus = AudioBus::WrapMemory(params, data.get());
|
| // Verify the test value we filled prior to wrapping.
|
| for (int i = 0; i < bus->channels(); ++i)
|
| - VerifyValue(bus->channel(i), bus->frames(), kTestValue);
|
| - VerifyParams(bus.get());
|
| - VerifyChannelData(bus.get());
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), kTestValue);
|
| + VerifyChannelAndFrameCount(bus.get());
|
| + VerifyReadWriteAndAlignment(bus.get());
|
|
|
| // Verify the channel vectors lie within the provided memory block.
|
| EXPECT_GE(bus->channel(0), data.get());
|
| @@ -217,9 +221,9 @@ TEST_F(AudioBusTest, Zero) {
|
| bus->ZeroFrames(kFrameCount / 2);
|
| for (int i = 0; i < bus->channels(); ++i) {
|
| SCOPED_TRACE("First Half Zero");
|
| - VerifyValue(bus->channel(i), kFrameCount / 2, 0);
|
| - VerifyValue(bus->channel(i) + kFrameCount / 2,
|
| - kFrameCount - kFrameCount / 2, i + 1);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), kFrameCount / 2, 0);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i) + kFrameCount / 2,
|
| + kFrameCount - kFrameCount / 2, i + 1);
|
| }
|
| EXPECT_FALSE(bus->AreFramesZero());
|
|
|
| @@ -231,9 +235,9 @@ TEST_F(AudioBusTest, Zero) {
|
| bus->ZeroFramesPartial(kFrameCount / 2, kFrameCount - kFrameCount / 2);
|
| for (int i = 0; i < bus->channels(); ++i) {
|
| SCOPED_TRACE("Last Half Zero");
|
| - VerifyValue(bus->channel(i) + kFrameCount / 2,
|
| - kFrameCount - kFrameCount / 2, 0);
|
| - VerifyValue(bus->channel(i), kFrameCount / 2, i + 1);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i) + kFrameCount / 2,
|
| + kFrameCount - kFrameCount / 2, 0);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), kFrameCount / 2, i + 1);
|
| }
|
| EXPECT_FALSE(bus->AreFramesZero());
|
|
|
| @@ -245,7 +249,7 @@ TEST_F(AudioBusTest, Zero) {
|
| bus->Zero();
|
| for (int i = 0; i < bus->channels(); ++i) {
|
| SCOPED_TRACE("All Zero");
|
| - VerifyValue(bus->channel(i), bus->frames(), 0);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), 0);
|
| }
|
| EXPECT_TRUE(bus->AreFramesZero());
|
| }
|
| @@ -264,48 +268,53 @@ static const int16_t kTestVectorInt16[kTestVectorSize] = {
|
| static const int32_t kTestVectorInt32[kTestVectorSize] = {
|
| INT32_MIN, 0, INT32_MAX, INT32_MIN, INT32_MAX / 2,
|
| INT32_MIN / 2, 0, INT32_MAX, 0, 0};
|
| +static const float kTestVectorFloat[kTestVectorSize] = {
|
| + -1.0f, 0.0f, 1.0f, -1.0f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f};
|
|
|
| // Expected results.
|
| -static const int kTestVectorFrames = kTestVectorSize / 2;
|
| -static const float kTestVectorResult[][kTestVectorFrames] = {
|
| - { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }};
|
| -static const int kTestVectorChannels = arraysize(kTestVectorResult);
|
| +static const int kTestVectorFrameCount = kTestVectorSize / 2;
|
| +static const float kTestVectorResult[][kTestVectorFrameCount] = {
|
| + {-1.0f, 1.0f, 0.5f, 0.0f, 0.0f},
|
| + {0.0f, -1.0f, -0.5f, 1.0f, 0.0f}};
|
| +static const int kTestVectorChannelCount = arraysize(kTestVectorResult);
|
|
|
| // Verify FromInterleaved() deinterleaves audio in supported formats correctly.
|
| TEST_F(AudioBusTest, FromInterleaved) {
|
| std::unique_ptr<AudioBus> bus =
|
| - AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
|
| + AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
|
| std::unique_ptr<AudioBus> expected =
|
| - AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
|
| - for (int ch = 0; ch < kTestVectorChannels; ++ch) {
|
| + AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
|
| + for (int ch = 0; ch < kTestVectorChannelCount; ++ch) {
|
| memcpy(expected->channel(ch), kTestVectorResult[ch],
|
| - kTestVectorFrames * sizeof(*expected->channel(ch)));
|
| + kTestVectorFrameCount * sizeof(*expected->channel(ch)));
|
| }
|
| {
|
| SCOPED_TRACE("uint8_t");
|
| bus->Zero();
|
| - bus->FromInterleaved(
|
| - kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8));
|
| + bus->FromInterleaved(kTestVectorUint8, kTestVectorFrameCount,
|
| + sizeof(*kTestVectorUint8));
|
| // Biased uint8_t calculations have poor precision, so the epsilon here is
|
| // slightly more permissive than int16_t and int32_t calculations.
|
| - VerifyBusWithEpsilon(bus.get(), expected.get(),
|
| - 1.0f / (std::numeric_limits<uint8_t>::max() - 1));
|
| + VerifyAreEqualWithEpsilon(bus.get(), expected.get(),
|
| + 1.0f / (std::numeric_limits<uint8_t>::max() - 1));
|
| }
|
| {
|
| SCOPED_TRACE("int16_t");
|
| bus->Zero();
|
| - bus->FromInterleaved(
|
| - kTestVectorInt16, kTestVectorFrames, sizeof(*kTestVectorInt16));
|
| - VerifyBusWithEpsilon(bus.get(), expected.get(),
|
| - 1.0f / (std::numeric_limits<uint16_t>::max() + 1.0f));
|
| + bus->FromInterleaved(kTestVectorInt16, kTestVectorFrameCount,
|
| + sizeof(*kTestVectorInt16));
|
| + VerifyAreEqualWithEpsilon(
|
| + bus.get(), expected.get(),
|
| + 1.0f / (std::numeric_limits<uint16_t>::max() + 1.0f));
|
| }
|
| {
|
| SCOPED_TRACE("int32_t");
|
| bus->Zero();
|
| - bus->FromInterleaved(
|
| - kTestVectorInt32, kTestVectorFrames, sizeof(*kTestVectorInt32));
|
| - VerifyBusWithEpsilon(bus.get(), expected.get(),
|
| - 1.0f / (std::numeric_limits<uint32_t>::max() + 1.0f));
|
| + bus->FromInterleaved(kTestVectorInt32, kTestVectorFrameCount,
|
| + sizeof(*kTestVectorInt32));
|
| + VerifyAreEqualWithEpsilon(
|
| + bus.get(), expected.get(),
|
| + 1.0f / (std::numeric_limits<uint32_t>::max() + 1.0f));
|
| }
|
| }
|
|
|
| @@ -314,14 +323,14 @@ TEST_F(AudioBusTest, FromInterleavedPartial) {
|
| // Only deinterleave the middle two frames in each channel.
|
| static const int kPartialStart = 1;
|
| static const int kPartialFrames = 2;
|
| - ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames);
|
| + ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrameCount);
|
|
|
| std::unique_ptr<AudioBus> bus =
|
| - AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
|
| + AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
|
| std::unique_ptr<AudioBus> expected =
|
| - AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
|
| + AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
|
| expected->Zero();
|
| - for (int ch = 0; ch < kTestVectorChannels; ++ch) {
|
| + for (int ch = 0; ch < kTestVectorChannelCount; ++ch) {
|
| memcpy(expected->channel(ch) + kPartialStart,
|
| kTestVectorResult[ch] + kPartialStart,
|
| kPartialFrames * sizeof(*expected->channel(ch)));
|
| @@ -331,17 +340,17 @@ TEST_F(AudioBusTest, FromInterleavedPartial) {
|
| bus->FromInterleavedPartial(
|
| kTestVectorInt32 + kPartialStart * bus->channels(), kPartialStart,
|
| kPartialFrames, sizeof(*kTestVectorInt32));
|
| - VerifyBus(bus.get(), expected.get());
|
| + VerifyAreEqual(bus.get(), expected.get());
|
| }
|
|
|
| // Verify ToInterleaved() interleaves audio in suported formats correctly.
|
| TEST_F(AudioBusTest, ToInterleaved) {
|
| std::unique_ptr<AudioBus> bus =
|
| - AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
|
| + AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
|
| // Fill the bus with our test vector.
|
| for (int ch = 0; ch < bus->channels(); ++ch) {
|
| memcpy(bus->channel(ch), kTestVectorResult[ch],
|
| - kTestVectorFrames * sizeof(*bus->channel(ch)));
|
| + kTestVectorFrameCount * sizeof(*bus->channel(ch)));
|
| }
|
| {
|
| SCOPED_TRACE("uint8_t");
|
| @@ -375,26 +384,46 @@ TEST_F(AudioBusTest, ToInterleaved) {
|
| }
|
| }
|
|
|
| +// Verify ToInterleavedFloat interleaves |AudioBus| channels of float
|
| +// into a single linear output buffer correctly
|
| +TEST_F(AudioBusTest, ToInterleavedFloat) {
|
| + SCOPED_TRACE("float");
|
| + std::unique_ptr<AudioBus> bus =
|
| + AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
|
| + for (int ch = 0; ch < bus->channels(); ++ch) {
|
| + memcpy(bus->channel(ch), kTestVectorResult[ch],
|
| + kTestVectorFrameCount * sizeof(*bus->channel(ch)));
|
| + }
|
| +
|
| + float test_array[arraysize(kTestVectorFloat)];
|
| + bus->ToInterleavedFloat(0, 0, bus->frames(), test_array);
|
| + ASSERT_EQ(memcmp(test_array, kTestVectorFloat,
|
| + kTestVectorFrameCount * sizeof(*kTestVectorFloat)),
|
| + 0);
|
| +}
|
| +
|
| // Verify ToInterleavedPartial() interleaves audio correctly.
|
| TEST_F(AudioBusTest, ToInterleavedPartial) {
|
| // Only interleave the middle two frames in each channel.
|
| static const int kPartialStart = 1;
|
| static const int kPartialFrames = 2;
|
| - ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames);
|
| + ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrameCount);
|
|
|
| std::unique_ptr<AudioBus> expected =
|
| - AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
|
| - for (int ch = 0; ch < kTestVectorChannels; ++ch) {
|
| + AudioBus::Create(kTestVectorChannelCount, kTestVectorFrameCount);
|
| + for (int ch = 0; ch < kTestVectorChannelCount; ++ch) {
|
| memcpy(expected->channel(ch), kTestVectorResult[ch],
|
| - kTestVectorFrames * sizeof(*expected->channel(ch)));
|
| + kTestVectorFrameCount * sizeof(*expected->channel(ch)));
|
| }
|
|
|
| int16_t test_array[arraysize(kTestVectorInt16)];
|
| expected->ToInterleavedPartial(
|
| kPartialStart, kPartialFrames, sizeof(*kTestVectorInt16), test_array);
|
| - ASSERT_EQ(memcmp(
|
| - test_array, kTestVectorInt16 + kPartialStart * kTestVectorChannels,
|
| - kPartialFrames * sizeof(*kTestVectorInt16) * kTestVectorChannels), 0);
|
| + ASSERT_EQ(memcmp(test_array,
|
| + kTestVectorInt16 + kPartialStart * kTestVectorChannelCount,
|
| + kPartialFrames * sizeof(*kTestVectorInt16) *
|
| + kTestVectorChannelCount),
|
| + 0);
|
| }
|
|
|
| TEST_F(AudioBusTest, Scale) {
|
| @@ -409,7 +438,7 @@ TEST_F(AudioBusTest, Scale) {
|
| bus->Scale(-1);
|
| for (int i = 0; i < bus->channels(); ++i) {
|
| SCOPED_TRACE("Invalid Scale");
|
| - VerifyValue(bus->channel(i), bus->frames(), kFillValue);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), kFillValue);
|
| }
|
|
|
| // Verify correct volume adjustment.
|
| @@ -417,14 +446,15 @@ TEST_F(AudioBusTest, Scale) {
|
| bus->Scale(kVolume);
|
| for (int i = 0; i < bus->channels(); ++i) {
|
| SCOPED_TRACE("Half Scale");
|
| - VerifyValue(bus->channel(i), bus->frames(), kFillValue * kVolume);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(),
|
| + kFillValue * kVolume);
|
| }
|
|
|
| // Verify zero volume case.
|
| bus->Scale(0);
|
| for (int i = 0; i < bus->channels(); ++i) {
|
| SCOPED_TRACE("Zero Scale");
|
| - VerifyValue(bus->channel(i), bus->frames(), 0);
|
| + VerifyArrayIsFilledWithValue(bus->channel(i), bus->frames(), 0);
|
| }
|
| }
|
|
|
|
|