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 | #include <stdlib.h> |
| 23 | #include <math.h> |
| 24 | #include <string.h> |
| 25 | #include <malloc.h> |
| 26 | #include <iostream> |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 27 | #include <algorithm> |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 28 | |
| 29 | #include "Resampler.h" |
| 30 | |
| 31 | extern "C" { |
| 32 | #include "convolve.h" |
| 33 | } |
| 34 | |
| 35 | #ifndef M_PI |
| 36 | #define M_PI 3.14159265358979323846264338327f |
| 37 | #endif |
| 38 | |
| 39 | #define MAX_OUTPUT_LEN 4096 |
| 40 | |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 41 | using namespace std; |
| 42 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 43 | static float sinc(float x) |
| 44 | { |
| 45 | if (x == 0.0) |
| 46 | return 0.9999999999; |
| 47 | |
| 48 | return sin(M_PI * x) / (M_PI * x); |
| 49 | } |
| 50 | |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 51 | void Resampler::initFilters(float bw) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 52 | { |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 53 | float cutoff; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 54 | float sum = 0.0f, scale = 0.0f; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 55 | |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 56 | /* |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 57 | * Allocate partition filters and the temporary prototype filter |
| 58 | * according to numerator of the rational rate. Coefficients are |
| 59 | * real only and must be 16-byte memory aligned for SSE usage. |
| 60 | */ |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 61 | auto proto = vector<float>(p * filt_len); |
| 62 | for (auto &part : partitions) |
| 63 | part = (complex<float> *) memalign(16, filt_len * sizeof(complex<float>)); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 64 | |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 65 | /* |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 66 | * Generate the prototype filter with a Blackman-harris window. |
| 67 | * Scale coefficients with DC filter gain set to unity divided |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 68 | * by the number of filter partitions. |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 69 | */ |
| 70 | float a0 = 0.35875; |
| 71 | float a1 = 0.48829; |
| 72 | float a2 = 0.14128; |
| 73 | float a3 = 0.01168; |
| 74 | |
| 75 | if (p > q) |
| 76 | cutoff = (float) p; |
| 77 | else |
| 78 | cutoff = (float) q; |
| 79 | |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 80 | float midpt = (proto.size() - 1) / 2.0; |
| 81 | for (size_t i = 0; i < proto.size(); i++) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 82 | proto[i] = sinc(((float) i - midpt) / cutoff * bw); |
| 83 | proto[i] *= a0 - |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 84 | a1 * cos(2 * M_PI * i / (proto.size() - 1)) + |
| 85 | a2 * cos(4 * M_PI * i / (proto.size() - 1)) - |
| 86 | a3 * cos(6 * M_PI * i / (proto.size() - 1)); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 87 | sum += proto[i]; |
| 88 | } |
| 89 | scale = p / sum; |
| 90 | |
| 91 | /* Populate filter partitions from the prototype filter */ |
| 92 | for (size_t i = 0; i < filt_len; i++) { |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 93 | for (size_t n = 0; n < p; n++) |
| 94 | partitions[n][i] = complex<float>(proto[i * p + n] * scale); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 97 | /* Store filter taps in reverse */ |
| 98 | for (auto &part : partitions) |
| 99 | reverse(&part[0], &part[filt_len]); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static bool check_vec_len(int in_len, int out_len, int p, int q) |
| 103 | { |
| 104 | if (in_len % q) { |
| 105 | std::cerr << "Invalid input length " << in_len |
| 106 | << " is not multiple of " << q << std::endl; |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | if (out_len % p) { |
| 111 | std::cerr << "Invalid output length " << out_len |
| 112 | << " is not multiple of " << p << std::endl; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if ((in_len / q) != (out_len / p)) { |
| 117 | std::cerr << "Input/output block length mismatch" << std::endl; |
| 118 | std::cerr << "P = " << p << ", Q = " << q << std::endl; |
| 119 | std::cerr << "Input len: " << in_len << std::endl; |
| 120 | std::cerr << "Output len: " << out_len << std::endl; |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | if (out_len > MAX_OUTPUT_LEN) { |
| 125 | std::cerr << "Block length of " << out_len |
| 126 | << " exceeds max of " << MAX_OUTPUT_LEN << std::endl; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 133 | int Resampler::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] | 134 | { |
| 135 | int n, path; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 136 | |
| 137 | if (!check_vec_len(in_len, out_len, p, q)) |
Tom Tsou | d325343 | 2016-03-06 03:08:01 -0800 | [diff] [blame] | 138 | return -1; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 139 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 140 | /* Generate output from precomputed input/output paths */ |
| 141 | for (size_t i = 0; i < out_len; i++) { |
Pau Espin Pedrol | bdb970e | 2019-07-22 12:03:39 +0200 | [diff] [blame] | 142 | n = in_index[i]; |
| 143 | path = out_path[i]; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 144 | |
| 145 | convolve_real(in, in_len, |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 146 | reinterpret_cast<float *>(partitions[path]), |
| 147 | filt_len, &out[2 * i], out_len - i, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 148 | n, 1); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 151 | return out_len; |
| 152 | } |
| 153 | |
| 154 | bool Resampler::init(float bw) |
| 155 | { |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 156 | if (p == 0 || q == 0 || filt_len == 0) return false; |
| 157 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 158 | /* Filterbank filter internals */ |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 159 | initFilters(bw); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 160 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 161 | /* Precompute filterbank paths */ |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 162 | int i = 0; |
| 163 | for (auto &index : in_index) |
| 164 | index = (q * i++) / p; |
| 165 | i = 0; |
| 166 | for (auto &path : out_path) |
| 167 | path = (q * i++) % p; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | size_t Resampler::len() |
| 173 | { |
| 174 | return filt_len; |
| 175 | } |
| 176 | |
| 177 | Resampler::Resampler(size_t p, size_t q, size_t filt_len) |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 178 | : in_index(MAX_OUTPUT_LEN), out_path(MAX_OUTPUT_LEN), partitions(p) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 179 | { |
| 180 | this->p = p; |
| 181 | this->q = q; |
| 182 | this->filt_len = filt_len; |
| 183 | } |
| 184 | |
| 185 | Resampler::~Resampler() |
| 186 | { |
Tom Tsou | d2e5c56 | 2017-06-19 15:23:59 -0700 | [diff] [blame] | 187 | for (auto &part : partitions) |
| 188 | free(part); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 189 | } |