blob: e1962c7f98e6e8aeddfad8c0075ca2ab9793c01b [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.
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040016 */
17
18#ifndef _RESAMPLER_H_
19#define _RESAMPLER_H_
20
Tom Tsoud2e5c562017-06-19 15:23:59 -070021#include <vector>
22#include <complex>
23
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040024class Resampler {
25public:
26 /* Constructor for rational sample rate conversion
27 * @param p numerator of resampling ratio
28 * @param q denominator of resampling ratio
Pau Espin Pedrolbdb970e2019-07-22 12:03:39 +020029 * @param filt_len length of each polyphase subfilter
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040030 */
31 Resampler(size_t p, size_t q, size_t filt_len = 16);
32 ~Resampler();
33
Martin Hauke066fd042019-10-13 19:08:00 +020034 /* Initialize resampler filterbank.
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040035 * @param bw bandwidth factor on filter generation (pre-window)
36 * @return false on error, zero otherwise
37 *
38 * Automatic setting is to compute the filter to prevent aliasing with
Martin Hauke066fd042019-10-13 19:08:00 +020039 * a Blackman-Harris window. Adjustment is made through a bandwidth
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040040 * factor to shift the cutoff and/or the constituent filter lengths.
41 * Calculation of specific rolloff factors or 3-dB cutoff points is
42 * left as an excersize for the reader.
43 */
44 bool init(float bw = 1.0f);
45
46 /* Rotate "commutator" and drive samples through filterbank
47 * @param in continuous buffer of input complex float values
48 * @param in_len input buffer length
49 * @param out continuous buffer of output complex float values
50 * @param out_len output buffer length
51 * @return number of samples outputted, negative on error
52 *
53 * Input and output vector lengths must of be equal multiples of the
54 * rational conversion rate denominator and numerator respectively.
55 */
Tom Tsou28670fb2015-08-21 19:32:58 -070056 int rotate(const float *in, size_t in_len, float *out, size_t out_len);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040057
58 /* Get filter length
Pau Espin Pedrolbdb970e2019-07-22 12:03:39 +020059 * @return number of taps in each filter partition
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040060 */
61 size_t len();
62
63private:
64 size_t p;
65 size_t q;
66 size_t filt_len;
Tom Tsoud2e5c562017-06-19 15:23:59 -070067 std::vector<size_t> in_index;
68 std::vector<size_t> out_path;
69 std::vector<std::complex<float> *> partitions;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040070
Tom Tsoud2e5c562017-06-19 15:23:59 -070071 void initFilters(float bw);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040072};
73
74#endif /* _RESAMPLER_H_ */