blob: 1b90d3e30b2c96b41bb27a04214ed666ece31288 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "common_audio/resampler/include/resampler.h"
12
Sam Zackrisson9da7c742017-10-30 10:05:10 +010013#include <array>
14
Jonas Olsson366a50c2018-09-06 13:41:30 +020015#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "test/gtest.h"
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000017
18// TODO(andrew): this is a work-in-progress. Many more tests are needed.
19
20namespace webrtc {
21namespace {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070022
23const int kNumChannels[] = {1, 2};
24const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000025
26// Rates we must support.
27const int kMaxRate = 96000;
Yves Gerey665174f2018-06-19 15:03:05 +020028const int kRates[] = {8000, 16000, 32000, 44000, 48000, kMaxRate};
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000029const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000030const int kMaxChannels = 2;
Yves Gerey665174f2018-06-19 15:03:05 +020031const size_t kDataSize = static_cast<size_t>(kMaxChannels * kMaxRate / 100);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000032
33// TODO(andrew): should we be supporting these combinations?
34bool ValidRates(int in_rate, int out_rate) {
35 // Not the most compact notation, for clarity.
36 if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) ||
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000037 (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000038 return false;
39 }
40
41 return true;
42}
43
Mirko Bonadei6a489f22019-04-09 15:11:12 +020044class ResamplerTest : public ::testing::Test {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000045 protected:
46 ResamplerTest();
Mirko Bonadei91df0912018-07-17 11:08:15 +020047 void SetUp() override;
48 void TearDown() override;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000049
Sam Zackrisson9da7c742017-10-30 10:05:10 +010050 void ResetIfNeededAndPush(int in_rate, int out_rate, int num_channels);
51
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000052 Resampler rs_;
53 int16_t data_in_[kDataSize];
54 int16_t data_out_[kDataSize];
55};
56
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000057ResamplerTest::ResamplerTest() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000058
bjornv@webrtc.org0edb25d2011-12-13 09:06:54 +000059void ResamplerTest::SetUp() {
60 // Initialize input data with anything. The tests are content independent.
61 memset(data_in_, 1, sizeof(data_in_));
62}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000063
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000064void ResamplerTest::TearDown() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000065
Sam Zackrisson9da7c742017-10-30 10:05:10 +010066void ResamplerTest::ResetIfNeededAndPush(int in_rate,
67 int out_rate,
68 int num_channels) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020069 rtc::StringBuilder ss;
Sam Zackrisson9da7c742017-10-30 10:05:10 +010070 ss << "Input rate: " << in_rate << ", output rate: " << out_rate
71 << ", channel count: " << num_channels;
72 SCOPED_TRACE(ss.str());
73
74 if (ValidRates(in_rate, out_rate)) {
75 size_t in_length = static_cast<size_t>(in_rate / 100);
76 size_t out_length = 0;
77 EXPECT_EQ(0, rs_.ResetIfNeeded(in_rate, out_rate, num_channels));
78 EXPECT_EQ(0,
79 rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length));
80 EXPECT_EQ(static_cast<size_t>(out_rate / 100), out_length);
81 } else {
82 EXPECT_EQ(-1, rs_.ResetIfNeeded(in_rate, out_rate, num_channels));
83 }
84}
85
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000086TEST_F(ResamplerTest, Reset) {
87 // The only failure mode for the constructor is if Reset() fails. For the
88 // time being then (until an Init function is added), we rely on Reset()
89 // to test the constructor.
90
91 // Check that all required combinations are supported.
92 for (size_t i = 0; i < kRatesSize; ++i) {
93 for (size_t j = 0; j < kRatesSize; ++j) {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070094 for (size_t k = 0; k < kNumChannelsSize; ++k) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020095 rtc::StringBuilder ss;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000096 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
Yves Gerey665174f2018-06-19 15:03:05 +020097 << ", channels: " << kNumChannels[k];
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000098 SCOPED_TRACE(ss.str());
99 if (ValidRates(kRates[i], kRates[j]))
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700100 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000101 else
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700102 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000103 }
104 }
105 }
106}
107
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000108// TODO(tlegrand): Replace code inside the two tests below with a function
109// with number of channels and ResamplerType as input.
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700110TEST_F(ResamplerTest, Mono) {
111 const int kChannels = 1;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000112 for (size_t i = 0; i < kRatesSize; ++i) {
113 for (size_t j = 0; j < kRatesSize; ++j) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200114 rtc::StringBuilder ss;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000115 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
116 SCOPED_TRACE(ss.str());
117
118 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 size_t in_length = static_cast<size_t>(kRates[i] / 100);
120 size_t out_length = 0;
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700121 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels));
Yves Gerey665174f2018-06-19 15:03:05 +0200122 EXPECT_EQ(
123 0, rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700124 EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000125 } else {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700126 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000127 }
128 }
129 }
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000130}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000131
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700132TEST_F(ResamplerTest, Stereo) {
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000133 const int kChannels = 2;
134 for (size_t i = 0; i < kRatesSize; ++i) {
135 for (size_t j = 0; j < kRatesSize; ++j) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200136 rtc::StringBuilder ss;
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000137 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
138 SCOPED_TRACE(ss.str());
139
140 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700141 size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100);
142 size_t out_length = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200143 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels));
144 EXPECT_EQ(
145 0, rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 EXPECT_EQ(static_cast<size_t>(kChannels * kRates[j] / 100), out_length);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000147 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200148 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels));
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000149 }
150 }
151 }
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000152}
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700153
Sam Zackrisson9da7c742017-10-30 10:05:10 +0100154// Try multiple resets between a few supported and unsupported rates.
155TEST_F(ResamplerTest, MultipleResets) {
156 constexpr size_t kNumChanges = 5;
157 constexpr std::array<int, kNumChanges> kInRates = {
158 {8000, 44000, 44000, 32000, 32000}};
159 constexpr std::array<int, kNumChanges> kOutRates = {
160 {16000, 48000, 48000, 16000, 16000}};
161 constexpr std::array<int, kNumChanges> kNumChannels = {{2, 2, 2, 2, 1}};
162 for (size_t i = 0; i < kNumChanges; ++i) {
163 ResetIfNeededAndPush(kInRates[i], kOutRates[i], kNumChannels[i]);
164 }
165}
166
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000167} // namespace
168} // namespace webrtc