FreeBSD: Add missing OSS option --excl to man page.
[jack2.git] / tools / tw.c
blobedc7ef3194d07769a8da3c131bdaf5bc16cdcce9
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>
12 #include <signal.h>
14 #include <jack/jack.h>
16 jack_port_t *input_port;
17 jack_port_t *output_port;
18 jack_client_t *client;
20 /* a simple state machine for this client */
21 volatile enum {
22 Init,
23 Run,
24 Exit
25 } client_state = Init;
27 static void signal_handler(int sig)
29 jack_client_close(client);
30 fprintf(stderr, "signal received, exiting ...\n");
31 exit(0);
34 /**
35 * The process callback for this JACK application is called in a
36 * special realtime thread once for each audio cycle.
38 * This client follows a simple rule: when the JACK transport is
39 * running, copy the input port to the output. When it stops, exit.
41 static int
42 _process (jack_nframes_t nframes)
44 jack_default_audio_sample_t *in, *out;
45 jack_transport_state_t ts = jack_transport_query(client, NULL);
47 if (ts == JackTransportRolling) {
49 if (client_state == Init)
50 client_state = Run;
52 in = jack_port_get_buffer (input_port, nframes);
53 out = jack_port_get_buffer (output_port, nframes);
54 memcpy (out, in,
55 sizeof (jack_default_audio_sample_t) * nframes);
57 } else if (ts == JackTransportStopped) {
59 if (client_state == Run) {
60 client_state = Exit;
61 return -1; // to stop the thread
65 return 0;
68 static void* jack_thread(void *arg)
70 jack_client_t* client = (jack_client_t*) arg;
72 while (1) {
74 jack_nframes_t frames = jack_cycle_wait (client);
75 int status = _process(frames);
76 jack_cycle_signal (client, status);
79 Possibly do something else after signaling next clients in the graph
82 /* End condition */
83 if (status != 0)
84 return 0;
87 /* not reached*/
88 return 0;
92 static void* jack_thread(void *arg)
94 jack_client_t* client = (jack_client_t*) arg;
96 while (1) {
97 jack_nframes_t frames;
98 int status;
99 // cycle 1
100 frames = jack_cycle_wait (client);
101 status = _process(frames);
102 jack_cycle_signal (client, status);
103 // cycle 2
104 frames = jack_cycle_wait (client);
105 status = _process(frames);
106 jack_cycle_signal (client, status);
107 // cycle 3
108 frames = jack_cycle_wait (client);
109 status = _process(frames);
110 jack_cycle_signal (client, status);
111 // cycle 4
112 frames = jack_cycle_wait (client);
113 status = _process(frames);
114 jack_cycle_signal (client, status);
117 return 0;
122 * JACK calls this shutdown_callback if the server ever shuts down or
123 * decides to disconnect the client.
125 static void
126 jack_shutdown (void *arg)
128 fprintf(stderr, "JACK shut down, exiting ...\n");
129 exit (1);
133 main (int argc, char *argv[])
135 const char **ports;
136 const char *client_name;
137 const char *server_name = NULL;
138 jack_options_t options = JackNullOption;
139 jack_status_t status;
141 if (argc >= 2) { /* client name specified? */
142 client_name = argv[1];
143 if (argc >= 3) { /* server name specified? */
144 server_name = argv[2];
145 options |= JackServerName;
147 } else { /* use basename of argv[0] */
148 client_name = strrchr(argv[0], '/');
149 if (client_name == 0) {
150 client_name = argv[0];
151 } else {
152 client_name++;
156 /* open a client connection to the JACK server */
158 client = jack_client_open (client_name, options, &status, server_name);
159 if (client == NULL) {
160 fprintf (stderr, "jack_client_open() failed, "
161 "status = 0x%2.0x\n", status);
162 if (status & JackServerFailed) {
163 fprintf (stderr, "Unable to connect to JACK server\n");
165 exit (1);
167 if (status & JackServerStarted) {
168 fprintf (stderr, "JACK server started\n");
170 if (status & JackNameNotUnique) {
171 client_name = jack_get_client_name(client);
172 fprintf (stderr, "unique name `%s' assigned\n", client_name);
175 /* tell the JACK server to call `process()' whenever
176 there is work to be done.
178 if (jack_set_process_thread(client, jack_thread, client) < 0)
179 exit(1);
181 /* tell the JACK server to call `jack_shutdown()' if
182 it ever shuts down, either entirely, or if it
183 just decides to stop calling us.
186 jack_on_shutdown (client, jack_shutdown, 0);
188 /* display the current sample rate.
191 printf ("engine sample rate: %" PRIu32 "\n",
192 jack_get_sample_rate (client));
194 /* create two ports */
196 input_port = jack_port_register (client, "input",
197 JACK_DEFAULT_AUDIO_TYPE,
198 JackPortIsInput, 0);
199 output_port = jack_port_register (client, "output",
200 JACK_DEFAULT_AUDIO_TYPE,
201 JackPortIsOutput, 0);
203 if ((input_port == NULL) || (output_port == NULL)) {
204 fprintf(stderr, "no more JACK ports available\n");
205 exit (1);
208 /* Tell the JACK server that we are ready to roll. Our
209 * process() callback will start running now. */
211 if (jack_activate (client)) {
212 fprintf (stderr, "cannot activate client");
213 exit (1);
216 /* Connect the ports. You can't do this before the client is
217 * activated, because we can't make connections to clients
218 * that aren't running. Note the confusing (but necessary)
219 * orientation of the driver backend ports: playback ports are
220 * "input" to the backend, and capture ports are "output" from
221 * it.
224 ports = jack_get_ports (client, NULL, NULL,
225 JackPortIsPhysical|JackPortIsOutput);
226 if (ports == NULL) {
227 fprintf(stderr, "no physical capture ports\n");
228 exit (1);
231 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
232 fprintf (stderr, "cannot connect input ports\n");
235 jack_free (ports);
237 ports = jack_get_ports (client, NULL, NULL,
238 JackPortIsPhysical|JackPortIsInput);
239 if (ports == NULL) {
240 fprintf(stderr, "no physical playback ports\n");
241 exit (1);
244 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
245 fprintf (stderr, "cannot connect output ports\n");
248 jack_free (ports);
250 /* install a signal handler to properly quits jack client */
251 signal(SIGQUIT, signal_handler);
252 signal(SIGTERM, signal_handler);
253 signal(SIGHUP, signal_handler);
254 signal(SIGINT, signal_handler);
256 /* keep running until the transport stops */
258 while (client_state != Exit) {
259 sleep (1);
262 jack_client_close (client);
263 exit (0);