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.

762 lines
21KB

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