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