waf: configure option for enforcing autostart method
[jack2.git] / example-clients / latent_client.c
blob3df147e17331ecc1c5917d33c7bf01f299784311
1 /** @file latent_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>
12 #include <inttypes.h>
14 #include <jack/jack.h>
16 jack_port_t *input_port;
17 jack_port_t *output_port;
18 jack_client_t *client;
20 jack_default_audio_sample_t *delay_line;
21 jack_nframes_t delay_index;
22 jack_nframes_t latency = 1024;
24 #ifdef WIN32
25 #define jack_sleep(val) Sleep((val))
26 #else
27 #define jack_sleep(val) usleep((val) * 1000)
28 #endif
30 /**
31 * The process callback for this JACK application is called in a
32 * special realtime thread once for each audio cycle.
34 * This client does nothing more than copy data from its input
35 * port to its output port. It will exit when stopped by
36 * the user (e.g. using Ctrl-C on a unix-ish operating system)
38 int
39 process (jack_nframes_t nframes, void *arg)
41 jack_default_audio_sample_t *in, *out;
42 int k;
44 in = jack_port_get_buffer (input_port, nframes);
45 out = jack_port_get_buffer (output_port, nframes);
47 for (k=0; k<nframes; k++) {
48 out[k] = delay_line[delay_index];
49 delay_line[delay_index] = in[k];
50 delay_index = (delay_index + 1) % latency;
53 return 0;
56 void
57 latency_cb (jack_latency_callback_mode_t mode, void *arg)
59 jack_latency_range_t range;
60 if (mode == JackCaptureLatency) {
61 jack_port_get_latency_range (input_port, mode, &range);
62 range.min += latency;
63 range.max += latency;
64 jack_port_set_latency_range (output_port, mode, &range);
65 } else {
66 jack_port_get_latency_range (output_port, mode, &range);
67 range.min += latency;
68 range.max += latency;
69 jack_port_set_latency_range (input_port, mode, &range);
73 /**
74 * JACK calls this shutdown_callback if the server ever shuts down or
75 * decides to disconnect the client.
77 void
78 jack_shutdown (void *arg)
80 fprintf(stderr, "JACK shut down, exiting ...\n");
81 exit (1);
84 int
85 main (int argc, char *argv[])
87 const char **ports;
88 const char *client_name = "latent";
89 const char *server_name = NULL;
90 jack_options_t options = JackNullOption;
91 jack_status_t status;
94 if (argc == 2)
95 latency = atoi(argv[1]);
97 delay_line = malloc( latency * sizeof(jack_default_audio_sample_t));
98 if (delay_line == NULL) {
99 fprintf (stderr, "no memory");
100 exit(1);
103 memset (delay_line, 0, latency * sizeof(jack_default_audio_sample_t));
105 /* open a client connection to the JACK server */
107 client = jack_client_open (client_name, options, &status, server_name);
108 if (client == NULL) {
109 fprintf (stderr, "jack_client_open() failed, "
110 "status = 0x%2.0x\n", status);
111 if (status & JackServerFailed) {
112 fprintf (stderr, "Unable to connect to JACK server\n");
114 exit (1);
116 if (status & JackServerStarted) {
117 fprintf (stderr, "JACK server started\n");
119 if (status & JackNameNotUnique) {
120 client_name = jack_get_client_name(client);
121 fprintf (stderr, "unique name `%s' assigned\n", client_name);
124 /* tell the JACK server to call `process()' whenever
125 there is work to be done.
128 jack_set_process_callback (client, process, 0);
130 /* tell the JACK server to call `latency()' whenever
131 the latency needs to be recalculated.
133 if (jack_set_latency_callback)
134 jack_set_latency_callback (client, latency_cb, 0);
136 /* tell the JACK server to call `jack_shutdown()' if
137 it ever shuts down, either entirely, or if it
138 just decides to stop calling us.
141 jack_on_shutdown (client, jack_shutdown, 0);
143 /* display the current sample rate.
146 printf ("engine sample rate: %" PRIu32 "\n",
147 jack_get_sample_rate (client));
149 /* create two ports */
151 input_port = jack_port_register (client, "input",
152 JACK_DEFAULT_AUDIO_TYPE,
153 JackPortIsInput, 0);
154 output_port = jack_port_register (client, "output",
155 JACK_DEFAULT_AUDIO_TYPE,
156 JackPortIsOutput, 0);
158 if ((input_port == NULL) || (output_port == NULL)) {
159 fprintf(stderr, "no more JACK ports available\n");
160 exit (1);
163 /* Tell the JACK server that we are ready to roll. Our
164 * process() callback will start running now. */
166 if (jack_activate (client)) {
167 fprintf (stderr, "cannot activate client");
168 exit (1);
171 /* Connect the ports. You can't do this before the client is
172 * activated, because we can't make connections to clients
173 * that aren't running. Note the confusing (but necessary)
174 * orientation of the driver backend ports: playback ports are
175 * "input" to the backend, and capture ports are "output" from
176 * it.
179 ports = jack_get_ports (client, NULL, NULL,
180 JackPortIsPhysical|JackPortIsOutput);
181 if (ports == NULL) {
182 fprintf(stderr, "no physical capture ports\n");
183 exit (1);
186 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
187 fprintf (stderr, "cannot connect input ports\n");
190 free (ports);
192 ports = jack_get_ports (client, NULL, NULL,
193 JackPortIsPhysical|JackPortIsInput);
194 if (ports == NULL) {
195 fprintf(stderr, "no physical playback ports\n");
196 exit (1);
199 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
200 fprintf (stderr, "cannot connect output ports\n");
203 free (ports);
205 /* keep running until stopped by the user */
207 jack_sleep (-1);
209 /* this is never reached but if the program
210 had some other way to exit besides being killed,
211 they would be important to call.
214 jack_client_close (client);
215 exit (0);