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.
14 //#include <jack/jack.h>
17 jack_port_t
*input_port
;
18 jack_port_t
*output_port1
, *output_port2
;
19 jack_client_t
*client
;
22 #define M_PI (3.14159265)
25 #define TABLE_SIZE (200)
28 float sine
[TABLE_SIZE
];
35 /* a simple state machine for this client */
40 } client_state
= Init
;
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.
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);
58 if (ts == JackTransportRolling) {
60 if (client_state == Init)
63 in = jack_port_get_buffer (input_port, nframes);
64 out = jack_port_get_buffer (output_port, nframes);
67 sizeof (jack_default_audio_sample_t) * nframes);
69 } else if (ts == JackTransportStopped) {
71 if (client_state == Run)
80 process (jack_nframes_t nframes
, void *arg
)
82 jack_default_audio_sample_t
*in
, *out1
, *out2
;
83 paTestData
*data
= (paTestData
*)arg
;
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
;
104 * JACK calls this shutdown_callback if the server ever shuts down or
105 * decides to disconnect the client.
108 jack_shutdown (void *arg
)
114 main (int argc
, char *argv
[])
117 const char *client_name
;
118 const char *server_name
= NULL
;
119 jack_options_t options
= JackNullOption
;
120 jack_status_t status
;
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];
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");
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
,
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");
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");
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
218 ports
= jack_get_ports (client
, NULL
, NULL
,
219 JackPortIsPhysical
|JackPortIsOutput
);
221 fprintf(stderr
, "no physical capture ports\n");
225 if (jack_connect (client
, ports
[0], jack_port_name (input_port
))) {
226 fprintf (stderr
, "cannot connect input ports\n");
231 ports
= jack_get_ports (client
, NULL
, NULL
,
232 JackPortIsPhysical
|JackPortIsInput
);
234 fprintf(stderr
, "no physical playback ports\n");
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");
248 /* keep running until the transport stops */
250 while (client_state
!= Exit
) {
258 jack_client_close (client
);