jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

550 lines
13KB

  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.
  14. //
  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/>.
  17. //
  18. // ----------------------------------------------------------------------------
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include "jackclient.h"
  22. #include "alsathread.h"
  23. #include "timers.h"
  24. Jackclient::Jackclient (jack_client_t* cl, const char *jserv, int mode, int nchan, bool sync, void *arg) :
  25. _client (cl),
  26. _arg (arg),
  27. _mode (mode),
  28. _nchan (nchan),
  29. _state (INIT),
  30. _freew (false),
  31. _resamp (0)
  32. {
  33. init (jserv);
  34. if (!sync) _resamp = new VResampler ();
  35. }
  36. Jackclient::~Jackclient (void)
  37. {
  38. fini ();
  39. }
  40. bool Jackclient::init (const char *jserv)
  41. {
  42. int i, spol, flags;
  43. char s [64];
  44. struct sched_param spar;
  45. if (_client == 0)
  46. {
  47. fprintf (stderr, "Can't connect to Jack, is the server running ?\n");
  48. return false;
  49. }
  50. jack_set_process_callback (_client, jack_static_process, (void *) this);
  51. jack_set_latency_callback (_client, jack_static_latency, (void *) this);
  52. jack_set_freewheel_callback (_client, jack_static_freewheel, (void *) this);
  53. jack_set_buffer_size_callback (_client, jack_static_buffsize, (void *) this);
  54. jack_on_shutdown (_client, jack_static_shutdown, (void *) this);
  55. _bsize = 0;
  56. _fsamp = 0;
  57. if (jack_activate (_client))
  58. {
  59. fprintf(stderr, "Can't activate Jack");
  60. return false;
  61. }
  62. _jname = jack_get_client_name (_client);
  63. _bsize = jack_get_buffer_size (_client);
  64. _fsamp = jack_get_sample_rate (_client);
  65. flags = JackPortIsTerminal | JackPortIsPhysical;
  66. for (i = 0; i < _nchan; i++)
  67. {
  68. if (_mode == PLAY)
  69. {
  70. sprintf (s, "USB_Audio_Playback_%d", i + 1);
  71. _ports [i] = jack_port_register (_client, s, JACK_DEFAULT_AUDIO_TYPE,
  72. flags | JackPortIsInput, 0);
  73. }
  74. else
  75. {
  76. sprintf (s, "USB_Audio_Capture_%d", i + 1);
  77. _ports [i] = jack_port_register (_client, s, JACK_DEFAULT_AUDIO_TYPE,
  78. flags | JackPortIsOutput, 0);
  79. }
  80. }
  81. pthread_getschedparam (jack_client_thread_id (_client), &spol, &spar);
  82. _rprio = spar.sched_priority - sched_get_priority_max (spol);
  83. _buff = new float [_bsize * _nchan];
  84. return true;
  85. }
  86. void Jackclient::fini (void)
  87. {
  88. delete[] _buff;
  89. delete _resamp;
  90. }
  91. void Jackclient::jack_static_shutdown (void *arg)
  92. {
  93. ((Jackclient *) arg)->sendinfo (TERM, 0, 0);
  94. }
  95. int Jackclient::jack_static_buffsize (jack_nframes_t nframes, void *arg)
  96. {
  97. Jackclient *J = (Jackclient *) arg;
  98. if (J->_bsize == 0) J->_bsize = nframes;
  99. else if (J->_bsize != (int) nframes) J->_state = Jackclient::TERM;
  100. return 0;
  101. }
  102. void Jackclient::jack_static_freewheel (int state, void *arg)
  103. {
  104. ((Jackclient *) arg)->jack_freewheel (state);
  105. }
  106. void Jackclient::jack_static_latency (jack_latency_callback_mode_t jlcm, void *arg)
  107. {
  108. ((Jackclient *) arg)->jack_latency (jlcm);
  109. }
  110. int Jackclient::jack_static_process (jack_nframes_t nframes, void *arg)
  111. {
  112. return ((Jackclient *) arg)->jack_process (nframes);
  113. }
  114. void Jackclient::start (Lfq_audio *audioq,
  115. Lfq_int32 *commq,
  116. Lfq_adata *alsaq,
  117. Lfq_jdata *infoq,
  118. double ratio,
  119. int delay,
  120. int ltcor,
  121. int rqual)
  122. {
  123. double d;
  124. _audioq = audioq;
  125. _commq = commq;
  126. _alsaq = alsaq;
  127. _infoq = infoq;
  128. _ratio = ratio;
  129. _delay = delay;
  130. _rcorr = 1.0;
  131. if (_resamp)
  132. {
  133. _resamp->setup (_ratio, _nchan, rqual);
  134. _resamp->set_rrfilt (100);
  135. d = _resamp->inpsize () / 2.0;
  136. if (_mode == PLAY) d *= _ratio;
  137. _delay += d;
  138. }
  139. _ltcor = ltcor;
  140. _ppsec = (_fsamp + _bsize / 2) / _bsize;
  141. initwait (_ppsec / 2);
  142. jack_recompute_total_latencies (_client);
  143. }
  144. void Jackclient::initwait (int nwait)
  145. {
  146. _count = -nwait;
  147. _commq->wr_int32 (Alsathread::WAIT);
  148. _state = WAIT;
  149. if (nwait > _ppsec) sendinfo (WAIT, 0, 0);
  150. }
  151. void Jackclient::initsync (void)
  152. {
  153. // Reset all lock-free queues.
  154. _commq->reset ();
  155. _alsaq->reset ();
  156. _audioq->reset ();
  157. if (_resamp)
  158. {
  159. // Reset and prefill the resampler.
  160. _resamp->reset ();
  161. _resamp->inp_count = _resamp->inpsize () / 2 - 1;
  162. _resamp->out_count = 99999;
  163. _resamp->process ();
  164. }
  165. // Initiliase state variables.
  166. _t_a0 = _t_a1 = 0;
  167. _k_a0 = _k_a1 = 0;
  168. // Initialise loop filter state.
  169. _z1 = _z2 = _z3 = 0;
  170. // Activate the ALSA thread,
  171. _commq->wr_int32 (Alsathread::PROC);
  172. _state = SYNC0;
  173. sendinfo (SYNC0, 0, 0);
  174. }
  175. void Jackclient::setloop (double bw)
  176. {
  177. double w;
  178. // Set the loop bandwidth to bw Hz.
  179. w = 6.28 * bw * _bsize / _fsamp;
  180. _w0 = 1.0 - exp (-20.0 * w);
  181. _w1 = w * 2 / _bsize;
  182. _w2 = w / 2;
  183. if (_mode == PLAY) _w1 /= _ratio;
  184. else _w1 *= _ratio;
  185. }
  186. void Jackclient::playback (int nframes)
  187. {
  188. int i, j, n;
  189. float *p, *q;
  190. float *inp [MAXCHAN];
  191. _bstat = _audioq->rd_avail ();
  192. for (i = 0; i < _nchan; i++)
  193. {
  194. inp [i] = (float *)(jack_port_get_buffer (_ports [i], nframes));
  195. }
  196. if (_resamp)
  197. {
  198. // Interleave inputs into _buff.
  199. for (i = 0; i < _nchan; i++)
  200. {
  201. p = inp [i];
  202. q = _buff + i;
  203. for (j = 0; j < _bsize; j++) q [j * _nchan] = p [j];
  204. }
  205. // Resample _buff and write to audio queue.
  206. // The while loop takes care of wraparound.
  207. _resamp->inp_count = _bsize;
  208. _resamp->inp_data = _buff;
  209. while (_resamp->inp_count)
  210. {
  211. _resamp->out_count = _audioq->wr_linav ();
  212. _resamp->out_data = _audioq->wr_datap ();
  213. n = _resamp->out_count;
  214. _resamp->process ();
  215. n -= _resamp->out_count;
  216. _audioq->wr_commit (n);
  217. }
  218. }
  219. else
  220. {
  221. // Interleave inputs into audio queue.
  222. // The while loop takes care of wraparound.
  223. while (nframes)
  224. {
  225. q = _audioq->wr_datap ();
  226. n = _audioq->wr_linav ();
  227. if (n > nframes) n = nframes;
  228. for (i = 0; i < _nchan; i++)
  229. {
  230. p = inp [i];
  231. for (j = 0; j < n; j++) q [j * _nchan] = p [j];
  232. inp [i] += n;
  233. q += 1;
  234. }
  235. _audioq->wr_commit (n);
  236. nframes -= n;
  237. }
  238. }
  239. }
  240. void Jackclient::capture (int nframes)
  241. {
  242. int i, j, n;
  243. float *p, *q;
  244. float *out [MAXCHAN];
  245. for (i = 0; i < _nchan; i++)
  246. {
  247. out [i] = (float *)(jack_port_get_buffer (_ports [i], nframes));
  248. }
  249. if (_resamp)
  250. {
  251. // Read from audio queue and resample.
  252. // The while loop takes care of wraparound.
  253. _resamp->out_count = _bsize;
  254. _resamp->out_data = _buff;
  255. while (_resamp->out_count)
  256. {
  257. _resamp->inp_count = _audioq->rd_linav ();
  258. _resamp->inp_data = _audioq->rd_datap ();
  259. n = _resamp->inp_count;
  260. _resamp->process ();
  261. n -= _resamp->inp_count;
  262. _audioq->rd_commit (n);
  263. }
  264. // Deinterleave _buff to outputs.
  265. for (i = 0; i < _nchan; i++)
  266. {
  267. p = _buff + i;
  268. q = out [i];
  269. for (j = 0; j < _bsize; j++) q [j] = p [j * _nchan];
  270. }
  271. }
  272. else
  273. {
  274. // Deinterleave audio queue to outputs.
  275. // The while loop takes care of wraparound.
  276. while (nframes)
  277. {
  278. p = _audioq->rd_datap ();
  279. n = _audioq->rd_linav ();
  280. if (n > nframes) n = nframes;
  281. for (i = 0; i < _nchan; i++)
  282. {
  283. q = out [i];
  284. for (j = 0; j < n; j++) q [j] = p [j * _nchan];
  285. out [i] += n;
  286. p += 1;
  287. }
  288. _audioq->rd_commit (n);
  289. nframes -= n;
  290. }
  291. }
  292. _bstat = _audioq->rd_avail ();
  293. }
  294. void Jackclient::silence (int nframes)
  295. {
  296. int i;
  297. float *q;
  298. // Write silence to all jack ports.
  299. for (i = 0; i < _nchan; i++)
  300. {
  301. q = (float *)(jack_port_get_buffer (_ports [i], nframes));
  302. memset (q, 0, nframes * sizeof (float));
  303. }
  304. }
  305. void Jackclient::sendinfo (int state, double error, double ratio)
  306. {
  307. Jdata *J;
  308. if (_infoq->wr_avail ())
  309. {
  310. J = _infoq->wr_datap ();
  311. J->_state = state;
  312. J->_error = error;
  313. J->_ratio = ratio;
  314. J->_bstat = _bstat;
  315. _infoq->wr_commit ();
  316. }
  317. }
  318. void Jackclient::jack_freewheel (int state)
  319. {
  320. _freew = state ? true : false;
  321. if (_freew) initwait (_ppsec / 4);
  322. }
  323. void Jackclient::jack_latency (jack_latency_callback_mode_t jlcm)
  324. {
  325. jack_latency_range_t R;
  326. int i;
  327. if (_state < WAIT) return;
  328. if (_mode == PLAY)
  329. {
  330. if (jlcm != JackPlaybackLatency) return;
  331. R.min = R.max = (int)(_delay / _ratio) + _ltcor;
  332. }
  333. else
  334. {
  335. if (jlcm != JackCaptureLatency) return;
  336. R.min = R.max = (int)(_delay * _ratio) + _ltcor;
  337. }
  338. for (i = 0; i < _nchan; i++)
  339. {
  340. jack_port_set_latency_range (_ports [i], jlcm, &R);
  341. }
  342. }
  343. int Jackclient::jack_process (int nframes)
  344. {
  345. int dk, n;
  346. Adata *D;
  347. jack_time_t t0, t1;
  348. jack_nframes_t ft;
  349. float us;
  350. double tj, err, d1, d2, rd;
  351. // Buffer size change or other evil.
  352. if (_state == TERM)
  353. {
  354. sendinfo (TERM, 0, 0);
  355. return 0;
  356. }
  357. // Skip cylce if ports may not yet exist.
  358. if (_state < WAIT) return 0;
  359. // Start synchronisation 1/2 second after entering
  360. // the WAIT state. This delay allows the ALSA thread
  361. // to restart cleanly if necessary. Disabled while
  362. // freewheeling.
  363. if (_state == WAIT)
  364. {
  365. if (_freew) return 0;
  366. if (_mode == CAPT) silence (nframes);
  367. if (++_count == 0) initsync ();
  368. else return 0;
  369. }
  370. // Get the start time of the current cycle.
  371. jack_get_cycle_times (_client, &ft, &t0, &t1, &us);
  372. tj = tjack (t0);
  373. // Check for any skipped cycles.
  374. if (_state >= SYNC1)
  375. {
  376. dk = ft - _ft - _bsize;
  377. if (_mode == PLAY)
  378. {
  379. dk = (int)(dk * _ratio + 0.5);
  380. _audioq->wr_commit (dk);
  381. }
  382. else
  383. {
  384. dk = (int)(dk / _ratio + 0.5);
  385. _audioq->rd_commit (dk);
  386. }
  387. }
  388. _ft = ft;
  389. // Check if we have timing data from the ALSA thread.
  390. n = _alsaq->rd_avail ();
  391. // If the data queue is full restart synchronisation.
  392. // This can happen e.g. on a jack engine timeout, or
  393. // when too many cycles have been skipped.
  394. if (n == _alsaq->size ())
  395. {
  396. initwait (_ppsec / 2);
  397. return 0;
  398. }
  399. if (n)
  400. {
  401. // Else move interval end to start, and update the
  402. // interval end keeping only the most recent data.
  403. if (_state < SYNC2) _state++;
  404. _t_a0 = _t_a1;
  405. _k_a0 = _k_a1;
  406. while (_alsaq->rd_avail ())
  407. {
  408. D = _alsaq->rd_datap ();
  409. // Restart synchronisation in case of
  410. // an error in the ALSA interface.
  411. if (D->_state == Alsathread::WAIT)
  412. {
  413. initwait (_ppsec / 2);
  414. return 0;
  415. }
  416. _t_a1 = D->_timer;
  417. _k_a1 += D->_nsamp;
  418. _alsaq->rd_commit ();
  419. }
  420. }
  421. err = 0;
  422. if (_state >= SYNC2)
  423. {
  424. // Compute the delay error.
  425. d1 = tjack_diff (tj, _t_a0);
  426. d2 = tjack_diff (_t_a1, _t_a0);
  427. rd = _resamp ? _resamp->inpdist () : 0.0;
  428. if (_mode == PLAY)
  429. {
  430. n = _audioq->nwr () - _k_a0; // Must be done as integer as both terms will overflow.
  431. err = n - (_k_a1 - _k_a0) * d1 / d2 + rd * _ratio - _delay;
  432. }
  433. else
  434. {
  435. n = _k_a0 - _audioq->nrd (); // Must be done as integer as both terms will overflow.
  436. err = n + (_k_a1 - _k_a0) * d1 / d2 + rd - _delay ;
  437. }
  438. n = (int)(floor (err + 0.5));
  439. if (_state == SYNC2)
  440. {
  441. // We have the first delay error value. Adjust the audio
  442. // queue to obtain the actually wanted delay, and start
  443. // tracking.
  444. if (_mode == PLAY) _audioq->wr_commit (-n);
  445. else _audioq->rd_commit (n);
  446. err -= n;
  447. setloop (1.0);
  448. _state = PROC1;
  449. }
  450. }
  451. // Switch to lower bandwidth after 4 seconds.
  452. if ((_state == PROC1) && (++_count == 4 * _ppsec))
  453. {
  454. _state = PROC2;
  455. setloop (0.05);
  456. }
  457. if (_state >= PROC1)
  458. {
  459. _z1 += _w0 * (_w1 * err - _z1);
  460. _z2 += _w0 * (_z1 - _z2);
  461. _z3 += _w2 * _z2;
  462. // Check error conditions.
  463. if (fabs (_z3) > 0.05)
  464. {
  465. // Something is really wrong, wait 10 seconds then restart.
  466. initwait (10 * _ppsec);
  467. return 0;
  468. }
  469. // Run loop filter and set resample ratio.
  470. if (_resamp)
  471. {
  472. _rcorr = 1 - (_z2 + _z3);
  473. if (_rcorr > 1.05) _rcorr = 1.05;
  474. if (_rcorr < 0.95) _rcorr = 0.95;
  475. _resamp->set_rratio (_rcorr);
  476. }
  477. sendinfo (_state, err, _rcorr);
  478. // Resample and transfer between audio
  479. // queue and jack ports.
  480. if (_mode == PLAY) playback (nframes);
  481. else capture (nframes);
  482. }
  483. else if (_mode == CAPT) silence (nframes);
  484. return 0;
  485. }