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

Side by Side Diff: media/base/channel_mixer_unittest.cc

Issue 11150034: Add support for channel transforms. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Unit tests. Created 8 years, 2 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 // MSVC++ requires this to be set before any other includes to get M_SQRT1_2.
6 #define _USE_MATH_DEFINES
7
8 #include <cmath>
9
10 #include "base/stringprintf.h"
11 #include "media/base/audio_bus.h"
12 #include "media/base/channel_mixer.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace media {
16
17 // Number of frames to test with.
18 enum { kFrames = 16 };
19
20 // Test all possible layout conversions can be constructed and mixed.
21 TEST(ChannelMixerTest, ConstructAllPossibleLayouts) {
22 for (ChannelLayout input_layout = CHANNEL_LAYOUT_MONO;
23 input_layout < CHANNEL_LAYOUT_MAX; input_layout++) {
24 for (ChannelLayout output_layout = CHANNEL_LAYOUT_MONO;
25 output_layout < CHANNEL_LAYOUT_STEREO_DOWNMIX; output_layout++) {
26 SCOPED_TRACE(base::StringPrintf(
27 "Input Layout: %d, Output Layout: %d", input_layout, output_layout));
28 ChannelMixer mixer(input_layout, output_layout);
29 scoped_ptr<AudioBus> input_bus = AudioBus::Create(
30 ChannelLayoutToChannelCount(input_layout), kFrames);
31 scoped_ptr<AudioBus> output_bus = AudioBus::Create(
32 ChannelLayoutToChannelCount(output_layout), kFrames);
33 for (int ch = 0; ch < input_bus->channels(); ++ch)
34 std::fill(input_bus->channel(ch), input_bus->channel(ch) + kFrames, 1);
35
36 mixer.Rematrix(input_bus.get(), output_bus.get());
37 }
38 }
39 }
40
41 // Tuple containing the input layout, output layout, fill value for each input
42 // channel, the number of channel values, and the expected channel scale.
43 typedef std::tr1::tuple<ChannelLayout, ChannelLayout, float*, int, float>
scherkus (not reviewing) 2012/10/18 05:39:23 hrmmm... is it possible to use a struct instead of
DaleCurtis 2012/10/18 06:30:38 Yeah, it's about 30 lines of boiler plate though.
DaleCurtis 2012/10/18 06:46:05 Argh, windows doesn't like this: http://build.chro
44 ChannelMixerTestData;
45 class ChannelMixerTest
46 : public testing::TestWithParam<ChannelMixerTestData> {
47 };
48
49 // Verify channels are mixed and scaled correctly. The test only works if all
50 // output channels have the same value.
51 TEST_P(ChannelMixerTest, Mixing) {
52 ChannelLayout input_layout = std::tr1::get<0>(GetParam());
53 ChannelLayout output_layout = std::tr1::get<1>(GetParam());
54
55 SCOPED_TRACE(base::StringPrintf(
56 "Input Layout: %d, Output Layout: %d", input_layout, output_layout));
57
58 ChannelMixer mixer(input_layout, output_layout);
59 scoped_ptr<AudioBus> input_bus = AudioBus::Create(
60 ChannelLayoutToChannelCount(input_layout), kFrames);
61 scoped_ptr<AudioBus> output_bus = AudioBus::Create(
62 ChannelLayoutToChannelCount(output_layout), kFrames);
63
64 float* channel_values = std::tr1::get<2>(GetParam());
65 int num_channel_values = std::tr1::get<3>(GetParam());
66 ASSERT_EQ(input_bus->channels(), num_channel_values);
67
68 float expected_value = 0;
69 float scale = std::tr1::get<4>(GetParam());
70 for (int ch = 0; ch < input_bus->channels(); ++ch) {
71 std::fill(input_bus->channel(ch), input_bus->channel(ch) + kFrames,
72 channel_values[ch]);
73 expected_value += channel_values[ch] * scale;
74 }
75
76 mixer.Rematrix(input_bus.get(), output_bus.get());
77
78 for (int ch = 0; ch < output_bus->channels(); ++ch) {
79 for (int frame = 0; frame < output_bus->frames(); ++frame) {
80 ASSERT_FLOAT_EQ(output_bus->channel(ch)[frame], expected_value);
81 }
82 }
83 }
84
85 static float kStereoToMonoValues[] = { 0.5, 0.75 };
86 static float kMonoToStereoValues[] = { 0.5 };
87 // Zero the center channel since it will be mixed at scale 1 vs M_SQRT1_2.
88 static float kFiveOneToMonoValues[] = { 0.1, 0.2, 0, 0.4, 0.5, 0.6 };
89
90 // Run through basic sanity tests for some common conversions.
91 INSTANTIATE_TEST_CASE_P(ChannelMixerTest, ChannelMixerTest, testing::Values(
92 std::tr1::make_tuple(CHANNEL_LAYOUT_STEREO, CHANNEL_LAYOUT_MONO,
93 kStereoToMonoValues, arraysize(kStereoToMonoValues),
94 0.5f),
95 std::tr1::make_tuple(CHANNEL_LAYOUT_MONO, CHANNEL_LAYOUT_STEREO,
96 kMonoToStereoValues, arraysize(kMonoToStereoValues),
97 1.0f),
98 std::tr1::make_tuple(CHANNEL_LAYOUT_5_1, CHANNEL_LAYOUT_MONO,
99 kFiveOneToMonoValues, arraysize(kFiveOneToMonoValues),
100 static_cast<float>(M_SQRT1_2))
101 ));
102
103 } // namespace media
OLDNEW
« media/base/channel_mixer.cc ('K') | « media/base/channel_mixer.cc ('k') | media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698