3 * @brief This simple client demonstrates the 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 /* a simple state machine for this client */
25 } client_state
= Init
;
27 static void signal_handler(int sig
)
29 jack_client_close(client
);
30 fprintf(stderr
, "signal received, exiting ...\n");
35 * The process callback for this JACK application is called in a
36 * special realtime thread once for each audio cycle.
38 * This client follows a simple rule: when the JACK transport is
39 * running, copy the input port to the output. When it stops, exit.
42 _process (jack_nframes_t nframes
)
44 jack_default_audio_sample_t
*in
, *out
;
45 jack_transport_state_t ts
= jack_transport_query(client
, NULL
);
47 if (ts
== JackTransportRolling
) {
49 if (client_state
== Init
)
52 in
= jack_port_get_buffer (input_port
, nframes
);
53 out
= jack_port_get_buffer (output_port
, nframes
);
55 sizeof (jack_default_audio_sample_t
) * nframes
);
57 } else if (ts
== JackTransportStopped
) {
59 if (client_state
== Run
) {
61 return -1; // to stop the thread
68 static void* jack_thread(void *arg
)
70 jack_client_t
* client
= (jack_client_t
*) arg
;
74 jack_nframes_t frames
= jack_cycle_wait (client
);
75 int status
= _process(frames
);
76 jack_cycle_signal (client
, status
);
79 Possibly do something else after signaling next clients in the graph
92 static void* jack_thread(void *arg)
94 jack_client_t* client = (jack_client_t*) arg;
97 jack_nframes_t frames;
100 frames = jack_cycle_wait (client);
101 status = _process(frames);
102 jack_cycle_signal (client, status);
104 frames = jack_cycle_wait (client);
105 status = _process(frames);
106 jack_cycle_signal (client, status);
108 frames = jack_cycle_wait (client);
109 status = _process(frames);
110 jack_cycle_signal (client, status);
112 frames = jack_cycle_wait (client);
113 status = _process(frames);
114 jack_cycle_signal (client, status);
122 * JACK calls this shutdown_callback if the server ever shuts down or
123 * decides to disconnect the client.
126 jack_shutdown (void *arg
)
128 fprintf(stderr
, "JACK shut down, exiting ...\n");
133 main (int argc
, char *argv
[])
136 const char *client_name
;
137 const char *server_name
= NULL
;
138 jack_options_t options
= JackNullOption
;
139 jack_status_t status
;
141 if (argc
>= 2) { /* client name specified? */
142 client_name
= argv
[1];
143 if (argc
>= 3) { /* server name specified? */
144 server_name
= argv
[2];
145 options
|= JackServerName
;
147 } else { /* use basename of argv[0] */
148 client_name
= strrchr(argv
[0], '/');
149 if (client_name
== 0) {
150 client_name
= argv
[0];
156 /* open a client connection to the JACK server */
158 client
= jack_client_open (client_name
, options
, &status
, server_name
);
159 if (client
== NULL
) {
160 fprintf (stderr
, "jack_client_open() failed, "
161 "status = 0x%2.0x\n", status
);
162 if (status
& JackServerFailed
) {
163 fprintf (stderr
, "Unable to connect to JACK server\n");
167 if (status
& JackServerStarted
) {
168 fprintf (stderr
, "JACK server started\n");
170 if (status
& JackNameNotUnique
) {
171 client_name
= jack_get_client_name(client
);
172 fprintf (stderr
, "unique name `%s' assigned\n", client_name
);
175 /* tell the JACK server to call `process()' whenever
176 there is work to be done.
178 if (jack_set_process_thread(client
, jack_thread
, client
) < 0)
181 /* tell the JACK server to call `jack_shutdown()' if
182 it ever shuts down, either entirely, or if it
183 just decides to stop calling us.
186 jack_on_shutdown (client
, jack_shutdown
, 0);
188 /* display the current sample rate.
191 printf ("engine sample rate: %" PRIu32
"\n",
192 jack_get_sample_rate (client
));
194 /* create two ports */
196 input_port
= jack_port_register (client
, "input",
197 JACK_DEFAULT_AUDIO_TYPE
,
199 output_port
= jack_port_register (client
, "output",
200 JACK_DEFAULT_AUDIO_TYPE
,
201 JackPortIsOutput
, 0);
203 if ((input_port
== NULL
) || (output_port
== NULL
)) {
204 fprintf(stderr
, "no more JACK ports available\n");
208 /* Tell the JACK server that we are ready to roll. Our
209 * process() callback will start running now. */
211 if (jack_activate (client
)) {
212 fprintf (stderr
, "cannot activate client");
216 /* Connect the ports. You can't do this before the client is
217 * activated, because we can't make connections to clients
218 * that aren't running. Note the confusing (but necessary)
219 * orientation of the driver backend ports: playback ports are
220 * "input" to the backend, and capture ports are "output" from
224 ports
= jack_get_ports (client
, NULL
, NULL
,
225 JackPortIsPhysical
|JackPortIsOutput
);
227 fprintf(stderr
, "no physical capture ports\n");
231 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
232 fprintf (stderr
, "cannot connect input ports\n");
237 ports
= jack_get_ports (client
, NULL
, NULL
,
238 JackPortIsPhysical
|JackPortIsInput
);
240 fprintf(stderr
, "no physical playback ports\n");
244 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
245 fprintf (stderr
, "cannot connect output ports\n");
250 /* install a signal handler to properly quits jack client */
251 signal(SIGQUIT
, signal_handler
);
252 signal(SIGTERM
, signal_handler
);
253 signal(SIGHUP
, signal_handler
);
254 signal(SIGINT
, signal_handler
);
256 /* keep running until the transport stops */
258 while (client_state
!= Exit
) {
262 jack_client_close (client
);