1 /* beep - just what it sounds like, makes the console beep - but with
2 * precision control. See the man page for details.
4 * Try beep -h for command line args
6 * This code is copyright (C) Johnathan Nightingale, 2000.
8 * This code may distributed only under the terms of the GNU Public License
9 * which can be found at http://www.gnu.org/copyleft or in the file COPYING
10 * supplied with this code.
12 * This code is not distributed with warranties of any kind, including implied
13 * warranties of merchantability or fitness for a particular use or ability to
14 * breed pandas in captivity, it just can't be done.
16 * Bug me, I like it: http://johnath.com/ or johnath@johnath.com
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
30 /* I don't know where this number comes from, I admit that freely. A
31 wonderful human named Raine M. Ekman used it in a program that played
32 a tune at the console, and apparently, it's how the kernel likes its
33 sound requests to be phrased. If you see Raine, thank him for me.
35 June 28, email from Peter Tirsek (peter at tirsek dot com):
37 This number represents the fixed frequency of the original PC XT's
38 timer chip (the 8254 AFAIR), which is approximately 1.193 MHz. This
39 number is divided with the desired frequency to obtain a counter value,
40 that is subsequently fed into the timer chip, tied to the PC speaker.
41 The chip decreases this counter at every tick (1.193 MHz) and when it
42 reaches zero, it toggles the state of the speaker (on/off, or in/out),
43 resets the counter to the original value, and starts over. The end
44 result of this is a tone at approximately the desired frequency. :)
46 #ifndef CLOCK_TICK_RATE
47 #define CLOCK_TICK_RATE 1193180
50 #define VERSION_STRING "beep-1.2.2"
52 "Copyright (C) Johnathan Nightingale, 2002. "
53 "Use and Distribution subject to GPL. "
54 "For information: http://www.gnu.org/copyleft/.";
56 /* Meaningful Defaults */
57 #define DEFAULT_FREQ 440.0 /* Middle A */
58 #define DEFAULT_LENGTH 200 /* milliseconds */
59 #define DEFAULT_REPS 1
60 #define DEFAULT_DELAY 100 /* milliseconds */
61 #define DEFAULT_END_DELAY NO_END_DELAY
62 #define DEFAULT_STDIN_BEEP NO_STDIN_BEEP
65 #define NO_END_DELAY 0
66 #define YES_END_DELAY 1
68 #define NO_STDIN_BEEP 0
69 #define LINE_STDIN_BEEP 1
70 #define CHAR_STDIN_BEEP 2
72 typedef struct beep_parms_t {
73 float freq; /* tone frequency (Hz) */
74 int length; /* tone length (ms) */
75 int reps; /* # of repetitions */
76 int delay; /* delay between reps (ms) */
77 int end_delay; /* do we delay after last rep? */
78 int stdin_beep; /* are we using stdin triggers? We have three options:
79 - just beep and terminate (default)
80 - beep after a line of input
81 - beep after a character of input
82 In the latter two cases, pass the text back out again,
83 so that beep can be tucked appropriately into a text-
86 struct beep_parms_t *next; /* in case -n/--new is used. */
89 /* Momma taught me never to use globals, but we need something the signal
90 handlers can get at.*/
93 /* If we get interrupted, it would be nice to not leave the speaker beeping in
95 void handle_signal(int signum) {
99 /* Kill the sound, quit gracefully */
100 ioctl(console_fd, KIOCSOUND, 0);
104 /* Just quit gracefully */
110 /* print usage and exit */
111 void usage_bail(const char *executable_name) {
112 printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] "
113 "[-D delay] [-s] [-c]\n",
115 printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name);
116 printf("%s [-h] [--help]\n", executable_name);
117 printf("%s [-v] [-V] [--version]\n", executable_name);
122 /* Parse the command line. argv should be untampered, as passed to main.
123 * Beep parameters returned in result, subsequent parameters in argv will over-
124 * ride previous ones.
126 * Currently valid parameters:
127 * "-f <frequency in Hz>"
128 * "-l <tone length in ms>"
131 * "-D <delay in ms>" (similar to -d, but delay after last repetition as well)
132 * "-s" (beep after each line of input from stdin, echo line to stdout)
133 * "-c" (beep after each char of input from stdin, echo char to stdout)
138 * March 29, 2002 - Daniel Eisenbud points out that c should be int, not char,
139 * for correctness on platforms with unsigned chars.
141 void parse_command_line(int argc, char **argv, beep_parms_t *result) {
144 struct option opt_list[4] = {{"help", 0, NULL, 'h'},
145 {"version", 0, NULL, 'V'},
146 {"new", 0, NULL, 'n'},
148 while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL))
150 int argval = -1; /* handle parsed numbers for various arguments */
154 if(!sscanf(optarg, "%f", &argfreq) || (argfreq >= 20000 /* ack! */) ||
158 result->freq = argfreq;
160 case 'l' : /* length */
161 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
164 result->length = argval;
166 case 'r' : /* repetitions */
167 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
170 result->reps = argval;
172 case 'd' : /* delay between reps - WITHOUT delay after last beep*/
173 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
176 result->delay = argval;
177 result->end_delay = NO_END_DELAY;
180 case 'D' : /* delay between reps - WITH delay after last beep */
181 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
184 result->delay = argval;
185 result->end_delay = YES_END_DELAY;
189 result->stdin_beep = LINE_STDIN_BEEP;
192 result->stdin_beep = CHAR_STDIN_BEEP;
195 case 'V' : /* also --version */
196 printf("%s\n",VERSION_STRING);
199 case 'n' : /* also --new - create another beep */
200 result->next = (beep_parms_t *)malloc(sizeof(beep_parms_t));
201 result->next->freq = DEFAULT_FREQ;
202 result->next->length = DEFAULT_LENGTH;
203 result->next->reps = DEFAULT_REPS;
204 result->next->delay = DEFAULT_DELAY;
205 result->next->end_delay = DEFAULT_END_DELAY;
206 result->next->stdin_beep = DEFAULT_STDIN_BEEP;
207 result->next->next = NULL;
208 result = result->next; /* yes, I meant to do that. */
210 case 'h' : /* notice that this is also --help */
217 void play_beep(beep_parms_t parms) {
218 int i; /* loop counter */
220 /* try to snag the console */
221 if((console_fd = open("/dev/console", O_WRONLY)) == -1) {
222 fprintf(stderr, "Could not open /dev/console for writing.\n");
223 printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
229 for (i = 0; i < parms.reps; i++) { /* start beep */
230 if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) {
231 printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
234 /* Look ma, I'm not ansi C compatible! */
235 usleep(1000*parms.length); /* wait... */
236 ioctl(console_fd, KIOCSOUND, 0); /* stop beep */
237 if(parms.end_delay || (i+1 < parms.reps))
238 usleep(1000*parms.delay); /* wait... */
246 int main(int argc, char **argv) {
247 char sin[4096], *ptr;
249 beep_parms_t *parms = (beep_parms_t *)malloc(sizeof(beep_parms_t));
250 parms->freq = DEFAULT_FREQ;
251 parms->length = DEFAULT_LENGTH;
252 parms->reps = DEFAULT_REPS;
253 parms->delay = DEFAULT_DELAY;
254 parms->end_delay = DEFAULT_END_DELAY;
255 parms->stdin_beep = DEFAULT_STDIN_BEEP;
258 signal(SIGINT, handle_signal);
259 parse_command_line(argc, argv, parms);
261 /* this outermost while loop handles the possibility that -n/--new has been
262 used, i.e. that we have multiple beeps specified. Each iteration will
263 play, then free() one parms instance. */
265 beep_parms_t *next = parms->next;
267 if(parms->stdin_beep) {
268 /* in this case, beep is probably part of a pipe, in which case POSIX
269 says stdin and out should be fuly buffered. This however means very
270 laggy performance with beep just twiddling it's thumbs until a buffer
271 fills. Thus, kill the buffering. In some situations, this too won't
272 be enough, namely if we're in the middle of a long pipe, and the
273 processes feeding us stdin are buffered, we'll have to wait for them,
274 not much to be done about that. */
275 setvbuf(stdin, NULL, _IONBF, 0);
276 setvbuf(stdout, NULL, _IONBF, 0);
277 while(fgets(sin, 4096, stdin)) {
278 if(parms->stdin_beep==CHAR_STDIN_BEEP) {
279 for(ptr=sin;*ptr;ptr++) {
293 /* Junk each parms struct after playing it */