OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 } | 48 } |
49 | 49 |
50 // Test requesting multiples of ChunkSize() frames results in the proper number | 50 // Test requesting multiples of ChunkSize() frames results in the proper number |
51 // of callbacks. | 51 // of callbacks. |
52 TEST(SincResamplerTest, ChunkedResample) { | 52 TEST(SincResamplerTest, ChunkedResample) { |
53 MockSource mock_source; | 53 MockSource mock_source; |
54 | 54 |
55 // Choose a high ratio of input to output samples which will result in quick | 55 // Choose a high ratio of input to output samples which will result in quick |
56 // exhaustion of SincResampler's internal buffers. | 56 // exhaustion of SincResampler's internal buffers. |
57 SincResampler resampler( | 57 SincResampler resampler( |
58 kSampleRateRatio, | 58 kSampleRateRatio, SincResampler::kDefaultBlockSize, |
59 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | 59 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); |
60 | 60 |
61 static const int kChunks = 2; | 61 static const int kChunks = 2; |
62 int max_chunk_size = resampler.ChunkSize() * kChunks; | 62 int max_chunk_size = resampler.ChunkSize() * kChunks; |
63 scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]); | 63 scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]); |
64 | 64 |
65 // Verify requesting ChunkSize() frames causes a single callback. | 65 // Verify requesting ChunkSize() frames causes a single callback. |
66 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 66 EXPECT_CALL(mock_source, ProvideInput(_, _)) |
67 .Times(1).WillOnce(ClearBuffer()); | 67 .Times(1).WillOnce(ClearBuffer()); |
68 resampler.Resample(resampled_destination.get(), resampler.ChunkSize()); | 68 resampler.Resample(resampled_destination.get(), resampler.ChunkSize()); |
69 | 69 |
70 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. | 70 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. |
71 testing::Mock::VerifyAndClear(&mock_source); | 71 testing::Mock::VerifyAndClear(&mock_source); |
72 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 72 EXPECT_CALL(mock_source, ProvideInput(_, _)) |
73 .Times(kChunks).WillRepeatedly(ClearBuffer()); | 73 .Times(kChunks).WillRepeatedly(ClearBuffer()); |
74 resampler.Resample(resampled_destination.get(), max_chunk_size); | 74 resampler.Resample(resampled_destination.get(), max_chunk_size); |
75 } | 75 } |
76 | 76 |
77 // Test flush resets the internal state properly. | 77 // Test flush resets the internal state properly. |
78 TEST(SincResamplerTest, Flush) { | 78 TEST(SincResamplerTest, Flush) { |
79 MockSource mock_source; | 79 MockSource mock_source; |
80 SincResampler resampler( | 80 SincResampler resampler( |
81 kSampleRateRatio, | 81 kSampleRateRatio, SincResampler::kDefaultBlockSize, |
82 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | 82 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); |
83 scoped_ptr<float[]> resampled_destination(new float[resampler.ChunkSize()]); | 83 scoped_ptr<float[]> resampled_destination(new float[resampler.ChunkSize()]); |
84 | 84 |
85 // Fill the resampler with junk data. | 85 // Fill the resampler with junk data. |
86 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 86 EXPECT_CALL(mock_source, ProvideInput(_, _)) |
87 .Times(1).WillOnce(FillBuffer()); | 87 .Times(1).WillOnce(FillBuffer()); |
88 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); | 88 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); |
89 ASSERT_NE(resampled_destination[0], 0); | 89 ASSERT_NE(resampled_destination[0], 0); |
90 | 90 |
91 // Flush and request more data, which should all be zeros now. | 91 // Flush and request more data, which should all be zeros now. |
92 resampler.Flush(); | 92 resampler.Flush(); |
93 testing::Mock::VerifyAndClear(&mock_source); | 93 testing::Mock::VerifyAndClear(&mock_source); |
94 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 94 EXPECT_CALL(mock_source, ProvideInput(_, _)) |
95 .Times(1).WillOnce(ClearBuffer()); | 95 .Times(1).WillOnce(ClearBuffer()); |
96 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); | 96 resampler.Resample(resampled_destination.get(), resampler.ChunkSize() / 2); |
97 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) | 97 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) |
98 ASSERT_FLOAT_EQ(resampled_destination[i], 0); | 98 ASSERT_FLOAT_EQ(resampled_destination[i], 0); |
99 } | 99 } |
100 | 100 |
101 // Test flush resets the internal state properly. | 101 // Test flush resets the internal state properly. |
102 TEST(SincResamplerTest, DISABLED_SetRatioBench) { | 102 TEST(SincResamplerTest, DISABLED_SetRatioBench) { |
103 MockSource mock_source; | 103 MockSource mock_source; |
104 SincResampler resampler( | 104 SincResampler resampler( |
105 kSampleRateRatio, | 105 kSampleRateRatio, SincResampler::kDefaultBlockSize, |
106 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | 106 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); |
107 | 107 |
108 base::TimeTicks start = base::TimeTicks::HighResNow(); | 108 base::TimeTicks start = base::TimeTicks::HighResNow(); |
109 for (int i = 1; i < 10000; ++i) | 109 for (int i = 1; i < 10000; ++i) |
110 resampler.SetRatio(1.0 / i); | 110 resampler.SetRatio(1.0 / i); |
111 double total_time_c_ms = | 111 double total_time_c_ms = |
112 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 112 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); |
113 printf("SetRatio() took %.2fms.\n", total_time_c_ms); | 113 printf("SetRatio() took %.2fms.\n", total_time_c_ms); |
114 } | 114 } |
115 | 115 |
(...skipping 10 matching lines...) Expand all Loading... |
126 // will be tested by the parameterized SincResampler tests below. | 126 // will be tested by the parameterized SincResampler tests below. |
127 #if defined(CONVOLVE_FUNC) | 127 #if defined(CONVOLVE_FUNC) |
128 TEST(SincResamplerTest, Convolve) { | 128 TEST(SincResamplerTest, Convolve) { |
129 #if defined(ARCH_CPU_X86_FAMILY) | 129 #if defined(ARCH_CPU_X86_FAMILY) |
130 ASSERT_TRUE(base::CPU().has_sse()); | 130 ASSERT_TRUE(base::CPU().has_sse()); |
131 #endif | 131 #endif |
132 | 132 |
133 // Initialize a dummy resampler. | 133 // Initialize a dummy resampler. |
134 MockSource mock_source; | 134 MockSource mock_source; |
135 SincResampler resampler( | 135 SincResampler resampler( |
136 kSampleRateRatio, | 136 kSampleRateRatio, SincResampler::kDefaultBlockSize, |
137 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | 137 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); |
138 | 138 |
139 // The optimized Convolve methods are slightly more precise than Convolve_C(), | 139 // The optimized Convolve methods are slightly more precise than Convolve_C(), |
140 // so comparison must be done using an epsilon. | 140 // so comparison must be done using an epsilon. |
141 static const double kEpsilon = 0.00000005; | 141 static const double kEpsilon = 0.00000005; |
142 | 142 |
143 // Use a kernel from SincResampler as input and kernel data, this has the | 143 // Use a kernel from SincResampler as input and kernel data, this has the |
144 // benefit of already being properly sized and aligned for Convolve_SSE(). | 144 // benefit of already being properly sized and aligned for Convolve_SSE(). |
145 double result = resampler.Convolve_C( | 145 double result = resampler.Convolve_C( |
146 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 146 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
(...skipping 14 matching lines...) Expand all Loading... |
161 } | 161 } |
162 #endif | 162 #endif |
163 | 163 |
164 // Benchmark for the various Convolve() methods. Make sure to build with | 164 // Benchmark for the various Convolve() methods. Make sure to build with |
165 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original | 165 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original |
166 // benchmarks were run with --convolve-iterations=50000000. | 166 // benchmarks were run with --convolve-iterations=50000000. |
167 TEST(SincResamplerTest, ConvolveBenchmark) { | 167 TEST(SincResamplerTest, ConvolveBenchmark) { |
168 // Initialize a dummy resampler. | 168 // Initialize a dummy resampler. |
169 MockSource mock_source; | 169 MockSource mock_source; |
170 SincResampler resampler( | 170 SincResampler resampler( |
171 kSampleRateRatio, | 171 kSampleRateRatio, SincResampler::kDefaultBlockSize, |
172 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | 172 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); |
173 | 173 |
174 // Retrieve benchmark iterations from command line. | 174 // Retrieve benchmark iterations from command line. |
175 int convolve_iterations = 10; | 175 int convolve_iterations = 10; |
176 std::string iterations(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 176 std::string iterations(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
177 kConvolveIterations)); | 177 kConvolveIterations)); |
178 if (!iterations.empty()) | 178 if (!iterations.empty()) |
179 base::StringToInt(iterations, &convolve_iterations); | 179 base::StringToInt(iterations, &convolve_iterations); |
180 | 180 |
181 printf("Benchmarking %d iterations:\n", convolve_iterations); | 181 printf("Benchmarking %d iterations:\n", convolve_iterations); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 | 310 |
311 // Nyquist frequency for the input sampling rate. | 311 // Nyquist frequency for the input sampling rate. |
312 double input_nyquist_freq = 0.5 * input_rate_; | 312 double input_nyquist_freq = 0.5 * input_rate_; |
313 | 313 |
314 // Source for data to be resampled. | 314 // Source for data to be resampled. |
315 SinusoidalLinearChirpSource resampler_source( | 315 SinusoidalLinearChirpSource resampler_source( |
316 input_rate_, input_samples, input_nyquist_freq); | 316 input_rate_, input_samples, input_nyquist_freq); |
317 | 317 |
318 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); | 318 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); |
319 SincResampler resampler( | 319 SincResampler resampler( |
320 io_ratio, | 320 io_ratio, SincResampler::kDefaultBlockSize, |
321 base::Bind(&SinusoidalLinearChirpSource::ProvideInput, | 321 base::Bind(&SinusoidalLinearChirpSource::ProvideInput, |
322 base::Unretained(&resampler_source))); | 322 base::Unretained(&resampler_source))); |
323 | 323 |
324 // Force an update to the sample rate ratio to ensure dyanmic sample rate | 324 // Force an update to the sample rate ratio to ensure dyanmic sample rate |
325 // changes are working correctly. | 325 // changes are working correctly. |
326 scoped_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]); | 326 scoped_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]); |
327 memcpy(kernel.get(), resampler.get_kernel_for_testing(), | 327 memcpy(kernel.get(), resampler.get_kernel_for_testing(), |
328 SincResampler::kKernelStorageSize); | 328 SincResampler::kKernelStorageSize); |
329 resampler.SetRatio(M_PI); | 329 resampler.SetRatio(M_PI); |
330 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), | 330 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), | 434 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), |
435 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), | 435 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), |
436 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), | 436 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), |
437 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), | 437 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), |
438 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), | 438 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), |
439 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), | 439 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), |
440 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), | 440 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), |
441 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); | 441 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); |
442 | 442 |
443 } // namespace media | 443 } // namespace media |
OLD | NEW |