1 // Copyright (c) 2012 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 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2.
6 #define _USE_MATH_DEFINES
10 #include "base/strings/stringprintf.h"
11 #include "media/audio/audio_parameters.h"
12 #include "media/base/audio_bus.h"
13 #include "media/base/channel_mixer.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 // Number of frames to test with.
19 enum { kFrames
= 16 };
21 // Test all possible layout conversions can be constructed and mixed.
22 TEST(ChannelMixerTest
, ConstructAllPossibleLayouts
) {
23 for (ChannelLayout input_layout
= CHANNEL_LAYOUT_MONO
;
24 input_layout
< CHANNEL_LAYOUT_MAX
;
25 input_layout
= static_cast<ChannelLayout
>(input_layout
+ 1)) {
26 for (ChannelLayout output_layout
= CHANNEL_LAYOUT_MONO
;
27 output_layout
< CHANNEL_LAYOUT_STEREO_DOWNMIX
;
28 output_layout
= static_cast<ChannelLayout
>(output_layout
+ 1)) {
29 // DISCRETE can't be tested here based on the current approach.
30 if (input_layout
== CHANNEL_LAYOUT_DISCRETE
||
31 output_layout
== CHANNEL_LAYOUT_DISCRETE
)
34 SCOPED_TRACE(base::StringPrintf(
35 "Input Layout: %d, Output Layout: %d", input_layout
, output_layout
));
36 ChannelMixer
mixer(input_layout
, output_layout
);
37 scoped_ptr
<AudioBus
> input_bus
= AudioBus::Create(
38 ChannelLayoutToChannelCount(input_layout
), kFrames
);
39 scoped_ptr
<AudioBus
> output_bus
= AudioBus::Create(
40 ChannelLayoutToChannelCount(output_layout
), kFrames
);
41 for (int ch
= 0; ch
< input_bus
->channels(); ++ch
)
42 std::fill(input_bus
->channel(ch
), input_bus
->channel(ch
) + kFrames
, 1);
44 mixer
.Transform(input_bus
.get(), output_bus
.get());
49 struct ChannelMixerTestData
{
50 ChannelMixerTestData(ChannelLayout input_layout
, ChannelLayout output_layout
,
51 float* channel_values
, int num_channel_values
,
53 : input_layout(input_layout
),
54 output_layout(output_layout
),
55 channel_values(channel_values
),
56 num_channel_values(num_channel_values
),
58 input_channels
= ChannelLayoutToChannelCount(input_layout
);
59 output_channels
= ChannelLayoutToChannelCount(output_layout
);
62 ChannelMixerTestData(ChannelLayout input_layout
, int input_channels
,
63 ChannelLayout output_layout
, int output_channels
,
64 float* channel_values
, int num_channel_values
)
65 : input_layout(input_layout
),
66 input_channels(input_channels
),
67 output_layout(output_layout
),
68 output_channels(output_channels
),
69 channel_values(channel_values
),
70 num_channel_values(num_channel_values
),
74 std::string
DebugString() const {
75 return base::StringPrintf(
76 "Input Layout: %d, Output Layout %d, Scale: %f", input_layout
,
77 output_layout
, scale
);
80 ChannelLayout input_layout
;
82 ChannelLayout output_layout
;
84 float* channel_values
;
85 int num_channel_values
;
89 std::ostream
& operator<<(std::ostream
& os
, const ChannelMixerTestData
& data
) {
90 return os
<< data
.DebugString();
93 class ChannelMixerTest
: public testing::TestWithParam
<ChannelMixerTestData
> {};
95 // Verify channels are mixed and scaled correctly. The test only works if all
96 // output channels have the same value.
97 TEST_P(ChannelMixerTest
, Mixing
) {
98 ChannelLayout input_layout
= GetParam().input_layout
;
99 int input_channels
= GetParam().input_channels
;
100 scoped_ptr
<AudioBus
> input_bus
= AudioBus::Create(input_channels
, kFrames
);
101 AudioParameters
input_audio(AudioParameters::AUDIO_PCM_LINEAR
,
103 AudioParameters::kAudioCDSampleRate
, 16,
105 if (input_layout
== CHANNEL_LAYOUT_DISCRETE
)
106 input_audio
.SetDiscreteChannels(input_channels
);
108 ChannelLayout output_layout
= GetParam().output_layout
;
109 int output_channels
= GetParam().output_channels
;
110 scoped_ptr
<AudioBus
> output_bus
= AudioBus::Create(output_channels
, kFrames
);
111 AudioParameters
output_audio(AudioParameters::AUDIO_PCM_LINEAR
,
113 AudioParameters::kAudioCDSampleRate
, 16,
115 if (output_layout
== CHANNEL_LAYOUT_DISCRETE
)
116 output_audio
.SetDiscreteChannels(output_channels
);
118 const float* channel_values
= GetParam().channel_values
;
119 ASSERT_EQ(input_bus
->channels(), GetParam().num_channel_values
);
121 float expected_value
= 0;
122 float scale
= GetParam().scale
;
123 for (int ch
= 0; ch
< input_bus
->channels(); ++ch
) {
124 std::fill(input_bus
->channel(ch
), input_bus
->channel(ch
) + kFrames
,
126 expected_value
+= channel_values
[ch
] * scale
;
129 ChannelMixer
mixer(input_audio
, output_audio
);
130 mixer
.Transform(input_bus
.get(), output_bus
.get());
132 // Validate the output channel
133 if (input_layout
!= CHANNEL_LAYOUT_DISCRETE
) {
134 for (int ch
= 0; ch
< output_bus
->channels(); ++ch
) {
135 for (int frame
= 0; frame
< output_bus
->frames(); ++frame
) {
136 ASSERT_FLOAT_EQ(output_bus
->channel(ch
)[frame
], expected_value
);
140 // Processing discrete mixing. If there is a matching input channel,
141 // then the output channel should be set. If no input channel,
142 // output channel should be 0
143 for (int ch
= 0; ch
< output_bus
->channels(); ++ch
) {
144 expected_value
= (ch
< input_channels
) ? channel_values
[ch
] : 0;
145 for (int frame
= 0; frame
< output_bus
->frames(); ++frame
) {
146 ASSERT_FLOAT_EQ(output_bus
->channel(ch
)[frame
], expected_value
);
152 static float kStereoToMonoValues
[] = { 0.5f
, 0.75f
};
153 static float kMonoToStereoValues
[] = { 0.5f
};
154 // Zero the center channel since it will be mixed at scale 1 vs M_SQRT1_2.
155 static float kFiveOneToMonoValues
[] = { 0.1f
, 0.2f
, 0.0f
, 0.4f
, 0.5f
, 0.6f
};
156 static float kFiveDiscreteValues
[] = { 0.1f
, 0.2f
, 0.3f
, 0.4f
, 0.5f
};
158 // Run through basic sanity tests for some common conversions.
159 INSTANTIATE_TEST_CASE_P(ChannelMixerTest
, ChannelMixerTest
, testing::Values(
160 ChannelMixerTestData(CHANNEL_LAYOUT_STEREO
, CHANNEL_LAYOUT_MONO
,
161 kStereoToMonoValues
, arraysize(kStereoToMonoValues
),
163 ChannelMixerTestData(CHANNEL_LAYOUT_MONO
, CHANNEL_LAYOUT_STEREO
,
164 kMonoToStereoValues
, arraysize(kMonoToStereoValues
),
166 ChannelMixerTestData(CHANNEL_LAYOUT_5_1
, CHANNEL_LAYOUT_MONO
,
167 kFiveOneToMonoValues
, arraysize(kFiveOneToMonoValues
),
168 static_cast<float>(M_SQRT1_2
)),
169 ChannelMixerTestData(CHANNEL_LAYOUT_DISCRETE
, 2,
170 CHANNEL_LAYOUT_DISCRETE
, 2,
171 kStereoToMonoValues
, arraysize(kStereoToMonoValues
)),
172 ChannelMixerTestData(CHANNEL_LAYOUT_DISCRETE
, 2,
173 CHANNEL_LAYOUT_DISCRETE
, 5,
174 kStereoToMonoValues
, arraysize(kStereoToMonoValues
)),
175 ChannelMixerTestData(CHANNEL_LAYOUT_DISCRETE
, 5,
176 CHANNEL_LAYOUT_DISCRETE
, 2,
177 kFiveDiscreteValues
, arraysize(kFiveDiscreteValues
))