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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: media/base/channel_mixer_unittest.cc
diff --git a/media/base/channel_mixer_unittest.cc b/media/base/channel_mixer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c407f33a30ee3a69096b42b56ecce2f1e9805139
--- /dev/null
+++ b/media/base/channel_mixer_unittest.cc
@@ -0,0 +1,103 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// MSVC++ requires this to be set before any other includes to get M_SQRT1_2.
+#define _USE_MATH_DEFINES
+
+#include <cmath>
+
+#include "base/stringprintf.h"
+#include "media/base/audio_bus.h"
+#include "media/base/channel_mixer.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace media {
+
+// Number of frames to test with.
+enum { kFrames = 16 };
+
+// Test all possible layout conversions can be constructed and mixed.
+TEST(ChannelMixerTest, ConstructAllPossibleLayouts) {
+ for (ChannelLayout input_layout = CHANNEL_LAYOUT_MONO;
+ input_layout < CHANNEL_LAYOUT_MAX; input_layout++) {
+ for (ChannelLayout output_layout = CHANNEL_LAYOUT_MONO;
+ output_layout < CHANNEL_LAYOUT_STEREO_DOWNMIX; output_layout++) {
+ SCOPED_TRACE(base::StringPrintf(
+ "Input Layout: %d, Output Layout: %d", input_layout, output_layout));
+ ChannelMixer mixer(input_layout, output_layout);
+ scoped_ptr<AudioBus> input_bus = AudioBus::Create(
+ ChannelLayoutToChannelCount(input_layout), kFrames);
+ scoped_ptr<AudioBus> output_bus = AudioBus::Create(
+ ChannelLayoutToChannelCount(output_layout), kFrames);
+ for (int ch = 0; ch < input_bus->channels(); ++ch)
+ std::fill(input_bus->channel(ch), input_bus->channel(ch) + kFrames, 1);
+
+ mixer.Rematrix(input_bus.get(), output_bus.get());
+ }
+ }
+}
+
+// Tuple containing the input layout, output layout, fill value for each input
+// channel, the number of channel values, and the expected channel scale.
+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
+ChannelMixerTestData;
+class ChannelMixerTest
+ : public testing::TestWithParam<ChannelMixerTestData> {
+};
+
+// Verify channels are mixed and scaled correctly. The test only works if all
+// output channels have the same value.
+TEST_P(ChannelMixerTest, Mixing) {
+ ChannelLayout input_layout = std::tr1::get<0>(GetParam());
+ ChannelLayout output_layout = std::tr1::get<1>(GetParam());
+
+ SCOPED_TRACE(base::StringPrintf(
+ "Input Layout: %d, Output Layout: %d", input_layout, output_layout));
+
+ ChannelMixer mixer(input_layout, output_layout);
+ scoped_ptr<AudioBus> input_bus = AudioBus::Create(
+ ChannelLayoutToChannelCount(input_layout), kFrames);
+ scoped_ptr<AudioBus> output_bus = AudioBus::Create(
+ ChannelLayoutToChannelCount(output_layout), kFrames);
+
+ float* channel_values = std::tr1::get<2>(GetParam());
+ int num_channel_values = std::tr1::get<3>(GetParam());
+ ASSERT_EQ(input_bus->channels(), num_channel_values);
+
+ float expected_value = 0;
+ float scale = std::tr1::get<4>(GetParam());
+ for (int ch = 0; ch < input_bus->channels(); ++ch) {
+ std::fill(input_bus->channel(ch), input_bus->channel(ch) + kFrames,
+ channel_values[ch]);
+ expected_value += channel_values[ch] * scale;
+ }
+
+ mixer.Rematrix(input_bus.get(), output_bus.get());
+
+ for (int ch = 0; ch < output_bus->channels(); ++ch) {
+ for (int frame = 0; frame < output_bus->frames(); ++frame) {
+ ASSERT_FLOAT_EQ(output_bus->channel(ch)[frame], expected_value);
+ }
+ }
+}
+
+static float kStereoToMonoValues[] = { 0.5, 0.75 };
+static float kMonoToStereoValues[] = { 0.5 };
+// Zero the center channel since it will be mixed at scale 1 vs M_SQRT1_2.
+static float kFiveOneToMonoValues[] = { 0.1, 0.2, 0, 0.4, 0.5, 0.6 };
+
+// Run through basic sanity tests for some common conversions.
+INSTANTIATE_TEST_CASE_P(ChannelMixerTest, ChannelMixerTest, testing::Values(
+ std::tr1::make_tuple(CHANNEL_LAYOUT_STEREO, CHANNEL_LAYOUT_MONO,
+ kStereoToMonoValues, arraysize(kStereoToMonoValues),
+ 0.5f),
+ std::tr1::make_tuple(CHANNEL_LAYOUT_MONO, CHANNEL_LAYOUT_STEREO,
+ kMonoToStereoValues, arraysize(kMonoToStereoValues),
+ 1.0f),
+ std::tr1::make_tuple(CHANNEL_LAYOUT_5_1, CHANNEL_LAYOUT_MONO,
+ kFiveOneToMonoValues, arraysize(kFiveOneToMonoValues),
+ static_cast<float>(M_SQRT1_2))
+));
+
+} // namespace media
« 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