int main (int ac, char *av []) { float t; jack_status_t s; jack_handle = jack_client_open ("jack_delay", JackNoStartServer, &s); if (jack_handle == 0) { fprintf (stderr, "Can't connect to Jack, is the server running ?\n"); exit (1); } mtdm = mtdm_new(jack_get_sample_rate(jack_handle)); jack_set_process_callback (jack_handle, jack_callback, 0); if (jack_set_latency_callback) jack_set_latency_callback (jack_handle, latency_cb, 0); jack_capt = jack_port_register (jack_handle, "in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); jack_play = jack_port_register (jack_handle, "out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); t = 1000.0f / jack_get_sample_rate (jack_handle); if (jack_activate (jack_handle)) { fprintf(stderr, "Can't activate Jack"); return 1; } while (1) { #ifdef WIN32 Sleep (250); #else usleep (250000); #endif if (mtdm_resolve (mtdm) < 0) printf ("Signal below threshold...\n"); else { jack_nframes_t systemic_latency; if (mtdm->_err > 0.3) { mtdm_invert ( mtdm ); mtdm_resolve ( mtdm ); } systemic_latency = (jack_nframes_t) floor (mtdm->_del - (capture_latency.max + playback_latency.max)); printf ("%10.3lf frames %10.3lf ms total roundtrip latency\n\textra loopback latency: %u frames\n\tuse %u for the backend arguments -I and -O", mtdm->_del, mtdm->_del * t, systemic_latency, systemic_latency/2); if (mtdm->_err > 0.2) printf (" ??"); if (mtdm->_inv) printf (" Inv"); printf ("\n"); } } return 0; }
void gojack_generic_callback_sync_destroy(struct gojack_generic_callback_sync *sync) { jack_set_freewheel_callback(sync->client, NULL, NULL); jack_set_buffer_size_callback(sync->client, NULL, NULL); jack_set_sample_rate_callback(sync->client, NULL, NULL); jack_set_client_registration_callback(sync->client, NULL, NULL); jack_set_port_registration_callback(sync->client, NULL, NULL); jack_set_port_connect_callback(sync->client, NULL, NULL); jack_set_port_rename_callback(sync->client, NULL, NULL); jack_set_graph_order_callback(sync->client, NULL, NULL); jack_set_xrun_callback(sync->client, NULL, NULL); jack_set_latency_callback(sync->client, NULL, NULL); cgo_callback_sync_destroy(sync->base); pthread_mutex_destroy(&sync->lock); free(sync); }
int main (int argc, char *argv[]) { char jack_name[64] = "alsa_out"; char alsa_device[64] = "hw:0"; char server_name[64] = "default"; extern char *optarg; extern int optind, optopt; int errflg=0; int c; while ((c = getopt(argc, argv, "ivj:r:c:p:n:d:q:m:t:f:F:C:Q:s:S:")) != -1) { switch(c) { case 'j': strncpy(jack_name,optarg, sizeof(jack_name)); break; case 'r': sample_rate = atoi(optarg); break; case 'c': num_channels = atoi(optarg); break; case 'p': period_size = atoi(optarg); break; case 'n': num_periods = atoi(optarg); break; case 'd': strncpy(alsa_device,optarg,sizeof(alsa_device)); break; case 't': target_delay = atoi(optarg); break; case 'q': samplerate_quality = atoi(optarg); break; case 'm': max_diff = atoi(optarg); break; case 'f': catch_factor = atoi(optarg); break; case 'F': catch_factor2 = atoi(optarg); break; case 'C': pclamp = (double) atoi(optarg); break; case 'Q': controlquant = (double) atoi(optarg); break; case 'v': verbose = 1; break; case 'i': instrument = 1; break; case 's': smooth_size = atoi(optarg); break; case 'S': strncpy(server_name,optarg,sizeof(server_name)); break; case ':': fprintf(stderr, "Option -%c requires an operand\n", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); errflg++; } } if (errflg) { printUsage(); exit(2); } if( (samplerate_quality < 0) || (samplerate_quality > 4) ) { fprintf (stderr, "invalid samplerate quality\n"); return 1; } if ((client = jack_client_open (jack_name, JackServerName, NULL, server_name)) == 0) { fprintf (stderr, "jack server not running?\n"); return 1; } /* tell the JACK server to call `process()' whenever there is work to be done. */ jack_set_process_callback (client, process, 0); /* tell the JACK server to call `jack_shutdown()' if it ever shuts down, either entirely, or if it just decides to stop calling us. */ jack_on_shutdown (client, jack_shutdown, 0); if (jack_set_latency_callback) jack_set_latency_callback (client, latency_cb, 0); // get jack sample_rate jack_sample_rate = jack_get_sample_rate( client ); if( !sample_rate ) sample_rate = jack_sample_rate; static_resample_factor = (double) sample_rate / (double) jack_sample_rate; resample_lower_limit = static_resample_factor * 0.25; resample_upper_limit = static_resample_factor * 4.0; resample_mean = static_resample_factor; offset_array = malloc( sizeof(double) * smooth_size ); if( offset_array == NULL ) { fprintf( stderr, "no memory for offset_array !!!\n" ); exit(20); } window_array = malloc( sizeof(double) * smooth_size ); if( window_array == NULL ) { fprintf( stderr, "no memory for window_array !!!\n" ); exit(20); } int i; for( i=0; i<smooth_size; i++ ) { offset_array[i] = 0.0; window_array[i] = hann( (double) i / ((double) smooth_size - 1.0) ); } jack_buffer_size = jack_get_buffer_size( client ); // Setup target delay and max_diff for the normal user, who does not play with them... if( !target_delay ) target_delay = (num_periods*period_size / 2) - jack_buffer_size/2; if( !max_diff ) max_diff = target_delay; if( max_diff > target_delay ) { fprintf( stderr, "target_delay (%d) cant be smaller than max_diff(%d)\n", target_delay, max_diff ); exit(20); } if( (target_delay+max_diff) > (num_periods*period_size) ) { fprintf( stderr, "target_delay+max_diff (%d) cant be bigger than buffersize(%d)\n", target_delay+max_diff, num_periods*period_size ); exit(20); } // now open the alsa fd... alsa_handle = open_audiofd( alsa_device, 0, sample_rate, num_channels, period_size, num_periods); if( alsa_handle == 0 ) exit(20); printf( "selected sample format: %s\n", formats[format].name ); // alloc input ports, which are blasted out to alsa... alloc_ports( 0, num_channels ); outbuf = malloc( num_periods * period_size * formats[format].sample_size * num_channels ); resampbuf = malloc( num_periods * period_size * sizeof( float ) ); tmpbuf = malloc( 512 * formats[format].sample_size * num_channels ); if ((outbuf == NULL) || (resampbuf == NULL) || (tmpbuf == NULL)) { fprintf( stderr, "no memory for buffers.\n" ); exit(20); } /* tell the JACK server that we are ready to roll */ if (jack_activate (client)) { fprintf (stderr, "cannot activate client"); return 1; } signal( SIGTERM, sigterm_handler ); signal( SIGINT, sigterm_handler ); if( verbose ) { while(!quit) { usleep(500000); if( output_new_delay ) { printf( "delay = %d\n", output_new_delay ); output_new_delay = 0; } printf( "res: %f, \tdiff = %f, \toffset = %f \n", output_resampling_factor, output_diff, output_offset ); } } else if( instrument ) { printf( "# n\tresamp\tdiff\toffseti\tintegral\n"); int n=0; while(!quit) { usleep(1000); printf( "%d\t%f\t%f\t%f\t%f\n", n++, output_resampling_factor, output_diff, output_offset, output_integral ); } } else { while(!quit) { usleep(500000); if( output_new_delay ) { printf( "delay = %d\n", output_new_delay ); output_new_delay = 0; } } } jack_deactivate( client ); jack_client_close (client); exit (0); }
int main (int argc, char *argv[]) { const char **ports; const char *client_name = "latent"; const char *server_name = NULL; jack_options_t options = JackNullOption; jack_status_t status; if (argc == 2) latency = atoi(argv[1]); delay_line = malloc( latency * sizeof(jack_default_audio_sample_t)); if (delay_line == NULL) { fprintf (stderr, "no memory"); exit(1); } memset (delay_line, 0, latency * sizeof(jack_default_audio_sample_t)); /* open a client connection to the JACK server */ client = jack_client_open (client_name, options, &status, server_name); if (client == NULL) { fprintf (stderr, "jack_client_open() failed, " "status = 0x%2.0x\n", status); if (status & JackServerFailed) { fprintf (stderr, "Unable to connect to JACK server\n"); } exit (1); } if (status & JackServerStarted) { fprintf (stderr, "JACK server started\n"); } if (status & JackNameNotUnique) { client_name = jack_get_client_name(client); fprintf (stderr, "unique name `%s' assigned\n", client_name); } /* tell the JACK server to call `process()' whenever there is work to be done. */ jack_set_process_callback (client, process, 0); /* tell the JACK server to call `latency()' whenever the latency needs to be recalculated. */ if (jack_set_latency_callback) jack_set_latency_callback (client, latency_cb, 0); /* tell the JACK server to call `jack_shutdown()' if it ever shuts down, either entirely, or if it just decides to stop calling us. */ jack_on_shutdown (client, jack_shutdown, 0); /* display the current sample rate. */ printf ("engine sample rate: %" PRIu32 "\n", jack_get_sample_rate (client)); /* create two ports */ input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); if ((input_port == NULL) || (output_port == NULL)) { fprintf(stderr, "no more JACK ports available\n"); exit (1); } /* Tell the JACK server that we are ready to roll. Our * process() callback will start running now. */ if (jack_activate (client)) { fprintf (stderr, "cannot activate client"); exit (1); } /* Connect the ports. You can't do this before the client is * activated, because we can't make connections to clients * that aren't running. Note the confusing (but necessary) * orientation of the driver backend ports: playback ports are * "input" to the backend, and capture ports are "output" from * it. */ ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput); if (ports == NULL) { fprintf(stderr, "no physical capture ports\n"); exit (1); } if (jack_connect (client, ports[0], jack_port_name (input_port))) { fprintf (stderr, "cannot connect input ports\n"); } free (ports); ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); if (ports == NULL) { fprintf(stderr, "no physical playback ports\n"); exit (1); } if (jack_connect (client, jack_port_name (output_port), ports[0])) { fprintf (stderr, "cannot connect output ports\n"); } free (ports); /* keep running until stopped by the user */ #ifdef WIN32 Sleep (-1); #else sleep (-1); #endif /* this is never reached but if the program had some other way to exit besides being killed, they would be important to call. */ jack_client_close (client); exit (0); }