Correct JackPortAudioDriver.
[jack2.git] / example-clients / alsa_out.c
blobbe25e4034d4bd7c187bf788a2ec2f21091da798e
1 /** @file simple_client.c
3 * @brief This simple client demonstrates the basic features of JACK
4 * as they would be used by many applications.
5 */
7 #include <stdio.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <signal.h>
14 #include <math.h>
16 #include <jack/jack.h>
17 #include <jack/jslist.h>
18 #include "memops.h"
20 #include "alsa/asoundlib.h"
22 #include <samplerate.h>
24 // Here are the lists of the jack ports...
26 JSList *capture_ports = NULL;
27 JSList *capture_srcs = NULL;
28 JSList *playback_ports = NULL;
29 JSList *playback_srcs = NULL;
30 jack_client_t *client;
32 snd_pcm_t *alsa_handle;
34 int jack_sample_rate;
35 int jack_buffer_size;
37 int quit = 0;
38 double resample_mean = 1.0;
39 double static_resample_factor = 1.0;
40 double resample_lower_limit = 0.25;
41 double resample_upper_limit = 4.0;
43 double *offset_array;
44 double *window_array;
45 int offset_differential_index = 0;
47 double offset_integral = 0;
49 // ------------------------------------------------------ commandline parameters
51 int sample_rate = 0; /* stream rate */
52 int num_channels = 2; /* count of channels */
53 int period_size = 1024;
54 int num_periods = 2;
56 int target_delay = 0; /* the delay which the program should try to approach. */
57 int max_diff = 0; /* the diff value, when a hard readpointer skip should occur */
58 int catch_factor = 100000;
59 int catch_factor2 = 10000;
60 double pclamp = 15.0;
61 double controlquant = 10000.0;
62 int smooth_size = 256;
63 int good_window=0;
64 int verbose = 0;
65 int instrument = 0;
66 int samplerate_quality = 2;
68 // Debug stuff:
70 volatile float output_resampling_factor = 1.0;
71 volatile int output_new_delay = 0;
72 volatile float output_offset = 0.0;
73 volatile float output_integral = 0.0;
74 volatile float output_diff = 0.0;
76 snd_pcm_uframes_t real_buffer_size;
77 snd_pcm_uframes_t real_period_size;
79 // buffers
81 char *tmpbuf;
82 char *outbuf;
83 float *resampbuf;
85 // format selection, and corresponding functions from memops in a nice set of structs.
87 typedef struct alsa_format {
88 snd_pcm_format_t format_id;
89 size_t sample_size;
90 void (*jack_to_soundcard) (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state);
91 void (*soundcard_to_jack) (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip);
92 const char *name;
93 } alsa_format_t;
95 alsa_format_t formats[] = {
96 { SND_PCM_FORMAT_FLOAT_LE, 4, sample_move_dS_floatLE, sample_move_floatLE_sSs, "float" },
97 { SND_PCM_FORMAT_S32, 4, sample_move_d32u24_sS, sample_move_dS_s32u24, "32bit" },
98 { SND_PCM_FORMAT_S24_3LE, 3, sample_move_d24_sS, sample_move_dS_s24, "24bit - real" },
99 { SND_PCM_FORMAT_S24, 4, sample_move_d24_sS, sample_move_dS_s24, "24bit" },
100 { SND_PCM_FORMAT_S16, 2, sample_move_d16_sS, sample_move_dS_s16, "16bit" }
102 #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
103 int format=0;
105 // Alsa stuff... i dont want to touch this bullshit in the next years.... please...
107 static int xrun_recovery(snd_pcm_t *handle, int err) {
108 // printf( "xrun !!!.... %d\n", err );
109 if (err == -EPIPE) { /* under-run */
110 err = snd_pcm_prepare(handle);
111 if (err < 0)
112 printf("Can't recovery from underrun, prepare failed: %s\n", snd_strerror(err));
113 return 0;
114 } else if (err == -EAGAIN) {
115 while ((err = snd_pcm_resume(handle)) == -EAGAIN)
116 usleep(100); /* wait until the suspend flag is released */
117 if (err < 0) {
118 err = snd_pcm_prepare(handle);
119 if (err < 0)
120 printf("Can't recovery from suspend, prepare failed: %s\n", snd_strerror(err));
122 return 0;
124 return err;
127 static int set_hwformat( snd_pcm_t *handle, snd_pcm_hw_params_t *params )
129 int i;
130 int err;
132 for( i=0; i<NUMFORMATS; i++ ) {
133 /* set the sample format */
134 err = snd_pcm_hw_params_set_format(handle, params, formats[i].format_id);
135 if (err == 0) {
136 format = i;
137 return 0;
141 return err;
144 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 ) {
145 int err, dir=0;
146 unsigned int buffer_time;
147 unsigned int period_time;
148 unsigned int rrate;
149 unsigned int rchannels;
151 /* choose all parameters */
152 err = snd_pcm_hw_params_any(handle, params);
153 if (err < 0) {
154 printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
155 return err;
157 /* set the interleaved read/write format */
158 err = snd_pcm_hw_params_set_access(handle, params, access);
159 if (err < 0) {
160 printf("Access type not available for playback: %s\n", snd_strerror(err));
161 return err;
164 /* set the sample format */
165 err = set_hwformat(handle, params);
166 if (err < 0) {
167 printf("Sample format not available for playback: %s\n", snd_strerror(err));
168 return err;
170 /* set the count of channels */
171 rchannels = channels;
172 err = snd_pcm_hw_params_set_channels_near(handle, params, &rchannels);
173 if (err < 0) {
174 printf("Channels count (%i) not available for record: %s\n", channels, snd_strerror(err));
175 return err;
177 if (rchannels != channels) {
178 printf("WARNING: chennel count does not match (requested %d got %d)\n", channels, rchannels);
179 num_channels = rchannels;
181 /* set the stream rate */
182 rrate = rate;
183 err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
184 if (err < 0) {
185 printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
186 return err;
188 if (rrate != rate) {
189 printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, rrate);
190 return -EINVAL;
192 /* set the buffer time */
194 buffer_time = 1000000*(uint64_t)period*nperiods/rate;
195 err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
196 if (err < 0) {
197 printf("Unable to set buffer time %i for playback: %s\n", 1000000*period*nperiods/rate, snd_strerror(err));
198 return err;
200 err = snd_pcm_hw_params_get_buffer_size( params, &real_buffer_size );
201 if (err < 0) {
202 printf("Unable to get buffer size back: %s\n", snd_strerror(err));
203 return err;
205 if( real_buffer_size != nperiods * period ) {
206 printf( "WARNING: buffer size does not match: (requested %d, got %d)\n", nperiods * period, (int) real_buffer_size );
208 /* set the period time */
209 period_time = 1000000*(uint64_t)period/rate;
210 err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
211 if (err < 0) {
212 printf("Unable to set period time %i for playback: %s\n", 1000000*period/rate, snd_strerror(err));
213 return err;
215 err = snd_pcm_hw_params_get_period_size(params, &real_period_size, NULL );
216 if (err < 0) {
217 printf("Unable to get period size back: %s\n", snd_strerror(err));
218 return err;
220 if( real_period_size != period ) {
221 printf( "WARNING: period size does not match: (requested %i, got %i)\n", period, (int)real_period_size );
223 /* write the parameters to device */
224 err = snd_pcm_hw_params(handle, params);
225 if (err < 0) {
226 printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
227 return err;
229 return 0;
232 static int set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams, int period, int nperiods) {
233 int err;
235 /* get the current swparams */
236 err = snd_pcm_sw_params_current(handle, swparams);
237 if (err < 0) {
238 printf("Unable to determine current swparams for capture: %s\n", snd_strerror(err));
239 return err;
241 /* start the transfer when the buffer is full */
242 err = snd_pcm_sw_params_set_start_threshold(handle, swparams, period );
243 if (err < 0) {
244 printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
245 return err;
247 err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, -1 );
248 if (err < 0) {
249 printf("Unable to set start threshold mode for capture: %s\n", snd_strerror(err));
250 return err;
252 /* allow the transfer when at least period_size samples can be processed */
253 err = snd_pcm_sw_params_set_avail_min(handle, swparams, 1 );
254 if (err < 0) {
255 printf("Unable to set avail min for capture: %s\n", snd_strerror(err));
256 return err;
258 /* align all transfers to 1 sample */
259 err = snd_pcm_sw_params_set_xfer_align(handle, swparams, 1);
260 if (err < 0) {
261 printf("Unable to set transfer align for capture: %s\n", snd_strerror(err));
262 return err;
264 /* write the parameters to the playback device */
265 err = snd_pcm_sw_params(handle, swparams);
266 if (err < 0) {
267 printf("Unable to set sw params for capture: %s\n", snd_strerror(err));
268 return err;
270 return 0;
273 // ok... i only need this function to communicate with the alsa bloat api...
275 static snd_pcm_t *open_audiofd( char *device_name, int capture, int rate, int channels, int period, int nperiods ) {
276 int err;
277 snd_pcm_t *handle;
278 snd_pcm_hw_params_t *hwparams;
279 snd_pcm_sw_params_t *swparams;
281 snd_pcm_hw_params_alloca(&hwparams);
282 snd_pcm_sw_params_alloca(&swparams);
284 if ((err = snd_pcm_open(&(handle), device_name, capture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK )) < 0) {
285 printf("Capture open error: %s\n", snd_strerror(err));
286 return NULL;
289 if ((err = set_hwparams(handle, hwparams,SND_PCM_ACCESS_RW_INTERLEAVED, rate, channels, period, nperiods )) < 0) {
290 printf("Setting of hwparams failed: %s\n", snd_strerror(err));
291 return NULL;
293 if ((err = set_swparams(handle, swparams, period, nperiods)) < 0) {
294 printf("Setting of swparams failed: %s\n", snd_strerror(err));
295 return NULL;
298 //snd_pcm_start( handle );
299 //snd_pcm_wait( handle, 200 );
300 int num_null_samples = nperiods * period * channels;
301 char *tmp = alloca( num_null_samples * formats[format].sample_size );
302 memset( tmp, 0, num_null_samples * formats[format].sample_size );
303 snd_pcm_writei( handle, tmp, num_null_samples );
306 return handle;
309 double hann( double x )
311 return 0.5 * (1.0 - cos( 2*M_PI * x ) );
315 * The process callback for this JACK application.
316 * It is called by JACK at the appropriate times.
318 int process (jack_nframes_t nframes, void *arg) {
320 int rlen;
321 int err;
322 snd_pcm_sframes_t delay = target_delay;
323 int i;
325 delay = (num_periods*period_size)-snd_pcm_avail( alsa_handle ) ;
327 delay -= jack_frames_since_cycle_start( client );
328 // Do it the hard way.
329 // this is for compensating xruns etc...
331 if( delay > (target_delay+max_diff) ) {
332 snd_pcm_rewind( alsa_handle, delay - target_delay );
333 output_new_delay = (int) delay;
335 delay = target_delay;
337 // Set the resample_rate... we need to adjust the offset integral, to do this.
338 // first look at the PI controller, this code is just a special case, which should never execute once
339 // everything is swung in.
340 offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
341 // Also clear the array. we are beginning a new control cycle.
342 for( i=0; i<smooth_size; i++ )
343 offset_array[i] = 0.0;
345 if( delay < (target_delay-max_diff) ) {
347 output_new_delay = (int) delay;
349 while ((target_delay-delay) > 0) {
350 snd_pcm_uframes_t to_write = ((target_delay-delay) > 512) ? 512 : (target_delay-delay);
351 snd_pcm_writei( alsa_handle, tmpbuf, to_write );
352 delay += to_write;
355 delay = target_delay;
357 // Set the resample_rate... we need to adjust the offset integral, to do this.
358 offset_integral = - (resample_mean - static_resample_factor) * catch_factor * catch_factor2;
359 // Also clear the array. we are beginning a new control cycle.
360 for( i=0; i<smooth_size; i++ )
361 offset_array[i] = 0.0;
363 /* ok... now we should have target_delay +- max_diff on the alsa side.
365 * calculate the number of frames, we want to get.
368 double offset = delay - target_delay;
370 // Save offset.
371 offset_array[(offset_differential_index++)% smooth_size ] = offset;
373 // Build the mean of the windowed offset array
374 // basically fir lowpassing.
375 double smooth_offset = 0.0;
376 for( i=0; i<smooth_size; i++ )
377 smooth_offset +=
378 offset_array[ (i + offset_differential_index-1) % smooth_size] * window_array[i];
379 smooth_offset /= (double) smooth_size;
381 // this is the integral of the smoothed_offset
382 offset_integral += smooth_offset;
384 // Clamp offset.
385 // the smooth offset still contains unwanted noise
386 // which would go straigth onto the resample coeff.
387 // it only used in the P component and the I component is used for the fine tuning anyways.
388 if( fabs( smooth_offset ) < pclamp )
389 smooth_offset = 0.0;
391 // ok. now this is the PI controller.
392 // u(t) = K * ( e(t) + 1/T \int e(t') dt' )
393 // K = 1/catch_factor and T = catch_factor2
394 double current_resample_factor = static_resample_factor - smooth_offset / (double) catch_factor - offset_integral / (double) catch_factor / (double)catch_factor2;
396 // now quantize this value around resample_mean, so that the noise which is in the integral component doesnt hurt.
397 current_resample_factor = floor( (current_resample_factor - resample_mean) * controlquant + 0.5 ) / controlquant + resample_mean;
399 // Output "instrumentatio" gonna change that to real instrumentation in a few.
400 output_resampling_factor = (float) current_resample_factor;
401 output_diff = (float) smooth_offset;
402 output_integral = (float) offset_integral;
403 output_offset = (float) offset;
405 // Clamp a bit.
406 if( current_resample_factor < resample_lower_limit ) current_resample_factor = resample_lower_limit;
407 if( current_resample_factor > resample_upper_limit ) current_resample_factor = resample_upper_limit;
409 // Now Calculate how many samples we need.
410 rlen = ceil( ((double)nframes) * current_resample_factor )+2;
411 assert( rlen > 2 );
413 // Calculate resample_mean so we can init ourselves to saner values.
414 resample_mean = 0.9999 * resample_mean + 0.0001 * current_resample_factor;
416 * now this should do it...
419 outbuf = alloca( rlen * formats[format].sample_size * num_channels );
421 resampbuf = alloca( rlen * sizeof( float ) );
423 * render jack ports to the outbuf...
426 int chn = 0;
427 JSList *node = playback_ports;
428 JSList *src_node = playback_srcs;
429 SRC_DATA src;
431 while ( node != NULL)
433 jack_port_t *port = (jack_port_t *) node->data;
434 float *buf = jack_port_get_buffer (port, nframes);
436 SRC_STATE *src_state = src_node->data;
438 src.data_in = buf;
439 src.input_frames = nframes;
441 src.data_out = resampbuf;
442 src.output_frames = rlen;
443 src.end_of_input = 0;
445 src.src_ratio = current_resample_factor;
447 src_process( src_state, &src );
449 formats[format].jack_to_soundcard( outbuf + format[formats].sample_size * chn, resampbuf, src.output_frames_gen, num_channels*format[formats].sample_size, NULL);
451 src_node = jack_slist_next (src_node);
452 node = jack_slist_next (node);
453 chn++;
456 // now write the output...
457 again:
458 err = snd_pcm_writei(alsa_handle, outbuf, src.output_frames_gen);
459 //err = snd_pcm_writei(alsa_handle, outbuf, src.output_frames_gen);
460 if( err < 0 ) {
461 printf( "err = %d\n", err );
462 if (xrun_recovery(alsa_handle, err) < 0) {
463 printf("Write error: %s\n", snd_strerror(err));
464 exit(EXIT_FAILURE);
466 goto again;
469 return 0;
473 * the latency callback.
474 * sets up the latencies on the ports.
477 void
478 latency_cb (jack_latency_callback_mode_t mode, void *arg)
480 jack_latency_range_t range;
481 JSList *node;
483 range.min = range.max = target_delay;
485 if (mode == JackCaptureLatency) {
486 for (node = capture_ports; node; node = jack_slist_next (node)) {
487 jack_port_t *port = node->data;
488 jack_port_set_latency_range (port, mode, &range);
490 } else {
491 for (node = playback_ports; node; node = jack_slist_next (node)) {
492 jack_port_t *port = node->data;
493 jack_port_set_latency_range (port, mode, &range);
500 * Allocate the necessary jack ports...
503 void alloc_ports( int n_capture, int n_playback ) {
505 int port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
506 int chn;
507 jack_port_t *port;
508 char buf[32];
510 capture_ports = NULL;
511 for (chn = 0; chn < n_capture; chn++)
513 snprintf (buf, sizeof(buf) - 1, "capture_%u", chn+1);
515 port = jack_port_register (client, buf,
516 JACK_DEFAULT_AUDIO_TYPE,
517 port_flags, 0);
519 if (!port)
521 printf( "jacknet_client: cannot register port for %s", buf);
522 break;
525 capture_srcs = jack_slist_append( capture_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
526 capture_ports = jack_slist_append (capture_ports, port);
529 port_flags = JackPortIsInput;
531 playback_ports = NULL;
532 for (chn = 0; chn < n_playback; chn++)
534 snprintf (buf, sizeof(buf) - 1, "playback_%u", chn+1);
536 port = jack_port_register (client, buf,
537 JACK_DEFAULT_AUDIO_TYPE,
538 port_flags, 0);
540 if (!port)
542 printf( "jacknet_client: cannot register port for %s", buf);
543 break;
546 playback_srcs = jack_slist_append( playback_srcs, src_new( 4-samplerate_quality, 1, NULL ) );
547 playback_ports = jack_slist_append (playback_ports, port);
552 * This is the shutdown callback for this JACK application.
553 * It is called by JACK if the server ever shuts down or
554 * decides to disconnect the client.
557 void jack_shutdown (void *arg) {
559 exit (1);
563 * be user friendly.
564 * be user friendly.
565 * be user friendly.
568 void printUsage() {
569 fprintf(stderr, "usage: alsa_out [options]\n"
570 "\n"
571 " -j <jack name> - client name\n"
572 " -d <alsa_device> \n"
573 " -c <channels> \n"
574 " -p <period_size> \n"
575 " -n <num_period> \n"
576 " -r <sample_rate> \n"
577 " -q <sample_rate quality [0..4]\n"
578 " -m <max_diff> \n"
579 " -t <target_delay> \n"
580 " -i turns on instrumentation\n"
581 " -v turns on printouts\n"
582 "\n");
587 * the main function....
590 void
591 sigterm_handler( int signal )
593 quit = 1;
597 int main (int argc, char *argv[]) {
598 char jack_name[30] = "alsa_out";
599 char alsa_device[30] = "hw:0";
601 extern char *optarg;
602 extern int optind, optopt;
603 int errflg=0;
604 int c;
606 while ((c = getopt(argc, argv, "ivj:r:c:p:n:d:q:m:t:f:F:C:Q:s:")) != -1) {
607 switch(c) {
608 case 'j':
609 strcpy(jack_name,optarg);
610 break;
611 case 'r':
612 sample_rate = atoi(optarg);
613 break;
614 case 'c':
615 num_channels = atoi(optarg);
616 break;
617 case 'p':
618 period_size = atoi(optarg);
619 break;
620 case 'n':
621 num_periods = atoi(optarg);
622 break;
623 case 'd':
624 strcpy(alsa_device,optarg);
625 break;
626 case 't':
627 target_delay = atoi(optarg);
628 break;
629 case 'q':
630 samplerate_quality = atoi(optarg);
631 break;
632 case 'm':
633 max_diff = atoi(optarg);
634 break;
635 case 'f':
636 catch_factor = atoi(optarg);
637 break;
638 case 'F':
639 catch_factor2 = atoi(optarg);
640 break;
641 case 'C':
642 pclamp = (double) atoi(optarg);
643 break;
644 case 'Q':
645 controlquant = (double) atoi(optarg);
646 break;
647 case 'v':
648 verbose = 1;
649 break;
650 case 'i':
651 instrument = 1;
652 break;
653 case 's':
654 smooth_size = atoi(optarg);
655 break;
656 case ':':
657 fprintf(stderr,
658 "Option -%c requires an operand\n", optopt);
659 errflg++;
660 break;
661 case '?':
662 fprintf(stderr,
663 "Unrecognized option: -%c\n", optopt);
664 errflg++;
667 if (errflg) {
668 printUsage();
669 exit(2);
672 if( (samplerate_quality < 0) || (samplerate_quality > 4) ) {
673 fprintf (stderr, "invalid samplerate quality\n");
674 return 1;
676 if ((client = jack_client_open (jack_name, 0, NULL)) == 0) {
677 fprintf (stderr, "jack server not running?\n");
678 return 1;
681 /* tell the JACK server to call `process()' whenever
682 there is work to be done.
685 jack_set_process_callback (client, process, 0);
687 /* tell the JACK server to call `jack_shutdown()' if
688 it ever shuts down, either entirely, or if it
689 just decides to stop calling us.
692 jack_on_shutdown (client, jack_shutdown, 0);
694 if (jack_set_latency_callback)
695 jack_set_latency_callback (client, latency_cb, 0);
697 // get jack sample_rate
699 jack_sample_rate = jack_get_sample_rate( client );
701 if( !sample_rate )
702 sample_rate = jack_sample_rate;
704 static_resample_factor = (double) sample_rate / (double) jack_sample_rate;
705 resample_lower_limit = static_resample_factor * 0.25;
706 resample_upper_limit = static_resample_factor * 4.0;
707 resample_mean = static_resample_factor;
709 offset_array = malloc( sizeof(double) * smooth_size );
710 if( offset_array == NULL ) {
711 fprintf( stderr, "no memory for offset_array !!!\n" );
712 exit(20);
714 window_array = malloc( sizeof(double) * smooth_size );
715 if( window_array == NULL ) {
716 fprintf( stderr, "no memory for window_array !!!\n" );
717 exit(20);
719 int i;
720 for( i=0; i<smooth_size; i++ ) {
721 offset_array[i] = 0.0;
722 window_array[i] = hann( (double) i / ((double) smooth_size - 1.0) );
725 jack_buffer_size = jack_get_buffer_size( client );
726 // Setup target delay and max_diff for the normal user, who does not play with them...
727 if( !target_delay )
728 target_delay = (num_periods*period_size / 2) - jack_buffer_size/2;
730 if( !max_diff )
731 max_diff = target_delay;
733 if( max_diff > target_delay ) {
734 fprintf( stderr, "target_delay (%d) cant be smaller than max_diff(%d)\n", target_delay, max_diff );
735 exit(20);
737 if( (target_delay+max_diff) > (num_periods*period_size) ) {
738 fprintf( stderr, "target_delay+max_diff (%d) cant be bigger than buffersize(%d)\n", target_delay+max_diff, num_periods*period_size );
739 exit(20);
741 // now open the alsa fd...
742 alsa_handle = open_audiofd( alsa_device, 0, sample_rate, num_channels, period_size, num_periods);
743 if( alsa_handle == 0 )
744 exit(20);
746 printf( "selected sample format: %s\n", formats[format].name );
748 // alloc input ports, which are blasted out to alsa...
749 alloc_ports( 0, num_channels );
751 outbuf = malloc( num_periods * period_size * formats[format].sample_size * num_channels );
752 resampbuf = malloc( num_periods * period_size * sizeof( float ) );
753 tmpbuf = malloc( 512 * formats[format].sample_size * num_channels );
755 if ((outbuf == NULL) || (resampbuf == NULL) || (tmpbuf == NULL))
757 fprintf( stderr, "no memory for buffers.\n" );
758 exit(20);
762 /* tell the JACK server that we are ready to roll */
764 if (jack_activate (client)) {
765 fprintf (stderr, "cannot activate client");
766 return 1;
769 signal( SIGTERM, sigterm_handler );
770 signal( SIGINT, sigterm_handler );
772 if( verbose ) {
773 while(!quit) {
774 usleep(500000);
775 if( output_new_delay ) {
776 printf( "delay = %d\n", output_new_delay );
777 output_new_delay = 0;
779 printf( "res: %f, \tdiff = %f, \toffset = %f \n", output_resampling_factor, output_diff, output_offset );
781 } else if( instrument ) {
782 printf( "# n\tresamp\tdiff\toffseti\tintegral\n");
783 int n=0;
784 while(!quit) {
785 usleep(1000);
786 printf( "%d\t%f\t%f\t%f\t%f\n", n++, output_resampling_factor, output_diff, output_offset, output_integral );
788 } else {
789 while(!quit)
791 usleep(500000);
792 if( output_new_delay ) {
793 printf( "delay = %d\n", output_new_delay );
794 output_new_delay = 0;
799 jack_deactivate( client );
800 jack_client_close (client);
801 exit (0);