fix building when PATH_MAX is not defined. (should fix debian bug 320736)
[jack.git] / tools / tw.c
blob3ba62cc228c2c8840a3526d1aa45e7e564e50267
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>
13 #include <jack/jack.h>
15 jack_port_t *input_port;
16 jack_port_t *output_port;
17 jack_client_t *client;
19 /* a simple state machine for this client */
20 volatile enum {
21 Init,
22 Run,
23 Exit
24 } client_state = Init;
26 /**
27 * The process callback for this JACK application is called in a
28 * special realtime thread once for each audio cycle.
30 * This client follows a simple rule: when the JACK transport is
31 * running, copy the input port to the output. When it stops, exit.
33 int
34 _process (jack_nframes_t nframes)
36 jack_default_audio_sample_t *in, *out;
37 jack_transport_state_t ts = jack_transport_query(client, NULL);
39 if (ts == JackTransportRolling) {
41 if (client_state == Init)
42 client_state = Run;
44 in = jack_port_get_buffer (input_port, nframes);
45 out = jack_port_get_buffer (output_port, nframes);
46 memcpy (out, in,
47 sizeof (jack_default_audio_sample_t) * nframes);
49 } else if (ts == JackTransportStopped) {
51 if (client_state == Run)
52 client_state = Exit;
55 return 0;
58 int
59 process (jack_nframes_t nframes, void* arg)
61 jack_client_t* client = (jack_client_t*) arg;
63 while ((nframes = jack_thread_wait (client, _process (nframes))) != 0);
65 return 0;
68 /**
69 * JACK calls this shutdown_callback if the server ever shuts down or
70 * decides to disconnect the client.
72 void
73 jack_shutdown (void *arg)
75 exit (1);
78 int
79 main (int argc, char *argv[])
81 const char **ports;
82 const char *client_name;
83 const char *server_name = NULL;
84 jack_options_t options = JackNullOption;
85 jack_status_t status;
87 if (argc >= 2) { /* client name specified? */
88 client_name = argv[1];
89 if (argc >= 3) { /* server name specified? */
90 server_name = argv[2];
91 options |= JackServerName;
93 } else { /* use basename of argv[0] */
94 client_name = strrchr(argv[0], '/');
95 if (client_name == 0) {
96 client_name = argv[0];
97 } else {
98 client_name++;
102 /* open a client connection to the JACK server */
104 client = jack_client_open (client_name, options, &status, server_name);
105 if (client == NULL) {
106 fprintf (stderr, "jack_client_open() failed, "
107 "status = 0x%2.0x\n", status);
108 if (status & JackServerFailed) {
109 fprintf (stderr, "Unable to connect to JACK server\n");
111 exit (1);
113 if (status & JackServerStarted) {
114 fprintf (stderr, "JACK server started\n");
116 if (status & JackNameNotUnique) {
117 client_name = jack_get_client_name(client);
118 fprintf (stderr, "unique name `%s' assigned\n", client_name);
121 /* tell the JACK server to call `process()' whenever
122 there is work to be done.
125 jack_set_process_callback (client, process, client);
127 /* tell the JACK server to call `jack_shutdown()' if
128 it ever shuts down, either entirely, or if it
129 just decides to stop calling us.
132 jack_on_shutdown (client, jack_shutdown, 0);
134 /* display the current sample rate.
137 printf ("engine sample rate: %" PRIu32 "\n",
138 jack_get_sample_rate (client));
140 /* create two ports */
142 input_port = jack_port_register (client, "input",
143 JACK_DEFAULT_AUDIO_TYPE,
144 JackPortIsInput, 0);
145 output_port = jack_port_register (client, "output",
146 JACK_DEFAULT_AUDIO_TYPE,
147 JackPortIsOutput, 0);
149 if ((input_port == NULL) || (output_port == NULL)) {
150 fprintf(stderr, "no more JACK ports available\n");
151 exit (1);
154 /* Tell the JACK server that we are ready to roll. Our
155 * process() callback will start running now. */
157 if (jack_activate (client)) {
158 fprintf (stderr, "cannot activate client");
159 exit (1);
162 /* Connect the ports. You can't do this before the client is
163 * activated, because we can't make connections to clients
164 * that aren't running. Note the confusing (but necessary)
165 * orientation of the driver backend ports: playback ports are
166 * "input" to the backend, and capture ports are "output" from
167 * it.
170 ports = jack_get_ports (client, NULL, NULL,
171 JackPortIsPhysical|JackPortIsOutput);
172 if (ports == NULL) {
173 fprintf(stderr, "no physical capture ports\n");
174 exit (1);
177 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
178 fprintf (stderr, "cannot connect input ports\n");
181 free (ports);
183 ports = jack_get_ports (client, NULL, NULL,
184 JackPortIsPhysical|JackPortIsInput);
185 if (ports == NULL) {
186 fprintf(stderr, "no physical playback ports\n");
187 exit (1);
190 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
191 fprintf (stderr, "cannot connect output ports\n");
194 free (ports);
196 /* keep running until the transport stops */
198 while (client_state != Exit) {
199 sleep (1);
202 jack_client_close (client);
203 exit (0);