check for alsa-lib 1.0.18, now required
[jack.git] / example-clients / simple_client.c
blob79ca4fa57d8e1401541ab245070f30902fd4278e
1 /** @file simple_client.c
3 * @brief This simple client demonstrates the most 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 /**
20 * The process callback for this JACK application is called in a
21 * special realtime thread once for each audio cycle.
23 * This client does nothing more than copy data from its input
24 * port to its output port. It will exit when stopped by
25 * the user (e.g. using Ctrl-C on a unix-ish operating system)
27 int
28 process (jack_nframes_t nframes, void *arg)
30 jack_default_audio_sample_t *in, *out;
32 in = jack_port_get_buffer (input_port, nframes);
33 out = jack_port_get_buffer (output_port, nframes);
34 memcpy (out, in,
35 sizeof (jack_default_audio_sample_t) * nframes);
37 return 0;
40 /**
41 * JACK calls this shutdown_callback if the server ever shuts down or
42 * decides to disconnect the client.
44 void
45 jack_shutdown (void *arg)
47 exit (1);
50 int
51 main (int argc, char *argv[])
53 const char **ports;
54 const char *client_name = "simple";
55 const char *server_name = NULL;
56 jack_options_t options = JackNullOption;
57 jack_status_t status;
59 /* open a client connection to the JACK server */
61 client = jack_client_open (client_name, options, &status, server_name);
62 if (client == NULL) {
63 fprintf (stderr, "jack_client_open() failed, "
64 "status = 0x%2.0x\n", status);
65 if (status & JackServerFailed) {
66 fprintf (stderr, "Unable to connect to JACK server\n");
68 exit (1);
70 if (status & JackServerStarted) {
71 fprintf (stderr, "JACK server started\n");
73 if (status & JackNameNotUnique) {
74 client_name = jack_get_client_name(client);
75 fprintf (stderr, "unique name `%s' assigned\n", client_name);
78 /* tell the JACK server to call `process()' whenever
79 there is work to be done.
82 jack_set_process_callback (client, process, 0);
84 /* tell the JACK server to call `jack_shutdown()' if
85 it ever shuts down, either entirely, or if it
86 just decides to stop calling us.
89 jack_on_shutdown (client, jack_shutdown, 0);
91 /* display the current sample rate.
94 printf ("engine sample rate: %" PRIu32 "\n",
95 jack_get_sample_rate (client));
97 /* create two ports */
99 input_port = jack_port_register (client, "input",
100 JACK_DEFAULT_AUDIO_TYPE,
101 JackPortIsInput, 0);
102 output_port = jack_port_register (client, "output",
103 JACK_DEFAULT_AUDIO_TYPE,
104 JackPortIsOutput, 0);
106 if ((input_port == NULL) || (output_port == NULL)) {
107 fprintf(stderr, "no more JACK ports available\n");
108 exit (1);
111 /* Tell the JACK server that we are ready to roll. Our
112 * process() callback will start running now. */
114 if (jack_activate (client)) {
115 fprintf (stderr, "cannot activate client");
116 exit (1);
119 /* Connect the ports. You can't do this before the client is
120 * activated, because we can't make connections to clients
121 * that aren't running. Note the confusing (but necessary)
122 * orientation of the driver backend ports: playback ports are
123 * "input" to the backend, and capture ports are "output" from
124 * it.
127 ports = jack_get_ports (client, NULL, NULL,
128 JackPortIsPhysical|JackPortIsOutput);
129 if (ports == NULL) {
130 fprintf(stderr, "no physical capture ports\n");
131 exit (1);
134 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
135 fprintf (stderr, "cannot connect input ports\n");
138 free (ports);
140 ports = jack_get_ports (client, NULL, NULL,
141 JackPortIsPhysical|JackPortIsInput);
142 if (ports == NULL) {
143 fprintf(stderr, "no physical playback ports\n");
144 exit (1);
147 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
148 fprintf (stderr, "cannot connect output ports\n");
151 free (ports);
153 /* keep running until stopped by the user */
155 sleep (-1);
157 /* this is never reached but if the program
158 had some other way to exit besides being killed,
159 they would be important to call.
162 jack_client_close (client);
163 exit (0);