fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / mtdm.cc
1 /*
2     Copyright (C) 2003-2012 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 #include <math.h>
20 #include <assert.h>
21
22 #include "ardour/mtdm.h"
23
24 MTDM::MTDM (int fsamp)
25   : _cnt (0)
26   , _inv (0)
27   , _peak (0)
28 {
29     int   i;
30     Freq  *F;
31
32     _freq [0].f  = 4096;
33     _freq [1].f  = 2048;
34     _freq [2].f  = 3072;
35     _freq [3].f  = 2560;
36     _freq [4].f  = 2304;
37     _freq [5].f  = 2176;
38     _freq [6].f  = 1088;
39     _freq [7].f  = 1312;
40     _freq [8].f  = 1552;
41     _freq [9].f  = 1800;
42     _freq [10].f = 3332;
43     _freq [11].f = 3586;
44     _freq [12].f = 3841;
45     _wlp = 200.0f / fsamp;
46     for (i = 0, F = _freq; i < 13; i++, F++)
47     {
48         F->p = 128;
49         F->xa = F->ya = 0.0f;
50         F->x1 = F->y1 = 0.0f;
51         F->x2 = F->y2 = 0.0f;
52     }
53 }
54
55
56 int MTDM::process (size_t len, float *ip, float *op)
57 {
58     int    i;
59     float  vip, vop, a, c, s;
60     Freq   *F;
61                 float peak = 0;
62                 assert (len > 0);
63
64     while (len--)
65     {
66         vop = 0.0f;
67         vip = *ip++;
68         if (fabsf(vip) > peak) { peak = vip; }
69         for (i = 0, F = _freq; i < 13; i++, F++)
70         {
71             a = 2 * (float) M_PI * (F->p & 65535) / 65536.0;
72             F->p += F->f;
73             c =  cosf (a);
74             s = -sinf (a);
75             vop += (i ? 0.01f : 0.20f) * s;
76             F->xa += s * vip;
77             F->ya += c * vip;
78         }
79         *op++ = vop;
80         if (++_cnt == 16)
81         {
82             for (i = 0, F = _freq; i < 13; i++, F++)
83             {
84                 F->x1 += _wlp * (F->xa - F->x1 + 1e-20);
85                 F->y1 += _wlp * (F->ya - F->y1 + 1e-20);
86                 F->x2 += _wlp * (F->x1 - F->x2 + 1e-20);
87                 F->y2 += _wlp * (F->y1 - F->y2 + 1e-20);
88                 F->xa = F->ya = 0.0f;
89             }
90             _cnt = 0;
91         }
92     }
93
94     if (peak > _peak) { _peak = vip; }
95
96     return 0;
97 }
98
99
100 int MTDM::resolve (void)
101 {
102     int     i, k, m;
103     double  d, e, f0, p;
104     Freq    *F = _freq;
105
106     if (hypot (F->x2, F->y2) < 0.001) return -1;
107     d = atan2 (F->y2, F->x2) / (2 * M_PI);
108     if (_inv) d += 0.5;
109     if (d > 0.5) d -= 1.0;
110     f0 = _freq [0].f;
111     m = 1;
112     _err = 0.0;
113     for (i = 0; i < 12; i++)
114     {
115         F++;
116         p = atan2 (F->y2, F->x2) / (2 * M_PI) - d * F->f / f0;
117         if (_inv) p += 0.5;
118         p -= floor (p);
119         p *= 2;
120         k = (int)(floor (p + 0.5));
121         e = fabs (p - k);
122         if (e > _err) _err = e;
123         if (e > 0.4) return 1;
124         d += m * (k & 1);
125         m *= 2;
126     }
127     _del = 16 * d;
128
129     return 0;
130 }
131
132