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