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