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