1dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org/*
2dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org *
4dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org */
10dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
11dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org#ifndef WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
12dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org#define WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
13dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
14774b3d38a4a0f1a8ec08972a3c543cb5d607ce13henrike@webrtc.org#include "webrtc/base/constructormagic.h"
15dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org#include "webrtc/common_audio/resampler/sinc_resampler.h"
16dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org#include "webrtc/system_wrappers/interface/scoped_ptr.h"
17dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org#include "webrtc/typedefs.h"
18dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
19dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.orgnamespace webrtc {
20dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
21dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org// A thin wrapper over SincResampler to provide a push-based interface as
22dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org// required by WebRTC.
23dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.orgclass PushSincResampler : public SincResamplerCallback {
24dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org public:
25dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  // Provide the size of the source and destination blocks in samples. These
26dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  // must correspond to the same time duration (typically 10 ms) as the sample
27dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  // ratio is inferred from them.
2805f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  PushSincResampler(int source_frames, int destination_frames);
29dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  virtual ~PushSincResampler();
30dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
3105f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  // Perform the resampling. |source_frames| must always equal the
3205f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  // |source_frames| provided at construction. |destination_capacity| must be
3305f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  // at least as large as |destination_frames|. Returns the number of samples
34dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  // provided in destination (for convenience, since this will always be equal
3505f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  // to |destination_frames|).
3605f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  int Resample(const int16_t* source, int source_frames,
37dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org               int16_t* destination, int destination_capacity);
386a5ccc50f2e8e6558172f3161022fcb323c359cfandrew@webrtc.org  int Resample(const float* source,
396a5ccc50f2e8e6558172f3161022fcb323c359cfandrew@webrtc.org               int source_frames,
406a5ccc50f2e8e6558172f3161022fcb323c359cfandrew@webrtc.org               float* destination,
416a5ccc50f2e8e6558172f3161022fcb323c359cfandrew@webrtc.org               int destination_capacity);
42dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
43dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  // Implements SincResamplerCallback.
44478d711a57e8b4936fdb749c7cb2da8778a46afcpbos@webrtc.org  virtual void Run(int frames, float* destination) OVERRIDE;
4505f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org
4605f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  SincResampler* get_resampler_for_testing() { return resampler_.get(); }
47708ff4d93770d863cdea26fd496ae71414eb8a53andrew@webrtc.org  static float AlgorithmicDelaySeconds(int source_rate_hz) {
48708ff4d93770d863cdea26fd496ae71414eb8a53andrew@webrtc.org    return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
49708ff4d93770d863cdea26fd496ae71414eb8a53andrew@webrtc.org  }
50dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
51dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org private:
52dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  scoped_ptr<SincResampler> resampler_;
536a5ccc50f2e8e6558172f3161022fcb323c359cfandrew@webrtc.org  scoped_ptr<float[]> float_buffer_;
546a5ccc50f2e8e6558172f3161022fcb323c359cfandrew@webrtc.org  const float* source_ptr_;
556a5ccc50f2e8e6558172f3161022fcb323c359cfandrew@webrtc.org  const int16_t* source_ptr_int_;
5605f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  const int destination_frames_;
5705f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org
5805f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  // True on the first call to Resample(), to prime the SincResampler buffer.
5905f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  bool first_pass_;
6005f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org
6105f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  // Used to assert we are only requested for as much data as is available.
6205f213189c0247a6e3ac82e1f39ef1db606cbba3andrew@webrtc.org  int source_available_;
63dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
64dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org  DISALLOW_COPY_AND_ASSIGN(PushSincResampler);
65dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org};
66dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
67dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org}  // namespace webrtc
68dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org
69dea537b43c923bd1176d8a9302fedc8ff1dadf7aandrew@webrtc.org#endif  // WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
70