iodelay.cpp 6.19 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
    Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
sletz's avatar
sletz committed
3

sletz's avatar
sletz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

// --------------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>
23
#include <math.h>
sletz's avatar
sletz committed
24
#include <unistd.h>
sletz's avatar
sletz committed
25
#include <jack/jack.h>
sletz's avatar
sletz committed
26

sletz's avatar
sletz committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class Freq
{
public:

    int   p;
    int   f;
    float a;
    float xa;
    float ya;
    float xf;
    float yf;
};

class MTDM
{
public:

    MTDM (void);
    int process (size_t len, float *inp, float *out);
    int resolve (void);
    void invert (void) { _inv ^= 1; }
    int    inv (void) { return _inv; }
    double del (void) { return _del; }
    double err (void) { return _err; }


    double  _del;
    double  _err;
    int     _cnt;
    int     _inv;
    Freq    _freq [5];
};

MTDM::MTDM (void) : _cnt (0), _inv (0)
{
    int   i;
    Freq  *F;

    _freq [0].f = 4096;
    _freq [1].f =  512;
    _freq [2].f = 1088;
    _freq [3].f = 1544;
    _freq [4].f = 2049;

    _freq [0].a = 0.2f;
    _freq [1].a = 0.1f;
    _freq [2].a = 0.1f;
    _freq [3].a = 0.1f;
    _freq [4].a = 0.1f;

    for (i = 0, F = _freq; i < 5; i++, F++)
    {
	F->p = 128;
	F->xa = F->ya = 0.0f;
	F->xf = F->yf = 0.0f;
    }
}

int MTDM::process (size_t len, float *ip, float *op)
{
    int    i;
    float  vip, vop, a, c, s;
    Freq   *F;

    while (len--)
    {
        vop = 0.0f;
	vip = *ip++;
	for (i = 0, F = _freq; i < 5; i++, F++)
	{
sletz's avatar
sletz committed
97
	    a = 2 * (float) M_PI * (F->p & 65535) / 65536.0;
sletz's avatar
sletz committed
98
	    F->p += F->f;
sletz's avatar
sletz committed
99
100
	    c =  cosf (a);
	    s = -sinf (a);
sletz's avatar
sletz committed
101
102
103
	    vop += F->a * s;
	    F->xa += s * vip;
	    F->ya += c * vip;
sletz's avatar
sletz committed
104
	}
sletz's avatar
sletz committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	*op++ = vop;
	if (++_cnt == 16)
	{
	    for (i = 0, F = _freq; i < 5; i++, F++)
	    {
		F->xf += 1e-3f * (F->xa - F->xf + 1e-20);
		F->yf += 1e-3f * (F->ya - F->yf + 1e-20);
		F->xa = F->ya = 0.0f;
	    }
            _cnt = 0;
	}
    }

    return 0;
}

int MTDM::resolve (void)
{
    int     i, k, m;
    double  d, e, f0, p;
    Freq    *F = _freq;

    if (hypot (F->xf, F->yf) < 0.01) return -1;
    d = atan2 (F->yf, F->xf) / (2 * M_PI);
    if (_inv) d += 0.5f;
    if (d > 0.5f) d -= 1.0f;
    f0 = _freq [0].f;
    m = 1;
    _err = 0.0;
    for (i = 0; i < 4; i++)
    {
	F++;
	p = atan2 (F->yf, F->xf) / (2 * M_PI) - d * F->f / f0;
        if (_inv) p += 0.5f;
	p -= floor (p);
	p *= 8;
	k = (int)(floor (p + 0.5));
	e = fabs (p - k);
        if (e > _err) _err = e;
sletz's avatar
sletz committed
144
        if (e > 0.4) return 1;
sletz's avatar
sletz committed
145
146
	d += m * (k & 7);
	m *= 8;
sletz's avatar
sletz committed
147
    }
sletz's avatar
sletz committed
148
149
150
151
152
153
    _del = 16 * d;

    return 0;
}

// --------------------------------------------------------------------------------
sletz's avatar
sletz committed
154

sletz's avatar
sletz committed
155
156
157
158
159
static MTDM            mtdm;
static jack_client_t  *jack_handle;
static jack_port_t    *jack_capt;
static jack_port_t    *jack_play;

sletz's avatar
sletz committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
jack_latency_range_t   capture_latency = {-1, -1};
jack_latency_range_t   playback_latency = {-1, -1};

void
latency_cb (jack_latency_callback_mode_t mode, void *arg)
{
	jack_latency_range_t range;

	range.min = range.max = 0;

	if (mode == JackCaptureLatency) {
		jack_port_set_latency_range (jack_play, mode, &range);
		jack_port_get_latency_range (jack_capt, mode, &range);
		if ((range.min != capture_latency.min) || (range.max != capture_latency.max)) {
			capture_latency = range;
			printf ("new capture latency: [%d, %d]\n", range.min, range.max);
		}
	} else {
		jack_port_set_latency_range (jack_capt, mode, &range);
		jack_port_get_latency_range (jack_play, mode, &range);
		if ((range.min != playback_latency.min) || (range.max != playback_latency.max)) {
			playback_latency = range;
			printf ("new playback latency: [%d, %d]\n", range.min, range.max);
		}
	}

}

sletz's avatar
sletz committed
188
int jack_callback (jack_nframes_t nframes, void *arg)
sletz's avatar
sletz committed
189
190
191
192
193
{
    float *ip, *op;

    ip = (float *)(jack_port_get_buffer (jack_capt, nframes));
    op = (float *)(jack_port_get_buffer (jack_play, nframes));
sletz's avatar
sletz committed
194
195
    mtdm.process (nframes, ip, op);
    return 0;
sletz's avatar
sletz committed
196
197
198
199
}

int main (int ac, char *av [])
{
sletz's avatar
sletz committed
200
201
    float          t;
    jack_status_t  s;
sletz's avatar
sletz committed
202

sletz's avatar
sletz committed
203
204
    jack_handle = jack_client_open ("jack_delay", JackNoStartServer, &s);
    if (jack_handle == 0)
sletz's avatar
sletz committed
205
    {
sletz's avatar
sletz committed
206
207
        fprintf (stderr, "Can't connect to Jack, is the server running ?\n");
        exit (1);
sletz's avatar
sletz committed
208
209
210
    }

    jack_set_process_callback (jack_handle, jack_callback, 0);
sletz's avatar
sletz committed
211

sletz's avatar
sletz committed
212
213
214
    if (jack_set_latency_callback)
	    jack_set_latency_callback (jack_handle, latency_cb, 0);

sletz's avatar
sletz committed
215
216
217
    jack_capt = jack_port_register (jack_handle, "in",  JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
    jack_play = jack_port_register (jack_handle, "out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);

sletz's avatar
sletz committed
218
219
    t = 1000.0f / jack_get_sample_rate (jack_handle);

sletz's avatar
sletz committed
220
221
    if (jack_activate (jack_handle))
    {
sletz's avatar
sletz committed
222
        fprintf(stderr, "Can't activate Jack");
sletz's avatar
sletz committed
223
224
225
226
227
        return 1;
    }

    while (1)
    {
sletz's avatar
sletz committed
228
229
230
231
232
233

    #ifdef WIN32
        Sleep (250);
    #else
        usleep (250000);
 	#endif
sletz's avatar
sletz committed
234
        if (mtdm.resolve() < 0) printf ("Signal below threshold...\n");
sletz's avatar
sletz committed
235
        else
sletz's avatar
sletz committed
236
        {
237
            jack_nframes_t systemic_latency;
sletz's avatar
sletz committed
238
            if (mtdm.err () > 0.3)
sletz's avatar
sletz committed
239
            {
sletz's avatar
sletz committed
240
241
                mtdm.invert ();
                mtdm.resolve ();
sletz's avatar
sletz committed
242
            }
sletz's avatar
sletz committed
243
            systemic_latency = (jack_nframes_t) floor (mtdm._del - (capture_latency.max + playback_latency.max));
244

sletz's avatar
sletz committed
245
246
            printf("%10.3lf frames %10.3lf ms total roundtrip latency\n\textra loopback latency: %u frames\n\tuse %u for the backend arguments -I and -O"
                    , mtdm._del, mtdm._del * t,
247
                    systemic_latency, systemic_latency/2);
sletz's avatar
sletz committed
248
249
            if (mtdm._err > 0.2) printf (" ??");
                if (mtdm._inv) printf (" Inv");
sletz's avatar
sletz committed
250
251
            printf ("\n");
        }
sletz's avatar
sletz committed
252
253
254
255
256
    }

    return 0;
}

sletz's avatar
sletz committed
257
// --------------------------------------------------------------------------------