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