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);
67 * JACK calls this shutdown_callback if the server ever shuts down or
68 * decides to disconnect the client.
71 jack_shutdown (void *arg
)
77 main (int argc
, char *argv
[])
80 const char *client_name
;
81 const char *server_name
= NULL
;
82 jack_options_t options
= JackNullOption
;
85 if (argc
>= 2) { /* client name specified? */
86 client_name
= argv
[1];
87 if (argc
>= 3) { /* server name specified? */
88 server_name
= argv
[2];
89 options
|= JackServerName
;
91 } else { /* use basename of argv[0] */
92 client_name
= strrchr(argv
[0], '/');
93 if (client_name
== 0) {
94 client_name
= argv
[0];
100 /* open a client connection to the JACK server */
102 client
= jack_client_open (client_name
, options
, &status
, server_name
);
103 if (client
== NULL
) {
104 fprintf (stderr
, "jack_client_open() failed, "
105 "status = 0x%2.0x\n", status
);
106 if (status
& JackServerFailed
) {
107 fprintf (stderr
, "Unable to connect to JACK server\n");
111 if (status
& JackServerStarted
) {
112 fprintf (stderr
, "JACK server started\n");
114 if (status
& JackNameNotUnique
) {
115 client_name
= jack_get_client_name(client
);
116 fprintf (stderr
, "unique name `%s' assigned\n", client_name
);
119 /* tell the JACK server to call `process()' whenever
120 there is work to be done.
123 jack_set_process_callback (client
, process
, client
);
125 /* tell the JACK server to call `jack_shutdown()' if
126 it ever shuts down, either entirely, or if it
127 just decides to stop calling us.
130 jack_on_shutdown (client
, jack_shutdown
, 0);
132 /* display the current sample rate.
135 printf ("engine sample rate: %" PRIu32
"\n",
136 jack_get_sample_rate (client
));
138 /* create two ports */
140 input_port
= jack_port_register (client
, "input",
141 JACK_DEFAULT_AUDIO_TYPE
,
143 output_port
= jack_port_register (client
, "output",
144 JACK_DEFAULT_AUDIO_TYPE
,
145 JackPortIsOutput
, 0);
147 if ((input_port
== NULL
) || (output_port
== NULL
)) {
148 fprintf(stderr
, "no more JACK ports available\n");
152 /* Tell the JACK server that we are ready to roll. Our
153 * process() callback will start running now. */
155 if (jack_activate (client
)) {
156 fprintf (stderr
, "cannot activate client");
160 /* Connect the ports. You can't do this before the client is
161 * activated, because we can't make connections to clients
162 * that aren't running. Note the confusing (but necessary)
163 * orientation of the driver backend ports: playback ports are
164 * "input" to the backend, and capture ports are "output" from
168 ports
= jack_get_ports (client
, NULL
, NULL
,
169 JackPortIsPhysical
|JackPortIsOutput
);
171 fprintf(stderr
, "no physical capture ports\n");
175 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
176 fprintf (stderr
, "cannot connect input ports\n");
181 ports
= jack_get_ports (client
, NULL
, NULL
,
182 JackPortIsPhysical
|JackPortIsInput
);
184 fprintf(stderr
, "no physical playback ports\n");
188 if (jack_connect (client
, jack_port_name (output_port
), ports
[0])) {
189 fprintf (stderr
, "cannot connect output ports\n");
194 /* keep running until the transport stops */
196 while (client_state
!= Exit
) {
200 jack_client_close (client
);