andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 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 | |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 16 | #include "webrtc/common_audio/resampler/sinc_resampler.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 17 | #include "webrtc/rtc_base/constructormagic.h" |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 18 | #include "webrtc/typedefs.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // A thin wrapper over SincResampler to provide a push-based interface as |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 23 | // required by WebRTC. SincResampler uses a pull-based interface, and will |
| 24 | // use SincResamplerCallback::Run() to request data upon a call to Resample(). |
| 25 | // These Run() calls will happen on the same thread Resample() is called on. |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 26 | class PushSincResampler : public SincResamplerCallback { |
| 27 | public: |
| 28 | // Provide the size of the source and destination blocks in samples. These |
| 29 | // must correspond to the same time duration (typically 10 ms) as the sample |
| 30 | // ratio is inferred from them. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 31 | PushSincResampler(size_t source_frames, size_t destination_frames); |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 32 | ~PushSincResampler() override; |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 33 | |
andrew@webrtc.org | b86fbaf | 2013-07-25 22:04:30 +0000 | [diff] [blame] | 34 | // Perform the resampling. |source_frames| must always equal the |
| 35 | // |source_frames| provided at construction. |destination_capacity| must be |
| 36 | // at least as large as |destination_frames|. Returns the number of samples |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 37 | // provided in destination (for convenience, since this will always be equal |
andrew@webrtc.org | b86fbaf | 2013-07-25 22:04:30 +0000 | [diff] [blame] | 38 | // to |destination_frames|). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 39 | size_t Resample(const int16_t* source, size_t source_frames, |
| 40 | int16_t* destination, size_t destination_capacity); |
| 41 | size_t Resample(const float* source, |
| 42 | size_t source_frames, |
| 43 | float* destination, |
| 44 | size_t destination_capacity); |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 45 | |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 46 | // Delay due to the filter kernel. Essentially, the time after which an input |
| 47 | // sample will appear in the resampled output. |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 48 | static float AlgorithmicDelaySeconds(int source_rate_hz) { |
| 49 | return 1.f / source_rate_hz * SincResampler::kKernelSize / 2; |
| 50 | } |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 51 | |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 52 | protected: |
| 53 | // Implements SincResamplerCallback. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 54 | void Run(size_t frames, float* destination) override; |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 55 | |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 56 | private: |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 57 | friend class PushSincResamplerTest; |
| 58 | SincResampler* get_resampler_for_testing() { return resampler_.get(); } |
| 59 | |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 60 | std::unique_ptr<SincResampler> resampler_; |
| 61 | std::unique_ptr<float[]> float_buffer_; |
andrew@webrtc.org | d32797f | 2014-03-10 18:51:42 +0000 | [diff] [blame] | 62 | const float* source_ptr_; |
| 63 | const int16_t* source_ptr_int_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 64 | const size_t destination_frames_; |
andrew@webrtc.org | b86fbaf | 2013-07-25 22:04:30 +0000 | [diff] [blame] | 65 | |
| 66 | // True on the first call to Resample(), to prime the SincResampler buffer. |
| 67 | bool first_pass_; |
| 68 | |
| 69 | // Used to assert we are only requested for as much data as is available. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 70 | size_t source_available_; |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 71 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 72 | RTC_DISALLOW_COPY_AND_ASSIGN(PushSincResampler); |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace webrtc |
| 76 | |
| 77 | #endif // WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_ |