| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdint.h> |
| 6 |
| 7 #include <limits> |
| 8 |
| 5 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 6 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 7 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 8 #include "media/audio/audio_parameters.h" | 12 #include "media/audio/audio_parameters.h" |
| 9 #include "media/base/audio_bus.h" | 13 #include "media/base/audio_bus.h" |
| 10 #include "media/base/channel_layout.h" | 14 #include "media/base/channel_layout.h" |
| 11 #include "media/base/fake_audio_render_callback.h" | 15 #include "media/base/fake_audio_render_callback.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 17 |
| 14 namespace media { | 18 namespace media { |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 for (int i = 0; i < bus->channels(); ++i) { | 244 for (int i = 0; i < bus->channels(); ++i) { |
| 241 SCOPED_TRACE("All Zero"); | 245 SCOPED_TRACE("All Zero"); |
| 242 VerifyValue(bus->channel(i), bus->frames(), 0); | 246 VerifyValue(bus->channel(i), bus->frames(), 0); |
| 243 } | 247 } |
| 244 EXPECT_TRUE(bus->AreFramesZero()); | 248 EXPECT_TRUE(bus->AreFramesZero()); |
| 245 } | 249 } |
| 246 | 250 |
| 247 // Each test vector represents two channels of data in the following arbitrary | 251 // Each test vector represents two channels of data in the following arbitrary |
| 248 // layout: <min, zero, max, min, max / 2, min / 2, zero, max, zero, zero>. | 252 // layout: <min, zero, max, min, max / 2, min / 2, zero, max, zero, zero>. |
| 249 static const int kTestVectorSize = 10; | 253 static const int kTestVectorSize = 10; |
| 250 static const uint8 kTestVectorUint8[kTestVectorSize] = { | 254 static const uint8_t kTestVectorUint8[kTestVectorSize] = { |
| 251 0, -kint8min, kuint8max, 0, kint8max / 2 + 128, kint8min / 2 + 128, | 255 0, -INT8_MIN, UINT8_MAX, |
| 252 -kint8min, kuint8max, -kint8min, -kint8min }; | 256 0, INT8_MAX / 2 + 128, INT8_MIN / 2 + 128, |
| 253 static const int16 kTestVectorInt16[kTestVectorSize] = { | 257 -INT8_MIN, UINT8_MAX, -INT8_MIN, |
| 254 kint16min, 0, kint16max, kint16min, kint16max / 2, kint16min / 2, | 258 -INT8_MIN}; |
| 255 0, kint16max, 0, 0 }; | 259 static const int16_t kTestVectorInt16[kTestVectorSize] = { |
| 256 static const int32 kTestVectorInt32[kTestVectorSize] = { | 260 INT16_MIN, 0, INT16_MAX, INT16_MIN, INT16_MAX / 2, |
| 257 kint32min, 0, kint32max, kint32min, kint32max / 2, kint32min / 2, | 261 INT16_MIN / 2, 0, INT16_MAX, 0, 0}; |
| 258 0, kint32max, 0, 0 }; | 262 static const int32_t kTestVectorInt32[kTestVectorSize] = { |
| 263 INT32_MIN, 0, INT32_MAX, INT32_MIN, INT32_MAX / 2, |
| 264 INT32_MIN / 2, 0, INT32_MAX, 0, 0}; |
| 259 | 265 |
| 260 // Expected results. | 266 // Expected results. |
| 261 static const int kTestVectorFrames = kTestVectorSize / 2; | 267 static const int kTestVectorFrames = kTestVectorSize / 2; |
| 262 static const float kTestVectorResult[][kTestVectorFrames] = { | 268 static const float kTestVectorResult[][kTestVectorFrames] = { |
| 263 { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }}; | 269 { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }}; |
| 264 static const int kTestVectorChannels = arraysize(kTestVectorResult); | 270 static const int kTestVectorChannels = arraysize(kTestVectorResult); |
| 265 | 271 |
| 266 // Verify FromInterleaved() deinterleaves audio in supported formats correctly. | 272 // Verify FromInterleaved() deinterleaves audio in supported formats correctly. |
| 267 TEST_F(AudioBusTest, FromInterleaved) { | 273 TEST_F(AudioBusTest, FromInterleaved) { |
| 268 scoped_ptr<AudioBus> bus = AudioBus::Create( | 274 scoped_ptr<AudioBus> bus = AudioBus::Create( |
| 269 kTestVectorChannels, kTestVectorFrames); | 275 kTestVectorChannels, kTestVectorFrames); |
| 270 scoped_ptr<AudioBus> expected = AudioBus::Create( | 276 scoped_ptr<AudioBus> expected = AudioBus::Create( |
| 271 kTestVectorChannels, kTestVectorFrames); | 277 kTestVectorChannels, kTestVectorFrames); |
| 272 for (int ch = 0; ch < kTestVectorChannels; ++ch) { | 278 for (int ch = 0; ch < kTestVectorChannels; ++ch) { |
| 273 memcpy(expected->channel(ch), kTestVectorResult[ch], | 279 memcpy(expected->channel(ch), kTestVectorResult[ch], |
| 274 kTestVectorFrames * sizeof(*expected->channel(ch))); | 280 kTestVectorFrames * sizeof(*expected->channel(ch))); |
| 275 } | 281 } |
| 276 { | 282 { |
| 277 SCOPED_TRACE("uint8"); | 283 SCOPED_TRACE("uint8"); |
| 278 bus->Zero(); | 284 bus->Zero(); |
| 279 bus->FromInterleaved( | 285 bus->FromInterleaved( |
| 280 kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8)); | 286 kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8)); |
| 281 // Biased uint8 calculations have poor precision, so the epsilon here is | 287 // Biased uint8_t calculations have poor precision, so the epsilon here is |
| 282 // slightly more permissive than int16 and int32 calculations. | 288 // slightly more permissive than int16_t and int32_t calculations. |
| 283 VerifyBusWithEpsilon(bus.get(), expected.get(), 1.0f / (kuint8max - 1)); | 289 VerifyBusWithEpsilon(bus.get(), expected.get(), |
| 290 1.0f / (std::numeric_limits<uint8_t>::max() - 1)); |
| 284 } | 291 } |
| 285 { | 292 { |
| 286 SCOPED_TRACE("int16"); | 293 SCOPED_TRACE("int16"); |
| 287 bus->Zero(); | 294 bus->Zero(); |
| 288 bus->FromInterleaved( | 295 bus->FromInterleaved( |
| 289 kTestVectorInt16, kTestVectorFrames, sizeof(*kTestVectorInt16)); | 296 kTestVectorInt16, kTestVectorFrames, sizeof(*kTestVectorInt16)); |
| 290 VerifyBusWithEpsilon(bus.get(), expected.get(), 1.0f / (kuint16max + 1.0f)); | 297 VerifyBusWithEpsilon(bus.get(), expected.get(), |
| 298 1.0f / (std::numeric_limits<uint16_t>::max() + 1.0f)); |
| 291 } | 299 } |
| 292 { | 300 { |
| 293 SCOPED_TRACE("int32"); | 301 SCOPED_TRACE("int32"); |
| 294 bus->Zero(); | 302 bus->Zero(); |
| 295 bus->FromInterleaved( | 303 bus->FromInterleaved( |
| 296 kTestVectorInt32, kTestVectorFrames, sizeof(*kTestVectorInt32)); | 304 kTestVectorInt32, kTestVectorFrames, sizeof(*kTestVectorInt32)); |
| 297 VerifyBusWithEpsilon(bus.get(), expected.get(), 1.0f / (kuint32max + 1.0f)); | 305 VerifyBusWithEpsilon(bus.get(), expected.get(), |
| 306 1.0f / (std::numeric_limits<uint32_t>::max() + 1.0f)); |
| 298 } | 307 } |
| 299 } | 308 } |
| 300 | 309 |
| 301 // Verify FromInterleavedPartial() deinterleaves audio correctly. | 310 // Verify FromInterleavedPartial() deinterleaves audio correctly. |
| 302 TEST_F(AudioBusTest, FromInterleavedPartial) { | 311 TEST_F(AudioBusTest, FromInterleavedPartial) { |
| 303 // Only deinterleave the middle two frames in each channel. | 312 // Only deinterleave the middle two frames in each channel. |
| 304 static const int kPartialStart = 1; | 313 static const int kPartialStart = 1; |
| 305 static const int kPartialFrames = 2; | 314 static const int kPartialFrames = 2; |
| 306 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); | 315 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); |
| 307 | 316 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 327 TEST_F(AudioBusTest, ToInterleaved) { | 336 TEST_F(AudioBusTest, ToInterleaved) { |
| 328 scoped_ptr<AudioBus> bus = AudioBus::Create( | 337 scoped_ptr<AudioBus> bus = AudioBus::Create( |
| 329 kTestVectorChannels, kTestVectorFrames); | 338 kTestVectorChannels, kTestVectorFrames); |
| 330 // Fill the bus with our test vector. | 339 // Fill the bus with our test vector. |
| 331 for (int ch = 0; ch < bus->channels(); ++ch) { | 340 for (int ch = 0; ch < bus->channels(); ++ch) { |
| 332 memcpy(bus->channel(ch), kTestVectorResult[ch], | 341 memcpy(bus->channel(ch), kTestVectorResult[ch], |
| 333 kTestVectorFrames * sizeof(*bus->channel(ch))); | 342 kTestVectorFrames * sizeof(*bus->channel(ch))); |
| 334 } | 343 } |
| 335 { | 344 { |
| 336 SCOPED_TRACE("uint8"); | 345 SCOPED_TRACE("uint8"); |
| 337 uint8 test_array[arraysize(kTestVectorUint8)]; | 346 uint8_t test_array[arraysize(kTestVectorUint8)]; |
| 338 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorUint8), test_array); | 347 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorUint8), test_array); |
| 339 ASSERT_EQ(memcmp( | 348 ASSERT_EQ(memcmp( |
| 340 test_array, kTestVectorUint8, sizeof(kTestVectorUint8)), 0); | 349 test_array, kTestVectorUint8, sizeof(kTestVectorUint8)), 0); |
| 341 } | 350 } |
| 342 { | 351 { |
| 343 SCOPED_TRACE("int16"); | 352 SCOPED_TRACE("int16"); |
| 344 int16 test_array[arraysize(kTestVectorInt16)]; | 353 int16_t test_array[arraysize(kTestVectorInt16)]; |
| 345 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt16), test_array); | 354 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt16), test_array); |
| 346 ASSERT_EQ(memcmp( | 355 ASSERT_EQ(memcmp( |
| 347 test_array, kTestVectorInt16, sizeof(kTestVectorInt16)), 0); | 356 test_array, kTestVectorInt16, sizeof(kTestVectorInt16)), 0); |
| 348 } | 357 } |
| 349 { | 358 { |
| 350 SCOPED_TRACE("int32"); | 359 SCOPED_TRACE("int32"); |
| 351 int32 test_array[arraysize(kTestVectorInt32)]; | 360 int32_t test_array[arraysize(kTestVectorInt32)]; |
| 352 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt32), test_array); | 361 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorInt32), test_array); |
| 353 | 362 |
| 354 // Some compilers get better precision than others on the half-max test, so | 363 // Some compilers get better precision than others on the half-max test, so |
| 355 // let the test pass with an off by one check on the half-max. | 364 // let the test pass with an off by one check on the half-max. |
| 356 int32 fixed_test_array[arraysize(kTestVectorInt32)]; | 365 int32_t fixed_test_array[arraysize(kTestVectorInt32)]; |
| 357 memcpy(fixed_test_array, kTestVectorInt32, sizeof(kTestVectorInt32)); | 366 memcpy(fixed_test_array, kTestVectorInt32, sizeof(kTestVectorInt32)); |
| 358 ASSERT_EQ(fixed_test_array[4], kint32max / 2); | 367 ASSERT_EQ(fixed_test_array[4], std::numeric_limits<int32_t>::max() / 2); |
| 359 fixed_test_array[4]++; | 368 fixed_test_array[4]++; |
| 360 | 369 |
| 361 ASSERT_TRUE( | 370 ASSERT_TRUE( |
| 362 memcmp(test_array, kTestVectorInt32, sizeof(kTestVectorInt32)) == 0 || | 371 memcmp(test_array, kTestVectorInt32, sizeof(kTestVectorInt32)) == 0 || |
| 363 memcmp(test_array, fixed_test_array, sizeof(fixed_test_array)) == 0); | 372 memcmp(test_array, fixed_test_array, sizeof(fixed_test_array)) == 0); |
| 364 } | 373 } |
| 365 } | 374 } |
| 366 | 375 |
| 367 // Verify ToInterleavedPartial() interleaves audio correctly. | 376 // Verify ToInterleavedPartial() interleaves audio correctly. |
| 368 TEST_F(AudioBusTest, ToInterleavedPartial) { | 377 TEST_F(AudioBusTest, ToInterleavedPartial) { |
| 369 // Only interleave the middle two frames in each channel. | 378 // Only interleave the middle two frames in each channel. |
| 370 static const int kPartialStart = 1; | 379 static const int kPartialStart = 1; |
| 371 static const int kPartialFrames = 2; | 380 static const int kPartialFrames = 2; |
| 372 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); | 381 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); |
| 373 | 382 |
| 374 scoped_ptr<AudioBus> expected = AudioBus::Create( | 383 scoped_ptr<AudioBus> expected = AudioBus::Create( |
| 375 kTestVectorChannels, kTestVectorFrames); | 384 kTestVectorChannels, kTestVectorFrames); |
| 376 for (int ch = 0; ch < kTestVectorChannels; ++ch) { | 385 for (int ch = 0; ch < kTestVectorChannels; ++ch) { |
| 377 memcpy(expected->channel(ch), kTestVectorResult[ch], | 386 memcpy(expected->channel(ch), kTestVectorResult[ch], |
| 378 kTestVectorFrames * sizeof(*expected->channel(ch))); | 387 kTestVectorFrames * sizeof(*expected->channel(ch))); |
| 379 } | 388 } |
| 380 | 389 |
| 381 int16 test_array[arraysize(kTestVectorInt16)]; | 390 int16_t test_array[arraysize(kTestVectorInt16)]; |
| 382 expected->ToInterleavedPartial( | 391 expected->ToInterleavedPartial( |
| 383 kPartialStart, kPartialFrames, sizeof(*kTestVectorInt16), test_array); | 392 kPartialStart, kPartialFrames, sizeof(*kTestVectorInt16), test_array); |
| 384 ASSERT_EQ(memcmp( | 393 ASSERT_EQ(memcmp( |
| 385 test_array, kTestVectorInt16 + kPartialStart * kTestVectorChannels, | 394 test_array, kTestVectorInt16 + kPartialStart * kTestVectorChannels, |
| 386 kPartialFrames * sizeof(*kTestVectorInt16) * kTestVectorChannels), 0); | 395 kPartialFrames * sizeof(*kTestVectorInt16) * kTestVectorChannels), 0); |
| 387 } | 396 } |
| 388 | 397 |
| 389 TEST_F(AudioBusTest, Scale) { | 398 TEST_F(AudioBusTest, Scale) { |
| 390 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); | 399 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
| 391 | 400 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 411 | 420 |
| 412 // Verify zero volume case. | 421 // Verify zero volume case. |
| 413 bus->Scale(0); | 422 bus->Scale(0); |
| 414 for (int i = 0; i < bus->channels(); ++i) { | 423 for (int i = 0; i < bus->channels(); ++i) { |
| 415 SCOPED_TRACE("Zero Scale"); | 424 SCOPED_TRACE("Zero Scale"); |
| 416 VerifyValue(bus->channel(i), bus->frames(), 0); | 425 VerifyValue(bus->channel(i), bus->frames(), 0); |
| 417 } | 426 } |
| 418 } | 427 } |
| 419 | 428 |
| 420 } // namespace media | 429 } // namespace media |
| OLD | NEW |