OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 // Modified from the Chromium original: | 11 // Modified from the Chromium original: |
12 // src/media/base/sinc_resampler_unittest.cc | 12 // src/media/base/sinc_resampler_unittest.cc |
13 | 13 |
14 // MSVC++ requires this to be set before any other includes to get M_PI. | 14 // MSVC++ requires this to be set before any other includes to get M_PI. |
15 #define _USE_MATH_DEFINES | 15 #define _USE_MATH_DEFINES |
16 | 16 |
17 #include <math.h> | 17 #include <math.h> |
18 | 18 |
19 #include <memory> | |
20 | |
21 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webrtc/base/scoped_ptr.h" |
23 #include "webrtc/common_audio/resampler/sinc_resampler.h" | 22 #include "webrtc/common_audio/resampler/sinc_resampler.h" |
24 #include "webrtc/common_audio/resampler/sinusoidal_linear_chirp_source.h" | 23 #include "webrtc/common_audio/resampler/sinusoidal_linear_chirp_source.h" |
25 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" | 24 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" |
26 #include "webrtc/system_wrappers/include/stringize_macros.h" | 25 #include "webrtc/system_wrappers/include/stringize_macros.h" |
27 #include "webrtc/system_wrappers/include/tick_util.h" | 26 #include "webrtc/system_wrappers/include/tick_util.h" |
28 #include "webrtc/test/test_suite.h" | 27 #include "webrtc/test/test_suite.h" |
29 | 28 |
30 using testing::_; | 29 using testing::_; |
31 | 30 |
32 namespace webrtc { | 31 namespace webrtc { |
(...skipping 23 matching lines...) Expand all Loading... |
56 TEST(SincResamplerTest, ChunkedResample) { | 55 TEST(SincResamplerTest, ChunkedResample) { |
57 MockSource mock_source; | 56 MockSource mock_source; |
58 | 57 |
59 // Choose a high ratio of input to output samples which will result in quick | 58 // Choose a high ratio of input to output samples which will result in quick |
60 // exhaustion of SincResampler's internal buffers. | 59 // exhaustion of SincResampler's internal buffers. |
61 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, | 60 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
62 &mock_source); | 61 &mock_source); |
63 | 62 |
64 static const int kChunks = 2; | 63 static const int kChunks = 2; |
65 size_t max_chunk_size = resampler.ChunkSize() * kChunks; | 64 size_t max_chunk_size = resampler.ChunkSize() * kChunks; |
66 std::unique_ptr<float[]> resampled_destination(new float[max_chunk_size]); | 65 rtc::scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]); |
67 | 66 |
68 // Verify requesting ChunkSize() frames causes a single callback. | 67 // Verify requesting ChunkSize() frames causes a single callback. |
69 EXPECT_CALL(mock_source, Run(_, _)) | 68 EXPECT_CALL(mock_source, Run(_, _)) |
70 .Times(1).WillOnce(ClearBuffer()); | 69 .Times(1).WillOnce(ClearBuffer()); |
71 resampler.Resample(resampler.ChunkSize(), resampled_destination.get()); | 70 resampler.Resample(resampler.ChunkSize(), resampled_destination.get()); |
72 | 71 |
73 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. | 72 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. |
74 testing::Mock::VerifyAndClear(&mock_source); | 73 testing::Mock::VerifyAndClear(&mock_source); |
75 EXPECT_CALL(mock_source, Run(_, _)) | 74 EXPECT_CALL(mock_source, Run(_, _)) |
76 .Times(kChunks).WillRepeatedly(ClearBuffer()); | 75 .Times(kChunks).WillRepeatedly(ClearBuffer()); |
77 resampler.Resample(max_chunk_size, resampled_destination.get()); | 76 resampler.Resample(max_chunk_size, resampled_destination.get()); |
78 } | 77 } |
79 | 78 |
80 // Test flush resets the internal state properly. | 79 // Test flush resets the internal state properly. |
81 TEST(SincResamplerTest, Flush) { | 80 TEST(SincResamplerTest, Flush) { |
82 MockSource mock_source; | 81 MockSource mock_source; |
83 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, | 82 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
84 &mock_source); | 83 &mock_source); |
85 std::unique_ptr<float[]> resampled_destination( | 84 rtc::scoped_ptr<float[]> resampled_destination( |
86 new float[resampler.ChunkSize()]); | 85 new float[resampler.ChunkSize()]); |
87 | 86 |
88 // Fill the resampler with junk data. | 87 // Fill the resampler with junk data. |
89 EXPECT_CALL(mock_source, Run(_, _)) | 88 EXPECT_CALL(mock_source, Run(_, _)) |
90 .Times(1).WillOnce(FillBuffer()); | 89 .Times(1).WillOnce(FillBuffer()); |
91 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); | 90 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); |
92 ASSERT_NE(resampled_destination[0], 0); | 91 ASSERT_NE(resampled_destination[0], 0); |
93 | 92 |
94 // Flush and request more data, which should all be zeros now. | 93 // Flush and request more data, which should all be zeros now. |
95 resampler.Flush(); | 94 resampler.Flush(); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 // Source for data to be resampled. | 262 // Source for data to be resampled. |
264 SinusoidalLinearChirpSource resampler_source( | 263 SinusoidalLinearChirpSource resampler_source( |
265 input_rate_, input_samples, input_nyquist_freq, 0); | 264 input_rate_, input_samples, input_nyquist_freq, 0); |
266 | 265 |
267 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); | 266 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); |
268 SincResampler resampler(io_ratio, SincResampler::kDefaultRequestSize, | 267 SincResampler resampler(io_ratio, SincResampler::kDefaultRequestSize, |
269 &resampler_source); | 268 &resampler_source); |
270 | 269 |
271 // Force an update to the sample rate ratio to ensure dyanmic sample rate | 270 // Force an update to the sample rate ratio to ensure dyanmic sample rate |
272 // changes are working correctly. | 271 // changes are working correctly. |
273 std::unique_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]); | 272 rtc::scoped_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]); |
274 memcpy(kernel.get(), resampler.get_kernel_for_testing(), | 273 memcpy(kernel.get(), resampler.get_kernel_for_testing(), |
275 SincResampler::kKernelStorageSize); | 274 SincResampler::kKernelStorageSize); |
276 resampler.SetRatio(M_PI); | 275 resampler.SetRatio(M_PI); |
277 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), | 276 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), |
278 SincResampler::kKernelStorageSize)); | 277 SincResampler::kKernelStorageSize)); |
279 resampler.SetRatio(io_ratio); | 278 resampler.SetRatio(io_ratio); |
280 ASSERT_EQ(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), | 279 ASSERT_EQ(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), |
281 SincResampler::kKernelStorageSize)); | 280 SincResampler::kKernelStorageSize)); |
282 | 281 |
283 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to | 282 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to |
284 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes. | 283 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes. |
285 std::unique_ptr<float[]> resampled_destination(new float[output_samples]); | 284 rtc::scoped_ptr<float[]> resampled_destination(new float[output_samples]); |
286 std::unique_ptr<float[]> pure_destination(new float[output_samples]); | 285 rtc::scoped_ptr<float[]> pure_destination(new float[output_samples]); |
287 | 286 |
288 // Generate resampled signal. | 287 // Generate resampled signal. |
289 resampler.Resample(output_samples, resampled_destination.get()); | 288 resampler.Resample(output_samples, resampled_destination.get()); |
290 | 289 |
291 // Generate pure signal. | 290 // Generate pure signal. |
292 SinusoidalLinearChirpSource pure_source( | 291 SinusoidalLinearChirpSource pure_source( |
293 output_rate_, output_samples, input_nyquist_freq, 0); | 292 output_rate_, output_samples, input_nyquist_freq, 0); |
294 pure_source.Run(output_samples, pure_destination.get()); | 293 pure_source.Run(output_samples, pure_destination.get()); |
295 | 294 |
296 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which | 295 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), | 380 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), |
382 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), | 381 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), |
383 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), | 382 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), |
384 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), | 383 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), |
385 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), | 384 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), |
386 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), | 385 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), |
387 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), | 386 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), |
388 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); | 387 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); |
389 | 388 |
390 } // namespace webrtc | 389 } // namespace webrtc |
OLD | NEW |