Merge pull request #23 from jackaudio/device_reservation_fixes
[jack2.git] / example-clients / bufsize.c
blob771fd5a8d984b9acdf57b8dfa838f37527a4c279
1 /*
2 * bufsize.c -- change JACK buffer size.
4 * Copyright (C) 2003 Jack O'Quin.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <stdio.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <jack/jack.h>
28 #include <jack/transport.h>
30 char *package; /* program name */
31 jack_client_t *client;
32 jack_nframes_t nframes;
33 int just_print_bufsize=0;
35 void jack_shutdown(void *arg)
37 fprintf(stderr, "JACK shut down, exiting ...\n");
38 exit(1);
41 void signal_handler(int sig)
43 jack_client_close(client);
44 fprintf(stderr, "signal received, exiting ...\n");
45 exit(0);
48 void parse_arguments(int argc, char *argv[])
51 /* basename $0 */
52 package = strrchr(argv[0], '/');
53 if (package == 0)
54 package = argv[0];
55 else
56 package++;
58 if (argc==1) {
59 just_print_bufsize = 1;
60 return;
62 if (argc < 2) {
63 fprintf(stderr, "usage: %s <bufsize>\n", package);
64 exit(9);
67 if (strspn (argv[1], "0123456789") != strlen (argv[1])) {
68 fprintf(stderr, "usage: %s <bufsize>\n", package);
69 exit(8);
72 nframes = strtoul(argv[1], NULL, 0);
73 if (errno == ERANGE) {
74 fprintf(stderr, "%s: invalid buffer size: %s (range is 1-8192)\n",
75 package, argv[1]);
76 exit(2);
79 if (nframes < 1 || nframes > 8182) {
80 fprintf(stderr, "%s: invalid buffer size: %s (range is 1-8192)\n",
81 package, argv[1]);
82 exit(3);
86 int main(int argc, char *argv[])
88 int rc;
90 parse_arguments(argc, argv);
92 /* become a JACK client */
93 if ((client = jack_client_open(package, JackNullOption, NULL)) == 0) {
94 fprintf(stderr, "JACK server not running?\n");
95 exit(1);
98 signal(SIGQUIT, signal_handler);
99 signal(SIGTERM, signal_handler);
100 signal(SIGHUP, signal_handler);
101 signal(SIGINT, signal_handler);
103 jack_on_shutdown(client, jack_shutdown, 0);
105 if (just_print_bufsize) {
106 fprintf(stdout, "buffer size = %d sample rate = %d\n", jack_get_buffer_size(client), jack_get_sample_rate(client));
107 rc=0;
109 else
111 rc = jack_set_buffer_size(client, nframes);
112 if (rc)
113 fprintf(stderr, "jack_set_buffer_size(): %s\n", strerror(rc));
115 jack_client_close(client);
117 return rc;