2 Copyright (C) 2003 Fons Adriaensen <fons.adriaensen@skynet.be>
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.
20 // --------------------------------------------------------------------------------
28 #define M_PI 3.141562653
31 #include <jack/jack.h>
53 int process (size_t len
, float *inp
, float *out
);
55 void invert (void) { _inv
^= 1; }
56 int inv (void) { return _inv
; }
57 double del (void) { return _del
; }
58 double err (void) { return _err
; }
70 MTDM::MTDM (void) : _cnt (0), _inv (0)
87 for (i
= 0, F
= _freq
; i
< 5; i
++, F
++)
95 int MTDM::process (size_t len
, float *ip
, float *op
)
98 float vip
, vop
, a
, c
, s
;
105 for (i
= 0, F
= _freq
; i
< 5; i
++, F
++)
107 a
= 2 * M_PI
* (F
->p
& 0xFFFF) / 65536.0;
118 for (i
= 0, F
= _freq
; i
< 5; i
++, F
++)
120 F
->xf
+= 1e-3f
* (F
->xa
- F
->xf
+ 1e-20);
121 F
->yf
+= 1e-3f
* (F
->ya
- F
->yf
+ 1e-20);
122 F
->xa
= F
->ya
= 0.0f
;
131 int MTDM::resolve (void)
137 if (hypot (F
->xf
, F
->yf
) < 0.01) return -1;
138 d
= atan2 (F
->yf
, F
->xf
) / (2 * M_PI
);
140 if (d
> 0.5f
) d
-= 1.0f
;
144 for (i
= 0; i
< 4; i
++)
147 p
= atan2 (F
->yf
, F
->xf
) / (2 * M_PI
) - d
* F
->f
/ f0
;
151 k
= (int)(floor (p
+ 0.5));
153 if (e
> _err
) _err
= e
;
154 if (e
> 0.4) return 1;
163 // --------------------------------------------------------------------------------
165 static jack_client_t
*jack_handle
;
166 static jack_port_t
*jack_capt
;
167 static jack_port_t
*jack_play
;
169 static void jack_shutdown (void *arg
)
174 static int jack_callback (jack_nframes_t nframes
, void *arg
)
178 ip
= (float *)(jack_port_get_buffer (jack_capt
, nframes
));
179 op
= (float *)(jack_port_get_buffer (jack_play
, nframes
));
180 return mtdm
.process (nframes
, ip
, op
);;
183 int main (int ac
, char *av
[])
188 if ((jack_handle
= jack_client_new ("jdelay")) == 0)
190 fprintf (stderr
, "Can't connect to JACK\n");
194 jack_set_process_callback (jack_handle
, jack_callback
, 0);
195 jack_on_shutdown (jack_handle
, jack_shutdown
, 0);
196 jack_capt
= jack_port_register (jack_handle
, "in", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
197 jack_play
= jack_port_register (jack_handle
, "out", JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
199 if (jack_activate (jack_handle
))
201 fprintf(stderr
, "Can't activate JACK");
205 if ((ports
= jack_get_ports(jack_handle
, NULL
, NULL
, JackPortIsPhysical
| JackPortIsOutput
)) == NULL
) {
206 printf("Cannot find any physical capture ports\n");
208 for (int i
= 0; i
< 8 && ports
[i
] ; i
++) {
209 jack_connect(jack_handle
, ports
[i
], jack_port_name(jack_capt
));
214 if ((ports
= jack_get_ports(jack_handle
, NULL
, NULL
, JackPortIsPhysical
| JackPortIsInput
)) == NULL
) {
215 printf("Cannot find any physical playback ports");
217 for (int i
= 0; i
< 8 && ports
[i
]; i
++) {
218 jack_connect(jack_handle
, jack_port_name(jack_play
), ports
[i
]);
231 if (mtdm
.resolve () < 0) printf ("Signal below threshold...\n");
234 if (mtdm
.err () > 0.3)
239 printf ("%10.3lf frames", mtdm
.del ());
240 if (mtdm
.err () > 0.2) printf (" ??");
241 if (mtdm
.inv ()) printf (" Inv");