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" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 14 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "test/gtest.h" |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 16 | |
| 17 | // TODO(andrew): this is a work-in-progress. Many more tests are needed. |
| 18 | |
| 19 | namespace webrtc { |
| 20 | namespace { |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 21 | |
| 22 | const int kNumChannels[] = {1, 2}; |
| 23 | const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 24 | |
| 25 | // Rates we must support. |
| 26 | const int kMaxRate = 96000; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | const int kRates[] = {8000, 16000, 32000, 44000, 48000, kMaxRate}; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 28 | const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates); |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 29 | const int kMaxChannels = 2; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | const size_t kDataSize = static_cast<size_t>(kMaxChannels * kMaxRate / 100); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 31 | |
| 32 | // TODO(andrew): should we be supporting these combinations? |
| 33 | bool 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.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 36 | (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) { |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | class ResamplerTest : public testing::Test { |
| 44 | protected: |
| 45 | ResamplerTest(); |
Mirko Bonadei | 91df091 | 2018-07-17 11:08:15 +0200 | [diff] [blame] | 46 | void SetUp() override; |
| 47 | void TearDown() override; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 48 | |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame] | 49 | void ResetIfNeededAndPush(int in_rate, int out_rate, int num_channels); |
| 50 | |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 51 | Resampler rs_; |
| 52 | int16_t data_in_[kDataSize]; |
| 53 | int16_t data_out_[kDataSize]; |
| 54 | }; |
| 55 | |
bjornv@webrtc.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 56 | ResamplerTest::ResamplerTest() {} |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 57 | |
bjornv@webrtc.org | 0edb25d | 2011-12-13 09:06:54 +0000 | [diff] [blame] | 58 | void ResamplerTest::SetUp() { |
| 59 | // Initialize input data with anything. The tests are content independent. |
| 60 | memset(data_in_, 1, sizeof(data_in_)); |
| 61 | } |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 62 | |
bjornv@webrtc.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 63 | void ResamplerTest::TearDown() {} |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 64 | |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame] | 65 | void ResamplerTest::ResetIfNeededAndPush(int in_rate, |
| 66 | int out_rate, |
| 67 | int num_channels) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 68 | rtc::StringBuilder ss; |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame] | 69 | 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.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 85 | TEST_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 MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 93 | for (size_t k = 0; k < kNumChannelsSize; ++k) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 94 | rtc::StringBuilder ss; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 95 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j] |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 96 | << ", channels: " << kNumChannels[k]; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 97 | SCOPED_TRACE(ss.str()); |
| 98 | if (ValidRates(kRates[i], kRates[j])) |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 99 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k])); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 100 | else |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 101 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k])); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 107 | // TODO(tlegrand): Replace code inside the two tests below with a function |
| 108 | // with number of channels and ResamplerType as input. |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 109 | TEST_F(ResamplerTest, Mono) { |
| 110 | const int kChannels = 1; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 111 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 112 | for (size_t j = 0; j < kRatesSize; ++j) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 113 | rtc::StringBuilder ss; |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 114 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]; |
| 115 | SCOPED_TRACE(ss.str()); |
| 116 | |
| 117 | if (ValidRates(kRates[i], kRates[j])) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 118 | size_t in_length = static_cast<size_t>(kRates[i] / 100); |
| 119 | size_t out_length = 0; |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 120 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 121 | EXPECT_EQ( |
| 122 | 0, rs_.Push(data_in_, in_length, data_out_, kDataSize, out_length)); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 123 | EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 124 | } else { |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 125 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels)); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 129 | } |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 130 | |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 131 | TEST_F(ResamplerTest, Stereo) { |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 132 | const int kChannels = 2; |
| 133 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 134 | for (size_t j = 0; j < kRatesSize; ++j) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 135 | rtc::StringBuilder ss; |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 136 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]; |
| 137 | SCOPED_TRACE(ss.str()); |
| 138 | |
| 139 | if (ValidRates(kRates[i], kRates[j])) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 140 | size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100); |
| 141 | size_t out_length = 0; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 142 | 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 Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 145 | 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] | 146 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 147 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels)); |
tina.legrand@webrtc.org | 5c43b1b | 2011-12-22 08:28:05 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 151 | } |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 152 | |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame] | 153 | // Try multiple resets between a few supported and unsupported rates. |
| 154 | TEST_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.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 166 | } // namespace |
| 167 | } // namespace webrtc |