blob: 4f50363288dc665da597349fab76aeb76580a6ec [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:231/*
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.orgf24ac592013-05-27 09:49:5811#include "webrtc/common_audio/resampler/include/resampler.h"
kwiberg36a24792016-10-01 05:29:4312#include "webrtc/test/gtest.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:2313
14// TODO(andrew): this is a work-in-progress. Many more tests are needed.
15
16namespace webrtc {
17namespace {
Andrew MacDonaldddde4b22015-03-30 17:08:2218
19const int kNumChannels[] = {1, 2};
20const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels);
andrew@webrtc.orgb015cbe2012-10-22 18:19:2321
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);
33const int kMaxChannels = 2;
34const size_t kDataSize = static_cast<size_t> (kMaxChannels * kMaxRate / 100);
35
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)) ||
40 (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
41 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
58ResamplerTest::ResamplerTest() {}
59
60void ResamplerTest::SetUp() {
61 // Initialize input data with anything. The tests are content independent.
62 memset(data_in_, 1, sizeof(data_in_));
63}
64
65void ResamplerTest::TearDown() {}
66
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 MacDonaldddde4b22015-03-30 17:08:2275 for (size_t k = 0; k < kNumChannelsSize; ++k) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:2376 std::ostringstream ss;
77 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
Andrew MacDonaldddde4b22015-03-30 17:08:2278 << ", channels: " << kNumChannels[k];
andrew@webrtc.orgb015cbe2012-10-22 18:19:2379 SCOPED_TRACE(ss.str());
80 if (ValidRates(kRates[i], kRates[j]))
Andrew MacDonaldddde4b22015-03-30 17:08:2281 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.orgb015cbe2012-10-22 18:19:2382 else
Andrew MacDonaldddde4b22015-03-30 17:08:2283 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.orgb015cbe2012-10-22 18:19:2384 }
85 }
86 }
87}
88
89// TODO(tlegrand): Replace code inside the two tests below with a function
90// with number of channels and ResamplerType as input.
Andrew MacDonaldddde4b22015-03-30 17:08:2291TEST_F(ResamplerTest, Mono) {
92 const int kChannels = 1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2393 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 Kastinga0ad2482015-08-24 21:52:23100 size_t in_length = static_cast<size_t>(kRates[i] / 100);
101 size_t out_length = 0;
Andrew MacDonaldddde4b22015-03-30 17:08:22102 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23103 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
104 out_length));
Peter Kastinga0ad2482015-08-24 21:52:23105 EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23106 } else {
Andrew MacDonaldddde4b22015-03-30 17:08:22107 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23108 }
109 }
110 }
111}
112
Andrew MacDonaldddde4b22015-03-30 17:08:22113TEST_F(ResamplerTest, Stereo) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23114 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 Kastinga0ad2482015-08-24 21:52:23122 size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100);
123 size_t out_length = 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23124 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j],
Andrew MacDonaldddde4b22015-03-30 17:08:22125 kChannels));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23126 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
127 out_length));
Peter Kastinga0ad2482015-08-24 21:52:23128 EXPECT_EQ(static_cast<size_t>(kChannels * kRates[j] / 100), out_length);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23129 } else {
130 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j],
Andrew MacDonaldddde4b22015-03-30 17:08:22131 kChannels));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23132 }
133 }
134 }
135}
Andrew MacDonaldddde4b22015-03-30 17:08:22136
andrew@webrtc.orgb015cbe2012-10-22 18:19:23137} // namespace
138} // namespace webrtc