no more dither when converting from float to 24 bit values - i am not sure how this...
[jack.git] / example-clients / simple_client.c
blobcdc29db4dcf02040897b12977c05d24f08b32921
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.
5 */
7 #include <stdio.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
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 */
20 volatile enum {
21 Init,
22 Run,
23 Exit
24 } client_state = Init;
26 /**
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.
33 int
34 process (jack_nframes_t nframes, void *arg)
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)
42 client_state = Run;
44 in = jack_port_get_buffer (input_port, nframes);
45 out = jack_port_get_buffer (output_port, nframes);
46 memcpy (out, in,
47 sizeof (jack_default_audio_sample_t) * nframes);
49 } else if (ts == JackTransportStopped) {
51 if (client_state == Run)
52 client_state = Exit;
55 return 0;
58 /**
59 * JACK calls this shutdown_callback if the server ever shuts down or
60 * decides to disconnect the client.
62 void
63 jack_shutdown (void *arg)
65 exit (1);
68 int
69 main (int argc, char *argv[])
71 const char **ports;
72 const char *client_name;
73 const char *server_name = NULL;
74 jack_options_t options = JackNullOption;
75 jack_status_t status;
77 if (argc >= 2) { /* client name specified? */
78 client_name = argv[1];
79 if (argc >= 3) { /* server name specified? */
80 server_name = argv[2];
81 options |= JackServerName;
83 } else { /* use basename of argv[0] */
84 client_name = strrchr(argv[0], '/');
85 if (client_name == 0) {
86 client_name = argv[0];
87 } else {
88 client_name++;
92 /* open a client connection to the JACK server */
94 client = jack_client_open (client_name, options, &status, server_name);
95 if (client == NULL) {
96 fprintf (stderr, "jack_client_open() failed, "
97 "status = 0x%2.0x\n", status);
98 if (status & JackServerFailed) {
99 fprintf (stderr, "Unable to connect to JACK server\n");
101 exit (1);
103 if (status & JackServerStarted) {
104 fprintf (stderr, "JACK server started\n");
106 if (status & JackNameNotUnique) {
107 client_name = jack_get_client_name(client);
108 fprintf (stderr, "unique name `%s' assigned\n", client_name);
111 /* tell the JACK server to call `process()' whenever
112 there is work to be done.
115 jack_set_process_callback (client, process, 0);
117 /* tell the JACK server to call `jack_shutdown()' if
118 it ever shuts down, either entirely, or if it
119 just decides to stop calling us.
122 jack_on_shutdown (client, jack_shutdown, 0);
124 /* display the current sample rate.
127 printf ("engine sample rate: %" PRIu32 "\n",
128 jack_get_sample_rate (client));
130 /* create two ports */
132 input_port = jack_port_register (client, "input",
133 JACK_DEFAULT_AUDIO_TYPE,
134 JackPortIsInput, 0);
135 output_port = jack_port_register (client, "output",
136 JACK_DEFAULT_AUDIO_TYPE,
137 JackPortIsOutput, 0);
139 if ((input_port == NULL) || (output_port == NULL)) {
140 fprintf(stderr, "no more JACK ports available\n");
141 exit (1);
144 /* Tell the JACK server that we are ready to roll. Our
145 * process() callback will start running now. */
147 if (jack_activate (client)) {
148 fprintf (stderr, "cannot activate client");
149 exit (1);
152 /* Connect the ports. You can't do this before the client is
153 * activated, because we can't make connections to clients
154 * that aren't running. Note the confusing (but necessary)
155 * orientation of the driver backend ports: playback ports are
156 * "input" to the backend, and capture ports are "output" from
157 * it.
160 ports = jack_get_ports (client, NULL, NULL,
161 JackPortIsPhysical|JackPortIsOutput);
162 if (ports == NULL) {
163 fprintf(stderr, "no physical capture ports\n");
164 exit (1);
167 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
168 fprintf (stderr, "cannot connect input ports\n");
171 free (ports);
173 ports = jack_get_ports (client, NULL, NULL,
174 JackPortIsPhysical|JackPortIsInput);
175 if (ports == NULL) {
176 fprintf(stderr, "no physical playback ports\n");
177 exit (1);
180 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
181 fprintf (stderr, "cannot connect output ports\n");
184 free (ports);
186 /* keep running until the transport stops */
188 while (client_state != Exit) {
189 sleep (1);
192 jack_client_close (client);
193 exit (0);