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) { |
| 64 | free(proto); |
| 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 | |
| 170 | int Resampler::rotate(float *in, size_t in_len, float *out, size_t out_len) |
| 171 | { |
| 172 | int n, path; |
| 173 | int hist_len = filt_len - 1; |
| 174 | |
| 175 | if (!check_vec_len(in_len, out_len, p, q)) |
| 176 | return -1; |
| 177 | |
| 178 | /* Insert history */ |
| 179 | memcpy(&in[-2 * hist_len], history, hist_len * 2 * sizeof(float)); |
| 180 | |
| 181 | /* Generate output from precomputed input/output paths */ |
| 182 | for (size_t i = 0; i < out_len; i++) { |
| 183 | n = in_index[i]; |
| 184 | path = out_path[i]; |
| 185 | |
| 186 | convolve_real(in, in_len, |
| 187 | partitions[path], filt_len, |
| 188 | &out[2 * i], out_len - i, |
| 189 | n, 1, 1, 0); |
| 190 | } |
| 191 | |
| 192 | /* Save history */ |
| 193 | memcpy(history, &in[2 * (in_len - hist_len)], |
| 194 | hist_len * 2 * sizeof(float)); |
| 195 | |
| 196 | return out_len; |
| 197 | } |
| 198 | |
| 199 | bool Resampler::init(float bw) |
| 200 | { |
| 201 | size_t hist_len = filt_len - 1; |
| 202 | |
| 203 | /* Filterbank filter internals */ |
| 204 | if (initFilters(bw) < 0) |
| 205 | return false; |
| 206 | |
| 207 | /* History buffer */ |
| 208 | history = new float[2 * hist_len]; |
| 209 | memset(history, 0, 2 * hist_len * sizeof(float)); |
| 210 | |
| 211 | /* Precompute filterbank paths */ |
| 212 | in_index = new size_t[MAX_OUTPUT_LEN]; |
| 213 | out_path = new size_t[MAX_OUTPUT_LEN]; |
| 214 | computePath(); |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | size_t Resampler::len() |
| 220 | { |
| 221 | return filt_len; |
| 222 | } |
| 223 | |
| 224 | Resampler::Resampler(size_t p, size_t q, size_t filt_len) |
| 225 | : in_index(NULL), out_path(NULL), partitions(NULL), history(NULL) |
| 226 | { |
| 227 | this->p = p; |
| 228 | this->q = q; |
| 229 | this->filt_len = filt_len; |
| 230 | } |
| 231 | |
| 232 | Resampler::~Resampler() |
| 233 | { |
| 234 | releaseFilters(); |
| 235 | |
| 236 | delete history; |
| 237 | delete in_index; |
| 238 | delete out_path; |
| 239 | } |