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#ifndef WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
12#define WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
13
14#include "webrtc/base/constructormagic.h"
15#include "webrtc/common_audio/resampler/sinc_resampler.h"
16#include "webrtc/system_wrappers/interface/scoped_ptr.h"
17#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21// A thin wrapper over SincResampler to provide a push-based interface as
22// required by WebRTC.
23class PushSincResampler : public SincResamplerCallback {
24 public:
25  // Provide the size of the source and destination blocks in samples. These
26  // must correspond to the same time duration (typically 10 ms) as the sample
27  // ratio is inferred from them.
28  PushSincResampler(int source_frames, int destination_frames);
29  virtual ~PushSincResampler();
30
31  // Perform the resampling. |source_frames| must always equal the
32  // |source_frames| provided at construction. |destination_capacity| must be
33  // at least as large as |destination_frames|. Returns the number of samples
34  // provided in destination (for convenience, since this will always be equal
35  // to |destination_frames|).
36  int Resample(const int16_t* source, int source_frames,
37               int16_t* destination, int destination_capacity);
38  int Resample(const float* source,
39               int source_frames,
40               float* destination,
41               int destination_capacity);
42
43  // Implements SincResamplerCallback.
44  virtual void Run(int frames, float* destination) OVERRIDE;
45
46  SincResampler* get_resampler_for_testing() { return resampler_.get(); }
47  static float AlgorithmicDelaySeconds(int source_rate_hz) {
48    return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
49  }
50
51 private:
52  scoped_ptr<SincResampler> resampler_;
53  scoped_ptr<float[]> float_buffer_;
54  const float* source_ptr_;
55  const int16_t* source_ptr_int_;
56  const int destination_frames_;
57
58  // True on the first call to Resample(), to prime the SincResampler buffer.
59  bool first_pass_;
60
61  // Used to assert we are only requested for as much data as is available.
62  int source_available_;
63
64  DISALLOW_COPY_AND_ASSIGN(PushSincResampler);
65};
66
67}  // namespace webrtc
68
69#endif  // WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
70