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 int verbose; /* verbose output? */
87 struct beep_parms_t *next; /* in case -n/--new is used. */
90 /* Momma taught me never to use globals, but we need something the signal
91 handlers can get at.*/
94 /* If we get interrupted, it would be nice to not leave the speaker beeping in
96 void handle_signal(int signum) {
100 /* Kill the sound, quit gracefully */
101 ioctl(console_fd, KIOCSOUND, 0);
105 /* Just quit gracefully */
111 /* print usage and exit */
112 void usage_bail(const char *executable_name) {
113 printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] "
114 "[-D delay] [-s] [-c] [--verbose | --debug]\n",
116 printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name);
117 printf("%s [-h] [--help]\n", executable_name);
118 printf("%s [-v] [-V] [--version]\n", executable_name);
123 /* Parse the command line. argv should be untampered, as passed to main.
124 * Beep parameters returned in result, subsequent parameters in argv will over-
125 * ride previous ones.
127 * Currently valid parameters:
128 * "-f <frequency in Hz>"
129 * "-l <tone length in ms>"
132 * "-D <delay in ms>" (similar to -d, but delay after last repetition as well)
133 * "-s" (beep after each line of input from stdin, echo line to stdout)
134 * "-c" (beep after each char of input from stdin, echo char to stdout)
135 * "--verbose/--debug"
140 * March 29, 2002 - Daniel Eisenbud points out that c should be int, not char,
141 * for correctness on platforms with unsigned chars.
143 void parse_command_line(int argc, char **argv, beep_parms_t *result) {
146 struct option opt_list[6] = {{"help", 0, NULL, 'h'},
147 {"version", 0, NULL, 'V'},
148 {"new", 0, NULL, 'n'},
149 {"verbose", 0, NULL, 'X'},
150 {"debug", 0, NULL, 'X'},
152 while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL))
154 int argval = -1; /* handle parsed numbers for various arguments */
158 if(!sscanf(optarg, "%f", &argfreq) || (argfreq >= 20000 /* ack! */) ||
162 if (result->freq != 0)
163 fprintf(stderr, "WARNING: multiple -f values given, only last "
165 result->freq = argfreq;
167 case 'l' : /* length */
168 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
171 result->length = argval;
173 case 'r' : /* repetitions */
174 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
177 result->reps = argval;
179 case 'd' : /* delay between reps - WITHOUT delay after last beep*/
180 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
183 result->delay = argval;
184 result->end_delay = NO_END_DELAY;
187 case 'D' : /* delay between reps - WITH delay after last beep */
188 if(!sscanf(optarg, "%d", &argval) || (argval < 0))
191 result->delay = argval;
192 result->end_delay = YES_END_DELAY;
196 result->stdin_beep = LINE_STDIN_BEEP;
199 result->stdin_beep = CHAR_STDIN_BEEP;
202 case 'V' : /* also --version */
203 printf("%s\n",VERSION_STRING);
206 case 'n' : /* also --new - create another beep */
207 if (result->freq == 0)
208 result->freq = DEFAULT_FREQ;
209 result->next = (beep_parms_t *)malloc(sizeof(beep_parms_t));
210 result->next->freq = 0;
211 result->next->length = DEFAULT_LENGTH;
212 result->next->reps = DEFAULT_REPS;
213 result->next->delay = DEFAULT_DELAY;
214 result->next->end_delay = DEFAULT_END_DELAY;
215 result->next->stdin_beep = DEFAULT_STDIN_BEEP;
216 result->next->verbose = result->verbose;
217 result->next->next = NULL;
218 result = result->next; /* yes, I meant to do that. */
220 case 'X' : /* --debug / --verbose */
223 case 'h' : /* notice that this is also --help */
228 if (result->freq == 0)
229 result->freq = DEFAULT_FREQ;
232 void play_beep(beep_parms_t parms) {
233 int i; /* loop counter */
235 if(parms.verbose == 1)
236 fprintf(stderr, "[DEBUG] %d times %d ms beeps (%d delay between, "
237 "%d delay after) @ %.2f Hz\n",
238 parms.reps, parms.length, parms.delay, parms.end_delay, parms.freq);
240 /* try to snag the console */
241 if((console_fd = open("/dev/tty0", O_WRONLY)) == -1) {
242 if((console_fd = open("/dev/vc/0", O_WRONLY)) == -1) {
243 fprintf(stderr, "Could not open /dev/tty0 or /dev/vc/0 for writing.\n");
244 printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
251 for (i = 0; i < parms.reps; i++) { /* start beep */
252 if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) {
253 printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
256 /* Look ma, I'm not ansi C compatible! */
257 usleep(1000*parms.length); /* wait... */
258 ioctl(console_fd, KIOCSOUND, 0); /* stop beep */
259 if(parms.end_delay || (i+1 < parms.reps))
260 usleep(1000*parms.delay); /* wait... */
268 int main(int argc, char **argv) {
269 char sin[4096], *ptr;
271 beep_parms_t *parms = (beep_parms_t *)malloc(sizeof(beep_parms_t));
273 parms->length = DEFAULT_LENGTH;
274 parms->reps = DEFAULT_REPS;
275 parms->delay = DEFAULT_DELAY;
276 parms->end_delay = DEFAULT_END_DELAY;
277 parms->stdin_beep = DEFAULT_STDIN_BEEP;
281 signal(SIGINT, handle_signal);
282 parse_command_line(argc, argv, parms);
284 /* this outermost while loop handles the possibility that -n/--new has been
285 used, i.e. that we have multiple beeps specified. Each iteration will
286 play, then free() one parms instance. */
288 beep_parms_t *next = parms->next;
290 if(parms->stdin_beep) {
291 /* in this case, beep is probably part of a pipe, in which case POSIX
292 says stdin and out should be fuly buffered. This however means very
293 laggy performance with beep just twiddling it's thumbs until a buffer
294 fills. Thus, kill the buffering. In some situations, this too won't
295 be enough, namely if we're in the middle of a long pipe, and the
296 processes feeding us stdin are buffered, we'll have to wait for them,
297 not much to be done about that. */
298 setvbuf(stdin, NULL, _IONBF, 0);
299 setvbuf(stdout, NULL, _IONBF, 0);
300 while(fgets(sin, 4096, stdin)) {
301 if(parms->stdin_beep==CHAR_STDIN_BEEP) {
302 for(ptr=sin;*ptr;ptr++) {
316 /* Junk each parms struct after playing it */