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.
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;
25 #define jack_sleep(val) Sleep((val))
27 #define jack_sleep(val) usleep((val) * 1000)
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)
39 process (jack_nframes_t nframes
, void *arg
)
41 jack_default_audio_sample_t
*in
, *out
;
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
;
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
);
64 jack_port_set_latency_range (output_port
, mode
, &range
);
66 jack_port_get_latency_range (output_port
, mode
, &range
);
69 jack_port_set_latency_range (input_port
, mode
, &range
);
74 * JACK calls this shutdown_callback if the server ever shuts down or
75 * decides to disconnect the client.
78 jack_shutdown (void *arg
)
84 main (int argc
, char *argv
[])
87 const char *client_name
= "latent";
88 const char *server_name
= NULL
;
89 jack_options_t options
= JackNullOption
;
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");
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");
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
,
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");
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");
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
178 ports
= jack_get_ports (client
, NULL
, NULL
,
179 JackPortIsPhysical
|JackPortIsOutput
);
181 fprintf(stderr
, "no physical capture ports\n");
185 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
186 fprintf (stderr
, "cannot connect input ports\n");
191 ports
= jack_get_ports (client
, NULL
, NULL
,
192 JackPortIsPhysical
|JackPortIsInput
);
194 fprintf(stderr
, "no physical playback ports\n");
198 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
199 fprintf (stderr
, "cannot connect output ports\n");
204 /* keep running until stopped by the user */
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
);