1 // ----------------------------------------------------------------------------
3 // Copyright (C) 2006-2013 Fons Adriaensen <fons@linuxaudio.org>
4 // Copyright (C) 2017 Robin Gareus <robin@gareus.org>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
27 #include "zita-resampler/vmresampler.h"
29 using namespace ArdourZita;
31 VMResampler::VMResampler (void)
40 VMResampler::~VMResampler (void)
46 VMResampler::setup (unsigned int hlen)
48 if ((hlen < 8) || (hlen > 96)) return 1;
49 return setup (hlen, 1.0 - 2.6 / hlen);
53 VMResampler::setup (unsigned int hlen, double frel)
57 Resampler_table *T = 0;
63 T = Resampler_table::create (frel, h, n);
67 _buff = new float [2 * h - 1 + k];
68 _c1 = new float [2 * h];
69 _c2 = new float [2 * h];
80 VMResampler::clear (void)
82 Resampler_table::destroy (_table);
98 VMResampler::set_phase (double p)
101 _phase = (p - floor (p)) * _table->_np;
105 VMResampler::set_rrfilt (double t)
108 _wstep = (t < 1) ? 1 : 1 - exp (-1 / t);
112 VMResampler::set_rratio (double r)
114 if (!_table) return 0;
115 if (r > 16.0) r = 16.0;
116 if (r < 0.02) r = 0.02;
118 _qstep = _table->_np / r;
123 if (_qstep > 2. * _table->_np * _table->_hl) {
124 _qstep = 2. * _table->_np * _table->_hl;
126 return _table->_np / _qstep;
130 VMResampler::inpdist (void) const
132 if (!_table) return 0;
133 return (int)(_table->_hl + 1 - _nread) - _phase / _table->_np;
137 VMResampler::inpsize (void) const
139 if (!_table) return 0;
140 return 2 * _table->_hl;
144 VMResampler::reset (void)
146 if (!_table) return 1;
154 _nread = 2 * _table->_hl;
156 memset (_buff, 0, sizeof(float) * (_nread + 249));
157 _nread -= _table->_hl - 1;
162 VMResampler::process (void)
164 unsigned int in, nr, n;
168 if (!_table) return 1;
170 const int hl = _table->_hl;
171 const unsigned int np = _table->_np;
179 /* optimized full-cycle no-resampling */
180 if (dp == np && _qstep == np && nr == 1 && inp_count == out_count) {
182 if (out_count >= n) {
183 const unsigned int h1 = hl - 1;
184 const unsigned int head = out_count - h1;
185 const unsigned int tail = out_count - n;
187 memcpy (out_data, &_buff[in + hl], h1 * sizeof (float));
188 memcpy (&out_data[h1], inp_data, head * sizeof (float));
189 memcpy (_buff, &inp_data[tail], n * sizeof (float));
197 unsigned int to_proc = std::min (out_count, _inmax - in);
198 memcpy (&_buff[in + n], inp_data, to_proc * sizeof (float));
199 memcpy (out_data, &_buff[in + hl], to_proc * sizeof (float));
202 out_count -= to_proc;
205 memcpy (_buff, _buff + in, (2 * hl - 1) * sizeof (float));
209 inp_count = out_count;
220 if (inp_count == 0) break;
228 const unsigned int k = (unsigned int) /*floor (ph / np) +*/ hl;
231 const unsigned int k = (unsigned int) ph;
232 const float bb = (float)(ph - k);
233 const float aa = 1.0f - bb;
234 float const* cq1 = _table->_ctab + hl * k;
235 float const* cq2 = _table->_ctab + hl * (np - k);
236 for (int i = 0; i < hl; i++) {
237 _c1 [i] = aa * cq1 [i] + bb * cq1 [i + hl];
238 _c2 [i] = aa * cq2 [i] + bb * cq2 [i - hl];
242 for (int i = 0; i < hl; i++) {
243 a += p1[i] * _c1 [i] + p2[-i-1] * _c2 [i];
245 *out_data++ = a - 1e-25f;
249 const double dd = _qstep - dp;
250 if (fabs (dd) < 1e-12) {
258 nr = (unsigned int) floor (ph / np);
264 memcpy (_buff, p1, n * sizeof (float));