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 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: LGPL-2.1+ |
| 6 | * |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 7 | * 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 Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | #include <complex> |
| 27 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 28 | class Resampler { |
| 29 | public: |
| 30 | /* Constructor for rational sample rate conversion |
| 31 | * @param p numerator of resampling ratio |
| 32 | * @param q denominator of resampling ratio |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 33 | * @param filt_len length of each polyphase subfilter |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 34 | */ |
| 35 | Resampler(size_t p, size_t q, size_t filt_len = 16); |
| 36 | ~Resampler(); |
| 37 | |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame^] | 38 | /* Initialize resampler filterbank. |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 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 |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame^] | 43 | * a Blackman-Harris window. Adjustment is made through a bandwidth |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 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 Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 60 | 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] | 61 | |
| 62 | /* Get filter length |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 63 | * @return number of taps in each filter partition |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 64 | */ |
| 65 | size_t len(); |
| 66 | |
| 67 | private: |
| 68 | size_t p; |
| 69 | size_t q; |
| 70 | size_t filt_len; |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 71 | std::vector<size_t> in_index; |
| 72 | std::vector<size_t> out_path; |
| 73 | std::vector<std::complex<float> *> partitions; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 74 | |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 75 | void initFilters(float bw); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | #endif /* _RESAMPLER_H_ */ |