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

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

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

Powered by Google App Engine
This is Rietveld 408576698