• 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 #ifndef COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
12 #define COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <memory>
18 
19 #include "common_audio/resampler/sinc_resampler.h"
20 #include "rtc_base/constructor_magic.h"
21 
22 namespace webrtc {
23 
24 // A thin wrapper over SincResampler to provide a push-based interface as
25 // required by WebRTC. SincResampler uses a pull-based interface, and will
26 // use SincResamplerCallback::Run() to request data upon a call to Resample().
27 // These Run() calls will happen on the same thread Resample() is called on.
28 class PushSincResampler : public SincResamplerCallback {
29  public:
30   // Provide the size of the source and destination blocks in samples. These
31   // must correspond to the same time duration (typically 10 ms) as the sample
32   // ratio is inferred from them.
33   PushSincResampler(size_t source_frames, size_t destination_frames);
34   ~PushSincResampler() override;
35 
36   // Perform the resampling. |source_frames| must always equal the
37   // |source_frames| provided at construction. |destination_capacity| must be
38   // at least as large as |destination_frames|. Returns the number of samples
39   // provided in destination (for convenience, since this will always be equal
40   // to |destination_frames|).
41   size_t Resample(const int16_t* source,
42                   size_t source_frames,
43                   int16_t* destination,
44                   size_t destination_capacity);
45   size_t Resample(const float* source,
46                   size_t source_frames,
47                   float* destination,
48                   size_t destination_capacity);
49 
50   // Delay due to the filter kernel. Essentially, the time after which an input
51   // sample will appear in the resampled output.
AlgorithmicDelaySeconds(int source_rate_hz)52   static float AlgorithmicDelaySeconds(int source_rate_hz) {
53     return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
54   }
55 
56  protected:
57   // Implements SincResamplerCallback.
58   void Run(size_t frames, float* destination) override;
59 
60  private:
61   friend class PushSincResamplerTest;
get_resampler_for_testing()62   SincResampler* get_resampler_for_testing() { return resampler_.get(); }
63 
64   std::unique_ptr<SincResampler> resampler_;
65   std::unique_ptr<float[]> float_buffer_;
66   const float* source_ptr_;
67   const int16_t* source_ptr_int_;
68   const size_t destination_frames_;
69 
70   // True on the first call to Resample(), to prime the SincResampler buffer.
71   bool first_pass_;
72 
73   // Used to assert we are only requested for as much data as is available.
74   size_t source_available_;
75 
76   RTC_DISALLOW_COPY_AND_ASSIGN(PushSincResampler);
77 };
78 
79 }  // namespace webrtc
80 
81 #endif  // COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_
82