Remove non-jackdbus man pages
[jackdbus.git] / tools / zalsa / jackclient.cc
blob095b26329707d4ed4ef7ef47fdf79c102caa66e7
1 // ----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2012-2018 Fons Adriaensen <fons@linuxaudio.org>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // ----------------------------------------------------------------------------
21 #include <stdio.h>
22 #include <math.h>
23 #include "jackclient.h"
24 #include "alsathread.h"
25 #include "timers.h"
28 Jackclient::Jackclient (jack_client_t* cl, const char *jserv, int mode, int nchan, bool sync, void *arg) :
29 _client (cl),
30 _arg (arg),
31 _mode (mode),
32 _nchan (nchan),
33 _state (INIT),
34 _freew (false),
35 _resamp (0)
37 init (jserv);
38 if (!sync) _resamp = new VResampler ();
42 Jackclient::~Jackclient (void)
44 fini ();
48 bool Jackclient::init (const char *jserv)
50 int i, spol, flags;
51 char s [64];
52 struct sched_param spar;
54 if (_client == 0)
56 fprintf (stderr, "Can't connect to Jack, is the server running ?\n");
57 return false;
59 jack_set_process_callback (_client, jack_static_process, (void *) this);
60 jack_set_latency_callback (_client, jack_static_latency, (void *) this);
61 jack_set_freewheel_callback (_client, jack_static_freewheel, (void *) this);
62 jack_set_buffer_size_callback (_client, jack_static_buffsize, (void *) this);
63 jack_on_shutdown (_client, jack_static_shutdown, (void *) this);
65 _bsize = 0;
66 _fsamp = 0;
67 if (jack_activate (_client))
69 fprintf(stderr, "Can't activate Jack");
70 return false;
72 _jname = jack_get_client_name (_client);
73 _bsize = jack_get_buffer_size (_client);
74 _fsamp = jack_get_sample_rate (_client);
76 flags = JackPortIsTerminal | JackPortIsPhysical;
77 for (i = 0; i < _nchan; i++)
79 if (_mode == PLAY)
81 sprintf (s, "playback_%d", i + 1);
82 _ports [i] = jack_port_register (_client, s, JACK_DEFAULT_AUDIO_TYPE,
83 flags | JackPortIsInput, 0);
85 else
87 sprintf (s, "capture_%d", i + 1);
88 _ports [i] = jack_port_register (_client, s, JACK_DEFAULT_AUDIO_TYPE,
89 flags | JackPortIsOutput, 0);
92 pthread_getschedparam (jack_client_thread_id (_client), &spol, &spar);
93 _rprio = spar.sched_priority - sched_get_priority_max (spol);
94 _buff = new float [_bsize * _nchan];
95 return true;
99 void Jackclient::fini (void)
101 delete[] _buff;
102 delete _resamp;
106 void Jackclient::jack_static_shutdown (void *arg)
108 ((Jackclient *) arg)->sendinfo (TERM, 0, 0);
112 int Jackclient::jack_static_buffsize (jack_nframes_t nframes, void *arg)
114 Jackclient *J = (Jackclient *) arg;
116 if (J->_bsize == 0) J->_bsize = nframes;
117 else if (J->_bsize != (int) nframes) J->_state = Jackclient::TERM;
118 return 0;
122 void Jackclient::jack_static_freewheel (int state, void *arg)
124 ((Jackclient *) arg)->jack_freewheel (state);
128 void Jackclient::jack_static_latency (jack_latency_callback_mode_t jlcm, void *arg)
130 ((Jackclient *) arg)->jack_latency (jlcm);
134 int Jackclient::jack_static_process (jack_nframes_t nframes, void *arg)
136 return ((Jackclient *) arg)->jack_process (nframes);
140 void Jackclient::start (Lfq_audio *audioq,
141 Lfq_int32 *commq,
142 Lfq_adata *alsaq,
143 Lfq_jdata *infoq,
144 double ratio,
145 int delay,
146 int ltcor,
147 int rqual)
149 double d;
151 _audioq = audioq;
152 _commq = commq;
153 _alsaq = alsaq;
154 _infoq = infoq;
155 _ratio = ratio;
156 _delay = delay;
157 _rcorr = 1.0;
158 if (_resamp)
160 _resamp->setup (_ratio, _nchan, rqual);
161 _resamp->set_rrfilt (100);
162 d = _resamp->inpsize () / 2.0;
163 if (_mode == PLAY) d *= _ratio;
164 _delay += d;
166 _ltcor = ltcor;
167 _ppsec = (_fsamp + _bsize / 2) / _bsize;
168 initwait (_ppsec / 2);
169 jack_recompute_total_latencies (_client);
173 void Jackclient::initwait (int nwait)
175 _count = -nwait;
176 _commq->wr_int32 (Alsathread::WAIT);
177 _state = WAIT;
178 if (nwait > _ppsec) sendinfo (WAIT, 0, 0);
182 void Jackclient::initsync (void)
184 // Reset all lock-free queues.
185 _commq->reset ();
186 _alsaq->reset ();
187 _audioq->reset ();
188 if (_resamp)
190 // Reset and prefill the resampler.
191 _resamp->reset ();
192 _resamp->inp_count = _resamp->inpsize () / 2 - 1;
193 _resamp->out_count = 99999;
194 _resamp->process ();
196 // Initialise state variables.
197 _t_a0 = _t_a1 = 0;
198 _k_a0 = _k_a1 = 0;
199 // Initialise loop filter state.
200 _z1 = _z2 = _z3 = 0;
201 // Activate the ALSA thread,
202 _commq->wr_int32 (Alsathread::PROC);
203 _state = SYNC0;
204 sendinfo (SYNC0, 0, 0);
208 void Jackclient::setloop (double bw)
210 double w;
212 // Set the loop bandwidth to bw Hz.
213 w = 6.28 * bw * _bsize / _fsamp;
214 _w0 = 1.0 - exp (-20.0 * w);
215 _w1 = w * 2 / _bsize;
216 _w2 = w / 2;
217 if (_mode == PLAY) _w1 /= _ratio;
218 else _w1 *= _ratio;
222 void Jackclient::playback (int nframes)
224 int i, j, n;
225 float *p, *q;
226 float *inp [MAXCHAN];
228 _bstat = _audioq->rd_avail ();
229 for (i = 0; i < _nchan; i++)
231 inp [i] = (float *)(jack_port_get_buffer (_ports [i], nframes));
233 if (_resamp)
235 // Interleave inputs into _buff.
236 for (i = 0; i < _nchan; i++)
238 p = inp [i];
239 q = _buff + i;
240 for (j = 0; j < _bsize; j++) q [j * _nchan] = p [j];
242 // Resample _buff and write to audio queue.
243 // The while loop takes care of wraparound.
244 _resamp->inp_count = _bsize;
245 _resamp->inp_data = _buff;
246 while (_resamp->inp_count)
248 _resamp->out_count = _audioq->wr_linav ();
249 _resamp->out_data = _audioq->wr_datap ();
250 n = _resamp->out_count;
251 _resamp->process ();
252 n -= _resamp->out_count;
253 _audioq->wr_commit (n);
256 else
258 // Interleave inputs into audio queue.
259 // The while loop takes care of wraparound.
260 while (nframes)
262 q = _audioq->wr_datap ();
263 n = _audioq->wr_linav ();
264 if (n > nframes) n = nframes;
265 for (i = 0; i < _nchan; i++)
267 p = inp [i];
268 for (j = 0; j < n; j++) q [j * _nchan] = p [j];
269 inp [i] += n;
270 q += 1;
272 _audioq->wr_commit (n);
273 nframes -= n;
279 void Jackclient::capture (int nframes)
281 int i, j, n;
282 float *p, *q;
283 float *out [MAXCHAN];
285 for (i = 0; i < _nchan; i++)
287 out [i] = (float *)(jack_port_get_buffer (_ports [i], nframes));
289 if (_resamp)
291 // Read from audio queue and resample.
292 // The while loop takes care of wraparound.
293 _resamp->out_count = _bsize;
294 _resamp->out_data = _buff;
295 while (_resamp->out_count)
297 _resamp->inp_count = _audioq->rd_linav ();
298 _resamp->inp_data = _audioq->rd_datap ();
299 n = _resamp->inp_count;
300 _resamp->process ();
301 n -= _resamp->inp_count;
302 _audioq->rd_commit (n);
304 // Deinterleave _buff to outputs.
305 for (i = 0; i < _nchan; i++)
307 p = _buff + i;
308 q = out [i];
309 for (j = 0; j < _bsize; j++) q [j] = p [j * _nchan];
312 else
314 // Deinterleave audio queue to outputs.
315 // The while loop takes care of wraparound.
316 while (nframes)
318 p = _audioq->rd_datap ();
319 n = _audioq->rd_linav ();
320 if (n > nframes) n = nframes;
321 for (i = 0; i < _nchan; i++)
323 q = out [i];
324 for (j = 0; j < n; j++) q [j] = p [j * _nchan];
325 out [i] += n;
326 p += 1;
328 _audioq->rd_commit (n);
329 nframes -= n;
332 _bstat = _audioq->rd_avail ();
336 void Jackclient::silence (int nframes)
338 int i;
339 float *q;
341 // Write silence to all jack ports.
342 for (i = 0; i < _nchan; i++)
344 q = (float *)(jack_port_get_buffer (_ports [i], nframes));
345 memset (q, 0, nframes * sizeof (float));
350 void Jackclient::sendinfo (int state, double error, double ratio)
352 Jdata *J;
354 if (_infoq->wr_avail ())
356 J = _infoq->wr_datap ();
357 J->_state = state;
358 J->_error = error;
359 J->_ratio = ratio;
360 J->_bstat = _bstat;
361 _infoq->wr_commit ();
366 void Jackclient::jack_freewheel (int state)
368 _freew = state ? true : false;
369 if (_freew) initwait (_ppsec / 4);
373 void Jackclient::jack_latency (jack_latency_callback_mode_t jlcm)
375 jack_latency_range_t R;
376 int i;
378 if (_state < WAIT) return;
379 if (_mode == PLAY)
381 if (jlcm != JackPlaybackLatency) return;
382 R.min = R.max = (int)(_delay / _ratio) + _ltcor;
384 else
386 if (jlcm != JackCaptureLatency) return;
387 R.min = R.max = (int)(_delay * _ratio) + _ltcor;
389 for (i = 0; i < _nchan; i++)
391 jack_port_set_latency_range (_ports [i], jlcm, &R);
396 int Jackclient::jack_process (int nframes)
398 int dk, n;
399 Adata *D;
400 jack_time_t t0, t1;
401 jack_nframes_t ft;
402 float us;
403 double tj, err, d1, d2, rd;
405 // Buffer size change or other evil.
406 if (_state == TERM)
408 sendinfo (TERM, 0, 0);
409 return 0;
411 // Skip cylce if ports may not yet exist.
412 if (_state < WAIT) return 0;
414 // Start synchronisation 1/2 second after entering
415 // the WAIT state. This delay allows the ALSA thread
416 // to restart cleanly if necessary. Disabled while
417 // freewheeling.
418 if (_state == WAIT)
420 if (_freew) return 0;
421 if (_mode == CAPT) silence (nframes);
422 if (++_count == 0) initsync ();
423 else return 0;
426 // Get the start time of the current cycle.
427 jack_get_cycle_times (_client, &ft, &t0, &t1, &us);
428 tj = tjack (t0);
430 // Check for any skipped cycles.
431 if (_state >= SYNC1)
433 dk = ft - _ft - _bsize;
434 if (_mode == PLAY)
436 dk = (int)(dk * _ratio + 0.5);
437 _audioq->wr_commit (dk);
439 else
441 dk = (int)(dk / _ratio + 0.5);
442 _audioq->rd_commit (dk);
445 _ft = ft;
447 // Check if we have timing data from the ALSA thread.
448 n = _alsaq->rd_avail ();
449 // If the data queue is full restart synchronisation.
450 // This can happen e.g. on a jack engine timeout, or
451 // when too many cycles have been skipped.
452 if (n == _alsaq->size ())
454 initwait (_ppsec / 2);
455 return 0;
457 if (n)
459 // Else move interval end to start, and update the
460 // interval end keeping only the most recent data.
461 if (_state < SYNC2) _state++;
462 _t_a0 = _t_a1;
463 _k_a0 = _k_a1;
464 while (_alsaq->rd_avail ())
466 D = _alsaq->rd_datap ();
467 // Restart synchronisation in case of
468 // an error in the ALSA interface.
469 if (D->_state == Alsathread::WAIT)
471 initwait (_ppsec / 2);
472 return 0;
474 _t_a1 = D->_timer;
475 _k_a1 += D->_nsamp;
476 _alsaq->rd_commit ();
480 err = 0;
481 if (_state >= SYNC2)
483 // Compute the delay error.
484 d1 = tjack_diff (tj, _t_a0);
485 d2 = tjack_diff (_t_a1, _t_a0);
486 rd = _resamp ? _resamp->inpdist () : 0.0;
488 if (_mode == PLAY)
490 n = _audioq->nwr () - _k_a0; // Must be done as integer as both terms will overflow.
491 err = n - (_k_a1 - _k_a0) * d1 / d2 + rd * _ratio - _delay;
493 else
495 n = _k_a0 - _audioq->nrd (); // Must be done as integer as both terms will overflow.
496 err = n + (_k_a1 - _k_a0) * d1 / d2 + rd - _delay ;
498 n = (int)(floor (err + 0.5));
499 if (_state == SYNC2)
501 // We have the first delay error value. Adjust the audio
502 // queue to obtain the actually wanted delay, and start
503 // tracking.
504 if (_mode == PLAY) _audioq->wr_commit (-n);
505 else _audioq->rd_commit (n);
506 err -= n;
507 setloop (1.0);
508 _state = PROC1;
512 // Switch to lower bandwidth after 4 seconds.
513 if ((_state == PROC1) && (++_count == 4 * _ppsec))
515 _state = PROC2;
516 setloop (0.05);
519 if (_state >= PROC1)
521 _z1 += _w0 * (_w1 * err - _z1);
522 _z2 += _w0 * (_z1 - _z2);
523 _z3 += _w2 * _z2;
524 // Check error conditions.
525 if (fabs (_z3) > 0.05)
527 // Something is really wrong, wait 10 seconds then restart.
528 initwait (10 * _ppsec);
529 return 0;
531 // Run loop filter and set resample ratio.
532 if (_resamp)
534 _rcorr = 1 - (_z2 + _z3);
535 if (_rcorr > 1.05) _rcorr = 1.05;
536 if (_rcorr < 0.95) _rcorr = 0.95;
537 _resamp->set_rratio (_rcorr);
539 sendinfo (_state, err, _rcorr);
541 // Resample and transfer between audio
542 // queue and jack ports.
543 if (_mode == PLAY) playback (nframes);
544 else capture (nframes);
546 else if (_mode == CAPT) silence (nframes);
548 return 0;