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.
16 #include <jack/jack.h>
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
];
34 static void signal_handler(int sig
)
36 jack_client_close(client
);
37 fprintf(stderr
, "signal received, exiting ...\n");
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.
50 process (jack_nframes_t nframes
, void *arg
)
52 jack_default_audio_sample_t
*out1
, *out2
;
53 paTestData
*data
= (paTestData
*)arg
;
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
;
73 * JACK calls this shutdown_callback if the server ever shuts down or
74 * decides to disconnect the client.
77 jack_shutdown (void *arg
)
83 main (int argc
, char *argv
[])
86 const char *client_name
;
87 const char *server_name
= NULL
;
88 jack_options_t options
= JackNullOption
;
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];
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");
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");
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");
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
179 ports
= jack_get_ports (client
, NULL
, NULL
,
180 JackPortIsPhysical
|JackPortIsInput
);
182 fprintf(stderr
, "no physical playback ports\n");
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");
196 /* install a signal handler to properly quits jack client */
198 signal(SIGINT
, signal_handler
);
199 signal(SIGABRT
, signal_handler
);
200 signal(SIGTERM
, signal_handler
);
202 signal(SIGQUIT
, signal_handler
);
203 signal(SIGTERM
, signal_handler
);
204 signal(SIGHUP
, signal_handler
);
205 signal(SIGINT
, signal_handler
);
208 /* keep running until the Ctrl+C */
218 jack_client_close (client
);