blob: 3d1091d414911f614d71786e05a8dd5b74f1554c [file] [log] [blame]
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000011#include "testing/gtest/include/gtest/gtest.h"
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000012
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000013#include "webrtc/common_audio/resampler/include/resampler.h"
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000014
15// TODO(andrew): this is a work-in-progress. Many more tests are needed.
16
17namespace webrtc {
18namespace {
19const ResamplerType kTypes[] = {
20 kResamplerSynchronous,
21 kResamplerAsynchronous,
22 kResamplerSynchronousStereo,
23 kResamplerAsynchronousStereo
24 // kResamplerInvalid excluded
25};
26const size_t kTypesSize = sizeof(kTypes) / sizeof(*kTypes);
27
28// Rates we must support.
29const int kMaxRate = 96000;
30const int kRates[] = {
31 8000,
32 16000,
33 32000,
34 44000,
35 48000,
36 kMaxRate
37};
38const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000039const int kMaxChannels = 2;
40const size_t kDataSize = static_cast<size_t> (kMaxChannels * kMaxRate / 100);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000041
42// TODO(andrew): should we be supporting these combinations?
43bool ValidRates(int in_rate, int out_rate) {
44 // Not the most compact notation, for clarity.
45 if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) ||
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000046 (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000047 return false;
48 }
49
50 return true;
51}
52
53class ResamplerTest : public testing::Test {
54 protected:
55 ResamplerTest();
56 virtual void SetUp();
57 virtual void TearDown();
58
59 Resampler rs_;
60 int16_t data_in_[kDataSize];
61 int16_t data_out_[kDataSize];
62};
63
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000064ResamplerTest::ResamplerTest() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000065
bjornv@webrtc.org0edb25d2011-12-13 09:06:54 +000066void ResamplerTest::SetUp() {
67 // Initialize input data with anything. The tests are content independent.
68 memset(data_in_, 1, sizeof(data_in_));
69}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000070
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000071void ResamplerTest::TearDown() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000072
73TEST_F(ResamplerTest, Reset) {
74 // The only failure mode for the constructor is if Reset() fails. For the
75 // time being then (until an Init function is added), we rely on Reset()
76 // to test the constructor.
77
78 // Check that all required combinations are supported.
79 for (size_t i = 0; i < kRatesSize; ++i) {
80 for (size_t j = 0; j < kRatesSize; ++j) {
81 for (size_t k = 0; k < kTypesSize; ++k) {
82 std::ostringstream ss;
83 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
84 << ", type: " << kTypes[k];
85 SCOPED_TRACE(ss.str());
86 if (ValidRates(kRates[i], kRates[j]))
87 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kTypes[k]));
88 else
89 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kTypes[k]));
90 }
91 }
92 }
93}
94
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000095// TODO(tlegrand): Replace code inside the two tests below with a function
96// with number of channels and ResamplerType as input.
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000097TEST_F(ResamplerTest, Synchronous) {
98 for (size_t i = 0; i < kRatesSize; ++i) {
99 for (size_t j = 0; j < kRatesSize; ++j) {
100 std::ostringstream ss;
101 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
102 SCOPED_TRACE(ss.str());
103
104 if (ValidRates(kRates[i], kRates[j])) {
105 int in_length = kRates[i] / 100;
106 int out_length = 0;
107 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kResamplerSynchronous));
108 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
109 out_length));
110 EXPECT_EQ(kRates[j] / 100, out_length);
111 } else {
112 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kResamplerSynchronous));
113 }
114 }
115 }
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000116}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000117
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000118TEST_F(ResamplerTest, SynchronousStereo) {
119 // Number of channels is 2, stereo mode.
120 const int kChannels = 2;
121 for (size_t i = 0; i < kRatesSize; ++i) {
122 for (size_t j = 0; j < kRatesSize; ++j) {
123 std::ostringstream ss;
124 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
125 SCOPED_TRACE(ss.str());
126
127 if (ValidRates(kRates[i], kRates[j])) {
128 int in_length = kChannels * kRates[i] / 100;
129 int out_length = 0;
130 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j],
131 kResamplerSynchronousStereo));
132 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
133 out_length));
134 EXPECT_EQ(kChannels * kRates[j] / 100, out_length);
135 } else {
136 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j],
137 kResamplerSynchronousStereo));
138 }
139 }
140 }
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000141}
142} // namespace
143} // namespace webrtc