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