jack1 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.

757 lines
21KB

  1. /** @file simple_client.c
  2. *
  3. * @brief This simple client demonstrates the basic features of JACK
  4. * as they would be used by many applications.
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <math.h>
  13. #include <jack/jack.h>
  14. #include <jack/jslist.h>
  15. #include <jack/memops.h>
  16. #include "alsa/asoundlib.h"
  17. #include <samplerate.h>
  18. // Here are the lists of the jack ports...
  19. JSList *capture_ports = NULL;
  20. JSList *capture_srcs = NULL;
  21. JSList *playback_ports = NULL;
  22. JSList *playback_srcs = NULL;
  23. jack_client_t *client;
  24. snd_pcm_t *alsa_handle;
  25. int jack_sample_rate;
  26. int jack_buffer_size;
  27. int quit = 0;
  28. double resample_mean = 1.0;
  29. double static_resample_factor = 1.0;
  30. double *offset_array;
  31. double *window_array;
  32. int offset_differential_index = 0;
  33. double offset_integral = 0;
  34. // ------------------------------------------------------ commandline parameters
  35. int sample_rate = 0; /* stream rate */
  36. int num_channels = 2; /* count of channels */
  37. int period_size = 1024;
  38. int num_periods = 2;
  39. int target_delay = 0; /* the delay which the program should try to approach. */
  40. int max_diff = 0; /* the diff value, when a hard readpointer skip should occur */
  41. int catch_factor = 100000;
  42. int catch_factor2 = 10000;
  43. double pclamp = 15.0;
  44. double controlquant = 10000.0;
  45. int smooth_size = 256;
  46. int good_window=0;
  47. int verbose = 0;
  48. int instrument = 0;
  49. int samplerate_quality = 2;
  50. // Debug stuff:
  51. volatile float output_resampling_factor = 1.0;
  52. volatile int output_new_delay = 0;
  53. volatile float output_offset = 0.0;
  54. volatile float output_integral = 0.0;
  55. volatile float output_diff = 0.0;
  56. snd_pcm_uframes_t real_buffer_size;
  57. snd_pcm_uframes_t real_period_size;
  58. // format selection, and corresponding functions from memops in a nice set of structs.
  59. typedef struct alsa_format {
  60. snd_pcm_format_t format_id;
  61. size_t sample_size;
  62. void (*jack_to_soundcard) (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state);
  63. void (*soundcard_to_jack) (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip);
  64. const char *name;
  65. } alsa_format_t;
  66. alsa_format_t formats[] = {
  67. { SND_PCM_FORMAT_FLOAT_LE, 4, sample_move_dS_floatLE, sample_move_floatLE_sSs, "float" },
  68. { SND_PCM_FORMAT_S32, 4, sample_move_d32u24_sS, sample_move_dS_s32u24, "32bit" },
  69. { SND_PCM_FORMAT_S24_3LE, 3, sample_move_d24_sS, sample_move_dS_s24, "24bit - real" },
  70. { SND_PCM_FORMAT_S24, 4, sample_move_d24_sS, sample_move_dS_s24, "24bit" },
  71. { SND_PCM_FORMAT_S16, 2, sample_move_d16_sS, sample_move_dS_s16, "16bit" }
  72. };
  73. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  74. int format=0;
  75. // Alsa stuff... i dont want to touch this bullshit in the next years.... please...
  76. static int xrun_recovery(snd_pcm_t *handle, int err) {
  77. // printf( "xrun !!!.... %d\n", err );
  78. if (err == -EPIPE) { /* under-run */
  79. err = snd_pcm_prepare(handle);
  80. if (err < 0)
  81. printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err));
  82. return 0;
  83. } else if (err == -EAGAIN) {
  84. while ((err = snd_pcm_resume(handle)) == -EAGAIN)
  85. usleep(100); /* wait until the suspend flag is released */
  86. if (err < 0) {
  87. err = snd_pcm_prepare(handle);
  88. if (err < 0)
  89. printf("Can't recovery from suspend, prepare failed: %s\n", snd_strerror(err));
  90. }
  91. return 0;
  92. }
  93. return err;
  94. }
  95. static int set_hwformat( snd_pcm_t *handle, snd_pcm_hw_params_t *params )
  96. {
  97. int i;
  98. int err;
  99. for( i=0; i<NUMFORMATS; i++ ) {
  100. /* set the sample format */
  101. err = snd_pcm_hw_params_set_format(handle, params, formats[i].format_id);
  102. if (err == 0) {
  103. format = i;
  104. return 0;
  105. }
  106. }
  107. return err;
  108. }
  109. static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, snd_pcm_access_t access, int rate, int channels, int period, int nperiods ) {
  110. int err, dir=0;
  111. unsigned int buffer_time;
  112. unsigned int period_time;
  113. unsigned int rrate;
  114. unsigned int rchannels;
  115. /* choose all parameters */
  116. err = snd_pcm_hw_params_any(handle, params);
  117. if (err < 0) {
  118. printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
  119. return err;
  120. }
  121. /* set the interleaved read/write format */
  122. err = snd_pcm_hw_params_set_access(handle, params, access);
  123. if (err < 0) {
  124. printf("Access type not available for playback: %s\n", snd_strerror(err));
  125. return err;
  126. }
  127. /* set the sample format */
  128. err = set_hwformat(handle, params);
  129. if (err < 0) {
  130. printf("Sample format not available for playback: %s\n", snd_strerror(err));
  131. return err;
  132. }
  133. /* set the count of channels */
  134. rchannels = channels;
  135. err = snd_pcm_hw_params_set_channels_near(handle, params, &rchannels);
  136. if (err < 0) {
  137. printf("Channels count (%i) not available for record: %s\n", channels, snd_strerror(err));
  138. return err;
  139. }
  140. if (rchannels != channels) {
  141. printf("WARNING: chennel count does not match (requested %d got %d)\n", channels, rchannels);
  142. num_channels = rchannels;
  143. }
  144. /* set the stream rate */
  145. rrate = rate;
  146. err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
  147. if (err < 0) {
  148. printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
  149. return err;
  150. }
  151. if (rrate != rate) {
  152. printf("WARNING: Rate doesn't match (requested %iHz, get %iHz)\n", rate, rrate);
  153. sample_rate = rrate;
  154. }
  155. /* set the buffer time */
  156. buffer_time = 1000000*period*nperiods/rate;
  157. err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
  158. if (err < 0) {
  159. printf("Unable to set buffer time %i for playback: %s\n", 1000000*period*nperiods/rate, snd_strerror(err));
  160. return err;
  161. }
  162. err = snd_pcm_hw_params_get_buffer_size( params, &real_buffer_size );
  163. if (err < 0) {
  164. printf("Unable to get buffer size back: %s\n", snd_strerror(err));
  165. return err;
  166. }
  167. if( real_buffer_size != nperiods * period ) {
  168. printf( "WARNING: buffer size does not match: (requested %d, got %d)\n", nperiods * period, (int) real_buffer_size );
  169. }
  170. /* set the period time */
  171. period_time = 1000000*period/rate;
  172. err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
  173. if (err < 0) {
  174. printf("Unable to set period time %i for playback: %s\n", 1000000*period/rate, snd_strerror(err));
  175. return err;
  176. }
  177. err = snd_pcm_hw_params_get_period_size(params, &real_period_size, NULL );
  178. if (err < 0) {
  179. printf("Unable to get period size back: %s\n", snd_strerror(err));
  180. return err;
  181. }
  182. if( real_period_size != period ) {
  183. printf( "WARNING: period size does not match: (requested %i, got %i)\n", period, (int)real_period_size );
  184. }
  185. /* write the parameters to device */
  186. err = snd_pcm_hw_params(handle, params);
  187. if (err < 0) {
  188. printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
  189. return err;
  190. }
  191. return 0;
  192. }
  193. static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams, int period) {
  194. int err;
  195. /* get the current swparams */
  196. err = snd_pcm_sw_params_current(handle, swparams);
  197. if (err < 0) {
  198. printf("Unable to determine current swparams for capture: %s\n", snd_strerror(err));
  199. return err;
  200. }
  201. /* start the transfer when the buffer is full */
  202. err = snd_pcm_sw_params_set_start_threshold(handle, swparams, period );
  203. if (err < 0) {
  204. printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
  205. return err;
  206. }
  207. err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, -1 );
  208. if (err < 0) {
  209. printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
  210. return err;
  211. }
  212. /* allow the transfer when at least period_size samples can be processed */
  213. err = snd_pcm_sw_params_set_avail_min(handle, swparams, 2*period );
  214. if (err < 0) {
  215. printf("Unable to set avail min for capture: %s\n", snd_strerror(err));
  216. return err;
  217. }
  218. /* align all transfers to 1 sample */
  219. err = snd_pcm_sw_params_set_xfer_align(handle, swparams, 1);
  220. if (err < 0) {
  221. printf("Unable to set transfer align for capture: %s\n", snd_strerror(err));
  222. return err;
  223. }
  224. /* write the parameters to the playback device */
  225. err = snd_pcm_sw_params(handle, swparams);
  226. if (err < 0) {
  227. printf("Unable to set sw params for capture: %s\n", snd_strerror(err));
  228. return err;
  229. }
  230. return 0;
  231. }
  232. // ok... i only need this function to communicate with the alsa bloat api...
  233. static snd_pcm_t *open_audiofd( char *device_name, int capture, int rate, int channels, int period, int nperiods ) {
  234. int err;
  235. snd_pcm_t *handle;
  236. snd_pcm_hw_params_t *hwparams;
  237. snd_pcm_sw_params_t *swparams;
  238. snd_pcm_hw_params_alloca(&hwparams);
  239. snd_pcm_sw_params_alloca(&swparams);
  240. if ((err = snd_pcm_open(&(handle), device_name, capture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK )) < 0) {
  241. printf("Capture open error: %s\n", snd_strerror(err));
  242. return NULL;
  243. }
  244. if ((err = set_hwparams(handle, hwparams,SND_PCM_ACCESS_RW_INTERLEAVED, rate, channels, period, nperiods )) < 0) {
  245. printf("Setting of hwparams failed: %s\n", snd_strerror(err));
  246. return NULL;
  247. }
  248. if ((err = set_swparams(handle, swparams, period)) < 0) {
  249. printf("Setting of swparams failed: %s\n", snd_strerror(err));
  250. return NULL;
  251. }
  252. snd_pcm_start( handle );
  253. snd_pcm_wait( handle, 200 );
  254. return handle;
  255. }
  256. double hann( double x )
  257. {
  258. return 0.5 * (1.0 - cos( 2*M_PI * x ) );
  259. }
  260. /**
  261. * The process callback for this JACK application.
  262. * It is called by JACK at the appropriate times.
  263. */
  264. int process (jack_nframes_t nframes, void *arg) {
  265. char *outbuf;
  266. float *resampbuf;
  267. int rlen;
  268. int err;
  269. snd_pcm_sframes_t delay = target_delay;
  270. int put_back_samples=0;
  271. int i;
  272. delay = snd_pcm_avail( alsa_handle );
  273. delay -= jack_frames_since_cycle_start( client );
  274. // Do it the hard way.
  275. // this is for compensating xruns etc...
  276. if( delay > (target_delay+max_diff) ) {
  277. char *tmp = alloca( (delay-target_delay) * formats[format].sample_size * num_channels );
  278. snd_pcm_readi( alsa_handle, tmp, delay-target_delay );
  279. output_new_delay = (int) delay;
  280. delay = target_delay;
  281. // Set the resample_rate... we need to adjust the offset integral, to do this.
  282. // first look at the PI controller, this code is just a special case, which should never execute once
  283. // everything is swung in.
  284. offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
  285. // Also clear the array. we are beginning a new control cycle.
  286. for( i=0; i<smooth_size; i++ )
  287. offset_array[i] = 0.0;
  288. }
  289. if( delay < (target_delay-max_diff) ) {
  290. snd_pcm_rewind( alsa_handle, target_delay - delay );
  291. output_new_delay = (int) delay;
  292. delay = target_delay;
  293. // Set the resample_rate... we need to adjust the offset integral, to do this.
  294. offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
  295. // Also clear the array. we are beginning a new control cycle.
  296. for( i=0; i<smooth_size; i++ )
  297. offset_array[i] = 0.0;
  298. }
  299. /* ok... now we should have target_delay +- max_diff on the alsa side.
  300. *
  301. * calculate the number of frames, we want to get.
  302. */
  303. double offset = delay - target_delay;
  304. // Save offset.
  305. offset_array[(offset_differential_index++)% smooth_size ] = offset;
  306. // Build the mean of the windowed offset array
  307. // basically fir lowpassing.
  308. double smooth_offset = 0.0;
  309. for( i=0; i<smooth_size; i++ )
  310. smooth_offset +=
  311. offset_array[ (i + offset_differential_index-1) % smooth_size] * window_array[i];
  312. smooth_offset /= (double) smooth_size;
  313. // this is the integral of the smoothed_offset
  314. offset_integral += smooth_offset;
  315. // Clamp offset.
  316. // the smooth offset still contains unwanted noise
  317. // which would go straigth onto the resample coeff.
  318. // it only used in the P component and the I component is used for the fine tuning anyways.
  319. if( fabs( smooth_offset ) < pclamp )
  320. smooth_offset = 0.0;
  321. // ok. now this is the PI controller.
  322. // u(t) = K * ( e(t) + 1/T \int e(t') dt' )
  323. // K = 1/catch_factor and T = catch_factor2
  324. double current_resample_factor = static_resample_factor - smooth_offset / (double) catch_factor - offset_integral / (double) catch_factor / (double)catch_factor2;
  325. // now quantize this value around resample_mean, so that the noise which is in the integral component doesnt hurt.
  326. current_resample_factor = floor( (current_resample_factor - resample_mean) * controlquant + 0.5 ) / controlquant + resample_mean;
  327. // Output "instrumentatio" gonna change that to real instrumentation in a few.
  328. output_resampling_factor = (float) current_resample_factor;
  329. output_diff = (float) smooth_offset;
  330. output_integral = (float) offset_integral;
  331. output_offset = (float) offset;
  332. // Clamp a bit.
  333. if( current_resample_factor < 0.25 ) current_resample_factor = 0.25;
  334. if( current_resample_factor > 4 ) current_resample_factor = 4;
  335. // Now Calculate how many samples we need.
  336. rlen = ceil( ((double)nframes) / current_resample_factor )+2;
  337. assert( rlen > 2 );
  338. // Calculate resample_mean so we can init ourselves to saner values.
  339. resample_mean = 0.9999 * resample_mean + 0.0001 * current_resample_factor;
  340. /*
  341. * now this should do it...
  342. */
  343. outbuf = alloca( rlen * formats[format].sample_size * num_channels );
  344. resampbuf = alloca( rlen * sizeof( float ) );
  345. // get the data...
  346. again:
  347. err = snd_pcm_readi(alsa_handle, outbuf, rlen);
  348. if( err < 0 ) {
  349. printf( "err = %d\n", err );
  350. if (xrun_recovery(alsa_handle, err) < 0) {
  351. //printf("Write error: %s\n", snd_strerror(err));
  352. //exit(EXIT_FAILURE);
  353. }
  354. goto again;
  355. }
  356. if( err != rlen ) {
  357. //printf( "read = %d\n", rlen );
  358. }
  359. /*
  360. * render jack ports to the outbuf...
  361. */
  362. int chn = 0;
  363. JSList *node = capture_ports;
  364. JSList *src_node = capture_srcs;
  365. SRC_DATA src;
  366. while ( node != NULL)
  367. {
  368. jack_port_t *port = (jack_port_t *) node->data;
  369. float *buf = jack_port_get_buffer (port, nframes);
  370. SRC_STATE *src_state = src_node->data;
  371. formats[format].soundcard_to_jack( resampbuf, outbuf + format[formats].sample_size * chn, rlen, num_channels*format[formats].sample_size );
  372. src.data_in = resampbuf;
  373. src.input_frames = rlen;
  374. src.data_out = buf;
  375. src.output_frames = nframes;
  376. src.end_of_input = 0;
  377. src.src_ratio = current_resample_factor;
  378. src_process( src_state, &src );
  379. put_back_samples = rlen-src.input_frames_used;
  380. src_node = jack_slist_next (src_node);
  381. node = jack_slist_next (node);
  382. chn++;
  383. }
  384. // Put back the samples libsamplerate did not consume.
  385. //printf( "putback = %d\n", put_back_samples );
  386. snd_pcm_rewind( alsa_handle, put_back_samples );
  387. return 0;
  388. }
  389. /**
  390. * Allocate the necessary jack ports...
  391. */
  392. void alloc_ports( int n_capture, int n_playback ) {
  393. int port_flags = JackPortIsOutput;
  394. int chn;
  395. jack_port_t *port;
  396. char buf[32];
  397. capture_ports = NULL;
  398. for (chn = 0; chn < n_capture; chn++)
  399. {
  400. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn+1);
  401. port = jack_port_register (client, buf,
  402. JACK_DEFAULT_AUDIO_TYPE,
  403. port_flags, 0);
  404. if (!port)
  405. {
  406. printf( "jacknet_client: cannot register port for %s", buf);
  407. break;
  408. }
  409. capture_srcs = jack_slist_append( capture_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
  410. capture_ports = jack_slist_append (capture_ports, port);
  411. }
  412. port_flags = JackPortIsInput;
  413. playback_ports = NULL;
  414. for (chn = 0; chn < n_playback; chn++)
  415. {
  416. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn+1);
  417. port = jack_port_register (client, buf,
  418. JACK_DEFAULT_AUDIO_TYPE,
  419. port_flags, 0);
  420. if (!port)
  421. {
  422. printf( "jacknet_client: cannot register port for %s", buf);
  423. break;
  424. }
  425. playback_srcs = jack_slist_append( playback_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
  426. playback_ports = jack_slist_append (playback_ports, port);
  427. }
  428. }
  429. /**
  430. * This is the shutdown callback for this JACK application.
  431. * It is called by JACK if the server ever shuts down or
  432. * decides to disconnect the client.
  433. */
  434. void jack_shutdown (void *arg) {
  435. exit (1);
  436. }
  437. /**
  438. * be user friendly.
  439. * be user friendly.
  440. * be user friendly.
  441. */
  442. void printUsage() {
  443. fprintf(stderr, "usage: alsa_out [options]\n"
  444. "\n"
  445. " -j <jack name> - client name\n"
  446. " -d <alsa_device> \n"
  447. " -c <channels> \n"
  448. " -p <period_size> \n"
  449. " -n <num_period> \n"
  450. " -r <sample_rate> \n"
  451. " -q <sample_rate quality [0..4]\n"
  452. " -m <max_diff> \n"
  453. " -t <target_delay> \n"
  454. " -i turns on instrumentation\n"
  455. " -v turns on printouts\n"
  456. "\n");
  457. }
  458. /**
  459. * the main function....
  460. */
  461. void
  462. sigterm_handler( int signal )
  463. {
  464. quit = 1;
  465. }
  466. int main (int argc, char *argv[]) {
  467. char jack_name[30] = "alsa_in";
  468. char alsa_device[30] = "hw:0";
  469. extern char *optarg;
  470. extern int optind, optopt;
  471. int errflg=0;
  472. int c;
  473. while ((c = getopt(argc, argv, "ivj:r:c:p:n:d:q:m:t:f:F:C:Q:s:")) != -1) {
  474. switch(c) {
  475. case 'j':
  476. strcpy(jack_name,optarg);
  477. break;
  478. case 'r':
  479. sample_rate = atoi(optarg);
  480. break;
  481. case 'c':
  482. num_channels = atoi(optarg);
  483. break;
  484. case 'p':
  485. period_size = atoi(optarg);
  486. break;
  487. case 'n':
  488. num_periods = atoi(optarg);
  489. break;
  490. case 'd':
  491. strcpy(alsa_device,optarg);
  492. break;
  493. case 't':
  494. target_delay = atoi(optarg);
  495. break;
  496. case 'q':
  497. samplerate_quality = atoi(optarg);
  498. break;
  499. case 'm':
  500. max_diff = atoi(optarg);
  501. break;
  502. case 'f':
  503. catch_factor = atoi(optarg);
  504. break;
  505. case 'F':
  506. catch_factor2 = atoi(optarg);
  507. break;
  508. case 'C':
  509. pclamp = (double) atoi(optarg);
  510. break;
  511. case 'Q':
  512. controlquant = (double) atoi(optarg);
  513. break;
  514. case 'v':
  515. verbose = 1;
  516. break;
  517. case 'i':
  518. instrument = 1;
  519. break;
  520. case 's':
  521. smooth_size = atoi(optarg);
  522. break;
  523. case ':':
  524. fprintf(stderr,
  525. "Option -%c requires an operand\n", optopt);
  526. errflg++;
  527. break;
  528. case '?':
  529. fprintf(stderr,
  530. "Unrecognized option: -%c\n", optopt);
  531. errflg++;
  532. }
  533. }
  534. if (errflg) {
  535. printUsage();
  536. exit(2);
  537. }
  538. if( (samplerate_quality < 0) || (samplerate_quality > 4) ) {
  539. fprintf (stderr, "invalid samplerate quality\n");
  540. return 1;
  541. }
  542. if ((client = jack_client_open (jack_name, 0, NULL)) == 0) {
  543. fprintf (stderr, "jack server not running?\n");
  544. return 1;
  545. }
  546. /* tell the JACK server to call `process()' whenever
  547. there is work to be done.
  548. */
  549. jack_set_process_callback (client, process, 0);
  550. /* tell the JACK server to call `jack_shutdown()' if
  551. it ever shuts down, either entirely, or if it
  552. just decides to stop calling us.
  553. */
  554. jack_on_shutdown (client, jack_shutdown, 0);
  555. // get jack sample_rate
  556. jack_sample_rate = jack_get_sample_rate( client );
  557. if( !sample_rate )
  558. sample_rate = jack_sample_rate;
  559. // now open the alsa fd...
  560. alsa_handle = open_audiofd( alsa_device, 1, sample_rate, num_channels, period_size, num_periods);
  561. if( alsa_handle == 0 )
  562. exit(20);
  563. printf( "selected sample format: %s\n", formats[format].name );
  564. static_resample_factor = (double) jack_sample_rate / (double) sample_rate;
  565. resample_mean = static_resample_factor;
  566. offset_array = malloc( sizeof(double) * smooth_size );
  567. if( offset_array == NULL ) {
  568. fprintf( stderr, "no memory for offset_array !!!\n" );
  569. exit(20);
  570. }
  571. window_array = malloc( sizeof(double) * smooth_size );
  572. if( window_array == NULL ) {
  573. fprintf( stderr, "no memory for window_array !!!\n" );
  574. exit(20);
  575. }
  576. int i;
  577. for( i=0; i<smooth_size; i++ ) {
  578. offset_array[i] = 0.0;
  579. window_array[i] = hann( (double) i / ((double) smooth_size - 1.0) );
  580. }
  581. jack_buffer_size = jack_get_buffer_size( client );
  582. // Setup target delay and max_diff for the normal user, who does not play with them...
  583. if( !target_delay )
  584. target_delay = (num_periods*period_size / 2) + jack_buffer_size/2;
  585. if( !max_diff )
  586. max_diff = num_periods*period_size - target_delay ;
  587. if( max_diff > target_delay ) {
  588. fprintf( stderr, "target_delay (%d) cant be smaller than max_diff(%d)\n", target_delay, max_diff );
  589. exit(20);
  590. }
  591. if( (target_delay+max_diff) > (num_periods*period_size) ) {
  592. fprintf( stderr, "target_delay+max_diff (%d) cant be bigger than buffersize(%d)\n", target_delay+max_diff, num_periods*period_size );
  593. exit(20);
  594. }
  595. // alloc input ports, which are blasted out to alsa...
  596. alloc_ports( num_channels, 0 );
  597. /* tell the JACK server that we are ready to roll */
  598. if (jack_activate (client)) {
  599. fprintf (stderr, "cannot activate client");
  600. return 1;
  601. }
  602. signal( SIGTERM, sigterm_handler );
  603. signal( SIGINT, sigterm_handler );
  604. if( verbose ) {
  605. while(!quit) {
  606. usleep(500000);
  607. if( output_new_delay ) {
  608. printf( "delay = %d\n", output_new_delay );
  609. output_new_delay = 0;
  610. }
  611. printf( "res: %f, \tdiff = %f, \toffset = %f \n", output_resampling_factor, output_diff, output_offset );
  612. }
  613. } else if( instrument ) {
  614. printf( "# n\tresamp\tdiff\toffseti\tintegral\n");
  615. int n=0;
  616. while(!quit) {
  617. usleep(1000);
  618. printf( "%d\t%f\t%f\t%f\t%f\n", n++, output_resampling_factor, output_diff, output_offset, output_integral );
  619. }
  620. } else {
  621. while(!quit)
  622. {
  623. usleep(500000);
  624. if( output_new_delay ) {
  625. printf( "delay = %d\n", output_new_delay );
  626. output_new_delay = 0;
  627. }
  628. }
  629. }
  630. jack_deactivate( client );
  631. jack_client_close (client);
  632. exit (0);
  633. }