Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[ardour.git] / libs / ardour / mtdm.cc
1 /*
2     Copyright (C) 2003-2008 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 <ardour/mtdm.h>
20
21 MTDM::MTDM (void)
22         : _cnt (0)
23         , _inv (0)
24 {
25         int   i;
26         Freq  *F;
27
28         _freq [0].f = 4096;
29         _freq [1].f =  512;
30         _freq [2].f = 1088;
31         _freq [3].f = 1544;
32         _freq [4].f = 2049;
33
34         _freq [0].a = 0.2f;
35         _freq [1].a = 0.1f;
36         _freq [2].a = 0.1f;
37         _freq [3].a = 0.1f;
38         _freq [4].a = 0.1f;
39
40         for (i = 0, F = _freq; i < 5; i++, F++) {
41                 F->p = 128;
42                 F->xa = F->ya = 0.0f;
43                 F->xf = F->yf = 0.0f;
44         }
45 }
46
47 int
48 MTDM::process (size_t len, float *ip, float *op)
49 {
50         int    i;
51         float  vip, vop, a, c, s;
52         Freq   *F;
53
54         while (len--)
55         {
56                 vop = 0.0f;
57                 vip = *ip++;
58                 for (i = 0, F = _freq; i < 5; i++, F++)
59                 {
60                         a = 2 * (float) M_PI * (F->p & 65535) / 65536.0;
61                         F->p += F->f;
62                         c =  cosf (a);
63                         s = -sinf (a);
64                         vop += F->a * s;
65                         F->xa += s * vip;
66                         F->ya += c * vip;
67                 }
68                 *op++ = vop;
69                 if (++_cnt == 16)
70                 {
71                         for (i = 0, F = _freq; i < 5; i++, F++)
72                         {
73                                 F->xf += 1e-3f * (F->xa - F->xf + 1e-20);
74                                 F->yf += 1e-3f * (F->ya - F->yf + 1e-20);
75                                 F->xa = F->ya = 0.0f;
76                         }
77                         _cnt = 0;
78                 }
79         }
80
81         return 0;
82 }
83
84 int
85 MTDM::resolve ()
86 {
87         int     i, k, m;
88         double  d, e, f0, p;
89         Freq    *F = _freq;
90
91         if (hypot (F->xf, F->yf) < 0.01) {
92                 return -1;
93         }
94
95         d = atan2 (F->yf, F->xf) / (2 * M_PI);
96
97         if (_inv) {
98                 d += 0.5f;
99         }
100
101         if (d > 0.5f) {
102                 d -= 1.0f;
103         }
104
105         f0 = _freq [0].f;
106         m = 1;
107         _err = 0.0;
108
109         for (i = 0; i < 4; i++) {
110                 F++;
111                 p = atan2 (F->yf, F->xf) / (2 * M_PI) - d * F->f / f0;
112                 if (_inv) {
113                         p += 0.5f;
114                 }
115                 p -= floor (p);
116                 p *= 8;
117                 k = (int)(floor (p + 0.5));
118                 e = fabs (p - k);
119                 if (e > _err) {
120                         _err = e;
121                 }
122                 if (e > 0.4) {
123                         return 1;
124                 }
125                 d += m * (k & 7);
126                 m *= 8;
127         }
128
129         _del = 16 * d;
130
131         return 0;
132 }