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
3 // found in the LICENSE file.
5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16 #include "media/base/sinc_resampler.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
24 static const double kSampleRateRatio
= 192000.0 / 44100.0;
26 // Helper class to ensure ChunkedResample() functions properly.
29 MOCK_METHOD2(ProvideInput
, void(int frames
, float* destination
));
33 memset(arg1
, 0, arg0
* sizeof(float));
37 // Value chosen arbitrarily such that SincResampler resamples it to something
38 // easily representable on all platforms; e.g., using kSampleRateRatio this
40 memset(arg1
, 64, arg0
* sizeof(float));
43 // Test requesting multiples of ChunkSize() frames results in the proper number
45 TEST(SincResamplerTest
, ChunkedResample
) {
46 MockSource mock_source
;
48 // Choose a high ratio of input to output samples which will result in quick
49 // exhaustion of SincResampler's internal buffers.
50 SincResampler
resampler(
51 kSampleRateRatio
, SincResampler::kDefaultRequestSize
,
52 base::Bind(&MockSource::ProvideInput
, base::Unretained(&mock_source
)));
54 static const int kChunks
= 2;
55 int max_chunk_size
= resampler
.ChunkSize() * kChunks
;
56 scoped_ptr
<float[]> resampled_destination(new float[max_chunk_size
]);
58 // Verify requesting ChunkSize() frames causes a single callback.
59 EXPECT_CALL(mock_source
, ProvideInput(_
, _
))
60 .Times(1).WillOnce(ClearBuffer());
61 resampler
.Resample(resampler
.ChunkSize(), resampled_destination
.get());
63 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks.
64 testing::Mock::VerifyAndClear(&mock_source
);
65 EXPECT_CALL(mock_source
, ProvideInput(_
, _
))
66 .Times(kChunks
).WillRepeatedly(ClearBuffer());
67 resampler
.Resample(max_chunk_size
, resampled_destination
.get());
70 // Test flush resets the internal state properly.
71 TEST(SincResamplerTest
, Flush
) {
72 MockSource mock_source
;
73 SincResampler
resampler(
74 kSampleRateRatio
, SincResampler::kDefaultRequestSize
,
75 base::Bind(&MockSource::ProvideInput
, base::Unretained(&mock_source
)));
76 scoped_ptr
<float[]> resampled_destination(new float[resampler
.ChunkSize()]);
78 // Fill the resampler with junk data.
79 EXPECT_CALL(mock_source
, ProvideInput(_
, _
))
80 .Times(1).WillOnce(FillBuffer());
81 resampler
.Resample(resampler
.ChunkSize() / 2, resampled_destination
.get());
82 ASSERT_NE(resampled_destination
[0], 0);
84 // Flush and request more data, which should all be zeros now.
86 testing::Mock::VerifyAndClear(&mock_source
);
87 EXPECT_CALL(mock_source
, ProvideInput(_
, _
))
88 .Times(1).WillOnce(ClearBuffer());
89 resampler
.Resample(resampler
.ChunkSize() / 2, resampled_destination
.get());
90 for (int i
= 0; i
< resampler
.ChunkSize() / 2; ++i
)
91 ASSERT_FLOAT_EQ(resampled_destination
[i
], 0);
94 // Test flush resets the internal state properly.
95 TEST(SincResamplerTest
, DISABLED_SetRatioBench
) {
96 MockSource mock_source
;
97 SincResampler
resampler(
98 kSampleRateRatio
, SincResampler::kDefaultRequestSize
,
99 base::Bind(&MockSource::ProvideInput
, base::Unretained(&mock_source
)));
101 base::TimeTicks start
= base::TimeTicks::HighResNow();
102 for (int i
= 1; i
< 10000; ++i
)
103 resampler
.SetRatio(1.0 / i
);
104 double total_time_c_ms
=
105 (base::TimeTicks::HighResNow() - start
).InMillisecondsF();
106 printf("SetRatio() took %.2fms.\n", total_time_c_ms
);
110 // Define platform independent function name for Convolve* tests.
111 #if defined(ARCH_CPU_X86_FAMILY)
112 #define CONVOLVE_FUNC Convolve_SSE
113 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
114 #define CONVOLVE_FUNC Convolve_NEON
117 // Ensure various optimized Convolve() methods return the same value. Only run
118 // this test if other optimized methods exist, otherwise the default Convolve()
119 // will be tested by the parameterized SincResampler tests below.
120 #if defined(CONVOLVE_FUNC)
121 static const double kKernelInterpolationFactor
= 0.5;
123 TEST(SincResamplerTest
, Convolve
) {
124 #if defined(ARCH_CPU_X86_FAMILY)
125 ASSERT_TRUE(base::CPU().has_sse());
128 // Initialize a dummy resampler.
129 MockSource mock_source
;
130 SincResampler
resampler(
131 kSampleRateRatio
, SincResampler::kDefaultRequestSize
,
132 base::Bind(&MockSource::ProvideInput
, base::Unretained(&mock_source
)));
134 // The optimized Convolve methods are slightly more precise than Convolve_C(),
135 // so comparison must be done using an epsilon.
136 static const double kEpsilon
= 0.00000005;
138 // Use a kernel from SincResampler as input and kernel data, this has the
139 // benefit of already being properly sized and aligned for Convolve_SSE().
140 double result
= resampler
.Convolve_C(
141 resampler
.kernel_storage_
.get(), resampler
.kernel_storage_
.get(),
142 resampler
.kernel_storage_
.get(), kKernelInterpolationFactor
);
143 double result2
= resampler
.CONVOLVE_FUNC(
144 resampler
.kernel_storage_
.get(), resampler
.kernel_storage_
.get(),
145 resampler
.kernel_storage_
.get(), kKernelInterpolationFactor
);
146 EXPECT_NEAR(result2
, result
, kEpsilon
);
148 // Test Convolve() w/ unaligned input pointer.
149 result
= resampler
.Convolve_C(
150 resampler
.kernel_storage_
.get() + 1, resampler
.kernel_storage_
.get(),
151 resampler
.kernel_storage_
.get(), kKernelInterpolationFactor
);
152 result2
= resampler
.CONVOLVE_FUNC(
153 resampler
.kernel_storage_
.get() + 1, resampler
.kernel_storage_
.get(),
154 resampler
.kernel_storage_
.get(), kKernelInterpolationFactor
);
155 EXPECT_NEAR(result2
, result
, kEpsilon
);
159 // Fake audio source for testing the resampler. Generates a sinusoidal linear
160 // chirp (http://en.wikipedia.org/wiki/Chirp) which can be tuned to stress the
161 // resampler for the specific sample rate conversion being used.
162 class SinusoidalLinearChirpSource
{
164 SinusoidalLinearChirpSource(int sample_rate
,
166 double max_frequency
)
167 : sample_rate_(sample_rate
),
168 total_samples_(samples
),
169 max_frequency_(max_frequency
),
172 double duration
= static_cast<double>(total_samples_
) / sample_rate_
;
173 k_
= (max_frequency_
- kMinFrequency
) / duration
;
176 virtual ~SinusoidalLinearChirpSource() {}
178 void ProvideInput(int frames
, float* destination
) {
179 for (int i
= 0; i
< frames
; ++i
, ++current_index_
) {
180 // Filter out frequencies higher than Nyquist.
181 if (Frequency(current_index_
) > 0.5 * sample_rate_
) {
184 // Calculate time in seconds.
185 double t
= static_cast<double>(current_index_
) / sample_rate_
;
187 // Sinusoidal linear chirp.
188 destination
[i
] = sin(2 * M_PI
* (kMinFrequency
* t
+ (k_
/ 2) * t
* t
));
193 double Frequency(int position
) {
194 return kMinFrequency
+ position
* (max_frequency_
- kMinFrequency
)
205 double max_frequency_
;
209 DISALLOW_COPY_AND_ASSIGN(SinusoidalLinearChirpSource
);
212 typedef std::tr1::tuple
<int, int, double, double> SincResamplerTestData
;
213 class SincResamplerTest
214 : public testing::TestWithParam
<SincResamplerTestData
> {
217 : input_rate_(std::tr1::get
<0>(GetParam())),
218 output_rate_(std::tr1::get
<1>(GetParam())),
219 rms_error_(std::tr1::get
<2>(GetParam())),
220 low_freq_error_(std::tr1::get
<3>(GetParam())) {
223 virtual ~SincResamplerTest() {}
229 double low_freq_error_
;
232 // Tests resampling using a given input and output sample rate.
233 TEST_P(SincResamplerTest
, Resample
) {
234 // Make comparisons using one second of data.
235 static const double kTestDurationSecs
= 1;
236 int input_samples
= kTestDurationSecs
* input_rate_
;
237 int output_samples
= kTestDurationSecs
* output_rate_
;
239 // Nyquist frequency for the input sampling rate.
240 double input_nyquist_freq
= 0.5 * input_rate_
;
242 // Source for data to be resampled.
243 SinusoidalLinearChirpSource
resampler_source(
244 input_rate_
, input_samples
, input_nyquist_freq
);
246 const double io_ratio
= input_rate_
/ static_cast<double>(output_rate_
);
247 SincResampler
resampler(
248 io_ratio
, SincResampler::kDefaultRequestSize
,
249 base::Bind(&SinusoidalLinearChirpSource::ProvideInput
,
250 base::Unretained(&resampler_source
)));
252 // Force an update to the sample rate ratio to ensure dyanmic sample rate
253 // changes are working correctly.
254 scoped_ptr
<float[]> kernel(new float[SincResampler::kKernelStorageSize
]);
255 memcpy(kernel
.get(), resampler
.get_kernel_for_testing(),
256 SincResampler::kKernelStorageSize
);
257 resampler
.SetRatio(M_PI
);
258 ASSERT_NE(0, memcmp(kernel
.get(), resampler
.get_kernel_for_testing(),
259 SincResampler::kKernelStorageSize
));
260 resampler
.SetRatio(io_ratio
);
261 ASSERT_EQ(0, memcmp(kernel
.get(), resampler
.get_kernel_for_testing(),
262 SincResampler::kKernelStorageSize
));
264 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to
265 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes.
266 scoped_ptr
<float[]> resampled_destination(new float[output_samples
]);
267 scoped_ptr
<float[]> pure_destination(new float[output_samples
]);
269 // Generate resampled signal.
270 resampler
.Resample(output_samples
, resampled_destination
.get());
272 // Generate pure signal.
273 SinusoidalLinearChirpSource
pure_source(
274 output_rate_
, output_samples
, input_nyquist_freq
);
275 pure_source
.ProvideInput(output_samples
, pure_destination
.get());
277 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which
278 // we refer to as low and high.
279 static const double kLowFrequencyNyquistRange
= 0.7;
280 static const double kHighFrequencyNyquistRange
= 0.9;
282 // Calculate Root-Mean-Square-Error and maximum error for the resampling.
283 double sum_of_squares
= 0;
284 double low_freq_max_error
= 0;
285 double high_freq_max_error
= 0;
286 int minimum_rate
= std::min(input_rate_
, output_rate_
);
287 double low_frequency_range
= kLowFrequencyNyquistRange
* 0.5 * minimum_rate
;
288 double high_frequency_range
= kHighFrequencyNyquistRange
* 0.5 * minimum_rate
;
289 for (int i
= 0; i
< output_samples
; ++i
) {
290 double error
= fabs(resampled_destination
[i
] - pure_destination
[i
]);
292 if (pure_source
.Frequency(i
) < low_frequency_range
) {
293 if (error
> low_freq_max_error
)
294 low_freq_max_error
= error
;
295 } else if (pure_source
.Frequency(i
) < high_frequency_range
) {
296 if (error
> high_freq_max_error
)
297 high_freq_max_error
= error
;
299 // TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange.
301 sum_of_squares
+= error
* error
;
304 double rms_error
= sqrt(sum_of_squares
/ output_samples
);
306 // Convert each error to dbFS.
307 #define DBFS(x) 20 * log10(x)
308 rms_error
= DBFS(rms_error
);
309 low_freq_max_error
= DBFS(low_freq_max_error
);
310 high_freq_max_error
= DBFS(high_freq_max_error
);
312 EXPECT_LE(rms_error
, rms_error_
);
313 EXPECT_LE(low_freq_max_error
, low_freq_error_
);
315 // All conversions currently have a high frequency error around -6 dbFS.
316 static const double kHighFrequencyMaxError
= -6.02;
317 EXPECT_LE(high_freq_max_error
, kHighFrequencyMaxError
);
320 // Almost all conversions have an RMS error of around -14 dbFS.
321 static const double kResamplingRMSError
= -14.58;
323 // Thresholds chosen arbitrarily based on what each resampling reported during
324 // testing. All thresholds are in dbFS, http://en.wikipedia.org/wiki/DBFS.
325 INSTANTIATE_TEST_CASE_P(
326 SincResamplerTest
, SincResamplerTest
, testing::Values(
328 std::tr1::make_tuple(8000, 44100, kResamplingRMSError
, -62.73),
329 std::tr1::make_tuple(11025, 44100, kResamplingRMSError
, -72.19),
330 std::tr1::make_tuple(16000, 44100, kResamplingRMSError
, -62.54),
331 std::tr1::make_tuple(22050, 44100, kResamplingRMSError
, -73.53),
332 std::tr1::make_tuple(32000, 44100, kResamplingRMSError
, -63.32),
333 std::tr1::make_tuple(44100, 44100, kResamplingRMSError
, -73.53),
334 std::tr1::make_tuple(48000, 44100, -15.01, -64.04),
335 std::tr1::make_tuple(96000, 44100, -18.49, -25.51),
336 std::tr1::make_tuple(192000, 44100, -20.50, -13.31),
339 std::tr1::make_tuple(8000, 48000, kResamplingRMSError
, -63.43),
340 std::tr1::make_tuple(11025, 48000, kResamplingRMSError
, -62.61),
341 std::tr1::make_tuple(16000, 48000, kResamplingRMSError
, -63.96),
342 std::tr1::make_tuple(22050, 48000, kResamplingRMSError
, -62.42),
343 std::tr1::make_tuple(32000, 48000, kResamplingRMSError
, -64.04),
344 std::tr1::make_tuple(44100, 48000, kResamplingRMSError
, -62.63),
345 std::tr1::make_tuple(48000, 48000, kResamplingRMSError
, -73.52),
346 std::tr1::make_tuple(96000, 48000, -18.40, -28.44),
347 std::tr1::make_tuple(192000, 48000, -20.43, -14.11),
350 std::tr1::make_tuple(8000, 96000, kResamplingRMSError
, -63.19),
351 std::tr1::make_tuple(11025, 96000, kResamplingRMSError
, -62.61),
352 std::tr1::make_tuple(16000, 96000, kResamplingRMSError
, -63.39),
353 std::tr1::make_tuple(22050, 96000, kResamplingRMSError
, -62.42),
354 std::tr1::make_tuple(32000, 96000, kResamplingRMSError
, -63.95),
355 std::tr1::make_tuple(44100, 96000, kResamplingRMSError
, -62.63),
356 std::tr1::make_tuple(48000, 96000, kResamplingRMSError
, -73.52),
357 std::tr1::make_tuple(96000, 96000, kResamplingRMSError
, -73.52),
358 std::tr1::make_tuple(192000, 96000, kResamplingRMSError
, -28.41),
361 std::tr1::make_tuple(8000, 192000, kResamplingRMSError
, -63.10),
362 std::tr1::make_tuple(11025, 192000, kResamplingRMSError
, -62.61),
363 std::tr1::make_tuple(16000, 192000, kResamplingRMSError
, -63.14),
364 std::tr1::make_tuple(22050, 192000, kResamplingRMSError
, -62.42),
365 std::tr1::make_tuple(32000, 192000, kResamplingRMSError
, -63.38),
366 std::tr1::make_tuple(44100, 192000, kResamplingRMSError
, -62.63),
367 std::tr1::make_tuple(48000, 192000, kResamplingRMSError
, -73.44),
368 std::tr1::make_tuple(96000, 192000, kResamplingRMSError
, -73.52),
369 std::tr1::make_tuple(192000, 192000, kResamplingRMSError
, -73.52)));