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.

760 lines
22KB

  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 resample_lower_limit = 0.25;
  31. double resample_upper_limit = 4.0;
  32. double *offset_array;
  33. double *window_array;
  34. int offset_differential_index = 0;
  35. double offset_integral = 0;
  36. int quit = 0;
  37. // ------------------------------------------------------ commandline parameters
  38. int sample_rate = 0; /* stream rate */
  39. int num_channels = 2; /* count of channels */
  40. int period_size = 1024;
  41. int num_periods = 2;
  42. int target_delay = 0; /* the delay which the program should try to approach. */
  43. int max_diff = 0; /* the diff value, when a hard readpointer skip should occur */
  44. int catch_factor = 100000;
  45. int catch_factor2 = 10000;
  46. double pclamp = 15.0;
  47. double controlquant = 10000.0;
  48. int smooth_size = 256;
  49. int good_window=0;
  50. int verbose = 0;
  51. int instrument = 0;
  52. int samplerate_quality = 2;
  53. // Debug stuff:
  54. volatile float output_resampling_factor = 1.0;
  55. volatile int output_new_delay = 0;
  56. volatile float output_offset = 0.0;
  57. volatile float output_integral = 0.0;
  58. volatile float output_diff = 0.0;
  59. snd_pcm_uframes_t real_buffer_size;
  60. snd_pcm_uframes_t real_period_size;
  61. // format selection, and corresponding functions from memops in a nice set of structs.
  62. typedef struct alsa_format {
  63. snd_pcm_format_t format_id;
  64. size_t sample_size;
  65. void (*jack_to_soundcard) (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state);
  66. void (*soundcard_to_jack) (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip);
  67. const char *name;
  68. } alsa_format_t;
  69. alsa_format_t formats[] = {
  70. { SND_PCM_FORMAT_FLOAT_LE, 4, sample_move_dS_floatLE, sample_move_floatLE_sSs, "float" },
  71. { SND_PCM_FORMAT_S32, 4, sample_move_d32u24_sS, sample_move_dS_s32u24, "32bit" },
  72. { SND_PCM_FORMAT_S24, 4, sample_move_d24_sS, sample_move_dS_s24, "24bit" },
  73. { SND_PCM_FORMAT_S16, 2, sample_move_d16_sS, sample_move_dS_s16, "16bit" }
  74. };
  75. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  76. int format=0;
  77. // Alsa stuff... i dont want to touch this bullshit in the next years.... please...
  78. static int xrun_recovery(snd_pcm_t *handle, int err) {
  79. // printf( "xrun !!!.... %d\n", err );
  80. if (err == -EPIPE) { /* under-run */
  81. err = snd_pcm_prepare(handle);
  82. if (err < 0)
  83. printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err));
  84. return 0;
  85. } else if (err == -EAGAIN) {
  86. while ((err = snd_pcm_resume(handle)) == -EAGAIN)
  87. usleep(100); /* wait until the suspend flag is released */
  88. if (err < 0) {
  89. err = snd_pcm_prepare(handle);
  90. if (err < 0)
  91. printf("Can't recovery from suspend, prepare failed: %s\n", snd_strerror(err));
  92. }
  93. return 0;
  94. }
  95. return err;
  96. }
  97. static int set_hwformat( snd_pcm_t *handle, snd_pcm_hw_params_t *params )
  98. {
  99. int i;
  100. int err;
  101. for( i=0; i<NUMFORMATS; i++ ) {
  102. /* set the sample format */
  103. err = snd_pcm_hw_params_set_format(handle, params, formats[i].format_id);
  104. if (err == 0) {
  105. format = i;
  106. return 0;
  107. }
  108. }
  109. return err;
  110. }
  111. 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 ) {
  112. int err, dir=0;
  113. unsigned int buffer_time;
  114. unsigned int period_time;
  115. unsigned int rrate;
  116. unsigned int rchannels;
  117. /* choose all parameters */
  118. err = snd_pcm_hw_params_any(handle, params);
  119. if (err < 0) {
  120. printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
  121. return err;
  122. }
  123. /* set the interleaved read/write format */
  124. err = snd_pcm_hw_params_set_access(handle, params, access);
  125. if (err < 0) {
  126. printf("Access type not available for playback: %s\n", snd_strerror(err));
  127. return err;
  128. }
  129. /* set the sample format */
  130. err = set_hwformat(handle, params);
  131. if (err < 0) {
  132. printf("Sample format not available for playback: %s\n", snd_strerror(err));
  133. return err;
  134. }
  135. /* set the count of channels */
  136. rchannels = channels;
  137. err = snd_pcm_hw_params_set_channels_near(handle, params, &rchannels);
  138. if (err < 0) {
  139. printf("Channels count (%i) not available for record: %s\n", channels, snd_strerror(err));
  140. return err;
  141. }
  142. if (rchannels != channels) {
  143. printf("WARNING: chennel count does not match (requested %d got %d)\n", channels, rchannels);
  144. num_channels = rchannels;
  145. }
  146. /* set the stream rate */
  147. rrate = rate;
  148. err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
  149. if (err < 0) {
  150. printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
  151. return err;
  152. }
  153. if (rrate != rate) {
  154. printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, rrate);
  155. return -EINVAL;
  156. }
  157. /* set the buffer time */
  158. buffer_time = 1000000*(uint64_t)period*nperiods/rate;
  159. err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
  160. if (err < 0) {
  161. printf("Unable to set buffer time %i for playback: %s\n", 1000000*period*nperiods/rate, snd_strerror(err));
  162. return err;
  163. }
  164. err = snd_pcm_hw_params_get_buffer_size( params, &real_buffer_size );
  165. if (err < 0) {
  166. printf("Unable to get buffer size back: %s\n", snd_strerror(err));
  167. return err;
  168. }
  169. if( real_buffer_size != nperiods * period ) {
  170. printf( "WARNING: buffer size does not match: (requested %d, got %d)\n", nperiods * period, (int) real_buffer_size );
  171. }
  172. /* set the period time */
  173. period_time = 1000000*(uint64_t)period/rate;
  174. err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
  175. if (err < 0) {
  176. printf("Unable to set period time %i for playback: %s\n", 1000000*period/rate, snd_strerror(err));
  177. return err;
  178. }
  179. err = snd_pcm_hw_params_get_period_size(params, &real_period_size, NULL );
  180. if (err < 0) {
  181. printf("Unable to get period size back: %s\n", snd_strerror(err));
  182. return err;
  183. }
  184. if( real_period_size != period ) {
  185. printf( "WARNING: period size does not match: (requested %i, got %i)\n", period, (int)real_period_size );
  186. }
  187. /* write the parameters to device */
  188. err = snd_pcm_hw_params(handle, params);
  189. if (err < 0) {
  190. printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
  191. return err;
  192. }
  193. return 0;
  194. }
  195. static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams, int period, int nperiods) {
  196. int err;
  197. /* get the current swparams */
  198. err = snd_pcm_sw_params_current(handle, swparams);
  199. if (err < 0) {
  200. printf("Unable to determine current swparams for capture: %s\n", snd_strerror(err));
  201. return err;
  202. }
  203. /* start the transfer when the buffer is full */
  204. err = snd_pcm_sw_params_set_start_threshold(handle, swparams, period );
  205. if (err < 0) {
  206. printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
  207. return err;
  208. }
  209. err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, -1 );
  210. if (err < 0) {
  211. printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
  212. return err;
  213. }
  214. /* allow the transfer when at least period_size samples can be processed */
  215. err = snd_pcm_sw_params_set_avail_min(handle, swparams, 1 );
  216. if (err < 0) {
  217. printf("Unable to set avail min for capture: %s\n", snd_strerror(err));
  218. return err;
  219. }
  220. /* align all transfers to 1 sample */
  221. err = snd_pcm_sw_params_set_xfer_align(handle, swparams, 1);
  222. if (err < 0) {
  223. printf("Unable to set transfer align for capture: %s\n", snd_strerror(err));
  224. return err;
  225. }
  226. /* write the parameters to the playback device */
  227. err = snd_pcm_sw_params(handle, swparams);
  228. if (err < 0) {
  229. printf("Unable to set sw params for capture: %s\n", snd_strerror(err));
  230. return err;
  231. }
  232. return 0;
  233. }
  234. // ok... i only need this function to communicate with the alsa bloat api...
  235. static snd_pcm_t *open_audiofd( char *device_name, int capture, int rate, int channels, int period, int nperiods ) {
  236. int err;
  237. snd_pcm_t *handle;
  238. snd_pcm_hw_params_t *hwparams;
  239. snd_pcm_sw_params_t *swparams;
  240. snd_pcm_hw_params_alloca(&hwparams);
  241. snd_pcm_sw_params_alloca(&swparams);
  242. if ((err = snd_pcm_open(&(handle), device_name, capture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK )) < 0) {
  243. printf("Capture open error: %s\n", snd_strerror(err));
  244. return NULL;
  245. }
  246. if ((err = set_hwparams(handle, hwparams,SND_PCM_ACCESS_RW_INTERLEAVED, rate, channels, period, nperiods )) < 0) {
  247. printf("Setting of hwparams failed: %s\n", snd_strerror(err));
  248. return NULL;
  249. }
  250. if ((err = set_swparams(handle, swparams, period, nperiods)) < 0) {
  251. printf("Setting of swparams failed: %s\n", snd_strerror(err));
  252. return NULL;
  253. }
  254. //snd_pcm_start( handle );
  255. //snd_pcm_wait( handle, 200 );
  256. int num_null_samples = nperiods * period * channels;
  257. char *tmp = alloca( num_null_samples * formats[format].sample_size );
  258. memset( tmp, 0, num_null_samples * formats[format].sample_size );
  259. snd_pcm_writei( handle, tmp, num_null_samples );
  260. return handle;
  261. }
  262. double hann( double x )
  263. {
  264. return 0.5 * (1.0 - cos( 2*M_PI * x ) );
  265. }
  266. /**
  267. * The process callback for this JACK application.
  268. * It is called by JACK at the appropriate times.
  269. */
  270. int process (jack_nframes_t nframes, void *arg) {
  271. char *outbuf;
  272. float *resampbuf;
  273. int rlen;
  274. int err;
  275. snd_pcm_sframes_t delay = target_delay;
  276. int i;
  277. delay = (num_periods*period_size)-snd_pcm_avail( alsa_handle ) ;
  278. delay -= jack_frames_since_cycle_start( client );
  279. delay += jack_get_buffer_size( client ) / 2;
  280. // Do it the hard way.
  281. // this is for compensating xruns etc...
  282. if( delay > (target_delay+max_diff) ) {
  283. snd_pcm_rewind( alsa_handle, delay - target_delay );
  284. output_new_delay = (int) delay;
  285. delay = target_delay;
  286. // Set the resample_rate... we need to adjust the offset integral, to do this.
  287. // first look at the PI controller, this code is just a special case, which should never execute once
  288. // everything is swung in.
  289. offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
  290. // Also clear the array. we are beginning a new control cycle.
  291. for( i=0; i<smooth_size; i++ )
  292. offset_array[i] = 0.0;
  293. }
  294. if( delay < (target_delay-max_diff) ) {
  295. char *tmp = alloca( (target_delay-delay) * formats[format].sample_size * num_channels );
  296. memset( tmp, 0, formats[format].sample_size * num_channels * (target_delay-delay) );
  297. snd_pcm_writei( alsa_handle, tmp, target_delay-delay );
  298. output_new_delay = (int) delay;
  299. delay = target_delay;
  300. // Set the resample_rate... we need to adjust the offset integral, to do this.
  301. offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
  302. // Also clear the array. we are beginning a new control cycle.
  303. for( i=0; i<smooth_size; i++ )
  304. offset_array[i] = 0.0;
  305. }
  306. /* ok... now we should have target_delay +- max_diff on the alsa side.
  307. *
  308. * calculate the number of frames, we want to get.
  309. */
  310. double offset = delay - target_delay;
  311. // Save offset.
  312. offset_array[(offset_differential_index++)% smooth_size ] = offset;
  313. // Build the mean of the windowed offset array
  314. // basically fir lowpassing.
  315. double smooth_offset = 0.0;
  316. for( i=0; i<smooth_size; i++ )
  317. smooth_offset +=
  318. offset_array[ (i + offset_differential_index-1) % smooth_size] * window_array[i];
  319. smooth_offset /= (double) smooth_size;
  320. // this is the integral of the smoothed_offset
  321. offset_integral += smooth_offset;
  322. // Clamp offset.
  323. // the smooth offset still contains unwanted noise
  324. // which would go straigth onto the resample coeff.
  325. // it only used in the P component and the I component is used for the fine tuning anyways.
  326. if( fabs( smooth_offset ) < pclamp )
  327. smooth_offset = 0.0;
  328. // ok. now this is the PI controller.
  329. // u(t) = K * ( e(t) + 1/T \int e(t') dt' )
  330. // K = 1/catch_factor and T = catch_factor2
  331. double current_resample_factor = static_resample_factor - smooth_offset / (double) catch_factor - offset_integral / (double) catch_factor / (double)catch_factor2;
  332. // now quantize this value around resample_mean, so that the noise which is in the integral component doesnt hurt.
  333. current_resample_factor = floor( (current_resample_factor - resample_mean) * controlquant + 0.5 ) / controlquant + resample_mean;
  334. // Output "instrumentatio" gonna change that to real instrumentation in a few.
  335. output_resampling_factor = (float) current_resample_factor;
  336. output_diff = (float) smooth_offset;
  337. output_integral = (float) offset_integral;
  338. output_offset = (float) offset;
  339. // Clamp a bit.
  340. if( current_resample_factor < resample_lower_limit ) current_resample_factor = resample_lower_limit;
  341. if( current_resample_factor > resample_upper_limit ) current_resample_factor = resample_upper_limit;
  342. // Now Calculate how many samples we need.
  343. rlen = ceil( ((double)nframes) * current_resample_factor )+2;
  344. assert( rlen > 2 );
  345. // Calculate resample_mean so we can init ourselves to saner values.
  346. resample_mean = 0.9999 * resample_mean + 0.0001 * current_resample_factor;
  347. /*
  348. * now this should do it...
  349. */
  350. outbuf = alloca( rlen * formats[format].sample_size * num_channels );
  351. resampbuf = alloca( rlen * sizeof( float ) );
  352. /*
  353. * render jack ports to the outbuf...
  354. */
  355. int chn = 0;
  356. JSList *node = playback_ports;
  357. JSList *src_node = playback_srcs;
  358. SRC_DATA src;
  359. while ( node != NULL)
  360. {
  361. jack_port_t *port = (jack_port_t *) node->data;
  362. float *buf = jack_port_get_buffer (port, nframes);
  363. SRC_STATE *src_state = src_node->data;
  364. src.data_in = buf;
  365. src.input_frames = nframes;
  366. src.data_out = resampbuf;
  367. src.output_frames = rlen;
  368. src.end_of_input = 0;
  369. src.src_ratio = current_resample_factor;
  370. src_process( src_state, &src );
  371. formats[format].jack_to_soundcard( outbuf + format[formats].sample_size * chn, resampbuf, src.output_frames_gen, num_channels*format[formats].sample_size, NULL);
  372. src_node = jack_slist_next (src_node);
  373. node = jack_slist_next (node);
  374. chn++;
  375. }
  376. // now write the output...
  377. again:
  378. err = snd_pcm_writei(alsa_handle, outbuf, src.output_frames_gen);
  379. //err = snd_pcm_writei(alsa_handle, outbuf, src.output_frames_gen);
  380. if( err < 0 ) {
  381. printf( "err = %d\n", err );
  382. if (xrun_recovery(alsa_handle, err) < 0) {
  383. printf("Write error: %s\n", snd_strerror(err));
  384. exit(EXIT_FAILURE);
  385. }
  386. goto again;
  387. }
  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 | JackPortIsPhysical | JackPortIsTerminal;
  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_out";
  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. static_resample_factor = (double) sample_rate / (double) jack_sample_rate;
  561. resample_lower_limit = static_resample_factor * 0.25;
  562. resample_upper_limit = static_resample_factor * 4.0;
  563. resample_mean = static_resample_factor;
  564. offset_array = malloc( sizeof(double) * smooth_size );
  565. if( offset_array == NULL ) {
  566. fprintf( stderr, "no memory for offset_array !!!\n" );
  567. exit(20);
  568. }
  569. window_array = malloc( sizeof(double) * smooth_size );
  570. if( window_array == NULL ) {
  571. fprintf( stderr, "no memory for window_array !!!\n" );
  572. exit(20);
  573. }
  574. int i;
  575. for( i=0; i<smooth_size; i++ ) {
  576. offset_array[i] = 0.0;
  577. window_array[i] = hann( (double) i / ((double) smooth_size - 1.0) );
  578. }
  579. jack_buffer_size = jack_get_buffer_size( client );
  580. // Setup target delay and max_diff for the normal user, who does not play with them...
  581. if( !target_delay )
  582. target_delay = (num_periods*period_size / 2) - jack_buffer_size/2;
  583. if( !max_diff )
  584. max_diff = target_delay;
  585. if( max_diff > target_delay ) {
  586. fprintf( stderr, "target_delay (%d) cant be smaller than max_diff(%d)\n", target_delay, max_diff );
  587. exit(20);
  588. }
  589. if( (target_delay+max_diff) > (num_periods*period_size) ) {
  590. fprintf( stderr, "target_delay+max_diff (%d) cant be bigger than buffersize(%d)\n", target_delay+max_diff, num_periods*period_size );
  591. exit(20);
  592. }
  593. // now open the alsa fd...
  594. alsa_handle = open_audiofd( alsa_device, 0, sample_rate, num_channels, period_size, num_periods);
  595. if( alsa_handle == 0 )
  596. exit(20);
  597. printf( "selected sample format: %s\n", formats[format].name );
  598. // alloc input ports, which are blasted out to alsa...
  599. alloc_ports( 0, num_channels );
  600. /* tell the JACK server that we are ready to roll */
  601. if (jack_activate (client)) {
  602. fprintf (stderr, "cannot activate client");
  603. return 1;
  604. }
  605. signal( SIGTERM, sigterm_handler );
  606. signal( SIGINT, sigterm_handler );
  607. if( verbose ) {
  608. while(!quit) {
  609. usleep(500000);
  610. if( output_new_delay ) {
  611. printf( "delay = %d\n", output_new_delay );
  612. output_new_delay = 0;
  613. }
  614. printf( "res: %f, \tdiff = %f, \toffset = %f \n", output_resampling_factor, output_diff, output_offset );
  615. }
  616. } else if( instrument ) {
  617. printf( "# n\tresamp\tdiff\toffseti\tintegral\n");
  618. int n=0;
  619. while(!quit) {
  620. usleep(1000);
  621. printf( "%d\t%f\t%f\t%f\t%f\n", n++, output_resampling_factor, output_diff, output_offset, output_integral );
  622. }
  623. } else {
  624. while(!quit)
  625. {
  626. usleep(500000);
  627. if( output_new_delay ) {
  628. printf( "delay = %d\n", output_new_delay );
  629. output_new_delay = 0;
  630. }
  631. }
  632. }
  633. jack_deactivate( client );
  634. jack_client_close (client);
  635. exit (0);
  636. }