andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 1 | /* |
| 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 Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame^] | 11 | #include <array> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "common_audio/resampler/include/resampler.h" |
| 14 | #include "test/gtest.h" |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 15 | |
| 16 | // TODO(andrew): this is a work-in-progress. Many more tests are needed. |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace { |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 20 | |
| 21 | const int kNumChannels[] = {1, 2}; |
| 22 | const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 23 | |
| 24 | // Rates we must support. |
| 25 | const int kMaxRate = 96000; |
| 26 | const int kRates[] = { |
| 27 | 8000, |
| 28 | 16000, |
| 29 | 32000, |
| 30 | 44000, |
| 31 | 48000, |
| 32 | kMaxRate |
| 33 | }; |
| 34 | const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates); |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 35 | const int kMaxChannels = 2; |
| 36 | const size_t kDataSize = static_cast<size_t> (kMaxChannels * kMaxRate / 100); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 37 | |
| 38 | // TODO(andrew): should we be supporting these combinations? |
| 39 | bool ValidRates(int in_rate, int out_rate) { |
| 40 | // Not the most compact notation, for clarity. |
| 41 | if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) || |
bjornv@webrtc.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 42 | (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) { |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | class ResamplerTest : public testing::Test { |
| 50 | protected: |
| 51 | ResamplerTest(); |
| 52 | virtual void SetUp(); |
| 53 | virtual void TearDown(); |
| 54 | |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame^] | 55 | void ResetIfNeededAndPush(int in_rate, int out_rate, int num_channels); |
| 56 | |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 57 | Resampler rs_; |
| 58 | int16_t data_in_[kDataSize]; |
| 59 | int16_t data_out_[kDataSize]; |
| 60 | }; |
| 61 | |
bjornv@webrtc.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 62 | ResamplerTest::ResamplerTest() {} |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 63 | |
bjornv@webrtc.org | 0edb25d | 2011-12-13 09:06:54 +0000 | [diff] [blame] | 64 | void ResamplerTest::SetUp() { |
| 65 | // Initialize input data with anything. The tests are content independent. |
| 66 | memset(data_in_, 1, sizeof(data_in_)); |
| 67 | } |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 68 | |
bjornv@webrtc.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 69 | void ResamplerTest::TearDown() {} |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 70 | |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame^] | 71 | void ResamplerTest::ResetIfNeededAndPush(int in_rate, |
| 72 | int out_rate, |
| 73 | int num_channels) { |
| 74 | std::ostringstream ss; |
| 75 | ss << "Input rate: " << in_rate << ", output rate: " << out_rate |
| 76 | << ", channel count: " << num_channels; |
| 77 | SCOPED_TRACE(ss.str()); |
| 78 | |
| 79 | if (ValidRates(in_rate, out_rate)) { |
| 80 | size_t in_length = static_cast<size_t>(in_rate / 100); |
| 81 | size_t out_length = 0; |
| 82 | EXPECT_EQ(0, rs_.ResetIfNeeded(in_rate, out_rate, num_channels)); |
| 83 | EXPECT_EQ(0, |
| 84 | rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length)); |
| 85 | EXPECT_EQ(static_cast<size_t>(out_rate / 100), out_length); |
| 86 | } else { |
| 87 | EXPECT_EQ(-1, rs_.ResetIfNeeded(in_rate, out_rate, num_channels)); |
| 88 | } |
| 89 | } |
| 90 | |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 91 | TEST_F(ResamplerTest, Reset) { |
| 92 | // The only failure mode for the constructor is if Reset() fails. For the |
| 93 | // time being then (until an Init function is added), we rely on Reset() |
| 94 | // to test the constructor. |
| 95 | |
| 96 | // Check that all required combinations are supported. |
| 97 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 98 | for (size_t j = 0; j < kRatesSize; ++j) { |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 99 | for (size_t k = 0; k < kNumChannelsSize; ++k) { |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 100 | std::ostringstream ss; |
| 101 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j] |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 102 | << ", channels: " << kNumChannels[k]; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 103 | SCOPED_TRACE(ss.str()); |
| 104 | if (ValidRates(kRates[i], kRates[j])) |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 105 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k])); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 106 | else |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 107 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k])); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 113 | // TODO(tlegrand): Replace code inside the two tests below with a function |
| 114 | // with number of channels and ResamplerType as input. |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 115 | TEST_F(ResamplerTest, Mono) { |
| 116 | const int kChannels = 1; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 117 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 118 | for (size_t j = 0; j < kRatesSize; ++j) { |
| 119 | std::ostringstream ss; |
| 120 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]; |
| 121 | SCOPED_TRACE(ss.str()); |
| 122 | |
| 123 | if (ValidRates(kRates[i], kRates[j])) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 124 | size_t in_length = static_cast<size_t>(kRates[i] / 100); |
| 125 | size_t out_length = 0; |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 126 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels)); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 127 | EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize, |
| 128 | out_length)); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 129 | EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 130 | } else { |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 131 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels)); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 135 | } |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 136 | |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 137 | TEST_F(ResamplerTest, Stereo) { |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 138 | const int kChannels = 2; |
| 139 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 140 | for (size_t j = 0; j < kRatesSize; ++j) { |
| 141 | std::ostringstream ss; |
| 142 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]; |
| 143 | SCOPED_TRACE(ss.str()); |
| 144 | |
| 145 | if (ValidRates(kRates[i], kRates[j])) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 146 | size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100); |
| 147 | size_t out_length = 0; |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 148 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 149 | kChannels)); |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 150 | EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize, |
| 151 | out_length)); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 152 | EXPECT_EQ(static_cast<size_t>(kChannels * kRates[j] / 100), out_length); |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 153 | } else { |
| 154 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 155 | kChannels)); |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 159 | } |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 160 | |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame^] | 161 | // Try multiple resets between a few supported and unsupported rates. |
| 162 | TEST_F(ResamplerTest, MultipleResets) { |
| 163 | constexpr size_t kNumChanges = 5; |
| 164 | constexpr std::array<int, kNumChanges> kInRates = { |
| 165 | {8000, 44000, 44000, 32000, 32000}}; |
| 166 | constexpr std::array<int, kNumChanges> kOutRates = { |
| 167 | {16000, 48000, 48000, 16000, 16000}}; |
| 168 | constexpr std::array<int, kNumChanges> kNumChannels = {{2, 2, 2, 2, 1}}; |
| 169 | for (size_t i = 0; i < kNumChanges; ++i) { |
| 170 | ResetIfNeededAndPush(kInRates[i], kOutRates[i], kNumChannels[i]); |
| 171 | } |
| 172 | } |
| 173 | |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 174 | } // namespace |
| 175 | } // namespace webrtc |