1 /** @file transport_client.c
3 * @brief This client demonstrates very simple use of the JACK
4 * transport API. Compare it with the simple_client example,
5 * which is even simpler. It also demonstrates taking a client
6 * name and optionally server name from the command line, rather
7 * than hard-coding either of these names.
16 #include <jack/jack.h>
18 jack_port_t
*input_port
;
19 jack_port_t
*output_port
;
20 jack_client_t
*client
;
22 /* a simple state machine for this client */
27 } client_state
= Init
;
30 * The process callback for this JACK application is called in a
31 * special realtime thread once for each audio cycle.
33 * This client follows a simple rule: when the JACK transport is
34 * running, copy the input port to the output. When it stops, exit.
37 process (jack_nframes_t nframes
, void *arg
)
39 jack_default_audio_sample_t
*in
, *out
;
40 jack_transport_state_t ts
= jack_transport_query(client
, NULL
);
42 if (ts
== JackTransportRolling
) {
44 if (client_state
== Init
)
47 in
= jack_port_get_buffer (input_port
, nframes
);
48 out
= jack_port_get_buffer (output_port
, nframes
);
50 sizeof (jack_default_audio_sample_t
) * nframes
);
52 } else if (ts
== JackTransportStopped
) {
54 if (client_state
== Run
)
62 * JACK calls this shutdown_callback if the server ever shuts down or
63 * decides to disconnect the client.
66 jack_shutdown (void *arg
)
72 main (int argc
, char *argv
[])
75 const char *client_name
;
76 const char *server_name
= NULL
;
77 jack_options_t options
= JackNullOption
;
80 if (argc
>= 2) { /* client name specified? */
81 client_name
= argv
[1];
82 if (argc
>= 3) { /* server name specified? */
83 server_name
= argv
[2];
84 options
|= JackServerName
;
86 } else { /* use basename of argv[0] */
87 client_name
= strrchr(argv
[0], '/');
88 if (client_name
== 0) {
89 client_name
= argv
[0];
95 /* open a client connection to the JACK server */
97 client
= jack_client_open (client_name
, options
, &status
, server_name
);
99 fprintf (stderr
, "jack_client_open() failed, "
100 "status = 0x%2.0x\n", status
);
101 if (status
& JackServerFailed
) {
102 fprintf (stderr
, "Unable to connect to JACK server\n");
106 if (status
& JackServerStarted
) {
107 fprintf (stderr
, "JACK server started\n");
109 if (status
& JackNameNotUnique
) {
110 client_name
= jack_get_client_name(client
);
111 fprintf (stderr
, "unique name `%s' assigned\n", client_name
);
114 /* tell the JACK server to call `process()' whenever
115 there is work to be done.
118 jack_set_process_callback (client
, process
, 0);
120 /* tell the JACK server to call `jack_shutdown()' if
121 it ever shuts down, either entirely, or if it
122 just decides to stop calling us.
125 jack_on_shutdown (client
, jack_shutdown
, 0);
127 /* display the current sample rate.
130 printf ("engine sample rate: %" PRIu32
"\n",
131 jack_get_sample_rate (client
));
133 /* create two ports */
135 input_port
= jack_port_register (client
, "input",
136 JACK_DEFAULT_AUDIO_TYPE
,
138 output_port
= jack_port_register (client
, "output",
139 JACK_DEFAULT_AUDIO_TYPE
,
140 JackPortIsOutput
, 0);
142 if ((input_port
== NULL
) || (output_port
== NULL
)) {
143 fprintf(stderr
, "no more JACK ports available\n");
147 /* Tell the JACK server that we are ready to roll. Our
148 * process() callback will start running now. */
150 if (jack_activate (client
)) {
151 fprintf (stderr
, "cannot activate client");
155 /* Connect the ports. You can't do this before the client is
156 * activated, because we can't make connections to clients
157 * that aren't running. Note the confusing (but necessary)
158 * orientation of the driver backend ports: playback ports are
159 * "input" to the backend, and capture ports are "output" from
163 ports
= jack_get_ports (client
, NULL
, NULL
,
164 JackPortIsPhysical
|JackPortIsOutput
);
166 fprintf(stderr
, "no physical capture ports\n");
170 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
171 fprintf (stderr
, "cannot connect input ports\n");
176 ports
= jack_get_ports (client
, NULL
, NULL
,
177 JackPortIsPhysical
|JackPortIsInput
);
179 fprintf(stderr
, "no physical playback ports\n");
183 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
184 fprintf (stderr
, "cannot connect output ports\n");
189 /* keep running until the transport stops */
191 while (client_state
!= Exit
) {
195 jack_client_close (client
);