Improve computation of the alsa control device name
[jack2.git] / tests / cpu.c
bloba3541db20209872a692c97186b5ff4d6403b0e52
1 /*
2 Copyright (C) 2005 Samuel TRACOL
3 Copyright (C) 2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /** @file jack_cpu.c
23 * @brief This client test the capacity for jackd to kick out a to heavy cpu client.
27 #include <stdio.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <jack/jack.h>
35 jack_port_t *input_port;
36 jack_port_t *output_port;
37 jack_client_t *client;
38 unsigned long sr;
39 jack_nframes_t idle_time = 0;
40 int percent_cpu = 0;
41 int time_to_run = 0;
42 int time_before_run = 0;
43 int time_before_exit = 1;
44 jack_nframes_t cur_buffer_size;
46 /* a simple state machine for this client */
47 volatile enum {
48 Init,
49 Run,
50 Exit
51 } client_state = Init;
53 void usage()
55 fprintf (stderr, "\n"
56 "usage: jack_cpu \n"
57 " [ --name OR -n client_name ]\n"
58 " [ --time OR -t time_to_run (in seconds) ]\n"
59 " [ --delay OR -d delay_before_cpuload__is_applied (in seconds) ]\n"
60 " --cpu OR -c percent_cpu_load (1-99)\n"
64 int update_buffer_size(jack_nframes_t nframes, void *arg)
66 cur_buffer_size = nframes;
67 printf("Buffer size = %d \n", cur_buffer_size);
68 idle_time = (jack_nframes_t) (cur_buffer_size * percent_cpu / 100);
69 printf("CPU load applies as %d sample delay.\n", idle_time);
70 return 0;
73 /**
74 * The process callback for this JACK application is called in a
75 * special realtime thread once for each audio cycle.
79 int process(jack_nframes_t nframes, void *arg)
81 jack_default_audio_sample_t *in, *out;
82 jack_nframes_t start_frame = jack_frame_time(client);
84 in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
85 out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
86 memset(out, 0, sizeof (jack_default_audio_sample_t) * nframes);
88 while ((client_state == Run) && (jack_frame_time(client) < (start_frame + idle_time))) {}
89 return 0;
92 /**
93 * JACK calls this shutdown_callback if the server ever shuts down or
94 * decides to disconnect the client.
97 void jack_shutdown(void *arg)
99 fprintf(stderr, "JACK shut down, exiting ...\n");
100 exit (1);
103 int main(int argc, char *argv[])
105 const char **ports;
106 const char *client_name;
107 int got_time = 0;
108 jack_status_t status;
110 int option_index;
111 int opt;
112 const char *options = "t:t:d:c:";
113 struct option long_options[] =
115 {"time", 1, 0, 't'},
116 {"name", 1, 0, 'n'},
117 {"delay", 1, 0, 'd'},
118 {"cpu", 1, 0, 'c'},
119 {0, 0, 0, 0}
122 client_name = "jack-cpu";
123 while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
124 switch (opt) {
125 case 'n':
126 client_name = optarg;
127 break;
128 case 't':
129 time_to_run = atoi(optarg);
130 break;
131 case 'd':
132 time_before_run = atoi(optarg);
133 break;
134 case 'c':
135 percent_cpu = atoi(optarg);
136 got_time = 1;
137 break;
138 default:
139 fprintf(stderr, "unknown option %c\n", opt);
140 usage();
144 if (!got_time) {
145 fprintf(stderr, "CPU load not specified ! See usage as following :\n");
146 usage();
147 return -1;
150 if (time_to_run != 0)
151 printf("Running jack-cpu for %d seconds...\n", time_to_run);
153 /* open a client connection to the JACK server */
155 client = jack_client_open (client_name, JackNoStartServer, &status);
157 if (client == NULL) {
158 fprintf(stderr, "jack_client_open() failed : is jack server running ?\n");
159 exit(1);
162 cur_buffer_size = jack_get_buffer_size(client);
163 printf("engine buffer size = %d \n", cur_buffer_size);
164 printf("engine sample rate: %d Hz\n", jack_get_sample_rate(client));
165 idle_time = (jack_nframes_t) (cur_buffer_size * percent_cpu / 100);
166 printf("CPU load applies as %d sample delay.\n", idle_time);
168 /* tell the JACK server to call `process()' whenever
169 there is work to be done.
172 jack_set_process_callback(client, process, 0);
174 /* tell the JACK server to call `jack_shutdown()' if
175 it ever shuts down, either entirely, or if it
176 just decides to stop calling us.
179 jack_on_shutdown(client, jack_shutdown, 0);
181 /* create two ports */
183 input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
184 output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
186 printf("registering ports...\n");
187 if ((input_port == NULL) || (output_port == NULL)) {
188 fprintf(stderr, "no more JACK ports available\n");
189 exit(1);
192 if (jack_set_buffer_size_callback(client, update_buffer_size, 0) != 0) {
193 printf("Error when calling buffer_size_callback !");
194 return -1;
197 /* Tell the JACK server that we are ready to roll. Our
198 * process() callback will start running now. */
200 printf("Activating as jackd client...\n");
201 if (jack_activate(client)) {
202 fprintf(stderr, "cannot activate client");
203 exit(1);
206 /* Connect the ports. You can't do this before the client is
207 * activated, because we can't make connections to clients
208 * that aren't running. Note the confusing (but necessary)
209 * orientation of the driver backend ports: playback ports are
210 * "input" to the backend, and capture ports are "output" from
211 * it.
214 ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
215 if (ports == NULL) {
216 fprintf(stderr, "no physical capture ports\n");
217 exit (1);
220 if (jack_connect(client, ports[0], jack_port_name(input_port))) {
221 fprintf (stderr, "cannot connect input ports\n");
223 jack_free(ports);
225 ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
226 if (ports == NULL) {
227 fprintf(stderr, "no physical playback ports\n");
228 exit(1);
231 if (jack_connect(client, jack_port_name (output_port), ports[0])) {
232 fprintf(stderr, "cannot connect output ports\n");
234 jack_free(ports);
236 if (time_before_run == 0) {
237 client_state = Run;
238 printf("Activating cpu load...\n");
241 if (time_to_run !=0)
242 time_before_exit = time_to_run + time_before_run;
244 while (client_state != Exit) {
245 if ((time_before_run > 0) && (client_state == Init))
246 time_before_run--;
247 sleep(1);
248 if ((time_before_run < 1) && (client_state != Run)) {
249 client_state = Run;
250 printf("Activating cpu load...\n");
252 if (time_to_run != 0)
253 time_before_exit--;
254 if (time_before_exit < 1)
255 client_state = Exit;
257 jack_client_close(client);
258 printf("Exiting after a %d seconds run.\n", time_to_run);
259 exit(0);