Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 1 | /* |
| 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 Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | #include <complex> |
| 25 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 26 | class Resampler { |
| 27 | public: |
| 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 Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 58 | int rotate(const float *in, size_t in_len, float *out, size_t out_len); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 59 | |
| 60 | /* Get filter length |
| 61 | * @return number of taps in each filter partition |
| 62 | */ |
| 63 | size_t len(); |
| 64 | |
| 65 | private: |
| 66 | size_t p; |
| 67 | size_t q; |
| 68 | size_t filt_len; |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 69 | std::vector<size_t> in_index; |
| 70 | std::vector<size_t> out_path; |
| 71 | std::vector<std::complex<float> *> partitions; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 72 | |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 73 | void initFilters(float bw); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | #endif /* _RESAMPLER_H_ */ |