1 /*
2 * Copyright (c) 2013 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 // Modified from the Chromium original:
12 // src/media/base/sinc_resampler_unittest.cc
13
14 // MSVC++ requires this to be set before any other includes to get M_PI.
15 #define _USE_MATH_DEFINES
16
17 #include "common_audio/resampler/sinc_resampler.h"
18
19 #include <math.h>
20
21 #include <algorithm>
22 #include <memory>
23 #include <tuple>
24
25 #include "common_audio/resampler/sinusoidal_linear_chirp_source.h"
26 #include "rtc_base/stringize_macros.h"
27 #include "rtc_base/system/arch.h"
28 #include "rtc_base/time_utils.h"
29 #include "system_wrappers/include/cpu_features_wrapper.h"
30 #include "test/gmock.h"
31 #include "test/gtest.h"
32
33 using ::testing::_;
34
35 namespace webrtc {
36
37 static const double kSampleRateRatio = 192000.0 / 44100.0;
38 static const double kKernelInterpolationFactor = 0.5;
39
40 // Helper class to ensure ChunkedResample() functions properly.
41 class MockSource : public SincResamplerCallback {
42 public:
43 MOCK_METHOD(void, Run, (size_t frames, float* destination), (override));
44 };
45
ACTION(ClearBuffer)46 ACTION(ClearBuffer) {
47 memset(arg1, 0, arg0 * sizeof(float));
48 }
49
ACTION(FillBuffer)50 ACTION(FillBuffer) {
51 // Value chosen arbitrarily such that SincResampler resamples it to something
52 // easily representable on all platforms; e.g., using kSampleRateRatio this
53 // becomes 1.81219.
54 memset(arg1, 64, arg0 * sizeof(float));
55 }
56
57 // Test requesting multiples of ChunkSize() frames results in the proper number
58 // of callbacks.
TEST(SincResamplerTest,ChunkedResample)59 TEST(SincResamplerTest, ChunkedResample) {
60 MockSource mock_source;
61
62 // Choose a high ratio of input to output samples which will result in quick
63 // exhaustion of SincResampler's internal buffers.
64 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize,
65 &mock_source);
66
67 static const int kChunks = 2;
68 size_t max_chunk_size = resampler.ChunkSize() * kChunks;
69 std::unique_ptr<float[]> resampled_destination(new float[max_chunk_size]);
70
71 // Verify requesting ChunkSize() frames causes a single callback.
72 EXPECT_CALL(mock_source, Run(_, _)).Times(1).WillOnce(ClearBuffer());
73 resampler.Resample(resampler.ChunkSize(), resampled_destination.get());
74
75 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks.
76 ::testing::Mock::VerifyAndClear(&mock_source);
77 EXPECT_CALL(mock_source, Run(_, _))
78 .Times(kChunks)
79 .WillRepeatedly(ClearBuffer());
80 resampler.Resample(max_chunk_size, resampled_destination.get());
81 }
82
83 // Test flush resets the internal state properly.
TEST(SincResamplerTest,Flush)84 TEST(SincResamplerTest, Flush) {
85 MockSource mock_source;
86 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize,
87 &mock_source);
88 std::unique_ptr<float[]> resampled_destination(
89 new float[resampler.ChunkSize()]);
90
91 // Fill the resampler with junk data.
92 EXPECT_CALL(mock_source, Run(_, _)).Times(1).WillOnce(FillBuffer());
93 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get());
94 ASSERT_NE(resampled_destination[0], 0);
95
96 // Flush and request more data, which should all be zeros now.
97 resampler.Flush();
98 ::testing::Mock::VerifyAndClear(&mock_source);
99 EXPECT_CALL(mock_source, Run(_, _)).Times(1).WillOnce(ClearBuffer());
100 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get());
101 for (size_t i = 0; i < resampler.ChunkSize() / 2; ++i)
102 ASSERT_FLOAT_EQ(resampled_destination[i], 0);
103 }
104
105 // Test flush resets the internal state properly.
TEST(SincResamplerTest,DISABLED_SetRatioBench)106 TEST(SincResamplerTest, DISABLED_SetRatioBench) {
107 MockSource mock_source;
108 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize,
109 &mock_source);
110
111 int64_t start = rtc::TimeNanos();
112 for (int i = 1; i < 10000; ++i)
113 resampler.SetRatio(1.0 / i);
114 double total_time_c_us =
115 (rtc::TimeNanos() - start) / rtc::kNumNanosecsPerMicrosec;
116 printf("SetRatio() took %.2fms.\n", total_time_c_us / 1000);
117 }
118
119 // Define platform independent function name for Convolve* tests.
120 #if defined(WEBRTC_ARCH_X86_FAMILY)
121 #define CONVOLVE_FUNC Convolve_SSE
122 #elif defined(WEBRTC_ARCH_ARM_V7)
123 #define CONVOLVE_FUNC Convolve_NEON
124 #endif
125
126 // Ensure various optimized Convolve() methods return the same value. Only run
127 // this test if other optimized methods exist, otherwise the default Convolve()
128 // will be tested by the parameterized SincResampler tests below.
129 #if defined(CONVOLVE_FUNC)
TEST(SincResamplerTest,Convolve)130 TEST(SincResamplerTest, Convolve) {
131 #if defined(WEBRTC_ARCH_X86_FAMILY)
132 ASSERT_TRUE(WebRtc_GetCPUInfo(kSSE2));
133 #elif defined(WEBRTC_ARCH_ARM_V7)
134 ASSERT_TRUE(WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON);
135 #endif
136
137 // Initialize a dummy resampler.
138 MockSource mock_source;
139 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize,
140 &mock_source);
141
142 // The optimized Convolve methods are slightly more precise than Convolve_C(),
143 // so comparison must be done using an epsilon.
144 static const double kEpsilon = 0.00000005;
145
146 // Use a kernel from SincResampler as input and kernel data, this has the
147 // benefit of already being properly sized and aligned for Convolve_SSE().
148 double result = resampler.Convolve_C(
149 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
150 resampler.kernel_storage_.get(), kKernelInterpolationFactor);
151 double result2 = resampler.CONVOLVE_FUNC(
152 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
153 resampler.kernel_storage_.get(), kKernelInterpolationFactor);
154 EXPECT_NEAR(result2, result, kEpsilon);
155
156 // Test Convolve() w/ unaligned input pointer.
157 result = resampler.Convolve_C(
158 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(),
159 resampler.kernel_storage_.get(), kKernelInterpolationFactor);
160 result2 = resampler.CONVOLVE_FUNC(
161 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(),
162 resampler.kernel_storage_.get(), kKernelInterpolationFactor);
163 EXPECT_NEAR(result2, result, kEpsilon);
164 }
165 #endif
166
167 // Benchmark for the various Convolve() methods. Make sure to build with
168 // branding=Chrome so that RTC_DCHECKs are compiled out when benchmarking.
169 // Original benchmarks were run with --convolve-iterations=50000000.
TEST(SincResamplerTest,ConvolveBenchmark)170 TEST(SincResamplerTest, ConvolveBenchmark) {
171 // Initialize a dummy resampler.
172 MockSource mock_source;
173 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize,
174 &mock_source);
175
176 // Retrieve benchmark iterations from command line.
177 // TODO(ajm): Reintroduce this as a command line option.
178 const int kConvolveIterations = 1000000;
179
180 printf("Benchmarking %d iterations:\n", kConvolveIterations);
181
182 // Benchmark Convolve_C().
183 int64_t start = rtc::TimeNanos();
184 for (int i = 0; i < kConvolveIterations; ++i) {
185 resampler.Convolve_C(
186 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
187 resampler.kernel_storage_.get(), kKernelInterpolationFactor);
188 }
189 double total_time_c_us =
190 (rtc::TimeNanos() - start) / rtc::kNumNanosecsPerMicrosec;
191 printf("Convolve_C took %.2fms.\n", total_time_c_us / 1000);
192
193 #if defined(CONVOLVE_FUNC)
194 #if defined(WEBRTC_ARCH_X86_FAMILY)
195 ASSERT_TRUE(WebRtc_GetCPUInfo(kSSE2));
196 #elif defined(WEBRTC_ARCH_ARM_V7)
197 ASSERT_TRUE(WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON);
198 #endif
199
200 // Benchmark with unaligned input pointer.
201 start = rtc::TimeNanos();
202 for (int j = 0; j < kConvolveIterations; ++j) {
203 resampler.CONVOLVE_FUNC(
204 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(),
205 resampler.kernel_storage_.get(), kKernelInterpolationFactor);
206 }
207 double total_time_optimized_unaligned_us =
208 (rtc::TimeNanos() - start) / rtc::kNumNanosecsPerMicrosec;
209 printf(STRINGIZE(CONVOLVE_FUNC) "(unaligned) took %.2fms; which is %.2fx "
210 "faster than Convolve_C.\n", total_time_optimized_unaligned_us / 1000,
211 total_time_c_us / total_time_optimized_unaligned_us);
212
213 // Benchmark with aligned input pointer.
214 start = rtc::TimeNanos();
215 for (int j = 0; j < kConvolveIterations; ++j) {
216 resampler.CONVOLVE_FUNC(
217 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(),
218 resampler.kernel_storage_.get(), kKernelInterpolationFactor);
219 }
220 double total_time_optimized_aligned_us =
221 (rtc::TimeNanos() - start) / rtc::kNumNanosecsPerMicrosec;
222 printf(STRINGIZE(CONVOLVE_FUNC) " (aligned) took %.2fms; which is %.2fx "
223 "faster than Convolve_C and %.2fx faster than "
224 STRINGIZE(CONVOLVE_FUNC) " (unaligned).\n",
225 total_time_optimized_aligned_us / 1000,
226 total_time_c_us / total_time_optimized_aligned_us,
227 total_time_optimized_unaligned_us / total_time_optimized_aligned_us);
228 #endif
229 }
230
231 #undef CONVOLVE_FUNC
232
233 typedef std::tuple<int, int, double, double> SincResamplerTestData;
234 class SincResamplerTest
235 : public ::testing::TestWithParam<SincResamplerTestData> {
236 public:
SincResamplerTest()237 SincResamplerTest()
238 : input_rate_(std::get<0>(GetParam())),
239 output_rate_(std::get<1>(GetParam())),
240 rms_error_(std::get<2>(GetParam())),
241 low_freq_error_(std::get<3>(GetParam())) {}
242
~SincResamplerTest()243 virtual ~SincResamplerTest() {}
244
245 protected:
246 int input_rate_;
247 int output_rate_;
248 double rms_error_;
249 double low_freq_error_;
250 };
251
252 // Tests resampling using a given input and output sample rate.
TEST_P(SincResamplerTest,Resample)253 TEST_P(SincResamplerTest, Resample) {
254 // Make comparisons using one second of data.
255 static const double kTestDurationSecs = 1;
256 const size_t input_samples =
257 static_cast<size_t>(kTestDurationSecs * input_rate_);
258 const size_t output_samples =
259 static_cast<size_t>(kTestDurationSecs * output_rate_);
260
261 // Nyquist frequency for the input sampling rate.
262 const double input_nyquist_freq = 0.5 * input_rate_;
263
264 // Source for data to be resampled.
265 SinusoidalLinearChirpSource resampler_source(input_rate_, input_samples,
266 input_nyquist_freq, 0);
267
268 const double io_ratio = input_rate_ / static_cast<double>(output_rate_);
269 SincResampler resampler(io_ratio, SincResampler::kDefaultRequestSize,
270 &resampler_source);
271
272 // Force an update to the sample rate ratio to ensure dyanmic sample rate
273 // changes are working correctly.
274 std::unique_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]);
275 memcpy(kernel.get(), resampler.get_kernel_for_testing(),
276 SincResampler::kKernelStorageSize);
277 resampler.SetRatio(M_PI);
278 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(),
279 SincResampler::kKernelStorageSize));
280 resampler.SetRatio(io_ratio);
281 ASSERT_EQ(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(),
282 SincResampler::kKernelStorageSize));
283
284 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to
285 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes.
286 std::unique_ptr<float[]> resampled_destination(new float[output_samples]);
287 std::unique_ptr<float[]> pure_destination(new float[output_samples]);
288
289 // Generate resampled signal.
290 resampler.Resample(output_samples, resampled_destination.get());
291
292 // Generate pure signal.
293 SinusoidalLinearChirpSource pure_source(output_rate_, output_samples,
294 input_nyquist_freq, 0);
295 pure_source.Run(output_samples, pure_destination.get());
296
297 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which
298 // we refer to as low and high.
299 static const double kLowFrequencyNyquistRange = 0.7;
300 static const double kHighFrequencyNyquistRange = 0.9;
301
302 // Calculate Root-Mean-Square-Error and maximum error for the resampling.
303 double sum_of_squares = 0;
304 double low_freq_max_error = 0;
305 double high_freq_max_error = 0;
306 int minimum_rate = std::min(input_rate_, output_rate_);
307 double low_frequency_range = kLowFrequencyNyquistRange * 0.5 * minimum_rate;
308 double high_frequency_range = kHighFrequencyNyquistRange * 0.5 * minimum_rate;
309 for (size_t i = 0; i < output_samples; ++i) {
310 double error = fabs(resampled_destination[i] - pure_destination[i]);
311
312 if (pure_source.Frequency(i) < low_frequency_range) {
313 if (error > low_freq_max_error)
314 low_freq_max_error = error;
315 } else if (pure_source.Frequency(i) < high_frequency_range) {
316 if (error > high_freq_max_error)
317 high_freq_max_error = error;
318 }
319 // TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange.
320
321 sum_of_squares += error * error;
322 }
323
324 double rms_error = sqrt(sum_of_squares / output_samples);
325
326 // Convert each error to dbFS.
327 #define DBFS(x) 20 * log10(x)
328 rms_error = DBFS(rms_error);
329 low_freq_max_error = DBFS(low_freq_max_error);
330 high_freq_max_error = DBFS(high_freq_max_error);
331
332 EXPECT_LE(rms_error, rms_error_);
333 EXPECT_LE(low_freq_max_error, low_freq_error_);
334
335 // All conversions currently have a high frequency error around -6 dbFS.
336 static const double kHighFrequencyMaxError = -6.02;
337 EXPECT_LE(high_freq_max_error, kHighFrequencyMaxError);
338 }
339
340 // Almost all conversions have an RMS error of around -14 dbFS.
341 static const double kResamplingRMSError = -14.58;
342
343 // Thresholds chosen arbitrarily based on what each resampling reported during
344 // testing. All thresholds are in dbFS, http://en.wikipedia.org/wiki/DBFS.
345 INSTANTIATE_TEST_SUITE_P(
346 SincResamplerTest,
347 SincResamplerTest,
348 ::testing::Values(
349 // To 44.1kHz
350 std::make_tuple(8000, 44100, kResamplingRMSError, -62.73),
351 std::make_tuple(11025, 44100, kResamplingRMSError, -72.19),
352 std::make_tuple(16000, 44100, kResamplingRMSError, -62.54),
353 std::make_tuple(22050, 44100, kResamplingRMSError, -73.53),
354 std::make_tuple(32000, 44100, kResamplingRMSError, -63.32),
355 std::make_tuple(44100, 44100, kResamplingRMSError, -73.53),
356 std::make_tuple(48000, 44100, -15.01, -64.04),
357 std::make_tuple(96000, 44100, -18.49, -25.51),
358 std::make_tuple(192000, 44100, -20.50, -13.31),
359
360 // To 48kHz
361 std::make_tuple(8000, 48000, kResamplingRMSError, -63.43),
362 std::make_tuple(11025, 48000, kResamplingRMSError, -62.61),
363 std::make_tuple(16000, 48000, kResamplingRMSError, -63.96),
364 std::make_tuple(22050, 48000, kResamplingRMSError, -62.42),
365 std::make_tuple(32000, 48000, kResamplingRMSError, -64.04),
366 std::make_tuple(44100, 48000, kResamplingRMSError, -62.63),
367 std::make_tuple(48000, 48000, kResamplingRMSError, -73.52),
368 std::make_tuple(96000, 48000, -18.40, -28.44),
369 std::make_tuple(192000, 48000, -20.43, -14.11),
370
371 // To 96kHz
372 std::make_tuple(8000, 96000, kResamplingRMSError, -63.19),
373 std::make_tuple(11025, 96000, kResamplingRMSError, -62.61),
374 std::make_tuple(16000, 96000, kResamplingRMSError, -63.39),
375 std::make_tuple(22050, 96000, kResamplingRMSError, -62.42),
376 std::make_tuple(32000, 96000, kResamplingRMSError, -63.95),
377 std::make_tuple(44100, 96000, kResamplingRMSError, -62.63),
378 std::make_tuple(48000, 96000, kResamplingRMSError, -73.52),
379 std::make_tuple(96000, 96000, kResamplingRMSError, -73.52),
380 std::make_tuple(192000, 96000, kResamplingRMSError, -28.41),
381
382 // To 192kHz
383 std::make_tuple(8000, 192000, kResamplingRMSError, -63.10),
384 std::make_tuple(11025, 192000, kResamplingRMSError, -62.61),
385 std::make_tuple(16000, 192000, kResamplingRMSError, -63.14),
386 std::make_tuple(22050, 192000, kResamplingRMSError, -62.42),
387 std::make_tuple(32000, 192000, kResamplingRMSError, -63.38),
388 std::make_tuple(44100, 192000, kResamplingRMSError, -62.63),
389 std::make_tuple(48000, 192000, kResamplingRMSError, -73.44),
390 std::make_tuple(96000, 192000, kResamplingRMSError, -73.52),
391 std::make_tuple(192000, 192000, kResamplingRMSError, -73.52)));
392
393 } // namespace webrtc
394