fix up file renaming code a little bit
[ArdourMidi.git] / libs / ardour / mtdm.cc
blobdceb4f8406fb7c7951f18cfcc4b3a5e053b7cdd7
1 /*
2 Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
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.
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.
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.
19 #include "ardour/mtdm.h"
21 MTDM::MTDM (void)
22 : _cnt (0)
23 , _inv (0)
25 int i;
26 Freq *F;
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;
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;
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;
47 int
48 MTDM::process (size_t len, float *ip, float *op)
50 int i;
51 float vip, vop, a, c, s;
52 Freq *F;
54 while (len--)
56 vop = 0.0f;
57 vip = *ip++;
58 for (i = 0, F = _freq; i < 5; i++, F++)
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;
68 *op++ = vop;
69 if (++_cnt == 16)
71 for (i = 0, F = _freq; i < 5; i++, F++)
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;
77 _cnt = 0;
81 return 0;
84 int
85 MTDM::resolve ()
87 int i, k, m;
88 double d, e, f0, p;
89 Freq *F = _freq;
91 if (hypot (F->xf, F->yf) < 0.01) {
92 return -1;
95 d = atan2 (F->yf, F->xf) / (2 * M_PI);
97 if (_inv) {
98 d += 0.5f;
101 if (d > 0.5f) {
102 d -= 1.0f;
105 f0 = _freq [0].f;
106 m = 1;
107 _err = 0.0;
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;
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;
122 if (e > 0.4) {
123 return 1;
125 d += m * (k & 7);
126 m *= 8;
129 _del = 16 * d;
131 return 0;