Correct MIDI ports memory management in JackNetDriver.
[jack2.git] / example-clients / latent_client.c
blob6721783276e263ab79ec69f4b32f9c5608a3e550
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>
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 exit (1);
83 int
84 main (int argc, char *argv[])
86 const char **ports;
87 const char *client_name = "latent";
88 const char *server_name = NULL;
89 jack_options_t options = JackNullOption;
90 jack_status_t status;
93 if (argc == 2)
94 latency = atoi(argv[1]);
96 delay_line = malloc( latency * sizeof(jack_default_audio_sample_t));
97 if (delay_line == NULL) {
98 fprintf (stderr, "no memory");
99 exit(1);
102 memset (delay_line, 0, latency * sizeof(jack_default_audio_sample_t));
104 /* open a client connection to the JACK server */
106 client = jack_client_open (client_name, options, &status, server_name);
107 if (client == NULL) {
108 fprintf (stderr, "jack_client_open() failed, "
109 "status = 0x%2.0x\n", status);
110 if (status & JackServerFailed) {
111 fprintf (stderr, "Unable to connect to JACK server\n");
113 exit (1);
115 if (status & JackServerStarted) {
116 fprintf (stderr, "JACK server started\n");
118 if (status & JackNameNotUnique) {
119 client_name = jack_get_client_name(client);
120 fprintf (stderr, "unique name `%s' assigned\n", client_name);
123 /* tell the JACK server to call `process()' whenever
124 there is work to be done.
127 jack_set_process_callback (client, process, 0);
129 /* tell the JACK server to call `latency()' whenever
130 the latency needs to be recalculated.
132 if (jack_set_latency_callback)
133 jack_set_latency_callback (client, latency_cb, 0);
135 /* tell the JACK server to call `jack_shutdown()' if
136 it ever shuts down, either entirely, or if it
137 just decides to stop calling us.
140 jack_on_shutdown (client, jack_shutdown, 0);
142 /* display the current sample rate.
145 printf ("engine sample rate: %" PRIu32 "\n",
146 jack_get_sample_rate (client));
148 /* create two ports */
150 input_port = jack_port_register (client, "input",
151 JACK_DEFAULT_AUDIO_TYPE,
152 JackPortIsInput, 0);
153 output_port = jack_port_register (client, "output",
154 JACK_DEFAULT_AUDIO_TYPE,
155 JackPortIsOutput, 0);
157 if ((input_port == NULL) || (output_port == NULL)) {
158 fprintf(stderr, "no more JACK ports available\n");
159 exit (1);
162 /* Tell the JACK server that we are ready to roll. Our
163 * process() callback will start running now. */
165 if (jack_activate (client)) {
166 fprintf (stderr, "cannot activate client");
167 exit (1);
170 /* Connect the ports. You can't do this before the client is
171 * activated, because we can't make connections to clients
172 * that aren't running. Note the confusing (but necessary)
173 * orientation of the driver backend ports: playback ports are
174 * "input" to the backend, and capture ports are "output" from
175 * it.
178 ports = jack_get_ports (client, NULL, NULL,
179 JackPortIsPhysical|JackPortIsOutput);
180 if (ports == NULL) {
181 fprintf(stderr, "no physical capture ports\n");
182 exit (1);
185 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
186 fprintf (stderr, "cannot connect input ports\n");
189 free (ports);
191 ports = jack_get_ports (client, NULL, NULL,
192 JackPortIsPhysical|JackPortIsInput);
193 if (ports == NULL) {
194 fprintf(stderr, "no physical playback ports\n");
195 exit (1);
198 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
199 fprintf (stderr, "cannot connect output ports\n");
202 free (ports);
204 /* keep running until stopped by the user */
206 jack_sleep (-1);
208 /* this is never reached but if the program
209 had some other way to exit besides being killed,
210 they would be important to call.
213 jack_client_close (client);
214 exit (0);