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.

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