blob: b9b62f79be691d3be3cff96c58c4ac5d1c9189f6 [file] [log] [blame]
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +02001/* vi: set sw=4 ts=4: */
2/*
3 * beep implementation for busybox
4 *
5 * Copyright (C) 2009 Bernhard Reutner-Fischer
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +02008 *
9 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010010//config:config BEEP
Denys Vlasenkoae178ce2017-07-19 14:32:54 +020011//config: bool "beep (3 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010012//config: default y
13//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: The beep applets beeps in a given freq/Hz.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010016//config:
17//config:config FEATURE_BEEP_FREQ
18//config: int "default frequency"
Denys Vlasenko1b84f4a2017-07-08 12:21:45 +020019//config: range 20 50000 # allowing 0 here breaks the build
Denys Vlasenkofb4da162016-11-22 23:14:24 +010020//config: default 4000
21//config: depends on BEEP
22//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: Frequency for default beep.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010024//config:
25//config:config FEATURE_BEEP_LENGTH_MS
26//config: int "default length"
27//config: range 0 2147483647
28//config: default 30
29//config: depends on BEEP
30//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020031//config: Length in ms for default beep.
Pere Orga5bc8c002011-04-11 03:29:49 +020032
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010033//applet:IF_BEEP(APPLET(beep, BB_DIR_USR_BIN, BB_SUID_DROP))
34
35//kbuild:lib-$(CONFIG_BEEP) += beep.o
36
Pere Orga5bc8c002011-04-11 03:29:49 +020037//usage:#define beep_trivial_usage
38//usage: "-f FREQ -l LEN -d DELAY -r COUNT -n"
39//usage:#define beep_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020040//usage: " -f Frequency in Hz"
Pere Orga5bc8c002011-04-11 03:29:49 +020041//usage: "\n -l Length in ms"
42//usage: "\n -d Delay in ms"
43//usage: "\n -r Repetitions"
44//usage: "\n -n Start new tone"
45
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020046#include "libbb.h"
47
48#include <linux/kd.h>
49#ifndef CLOCK_TICK_RATE
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020050# define CLOCK_TICK_RATE 1193180
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020051#endif
52
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020053/* defaults */
Bernhard Reutner-Fischer00ea82e2009-08-21 14:40:29 +020054#ifndef CONFIG_FEATURE_BEEP_FREQ
55# define FREQ (4000)
56#else
57# define FREQ (CONFIG_FEATURE_BEEP_FREQ)
58#endif
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020059#ifndef CONFIG_FEATURE_BEEP_LENGTH_MS
Bernhard Reutner-Fischer00ea82e2009-08-21 14:40:29 +020060# define LENGTH (30)
61#else
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020062# define LENGTH (CONFIG_FEATURE_BEEP_LENGTH_MS)
Bernhard Reutner-Fischer00ea82e2009-08-21 14:40:29 +020063#endif
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020064#define DELAY (0)
65#define REPETITIONS (1)
66
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020067int beep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020068int beep_main(int argc, char **argv)
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020069{
70 int speaker = get_console_fd_or_die();
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010071 unsigned tickrate_div_freq = tickrate_div_freq; /* for compiler */
72 unsigned length = length;
73 unsigned delay = delay;
74 unsigned rep = rep;
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020075 int c;
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020076
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020077 c = 'n';
78 while (c != -1) {
79 if (c == 'n') {
80 tickrate_div_freq = CLOCK_TICK_RATE / FREQ;
81 length = LENGTH;
82 delay = DELAY;
83 rep = REPETITIONS;
84 }
85 c = getopt(argc, argv, "f:l:d:r:n");
86/* TODO: -s, -c:
87 * pipe stdin to stdout, but also beep after each line (-s) or char (-c)
88 */
89 switch (c) {
90 case 'f':
91/* TODO: what "-f 0" should do? */
92 tickrate_div_freq = (unsigned)CLOCK_TICK_RATE / xatou(optarg);
93 continue;
94 case 'l':
95 length = xatou(optarg);
96 continue;
97 case 'd':
98/* TODO:
99 * -d N, -D N
100 * specify a delay of N milliseconds between repetitions.
101 * -d specifies that this delay should only occur between beeps,
102 * that is, it should not occur after the last repetition.
103 * -D indicates that the delay should occur after every repetition
104 */
105 delay = xatou(optarg);
106 continue;
107 case 'r':
108 rep = xatou(optarg);
109 continue;
110 case 'n':
111 case -1:
112 break;
113 default:
114 bb_show_usage();
115 }
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +0200116 while (rep) {
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200117//bb_error_msg("rep[%d] freq=%d, length=%d, delay=%d", rep, freq, length, delay);
Natanael Copa7cfec4b2010-03-14 15:32:49 +0100118 xioctl(speaker, KIOCSOUND, (void*)(uintptr_t)tickrate_div_freq);
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +0200119 usleep(1000 * length);
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +0200120 ioctl(speaker, KIOCSOUND, (void*)0);
121 if (--rep)
Natanael Copa7cfec4b2010-03-14 15:32:49 +0100122 usleep(1000 * delay);
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +0200123 }
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +0200124 }
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +0200125
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +0200126 if (ENABLE_FEATURE_CLEAN_UP)
127 close(speaker);
128 return EXIT_SUCCESS;
129}
130/*
131 * so, e.g. Beethoven's 9th symphony "Ode an die Freude" would be
132 * something like:
133a=$((220*3))
134b=$((247*3))
135c=$((262*3))
136d=$((294*3))
137e=$((329*3))
138f=$((349*3))
139g=$((392*3))
140#./beep -f$d -l200 -r2 -n -f$e -l100 -d 10 -n -f$c -l400 -f$g -l200
141./beep -f$e -l200 -r2 \
142 -n -d 100 -f$f -l200 \
143 -n -f$g -l200 -r2 \
144 -n -f$f -l200 \
145 -n -f$e -l200 \
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +0200146 -n -f$d -l200 \
147 -n -f$c -l200 -r2 \
148 -n -f$d -l200 \
149 -n -f$e -l200 \
150 -n -f$e -l400 \
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +0200151 -n -f$d -l100 \
152 -n -f$d -l200 \
153*/