Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: chromecast/media/cma/backend/alsa/post_processors/governor_unittest.cc

Issue 2860673003: [Chromecast] Correct libcast_governor behavior. (Closed)
Patch Set: Remove debug logging Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <limits>
6 #include <string>
7 #include <vector>
8
9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "chromecast/media/cma/backend/alsa/post_processors/governor_shlib.h"
13 #include "media/base/audio_bus.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace chromecast {
17 namespace media {
18
19 namespace {
20
21 const char* kConfigTemplate =
22 R"config({"onset_volume": %f, "clamp_multiplier": %f})config";
23
24 const int kNumChannels = 2;
25 const float kDefaultClamp = .6f;
bcf 2017/05/03 06:14:07 nit: 0.6f
bshaya 2017/05/03 16:51:55 Done.
26 const double kPi = 3.1415;
bcf 2017/05/03 06:14:07 Use M_PI from <cmath>
bshaya 2017/05/03 16:51:55 Done.
bcf 2017/05/03 18:07:09 Need to include <cmath>
bshaya 2017/05/04 03:01:18 Done.
27 const int kNumFrames = 100;
28 const float kFrequency = 1.0f / kNumFrames;
29 const int kBytesPerSample = sizeof(int);
bcf 2017/05/03 06:14:06 sizeof(int32_t);
bshaya 2017/05/03 16:51:55 Done.
30 const int kSampleRate = 44100;
31
32 std::string MakeConfigString(float onset_volume, float clamp_multiplier) {
33 return base::StringPrintf(kConfigTemplate, onset_volume, clamp_multiplier);
34 }
35
36 // Frequency is in frames (frequency = frequency_in_hz / sample rate)
37 std::unique_ptr<::media::AudioBus> GetSineData(size_t frames, float frequency) {
38 auto data = ::media::AudioBus::Create(kNumChannels, frames);
39 std::vector<int> sine(frames * 2);
bcf 2017/05/03 06:14:07 int32_t here and for other samples.
bshaya 2017/05/03 16:51:55 Done.
40 for (size_t i = 0; i < frames; ++i) {
41 sine[i * 2] = sin(static_cast<float>(i) * frequency * 2 * kPi) *
42 std::numeric_limits<int>::max();
43 sine[i * 2 + 1] = cos(static_cast<float>(i) * frequency * 2 * kPi) *
44 std::numeric_limits<int>::max();
45 }
46 data->FromInterleaved(sine.data(), frames, kBytesPerSample);
47 return data;
48 }
49
50 std::vector<float*> GetDataChannels(::media::AudioBus* audio) {
51 std::vector<float*> data(kNumChannels);
52 for (int i = 0; i < kNumChannels; ++i) {
53 data[i] = audio->channel(i);
54 }
55 return data;
56 }
57
58 void ScaleData(const std::vector<float*>& data, int frames, float scale) {
59 for (size_t ch = 0; ch < data.size(); ++ch) {
60 for (int f = 0; f < frames; ++f) {
61 data[ch][f] *= scale;
62 }
63 }
64 }
65
66 void CompareData(const std::vector<float*>& expected,
67 const std::vector<float*>& actual,
68 int frames) {
69 ASSERT_EQ(expected.size(), actual.size());
70 for (size_t ch = 0; ch < expected.size(); ++ch) {
71 for (int f = 0; f < frames; ++f) {
72 ASSERT_FLOAT_EQ(expected[ch][f], actual[ch][f])
bcf 2017/05/03 06:14:06 EXPECT_FLOAT_EQ
bshaya 2017/05/03 16:51:55 Done.
73 << "ch: " << ch << " f: " << f;
74 }
75 }
76 }
77
78 } // namespace
79
80 class GovernorTest : public ::testing::TestWithParam<float> {
81 protected:
82 GovernorTest() = default;
83 ~GovernorTest() = default;
84 void SetUp() override {
85 clamp_ = kDefaultClamp;
86 onset_volume_ = GetParam();
87 std::string config = MakeConfigString(onset_volume_, clamp_);
88 governor_ = base::MakeUnique<Governor>(config, kNumChannels);
89 governor_->SetSlewTimeMsForTest(0);
90 governor_->SetSampleRate(kSampleRate);
91
92 data_bus_ = GetSineData(kNumFrames, kFrequency);
93 expected_bus_ = GetSineData(kNumFrames, kFrequency);
94 data_ = GetDataChannels(data_bus_.get());
95 expected_ = GetDataChannels(expected_bus_.get());
96 }
97
98 void CompareBuffers() { CompareData(expected_, data_, kNumFrames); }
99
100 void ProcessFrames(float volume) {
101 EXPECT_EQ(governor_->ProcessFrames(data_, kNumFrames, volume), 0);
102 }
103
104 float clamp_;
105 float onset_volume_;
106 std::unique_ptr<Governor> governor_;
107 std::unique_ptr<::media::AudioBus> data_bus_;
108 std::unique_ptr<::media::AudioBus> expected_bus_;
109 std::vector<float*> data_;
110 std::vector<float*> expected_;
111
112 private:
113 DISALLOW_COPY_AND_ASSIGN(GovernorTest);
114 };
115
116 TEST_P(GovernorTest, ZeroVolume) {
117 ProcessFrames(0.0f);
118 if (onset_volume_ <= 0.0f) {
119 ScaleData(expected_, kNumFrames, clamp_);
120 }
121 CompareBuffers();
122 }
123
124 TEST_P(GovernorTest, EpsilonBelowOnset) {
125 float volume = onset_volume_ - std::numeric_limits<float>::epsilon();
126 ProcessFrames(volume);
127 CompareBuffers();
128 }
129
130 TEST_P(GovernorTest, EpsilonAboveOnset) {
131 float volume = onset_volume_ + std::numeric_limits<float>::epsilon();
132 ProcessFrames(volume);
133 ScaleData(expected_, kNumFrames, clamp_);
134 CompareBuffers();
135 }
136
137 TEST_P(GovernorTest, MaxVolume) {
138 ProcessFrames(1.0);
139 if (onset_volume_ <= 1.0) {
140 ScaleData(expected_, kNumFrames, clamp_);
141 }
142 CompareBuffers();
143 }
144
145 INSTANTIATE_TEST_CASE_P(GovernorClampVolumeTest,
146 GovernorTest,
147 ::testing::Values(0.0f, 0.1f, 0.5f, 0.9f, 1.0f, 1.1f));
148
149 } // namespace media
150 } // namespace chromecast
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698