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.

756 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. 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. int quit = 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, 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("Rate doesn't match (requested %iHz, get %iHz)\n", rate, rrate);
  153. return -EINVAL;
  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, int nperiods) {
  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, 1 );
  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, nperiods)) < 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. int num_null_samples = nperiods * period * channels;
  255. char *tmp = alloca( num_null_samples * formats[format].sample_size );
  256. memset( tmp, 0, num_null_samples * formats[format].sample_size );
  257. snd_pcm_writei( handle, tmp, num_null_samples );
  258. return handle;
  259. }
  260. double hann( double x )
  261. {
  262. return 0.5 * (1.0 - cos( 2*M_PI * x ) );
  263. }
  264. /**
  265. * The process callback for this JACK application.
  266. * It is called by JACK at the appropriate times.
  267. */
  268. int process (jack_nframes_t nframes, void *arg) {
  269. char *outbuf;
  270. float *resampbuf;
  271. int rlen;
  272. int err;
  273. snd_pcm_sframes_t delay = target_delay;
  274. int i;
  275. snd_pcm_delay( alsa_handle, &delay );
  276. delay -= jack_frames_since_cycle_start( client );
  277. delay += jack_get_buffer_size( client ) / 2;
  278. // Do it the hard way.
  279. // this is for compensating xruns etc...
  280. if( delay > (target_delay+max_diff) ) {
  281. snd_pcm_rewind( alsa_handle, delay - target_delay );
  282. output_new_delay = (int) delay;
  283. delay = target_delay;
  284. // Set the resample_rate... we need to adjust the offset integral, to do this.
  285. // first look at the PI controller, this code is just a special case, which should never execute once
  286. // everything is swung in.
  287. offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
  288. // Also clear the array. we are beginning a new control cycle.
  289. for( i=0; i<smooth_size; i++ )
  290. offset_array[i] = 0.0;
  291. }
  292. if( delay < (target_delay-max_diff) ) {
  293. char *tmp = alloca( (target_delay-delay) * formats[format].sample_size * num_channels );
  294. memset( tmp, 0, formats[format].sample_size * num_channels * (target_delay-delay) );
  295. snd_pcm_writei( alsa_handle, tmp, target_delay-delay );
  296. output_new_delay = (int) delay;
  297. delay = target_delay;
  298. // Set the resample_rate... we need to adjust the offset integral, to do this.
  299. offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
  300. // Also clear the array. we are beginning a new control cycle.
  301. for( i=0; i<smooth_size; i++ )
  302. offset_array[i] = 0.0;
  303. }
  304. /* ok... now we should have target_delay +- max_diff on the alsa side.
  305. *
  306. * calculate the number of frames, we want to get.
  307. */
  308. double offset = delay - target_delay;
  309. // Save offset.
  310. offset_array[(offset_differential_index++)% smooth_size ] = offset;
  311. // Build the mean of the windowed offset array
  312. // basically fir lowpassing.
  313. double smooth_offset = 0.0;
  314. for( i=0; i<smooth_size; i++ )
  315. smooth_offset +=
  316. offset_array[ (i + offset_differential_index-1) % smooth_size] * window_array[i];
  317. smooth_offset /= (double) smooth_size;
  318. // this is the integral of the smoothed_offset
  319. offset_integral += smooth_offset;
  320. // Clamp offset.
  321. // the smooth offset still contains unwanted noise
  322. // which would go straigth onto the resample coeff.
  323. // it only used in the P component and the I component is used for the fine tuning anyways.
  324. if( fabs( smooth_offset ) < pclamp )
  325. smooth_offset = 0.0;
  326. // ok. now this is the PI controller.
  327. // u(t) = K * ( e(t) + 1/T \int e(t') dt' )
  328. // K = 1/catch_factor and T = catch_factor2
  329. double current_resample_factor = static_resample_factor - smooth_offset / (double) catch_factor - offset_integral / (double) catch_factor / (double)catch_factor2;
  330. // now quantize this value around resample_mean, so that the noise which is in the integral component doesnt hurt.
  331. current_resample_factor = floor( (current_resample_factor - resample_mean) * controlquant + 0.5 ) / controlquant + resample_mean;
  332. // Output "instrumentatio" gonna change that to real instrumentation in a few.
  333. output_resampling_factor = (float) current_resample_factor;
  334. output_diff = (float) smooth_offset;
  335. output_integral = (float) offset_integral;
  336. output_offset = (float) offset;
  337. // Clamp a bit.
  338. if( current_resample_factor < 0.25 ) current_resample_factor = 0.25;
  339. if( current_resample_factor > 4 ) current_resample_factor = 4;
  340. // Now Calculate how many samples we need.
  341. rlen = ceil( ((double)nframes) * current_resample_factor )+2;
  342. assert( rlen > 2 );
  343. // Calculate resample_mean so we can init ourselves to saner values.
  344. resample_mean = 0.9999 * resample_mean + 0.0001 * current_resample_factor;
  345. /*
  346. * now this should do it...
  347. */
  348. outbuf = alloca( rlen * formats[format].sample_size * num_channels );
  349. resampbuf = alloca( rlen * sizeof( float ) );
  350. /*
  351. * render jack ports to the outbuf...
  352. */
  353. int chn = 0;
  354. JSList *node = playback_ports;
  355. JSList *src_node = playback_srcs;
  356. SRC_DATA src;
  357. while ( node != NULL)
  358. {
  359. jack_port_t *port = (jack_port_t *) node->data;
  360. float *buf = jack_port_get_buffer (port, nframes);
  361. SRC_STATE *src_state = src_node->data;
  362. src.data_in = buf;
  363. src.input_frames = nframes;
  364. src.data_out = resampbuf;
  365. src.output_frames = rlen;
  366. src.end_of_input = 0;
  367. src.src_ratio = current_resample_factor;
  368. src_process( src_state, &src );
  369. formats[format].jack_to_soundcard( outbuf + format[formats].sample_size * chn, resampbuf, src.output_frames_gen, num_channels*format[formats].sample_size, NULL);
  370. src_node = jack_slist_next (src_node);
  371. node = jack_slist_next (node);
  372. chn++;
  373. }
  374. // now write the output...
  375. again:
  376. err = snd_pcm_writei(alsa_handle, outbuf, src.output_frames_gen);
  377. //err = snd_pcm_writei(alsa_handle, outbuf, src.output_frames_gen);
  378. if( err < 0 ) {
  379. printf( "err = %d\n", err );
  380. if (xrun_recovery(alsa_handle, err) < 0) {
  381. printf("Write error: %s\n", snd_strerror(err));
  382. exit(EXIT_FAILURE);
  383. }
  384. goto again;
  385. }
  386. return 0;
  387. }
  388. /**
  389. * Allocate the necessary jack ports...
  390. */
  391. void alloc_ports( int n_capture, int n_playback ) {
  392. int port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  393. int chn;
  394. jack_port_t *port;
  395. char buf[32];
  396. capture_ports = NULL;
  397. for (chn = 0; chn < n_capture; chn++)
  398. {
  399. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn+1);
  400. port = jack_port_register (client, buf,
  401. JACK_DEFAULT_AUDIO_TYPE,
  402. port_flags, 0);
  403. if (!port)
  404. {
  405. printf( "jacknet_client: cannot register port for %s", buf);
  406. break;
  407. }
  408. capture_srcs = jack_slist_append( capture_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
  409. capture_ports = jack_slist_append (capture_ports, port);
  410. }
  411. port_flags = JackPortIsInput;
  412. playback_ports = NULL;
  413. for (chn = 0; chn < n_playback; chn++)
  414. {
  415. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn+1);
  416. port = jack_port_register (client, buf,
  417. JACK_DEFAULT_AUDIO_TYPE,
  418. port_flags, 0);
  419. if (!port)
  420. {
  421. printf( "jacknet_client: cannot register port for %s", buf);
  422. break;
  423. }
  424. playback_srcs = jack_slist_append( playback_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
  425. playback_ports = jack_slist_append (playback_ports, port);
  426. }
  427. }
  428. /**
  429. * This is the shutdown callback for this JACK application.
  430. * It is called by JACK if the server ever shuts down or
  431. * decides to disconnect the client.
  432. */
  433. void jack_shutdown (void *arg) {
  434. exit (1);
  435. }
  436. /**
  437. * be user friendly.
  438. * be user friendly.
  439. * be user friendly.
  440. */
  441. void printUsage() {
  442. fprintf(stderr, "usage: alsa_out [options]\n"
  443. "\n"
  444. " -j <jack name> - client name\n"
  445. " -d <alsa_device> \n"
  446. " -c <channels> \n"
  447. " -p <period_size> \n"
  448. " -n <num_period> \n"
  449. " -r <sample_rate> \n"
  450. " -q <sample_rate quality [0..4]\n"
  451. " -m <max_diff> \n"
  452. " -t <target_delay> \n"
  453. " -i turns on instrumentation\n"
  454. " -v turns on printouts\n"
  455. "\n");
  456. }
  457. /**
  458. * the main function....
  459. */
  460. void
  461. sigterm_handler( int signal )
  462. {
  463. quit = 1;
  464. }
  465. int main (int argc, char *argv[]) {
  466. char jack_name[30] = "alsa_out";
  467. char alsa_device[30] = "hw:0";
  468. extern char *optarg;
  469. extern int optind, optopt;
  470. int errflg=0;
  471. int c;
  472. while ((c = getopt(argc, argv, "ivj:r:c:p:n:d:q:m:t:f:F:C:Q:s:")) != -1) {
  473. switch(c) {
  474. case 'j':
  475. strcpy(jack_name,optarg);
  476. break;
  477. case 'r':
  478. sample_rate = atoi(optarg);
  479. break;
  480. case 'c':
  481. num_channels = atoi(optarg);
  482. break;
  483. case 'p':
  484. period_size = atoi(optarg);
  485. break;
  486. case 'n':
  487. num_periods = atoi(optarg);
  488. break;
  489. case 'd':
  490. strcpy(alsa_device,optarg);
  491. break;
  492. case 't':
  493. target_delay = atoi(optarg);
  494. break;
  495. case 'q':
  496. samplerate_quality = atoi(optarg);
  497. break;
  498. case 'm':
  499. max_diff = atoi(optarg);
  500. break;
  501. case 'f':
  502. catch_factor = atoi(optarg);
  503. break;
  504. case 'F':
  505. catch_factor2 = atoi(optarg);
  506. break;
  507. case 'C':
  508. pclamp = (double) atoi(optarg);
  509. break;
  510. case 'Q':
  511. controlquant = (double) atoi(optarg);
  512. break;
  513. case 'v':
  514. verbose = 1;
  515. break;
  516. case 'i':
  517. instrument = 1;
  518. break;
  519. case 's':
  520. smooth_size = atoi(optarg);
  521. break;
  522. case ':':
  523. fprintf(stderr,
  524. "Option -%c requires an operand\n", optopt);
  525. errflg++;
  526. break;
  527. case '?':
  528. fprintf(stderr,
  529. "Unrecognized option: -%c\n", optopt);
  530. errflg++;
  531. }
  532. }
  533. if (errflg) {
  534. printUsage();
  535. exit(2);
  536. }
  537. if( (samplerate_quality < 0) || (samplerate_quality > 4) ) {
  538. fprintf (stderr, "invalid samplerate quality\n");
  539. return 1;
  540. }
  541. if ((client = jack_client_open (jack_name, 0, NULL)) == 0) {
  542. fprintf (stderr, "jack server not running?\n");
  543. return 1;
  544. }
  545. /* tell the JACK server to call `process()' whenever
  546. there is work to be done.
  547. */
  548. jack_set_process_callback (client, process, 0);
  549. /* tell the JACK server to call `jack_shutdown()' if
  550. it ever shuts down, either entirely, or if it
  551. just decides to stop calling us.
  552. */
  553. jack_on_shutdown (client, jack_shutdown, 0);
  554. // get jack sample_rate
  555. jack_sample_rate = jack_get_sample_rate( client );
  556. if( !sample_rate )
  557. sample_rate = jack_sample_rate;
  558. static_resample_factor = (double) sample_rate / (double) jack_sample_rate;
  559. resample_mean = static_resample_factor;
  560. offset_array = malloc( sizeof(double) * smooth_size );
  561. if( offset_array == NULL ) {
  562. fprintf( stderr, "no memory for offset_array !!!\n" );
  563. exit(20);
  564. }
  565. window_array = malloc( sizeof(double) * smooth_size );
  566. if( window_array == NULL ) {
  567. fprintf( stderr, "no memory for window_array !!!\n" );
  568. exit(20);
  569. }
  570. int i;
  571. for( i=0; i<smooth_size; i++ ) {
  572. offset_array[i] = 0.0;
  573. window_array[i] = hann( (double) i / ((double) smooth_size - 1.0) );
  574. }
  575. jack_buffer_size = jack_get_buffer_size( client );
  576. // Setup target delay and max_diff for the normal user, who does not play with them...
  577. if( !target_delay )
  578. target_delay = (num_periods*period_size / 2) - jack_buffer_size/2;
  579. if( !max_diff )
  580. max_diff = target_delay;
  581. if( max_diff > target_delay ) {
  582. fprintf( stderr, "target_delay (%d) cant be smaller than max_diff(%d)\n", target_delay, max_diff );
  583. exit(20);
  584. }
  585. if( (target_delay+max_diff) > (num_periods*period_size) ) {
  586. fprintf( stderr, "target_delay+max_diff (%d) cant be bigger than buffersize(%d)\n", target_delay+max_diff, num_periods*period_size );
  587. exit(20);
  588. }
  589. // now open the alsa fd...
  590. alsa_handle = open_audiofd( alsa_device, 0, sample_rate, num_channels, period_size, num_periods);
  591. if( alsa_handle == 0 )
  592. exit(20);
  593. printf( "selected sample format: %s\n", formats[format].name );
  594. // alloc input ports, which are blasted out to alsa...
  595. alloc_ports( 0, num_channels );
  596. /* tell the JACK server that we are ready to roll */
  597. if (jack_activate (client)) {
  598. fprintf (stderr, "cannot activate client");
  599. return 1;
  600. }
  601. signal( SIGTERM, sigterm_handler );
  602. signal( SIGINT, sigterm_handler );
  603. if( verbose ) {
  604. while(!quit) {
  605. usleep(500000);
  606. if( output_new_delay ) {
  607. printf( "delay = %d\n", output_new_delay );
  608. output_new_delay = 0;
  609. }
  610. printf( "res: %f, \tdiff = %f, \toffset = %f \n", output_resampling_factor, output_diff, output_offset );
  611. }
  612. } else if( instrument ) {
  613. printf( "# n\tresamp\tdiff\toffseti\tintegral\n");
  614. int n=0;
  615. while(!quit) {
  616. usleep(1000);
  617. printf( "%d\t%f\t%f\t%f\t%f\n", n++, output_resampling_factor, output_diff, output_offset, output_integral );
  618. }
  619. } else {
  620. while(!quit)
  621. {
  622. usleep(500000);
  623. if( output_new_delay ) {
  624. printf( "delay = %d\n", output_new_delay );
  625. output_new_delay = 0;
  626. }
  627. }
  628. }
  629. jack_deactivate( client );
  630. jack_client_close (client);
  631. exit (0);
  632. }