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.

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