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
)
127 fprintf(stderr
, "JACK shut down, exiting ...\n");
132 main (int argc
, char *argv
[])
135 const char *client_name
;
136 const char *server_name
= NULL
;
137 jack_options_t options
= JackNullOption
;
138 jack_status_t status
;
140 if (argc
>= 2) { /* client name specified? */
141 client_name
= argv
[1];
142 if (argc
>= 3) { /* server name specified? */
143 server_name
= argv
[2];
144 options
|= JackServerName
;
146 } else { /* use basename of argv[0] */
147 client_name
= strrchr(argv
[0], '/');
148 if (client_name
== 0) {
149 client_name
= argv
[0];
155 /* open a client connection to the JACK server */
157 client
= jack_client_open (client_name
, options
, &status
, server_name
);
158 if (client
== NULL
) {
159 fprintf (stderr
, "jack_client_open() failed, "
160 "status = 0x%2.0x\n", status
);
161 if (status
& JackServerFailed
) {
162 fprintf (stderr
, "Unable to connect to JACK server\n");
166 if (status
& JackServerStarted
) {
167 fprintf (stderr
, "JACK server started\n");
169 if (status
& JackNameNotUnique
) {
170 client_name
= jack_get_client_name(client
);
171 fprintf (stderr
, "unique name `%s' assigned\n", client_name
);
174 /* tell the JACK server to call `process()' whenever
175 there is work to be done.
177 if (jack_set_process_thread(client
, jack_thread
, client
) < 0)
180 /* tell the JACK server to call `jack_shutdown()' if
181 it ever shuts down, either entirely, or if it
182 just decides to stop calling us.
185 jack_on_shutdown (client
, jack_shutdown
, 0);
187 /* display the current sample rate.
190 printf ("engine sample rate: %" PRIu32
"\n",
191 jack_get_sample_rate (client
));
193 /* create two ports */
195 input_port
= jack_port_register (client
, "input",
196 JACK_DEFAULT_AUDIO_TYPE
,
198 output_port
= jack_port_register (client
, "output",
199 JACK_DEFAULT_AUDIO_TYPE
,
200 JackPortIsOutput
, 0);
202 if ((input_port
== NULL
) || (output_port
== NULL
)) {
203 fprintf(stderr
, "no more JACK ports available\n");
207 /* Tell the JACK server that we are ready to roll. Our
208 * process() callback will start running now. */
210 if (jack_activate (client
)) {
211 fprintf (stderr
, "cannot activate client");
215 /* Connect the ports. You can't do this before the client is
216 * activated, because we can't make connections to clients
217 * that aren't running. Note the confusing (but necessary)
218 * orientation of the driver backend ports: playback ports are
219 * "input" to the backend, and capture ports are "output" from
223 ports
= jack_get_ports (client
, NULL
, NULL
,
224 JackPortIsPhysical
|JackPortIsOutput
);
226 fprintf(stderr
, "no physical capture ports\n");
230 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
231 fprintf (stderr
, "cannot connect input ports\n");
236 ports
= jack_get_ports (client
, NULL
, NULL
,
237 JackPortIsPhysical
|JackPortIsInput
);
239 fprintf(stderr
, "no physical playback ports\n");
243 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
244 fprintf (stderr
, "cannot connect output ports\n");
249 /* install a signal handler to properly quits jack client */
250 signal(SIGQUIT
, signal_handler
);
251 signal(SIGTERM
, signal_handler
);
252 signal(SIGHUP
, signal_handler
);
253 signal(SIGINT
, signal_handler
);
255 /* keep running until the transport stops */
257 while (client_state
!= Exit
) {
261 jack_client_close (client
);