1 // ----------------------------------------------------------------------------
3 // Copyright (C) 2006-2012 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/resampler.h"
27 using namespace ArdourZita;
29 static unsigned int gcd (unsigned int a, unsigned int b)
47 Resampler::Resampler (void)
55 Resampler::~Resampler (void)
61 Resampler::setup (unsigned int fs_inp,
66 if ((hlen < 8) || (hlen > 96)) return 1;
67 return setup (fs_inp, fs_out, nchan, hlen, 1.0 - 2.6 / hlen);
71 Resampler::setup (unsigned int fs_inp,
77 unsigned int g, h, k, n, s;
80 Resampler_table *T = 0;
83 if (fs_inp && fs_out && nchan) {
84 r = (double) fs_out / (double) fs_inp;
85 g = gcd (fs_out, fs_inp);
88 if ((16 * r >= 1) && (n <= 1000)) {
93 h = (unsigned int)(ceil (h / r));
94 k = (unsigned int)(ceil (k / r));
96 T = Resampler_table::create (frel, h, n);
97 B = new float [nchan * (2 * h - 1 + k)];
113 Resampler::clear (void)
115 Resampler_table::destroy (_table);
126 Resampler::inpdist (void) const
128 if (!_table) return 0;
129 return (int)(_table->_hl + 1 - _nread) - (double)_phase / _table->_np;
133 Resampler::inpsize (void) const
135 if (!_table) return 0;
136 return 2 * _table->_hl;
140 Resampler::reset (void)
142 if (!_table) return 1;
153 _nread = 2 * _table->_hl;
160 Resampler::process (void)
162 unsigned int hl, ph, np, dp, in, nr, nz, i, n, c;
165 if (!_table) return 1;
174 n = (2 * hl - nr) * _nchan;
175 p1 = _buff + in * _nchan;
180 if (inp_count == 0) break;
182 for (c = 0; c < _nchan; c++) p2 [c] = inp_data [c];
186 for (c = 0; c < _nchan; c++) p2 [c] = 0;
187 if (nz < 2 * hl) nz++;
195 float *c1 = _table->_ctab + hl * ph;
196 float *c2 = _table->_ctab + hl * (np - ph);
197 for (c = 0; c < _nchan; c++) {
201 for (i = 0; i < hl; i++) {
203 s += *q1 * c1 [i] + *q2 * c2 [i];
206 *out_data++ = s - 1e-20f;
209 for (c = 0; c < _nchan; c++) *out_data++ = 0;
221 n = (2 * hl - nr) * _nchan;
222 memcpy (_buff, p1, n * sizeof (float));