1 // ----------------------------------------------------------------------------
3 // Copyright (C) 2006-2013 Fons Adriaensen <fons@linuxaudio.org>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program 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
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // ----------------------------------------------------------------------------
25 #include "zita-resampler/vresampler.h"
27 using namespace ArdourZita;
29 VResampler::VResampler (void)
39 VResampler::~VResampler (void)
45 VResampler::setup (double ratio,
49 if ((hlen < 8) || (hlen > 96) || (16 * ratio < 1) || (ratio > 256)) return 1;
50 return setup (ratio, nchan, hlen, 1.0 - 2.6 / hlen);
54 VResampler::setup (double ratio,
61 Resampler_table *T = 0;
63 if (! nchan) return 1;
70 h = (unsigned int)(ceil (h / ratio));
71 k = (unsigned int)(ceil (k / ratio));
73 T = Resampler_table::create (frel, h, n);
77 _buff = new float [nchan * (2 * h - 1 + k)];
78 _c1 = new float [2 * h];
79 _c2 = new float [2 * h];
92 VResampler::clear (void)
94 Resampler_table::destroy (_table);
111 VResampler::set_phase (double p)
114 _phase = (p - floor (p)) * _table->_np;
118 VResampler::set_rrfilt (double t)
121 _wstep = (t < 1) ? 1 : 1 - exp (-1 / t);
125 VResampler::set_rratio (double r)
128 if (r > 16.0) r = 16.0;
129 if (r < 0.95) r = 0.95;
130 _qstep = _table->_np / (_ratio * r);
134 VResampler::inpdist (void) const
136 if (!_table) return 0;
137 return (int)(_table->_hl + 1 - _nread) - _phase / _table->_np;
141 VResampler::inpsize (void) const
143 if (!_table) return 0;
144 return 2 * _table->_hl;
148 VResampler::reset (void)
150 if (!_table) return 1;
158 _nread = 2 * _table->_hl;
164 VResampler::process (void)
166 unsigned int k, np, in, nr, n, c;
169 float a, b, *p1, *p2, *q1, *q2;
171 if (!_table) return 1;
180 n = (2 * hl - nr) * _nchan;
181 p1 = _buff + in * _nchan;
186 if (inp_count == 0) break;
188 for (c = 0; c < _nchan; c++) p2 [c] = inp_data [c];
192 for (c = 0; c < _nchan; c++) p2 [c] = 0;
193 if (nz < 2 * hl) nz++;
201 k = (unsigned int) ph;
204 q1 = _table->_ctab + hl * k;
205 q2 = _table->_ctab + hl * (np - k);
206 for (i = 0; i < hl; i++) {
207 _c1 [i] = a * q1 [i] + b * q1 [i + hl];
208 _c2 [i] = a * q2 [i] + b * q2 [i - hl];
210 for (c = 0; c < _nchan; c++) {
214 for (i = 0; i < hl; i++) {
216 a += *q1 * _c1 [i] + *q2 * _c2 [i];
219 *out_data++ = a - 1e-25f;
222 for (c = 0; c < _nchan; c++) *out_data++ = 0;
228 if (fabs (dd) < 1e-30) dp = _qstep;
229 else dp += _wstep * dd;
232 nr = (unsigned int) floor( ph / np);
237 n = (2 * hl - nr) * _nchan;
238 memcpy (_buff, p1, n * sizeof (float));