blob: c5953d030b1228a9c288f83468ced56ecc04b0b8 [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 {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070019
20const int kNumChannels[] = {1, 2};
21const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000022
23// Rates we must support.
24const int kMaxRate = 96000;
25const int kRates[] = {
26 8000,
27 16000,
28 32000,
29 44000,
30 48000,
31 kMaxRate
32};
33const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000034const int kMaxChannels = 2;
35const size_t kDataSize = static_cast<size_t> (kMaxChannels * kMaxRate / 100);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000036
37// TODO(andrew): should we be supporting these combinations?
38bool ValidRates(int in_rate, int out_rate) {
39 // Not the most compact notation, for clarity.
40 if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) ||
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000041 (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000042 return false;
43 }
44
45 return true;
46}
47
48class ResamplerTest : public testing::Test {
49 protected:
50 ResamplerTest();
51 virtual void SetUp();
52 virtual void TearDown();
53
54 Resampler rs_;
55 int16_t data_in_[kDataSize];
56 int16_t data_out_[kDataSize];
57};
58
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000059ResamplerTest::ResamplerTest() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000060
bjornv@webrtc.org0edb25d2011-12-13 09:06:54 +000061void ResamplerTest::SetUp() {
62 // Initialize input data with anything. The tests are content independent.
63 memset(data_in_, 1, sizeof(data_in_));
64}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000065
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000066void ResamplerTest::TearDown() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000067
68TEST_F(ResamplerTest, Reset) {
69 // The only failure mode for the constructor is if Reset() fails. For the
70 // time being then (until an Init function is added), we rely on Reset()
71 // to test the constructor.
72
73 // Check that all required combinations are supported.
74 for (size_t i = 0; i < kRatesSize; ++i) {
75 for (size_t j = 0; j < kRatesSize; ++j) {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070076 for (size_t k = 0; k < kNumChannelsSize; ++k) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000077 std::ostringstream ss;
78 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070079 << ", channels: " << kNumChannels[k];
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000080 SCOPED_TRACE(ss.str());
81 if (ValidRates(kRates[i], kRates[j]))
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070082 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000083 else
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070084 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000085 }
86 }
87 }
88}
89
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000090// TODO(tlegrand): Replace code inside the two tests below with a function
91// with number of channels and ResamplerType as input.
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070092TEST_F(ResamplerTest, Mono) {
93 const int kChannels = 1;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000094 for (size_t i = 0; i < kRatesSize; ++i) {
95 for (size_t j = 0; j < kRatesSize; ++j) {
96 std::ostringstream ss;
97 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
98 SCOPED_TRACE(ss.str());
99
100 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700101 size_t in_length = static_cast<size_t>(kRates[i] / 100);
102 size_t out_length = 0;
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700103 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000104 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
105 out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700106 EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000107 } else {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700108 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000109 }
110 }
111 }
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000112}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000113
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700114TEST_F(ResamplerTest, Stereo) {
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000115 const int kChannels = 2;
116 for (size_t i = 0; i < kRatesSize; ++i) {
117 for (size_t j = 0; j < kRatesSize; ++j) {
118 std::ostringstream ss;
119 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
120 SCOPED_TRACE(ss.str());
121
122 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700123 size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100);
124 size_t out_length = 0;
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000125 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j],
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700126 kChannels));
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000127 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
128 out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700129 EXPECT_EQ(static_cast<size_t>(kChannels * kRates[j] / 100), out_length);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000130 } else {
131 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j],
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700132 kChannels));
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000133 }
134 }
135 }
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000136}
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700137
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000138} // namespace
139} // namespace webrtc