| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include <cstdint> | 6 #include <cstdint> |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "chromecast/media/cma/backend/alsa/post_processors/governor.h" | 14 #include "chromecast/media/cma/backend/alsa/post_processors/governor.h" |
| 15 #include "chromecast/media/cma/backend/alsa/post_processors/post_processor_unitt
est.h" |
| 15 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
| 16 #include "media/base/audio_sample_types.h" | 17 #include "media/base/audio_sample_types.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace chromecast { | 20 namespace chromecast { |
| 20 namespace media { | 21 namespace media { |
| 22 namespace post_processor_test { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 const char* kConfigTemplate = | 26 const char* kConfigTemplate = |
| 25 R"config({"onset_volume": %f, "clamp_multiplier": %f})config"; | 27 R"config({"onset_volume": %f, "clamp_multiplier": %f})config"; |
| 26 | 28 |
| 27 const int kNumChannels = 2; | |
| 28 const float kDefaultClamp = 0.6f; | 29 const float kDefaultClamp = 0.6f; |
| 29 const int kNumFrames = 100; | 30 const int kNumFrames = 100; |
| 30 const float kFrequency = 1.0f / kNumFrames; | 31 const int kFrequency = 2000; |
| 31 const int kBytesPerSample = sizeof(int32_t); | |
| 32 const int kSampleRate = 44100; | 32 const int kSampleRate = 44100; |
| 33 | 33 |
| 34 std::string MakeConfigString(float onset_volume, float clamp_multiplier) { | 34 std::string MakeConfigString(float onset_volume, float clamp_multiplier) { |
| 35 return base::StringPrintf(kConfigTemplate, onset_volume, clamp_multiplier); | 35 return base::StringPrintf(kConfigTemplate, onset_volume, clamp_multiplier); |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Frequency is in frames (frequency = frequency_in_hz / sample rate) | |
| 39 std::unique_ptr<::media::AudioBus> GetSineData(size_t frames, float frequency) { | |
| 40 auto data = ::media::AudioBus::Create(kNumChannels, frames); | |
| 41 std::vector<int32_t> sine(frames * 2); | |
| 42 for (size_t i = 0; i < frames; ++i) { | |
| 43 sine[i * 2] = sin(static_cast<float>(i) * frequency * 2 * M_PI) * | |
| 44 std::numeric_limits<int32_t>::max(); | |
| 45 sine[i * 2 + 1] = cos(static_cast<float>(i) * frequency * 2 * M_PI) * | |
| 46 std::numeric_limits<int32_t>::max(); | |
| 47 } | |
| 48 data->FromInterleaved(sine.data(), frames, kBytesPerSample); | |
| 49 return data; | |
| 50 } | |
| 51 | |
| 52 void ScaleData(float* data, int frames, float scale) { | 38 void ScaleData(float* data, int frames, float scale) { |
| 53 for (int f = 0; f < frames; ++f) { | 39 for (int f = 0; f < frames; ++f) { |
| 54 data[f] *= scale; | 40 data[f] *= scale; |
| 55 } | 41 } |
| 56 } | 42 } |
| 57 | 43 |
| 58 void CompareData(const std::vector<float>& expected, | |
| 59 const std::vector<float>& actual, | |
| 60 int frames) { | |
| 61 ASSERT_EQ(expected.size(), actual.size()); | |
| 62 for (int f = 0; f < frames; ++f) { | |
| 63 EXPECT_FLOAT_EQ(expected[f], actual[f]) << "f: " << f; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 } // namespace | 44 } // namespace |
| 68 | 45 |
| 69 class GovernorTest : public ::testing::TestWithParam<float> { | 46 class GovernorTest : public ::testing::TestWithParam<float> { |
| 70 protected: | 47 protected: |
| 71 GovernorTest() = default; | 48 GovernorTest() = default; |
| 72 ~GovernorTest() = default; | 49 ~GovernorTest() = default; |
| 73 void SetUp() override { | 50 void SetUp() override { |
| 74 clamp_ = kDefaultClamp; | 51 clamp_ = kDefaultClamp; |
| 75 onset_volume_ = GetParam(); | 52 onset_volume_ = GetParam(); |
| 76 std::string config = MakeConfigString(onset_volume_, clamp_); | 53 std::string config = MakeConfigString(onset_volume_, clamp_); |
| 77 governor_ = base::MakeUnique<Governor>(config, kNumChannels); | 54 governor_ = base::MakeUnique<Governor>(config, kNumChannels); |
| 78 governor_->SetSlewTimeMsForTest(0); | 55 governor_->SetSlewTimeMsForTest(0); |
| 79 governor_->SetSampleRate(kSampleRate); | 56 governor_->SetSampleRate(kSampleRate); |
| 80 | 57 |
| 81 auto data_bus = GetSineData(kNumFrames, kFrequency); | 58 data_ = GetSineData(kNumFrames, kFrequency, kSampleRate); |
| 82 auto expected_bus = GetSineData(kNumFrames, kFrequency); | 59 expected_ = GetSineData(kNumFrames, kFrequency, kSampleRate); |
| 83 data_.resize(kNumFrames * kNumChannels); | |
| 84 expected_.resize(kNumFrames * kNumChannels); | |
| 85 data_bus->ToInterleaved<::media::FloatSampleTypeTraits<float>>( | |
| 86 kNumFrames, data_.data()); | |
| 87 expected_bus->ToInterleaved<::media::FloatSampleTypeTraits<float>>( | |
| 88 kNumFrames, expected_.data()); | |
| 89 } | |
| 90 | |
| 91 void CompareBuffers() { | |
| 92 CompareData(expected_, data_, kNumFrames * kNumChannels); | |
| 93 } | 60 } |
| 94 | 61 |
| 95 void ProcessFrames(float volume) { | 62 void ProcessFrames(float volume) { |
| 96 EXPECT_EQ(governor_->ProcessFrames(data_.data(), kNumFrames, volume), 0); | 63 EXPECT_EQ(governor_->ProcessFrames(data_.data(), kNumFrames, volume), 0); |
| 97 } | 64 } |
| 98 | 65 |
| 66 void CompareBuffers() { |
| 67 CheckArraysEqual(expected_.data(), data_.data(), expected_.size()); |
| 68 } |
| 69 |
| 99 float clamp_; | 70 float clamp_; |
| 100 float onset_volume_; | 71 float onset_volume_; |
| 101 std::unique_ptr<Governor> governor_; | 72 std::unique_ptr<Governor> governor_; |
| 102 std::vector<float> data_; | 73 std::vector<float> data_; |
| 103 std::vector<float> expected_; | 74 std::vector<float> expected_; |
| 104 | 75 |
| 105 private: | 76 private: |
| 106 DISALLOW_COPY_AND_ASSIGN(GovernorTest); | 77 DISALLOW_COPY_AND_ASSIGN(GovernorTest); |
| 107 }; | 78 }; |
| 108 | 79 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 132 if (onset_volume_ <= 1.0) { | 103 if (onset_volume_ <= 1.0) { |
| 133 ScaleData(expected_.data(), kNumFrames * kNumChannels, clamp_); | 104 ScaleData(expected_.data(), kNumFrames * kNumChannels, clamp_); |
| 134 } | 105 } |
| 135 CompareBuffers(); | 106 CompareBuffers(); |
| 136 } | 107 } |
| 137 | 108 |
| 138 INSTANTIATE_TEST_CASE_P(GovernorClampVolumeTest, | 109 INSTANTIATE_TEST_CASE_P(GovernorClampVolumeTest, |
| 139 GovernorTest, | 110 GovernorTest, |
| 140 ::testing::Values(0.0f, 0.1f, 0.5f, 0.9f, 1.0f, 1.1f)); | 111 ::testing::Values(0.0f, 0.1f, 0.5f, 0.9f, 1.0f, 1.1f)); |
| 141 | 112 |
| 113 // Default tests from post_processor_test |
| 114 TEST_P(PostProcessorTest, TestDelay) { |
| 115 std::string config = MakeConfigString(1.0, 1.0); |
| 116 auto pp = |
| 117 base::WrapUnique(AudioPostProcessorShlib_Create(config, kNumChannels)); |
| 118 TestDelay(pp.get(), sample_rate_); |
| 119 } |
| 120 |
| 121 TEST_P(PostProcessorTest, TestRinging) { |
| 122 std::string config = MakeConfigString(1.0, 1.0); |
| 123 auto pp = |
| 124 base::WrapUnique(AudioPostProcessorShlib_Create(config, kNumChannels)); |
| 125 TestRingingTime(pp.get(), sample_rate_); |
| 126 } |
| 127 |
| 128 TEST_P(PostProcessorTest, TestPassthrough) { |
| 129 std::string config = MakeConfigString(1.0, 1.0); |
| 130 auto pp = |
| 131 base::WrapUnique(AudioPostProcessorShlib_Create(config, kNumChannels)); |
| 132 TestPassthrough(pp.get(), sample_rate_); |
| 133 } |
| 134 |
| 135 } // namespace post_processor_test |
| 142 } // namespace media | 136 } // namespace media |
| 143 } // namespace chromecast | 137 } // namespace chromecast |
| OLD | NEW |