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.

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