andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [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 | |
pbos@webrtc.org | f24ac59 | 2013-05-27 09:49:58 | [diff] [blame] | 11 | #include "webrtc/common_audio/resampler/include/resampler.h" |
kwiberg | 36a2479 | 2016-10-01 05:29:43 | [diff] [blame] | 12 | #include "webrtc/test/gtest.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 13 | |
| 14 | // TODO(andrew): this is a work-in-progress. Many more tests are needed. |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 18 | |
| 19 | const int kNumChannels[] = {1, 2}; |
| 20 | const size_t kNumChannelsSize = sizeof(kNumChannels) / sizeof(*kNumChannels); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 21 | |
| 22 | // Rates we must support. |
| 23 | const int kMaxRate = 96000; |
| 24 | const int kRates[] = { |
| 25 | 8000, |
| 26 | 16000, |
| 27 | 32000, |
| 28 | 44000, |
| 29 | 48000, |
| 30 | kMaxRate |
| 31 | }; |
| 32 | const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates); |
| 33 | const int kMaxChannels = 2; |
| 34 | const size_t kDataSize = static_cast<size_t> (kMaxChannels * kMaxRate / 100); |
| 35 | |
| 36 | // TODO(andrew): should we be supporting these combinations? |
| 37 | bool ValidRates(int in_rate, int out_rate) { |
| 38 | // Not the most compact notation, for clarity. |
| 39 | if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) || |
| 40 | (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | class ResamplerTest : public testing::Test { |
| 48 | protected: |
| 49 | ResamplerTest(); |
| 50 | virtual void SetUp(); |
| 51 | virtual void TearDown(); |
| 52 | |
| 53 | Resampler rs_; |
| 54 | int16_t data_in_[kDataSize]; |
| 55 | int16_t data_out_[kDataSize]; |
| 56 | }; |
| 57 | |
| 58 | ResamplerTest::ResamplerTest() {} |
| 59 | |
| 60 | void ResamplerTest::SetUp() { |
| 61 | // Initialize input data with anything. The tests are content independent. |
| 62 | memset(data_in_, 1, sizeof(data_in_)); |
| 63 | } |
| 64 | |
| 65 | void ResamplerTest::TearDown() {} |
| 66 | |
| 67 | TEST_F(ResamplerTest, Reset) { |
| 68 | // The only failure mode for the constructor is if Reset() fails. For the |
| 69 | // time being then (until an Init function is added), we rely on Reset() |
| 70 | // to test the constructor. |
| 71 | |
| 72 | // Check that all required combinations are supported. |
| 73 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 74 | for (size_t j = 0; j < kRatesSize; ++j) { |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 75 | for (size_t k = 0; k < kNumChannelsSize; ++k) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 76 | std::ostringstream ss; |
| 77 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j] |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 78 | << ", channels: " << kNumChannels[k]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 79 | SCOPED_TRACE(ss.str()); |
| 80 | if (ValidRates(kRates[i], kRates[j])) |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 81 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kNumChannels[k])); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 82 | else |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 83 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kNumChannels[k])); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // TODO(tlegrand): Replace code inside the two tests below with a function |
| 90 | // with number of channels and ResamplerType as input. |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 91 | TEST_F(ResamplerTest, Mono) { |
| 92 | const int kChannels = 1; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 93 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 94 | for (size_t j = 0; j < kRatesSize; ++j) { |
| 95 | std::ostringstream ss; |
| 96 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]; |
| 97 | SCOPED_TRACE(ss.str()); |
| 98 | |
| 99 | if (ValidRates(kRates[i], kRates[j])) { |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 100 | size_t in_length = static_cast<size_t>(kRates[i] / 100); |
| 101 | size_t out_length = 0; |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 102 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kChannels)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 103 | EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize, |
| 104 | out_length)); |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 105 | EXPECT_EQ(static_cast<size_t>(kRates[j] / 100), out_length); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 106 | } else { |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 107 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kChannels)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 113 | TEST_F(ResamplerTest, Stereo) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 114 | const int kChannels = 2; |
| 115 | for (size_t i = 0; i < kRatesSize; ++i) { |
| 116 | for (size_t j = 0; j < kRatesSize; ++j) { |
| 117 | std::ostringstream ss; |
| 118 | ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]; |
| 119 | SCOPED_TRACE(ss.str()); |
| 120 | |
| 121 | if (ValidRates(kRates[i], kRates[j])) { |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 122 | size_t in_length = static_cast<size_t>(kChannels * kRates[i] / 100); |
| 123 | size_t out_length = 0; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 124 | EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 125 | kChannels)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 126 | EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize, |
| 127 | out_length)); |
Peter Kasting | a0ad248 | 2015-08-24 21:52:23 | [diff] [blame] | 128 | EXPECT_EQ(static_cast<size_t>(kChannels * kRates[j] / 100), out_length); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 129 | } else { |
| 130 | EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 131 | kChannels)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
Andrew MacDonald | ddde4b2 | 2015-03-30 17:08:22 | [diff] [blame] | 136 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 137 | } // namespace |
| 138 | } // namespace webrtc |