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