remove Session::AudioMidiSetupRequired signal (no longer necessary)
[ardour.git] / libs / ardour / mtdm.cc
1 /*
2  * Copyright (C) 2003-2012 Fons Adriaensen <fons@kokkinizita.net>
3  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2015 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <math.h>
23 #include <assert.h>
24
25 #include "ardour/mtdm.h"
26
27 MTDM::MTDM (int fsamp)
28   : _cnt (0)
29   , _inv (0)
30   , _peak (0)
31 {
32     int   i;
33     Freq  *F;
34
35     _freq [0].f  = 4096;
36     _freq [1].f  = 2048;
37     _freq [2].f  = 3072;
38     _freq [3].f  = 2560;
39     _freq [4].f  = 2304;
40     _freq [5].f  = 2176;
41     _freq [6].f  = 1088;
42     _freq [7].f  = 1312;
43     _freq [8].f  = 1552;
44     _freq [9].f  = 1800;
45     _freq [10].f = 3332;
46     _freq [11].f = 3586;
47     _freq [12].f = 3841;
48     _wlp = 200.0f / fsamp;
49     for (i = 0, F = _freq; i < 13; i++, F++)
50     {
51         F->p = 128;
52         F->xa = F->ya = 0.0f;
53         F->x1 = F->y1 = 0.0f;
54         F->x2 = F->y2 = 0.0f;
55     }
56 }
57
58
59 int MTDM::process (size_t len, float *ip, float *op)
60 {
61     int    i;
62     float  vip, vop, a, c, s;
63     Freq   *F;
64                 float peak = 0;
65                 assert (len > 0);
66
67     while (len--)
68     {
69         vop = 0.0f;
70         vip = *ip++;
71         if (fabsf(vip) > peak) { peak = vip; }
72         for (i = 0, F = _freq; i < 13; i++, F++)
73         {
74             a = 2 * (float) M_PI * (F->p & 65535) / 65536.0;
75             F->p += F->f;
76             c =  cosf (a);
77             s = -sinf (a);
78             vop += (i ? 0.01f : 0.20f) * s;
79             F->xa += s * vip;
80             F->ya += c * vip;
81         }
82         *op++ = vop;
83         if (++_cnt == 16)
84         {
85             for (i = 0, F = _freq; i < 13; i++, F++)
86             {
87                 F->x1 += _wlp * (F->xa - F->x1 + 1e-20);
88                 F->y1 += _wlp * (F->ya - F->y1 + 1e-20);
89                 F->x2 += _wlp * (F->x1 - F->x2 + 1e-20);
90                 F->y2 += _wlp * (F->y1 - F->y2 + 1e-20);
91                 F->xa = F->ya = 0.0f;
92             }
93             _cnt = 0;
94         }
95     }
96
97     if (peak > _peak) { _peak = vip; }
98
99     return 0;
100 }
101
102
103 int MTDM::resolve (void)
104 {
105     int     i, k, m;
106     double  d, e, f0, p;
107     Freq    *F = _freq;
108
109     if (hypot (F->x2, F->y2) < 0.001) return -1;
110     d = atan2 (F->y2, F->x2) / (2 * M_PI);
111     if (_inv) d += 0.5;
112     if (d > 0.5) d -= 1.0;
113     f0 = _freq [0].f;
114     m = 1;
115     _err = 0.0;
116     for (i = 0; i < 12; i++)
117     {
118         F++;
119         p = atan2 (F->y2, F->x2) / (2 * M_PI) - d * F->f / f0;
120         if (_inv) p += 0.5;
121         p -= floor (p);
122         p *= 2;
123         k = (int)(floor (p + 0.5));
124         e = fabs (p - k);
125         if (e > _err) _err = e;
126         if (e > 0.4) return 1;
127         d += m * (k & 1);
128         m *= 2;
129     }
130     _del = 16 * d;
131
132     return 0;
133 }
134
135