Correct JackPortAudioDriver.
[jack2.git] / example-clients / tw.c
blob623ff2404a08b4c4c1cccb4e36ad4cfa407ecdcc
1 /** @file tw.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 static void signal_handler(int sig)
28 jack_client_close(client);
29 fprintf(stderr, "signal received, exiting ...\n");
30 exit(0);
33 /**
34 * The process callback for this JACK application is called in a
35 * special realtime thread once for each audio cycle.
37 * This client follows a simple rule: when the JACK transport is
38 * running, copy the input port to the output. When it stops, exit.
40 static int
41 _process (jack_nframes_t nframes)
43 jack_default_audio_sample_t *in, *out;
44 jack_transport_state_t ts = jack_transport_query(client, NULL);
46 if (ts == JackTransportRolling) {
48 if (client_state == Init)
49 client_state = Run;
51 in = jack_port_get_buffer (input_port, nframes);
52 out = jack_port_get_buffer (output_port, nframes);
53 memcpy (out, in,
54 sizeof (jack_default_audio_sample_t) * nframes);
56 } else if (ts == JackTransportStopped) {
58 if (client_state == Run) {
59 client_state = Exit;
60 return -1; // to stop the thread
64 return 0;
67 static void* jack_thread(void *arg)
69 jack_client_t* client = (jack_client_t*) arg;
71 while (1) {
73 jack_nframes_t frames = jack_cycle_wait (client);
74 int status = _process(frames);
75 jack_cycle_signal (client, status);
78 Possibly do something else after signaling next clients in the graph
81 /* End condition */
82 if (status != 0)
83 return 0;
86 /* not reached*/
87 return 0;
91 static void* jack_thread(void *arg)
93 jack_client_t* client = (jack_client_t*) arg;
95 while (1) {
96 jack_nframes_t frames;
97 int status;
98 // cycle 1
99 frames = jack_cycle_wait (client);
100 status = _process(frames);
101 jack_cycle_signal (client, status);
102 // cycle 2
103 frames = jack_cycle_wait (client);
104 status = _process(frames);
105 jack_cycle_signal (client, status);
106 // cycle 3
107 frames = jack_cycle_wait (client);
108 status = _process(frames);
109 jack_cycle_signal (client, status);
110 // cycle 4
111 frames = jack_cycle_wait (client);
112 status = _process(frames);
113 jack_cycle_signal (client, status);
116 return 0;
121 * JACK calls this shutdown_callback if the server ever shuts down or
122 * decides to disconnect the client.
124 static void
125 jack_shutdown (void *arg)
127 fprintf(stderr, "JACK shut down, exiting ...\n");
128 exit (1);
132 main (int argc, char *argv[])
134 const char **ports;
135 const char *client_name;
136 const char *server_name = NULL;
137 jack_options_t options = JackNullOption;
138 jack_status_t status;
140 if (argc >= 2) { /* client name specified? */
141 client_name = argv[1];
142 if (argc >= 3) { /* server name specified? */
143 server_name = argv[2];
144 options |= JackServerName;
146 } else { /* use basename of argv[0] */
147 client_name = strrchr(argv[0], '/');
148 if (client_name == 0) {
149 client_name = argv[0];
150 } else {
151 client_name++;
155 /* open a client connection to the JACK server */
157 client = jack_client_open (client_name, options, &status, server_name);
158 if (client == NULL) {
159 fprintf (stderr, "jack_client_open() failed, "
160 "status = 0x%2.0x\n", status);
161 if (status & JackServerFailed) {
162 fprintf (stderr, "Unable to connect to JACK server\n");
164 exit (1);
166 if (status & JackServerStarted) {
167 fprintf (stderr, "JACK server started\n");
169 if (status & JackNameNotUnique) {
170 client_name = jack_get_client_name(client);
171 fprintf (stderr, "unique name `%s' assigned\n", client_name);
174 /* tell the JACK server to call `process()' whenever
175 there is work to be done.
177 if (jack_set_process_thread(client, jack_thread, client) < 0)
178 exit(1);
180 /* tell the JACK server to call `jack_shutdown()' if
181 it ever shuts down, either entirely, or if it
182 just decides to stop calling us.
185 jack_on_shutdown (client, jack_shutdown, 0);
187 /* display the current sample rate.
190 printf ("engine sample rate: %" PRIu32 "\n",
191 jack_get_sample_rate (client));
193 /* create two ports */
195 input_port = jack_port_register (client, "input",
196 JACK_DEFAULT_AUDIO_TYPE,
197 JackPortIsInput, 0);
198 output_port = jack_port_register (client, "output",
199 JACK_DEFAULT_AUDIO_TYPE,
200 JackPortIsOutput, 0);
202 if ((input_port == NULL) || (output_port == NULL)) {
203 fprintf(stderr, "no more JACK ports available\n");
204 exit (1);
207 /* Tell the JACK server that we are ready to roll. Our
208 * process() callback will start running now. */
210 if (jack_activate (client)) {
211 fprintf (stderr, "cannot activate client");
212 exit (1);
215 /* Connect the ports. You can't do this before the client is
216 * activated, because we can't make connections to clients
217 * that aren't running. Note the confusing (but necessary)
218 * orientation of the driver backend ports: playback ports are
219 * "input" to the backend, and capture ports are "output" from
220 * it.
223 ports = jack_get_ports (client, NULL, NULL,
224 JackPortIsPhysical|JackPortIsOutput);
225 if (ports == NULL) {
226 fprintf(stderr, "no physical capture ports\n");
227 exit (1);
230 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
231 fprintf (stderr, "cannot connect input ports\n");
234 jack_free (ports);
236 ports = jack_get_ports (client, NULL, NULL,
237 JackPortIsPhysical|JackPortIsInput);
238 if (ports == NULL) {
239 fprintf(stderr, "no physical playback ports\n");
240 exit (1);
243 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
244 fprintf (stderr, "cannot connect output ports\n");
247 jack_free (ports);
249 /* install a signal handler to properly quits jack client */
250 signal(SIGQUIT, signal_handler);
251 signal(SIGTERM, signal_handler);
252 signal(SIGHUP, signal_handler);
253 signal(SIGINT, signal_handler);
255 /* keep running until the transport stops */
257 while (client_state != Exit) {
258 sleep (1);
261 jack_client_close (client);
262 exit (0);