1 /*
2   Copyright (C) 2004-2009 Fons Adriaensen <fons@kokkinizita.net>
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8 
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 
20 #include <string.h>
21 #include "filters.h"
22 
23 
24 extern float exp2ap (float x);
25 
26 
setport(unsigned long port,LADSPA_Data * data)27 void Ladspa_Paramfilt::setport (unsigned long port, LADSPA_Data *data)
28 {
29   _port [port] = data;
30 }
31 
32 
active(bool act)33 void Ladspa_Paramfilt::active (bool act)
34 {
35   int j;
36 
37   if (! act) return;
38   _fade = 0;
39   _gain = 1;
40   for (j = 0; j < NSECT; j++)
41     _sect[j].init();
42 
43 }
44 
45 
runproc(unsigned long len,bool add)46 void Ladspa_Paramfilt::runproc (unsigned long len, bool add)
47 {
48   int   i, j, k;
49   float *aip = _port [AIP];
50   float *aop = _port [AOP];
51   float *p, sig [48];
52   float t, g, d;
53   float fgain;
54   float sfreq [NSECT];
55   float sband [NSECT];
56   float sgain [NSECT];
57 
58   fgain = exp2ap (0.1661 * _port [GAIN][0]);
59 
60   for (j = 0; j < NSECT; j++)
61   {
62     t = _port [SECT + 4 * j + Paramsect::FREQ][0] / _fsam;
63       if (t < 0.0002) t = 0.0002;
64       if (t > 0.4998) t = 0.4998;
65       sfreq [j] = t;
66       sband [j] = _port [SECT + 4 * j + Paramsect::BAND][0];
67       if (_port [SECT + 4 * j + Paramsect::SECT][0] > 0) sgain [j] = exp2ap (0.1661 * _port [SECT + 4 * j + Paramsect::GAIN][0]);
68       else sgain [j] = 1.0;
69   }
70 
71   while (len)
72   {
73       k = (len > 48) ? 32 : len;
74 
75       t = fgain;
76       g = _gain;
77       if      (t > 1.25 * g) t = 1.25 * g;
78       else if (t < 0.80 * g) t = 0.80 * g;
79       _gain = t;
80       d = (t - g) / k;
81       for (i = 0; i < k; i++)
82     {
83         g += d;
84           sig [i] = g * aip [i];
85     }
86 
87       for (j = 0; j < NSECT; j++) _sect [j].proc (k, sig, sfreq [j], sband [j], sgain [j]);
88 
89       j = _fade;
90       g = j / 16.0;
91     p = 0;
92     if (_port [FILT][0] > 0)
93     {
94         if (j == 16) p = sig;
95         else ++j;
96     }
97     else
98     {
99         if (j == 0) p = aip;
100         else --j;
101     }
102     _fade = j;
103     if (p) memcpy (aop, p, k * sizeof (float));
104     else
105     {
106         d = (j / 16.0 - g) / k;
107         for (i = 0; i < k; i++)
108         {
109           g += d;
110           aop [i] = g * sig [i] + (1 - g) * aip [i];
111         }
112     }
113     aip += k;
114     aop += k;
115     len -= k;
116   }
117 }
118 
119