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
;
27 * The process callback for this JACK application is called in a
28 * special realtime thread once for each audio cycle.
30 * This client follows a simple rule: when the JACK transport is
31 * running, copy the input port to the output. When it stops, exit.
34 _process (jack_nframes_t nframes
)
36 jack_default_audio_sample_t
*in
, *out
;
37 jack_transport_state_t ts
= jack_transport_query(client
, NULL
);
39 if (ts
== JackTransportRolling
) {
41 if (client_state
== Init
)
44 in
= jack_port_get_buffer (input_port
, nframes
);
45 out
= jack_port_get_buffer (output_port
, nframes
);
47 sizeof (jack_default_audio_sample_t
) * nframes
);
49 } else if (ts
== JackTransportStopped
) {
51 if (client_state
== Run
)
59 process (jack_nframes_t nframes
, void* arg
)
61 jack_client_t
* client
= (jack_client_t
*) arg
;
63 while ((nframes
= jack_thread_wait (client
, _process (nframes
))) != 0);
69 * JACK calls this shutdown_callback if the server ever shuts down or
70 * decides to disconnect the client.
73 jack_shutdown (void *arg
)
79 main (int argc
, char *argv
[])
82 const char *client_name
;
83 const char *server_name
= NULL
;
84 jack_options_t options
= JackNullOption
;
87 if (argc
>= 2) { /* client name specified? */
88 client_name
= argv
[1];
89 if (argc
>= 3) { /* server name specified? */
90 server_name
= argv
[2];
91 options
|= JackServerName
;
93 } else { /* use basename of argv[0] */
94 client_name
= strrchr(argv
[0], '/');
95 if (client_name
== 0) {
96 client_name
= argv
[0];
102 /* open a client connection to the JACK server */
104 client
= jack_client_open (client_name
, options
, &status
, server_name
);
105 if (client
== NULL
) {
106 fprintf (stderr
, "jack_client_open() failed, "
107 "status = 0x%2.0x\n", status
);
108 if (status
& JackServerFailed
) {
109 fprintf (stderr
, "Unable to connect to JACK server\n");
113 if (status
& JackServerStarted
) {
114 fprintf (stderr
, "JACK server started\n");
116 if (status
& JackNameNotUnique
) {
117 client_name
= jack_get_client_name(client
);
118 fprintf (stderr
, "unique name `%s' assigned\n", client_name
);
121 /* tell the JACK server to call `process()' whenever
122 there is work to be done.
125 jack_set_process_callback (client
, process
, client
);
127 /* tell the JACK server to call `jack_shutdown()' if
128 it ever shuts down, either entirely, or if it
129 just decides to stop calling us.
132 jack_on_shutdown (client
, jack_shutdown
, 0);
134 /* display the current sample rate.
137 printf ("engine sample rate: %" PRIu32
"\n",
138 jack_get_sample_rate (client
));
140 /* create two ports */
142 input_port
= jack_port_register (client
, "input",
143 JACK_DEFAULT_AUDIO_TYPE
,
145 output_port
= jack_port_register (client
, "output",
146 JACK_DEFAULT_AUDIO_TYPE
,
147 JackPortIsOutput
, 0);
149 if ((input_port
== NULL
) || (output_port
== NULL
)) {
150 fprintf(stderr
, "no more JACK ports available\n");
154 /* Tell the JACK server that we are ready to roll. Our
155 * process() callback will start running now. */
157 if (jack_activate (client
)) {
158 fprintf (stderr
, "cannot activate client");
162 /* Connect the ports. You can't do this before the client is
163 * activated, because we can't make connections to clients
164 * that aren't running. Note the confusing (but necessary)
165 * orientation of the driver backend ports: playback ports are
166 * "input" to the backend, and capture ports are "output" from
170 ports
= jack_get_ports (client
, NULL
, NULL
,
171 JackPortIsPhysical
|JackPortIsOutput
);
173 fprintf(stderr
, "no physical capture ports\n");
177 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
178 fprintf (stderr
, "cannot connect input ports\n");
183 ports
= jack_get_ports (client
, NULL
, NULL
,
184 JackPortIsPhysical
|JackPortIsInput
);
186 fprintf(stderr
, "no physical playback ports\n");
190 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
191 fprintf (stderr
, "cannot connect output ports\n");
196 /* keep running until the transport stops */
198 while (client_state
!= Exit
) {
202 jack_client_close (client
);