Correct JackPortAudioDriver.
[jack2.git] / example-clients / simple_client.c
bloba9f7e64b59f475723b18c137c9229d569820e53e
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 <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12 #include <signal.h>
13 #ifndef WIN32
14 #include <unistd.h>
15 #endif
16 #include <jack/jack.h>
18 jack_port_t *output_port1, *output_port2;
19 jack_client_t *client;
21 #ifndef M_PI
22 #define M_PI (3.14159265)
23 #endif
25 #define TABLE_SIZE (200)
26 typedef struct
28 float sine[TABLE_SIZE];
29 int left_phase;
30 int right_phase;
32 paTestData;
34 static void signal_handler(int sig)
36 jack_client_close(client);
37 fprintf(stderr, "signal received, exiting ...\n");
38 exit(0);
41 /**
42 * The process callback for this JACK application is called in a
43 * special realtime thread once for each audio cycle.
45 * This client follows a simple rule: when the JACK transport is
46 * running, copy the input port to the output. When it stops, exit.
49 int
50 process (jack_nframes_t nframes, void *arg)
52 jack_default_audio_sample_t *out1, *out2;
53 paTestData *data = (paTestData*)arg;
54 int i;
56 out1 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port1, nframes);
57 out2 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port2, nframes);
59 for( i=0; i<nframes; i++ )
61 out1[i] = data->sine[data->left_phase]; /* left */
62 out2[i] = data->sine[data->right_phase]; /* right */
63 data->left_phase += 1;
64 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
65 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
66 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
69 return 0;
72 /**
73 * JACK calls this shutdown_callback if the server ever shuts down or
74 * decides to disconnect the client.
76 void
77 jack_shutdown (void *arg)
79 exit (1);
82 int
83 main (int argc, char *argv[])
85 const char **ports;
86 const char *client_name;
87 const char *server_name = NULL;
88 jack_options_t options = JackNullOption;
89 jack_status_t status;
90 paTestData data;
91 int i;
93 if (argc >= 2) { /* client name specified? */
94 client_name = argv[1];
95 if (argc >= 3) { /* server name specified? */
96 server_name = argv[2];
97 int my_option = JackNullOption | JackServerName;
98 options = (jack_options_t)my_option;
100 } else { /* use basename of argv[0] */
101 client_name = strrchr(argv[0], '/');
102 if (client_name == 0) {
103 client_name = argv[0];
104 } else {
105 client_name++;
109 for( i=0; i<TABLE_SIZE; i++ )
111 data.sine[i] = 0.2 * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
113 data.left_phase = data.right_phase = 0;
116 /* open a client connection to the JACK server */
118 client = jack_client_open (client_name, options, &status, server_name);
119 if (client == NULL) {
120 fprintf (stderr, "jack_client_open() failed, "
121 "status = 0x%2.0x\n", status);
122 if (status & JackServerFailed) {
123 fprintf (stderr, "Unable to connect to JACK server\n");
125 exit (1);
127 if (status & JackServerStarted) {
128 fprintf (stderr, "JACK server started\n");
130 if (status & JackNameNotUnique) {
131 client_name = jack_get_client_name(client);
132 fprintf (stderr, "unique name `%s' assigned\n", client_name);
135 /* tell the JACK server to call `process()' whenever
136 there is work to be done.
139 jack_set_process_callback (client, process, &data);
141 /* tell the JACK server to call `jack_shutdown()' if
142 it ever shuts down, either entirely, or if it
143 just decides to stop calling us.
146 jack_on_shutdown (client, jack_shutdown, 0);
148 /* create two ports */
150 output_port1 = jack_port_register (client, "output1",
151 JACK_DEFAULT_AUDIO_TYPE,
152 JackPortIsOutput, 0);
154 output_port2 = jack_port_register (client, "output2",
155 JACK_DEFAULT_AUDIO_TYPE,
156 JackPortIsOutput, 0);
158 if ((output_port1 == NULL) || (output_port2 == NULL)) {
159 fprintf(stderr, "no more JACK ports available\n");
160 exit (1);
163 /* Tell the JACK server that we are ready to roll. Our
164 * process() callback will start running now. */
166 if (jack_activate (client)) {
167 fprintf (stderr, "cannot activate client");
168 exit (1);
171 /* Connect the ports. You can't do this before the client is
172 * activated, because we can't make connections to clients
173 * that aren't running. Note the confusing (but necessary)
174 * orientation of the driver backend ports: playback ports are
175 * "input" to the backend, and capture ports are "output" from
176 * it.
179 ports = jack_get_ports (client, NULL, NULL,
180 JackPortIsPhysical|JackPortIsInput);
181 if (ports == NULL) {
182 fprintf(stderr, "no physical playback ports\n");
183 exit (1);
186 if (jack_connect (client, jack_port_name (output_port1), ports[0])) {
187 fprintf (stderr, "cannot connect output ports\n");
190 if (jack_connect (client, jack_port_name (output_port2), ports[1])) {
191 fprintf (stderr, "cannot connect output ports\n");
194 jack_free (ports);
196 /* install a signal handler to properly quits jack client */
197 #ifdef WIN32
198 signal(SIGINT, signal_handler);
199 signal(SIGABRT, signal_handler);
200 signal(SIGTERM, signal_handler);
201 #else
202 signal(SIGQUIT, signal_handler);
203 signal(SIGTERM, signal_handler);
204 signal(SIGHUP, signal_handler);
205 signal(SIGINT, signal_handler);
206 #endif
208 /* keep running until the Ctrl+C */
210 while (1) {
211 #ifdef WIN32
212 Sleep(1000);
213 #else
214 sleep (1);
215 #endif
218 jack_client_close (client);
219 exit (0);