1 /** @file simple_client.c
3 * @brief This simple client demonstrates the basic features of JACK
4 * as they would be used by many applications.
13 #include <jack/jack.h>
15 jack_port_t
*input_port
;
16 jack_port_t
*output_port
;
17 jack_client_t
*client
;
19 /* a simple state machine for this client */
24 } client_state
= Init
;
26 static void signal_handler(int sig
)
28 jack_client_close(client
);
29 fprintf(stderr
, "signal received, exiting ...\n");
34 * The process callback for this JACK application is called in a
35 * special realtime thread once for each audio cycle.
37 * This client follows a simple rule: when the JACK transport is
38 * running, copy the input port to the output. When it stops, exit.
41 _process (jack_nframes_t nframes
)
43 jack_default_audio_sample_t
*in
, *out
;
44 jack_transport_state_t ts
= jack_transport_query(client
, NULL
);
46 if (ts
== JackTransportRolling
) {
48 if (client_state
== Init
)
51 in
= jack_port_get_buffer (input_port
, nframes
);
52 out
= jack_port_get_buffer (output_port
, nframes
);
54 sizeof (jack_default_audio_sample_t
) * nframes
);
56 } else if (ts
== JackTransportStopped
) {
58 if (client_state
== Run
) {
60 return -1; // to stop the thread
67 static void* jack_thread(void *arg
)
69 jack_client_t
* client
= (jack_client_t
*) arg
;
73 jack_nframes_t frames
= jack_cycle_wait (client
);
74 int status
= _process(frames
);
75 jack_cycle_signal (client
, status
);
78 Possibly do something else after signaling next clients in the graph
91 static void* jack_thread(void *arg)
93 jack_client_t* client = (jack_client_t*) arg;
96 jack_nframes_t frames;
99 frames = jack_cycle_wait (client);
100 status = _process(frames);
101 jack_cycle_signal (client, status);
103 frames = jack_cycle_wait (client);
104 status = _process(frames);
105 jack_cycle_signal (client, status);
107 frames = jack_cycle_wait (client);
108 status = _process(frames);
109 jack_cycle_signal (client, status);
111 frames = jack_cycle_wait (client);
112 status = _process(frames);
113 jack_cycle_signal (client, status);
121 * JACK calls this shutdown_callback if the server ever shuts down or
122 * decides to disconnect the client.
125 jack_shutdown (void *arg
)
131 main (int argc
, char *argv
[])
134 const char *client_name
;
135 const char *server_name
= NULL
;
136 jack_options_t options
= JackNullOption
;
137 jack_status_t status
;
139 if (argc
>= 2) { /* client name specified? */
140 client_name
= argv
[1];
141 if (argc
>= 3) { /* server name specified? */
142 server_name
= argv
[2];
143 options
|= JackServerName
;
145 } else { /* use basename of argv[0] */
146 client_name
= strrchr(argv
[0], '/');
147 if (client_name
== 0) {
148 client_name
= argv
[0];
154 /* open a client connection to the JACK server */
156 client
= jack_client_open (client_name
, options
, &status
, server_name
);
157 if (client
== NULL
) {
158 fprintf (stderr
, "jack_client_open() failed, "
159 "status = 0x%2.0x\n", status
);
160 if (status
& JackServerFailed
) {
161 fprintf (stderr
, "Unable to connect to JACK server\n");
165 if (status
& JackServerStarted
) {
166 fprintf (stderr
, "JACK server started\n");
168 if (status
& JackNameNotUnique
) {
169 client_name
= jack_get_client_name(client
);
170 fprintf (stderr
, "unique name `%s' assigned\n", client_name
);
173 /* tell the JACK server to call `process()' whenever
174 there is work to be done.
176 if (jack_set_process_thread(client
, jack_thread
, client
) < 0)
179 /* tell the JACK server to call `jack_shutdown()' if
180 it ever shuts down, either entirely, or if it
181 just decides to stop calling us.
184 jack_on_shutdown (client
, jack_shutdown
, 0);
186 /* display the current sample rate.
189 printf ("engine sample rate: %" PRIu32
"\n",
190 jack_get_sample_rate (client
));
192 /* create two ports */
194 input_port
= jack_port_register (client
, "input",
195 JACK_DEFAULT_AUDIO_TYPE
,
197 output_port
= jack_port_register (client
, "output",
198 JACK_DEFAULT_AUDIO_TYPE
,
199 JackPortIsOutput
, 0);
201 if ((input_port
== NULL
) || (output_port
== NULL
)) {
202 fprintf(stderr
, "no more JACK ports available\n");
206 /* Tell the JACK server that we are ready to roll. Our
207 * process() callback will start running now. */
209 if (jack_activate (client
)) {
210 fprintf (stderr
, "cannot activate client");
214 /* Connect the ports. You can't do this before the client is
215 * activated, because we can't make connections to clients
216 * that aren't running. Note the confusing (but necessary)
217 * orientation of the driver backend ports: playback ports are
218 * "input" to the backend, and capture ports are "output" from
222 ports
= jack_get_ports (client
, NULL
, NULL
,
223 JackPortIsPhysical
|JackPortIsOutput
);
225 fprintf(stderr
, "no physical capture ports\n");
229 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
230 fprintf (stderr
, "cannot connect input ports\n");
235 ports
= jack_get_ports (client
, NULL
, NULL
,
236 JackPortIsPhysical
|JackPortIsInput
);
238 fprintf(stderr
, "no physical playback ports\n");
242 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
243 fprintf (stderr
, "cannot connect output ports\n");
248 /* install a signal handler to properly quits jack client */
249 signal(SIGQUIT
, signal_handler
);
250 signal(SIGTERM
, signal_handler
);
251 signal(SIGHUP
, signal_handler
);
252 signal(SIGINT
, signal_handler
);
254 /* keep running until the transport stops */
256 while (client_state
!= Exit
) {
260 jack_client_close (client
);