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 
11 #include "gtest/gtest.h"
12 
13 #include "common_audio/resampler/include/resampler.h"
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);
39 const int kMaxChannels = 2;
40 const size_t kDataSize = static_cast<size_t> (kMaxChannels * kMaxRate / 100);
41 
42 // TODO(andrew): should we be supporting these combinations?
ValidRates(int in_rate,int out_rate)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)) ||
46       (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
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 
ResamplerTest()64 ResamplerTest::ResamplerTest() {}
65 
SetUp()66 void ResamplerTest::SetUp() {
67   // Initialize input data with anything. The tests are content independent.
68   memset(data_in_, 1, sizeof(data_in_));
69 }
70 
TearDown()71 void ResamplerTest::TearDown() {}
72 
TEST_F(ResamplerTest,Reset)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 
95 // TODO(tlegrand): Replace code inside the two tests below with a function
96 // with number of channels and ResamplerType as input.
TEST_F(ResamplerTest,Synchronous)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   }
116 }
117 
TEST_F(ResamplerTest,SynchronousStereo)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   }
141 }
142 }  // namespace
143 }  // namespace webrtc
144