blob: caffc08cba30188aa424fc5c97211998502b2631 [file] [log] [blame]
Thomas Tsou03e6ecf2013-08-20 20:54:54 -04001/*
2 * Rational Sample Rate Conversion
3 * Copyright (C) 2012, 2013 Thomas Tsou <tom@tsou.cc>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef _RESAMPLER_H_
21#define _RESAMPLER_H_
22
Tom Tsoud2e5c562017-06-19 15:23:59 -070023#include <vector>
24#include <complex>
25
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040026class Resampler {
27public:
28 /* Constructor for rational sample rate conversion
29 * @param p numerator of resampling ratio
30 * @param q denominator of resampling ratio
31 * @param filt_len length of each polyphase subfilter
32 */
33 Resampler(size_t p, size_t q, size_t filt_len = 16);
34 ~Resampler();
35
36 /* Initilize resampler filterbank.
37 * @param bw bandwidth factor on filter generation (pre-window)
38 * @return false on error, zero otherwise
39 *
40 * Automatic setting is to compute the filter to prevent aliasing with
41 * a Blackman-Harris window. Adjustment is made through a bandwith
42 * factor to shift the cutoff and/or the constituent filter lengths.
43 * Calculation of specific rolloff factors or 3-dB cutoff points is
44 * left as an excersize for the reader.
45 */
46 bool init(float bw = 1.0f);
47
48 /* Rotate "commutator" and drive samples through filterbank
49 * @param in continuous buffer of input complex float values
50 * @param in_len input buffer length
51 * @param out continuous buffer of output complex float values
52 * @param out_len output buffer length
53 * @return number of samples outputted, negative on error
54 *
55 * Input and output vector lengths must of be equal multiples of the
56 * rational conversion rate denominator and numerator respectively.
57 */
Tom Tsou28670fb2015-08-21 19:32:58 -070058 int rotate(const float *in, size_t in_len, float *out, size_t out_len);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040059
60 /* Get filter length
61 * @return number of taps in each filter partition
62 */
63 size_t len();
64
65private:
66 size_t p;
67 size_t q;
68 size_t filt_len;
Tom Tsoud2e5c562017-06-19 15:23:59 -070069 std::vector<size_t> in_index;
70 std::vector<size_t> out_path;
71 std::vector<std::complex<float> *> partitions;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040072
Tom Tsoud2e5c562017-06-19 15:23:59 -070073 void initFilters(float bw);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040074};
75
76#endif /* _RESAMPLER_H_ */