blob: e4b66a701c228c66d87c959c446ba53510bb1648 [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 *
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
28extern "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
38static 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
46bool 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
120void 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
131static 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
162void 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
170int 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))
Tom Tsoud3253432016-03-06 03:08:01 -0800176 return -1;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400177
Tom Tsoud3253432016-03-06 03:08:01 -0800178 if (history_on) {
179 memcpy(&in[-2 * hist_len],
180 history, hist_len * 2 * sizeof(float));
181 } else {
182 memset(&in[-2 * hist_len], 0,
183 hist_len * 2 * sizeof(float));
184 }
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400185
186 /* Generate output from precomputed input/output paths */
187 for (size_t i = 0; i < out_len; i++) {
188 n = in_index[i];
189 path = out_path[i];
190
191 convolve_real(in, in_len,
192 partitions[path], filt_len,
193 &out[2 * i], out_len - i,
194 n, 1, 1, 0);
195 }
196
197 /* Save history */
Tom Tsoud3253432016-03-06 03:08:01 -0800198 if (history_on) {
199 memcpy(history, &in[2 * (in_len - hist_len)],
200 hist_len * 2 * sizeof(float));
201 }
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400202
203 return out_len;
204}
205
206bool Resampler::init(float bw)
207{
208 size_t hist_len = filt_len - 1;
209
210 /* Filterbank filter internals */
211 if (initFilters(bw) < 0)
212 return false;
213
214 /* History buffer */
215 history = new float[2 * hist_len];
216 memset(history, 0, 2 * hist_len * sizeof(float));
217
218 /* Precompute filterbank paths */
219 in_index = new size_t[MAX_OUTPUT_LEN];
220 out_path = new size_t[MAX_OUTPUT_LEN];
221 computePath();
222
223 return true;
224}
225
226size_t Resampler::len()
227{
228 return filt_len;
229}
230
Tom Tsoud3253432016-03-06 03:08:01 -0800231void Resampler::enableHistory(bool on)
232{
233 history_on = on;
234}
235
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400236Resampler::Resampler(size_t p, size_t q, size_t filt_len)
Tom Tsoud3253432016-03-06 03:08:01 -0800237 : in_index(NULL), out_path(NULL), partitions(NULL),
238 history(NULL), history_on(true)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400239{
240 this->p = p;
241 this->q = q;
242 this->filt_len = filt_len;
243}
244
245Resampler::~Resampler()
246{
247 releaseFilters();
248
249 delete history;
250 delete in_index;
251 delete out_path;
252}