1 // ---------------------------------------------------------------------------- 2 // 3 // Copyright (C) 2013-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 20 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <math.h> 24 #include "netdata.h" 25 #include "jackrx.h" 26 #include "timers.h" 27 #include "netrx.h" 28 29 30 Jackrx::Jackrx (const char *jname, const char*jserv, int nchan, const int *clist) : 31 _client (0), 32 _nchan (nchan), 33 _state (INIT), 34 _freew (false) 35 { 36 init (jname, jserv, clist); 37 } 38 39 40 Jackrx::~Jackrx (void) 41 { 42 fini (); 43 } 44 45 46 void Jackrx::init (const char *jname, const char *jserv, const int *clist) 47 { 48 int i, opts, spol, flags; 49 char s [16]; 50 jack_status_t stat; 51 struct sched_param spar; 52 53 opts = JackNoStartServer; 54 if (jserv) opts |= JackServerName; 55 _client = jack_client_open (jname, (jack_options_t) opts, &stat, jserv); 56 if (_client == 0) 57 { 58 fprintf (stderr, "Can't connect to Jack, is the server running ?\n"); 59 exit (1); 60 } 61 jack_set_process_callback (_client, jack_static_process, (void *) this); 62 jack_set_latency_callback (_client, jack_static_latency, (void *) this); 63 jack_set_freewheel_callback (_client, jack_static_freewheel, (void *) this); 64 jack_set_buffer_size_callback (_client, jack_static_buffsize, (void *) this); 65 jack_on_shutdown (_client, jack_static_shutdown, (void *) this); 66 67 _bsize = 0; 68 _fsamp = 0; 69 if (jack_activate (_client)) 70 { 71 fprintf(stderr, "Can't activate Jack"); 72 exit (1); 73 } 74 _jname = jack_get_client_name (_client); 75 _bsize = jack_get_buffer_size (_client); 76 _fsamp = jack_get_sample_rate (_client); 77 78 flags = JackPortIsTerminal | JackPortIsPhysical; 79 if (_nchan > Netdata::MAXCHAN) _nchan = Netdata::MAXCHAN; 80 for (i = 0; i < _nchan; i++) 81 { 82 sprintf (s, "out_%d", clist [i] + 1); 83 _ports [i] = jack_port_register (_client, s, JACK_DEFAULT_AUDIO_TYPE, 84 flags | JackPortIsOutput, 0); 85 } 86 pthread_getschedparam (jack_client_thread_id (_client), &spol, &spar); 87 _rprio = spar.sched_priority; 88 _buff = new float [_bsize * _nchan]; 89 _state = IDLE; 90 } 91 92 93 void Jackrx::fini (void) 94 { 95 if (_client) 96 { 97 jack_deactivate (_client); 98 jack_client_close (_client); 99 } 100 delete[] _buff; 101 } 102 103 104 void Jackrx::jack_static_shutdown (void *arg) 105 { 106 ((Jackrx *) arg)->sendinfo (FATAL, 0, 0, 0); 107 } 108 109 110 int Jackrx::jack_static_buffsize (jack_nframes_t bsize, void *arg) 111 { 112 ((Jackrx *) arg)->jack_buffsize (bsize); 113 return 0; 114 } 115 116 117 void Jackrx::jack_static_freewheel (int state, void *arg) 118 { 119 ((Jackrx *) arg)->jack_freewheel (state); 120 } 121 122 123 void Jackrx::jack_static_latency (jack_latency_callback_mode_t jlcm, void *arg) 124 { 125 } 126 127 128 int Jackrx::jack_static_process (jack_nframes_t nframes, void *arg) 129 { 130 return ((Jackrx *) arg)->jack_process (nframes); 131 } 132 133 134 void Jackrx::start (Lfq_audio *audioq, 135 Lfq_int32 *commq, 136 Lfq_timedata *timeq, 137 Lfq_infodata *infoq, 138 double ratio, 139 int delay, 140 int rqual) 141 { 142 _audioq = audioq; 143 _commq = commq; 144 _timeq = timeq; 145 _infoq = infoq; 146 _ratio = ratio; 147 _rcorr = 1.0; 148 _resamp.setup (_ratio, _nchan, rqual); 149 _resamp.set_rrfilt (100); 150 _delay = delay; 151 _ppsec = (_fsamp + _bsize / 2) / _bsize; 152 _first = true; 153 _tnext = 0; 154 _limit = (int)(_fsamp / _ratio); 155 initwait (_ppsec / 2); 156 } 157 158 159 void Jackrx::initwait (int nwait) 160 { 161 _count = -nwait; 162 _commq->wr_int32 (Netrx::WAIT); 163 _state = WAIT; 164 if (nwait > _ppsec) sendinfo (_state, (double) nwait / _ppsec, 0, 0); 165 } 166 167 168 void Jackrx::initsync (void) 169 { 170 // Reset all lock-free queues. 171 _commq->reset (); 172 _timeq->reset (); 173 _audioq->reset (); 174 // Reset and prefill the resampler. 175 _resamp.reset (); 176 _resamp.inp_count = _resamp.inpsize () / 2 - 1; 177 _resamp.out_count = 10000; 178 _resamp.process (); 179 // Initiliase state variables. 180 _first = true; 181 _t_a0 = _t_a1 = 0; 182 _k_a0 = _k_a1 = 0; 183 // Initialise loop filter state. 184 _z1 = _z2 = _z3 = 0; 185 // Activate the netrx thread, 186 _commq->wr_int32 (Netrx::PROC); 187 _state = SYNC0; 188 sendinfo (_state, 0, 0, 0); 189 } 190 191 192 void Jackrx::setloop (double bw) 193 { 194 double w; 195 196 // Set the loop bandwidth to bw Hz. 197 w = 6.28 * bw * _bsize / _fsamp; 198 _w0 = 1.0 - exp (-20.0 * w); 199 _w1 = w * 2.0 * _ratio / _bsize; 200 _w2 = w / 2.0; 201 } 202 203 204 void Jackrx::capture (int nframes) 205 { 206 int i, j, k1, k2; 207 float *p, *q; 208 209 // Read from audio queue and resample. 210 // The while loop takes care of wraparound. 211 _resamp.out_count = _bsize; 212 _resamp.out_data = _buff; 213 while (_resamp.out_count) 214 { 215 // Allow the audio queue to underrun, but 216 // use zero valued samples in that case. 217 // This will happen when the sender skips 218 // some cycles and the receiver is not 219 // configured for additional latency. 220 k1 = _audioq->rd_avail (); 221 k2 = _audioq->rd_linav (); 222 if (k1 > 0) 223 { 224 _resamp.inp_count = (k1 < k2) ? k1 : k2; 225 _resamp.inp_data = _audioq->rd_datap (); 226 } 227 else 228 { 229 _resamp.inp_count = 999999; 230 _resamp.inp_data = 0; 231 } 232 // Resample up to a full output buffer. 233 k1 = _resamp.inp_count; 234 _resamp.process (); 235 k1 -= _resamp.inp_count; 236 // Adjust audio queue and state by the 237 // number of frames consumed. 238 _audioq->rd_commit (k1); 239 } 240 // Deinterleave _buff to outputs. 241 for (j = 0; j < _nchan; j++) 242 { 243 p = _buff + j; 244 q = (float *)(jack_port_get_buffer (_ports [j], nframes)); 245 for (i = 0; i < _bsize; i++) q [i] = p [i * _nchan]; 246 } 247 } 248 249 250 void Jackrx::silence (int nframes) 251 { 252 int i; 253 float *q; 254 255 // Write silence to all jack ports. 256 for (i = 0; i < _nchan; i++) 257 { 258 q = (float *)(jack_port_get_buffer (_ports [i], nframes)); 259 memset (q, 0, nframes * sizeof (float)); 260 } 261 } 262 263 264 void Jackrx::sendinfo (int state, double error, double ratio, int nfram) 265 { 266 Infodata *I; 267 268 if (_infoq->wr_avail ()) 269 { 270 I = _infoq->wr_datap (); 271 I->_state = state; 272 I->_error = error; 273 I->_ratio = ratio; 274 I->_nfram = nfram; 275 I->_syncc = 0; 276 _infoq->wr_commit (); 277 } 278 } 279 280 281 void Jackrx::jack_freewheel (int yesno) 282 { 283 _freew = yesno ? true : false; 284 if (_freew) initwait (_ppsec / 4); 285 } 286 287 288 void Jackrx::jack_buffsize (int bsize) 289 { 290 if (_bsize == 0) _bsize = bsize; 291 else if (_bsize != bsize) _state = Jackrx::FATAL; 292 } 293 294 295 int Jackrx::jack_process (int nframes) 296 { 297 int k, nskip; 298 double d1, d2, err; 299 jack_time_t t0, t1; 300 jack_nframes_t ft; 301 float usecs; 302 bool shift; 303 Timedata *D; 304 305 // Skip cylce if ports may not yet exist. 306 if (_state < IDLE) return 0; 307 308 // Buffer size change, no data, or other evil. 309 if (_state >= TXEND) 310 { 311 sendinfo (_state, 0, 0, 0); 312 _state = IDLE; 313 return 0; 314 } 315 // Output silence if idle. 316 if (_state < WAIT) 317 { 318 silence (nframes); 319 return 0; 320 } 321 322 // Start synchronisation 1/2 second after entering 323 // the WAIT state. Disabled while freewheeling. 324 if (_state == WAIT) 325 { 326 silence (nframes); 327 if (_freew) return 0; 328 if (++_count == 0) initsync (); 329 else return 0; 330 } 331 332 // Get local timing info. 333 jack_get_cycle_times (_client, &ft, &t0, &t1, &usecs); 334 _t_j0 = tjack (t0); 335 336 if (_first) 337 { 338 _first = false; 339 nskip = 0; 340 } 341 else 342 { 343 usecs = (float)(t0 - _tnext); 344 nskip = (int)(1e-6f * usecs * _fsamp / _ratio + 0.5f); 345 } 346 _tnext = t1; 347 _audioq->rd_commit (nskip); 348 349 // Check if we have info from the netrx thread. 350 // If the queue is full restart synchronisation. 351 // This can happen e.g. on a jack engine timeout, 352 // or when too many cycles have been skipped. 353 if (_timeq->rd_avail () >= _timeq->nelm ()) 354 { 355 initwait (_ppsec / 2); 356 return 0; 357 } 358 shift = true; 359 while (_timeq->rd_avail ()) 360 { 361 D = _timeq->rd_datap (); 362 switch (D->_flags) 363 { 364 case Netrx::WAIT: 365 // Restart synchronisation in case the netrx 366 // thread signals a problem. This will happen 367 // when the sender goes into freewheeling mode. 368 initwait (_ppsec / 2); 369 return 0; 370 case Netrx::PROC: 371 // Frame count and reception time stamp. 372 if (shift) 373 { 374 shift = false; 375 _t_a0 = _t_a1; 376 _k_a0 = _k_a1; 377 if (_state < SYNC2) 378 { 379 _state++; 380 sendinfo (_state, 0, 0, 0); 381 } 382 } 383 _k_a1 = D->_count; 384 _t_a1 = D->_tjack; 385 break; 386 case Netrx::TERM: 387 // Sender terminated. 388 _state = TXEND; 389 return 0; 390 case Netrx::FAIL: 391 // Fatal error in netrx thread. 392 _state = FATAL; 393 return 0; 394 } 395 _timeq->rd_commit (); 396 } 397 398 err = 0; 399 if (_state >= SYNC2) 400 { 401 // Compute the delay error. 402 d1 = tjack_diff (_t_j0, _t_a0); 403 d2 = tjack_diff (_t_a1, _t_a0); 404 // This must be done as integer as both terms will overflow. 405 k = _k_a0 - _audioq->nrd (); 406 err = k + (_k_a1 - _k_a0) * d1 / d2 + _resamp.inpdist () - _delay; 407 if (_state == SYNC2) 408 { 409 // We have the first delay error value. Adjust the audio queue 410 // to obtain the actually wanted delay, and start tracking. 411 k = (int)(floor (err + 0.5)); 412 _audioq->rd_commit (k); 413 err -= k; 414 setloop (0.5); 415 _state = PROC1; 416 } 417 } 418 419 // Switch to lower bandwidth after 4 seconds. 420 if ((_state == PROC1) && (++_count == 4 * _ppsec)) 421 { 422 _state = PROC2; 423 setloop (0.05); 424 } 425 426 if (_state >= PROC1) 427 { 428 // Run loop filter and set resample ratio. 429 _z1 += _w0 * (_w1 * err - _z1); 430 _z2 += _w0 * (_z1 - _z2); 431 _z3 += _w2 * _z2; 432 if (fabs (_z3) > 0.05) 433 { 434 // Something is really wrong. 435 // Wait 10 seconds then restart. 436 initwait (10 * _ppsec); 437 return 0; 438 } 439 _rcorr = 1 - (_z2 + _z3); 440 if (_rcorr > 1.05) _rcorr = 1.05; 441 if (_rcorr < 0.95) _rcorr = 0.95; 442 _resamp.set_rratio (_rcorr); 443 444 // Resample and transfer between audio 445 // queue and jack ports. 446 capture (nframes); 447 k = _audioq->rd_avail (); 448 sendinfo (_state, err, _rcorr, k); 449 if (k < -_limit) _state = TXEND; 450 } 451 else silence (nframes); 452 453 return 0; 454 } 455 456 457