| 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 <cmath> | 5 #include <cmath> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "media/base/audio_bus.h" |
| 11 #include "media/base/multi_channel_resampler.h" | 12 #include "media/base/multi_channel_resampler.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 // Just test a basic resampling case. The SincResampler unit test will take | 17 // Just test a basic resampling case. The SincResampler unit test will take |
| 17 // care of accuracy testing; we just need to check that multichannel works as | 18 // care of accuracy testing; we just need to check that multichannel works as |
| 18 // expected within some tolerance. | 19 // expected within some tolerance. |
| 19 static const float kScaleFactor = 192000.0f / 44100.0f; | 20 static const float kScaleFactor = 192000.0f / 44100.0f; |
| 20 | 21 |
| 21 // Simulate large and small sample requests used by the different audio paths. | 22 // Simulate large and small sample requests used by the different audio paths. |
| 22 static const int kHighLatencySize = 8192; | 23 static const int kHighLatencySize = 8192; |
| 23 // Low latency buffers show a larger error than high latency ones. Which makes | 24 // Low latency buffers show a larger error than high latency ones. Which makes |
| 24 // sense since each error represents a larger portion of the total request. | 25 // sense since each error represents a larger portion of the total request. |
| 25 static const int kLowLatencySize = 128; | 26 static const int kLowLatencySize = 128; |
| 26 | 27 |
| 27 // Test fill value. | 28 // Test fill value. |
| 28 static const float kFillValue = 0.1f; | 29 static const float kFillValue = 0.1f; |
| 29 | 30 |
| 30 // Chosen arbitrarily based on what each resampler reported during testing. | 31 // Chosen arbitrarily based on what each resampler reported during testing. |
| 31 static const double kLowLatencyMaxRMSError = 0.0036; | 32 static const double kLowLatencyMaxRMSError = 0.0036; |
| 32 static const double kLowLatencyMaxError = 0.04; | 33 static const double kLowLatencyMaxError = 0.04; |
| 33 static const double kHighLatencyMaxRMSError = 0.0036; | 34 static const double kHighLatencyMaxRMSError = 0.0036; |
| 34 static const double kHighLatencyMaxError = 0.04; | 35 static const double kHighLatencyMaxError = 0.04; |
| 35 | 36 |
| 36 class MultiChannelResamplerTest | 37 class MultiChannelResamplerTest |
| 37 : public testing::TestWithParam<int> { | 38 : public testing::TestWithParam<int> { |
| 38 public: | 39 public: |
| 39 MultiChannelResamplerTest() {} | 40 MultiChannelResamplerTest() {} |
| 40 virtual ~MultiChannelResamplerTest() { | 41 virtual ~MultiChannelResamplerTest() {} |
| 41 if (!audio_data_.empty()) { | |
| 42 for (size_t i = 0; i < audio_data_.size(); ++i) | |
| 43 delete [] audio_data_[i]; | |
| 44 audio_data_.clear(); | |
| 45 } | |
| 46 } | |
| 47 | 42 |
| 48 void InitializeAudioData(int channels, int frames) { | 43 void InitializeAudioData(int channels, int frames) { |
| 49 frames_ = frames; | 44 frames_ = frames; |
| 50 audio_data_.reserve(channels); | 45 audio_bus_ = AudioBus::Create(channels, frames); |
| 51 for (int i = 0; i < channels; ++i) { | |
| 52 audio_data_.push_back(new float[frames]); | |
| 53 | |
| 54 // Zero initialize so we can be sure every value has been provided. | |
| 55 memset(audio_data_[i], 0, sizeof(*audio_data_[i]) * frames); | |
| 56 } | |
| 57 } | 46 } |
| 58 | 47 |
| 59 // MultiChannelResampler::MultiChannelAudioSourceProvider implementation, just | 48 // MultiChannelResampler::MultiChannelAudioSourceProvider implementation, just |
| 60 // fills the provided audio_data with |kFillValue|. | 49 // fills the provided audio_data with |kFillValue|. |
| 61 virtual void ProvideInput(const std::vector<float*>& audio_data, | 50 virtual void ProvideInput(AudioBus* audio_bus, int number_of_frames) { |
| 62 int number_of_frames) { | 51 EXPECT_EQ(audio_bus->channels(), audio_bus_->channels()); |
| 63 EXPECT_EQ(audio_data.size(), audio_data_.size()); | 52 for (int i = 0; i < audio_bus->channels(); ++i) |
| 64 for (size_t i = 0; i < audio_data.size(); ++i) | |
| 65 for (int j = 0; j < number_of_frames; ++j) | 53 for (int j = 0; j < number_of_frames; ++j) |
| 66 audio_data[i][j] = kFillValue; | 54 audio_bus->channel(i)[j] = kFillValue; |
| 67 } | 55 } |
| 68 | 56 |
| 69 void MultiChannelTest(int channels, int frames, double expected_max_rms_error, | 57 void MultiChannelTest(int channels, int frames, double expected_max_rms_error, |
| 70 double expected_max_error) { | 58 double expected_max_error) { |
| 71 InitializeAudioData(channels, frames); | 59 InitializeAudioData(channels, frames); |
| 72 MultiChannelResampler resampler( | 60 MultiChannelResampler resampler( |
| 73 channels, kScaleFactor, base::Bind( | 61 channels, kScaleFactor, base::Bind( |
| 74 &MultiChannelResamplerTest::ProvideInput, | 62 &MultiChannelResamplerTest::ProvideInput, |
| 75 base::Unretained(this))); | 63 base::Unretained(this))); |
| 76 resampler.Resample(audio_data_, frames); | 64 resampler.Resample(audio_bus_.get(), frames); |
| 77 TestValues(expected_max_rms_error, expected_max_error); | 65 TestValues(expected_max_rms_error, expected_max_error); |
| 78 } | 66 } |
| 79 | 67 |
| 80 void HighLatencyTest(int channels) { | 68 void HighLatencyTest(int channels) { |
| 81 MultiChannelTest(channels, kHighLatencySize, kHighLatencyMaxRMSError, | 69 MultiChannelTest(channels, kHighLatencySize, kHighLatencyMaxRMSError, |
| 82 kHighLatencyMaxError); | 70 kHighLatencyMaxError); |
| 83 } | 71 } |
| 84 | 72 |
| 85 void LowLatencyTest(int channels) { | 73 void LowLatencyTest(int channels) { |
| 86 MultiChannelTest(channels, kLowLatencySize, kLowLatencyMaxRMSError, | 74 MultiChannelTest(channels, kLowLatencySize, kLowLatencyMaxRMSError, |
| 87 kLowLatencyMaxError); | 75 kLowLatencyMaxError); |
| 88 } | 76 } |
| 89 | 77 |
| 90 void TestValues(double expected_max_rms_error, double expected_max_error ) { | 78 void TestValues(double expected_max_rms_error, double expected_max_error ) { |
| 91 // Calculate Root-Mean-Square-Error for the resampling. | 79 // Calculate Root-Mean-Square-Error for the resampling. |
| 92 double max_error = 0.0; | 80 double max_error = 0.0; |
| 93 double sum_of_squares = 0.0; | 81 double sum_of_squares = 0.0; |
| 94 for (size_t i = 0; i < audio_data_.size(); ++i) { | 82 for (int i = 0; i < audio_bus_->channels(); ++i) { |
| 95 for (int j = 0; j < frames_; ++j) { | 83 for (int j = 0; j < frames_; ++j) { |
| 96 // Ensure all values are accounted for. | 84 // Ensure all values are accounted for. |
| 97 ASSERT_NE(audio_data_[i][j], 0); | 85 ASSERT_NE(audio_bus_->channel(i)[j], 0); |
| 98 | 86 |
| 99 double error = fabs(audio_data_[i][j] - kFillValue); | 87 double error = fabs(audio_bus_->channel(i)[j] - kFillValue); |
| 100 max_error = std::max(max_error, error); | 88 max_error = std::max(max_error, error); |
| 101 sum_of_squares += error * error; | 89 sum_of_squares += error * error; |
| 102 } | 90 } |
| 103 } | 91 } |
| 104 | 92 |
| 105 double rms_error = sqrt( | 93 double rms_error = sqrt( |
| 106 sum_of_squares / (frames_ * audio_data_.size())); | 94 sum_of_squares / (frames_ * audio_bus_->channels())); |
| 107 | 95 |
| 108 EXPECT_LE(rms_error, expected_max_rms_error); | 96 EXPECT_LE(rms_error, expected_max_rms_error); |
| 109 EXPECT_LE(max_error, expected_max_error); | 97 EXPECT_LE(max_error, expected_max_error); |
| 110 } | 98 } |
| 111 | 99 |
| 112 protected: | 100 protected: |
| 113 int frames_; | 101 int frames_; |
| 114 std::vector<float*> audio_data_; | 102 scoped_ptr<AudioBus> audio_bus_; |
| 115 | 103 |
| 116 DISALLOW_COPY_AND_ASSIGN(MultiChannelResamplerTest); | 104 DISALLOW_COPY_AND_ASSIGN(MultiChannelResamplerTest); |
| 117 }; | 105 }; |
| 118 | 106 |
| 119 TEST_P(MultiChannelResamplerTest, HighLatency) { | 107 TEST_P(MultiChannelResamplerTest, HighLatency) { |
| 120 HighLatencyTest(GetParam()); | 108 HighLatencyTest(GetParam()); |
| 121 } | 109 } |
| 122 | 110 |
| 123 TEST_P(MultiChannelResamplerTest, LowLatency) { | 111 TEST_P(MultiChannelResamplerTest, LowLatency) { |
| 124 LowLatencyTest(GetParam()); | 112 LowLatencyTest(GetParam()); |
| 125 } | 113 } |
| 126 | 114 |
| 127 // Test common channel layouts: mono, stereo, 5.1, 7.1. | 115 // Test common channel layouts: mono, stereo, 5.1, 7.1. |
| 128 INSTANTIATE_TEST_CASE_P( | 116 INSTANTIATE_TEST_CASE_P( |
| 129 MultiChannelResamplerTest, MultiChannelResamplerTest, | 117 MultiChannelResamplerTest, MultiChannelResamplerTest, |
| 130 testing::Values(1, 2, 6, 8)); | 118 testing::Values(1, 2, 6, 8)); |
| 131 | 119 |
| 132 } // namespace media | 120 } // namespace media |
| OLD | NEW |