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.
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
)
80 fprintf(stderr
, "JACK shut down, exiting ...\n");
85 main (int argc
, char *argv
[])
88 const char *client_name
= "latent";
89 const char *server_name
= NULL
;
90 jack_options_t options
= JackNullOption
;
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");
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");
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
,
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");
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");
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
179 ports
= jack_get_ports (client
, NULL
, NULL
,
180 JackPortIsPhysical
|JackPortIsOutput
);
182 fprintf(stderr
, "no physical capture ports\n");
186 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
187 fprintf (stderr
, "cannot connect input ports\n");
192 ports
= jack_get_ports (client
, NULL
, NULL
,
193 JackPortIsPhysical
|JackPortIsInput
);
195 fprintf(stderr
, "no physical playback ports\n");
199 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
200 fprintf (stderr
, "cannot connect output ports\n");
205 /* keep running until stopped by the user */
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
);