blob: 139b857dc2b87c7e03f047eb08b5ded5ebd7b72a [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 *
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02005 * SPDX-License-Identifier: LGPL-2.1+
6 *
Thomas Tsou03e6ecf2013-08-20 20:54:54 -04007 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _RESAMPLER_H_
23#define _RESAMPLER_H_
24
Tom Tsoud2e5c562017-06-19 15:23:59 -070025#include <vector>
26#include <complex>
27
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040028class Resampler {
29public:
30 /* Constructor for rational sample rate conversion
31 * @param p numerator of resampling ratio
32 * @param q denominator of resampling ratio
Pau Espin Pedrolbdb970e2019-07-22 12:03:39 +020033 * @param filt_len length of each polyphase subfilter
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040034 */
35 Resampler(size_t p, size_t q, size_t filt_len = 16);
36 ~Resampler();
37
38 /* Initilize resampler filterbank.
39 * @param bw bandwidth factor on filter generation (pre-window)
40 * @return false on error, zero otherwise
41 *
42 * Automatic setting is to compute the filter to prevent aliasing with
43 * a Blackman-Harris window. Adjustment is made through a bandwith
44 * factor to shift the cutoff and/or the constituent filter lengths.
45 * Calculation of specific rolloff factors or 3-dB cutoff points is
46 * left as an excersize for the reader.
47 */
48 bool init(float bw = 1.0f);
49
50 /* Rotate "commutator" and drive samples through filterbank
51 * @param in continuous buffer of input complex float values
52 * @param in_len input buffer length
53 * @param out continuous buffer of output complex float values
54 * @param out_len output buffer length
55 * @return number of samples outputted, negative on error
56 *
57 * Input and output vector lengths must of be equal multiples of the
58 * rational conversion rate denominator and numerator respectively.
59 */
Tom Tsou28670fb2015-08-21 19:32:58 -070060 int rotate(const float *in, size_t in_len, float *out, size_t out_len);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040061
62 /* Get filter length
Pau Espin Pedrolbdb970e2019-07-22 12:03:39 +020063 * @return number of taps in each filter partition
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040064 */
65 size_t len();
66
67private:
68 size_t p;
69 size_t q;
70 size_t filt_len;
Tom Tsoud2e5c562017-06-19 15:23:59 -070071 std::vector<size_t> in_index;
72 std::vector<size_t> out_path;
73 std::vector<std::complex<float> *> partitions;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040074
Tom Tsoud2e5c562017-06-19 15:23:59 -070075 void initFilters(float bw);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040076};
77
78#endif /* _RESAMPLER_H_ */