OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <cmath> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "media/base/multi_channel_resampler.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 // 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 // expected within some tolerance. |
| 19 static const float kScaleFactor = 44100.0f / 48000.0f; |
| 20 |
| 21 // Simulate large and small sample requests used by the different audio paths. |
| 22 static const int kHighLatencySize = 8192; |
| 23 // 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 static const int kLowLatencySize = 128; |
| 26 |
| 27 // Test fill value. |
| 28 static const float kFillValue = 0.1f; |
| 29 |
| 30 // Chosen arbitrarily based on what each resampler reported during testing. |
| 31 static const double kLowLatencyMaxRMSError = 0.001; |
| 32 static const double kLowLatencyMaxError = 0.0068; |
| 33 static const double kHighLatencyMaxRMSError = 0.00013; |
| 34 static const double kHighLatencyMaxError = 0.0068; |
| 35 |
| 36 class MultiChannelResamplerTestCase |
| 37 : public testing::TestWithParam<int> { |
| 38 public: |
| 39 MultiChannelResamplerTestCase() {} |
| 40 virtual ~MultiChannelResamplerTestCase() { |
| 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 |
| 48 void InitializeAudioData(int channels, int frames) { |
| 49 frames_ = frames; |
| 50 audio_data_.reserve(channels); |
| 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 } |
| 58 |
| 59 // MultiChannelResampler::MultiChannelAudioSourceProvider implementation, just |
| 60 // fills the provided audio_data with |kFillValue|. |
| 61 virtual void ProvideInput(const std::vector<float*>& audio_data, |
| 62 int number_of_frames) { |
| 63 EXPECT_EQ(audio_data.size(), audio_data_.size()); |
| 64 for (size_t i = 0; i < audio_data.size(); ++i) |
| 65 for (int j = 0; j < number_of_frames; ++j) |
| 66 audio_data[i][j] = kFillValue; |
| 67 } |
| 68 |
| 69 void MultiChannelTest(int channels, int frames, double expected_max_rms_error, |
| 70 double expected_max_error) { |
| 71 InitializeAudioData(channels, frames); |
| 72 MultiChannelResampler resampler( |
| 73 channels, kScaleFactor, base::Bind( |
| 74 &MultiChannelResamplerTestCase::ProvideInput, |
| 75 base::Unretained(this))); |
| 76 resampler.Resample(audio_data_, frames); |
| 77 TestValues(expected_max_rms_error, expected_max_error); |
| 78 } |
| 79 |
| 80 void HighLatencyTest(int channels) { |
| 81 MultiChannelTest(channels, kHighLatencySize, kHighLatencyMaxRMSError, |
| 82 kHighLatencyMaxError); |
| 83 } |
| 84 |
| 85 void LowLatencyTest(int channels) { |
| 86 MultiChannelTest(channels, kLowLatencySize, kLowLatencyMaxRMSError, |
| 87 kLowLatencyMaxError); |
| 88 } |
| 89 |
| 90 void TestValues(double expected_max_rms_error, double expected_max_error ) { |
| 91 // Calculate Root-Mean-Square-Error for the resampling. |
| 92 double max_error = 0.0; |
| 93 double sum_of_squares = 0.0; |
| 94 for (size_t i = 0; i < audio_data_.size(); ++i) { |
| 95 for (int j = 0; j < frames_; ++j) { |
| 96 // Ensure all values are accounted for. |
| 97 ASSERT_NE(audio_data_[i][j], 0); |
| 98 |
| 99 double error = fabs(audio_data_[i][j] - kFillValue); |
| 100 max_error = std::max(max_error, error); |
| 101 sum_of_squares += error * error; |
| 102 } |
| 103 } |
| 104 |
| 105 double rms_error = sqrt( |
| 106 sum_of_squares / (frames_ * audio_data_.size())); |
| 107 |
| 108 EXPECT_LE(rms_error, expected_max_rms_error); |
| 109 EXPECT_LE(max_error, expected_max_error); |
| 110 } |
| 111 |
| 112 protected: |
| 113 int frames_; |
| 114 std::vector<float*> audio_data_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(MultiChannelResamplerTestCase); |
| 117 }; |
| 118 |
| 119 TEST_P(MultiChannelResamplerTestCase, HighLatency) { |
| 120 HighLatencyTest(GetParam()); |
| 121 } |
| 122 |
| 123 TEST_P(MultiChannelResamplerTestCase, LowLatency) { |
| 124 LowLatencyTest(GetParam()); |
| 125 } |
| 126 |
| 127 // Test common channel layouts: mono, stereo, 5.1, 7.1. |
| 128 INSTANTIATE_TEST_CASE_P( |
| 129 MultiChannelResamplerTest, MultiChannelResamplerTestCase, |
| 130 testing::Values(1, 2, 6, 8)); |
| 131 |
| 132 } // namespace media |
OLD | NEW |