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.

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