Fix merge conflicts.
[jack2.git] / tests / jdelay.cpp
blob709fd7e2db579f34e943937a526958ccfcfece91
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 // --------------------------------------------------------------------------------
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <unistd.h>
25 #include <jack/jack.h>
27 class Freq
29 public:
31 int p;
32 int f;
33 float a;
34 float xa;
35 float ya;
36 float xf;
37 float yf;
40 class MTDM
42 public:
44 MTDM (void);
45 int process (size_t len, float *inp, float *out);
46 int resolve (void);
47 void invert (void) { _inv ^= 1; }
48 int inv (void) { return _inv; }
49 double del (void) { return _del; }
50 double err (void) { return _err; }
52 private:
54 double _del;
55 double _err;
56 int _cnt;
57 int _inv;
58 Freq _freq [5];
61 MTDM::MTDM (void) : _cnt (0), _inv (0)
63 int i;
64 Freq *F;
66 _freq [0].f = 4096;
67 _freq [1].f = 512;
68 _freq [2].f = 1088;
69 _freq [3].f = 1544;
70 _freq [4].f = 2049;
72 _freq [0].a = 0.2f;
73 _freq [1].a = 0.1f;
74 _freq [2].a = 0.1f;
75 _freq [3].a = 0.1f;
76 _freq [4].a = 0.1f;
78 for (i = 0, F = _freq; i < 5; i++, F++)
80 F->p = 128;
81 F->xa = F->ya = 0.0f;
82 F->xf = F->yf = 0.0f;
86 int MTDM::process (size_t len, float *ip, float *op)
88 int i;
89 float vip, vop, a, c, s;
90 Freq *F;
92 while (len--)
94 vop = 0.0f;
95 vip = *ip++;
96 for (i = 0, F = _freq; i < 5; i++, F++)
98 a = 2 * (float) M_PI * (F->p & 65535) / 65536.0;
99 F->p += F->f;
100 c = cosf (a);
101 s = -sinf (a);
102 vop += F->a * s;
103 F->xa += s * vip;
104 F->ya += c * vip;
106 *op++ = vop;
107 if (++_cnt == 16)
109 for (i = 0, F = _freq; i < 5; i++, F++)
111 F->xf += 1e-3f * (F->xa - F->xf + 1e-20);
112 F->yf += 1e-3f * (F->ya - F->yf + 1e-20);
113 F->xa = F->ya = 0.0f;
115 _cnt = 0;
119 return 0;
122 int MTDM::resolve (void)
124 int i, k, m;
125 double d, e, f0, p;
126 Freq *F = _freq;
128 if (hypot (F->xf, F->yf) < 0.01) return -1;
129 d = atan2 (F->yf, F->xf) / (2 * M_PI);
130 if (_inv) d += 0.5f;
131 if (d > 0.5f) d -= 1.0f;
132 f0 = _freq [0].f;
133 m = 1;
134 _err = 0.0;
135 for (i = 0; i < 4; i++)
137 F++;
138 p = atan2 (F->yf, F->xf) / (2 * M_PI) - d * F->f / f0;
139 if (_inv) p += 0.5f;
140 p -= floor (p);
141 p *= 8;
142 k = (int)(floor (p + 0.5));
143 e = fabs (p - k);
144 if (e > _err) _err = e;
145 if (e > 0.4) return 1;
146 d += m * (k & 7);
147 m *= 8;
149 _del = 16 * d;
151 return 0;
154 // --------------------------------------------------------------------------------
156 static MTDM mtdm;
157 static jack_client_t *jack_handle;
158 static jack_port_t *jack_capt;
159 static jack_port_t *jack_play;
161 int jack_callback (jack_nframes_t nframes, void *arg)
163 float *ip, *op;
165 ip = (float *)(jack_port_get_buffer (jack_capt, nframes));
166 op = (float *)(jack_port_get_buffer (jack_play, nframes));
167 mtdm.process (nframes, ip, op);
168 return 0;
171 int main (int ac, char *av [])
173 float t;
174 jack_status_t s;
176 jack_handle = jack_client_open ("jack_delay", JackNoStartServer, &s);
177 if (jack_handle == 0)
179 fprintf (stderr, "Can't connect to Jack, is the server running ?\n");
180 exit (1);
183 jack_set_process_callback (jack_handle, jack_callback, 0);
185 jack_capt = jack_port_register (jack_handle, "in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
186 jack_play = jack_port_register (jack_handle, "out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
188 printf ("capture latency = %d\n",
189 jack_port_get_latency (jack_port_by_name (jack_handle, "system:capture_1")));
190 printf ("playback_latency = %d\n",
191 jack_port_get_latency (jack_port_by_name (jack_handle, "system:playback_1")));
193 t = 1000.0f / jack_get_sample_rate (jack_handle);
195 if (jack_activate (jack_handle))
197 fprintf(stderr, "Can't activate Jack");
198 return 1;
201 while (1)
204 #ifdef WIN32
205 Sleep (250);
206 #else
207 usleep (250000);
208 #endif
209 if (mtdm.resolve () < 0) printf ("Signal below threshold...\n");
210 else
212 if (mtdm.err () > 0.3)
214 mtdm.invert ();
215 mtdm.resolve ();
217 printf ("%10.3lf frames %10.3lf ms", mtdm.del (), mtdm.del () * t);
218 if (mtdm.err () > 0.2) printf (" ??");
219 if (mtdm.inv ()) printf (" Inv");
220 printf ("\n");
224 return 0;
227 // --------------------------------------------------------------------------------