blob: 61be04046832aa7e07325d5794a685ee79a6261d [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
Sam Zackrisson9da7c742017-10-30 10:05:10 +010011#include <array>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "common_audio/resampler/include/resampler.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020014#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "test/gtest.h"
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000016
17// TODO(andrew): this is a work-in-progress. Many more tests are needed.
18
19namespace webrtc {
20namespace {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070021
22const int kNumChannels[] = {1, 2};
23const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000024
25// Rates we must support.
26const int kMaxRate = 96000;
Yves Gerey665174f2018-06-19 15:03:05 +020027const int kRates[] = {8000, 16000, 32000, 44000, 48000, kMaxRate};
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000028const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +000029const int kMaxChannels = 2;
Yves Gerey665174f2018-06-19 15:03:05 +020030const size_t kDataSize = static_cast<size_t>(kMaxChannels * kMaxRate / 100);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000031
32// TODO(andrew): should we be supporting these combinations?
33bool ValidRates(int in_rate, int out_rate) {
34 // Not the most compact notation, for clarity.
35 if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) ||
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000036 (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000037 return false;
38 }
39
40 return true;
41}
42
43class ResamplerTest : public testing::Test {
44 protected:
45 ResamplerTest();
Mirko Bonadei91df0912018-07-17 11:08:15 +020046 void SetUp() override;
47 void TearDown() override;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000048
Sam Zackrisson9da7c742017-10-30 10:05:10 +010049 void ResetIfNeededAndPush(int in_rate, int out_rate, int num_channels);
50
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000051 Resampler rs_;
52 int16_t data_in_[kDataSize];
53 int16_t data_out_[kDataSize];
54};
55
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000056ResamplerTest::ResamplerTest() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000057
bjornv@webrtc.org0edb25d2011-12-13 09:06:54 +000058void ResamplerTest::SetUp() {
59 // Initialize input data with anything. The tests are content independent.
60 memset(data_in_, 1, sizeof(data_in_));
61}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000062
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000063void ResamplerTest::TearDown() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000064
Sam Zackrisson9da7c742017-10-30 10:05:10 +010065void ResamplerTest::ResetIfNeededAndPush(int in_rate,
66 int out_rate,
67 int num_channels) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020068 rtc::StringBuilder ss;
Sam Zackrisson9da7c742017-10-30 10:05:10 +010069 ss << "Input rate: " << in_rate << ", output rate: " << out_rate
70 << ", channel count: " << num_channels;
71 SCOPED_TRACE(ss.str());
72
73 if (ValidRates(in_rate, out_rate)) {
74 size_t in_length = static_cast<size_t>(in_rate / 100);
75 size_t out_length = 0;
76 EXPECT_EQ(0, rs_.ResetIfNeeded(in_rate, out_rate, num_channels));
77 EXPECT_EQ(0,
78 rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length));
79 EXPECT_EQ(static_cast<size_t>(out_rate / 100), out_length);
80 } else {
81 EXPECT_EQ(-1, rs_.ResetIfNeeded(in_rate, out_rate, num_channels));
82 }
83}
84
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000085TEST_F(ResamplerTest, Reset) {
86 // The only failure mode for the constructor is if Reset() fails. For the
87 // time being then (until an Init function is added), we rely on Reset()
88 // to test the constructor.
89
90 // Check that all required combinations are supported.
91 for (size_t i = 0; i < kRatesSize; ++i) {
92 for (size_t j = 0; j < kRatesSize; ++j) {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070093 for (size_t k = 0; k < kNumChannelsSize; ++k) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020094 rtc::StringBuilder ss;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000095 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
Yves Gerey665174f2018-06-19 15:03:05 +020096 << ", channels: " << kNumChannels[k];
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000097 SCOPED_TRACE(ss.str());
98 if (ValidRates(kRates[i], kRates[j]))
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070099 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000100 else
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700101 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k]));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000102 }
103 }
104 }
105}
106
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000107// TODO(tlegrand): Replace code inside the two tests below with a function
108// with number of channels and ResamplerType as input.
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700109TEST_F(ResamplerTest, Mono) {
110 const int kChannels = 1;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000111 for (size_t i = 0; i < kRatesSize; ++i) {
112 for (size_t j = 0; j < kRatesSize; ++j) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200113 rtc::StringBuilder ss;
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000114 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
115 SCOPED_TRACE(ss.str());
116
117 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700118 size_t in_length = static_cast<size_t>(kRates[i] / 100);
119 size_t out_length = 0;
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700120 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels));
Yves Gerey665174f2018-06-19 15:03:05 +0200121 EXPECT_EQ(
122 0, rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700123 EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length);
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000124 } else {
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700125 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels));
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000126 }
127 }
128 }
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000129}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000130
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700131TEST_F(ResamplerTest, Stereo) {
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000132 const int kChannels = 2;
133 for (size_t i = 0; i < kRatesSize; ++i) {
134 for (size_t j = 0; j < kRatesSize; ++j) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200135 rtc::StringBuilder ss;
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000136 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
137 SCOPED_TRACE(ss.str());
138
139 if (ValidRates(kRates[i], kRates[j])) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700140 size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100);
141 size_t out_length = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200142 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels));
143 EXPECT_EQ(
144 0, rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 EXPECT_EQ(static_cast<size_t>(kChannels * kRates[j] / 100), out_length);
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000146 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200147 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels));
tina.legrand@webrtc.org5c43b1b2011-12-22 08:28:05 +0000148 }
149 }
150 }
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000151}
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -0700152
Sam Zackrisson9da7c742017-10-30 10:05:10 +0100153// Try multiple resets between a few supported and unsupported rates.
154TEST_F(ResamplerTest, MultipleResets) {
155 constexpr size_t kNumChanges = 5;
156 constexpr std::array<int, kNumChanges> kInRates = {
157 {8000, 44000, 44000, 32000, 32000}};
158 constexpr std::array<int, kNumChanges> kOutRates = {
159 {16000, 48000, 48000, 16000, 16000}};
160 constexpr std::array<int, kNumChanges> kNumChannels = {{2, 2, 2, 2, 1}};
161 for (size_t i = 0; i < kNumChanges; ++i) {
162 ResetIfNeededAndPush(kInRates[i], kOutRates[i], kNumChannels[i]);
163 }
164}
165
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +0000166} // namespace
167} // namespace webrtc