Main Page | Class List | File List | Class Members

WSOLA4Jack.H

00001 /* Copyright 2001-2005 Matt Flax <flatmax@ieee.org>
00002    This file is part of MFFM Time Scale Modification for Audio.
00003 
00004    MFFM Time Scale Modification for Audio is free software; you can redistribute
00005  it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009    
00010    MFFM Time Scale Modification for Audio is distributed in the hope that it wil
00011 l be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You have received a copy of the GNU General Public License
00017    along with MFFM Time Scale Modification for Audio
00018  */
00019 
00020 #include <stdio.h>
00021 #include <errno.h>
00022 #include <unistd.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 #include <jack/jack.h>
00027 
00028 jack_port_t *input_port;
00029 jack_port_t *output_port;
00030 jack_client_t *client;
00031 
00032 /* a simple state machine for this client */
00033 volatile enum {
00034         Init,
00035         Run,
00036         Exit
00037 } client_state = Init;
00038 
00046 int
00047 process (jack_nframes_t nframes, void *arg)
00048 {
00049         jack_default_audio_sample_t *in, *out;
00050         jack_transport_state_t ts = jack_transport_query(client, NULL);
00051 
00052         if (ts == JackTransportRolling) {
00053 
00054                 if (client_state == Init)
00055                         client_state = Run;
00056 
00057                 in = jack_port_get_buffer (input_port, nframes);
00058                 out = jack_port_get_buffer (output_port, nframes);
00059                 memcpy (out, in,
00060                         sizeof (jack_default_audio_sample_t) * nframes);
00061 
00062         } else if (ts == JackTransportStopped) {
00063 
00064                 if (client_state == Run)
00065                         client_state = Exit;
00066         }
00067 
00068         return 0;      
00069 }
00070 
00075 void
00076 jack_shutdown (void *arg)
00077 {
00078         exit (1);
00079 }
00080 
00081 int
00082 main (int argc, char *argv[])
00083 {
00084         const char **ports;
00085         const char *client_name;
00086         const char *server_name = NULL;
00087         jack_options_t options = JackNullOption;
00088         jack_status_t status;
00089 
00090         if (argc >= 2) {                /* client name specified? */
00091                 client_name = argv[1];
00092                 if (argc >= 3) {        /* server name specified? */
00093                         server_name = argv[2];
00094                         options |= JackServerName;
00095                 }
00096         } else {                        /* use basename of argv[0] */
00097                 client_name = strrchr(argv[0], '/');
00098                 if (client_name == 0) {
00099                         client_name = argv[0];
00100                 } else {
00101                         client_name++;
00102                 }
00103         }
00104 
00105         /* open a client connection to the JACK server */
00106 
00107         client = jack_client_open (client_name, options, &status, server_name);
00108         if (client == NULL) {
00109                 fprintf (stderr, "jack_client_open() failed, "
00110                          "status = 0x%2.0x\n", status);
00111                 if (status & JackServerFailed) {
00112                         fprintf (stderr, "Unable to connect to JACK server\n");
00113                 }
00114                 exit (1);
00115         }
00116         if (status & JackServerStarted) {
00117                 fprintf (stderr, "JACK server started\n");
00118         }
00119         if (status & JackNameNotUnique) {
00120                 client_name = jack_get_client_name(client);
00121                 fprintf (stderr, "unique name `%s' assigned\n", client_name);
00122         }
00123 
00124         /* tell the JACK server to call `process()' whenever
00125            there is work to be done.
00126         */
00127 
00128         jack_set_process_callback (client, process, 0);
00129 
00130         /* tell the JACK server to call `jack_shutdown()' if
00131            it ever shuts down, either entirely, or if it
00132            just decides to stop calling us.
00133         */
00134 
00135         jack_on_shutdown (client, jack_shutdown, 0);
00136 
00137         /* display the current sample rate. 
00138          */
00139 
00140         printf ("engine sample rate: %" PRIu32 "\n",
00141                 jack_get_sample_rate (client));
00142 
00143         /* create two ports */
00144 
00145         input_port = jack_port_register (client, "input",
00146                                          JACK_DEFAULT_AUDIO_TYPE,
00147                                          JackPortIsInput, 0);
00148         output_port = jack_port_register (client, "output",
00149                                           JACK_DEFAULT_AUDIO_TYPE,
00150                                           JackPortIsOutput, 0);
00151 
00152         if ((input_port == NULL) || (output_port == NULL)) {
00153                 fprintf(stderr, "no more JACK ports available\n");
00154                 exit (1);
00155         }
00156 
00157         /* Tell the JACK server that we are ready to roll.  Our
00158          * process() callback will start running now. */
00159 
00160         if (jack_activate (client)) {
00161                 fprintf (stderr, "cannot activate client");
00162                 exit (1);
00163         }
00164 
00165         /* Connect the ports.  You can't do this before the client is
00166          * activated, because we can't make connections to clients
00167          * that aren't running.  Note the confusing (but necessary)
00168          * orientation of the driver backend ports: playback ports are
00169          * "input" to the backend, and capture ports are "output" from
00170          * it.
00171          */
00172 
00173         ports = jack_get_ports (client, NULL, NULL,
00174                                 JackPortIsPhysical|JackPortIsOutput);
00175         if (ports == NULL) {
00176                 fprintf(stderr, "no physical capture ports\n");
00177                 exit (1);
00178         }
00179 
00180         if (jack_connect (client, ports[0], jack_port_name (input_port))) {
00181                 fprintf (stderr, "cannot connect input ports\n");
00182         }
00183 
00184         free (ports);
00185         
00186         ports = jack_get_ports (client, NULL, NULL,
00187                                 JackPortIsPhysical|JackPortIsInput);
00188         if (ports == NULL) {
00189                 fprintf(stderr, "no physical playback ports\n");
00190                 exit (1);
00191         }
00192 
00193         if (jack_connect (client, jack_port_name (output_port), ports[0])) {
00194                 fprintf (stderr, "cannot connect output ports\n");
00195         }
00196 
00197         free (ports);
00198 
00199         /* keep running until the transport stops */
00200 
00201         while (client_state != Exit) {
00202                 sleep (1);
00203         }
00204 
00205         jack_client_close (client);
00206         exit (0);
00207 }

Generated on Thu Apr 21 00:33:47 2005 by  doxygen 1.4.2