1 // Copyright 2013 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.
5 #include "media/audio/audio_power_monitor.h"
9 #include "base/time/time.h"
10 #include "media/base/audio_bus.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 static const int kSampleRate
= 48000;
16 static const int kFramesPerBuffer
= 128;
18 static const int kTimeConstantMillis
= 5;
22 // Container for each parameterized test's data (input and expected results).
25 TestScenario(const float* data
, int num_channels
, int num_frames
,
26 float expected_power
, bool expected_clipped
)
27 : expected_power_(expected_power
), expected_clipped_(expected_clipped
) {
28 CreatePopulatedBuffer(data
, num_channels
, num_frames
);
31 // Copy constructor and assignment operator for ::testing::Values(...).
32 TestScenario(const TestScenario
& other
) { *this = other
; }
33 TestScenario
& operator=(const TestScenario
& other
) {
34 this->expected_power_
= other
.expected_power_
;
35 this->expected_clipped_
= other
.expected_clipped_
;
36 this->bus_
= AudioBus::Create(other
.bus_
->channels(), other
.bus_
->frames());
37 other
.bus_
->CopyTo(this->bus_
.get());
41 // Returns this TestScenario, but with a bad sample value placed in the middle
43 TestScenario
WithABadSample(float bad_value
) const {
44 TestScenario
result(*this);
45 result
.bus_
->channel(0)[result
.bus_
->frames() / 2] = bad_value
;
49 const AudioBus
& data() const {
53 float expected_power() const {
54 return expected_power_
;
57 bool expected_clipped() const {
58 return expected_clipped_
;
62 // Creates an AudioBus, sized and populated with kFramesPerBuffer frames of
63 // data. The given test |data| is repeated to fill the buffer.
64 void CreatePopulatedBuffer(
65 const float* data
, int num_channels
, int num_frames
) {
66 bus_
= AudioBus::Create(num_channels
, kFramesPerBuffer
);
67 for (int ch
= 0; ch
< num_channels
; ++ch
) {
68 for (int frames
= 0; frames
< kFramesPerBuffer
; frames
+= num_frames
) {
69 const int num_to_copy
= std::min(num_frames
, kFramesPerBuffer
- frames
);
70 memcpy(bus_
->channel(ch
) + frames
, data
+ num_frames
* ch
,
71 sizeof(float) * num_to_copy
);
76 float expected_power_
;
77 bool expected_clipped_
;
78 scoped_ptr
<AudioBus
> bus_
;
81 // Value printer for TestScenario. Required to prevent Valgrind "access to
82 // uninitialized memory" errors (http://crbug.com/263315).
83 ::std::ostream
& operator<<(::std::ostream
& os
, const TestScenario
& ts
) {
84 return os
<< "{" << ts
.data().channels() << "-channel signal} --> {"
85 << ts
.expected_power() << " dBFS, "
86 << (ts
.expected_clipped() ? "clipped" : "not clipped")
90 // An observer that receives power measurements. Each power measurement should
91 // should make progress towards the goal value.
92 class MeasurementObserver
{
94 explicit MeasurementObserver(float goal_power_measurement
)
95 : goal_power_measurement_(goal_power_measurement
),
96 measurement_count_(0),
97 last_power_measurement_(AudioPowerMonitor::zero_power()),
98 last_clipped_(false) {}
100 int measurement_count() const {
101 return measurement_count_
;
104 float last_power_measurement() const {
105 return last_power_measurement_
;
108 bool last_clipped() const {
109 return last_clipped_
;
112 void OnPowerMeasured(float cur_power_measurement
, bool clipped
) {
113 if (measurement_count_
== 0) {
114 measurements_should_increase_
=
115 (cur_power_measurement
< goal_power_measurement_
);
117 SCOPED_TRACE(::testing::Message()
118 << "Power: goal=" << goal_power_measurement_
119 << "; last=" << last_power_measurement_
120 << "; cur=" << cur_power_measurement
);
122 if (last_power_measurement_
!= goal_power_measurement_
) {
123 if (measurements_should_increase_
) {
124 EXPECT_LE(last_power_measurement_
, cur_power_measurement
)
125 << "Measurements should be monotonically increasing.";
127 EXPECT_GE(last_power_measurement_
, cur_power_measurement
)
128 << "Measurements should be monotonically decreasing.";
131 EXPECT_EQ(last_power_measurement_
, cur_power_measurement
)
132 << "Measurements are numerically unstable at goal value.";
136 last_power_measurement_
= cur_power_measurement
;
137 last_clipped_
= clipped
;
138 ++measurement_count_
;
142 const float goal_power_measurement_
;
143 int measurement_count_
;
144 bool measurements_should_increase_
;
145 float last_power_measurement_
;
148 DISALLOW_COPY_AND_ASSIGN(MeasurementObserver
);
153 class AudioPowerMonitorTest
: public ::testing::TestWithParam
<TestScenario
> {
155 AudioPowerMonitorTest()
156 : power_monitor_(kSampleRate
,
157 base::TimeDelta::FromMilliseconds(kTimeConstantMillis
)) {
160 void FeedAndCheckExpectedPowerIsMeasured(
161 const AudioBus
& bus
, float power
, bool clipped
) {
162 // Feed the AudioPowerMonitor, read measurements from it, and record them in
163 // MeasurementObserver.
164 static const int kNumFeedIters
= 100;
165 MeasurementObserver
observer(power
);
166 for (int i
= 0; i
< kNumFeedIters
; ++i
) {
167 power_monitor_
.Scan(bus
, bus
.frames());
168 const std::pair
<float, bool>& reading
=
169 power_monitor_
.ReadCurrentPowerAndClip();
170 observer
.OnPowerMeasured(reading
.first
, reading
.second
);
173 // Check that the results recorded by the observer are the same whole-number
175 EXPECT_EQ(static_cast<int>(power
),
176 static_cast<int>(observer
.last_power_measurement()));
177 EXPECT_EQ(clipped
, observer
.last_clipped());
181 AudioPowerMonitor power_monitor_
;
183 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitorTest
);
186 TEST_P(AudioPowerMonitorTest
, MeasuresPowerOfSignal
) {
187 const TestScenario
& scenario
= GetParam();
189 scoped_ptr
<AudioBus
> zeroed_bus
=
190 AudioBus::Create(scenario
.data().channels(), scenario
.data().frames());
193 // Send a "zero power" audio signal, then this scenario's audio signal, then
194 // the "zero power" audio signal again; testing that the power monitor
195 // measurements match expected values.
196 FeedAndCheckExpectedPowerIsMeasured(
197 *zeroed_bus
, AudioPowerMonitor::zero_power(), false);
198 FeedAndCheckExpectedPowerIsMeasured(
199 scenario
.data(), scenario
.expected_power(), scenario
.expected_clipped());
200 FeedAndCheckExpectedPowerIsMeasured(
201 *zeroed_bus
, AudioPowerMonitor::zero_power(), false);
204 static const float kMonoSilentNoise
[] = {
208 static const float kMonoMaxAmplitude
[] = {
212 static const float kMonoMaxAmplitude2
[] = {
216 static const float kMonoHalfMaxAmplitude
[] = {
217 0.5f
, -0.5f
, 0.5f
, -0.5f
220 static const float kMonoAmplitudeClipped
[] = {
224 static const float kMonoMaxAmplitudeWithClip
[] = {
225 2.0f
, 0.0, 0.0f
, 0.0f
228 static const float kMonoMaxAmplitudeWithClip2
[] = {
229 4.0f
, 0.0, 0.0f
, 0.0f
232 static const float kStereoSilentNoise
[] = {
239 static const float kStereoMaxAmplitude
[] = {
246 static const float kRightChannelMaxAmplitude
[] = {
248 0.0f
, 0.0f
, 0.0f
, 0.0f
,
250 -1.0f
, 1.0f
, -1.0f
, 1.0f
253 static const float kLeftChannelHalfMaxAmplitude
[] = {
255 0.5f
, -0.5f
, 0.5f
, -0.5f
,
257 0.0f
, 0.0f
, 0.0f
, 0.0f
,
260 static const float kStereoMixed
[] = {
262 0.5f
, -0.5f
, 0.5f
, -0.5f
,
264 -1.0f
, 1.0f
, -1.0f
, 1.0f
267 static const float kStereoMixed2
[] = {
269 1.0f
, -1.0f
, 0.75f
, -0.75f
, 0.5f
, -0.5f
, 0.25f
, -0.25f
,
271 0.25f
, -0.25f
, 0.5f
, -0.5f
, 0.75f
, -0.75f
, 1.0f
, -1.0f
274 INSTANTIATE_TEST_CASE_P(
275 Scenarios
, AudioPowerMonitorTest
,
277 TestScenario(kMonoSilentNoise
, 1, 2, -40, false),
278 TestScenario(kMonoMaxAmplitude
, 1, 1,
279 AudioPowerMonitor::max_power(), false),
280 TestScenario(kMonoMaxAmplitude2
, 1, 2,
281 AudioPowerMonitor::max_power(), false),
282 TestScenario(kMonoHalfMaxAmplitude
, 1, 4, -6, false),
283 TestScenario(kMonoAmplitudeClipped
, 1, 2,
284 AudioPowerMonitor::max_power(), true),
285 TestScenario(kMonoMaxAmplitudeWithClip
, 1, 4,
286 AudioPowerMonitor::max_power(), true),
287 TestScenario(kMonoMaxAmplitudeWithClip2
, 1, 4,
288 AudioPowerMonitor::max_power(), true),
289 TestScenario(kMonoSilentNoise
, 1, 2,
290 AudioPowerMonitor::zero_power(), false).
291 WithABadSample(std::numeric_limits
<float>::infinity()),
292 TestScenario(kMonoHalfMaxAmplitude
, 1, 4,
293 AudioPowerMonitor::zero_power(), false).
294 WithABadSample(std::numeric_limits
<float>::quiet_NaN()),
295 TestScenario(kStereoSilentNoise
, 2, 2, -46, false),
296 TestScenario(kStereoMaxAmplitude
, 2, 2,
297 AudioPowerMonitor::max_power(), false),
298 TestScenario(kRightChannelMaxAmplitude
, 2, 4, -3, false),
299 TestScenario(kLeftChannelHalfMaxAmplitude
, 2, 4, -9, false),
300 TestScenario(kStereoMixed
, 2, 4, -2, false),
301 TestScenario(kStereoMixed2
, 2, 8, -3, false)));