Lock/Unlock shared memory segments (to test...).
[jack2.git] / example-clients / tw.c
blobb583720eaab26d8380917a198dd53e7570686051
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 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 exit (1);
131 main (int argc, char *argv[])
133 const char **ports;
134 const char *client_name;
135 const char *server_name = NULL;
136 jack_options_t options = JackNullOption;
137 jack_status_t status;
139 if (argc >= 2) { /* client name specified? */
140 client_name = argv[1];
141 if (argc >= 3) { /* server name specified? */
142 server_name = argv[2];
143 options |= JackServerName;
145 } else { /* use basename of argv[0] */
146 client_name = strrchr(argv[0], '/');
147 if (client_name == 0) {
148 client_name = argv[0];
149 } else {
150 client_name++;
154 /* open a client connection to the JACK server */
156 client = jack_client_open (client_name, options, &status, server_name);
157 if (client == NULL) {
158 fprintf (stderr, "jack_client_open() failed, "
159 "status = 0x%2.0x\n", status);
160 if (status & JackServerFailed) {
161 fprintf (stderr, "Unable to connect to JACK server\n");
163 exit (1);
165 if (status & JackServerStarted) {
166 fprintf (stderr, "JACK server started\n");
168 if (status & JackNameNotUnique) {
169 client_name = jack_get_client_name(client);
170 fprintf (stderr, "unique name `%s' assigned\n", client_name);
173 /* tell the JACK server to call `process()' whenever
174 there is work to be done.
176 if (jack_set_process_thread(client, jack_thread, client) < 0)
177 exit(1);
179 /* tell the JACK server to call `jack_shutdown()' if
180 it ever shuts down, either entirely, or if it
181 just decides to stop calling us.
184 jack_on_shutdown (client, jack_shutdown, 0);
186 /* display the current sample rate.
189 printf ("engine sample rate: %" PRIu32 "\n",
190 jack_get_sample_rate (client));
192 /* create two ports */
194 input_port = jack_port_register (client, "input",
195 JACK_DEFAULT_AUDIO_TYPE,
196 JackPortIsInput, 0);
197 output_port = jack_port_register (client, "output",
198 JACK_DEFAULT_AUDIO_TYPE,
199 JackPortIsOutput, 0);
201 if ((input_port == NULL) || (output_port == NULL)) {
202 fprintf(stderr, "no more JACK ports available\n");
203 exit (1);
206 /* Tell the JACK server that we are ready to roll. Our
207 * process() callback will start running now. */
209 if (jack_activate (client)) {
210 fprintf (stderr, "cannot activate client");
211 exit (1);
214 /* Connect the ports. You can't do this before the client is
215 * activated, because we can't make connections to clients
216 * that aren't running. Note the confusing (but necessary)
217 * orientation of the driver backend ports: playback ports are
218 * "input" to the backend, and capture ports are "output" from
219 * it.
222 ports = jack_get_ports (client, NULL, NULL,
223 JackPortIsPhysical|JackPortIsOutput);
224 if (ports == NULL) {
225 fprintf(stderr, "no physical capture ports\n");
226 exit (1);
229 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
230 fprintf (stderr, "cannot connect input ports\n");
233 free (ports);
235 ports = jack_get_ports (client, NULL, NULL,
236 JackPortIsPhysical|JackPortIsInput);
237 if (ports == NULL) {
238 fprintf(stderr, "no physical playback ports\n");
239 exit (1);
242 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
243 fprintf (stderr, "cannot connect output ports\n");
246 free (ports);
248 /* install a signal handler to properly quits jack client */
249 signal(SIGQUIT, signal_handler);
250 signal(SIGTERM, signal_handler);
251 signal(SIGHUP, signal_handler);
252 signal(SIGINT, signal_handler);
254 /* keep running until the transport stops */
256 while (client_state != Exit) {
257 sleep (1);
260 jack_client_close (client);
261 exit (0);