• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "common_audio/resampler/push_sinc_resampler.h"
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <cstring>
16 #include <memory>
17 
18 #include "common_audio/include/audio_util.h"
19 #include "common_audio/resampler/sinusoidal_linear_chirp_source.h"
20 #include "rtc_base/time_utils.h"
21 #include "test/gmock.h"
22 #include "test/gtest.h"
23 
24 namespace webrtc {
25 namespace {
26 
27 // Almost all conversions have an RMS error of around -14 dbFS.
28 const double kResamplingRMSError = -14.42;
29 
30 // Used to convert errors to dbFS.
31 template <typename T>
DBFS(T x)32 T DBFS(T x) {
33   return 20 * std::log10(x);
34 }
35 
36 }  // namespace
37 
38 class PushSincResamplerTest : public ::testing::TestWithParam<
39                                   ::testing::tuple<int, int, double, double>> {
40  public:
PushSincResamplerTest()41   PushSincResamplerTest()
42       : input_rate_(::testing::get<0>(GetParam())),
43         output_rate_(::testing::get<1>(GetParam())),
44         rms_error_(::testing::get<2>(GetParam())),
45         low_freq_error_(::testing::get<3>(GetParam())) {}
46 
~PushSincResamplerTest()47   ~PushSincResamplerTest() override {}
48 
49  protected:
50   void ResampleBenchmarkTest(bool int_format);
51   void ResampleTest(bool int_format);
52 
53   int input_rate_;
54   int output_rate_;
55   double rms_error_;
56   double low_freq_error_;
57 };
58 
59 class ZeroSource : public SincResamplerCallback {
60  public:
Run(size_t frames,float * destination)61   void Run(size_t frames, float* destination) override {
62     std::memset(destination, 0, sizeof(float) * frames);
63   }
64 };
65 
ResampleBenchmarkTest(bool int_format)66 void PushSincResamplerTest::ResampleBenchmarkTest(bool int_format) {
67   const size_t input_samples = static_cast<size_t>(input_rate_ / 100);
68   const size_t output_samples = static_cast<size_t>(output_rate_ / 100);
69   const int kResampleIterations = 500000;
70 
71   // Source for data to be resampled.
72   ZeroSource resampler_source;
73 
74   std::unique_ptr<float[]> resampled_destination(new float[output_samples]);
75   std::unique_ptr<float[]> source(new float[input_samples]);
76   std::unique_ptr<int16_t[]> source_int(new int16_t[input_samples]);
77   std::unique_ptr<int16_t[]> destination_int(new int16_t[output_samples]);
78 
79   resampler_source.Run(input_samples, source.get());
80   for (size_t i = 0; i < input_samples; ++i) {
81     source_int[i] = static_cast<int16_t>(floor(32767 * source[i] + 0.5));
82   }
83 
84   printf("Benchmarking %d iterations of %d Hz -> %d Hz:\n", kResampleIterations,
85          input_rate_, output_rate_);
86   const double io_ratio = input_rate_ / static_cast<double>(output_rate_);
87   SincResampler sinc_resampler(io_ratio, SincResampler::kDefaultRequestSize,
88                                &resampler_source);
89   int64_t start = rtc::TimeNanos();
90   for (int i = 0; i < kResampleIterations; ++i) {
91     sinc_resampler.Resample(output_samples, resampled_destination.get());
92   }
93   double total_time_sinc_us =
94       (rtc::TimeNanos() - start) / rtc::kNumNanosecsPerMicrosec;
95   printf("SincResampler took %.2f us per frame.\n",
96          total_time_sinc_us / kResampleIterations);
97 
98   PushSincResampler resampler(input_samples, output_samples);
99   start = rtc::TimeNanos();
100   if (int_format) {
101     for (int i = 0; i < kResampleIterations; ++i) {
102       EXPECT_EQ(output_samples,
103                 resampler.Resample(source_int.get(), input_samples,
104                                    destination_int.get(), output_samples));
105     }
106   } else {
107     for (int i = 0; i < kResampleIterations; ++i) {
108       EXPECT_EQ(output_samples, resampler.Resample(source.get(), input_samples,
109                                                    resampled_destination.get(),
110                                                    output_samples));
111     }
112   }
113   double total_time_us =
114       (rtc::TimeNanos() - start) / rtc::kNumNanosecsPerMicrosec;
115   printf(
116       "PushSincResampler took %.2f us per frame; which is a %.1f%% overhead "
117       "on SincResampler.\n\n",
118       total_time_us / kResampleIterations,
119       (total_time_us - total_time_sinc_us) / total_time_sinc_us * 100);
120 }
121 
122 // Disabled because it takes too long to run routinely. Use for performance
123 // benchmarking when needed.
TEST_P(PushSincResamplerTest,DISABLED_BenchmarkInt)124 TEST_P(PushSincResamplerTest, DISABLED_BenchmarkInt) {
125   ResampleBenchmarkTest(true);
126 }
127 
TEST_P(PushSincResamplerTest,DISABLED_BenchmarkFloat)128 TEST_P(PushSincResamplerTest, DISABLED_BenchmarkFloat) {
129   ResampleBenchmarkTest(false);
130 }
131 
132 // Tests resampling using a given input and output sample rate.
ResampleTest(bool int_format)133 void PushSincResamplerTest::ResampleTest(bool int_format) {
134   // Make comparisons using one second of data.
135   static const double kTestDurationSecs = 1;
136   // 10 ms blocks.
137   const size_t kNumBlocks = static_cast<size_t>(kTestDurationSecs * 100);
138   const size_t input_block_size = static_cast<size_t>(input_rate_ / 100);
139   const size_t output_block_size = static_cast<size_t>(output_rate_ / 100);
140   const size_t input_samples =
141       static_cast<size_t>(kTestDurationSecs * input_rate_);
142   const size_t output_samples =
143       static_cast<size_t>(kTestDurationSecs * output_rate_);
144 
145   // Nyquist frequency for the input sampling rate.
146   const double input_nyquist_freq = 0.5 * input_rate_;
147 
148   // Source for data to be resampled.
149   SinusoidalLinearChirpSource resampler_source(input_rate_, input_samples,
150                                                input_nyquist_freq, 0);
151 
152   PushSincResampler resampler(input_block_size, output_block_size);
153 
154   // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to
155   // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes.
156   std::unique_ptr<float[]> resampled_destination(new float[output_samples]);
157   std::unique_ptr<float[]> pure_destination(new float[output_samples]);
158   std::unique_ptr<float[]> source(new float[input_samples]);
159   std::unique_ptr<int16_t[]> source_int(new int16_t[input_block_size]);
160   std::unique_ptr<int16_t[]> destination_int(new int16_t[output_block_size]);
161 
162   // The sinc resampler has an implicit delay of approximately half the kernel
163   // size at the input sample rate. By moving to a push model, this delay
164   // becomes explicit and is managed by zero-stuffing in PushSincResampler. We
165   // deal with it in the test by delaying the "pure" source to match. It must be
166   // checked before the first call to Resample(), because ChunkSize() will
167   // change afterwards.
168   const size_t output_delay_samples =
169       output_block_size - resampler.get_resampler_for_testing()->ChunkSize();
170 
171   // Generate resampled signal.
172   // With the PushSincResampler, we produce the signal block-by-10ms-block
173   // rather than in a single pass, to exercise how it will be used in WebRTC.
174   resampler_source.Run(input_samples, source.get());
175   if (int_format) {
176     for (size_t i = 0; i < kNumBlocks; ++i) {
177       FloatToS16(&source[i * input_block_size], input_block_size,
178                  source_int.get());
179       EXPECT_EQ(output_block_size,
180                 resampler.Resample(source_int.get(), input_block_size,
181                                    destination_int.get(), output_block_size));
182       S16ToFloat(destination_int.get(), output_block_size,
183                  &resampled_destination[i * output_block_size]);
184     }
185   } else {
186     for (size_t i = 0; i < kNumBlocks; ++i) {
187       EXPECT_EQ(
188           output_block_size,
189           resampler.Resample(&source[i * input_block_size], input_block_size,
190                              &resampled_destination[i * output_block_size],
191                              output_block_size));
192     }
193   }
194 
195   // Generate pure signal.
196   SinusoidalLinearChirpSource pure_source(
197       output_rate_, output_samples, input_nyquist_freq, output_delay_samples);
198   pure_source.Run(output_samples, pure_destination.get());
199 
200   // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which
201   // we refer to as low and high.
202   static const double kLowFrequencyNyquistRange = 0.7;
203   static const double kHighFrequencyNyquistRange = 0.9;
204 
205   // Calculate Root-Mean-Square-Error and maximum error for the resampling.
206   double sum_of_squares = 0;
207   double low_freq_max_error = 0;
208   double high_freq_max_error = 0;
209   int minimum_rate = std::min(input_rate_, output_rate_);
210   double low_frequency_range = kLowFrequencyNyquistRange * 0.5 * minimum_rate;
211   double high_frequency_range = kHighFrequencyNyquistRange * 0.5 * minimum_rate;
212 
213   for (size_t i = 0; i < output_samples; ++i) {
214     double error = fabs(resampled_destination[i] - pure_destination[i]);
215 
216     if (pure_source.Frequency(i) < low_frequency_range) {
217       if (error > low_freq_max_error)
218         low_freq_max_error = error;
219     } else if (pure_source.Frequency(i) < high_frequency_range) {
220       if (error > high_freq_max_error)
221         high_freq_max_error = error;
222     }
223     // TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange.
224 
225     sum_of_squares += error * error;
226   }
227 
228   double rms_error = sqrt(sum_of_squares / output_samples);
229 
230   rms_error = DBFS(rms_error);
231   // In order to keep the thresholds in this test identical to SincResamplerTest
232   // we must account for the quantization error introduced by truncating from
233   // float to int. This happens twice (once at input and once at output) and we
234   // allow for the maximum possible error (1 / 32767) for each step.
235   //
236   // The quantization error is insignificant in the RMS calculation so does not
237   // need to be accounted for there.
238   low_freq_max_error = DBFS(low_freq_max_error - 2.0 / 32767);
239   high_freq_max_error = DBFS(high_freq_max_error - 2.0 / 32767);
240 
241   EXPECT_LE(rms_error, rms_error_);
242   EXPECT_LE(low_freq_max_error, low_freq_error_);
243 
244   // All conversions currently have a high frequency error around -6 dbFS.
245   static const double kHighFrequencyMaxError = -6.02;
246   EXPECT_LE(high_freq_max_error, kHighFrequencyMaxError);
247 }
248 
TEST_P(PushSincResamplerTest,ResampleInt)249 TEST_P(PushSincResamplerTest, ResampleInt) {
250   ResampleTest(true);
251 }
252 
TEST_P(PushSincResamplerTest,ResampleFloat)253 TEST_P(PushSincResamplerTest, ResampleFloat) {
254   ResampleTest(false);
255 }
256 
257 // Thresholds chosen arbitrarily based on what each resampling reported during
258 // testing.  All thresholds are in dbFS, http://en.wikipedia.org/wiki/DBFS.
259 INSTANTIATE_TEST_SUITE_P(
260     PushSincResamplerTest,
261     PushSincResamplerTest,
262     ::testing::Values(
263         // First run through the rates tested in SincResamplerTest. The
264         // thresholds are identical.
265         //
266         // We don't test rates which fail to provide an integer number of
267         // samples in a 10 ms block (22050 and 11025 Hz). WebRTC doesn't support
268         // these rates in any case (for the same reason).
269 
270         // To 44.1kHz
271         ::testing::make_tuple(8000, 44100, kResamplingRMSError, -62.73),
272         ::testing::make_tuple(16000, 44100, kResamplingRMSError, -62.54),
273         ::testing::make_tuple(32000, 44100, kResamplingRMSError, -63.32),
274         ::testing::make_tuple(44100, 44100, kResamplingRMSError, -73.53),
275         ::testing::make_tuple(48000, 44100, -15.01, -64.04),
276         ::testing::make_tuple(96000, 44100, -18.49, -25.51),
277         ::testing::make_tuple(192000, 44100, -20.50, -13.31),
278 
279         // To 48kHz
280         ::testing::make_tuple(8000, 48000, kResamplingRMSError, -63.43),
281         ::testing::make_tuple(16000, 48000, kResamplingRMSError, -63.96),
282         ::testing::make_tuple(32000, 48000, kResamplingRMSError, -64.04),
283         ::testing::make_tuple(44100, 48000, kResamplingRMSError, -62.63),
284         ::testing::make_tuple(48000, 48000, kResamplingRMSError, -73.52),
285         ::testing::make_tuple(96000, 48000, -18.40, -28.44),
286         ::testing::make_tuple(192000, 48000, -20.43, -14.11),
287 
288         // To 96kHz
289         ::testing::make_tuple(8000, 96000, kResamplingRMSError, -63.19),
290         ::testing::make_tuple(16000, 96000, kResamplingRMSError, -63.39),
291         ::testing::make_tuple(32000, 96000, kResamplingRMSError, -63.95),
292         ::testing::make_tuple(44100, 96000, kResamplingRMSError, -62.63),
293         ::testing::make_tuple(48000, 96000, kResamplingRMSError, -73.52),
294         ::testing::make_tuple(96000, 96000, kResamplingRMSError, -73.52),
295         ::testing::make_tuple(192000, 96000, kResamplingRMSError, -28.41),
296 
297         // To 192kHz
298         ::testing::make_tuple(8000, 192000, kResamplingRMSError, -63.10),
299         ::testing::make_tuple(16000, 192000, kResamplingRMSError, -63.14),
300         ::testing::make_tuple(32000, 192000, kResamplingRMSError, -63.38),
301         ::testing::make_tuple(44100, 192000, kResamplingRMSError, -62.63),
302         ::testing::make_tuple(48000, 192000, kResamplingRMSError, -73.44),
303         ::testing::make_tuple(96000, 192000, kResamplingRMSError, -73.52),
304         ::testing::make_tuple(192000, 192000, kResamplingRMSError, -73.52),
305 
306         // Next run through some additional cases interesting for WebRTC.
307         // We skip some extreme downsampled cases (192 -> {8, 16}, 96 -> 8)
308         // because they violate |kHighFrequencyMaxError|, which is not
309         // unexpected. It's very unlikely that we'll see these conversions in
310         // practice anyway.
311 
312         // To 8 kHz
313         ::testing::make_tuple(8000, 8000, kResamplingRMSError, -75.50),
314         ::testing::make_tuple(16000, 8000, -18.56, -28.79),
315         ::testing::make_tuple(32000, 8000, -20.36, -14.13),
316         ::testing::make_tuple(44100, 8000, -21.00, -11.39),
317         ::testing::make_tuple(48000, 8000, -20.96, -11.04),
318 
319         // To 16 kHz
320         ::testing::make_tuple(8000, 16000, kResamplingRMSError, -70.30),
321         ::testing::make_tuple(16000, 16000, kResamplingRMSError, -75.51),
322         ::testing::make_tuple(32000, 16000, -18.48, -28.59),
323         ::testing::make_tuple(44100, 16000, -19.30, -19.67),
324         ::testing::make_tuple(48000, 16000, -19.81, -18.11),
325         ::testing::make_tuple(96000, 16000, -20.95, -10.9596),
326 
327         // To 32 kHz
328         ::testing::make_tuple(8000, 32000, kResamplingRMSError, -70.30),
329         ::testing::make_tuple(16000, 32000, kResamplingRMSError, -75.51),
330         ::testing::make_tuple(32000, 32000, kResamplingRMSError, -75.51),
331         ::testing::make_tuple(44100, 32000, -16.44, -51.0349),
332         ::testing::make_tuple(48000, 32000, -16.90, -43.9967),
333         ::testing::make_tuple(96000, 32000, -19.61, -18.04),
334         ::testing::make_tuple(192000, 32000, -21.02, -10.94)));
335 
336 }  // namespace webrtc
337