blob: 4f50363288dc665da597349fab76aeb76580a6ec [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 "webrtc/common_audio/resampler/include/resampler.h"
kwibergac9f8762016-09-30 22:29:43 -070012#include "webrtc/test/gtest.h"
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000013
14// TODO(andrew): this is a work-in-progress. Many more tests are needed.
15
16namespace webrtc {
17namespace {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070018
19const int kNumChannels[] = {1, 2};
20const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000021
22// Rates we must support.
23const int kMaxRate = 96000;
24const int kRates[] = {
25 8000,
26 16000,
27 32000,
28 44000,
29 48000,
30 kMaxRate
31};
32const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000033const int kMaxChannels = 2;
34const size_t kDataSize = static_cast<size_t> (kMaxChannels * kMaxRate / 100);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000035
36// TODO(andrew): should we be supporting these combinations?
37bool ValidRates(int in_rate, int out_rate) {
38 // Not the most compact notation, for clarity.
39 if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) ||
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000040 (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000041 return false;
42 }
43
44 return true;
45}
46
47class ResamplerTest : public testing::Test {
48 protected:
49 ResamplerTest();
50 virtual void SetUp();
51 virtual void TearDown();
52
53 Resampler rs_;
54 int16_t data_in_[kDataSize];
55 int16_t data_out_[kDataSize];
56};
57
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000058ResamplerTest::ResamplerTest() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000059
bjornv@webrtc.org0edb25d2011-12-13 09:06:54 +000060void ResamplerTest::SetUp() {
61 // Initialize input data with anything. The tests are content independent.
62 memset(data_in_, 1, sizeof(data_in_));
63}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000064
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000065void ResamplerTest::TearDown() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000066
67TEST_F(ResamplerTest, Reset) {
68 // The only failure mode for the constructor is if Reset() fails. For the
69 // time being then (until an Init function is added), we rely on Reset()
70 // to test the constructor.
71
72 // Check that all required combinations are supported.
73 for (size_t i = 0; i < kRatesSize; ++i) {
74 for (size_t j = 0; j < kRatesSize; ++j) {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070075 for (size_t k = 0; k < kNumChannelsSize; ++k) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000076 std::ostringstream ss;
77 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070078 << ", channels: " << kNumChannels[k];
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000079 SCOPED_TRACE(ss.str());
80 if (ValidRates(kRates[i], kRates[j]))
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070081 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000082 else
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070083 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000084 }
85 }
86 }
87}
88
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000089// TODO(tlegrand): Replace code inside the two tests below with a function
90// with number of channels and ResamplerType as input.
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070091TEST_F(ResamplerTest, Mono) {
92 const int kChannels = 1;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000093 for (size_t i = 0; i < kRatesSize; ++i) {
94 for (size_t j = 0; j < kRatesSize; ++j) {
95 std::ostringstream ss;
96 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
97 SCOPED_TRACE(ss.str());
98
99 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700100 size_t in_length = static_cast<size_t>(kRates[i] / 100);
101 size_t out_length = 0;
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700102 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000103 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
104 out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700105 EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000106 } else {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700107 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000108 }
109 }
110 }
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000111}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000112
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700113TEST_F(ResamplerTest, Stereo) {
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000114 const int kChannels = 2;
115 for (size_t i = 0; i < kRatesSize; ++i) {
116 for (size_t j = 0; j < kRatesSize; ++j) {
117 std::ostringstream ss;
118 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
119 SCOPED_TRACE(ss.str());
120
121 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700122 size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100);
123 size_t out_length = 0;
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000124 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j],
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700125 kChannels));
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000126 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
127 out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700128 EXPECT_EQ(static_cast<size_t>(kChannels * kRates[j] / 100), out_length);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000129 } else {
130 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j],
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700131 kChannels));
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000132 }
133 }
134 }
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000135}
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700136
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000137} // namespace
138} // namespace webrtc