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