Initial backend support for external export encoder
[ardour.git] / libs / zita-resampler / resampler-table.cc
1 // ----------------------------------------------------------------------------
2 //
3 //  Copyright (C) 2006-2012 Fons Adriaensen <fons@linuxaudio.org>
4 //
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.
9 //
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.
14 //
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/>.
17 //
18 // ----------------------------------------------------------------------------
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <math.h>
24
25 #include "zita-resampler/resampler-table.h"
26
27 using namespace ArdourZita;
28
29 Resampler_table  *Resampler_table::_list = 0;
30 Resampler_mutex   Resampler_table::_mutex;
31
32 static double sinc (double x)
33 {
34         x = fabs (x);
35         if (x < 1e-6) return 1.0;
36         x *= M_PI;
37         return sin (x) / x;
38 }
39
40 static double wind (double x)
41 {
42         x = fabs (x);
43         if (x >= 1.0) return 0.0f;
44         x *= M_PI;
45         return 0.384 + 0.500 * cos (x) + 0.116 * cos (2 * x);
46 }
47
48 Resampler_table::Resampler_table (double fr, unsigned int hl, unsigned int np)
49         : _next (0)
50         , _refc (0)
51         , _fr (fr)
52         , _hl (hl)
53         , _np (np)
54 {
55         unsigned int  i, j;
56         double        t;
57         float         *p;
58
59         _ctab = new float [hl * (np + 1)];
60         p = _ctab;
61         for (j = 0; j <= np; j++) {
62                 t = (double) j / (double) np;
63                 for (i = 0; i < hl; i++) {
64                         p [hl - i - 1] = (float)(fr * sinc (t * fr) * wind (t / hl));
65                         t += 1;
66                 }
67                 p += hl;
68         }
69 }
70
71 Resampler_table::~Resampler_table (void)
72 {
73         delete[] _ctab;
74 }
75
76 Resampler_table*
77 Resampler_table::create (double fr, unsigned int hl, unsigned int np)
78 {
79         Resampler_table *P;
80
81         _mutex.lock ();
82         P = _list;
83         while (P) {
84                 if ((fr >= P->_fr * 0.999) && (fr <= P->_fr * 1.001) && (hl == P->_hl) && (np == P->_np)) {
85                         P->_refc++;
86                         _mutex.unlock ();
87                         return P;
88                 }
89                 P = P->_next;
90         }
91         P = new Resampler_table (fr, hl, np);
92         P->_refc = 1;
93         P->_next = _list;
94         _list = P;
95         _mutex.unlock ();
96         return P;
97 }
98
99 void
100 Resampler_table::destroy (Resampler_table *T)
101 {
102         Resampler_table *P, *Q;
103
104         _mutex.lock ();
105         if (T) {
106                 T->_refc--;
107                 if (T->_refc == 0) {
108                         P = _list;
109                         Q = 0;
110                         while (P) {
111                                 if (P == T) {
112                                         if (Q) Q->_next = T->_next;
113                                         else      _list = T->_next;
114                                         break;
115                                 }
116                                 Q = P;
117                                 P = P->_next;
118                         }
119                         delete T;
120                 }
121         }
122         _mutex.unlock ();
123 }