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.

802 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 "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. // buffers
  61. char *tmpbuf;
  62. char *outbuf;
  63. float *resampbuf;
  64. // format selection, and corresponding functions from memops in a nice set of structs.
  65. typedef struct alsa_format {
  66. snd_pcm_format_t format_id;
  67. size_t sample_size;
  68. void (*jack_to_soundcard) (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state);
  69. void (*soundcard_to_jack) (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip);
  70. const char *name;
  71. } alsa_format_t;
  72. alsa_format_t formats[] = {
  73. { SND_PCM_FORMAT_FLOAT_LE, 4, sample_move_dS_floatLE, sample_move_floatLE_sSs, "float" },
  74. { SND_PCM_FORMAT_S32, 4, sample_move_d32u24_sS, sample_move_dS_s32u24, "32bit" },
  75. { SND_PCM_FORMAT_S24_3LE, 3, sample_move_d24_sS, sample_move_dS_s24, "24bit - real" },
  76. { SND_PCM_FORMAT_S24, 4, sample_move_d24_sS, sample_move_dS_s24, "24bit" },
  77. { SND_PCM_FORMAT_S16, 2, sample_move_d16_sS, sample_move_dS_s16, "16bit" }
  78. };
  79. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  80. int format=0;
  81. // Alsa stuff... i dont want to touch this bullshit in the next years.... please...
  82. static int xrun_recovery(snd_pcm_t *handle, int err) {
  83. // printf( "xrun !!!.... %d\n", err );
  84. if (err == -EPIPE) { /* under-run */
  85. err = snd_pcm_prepare(handle);
  86. if (err < 0)
  87. printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err));
  88. return 0;
  89. } else if (err == -EAGAIN) {
  90. while ((err = snd_pcm_resume(handle)) == -EAGAIN)
  91. usleep(100); /* wait until the suspend flag is released */
  92. if (err < 0) {
  93. err = snd_pcm_prepare(handle);
  94. if (err < 0)
  95. printf("Can't recovery from suspend, prepare failed: %s\n", snd_strerror(err));
  96. }
  97. return 0;
  98. }
  99. return err;
  100. }
  101. static int set_hwformat( snd_pcm_t *handle, snd_pcm_hw_params_t *params )
  102. {
  103. int i;
  104. int err;
  105. for( i=0; i<NUMFORMATS; i++ ) {
  106. /* set the sample format */
  107. err = snd_pcm_hw_params_set_format(handle, params, formats[i].format_id);
  108. if (err == 0) {
  109. format = i;
  110. return 0;
  111. }
  112. }
  113. return err;
  114. }
  115. 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 ) {
  116. int err, dir=0;
  117. unsigned int buffer_time;
  118. unsigned int period_time;
  119. unsigned int rrate;
  120. unsigned int rchannels;
  121. /* choose all parameters */
  122. err = snd_pcm_hw_params_any(handle, params);
  123. if (err < 0) {
  124. printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
  125. return err;
  126. }
  127. /* set the interleaved read/write format */
  128. err = snd_pcm_hw_params_set_access(handle, params, access);
  129. if (err < 0) {
  130. printf("Access type not available for playback: %s\n", snd_strerror(err));
  131. return err;
  132. }
  133. /* set the sample format */
  134. err = set_hwformat(handle, params);
  135. if (err < 0) {
  136. printf("Sample format not available for playback: %s\n", snd_strerror(err));
  137. return err;
  138. }
  139. /* set the count of channels */
  140. rchannels = channels;
  141. err = snd_pcm_hw_params_set_channels_near(handle, params, &rchannels);
  142. if (err < 0) {
  143. printf("Channels count (%i) not available for record: %s\n", channels, snd_strerror(err));
  144. return err;
  145. }
  146. if (rchannels != channels) {
  147. printf("WARNING: chennel count does not match (requested %d got %d)\n", channels, rchannels);
  148. num_channels = rchannels;
  149. }
  150. /* set the stream rate */
  151. rrate = rate;
  152. err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
  153. if (err < 0) {
  154. printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
  155. return err;
  156. }
  157. if (rrate != rate) {
  158. printf("WARNING: Rate doesn't match (requested %iHz, get %iHz)\n", rate, rrate);
  159. sample_rate = rrate;
  160. }
  161. /* set the buffer time */
  162. buffer_time = 1000000*(uint64_t)period*nperiods/rate;
  163. err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
  164. if (err < 0) {
  165. printf("Unable to set buffer time %i for playback: %s\n", 1000000*period*nperiods/rate, snd_strerror(err));
  166. return err;
  167. }
  168. err = snd_pcm_hw_params_get_buffer_size( params, &real_buffer_size );
  169. if (err < 0) {
  170. printf("Unable to get buffer size back: %s\n", snd_strerror(err));
  171. return err;
  172. }
  173. if( real_buffer_size != nperiods * period ) {
  174. printf( "WARNING: buffer size does not match: (requested %d, got %d)\n", nperiods * period, (int) real_buffer_size );
  175. }
  176. /* set the period time */
  177. period_time = 1000000*(uint64_t)period/rate;
  178. err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
  179. if (err < 0) {
  180. printf("Unable to set period time %i for playback: %s\n", 1000000*period/rate, snd_strerror(err));
  181. return err;
  182. }
  183. err = snd_pcm_hw_params_get_period_size(params, &real_period_size, NULL );
  184. if (err < 0) {
  185. printf("Unable to get period size back: %s\n", snd_strerror(err));
  186. return err;
  187. }
  188. if( real_period_size != period ) {
  189. printf( "WARNING: period size does not match: (requested %i, got %i)\n", period, (int)real_period_size );
  190. }
  191. /* write the parameters to device */
  192. err = snd_pcm_hw_params(handle, params);
  193. if (err < 0) {
  194. printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
  195. return err;
  196. }
  197. return 0;
  198. }
  199. static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams, int period) {
  200. int err;
  201. /* get the current swparams */
  202. err = snd_pcm_sw_params_current(handle, swparams);
  203. if (err < 0) {
  204. printf("Unable to determine current swparams for capture: %s\n", snd_strerror(err));
  205. return err;
  206. }
  207. /* start the transfer when the buffer is full */
  208. err = snd_pcm_sw_params_set_start_threshold(handle, swparams, period );
  209. if (err < 0) {
  210. printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
  211. return err;
  212. }
  213. err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, -1 );
  214. if (err < 0) {
  215. printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
  216. return err;
  217. }
  218. /* allow the transfer when at least period_size samples can be processed */
  219. err = snd_pcm_sw_params_set_avail_min(handle, swparams, 2*period );
  220. if (err < 0) {
  221. printf("Unable to set avail min for capture: %s\n", snd_strerror(err));
  222. return err;
  223. }
  224. /* align all transfers to 1 sample */
  225. err = snd_pcm_sw_params_set_xfer_align(handle, swparams, 1);
  226. if (err < 0) {
  227. printf("Unable to set transfer align for capture: %s\n", snd_strerror(err));
  228. return err;
  229. }
  230. /* write the parameters to the playback device */
  231. err = snd_pcm_sw_params(handle, swparams);
  232. if (err < 0) {
  233. printf("Unable to set sw params for capture: %s\n", snd_strerror(err));
  234. return err;
  235. }
  236. return 0;
  237. }
  238. // ok... i only need this function to communicate with the alsa bloat api...
  239. static snd_pcm_t *open_audiofd( char *device_name, int capture, int rate, int channels, int period, int nperiods ) {
  240. int err;
  241. snd_pcm_t *handle;
  242. snd_pcm_hw_params_t *hwparams;
  243. snd_pcm_sw_params_t *swparams;
  244. snd_pcm_hw_params_alloca(&hwparams);
  245. snd_pcm_sw_params_alloca(&swparams);
  246. if ((err = snd_pcm_open(&(handle), device_name, capture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK )) < 0) {
  247. printf("Capture open error: %s\n", snd_strerror(err));
  248. return NULL;
  249. }
  250. if ((err = set_hwparams(handle, hwparams,SND_PCM_ACCESS_RW_INTERLEAVED, rate, channels, period, nperiods )) < 0) {
  251. printf("Setting of hwparams failed: %s\n", snd_strerror(err));
  252. return NULL;
  253. }
  254. if ((err = set_swparams(handle, swparams, period)) < 0) {
  255. printf("Setting of swparams failed: %s\n", snd_strerror(err));
  256. return NULL;
  257. }
  258. snd_pcm_start( handle );
  259. snd_pcm_wait( handle, 200 );
  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. int rlen;
  272. int err;
  273. snd_pcm_sframes_t delay = target_delay;
  274. int put_back_samples=0;
  275. int i;
  276. delay = snd_pcm_avail( alsa_handle );
  277. delay -= jack_frames_since_cycle_start( client );
  278. // Do it the hard way.
  279. // this is for compensating xruns etc...
  280. if( delay > (target_delay+max_diff) ) {
  281. output_new_delay = (int) delay;
  282. while ((delay-target_delay) > 0) {
  283. snd_pcm_uframes_t to_read = ((delay-target_delay) > 512) ? 512 : (delay-target_delay);
  284. snd_pcm_readi( alsa_handle, tmpbuf, to_read );
  285. delay -= to_read;
  286. }
  287. delay = target_delay;
  288. // Set the resample_rate... we need to adjust the offset integral, to do this.
  289. // first look at the PI controller, this code is just a special case, which should never execute once
  290. // everything is swung in.
  291. offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
  292. // Also clear the array. we are beginning a new control cycle.
  293. for( i=0; i<smooth_size; i++ )
  294. offset_array[i] = 0.0;
  295. }
  296. if( delay < (target_delay-max_diff) ) {
  297. snd_pcm_rewind( alsa_handle, 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. // 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. * the latency callback.
  393. * sets up the latencies on the ports.
  394. */
  395. void
  396. latency_cb (jack_latency_callback_mode_t mode, void *arg)
  397. {
  398. jack_latency_range_t range;
  399. JSList *node;
  400. range.min = range.max = target_delay;
  401. if (mode == JackCaptureLatency) {
  402. for (node = capture_ports; node; node = jack_slist_next (node)) {
  403. jack_port_t *port = node->data;
  404. jack_port_set_latency_range (port, mode, &range);
  405. }
  406. } else {
  407. for (node = playback_ports; node; node = jack_slist_next (node)) {
  408. jack_port_t *port = node->data;
  409. jack_port_set_latency_range (port, mode, &range);
  410. }
  411. }
  412. }
  413. /**
  414. * Allocate the necessary jack ports...
  415. */
  416. void alloc_ports( int n_capture, int n_playback ) {
  417. int port_flags = JackPortIsOutput;
  418. int chn;
  419. jack_port_t *port;
  420. char buf[32];
  421. capture_ports = NULL;
  422. for (chn = 0; chn < n_capture; chn++)
  423. {
  424. snprintf (buf, sizeof(buf) - 1, "capture_%u", chn+1);
  425. port = jack_port_register (client, buf,
  426. JACK_DEFAULT_AUDIO_TYPE,
  427. port_flags, 0);
  428. if (!port)
  429. {
  430. printf( "jacknet_client: cannot register port for %s", buf);
  431. break;
  432. }
  433. capture_srcs = jack_slist_append( capture_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
  434. capture_ports = jack_slist_append (capture_ports, port);
  435. }
  436. port_flags = JackPortIsInput;
  437. playback_ports = NULL;
  438. for (chn = 0; chn < n_playback; chn++)
  439. {
  440. snprintf (buf, sizeof(buf) - 1, "playback_%u", chn+1);
  441. port = jack_port_register (client, buf,
  442. JACK_DEFAULT_AUDIO_TYPE,
  443. port_flags, 0);
  444. if (!port)
  445. {
  446. printf( "jacknet_client: cannot register port for %s", buf);
  447. break;
  448. }
  449. playback_srcs = jack_slist_append( playback_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
  450. playback_ports = jack_slist_append (playback_ports, port);
  451. }
  452. }
  453. /**
  454. * This is the shutdown callback for this JACK application.
  455. * It is called by JACK if the server ever shuts down or
  456. * decides to disconnect the client.
  457. */
  458. void jack_shutdown (void *arg) {
  459. exit (1);
  460. }
  461. /**
  462. * be user friendly.
  463. * be user friendly.
  464. * be user friendly.
  465. */
  466. void printUsage() {
  467. fprintf(stderr, "usage: alsa_out [options]\n"
  468. "\n"
  469. " -j <jack name> - client name\n"
  470. " -d <alsa_device> \n"
  471. " -c <channels> \n"
  472. " -p <period_size> \n"
  473. " -n <num_period> \n"
  474. " -r <sample_rate> \n"
  475. " -q <sample_rate quality [0..4]\n"
  476. " -m <max_diff> \n"
  477. " -t <target_delay> \n"
  478. " -i turns on instrumentation\n"
  479. " -v turns on printouts\n"
  480. "\n");
  481. }
  482. /**
  483. * the main function....
  484. */
  485. void
  486. sigterm_handler( int signal )
  487. {
  488. quit = 1;
  489. }
  490. int main (int argc, char *argv[]) {
  491. char jack_name[30] = "alsa_in";
  492. char alsa_device[30] = "hw:0";
  493. extern char *optarg;
  494. extern int optind, optopt;
  495. int errflg=0;
  496. int c;
  497. while ((c = getopt(argc, argv, "ivj:r:c:p:n:d:q:m:t:f:F:C:Q:s:")) != -1) {
  498. switch(c) {
  499. case 'j':
  500. strcpy(jack_name,optarg);
  501. break;
  502. case 'r':
  503. sample_rate = atoi(optarg);
  504. break;
  505. case 'c':
  506. num_channels = atoi(optarg);
  507. break;
  508. case 'p':
  509. period_size = atoi(optarg);
  510. break;
  511. case 'n':
  512. num_periods = atoi(optarg);
  513. break;
  514. case 'd':
  515. strcpy(alsa_device,optarg);
  516. break;
  517. case 't':
  518. target_delay = atoi(optarg);
  519. break;
  520. case 'q':
  521. samplerate_quality = atoi(optarg);
  522. break;
  523. case 'm':
  524. max_diff = atoi(optarg);
  525. break;
  526. case 'f':
  527. catch_factor = atoi(optarg);
  528. break;
  529. case 'F':
  530. catch_factor2 = atoi(optarg);
  531. break;
  532. case 'C':
  533. pclamp = (double) atoi(optarg);
  534. break;
  535. case 'Q':
  536. controlquant = (double) atoi(optarg);
  537. break;
  538. case 'v':
  539. verbose = 1;
  540. break;
  541. case 'i':
  542. instrument = 1;
  543. break;
  544. case 's':
  545. smooth_size = atoi(optarg);
  546. break;
  547. case ':':
  548. fprintf(stderr,
  549. "Option -%c requires an operand\n", optopt);
  550. errflg++;
  551. break;
  552. case '?':
  553. fprintf(stderr,
  554. "Unrecognized option: -%c\n", optopt);
  555. errflg++;
  556. }
  557. }
  558. if (errflg) {
  559. printUsage();
  560. exit(2);
  561. }
  562. if( (samplerate_quality < 0) || (samplerate_quality > 4) ) {
  563. fprintf (stderr, "invalid samplerate quality\n");
  564. return 1;
  565. }
  566. if ((client = jack_client_open (jack_name, 0, NULL)) == 0) {
  567. fprintf (stderr, "jack server not running?\n");
  568. return 1;
  569. }
  570. /* tell the JACK server to call `process()' whenever
  571. there is work to be done.
  572. */
  573. jack_set_process_callback (client, process, 0);
  574. /* tell the JACK server to call `jack_shutdown()' if
  575. it ever shuts down, either entirely, or if it
  576. just decides to stop calling us.
  577. */
  578. jack_on_shutdown (client, jack_shutdown, 0);
  579. if (jack_set_latency_callback)
  580. jack_set_latency_callback (client, latency_cb, 0);
  581. // get jack sample_rate
  582. jack_sample_rate = jack_get_sample_rate( client );
  583. if( !sample_rate )
  584. sample_rate = jack_sample_rate;
  585. // now open the alsa fd...
  586. alsa_handle = open_audiofd( alsa_device, 1, sample_rate, num_channels, period_size, num_periods);
  587. if( alsa_handle == 0 )
  588. exit(20);
  589. printf( "selected sample format: %s\n", formats[format].name );
  590. static_resample_factor = (double) jack_sample_rate / (double) sample_rate;
  591. resample_lower_limit = static_resample_factor * 0.25;
  592. resample_upper_limit = static_resample_factor * 4.0;
  593. resample_mean = static_resample_factor;
  594. offset_array = malloc( sizeof(double) * smooth_size );
  595. if( offset_array == NULL ) {
  596. fprintf( stderr, "no memory for offset_array !!!\n" );
  597. exit(20);
  598. }
  599. window_array = malloc( sizeof(double) * smooth_size );
  600. if( window_array == NULL ) {
  601. fprintf( stderr, "no memory for window_array !!!\n" );
  602. exit(20);
  603. }
  604. int i;
  605. for( i=0; i<smooth_size; i++ ) {
  606. offset_array[i] = 0.0;
  607. window_array[i] = hann( (double) i / ((double) smooth_size - 1.0) );
  608. }
  609. jack_buffer_size = jack_get_buffer_size( client );
  610. // Setup target delay and max_diff for the normal user, who does not play with them...
  611. if( !target_delay )
  612. target_delay = (num_periods*period_size / 2) + jack_buffer_size/2;
  613. if( !max_diff )
  614. max_diff = num_periods*period_size - target_delay ;
  615. if( max_diff > target_delay ) {
  616. fprintf( stderr, "target_delay (%d) cant be smaller than max_diff(%d)\n", target_delay, max_diff );
  617. exit(20);
  618. }
  619. if( (target_delay+max_diff) > (num_periods*period_size) ) {
  620. fprintf( stderr, "target_delay+max_diff (%d) cant be bigger than buffersize(%d)\n", target_delay+max_diff, num_periods*period_size );
  621. exit(20);
  622. }
  623. // alloc input ports, which are blasted out to alsa...
  624. alloc_ports( num_channels, 0 );
  625. outbuf = malloc( num_periods * period_size * formats[format].sample_size * num_channels );
  626. resampbuf = malloc( num_periods * period_size * sizeof( float ) );
  627. tmpbuf = malloc( 512 * formats[format].sample_size * num_channels );
  628. if ((outbuf == NULL) || (resampbuf == NULL) || (tmpbuf == NULL))
  629. {
  630. fprintf( stderr, "no memory for buffers.\n" );
  631. exit(20);
  632. }
  633. memset( tmpbuf, 0, 512 * formats[format].sample_size * num_channels);
  634. /* tell the JACK server that we are ready to roll */
  635. if (jack_activate (client)) {
  636. fprintf (stderr, "cannot activate client");
  637. return 1;
  638. }
  639. signal( SIGTERM, sigterm_handler );
  640. signal( SIGINT, sigterm_handler );
  641. if( verbose ) {
  642. while(!quit) {
  643. usleep(500000);
  644. if( output_new_delay ) {
  645. printf( "delay = %d\n", output_new_delay );
  646. output_new_delay = 0;
  647. }
  648. printf( "res: %f, \tdiff = %f, \toffset = %f \n", output_resampling_factor, output_diff, output_offset );
  649. }
  650. } else if( instrument ) {
  651. printf( "# n\tresamp\tdiff\toffseti\tintegral\n");
  652. int n=0;
  653. while(!quit) {
  654. usleep(1000);
  655. printf( "%d\t%f\t%f\t%f\t%f\n", n++, output_resampling_factor, output_diff, output_offset, output_integral );
  656. }
  657. } else {
  658. while(!quit)
  659. {
  660. usleep(500000);
  661. if( output_new_delay ) {
  662. printf( "delay = %d\n", output_new_delay );
  663. output_new_delay = 0;
  664. }
  665. }
  666. }
  667. jack_deactivate( client );
  668. jack_client_close (client);
  669. exit (0);
  670. }