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.

841 lines
23KB

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