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