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"
10 #include "base/bind_helpers.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_bus.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 static const int kSampleRate
= 48000;
19 static const int kFramesPerBuffer
= 128;
21 static const int kTimeConstantMillis
= 5;
22 static const int kMeasurementPeriodMillis
= 20;
26 // Container for each parameterized test's data (input and expected results).
29 TestScenario(const float* data
, int num_channels
, int num_frames
,
30 float expected_power
, bool expected_clipped
)
31 : expected_power_(expected_power
), expected_clipped_(expected_clipped
) {
32 CreatePopulatedBuffer(data
, num_channels
, num_frames
);
35 // Copy constructor and assignment operator for ::testing::Values(...).
36 TestScenario(const TestScenario
& other
) { *this = other
; }
37 TestScenario
& operator=(const TestScenario
& other
) {
38 this->expected_power_
= other
.expected_power_
;
39 this->expected_clipped_
= other
.expected_clipped_
;
40 this->bus_
= AudioBus::Create(other
.bus_
->channels(), other
.bus_
->frames());
41 other
.bus_
->CopyTo(this->bus_
.get());
45 // Returns this TestScenario, but with a bad sample value placed in the middle
47 TestScenario
WithABadSample(float bad_value
) const {
48 TestScenario
result(*this);
49 result
.bus_
->channel(0)[result
.bus_
->frames() / 2] = bad_value
;
53 const AudioBus
& data() const {
57 float expected_power() const {
58 return expected_power_
;
61 bool expected_clipped() const {
62 return expected_clipped_
;
66 // Creates an AudioBus, sized and populated with kFramesPerBuffer frames of
67 // data. The given test |data| is repeated to fill the buffer.
68 void CreatePopulatedBuffer(
69 const float* data
, int num_channels
, int num_frames
) {
70 bus_
= AudioBus::Create(num_channels
, kFramesPerBuffer
);
71 for (int ch
= 0; ch
< num_channels
; ++ch
) {
72 for (int frames
= 0; frames
< kFramesPerBuffer
; frames
+= num_frames
) {
73 const int num_to_copy
= std::min(num_frames
, kFramesPerBuffer
- frames
);
74 memcpy(bus_
->channel(ch
) + frames
, data
+ num_frames
* ch
,
75 sizeof(float) * num_to_copy
);
80 float expected_power_
;
81 bool expected_clipped_
;
82 scoped_ptr
<AudioBus
> bus_
;
85 // Value printer for TestScenario. Required to prevent Valgrind "access to
86 // uninitialized memory" errors (http://crbug.com/263315).
87 ::std::ostream
& operator<<(::std::ostream
& os
, const TestScenario
& ts
) {
88 return os
<< "{" << ts
.data().channels() << "-channel signal} --> {"
89 << ts
.expected_power() << " dBFS, "
90 << (ts
.expected_clipped() ? "clipped" : "not clipped")
94 // An observer that receives power measurements. Each power measurement should
95 // should make progress towards the goal value.
96 class MeasurementObserver
{
98 MeasurementObserver(float goal_power_measurement
, bool goal_clipped
)
99 : goal_power_measurement_(goal_power_measurement
),
100 goal_clipped_(goal_clipped
), measurement_count_(0),
101 last_power_measurement_(AudioPowerMonitor::zero_power()),
102 last_clipped_(false) {}
104 int measurement_count() const {
105 return measurement_count_
;
108 float last_power_measurement() const {
109 return last_power_measurement_
;
112 bool last_clipped() const {
113 return last_clipped_
;
116 void OnPowerMeasured(float cur_power_measurement
, bool clipped
) {
117 if (measurement_count_
== 0) {
118 measurements_should_increase_
=
119 (cur_power_measurement
< goal_power_measurement_
);
121 SCOPED_TRACE(::testing::Message()
122 << "Power: goal=" << goal_power_measurement_
123 << "; last=" << last_power_measurement_
124 << "; cur=" << cur_power_measurement
);
126 if (last_power_measurement_
!= goal_power_measurement_
) {
127 if (measurements_should_increase_
) {
128 EXPECT_LE(last_power_measurement_
, cur_power_measurement
)
129 << "Measurements should be monotonically increasing.";
131 EXPECT_GE(last_power_measurement_
, cur_power_measurement
)
132 << "Measurements should be monotonically decreasing.";
135 EXPECT_EQ(last_power_measurement_
, cur_power_measurement
)
136 << "Measurements are numerically unstable at goal value.";
140 last_power_measurement_
= cur_power_measurement
;
141 last_clipped_
= clipped
;
142 ++measurement_count_
;
146 const float goal_power_measurement_
;
147 const bool goal_clipped_
;
148 int measurement_count_
;
149 bool measurements_should_increase_
;
150 float last_power_measurement_
;
153 DISALLOW_COPY_AND_ASSIGN(MeasurementObserver
);
158 class AudioPowerMonitorTest
: public ::testing::TestWithParam
<TestScenario
> {
160 AudioPowerMonitorTest()
163 base::TimeDelta::FromMilliseconds(kTimeConstantMillis
),
164 base::TimeDelta::FromMilliseconds(kMeasurementPeriodMillis
),
166 base::Bind(&AudioPowerMonitorTest::OnPowerMeasured
,
167 base::Unretained(this))) {}
169 void FeedAndCheckExpectedPowerIsMeasured(
170 const AudioBus
& bus
, float power
, bool clipped
) {
171 // Feed the AudioPowerMonitor. It should post tasks to |message_loop_|.
172 static const int kNumFeedIters
= 100;
173 for (int i
= 0; i
< kNumFeedIters
; ++i
)
174 power_monitor_
.Scan(bus
, bus
.frames());
176 // Set up an observer and run all the enqueued tasks.
177 MeasurementObserver
observer(power
, clipped
);
178 current_observer_
= &observer
;
179 message_loop_
.RunUntilIdle();
180 current_observer_
= NULL
;
182 // Check that the results recorded by the observer are the same whole-number
184 EXPECT_EQ(static_cast<int>(power
),
185 static_cast<int>(observer
.last_power_measurement()));
186 EXPECT_EQ(clipped
, observer
.last_clipped());
190 void OnPowerMeasured(float power
, bool clipped
) {
191 CHECK(current_observer_
);
192 current_observer_
->OnPowerMeasured(power
, clipped
);
195 base::MessageLoop message_loop_
;
196 AudioPowerMonitor power_monitor_
;
197 MeasurementObserver
* current_observer_
;
199 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitorTest
);
202 TEST_P(AudioPowerMonitorTest
, MeasuresPowerOfSignal
) {
203 const TestScenario
& scenario
= GetParam();
205 scoped_ptr
<AudioBus
> zeroed_bus
=
206 AudioBus::Create(scenario
.data().channels(), scenario
.data().frames());
209 // Send a "zero power" audio signal, then this scenario's audio signal, then
210 // the "zero power" audio signal again; testing that the power monitor
211 // measurements match expected values.
212 FeedAndCheckExpectedPowerIsMeasured(
213 *zeroed_bus
, AudioPowerMonitor::zero_power(), false);
214 FeedAndCheckExpectedPowerIsMeasured(
215 scenario
.data(), scenario
.expected_power(), scenario
.expected_clipped());
216 FeedAndCheckExpectedPowerIsMeasured(
217 *zeroed_bus
, AudioPowerMonitor::zero_power(), false);
220 static const float kMonoSilentNoise
[] = {
224 static const float kMonoMaxAmplitude
[] = {
228 static const float kMonoMaxAmplitude2
[] = {
232 static const float kMonoHalfMaxAmplitude
[] = {
233 0.5f
, -0.5f
, 0.5f
, -0.5f
236 static const float kMonoAmplitudeClipped
[] = {
240 static const float kMonoMaxAmplitudeWithClip
[] = {
241 2.0f
, 0.0, 0.0f
, 0.0f
244 static const float kMonoMaxAmplitudeWithClip2
[] = {
245 4.0f
, 0.0, 0.0f
, 0.0f
248 static const float kStereoSilentNoise
[] = {
255 static const float kStereoMaxAmplitude
[] = {
262 static const float kRightChannelMaxAmplitude
[] = {
264 0.0f
, 0.0f
, 0.0f
, 0.0f
,
266 -1.0f
, 1.0f
, -1.0f
, 1.0f
269 static const float kLeftChannelHalfMaxAmplitude
[] = {
271 0.5f
, -0.5f
, 0.5f
, -0.5f
,
273 0.0f
, 0.0f
, 0.0f
, 0.0f
,
276 static const float kStereoMixed
[] = {
278 0.5f
, -0.5f
, 0.5f
, -0.5f
,
280 -1.0f
, 1.0f
, -1.0f
, 1.0f
283 static const float kStereoMixed2
[] = {
285 1.0f
, -1.0f
, 0.75f
, -0.75f
, 0.5f
, -0.5f
, 0.25f
, -0.25f
,
287 0.25f
, -0.25f
, 0.5f
, -0.5f
, 0.75f
, -0.75f
, 1.0f
, -1.0f
290 INSTANTIATE_TEST_CASE_P(
291 Scenarios
, AudioPowerMonitorTest
,
293 TestScenario(kMonoSilentNoise
, 1, 2, -40, false),
294 TestScenario(kMonoMaxAmplitude
, 1, 1,
295 AudioPowerMonitor::max_power(), false),
296 TestScenario(kMonoMaxAmplitude2
, 1, 2,
297 AudioPowerMonitor::max_power(), false),
298 TestScenario(kMonoHalfMaxAmplitude
, 1, 4, -6, false),
299 TestScenario(kMonoAmplitudeClipped
, 1, 2,
300 AudioPowerMonitor::max_power(), true),
301 TestScenario(kMonoMaxAmplitudeWithClip
, 1, 4,
302 AudioPowerMonitor::max_power(), true),
303 TestScenario(kMonoMaxAmplitudeWithClip2
, 1, 4,
304 AudioPowerMonitor::max_power(), true),
305 TestScenario(kMonoSilentNoise
, 1, 2,
306 AudioPowerMonitor::zero_power(), true).
307 WithABadSample(std::numeric_limits
<float>::infinity()),
308 TestScenario(kMonoHalfMaxAmplitude
, 1, 4,
309 AudioPowerMonitor::zero_power(), false).
310 WithABadSample(std::numeric_limits
<float>::quiet_NaN()),
311 TestScenario(kStereoSilentNoise
, 2, 2, -46, false),
312 TestScenario(kStereoMaxAmplitude
, 2, 2,
313 AudioPowerMonitor::max_power(), false),
314 TestScenario(kRightChannelMaxAmplitude
, 2, 4, -3, false),
315 TestScenario(kLeftChannelHalfMaxAmplitude
, 2, 4, -9, false),
316 TestScenario(kStereoMixed
, 2, 4, -2, false),
317 TestScenario(kStereoMixed2
, 2, 8, -3, false)));