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 // --------------------------------------------------------------------------------
25 #include <jack/jack.h>
45 int process (size_t len
, float *inp
, float *out
);
47 void invert (void) { _inv
^= 1; }
48 int inv (void) { return _inv
; }
49 double del (void) { return _del
; }
50 double err (void) { return _err
; }
61 MTDM::MTDM (void) : _cnt (0), _inv (0)
78 for (i
= 0, F
= _freq
; i
< 5; i
++, F
++)
86 int MTDM::process (size_t len
, float *ip
, float *op
)
89 float vip
, vop
, a
, c
, s
;
96 for (i
= 0, F
= _freq
; i
< 5; i
++, F
++)
98 a
= 2 * (float) M_PI
* (F
->p
& 65535) / 65536.0;
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
;
122 int MTDM::resolve (void)
128 if (hypot (F
->xf
, F
->yf
) < 0.01) return -1;
129 d
= atan2 (F
->yf
, F
->xf
) / (2 * M_PI
);
131 if (d
> 0.5f
) d
-= 1.0f
;
135 for (i
= 0; i
< 4; i
++)
138 p
= atan2 (F
->yf
, F
->xf
) / (2 * M_PI
) - d
* F
->f
/ f0
;
142 k
= (int)(floor (p
+ 0.5));
144 if (e
> _err
) _err
= e
;
145 if (e
> 0.4) return 1;
154 // --------------------------------------------------------------------------------
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
)
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
);
171 int main (int ac
, char *av
[])
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");
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");
209 if (mtdm
.resolve () < 0) printf ("Signal below threshold...\n");
212 if (mtdm
.err () > 0.3)
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");
227 // --------------------------------------------------------------------------------