blob: 7af85c8c4ce41ef027d2c0bf5dc93daca95486f0 [file] [log] [blame]
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +00001/*
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
kwibergc2b785d2016-02-24 05:22:32 -080014#include <memory>
15
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +000016#include "webrtc/common_audio/resampler/sinc_resampler.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/constructormagic.h"
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +000018#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
22// A thin wrapper over SincResampler to provide a push-based interface as
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000023// 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.org8fc05fe2013-04-26 14:56:51 +000026class 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 Kastingdce40cf2015-08-24 14:52:23 -070031 PushSincResampler(size_t source_frames, size_t destination_frames);
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000032 ~PushSincResampler() override;
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +000033
andrew@webrtc.orgb86fbaf2013-07-25 22:04:30 +000034 // 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.org8fc05fe2013-04-26 14:56:51 +000037 // provided in destination (for convenience, since this will always be equal
andrew@webrtc.orgb86fbaf2013-07-25 22:04:30 +000038 // to |destination_frames|).
Peter Kastingdce40cf2015-08-24 14:52:23 -070039 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.org8fc05fe2013-04-26 14:56:51 +000045
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000046 // Delay due to the filter kernel. Essentially, the time after which an input
47 // sample will appear in the resampled output.
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000048 static float AlgorithmicDelaySeconds(int source_rate_hz) {
49 return 1.f / source_rate_hz * SincResampler::kKernelSize / 2;
50 }
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +000051
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000052 protected:
53 // Implements SincResamplerCallback.
Peter Kastingdce40cf2015-08-24 14:52:23 -070054 void Run(size_t frames, float* destination) override;
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000055
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +000056 private:
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000057 friend class PushSincResamplerTest;
58 SincResampler* get_resampler_for_testing() { return resampler_.get(); }
59
kwibergc2b785d2016-02-24 05:22:32 -080060 std::unique_ptr<SincResampler> resampler_;
61 std::unique_ptr<float[]> float_buffer_;
andrew@webrtc.orgd32797f2014-03-10 18:51:42 +000062 const float* source_ptr_;
63 const int16_t* source_ptr_int_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070064 const size_t destination_frames_;
andrew@webrtc.orgb86fbaf2013-07-25 22:04:30 +000065
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 Kastingdce40cf2015-08-24 14:52:23 -070070 size_t source_available_;
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +000071
henrikg3c089d72015-09-16 05:37:44 -070072 RTC_DISALLOW_COPY_AND_ASSIGN(PushSincResampler);
andrew@webrtc.org8fc05fe2013-04-26 14:56:51 +000073};
74
75} // namespace webrtc
76
77#endif // WEBRTC_COMMON_AUDIO_RESAMPLER_PUSH_SINC_RESAMPLER_H_