blob: 072ec92899ef1dd4fdce3ab9dac08439200d6479 [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
23class Resampler {
24public:
25 /* Constructor for rational sample rate conversion
26 * @param p numerator of resampling ratio
27 * @param q denominator of resampling ratio
28 * @param filt_len length of each polyphase subfilter
29 */
30 Resampler(size_t p, size_t q, size_t filt_len = 16);
31 ~Resampler();
32
33 /* Initilize resampler filterbank.
34 * @param bw bandwidth factor on filter generation (pre-window)
35 * @return false on error, zero otherwise
36 *
37 * Automatic setting is to compute the filter to prevent aliasing with
38 * a Blackman-Harris window. Adjustment is made through a bandwith
39 * factor to shift the cutoff and/or the constituent filter lengths.
40 * Calculation of specific rolloff factors or 3-dB cutoff points is
41 * left as an excersize for the reader.
42 */
43 bool init(float bw = 1.0f);
44
45 /* Rotate "commutator" and drive samples through filterbank
46 * @param in continuous buffer of input complex float values
47 * @param in_len input buffer length
48 * @param out continuous buffer of output complex float values
49 * @param out_len output buffer length
50 * @return number of samples outputted, negative on error
51 *
52 * Input and output vector lengths must of be equal multiples of the
53 * rational conversion rate denominator and numerator respectively.
54 */
55 int rotate(float *in, size_t in_len, float *out, size_t out_len);
56
57 /* Get filter length
58 * @return number of taps in each filter partition
59 */
60 size_t len();
61
Tom Tsoud3253432016-03-06 03:08:01 -080062 /*
63 * Enable/disable history
64 */
65 void enableHistory(bool on);
66
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040067private:
68 size_t p;
69 size_t q;
70 size_t filt_len;
71 size_t *in_index;
72 size_t *out_path;
73
74 float **partitions;
75 float *history;
Tom Tsoud3253432016-03-06 03:08:01 -080076 bool history_on;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040077
78 bool initFilters(float bw);
79 void releaseFilters();
80 void computePath();
81};
82
83#endif /* _RESAMPLER_H_ */