OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <math.h> | 11 #include <math.h> |
12 | 12 |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "webrtc/base/format_macros.h" | 14 #include "webrtc/base/format_macros.h" |
15 #include "webrtc/common_audio/resampler/include/push_resampler.h" | 15 #include "webrtc/common_audio/resampler/include/push_resampler.h" |
16 #include "webrtc/modules/interface/module_common_types.h" | 16 #include "webrtc/modules/interface/module_common_types.h" |
17 #include "webrtc/voice_engine/utility.h" | 17 #include "webrtc/voice_engine/utility.h" |
18 #include "webrtc/voice_engine/voice_engine_defines.h" | 18 #include "webrtc/voice_engine/voice_engine_defines.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace webrtc { |
21 namespace voe { | 21 namespace voe { |
22 namespace { | 22 namespace { |
23 | 23 |
24 enum FunctionToTest { | |
25 TestRemixAndResample, | |
26 TestDownConvertToCodecFormat | |
27 }; | |
28 | |
29 class UtilityTest : public ::testing::Test { | 24 class UtilityTest : public ::testing::Test { |
30 protected: | 25 protected: |
31 UtilityTest() { | 26 UtilityTest() { |
32 src_frame_.sample_rate_hz_ = 16000; | 27 src_frame_.sample_rate_hz_ = 16000; |
33 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100; | 28 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100; |
34 src_frame_.num_channels_ = 1; | 29 src_frame_.num_channels_ = 1; |
35 dst_frame_.CopyFrom(src_frame_); | 30 dst_frame_.CopyFrom(src_frame_); |
36 golden_frame_.CopyFrom(src_frame_); | 31 golden_frame_.CopyFrom(src_frame_); |
37 } | 32 } |
38 | 33 |
39 void RunResampleTest(int src_channels, int src_sample_rate_hz, | 34 void RunResampleTest(int src_channels, |
40 int dst_channels, int dst_sample_rate_hz, | 35 int src_sample_rate_hz, |
41 FunctionToTest function); | 36 int dst_channels, |
| 37 int dst_sample_rate_hz); |
42 | 38 |
43 PushResampler<int16_t> resampler_; | 39 PushResampler<int16_t> resampler_; |
44 AudioFrame src_frame_; | 40 AudioFrame src_frame_; |
45 AudioFrame dst_frame_; | 41 AudioFrame dst_frame_; |
46 AudioFrame golden_frame_; | 42 AudioFrame golden_frame_; |
47 }; | 43 }; |
48 | 44 |
49 // Sets the signal value to increase by |data| with every sample. Floats are | 45 // Sets the signal value to increase by |data| with every sample. Floats are |
50 // used so non-integer values result in rounding error, but not an accumulating | 46 // used so non-integer values result in rounding error, but not an accumulating |
51 // error. | 47 // error. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 VerifyParams(ref_frame, test_frame); | 119 VerifyParams(ref_frame, test_frame); |
124 for (size_t i = 0; | 120 for (size_t i = 0; |
125 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { | 121 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { |
126 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); | 122 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); |
127 } | 123 } |
128 } | 124 } |
129 | 125 |
130 void UtilityTest::RunResampleTest(int src_channels, | 126 void UtilityTest::RunResampleTest(int src_channels, |
131 int src_sample_rate_hz, | 127 int src_sample_rate_hz, |
132 int dst_channels, | 128 int dst_channels, |
133 int dst_sample_rate_hz, | 129 int dst_sample_rate_hz) { |
134 FunctionToTest function) { | |
135 PushResampler<int16_t> resampler; // Create a new one with every test. | 130 PushResampler<int16_t> resampler; // Create a new one with every test. |
136 const int16_t kSrcLeft = 30; // Shouldn't overflow for any used sample rate. | 131 const int16_t kSrcLeft = 30; // Shouldn't overflow for any used sample rate. |
137 const int16_t kSrcRight = 15; | 132 const int16_t kSrcRight = 15; |
138 const float resampling_factor = (1.0 * src_sample_rate_hz) / | 133 const float resampling_factor = (1.0 * src_sample_rate_hz) / |
139 dst_sample_rate_hz; | 134 dst_sample_rate_hz; |
140 const float dst_left = resampling_factor * kSrcLeft; | 135 const float dst_left = resampling_factor * kSrcLeft; |
141 const float dst_right = resampling_factor * kSrcRight; | 136 const float dst_right = resampling_factor * kSrcRight; |
142 const float dst_mono = (dst_left + dst_right) / 2; | 137 const float dst_mono = (dst_left + dst_right) / 2; |
143 if (src_channels == 1) | 138 if (src_channels == 1) |
144 SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz); | 139 SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz); |
(...skipping 16 matching lines...) Expand all Loading... |
161 | 156 |
162 // The sinc resampler has a known delay, which we compute here. Multiplying by | 157 // The sinc resampler has a known delay, which we compute here. Multiplying by |
163 // two gives us a crude maximum for any resampling, as the old resampler | 158 // two gives us a crude maximum for any resampling, as the old resampler |
164 // typically (but not always) has lower delay. | 159 // typically (but not always) has lower delay. |
165 static const size_t kInputKernelDelaySamples = 16; | 160 static const size_t kInputKernelDelaySamples = 16; |
166 const size_t max_delay = static_cast<size_t>( | 161 const size_t max_delay = static_cast<size_t>( |
167 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz * | 162 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz * |
168 kInputKernelDelaySamples * dst_channels * 2); | 163 kInputKernelDelaySamples * dst_channels * 2); |
169 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. | 164 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. |
170 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz); | 165 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz); |
171 if (function == TestRemixAndResample) { | 166 RemixAndResample(src_frame_, &resampler, &dst_frame_); |
172 RemixAndResample(src_frame_, &resampler, &dst_frame_); | |
173 } else { | |
174 int16_t mono_buffer[kMaxMonoDataSizeSamples]; | |
175 DownConvertToCodecFormat(src_frame_.data_, | |
176 src_frame_.samples_per_channel_, | |
177 src_frame_.num_channels_, | |
178 src_frame_.sample_rate_hz_, | |
179 dst_frame_.num_channels_, | |
180 dst_frame_.sample_rate_hz_, | |
181 mono_buffer, | |
182 &resampler, | |
183 &dst_frame_); | |
184 } | |
185 | 167 |
186 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) { | 168 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) { |
187 // The sinc resampler gives poor SNR at this extreme conversion, but we | 169 // The sinc resampler gives poor SNR at this extreme conversion, but we |
188 // expect to see this rarely in practice. | 170 // expect to see this rarely in practice. |
189 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f); | 171 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f); |
190 } else { | 172 } else { |
191 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f); | 173 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f); |
192 } | 174 } |
193 } | 175 } |
194 | 176 |
(...skipping 30 matching lines...) Expand all Loading... |
225 TEST_F(UtilityTest, RemixAndResampleSucceeds) { | 207 TEST_F(UtilityTest, RemixAndResampleSucceeds) { |
226 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; | 208 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; |
227 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); | 209 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); |
228 const int kChannels[] = {1, 2}; | 210 const int kChannels[] = {1, 2}; |
229 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); | 211 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); |
230 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { | 212 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { |
231 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { | 213 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { |
232 for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) { | 214 for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) { |
233 for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) { | 215 for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) { |
234 RunResampleTest(kChannels[src_channel], kSampleRates[src_rate], | 216 RunResampleTest(kChannels[src_channel], kSampleRates[src_rate], |
235 kChannels[dst_channel], kSampleRates[dst_rate], | 217 kChannels[dst_channel], kSampleRates[dst_rate]); |
236 TestRemixAndResample); | |
237 } | 218 } |
238 } | 219 } |
239 } | 220 } |
240 } | |
241 } | |
242 | |
243 TEST_F(UtilityTest, ConvertToCodecFormatSucceeds) { | |
244 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; | |
245 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); | |
246 const int kChannels[] = {1, 2}; | |
247 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); | |
248 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { | |
249 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { | |
250 for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) { | |
251 for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) { | |
252 if (dst_rate <= src_rate && dst_channel <= src_channel) { | |
253 RunResampleTest(kChannels[src_channel], kSampleRates[src_rate], | |
254 kChannels[src_channel], kSampleRates[dst_rate], | |
255 TestDownConvertToCodecFormat); | |
256 } | |
257 } | |
258 } | |
259 } | |
260 } | 221 } |
261 } | 222 } |
262 | 223 |
263 } // namespace | 224 } // namespace |
264 } // namespace voe | 225 } // namespace voe |
265 } // namespace webrtc | 226 } // namespace webrtc |
OLD | NEW |