New LockAllMemory and UnlockAllMemory functions.
[jack2.git] / example-clients / simple_client.c
blob20005ca7884be1ee60732787a99d5bfcfd25eff0
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>
12 #include <math.h>
14 //#include <jack/jack.h>
15 #include "jack.h"
17 jack_port_t *input_port;
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;
35 /* a simple state machine for this client */
36 volatile enum {
37 Init,
38 Run,
39 Exit
40 } client_state = Init;
42 /**
43 * The process callback for this JACK application is called in a
44 * special realtime thread once for each audio cycle.
46 * This client follows a simple rule: when the JACK transport is
47 * running, copy the input port to the output. When it stops, exit.
51 int
52 process (jack_nframes_t nframes, void *arg)
54 jack_default_audio_sample_t *in, *out;
55 jack_transport_state_t ts = jack_transport_query(client, NULL);
56 int i;
58 if (ts == JackTransportRolling) {
60 if (client_state == Init)
61 client_state = Run;
63 in = jack_port_get_buffer (input_port, nframes);
64 out = jack_port_get_buffer (output_port, nframes);
66 memcpy (out, in,
67 sizeof (jack_default_audio_sample_t) * nframes);
69 } else if (ts == JackTransportStopped) {
71 if (client_state == Run)
72 client_state = Exit;
75 return 0;
79 int
80 process (jack_nframes_t nframes, void *arg)
82 jack_default_audio_sample_t *in, *out1, *out2;
83 paTestData *data = (paTestData*)arg;
84 int i;
86 in = jack_port_get_buffer (input_port, nframes);
87 out1 = jack_port_get_buffer (output_port1, nframes);
88 out2 = jack_port_get_buffer (output_port2, nframes);
90 for( i=0; i<nframes; i++ )
92 out1[i] = data->sine[data->left_phase]; /* left */
93 out2[i] = data->sine[data->right_phase]; /* right */
94 data->left_phase += 1;
95 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
96 data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
97 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
100 return 0;
104 * JACK calls this shutdown_callback if the server ever shuts down or
105 * decides to disconnect the client.
107 void
108 jack_shutdown (void *arg)
110 exit (1);
114 main (int argc, char *argv[])
116 const char **ports;
117 const char *client_name;
118 const char *server_name = NULL;
119 jack_options_t options = JackNullOption;
120 jack_status_t status;
121 paTestData data;
122 int i;
124 if (argc >= 2) { /* client name specified? */
125 client_name = argv[1];
126 if (argc >= 3) { /* server name specified? */
127 server_name = argv[2];
128 options |= JackServerName;
130 } else { /* use basename of argv[0] */
131 client_name = strrchr(argv[0], '/');
132 if (client_name == 0) {
133 client_name = argv[0];
134 } else {
135 client_name++;
139 for( i=0; i<TABLE_SIZE; i++ )
141 data.sine[i] = 0.2 * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
143 data.left_phase = data.right_phase = 0;
146 /* open a client connection to the JACK server */
148 client = jack_client_open (client_name, options, &status, server_name);
149 if (client == NULL) {
150 fprintf (stderr, "jack_client_open() failed, "
151 "status = 0x%2.0x\n", status);
152 if (status & JackServerFailed) {
153 fprintf (stderr, "Unable to connect to JACK server\n");
155 exit (1);
157 if (status & JackServerStarted) {
158 fprintf (stderr, "JACK server started\n");
160 if (status & JackNameNotUnique) {
161 client_name = jack_get_client_name(client);
162 fprintf (stderr, "unique name `%s' assigned\n", client_name);
165 /* tell the JACK server to call `process()' whenever
166 there is work to be done.
169 jack_set_process_callback (client, process, &data);
171 /* tell the JACK server to call `jack_shutdown()' if
172 it ever shuts down, either entirely, or if it
173 just decides to stop calling us.
176 jack_on_shutdown (client, jack_shutdown, 0);
178 /* display the current sample rate.
181 // printf ("engine sample rate: %" PRIu32 "\n",
182 // jack_get_sample_rate (client));
184 /* create two ports */
186 input_port = jack_port_register (client, "input",
187 JACK_DEFAULT_AUDIO_TYPE,
188 JackPortIsInput, 0);
189 output_port1 = jack_port_register (client, "output1",
190 JACK_DEFAULT_AUDIO_TYPE,
191 JackPortIsOutput, 0);
193 output_port2 = jack_port_register (client, "output2",
194 JACK_DEFAULT_AUDIO_TYPE,
195 JackPortIsOutput, 0);
197 if ((input_port == NULL) || (output_port1 == NULL) || (output_port2 == NULL)) {
198 fprintf(stderr, "no more JACK ports available\n");
199 exit (1);
202 /* Tell the JACK server that we are ready to roll. Our
203 * process() callback will start running now. */
205 if (jack_activate (client)) {
206 fprintf (stderr, "cannot activate client");
207 exit (1);
210 /* Connect the ports. You can't do this before the client is
211 * activated, because we can't make connections to clients
212 * that aren't running. Note the confusing (but necessary)
213 * orientation of the driver backend ports: playback ports are
214 * "input" to the backend, and capture ports are "output" from
215 * it.
218 ports = jack_get_ports (client, NULL, NULL,
219 JackPortIsPhysical|JackPortIsOutput);
220 if (ports == NULL) {
221 fprintf(stderr, "no physical capture ports\n");
222 exit (1);
225 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
226 fprintf (stderr, "cannot connect input ports\n");
229 free (ports);
231 ports = jack_get_ports (client, NULL, NULL,
232 JackPortIsPhysical|JackPortIsInput);
233 if (ports == NULL) {
234 fprintf(stderr, "no physical playback ports\n");
235 exit (1);
238 if (jack_connect (client, jack_port_name (output_port1), ports[0])) {
239 fprintf (stderr, "cannot connect output ports\n");
242 if (jack_connect (client, jack_port_name (output_port2), ports[1])) {
243 fprintf (stderr, "cannot connect output ports\n");
246 free (ports);
248 /* keep running until the transport stops */
250 while (client_state != Exit) {
251 #ifdef WIN32
252 Sleep(1000);
253 #else
254 sleep (1);
255 #endif
258 jack_client_close (client);
259 exit (0);