change version number for last fix
[jack.git] / example-clients / tw.c
blob348f7afc2ce0d8167815a72a7b2d3212a5bc5f0a
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);
66 /**
67 * JACK calls this shutdown_callback if the server ever shuts down or
68 * decides to disconnect the client.
70 void
71 jack_shutdown (void *arg)
73 exit (1);
76 int
77 main (int argc, char *argv[])
79 const char **ports;
80 const char *client_name;
81 const char *server_name = NULL;
82 jack_options_t options = JackNullOption;
83 jack_status_t status;
85 if (argc >= 2) { /* client name specified? */
86 client_name = argv[1];
87 if (argc >= 3) { /* server name specified? */
88 server_name = argv[2];
89 options |= JackServerName;
91 } else { /* use basename of argv[0] */
92 client_name = strrchr(argv[0], '/');
93 if (client_name == 0) {
94 client_name = argv[0];
95 } else {
96 client_name++;
100 /* open a client connection to the JACK server */
102 client = jack_client_open (client_name, options, &status, server_name);
103 if (client == NULL) {
104 fprintf (stderr, "jack_client_open() failed, "
105 "status = 0x%2.0x\n", status);
106 if (status & JackServerFailed) {
107 fprintf (stderr, "Unable to connect to JACK server\n");
109 exit (1);
111 if (status & JackServerStarted) {
112 fprintf (stderr, "JACK server started\n");
114 if (status & JackNameNotUnique) {
115 client_name = jack_get_client_name(client);
116 fprintf (stderr, "unique name `%s' assigned\n", client_name);
119 /* tell the JACK server to call `process()' whenever
120 there is work to be done.
123 jack_set_process_callback (client, process, client);
125 /* tell the JACK server to call `jack_shutdown()' if
126 it ever shuts down, either entirely, or if it
127 just decides to stop calling us.
130 jack_on_shutdown (client, jack_shutdown, 0);
132 /* display the current sample rate.
135 printf ("engine sample rate: %" PRIu32 "\n",
136 jack_get_sample_rate (client));
138 /* create two ports */
140 input_port = jack_port_register (client, "input",
141 JACK_DEFAULT_AUDIO_TYPE,
142 JackPortIsInput, 0);
143 output_port = jack_port_register (client, "output",
144 JACK_DEFAULT_AUDIO_TYPE,
145 JackPortIsOutput, 0);
147 if ((input_port == NULL) || (output_port == NULL)) {
148 fprintf(stderr, "no more JACK ports available\n");
149 exit (1);
152 /* Tell the JACK server that we are ready to roll. Our
153 * process() callback will start running now. */
155 if (jack_activate (client)) {
156 fprintf (stderr, "cannot activate client");
157 exit (1);
160 /* Connect the ports. You can't do this before the client is
161 * activated, because we can't make connections to clients
162 * that aren't running. Note the confusing (but necessary)
163 * orientation of the driver backend ports: playback ports are
164 * "input" to the backend, and capture ports are "output" from
165 * it.
168 ports = jack_get_ports (client, NULL, NULL,
169 JackPortIsPhysical|JackPortIsOutput);
170 if (ports == NULL) {
171 fprintf(stderr, "no physical capture ports\n");
172 exit (1);
175 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
176 fprintf (stderr, "cannot connect input ports\n");
179 free (ports);
181 ports = jack_get_ports (client, NULL, NULL,
182 JackPortIsPhysical|JackPortIsInput);
183 if (ports == NULL) {
184 fprintf(stderr, "no physical playback ports\n");
185 exit (1);
188 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
189 fprintf (stderr, "cannot connect output ports\n");
192 free (ports);
194 /* keep running until the transport stops */
196 while (client_state != Exit) {
197 sleep (1);
200 jack_client_close (client);
201 exit (0);