beep.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * beep implementation for busybox
  4. *
  5. * Copyright (C) 2009 Bernhard Reutner-Fischer
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config BEEP
  10. //config: bool "beep (3 kb)"
  11. //config: default y
  12. //config: select PLATFORM_LINUX
  13. //config: help
  14. //config: The beep applets beeps in a given freq/Hz.
  15. //config:
  16. //config:config FEATURE_BEEP_FREQ
  17. //config: int "default frequency"
  18. //config: range 20 50000 # allowing 0 here breaks the build
  19. //config: default 4000
  20. //config: depends on BEEP
  21. //config: help
  22. //config: Frequency for default beep.
  23. //config:
  24. //config:config FEATURE_BEEP_LENGTH_MS
  25. //config: int "default length"
  26. //config: range 0 2147483647
  27. //config: default 30
  28. //config: depends on BEEP
  29. //config: help
  30. //config: Length in ms for default beep.
  31. //applet:IF_BEEP(APPLET(beep, BB_DIR_USR_BIN, BB_SUID_DROP))
  32. //kbuild:lib-$(CONFIG_BEEP) += beep.o
  33. //usage:#define beep_trivial_usage
  34. //usage: "-f FREQ -l LEN -d DELAY -r COUNT -n"
  35. //usage:#define beep_full_usage "\n\n"
  36. //usage: " -f Frequency in Hz"
  37. //usage: "\n -l Length in ms"
  38. //usage: "\n -d Delay in ms"
  39. //usage: "\n -r Repetitions"
  40. //usage: "\n -n Start new tone"
  41. #include "libbb.h"
  42. #include <linux/kd.h>
  43. #ifndef CLOCK_TICK_RATE
  44. # define CLOCK_TICK_RATE 1193180
  45. #endif
  46. /* defaults */
  47. #ifndef CONFIG_FEATURE_BEEP_FREQ
  48. # define FREQ (4000)
  49. #else
  50. # define FREQ (CONFIG_FEATURE_BEEP_FREQ)
  51. #endif
  52. #ifndef CONFIG_FEATURE_BEEP_LENGTH_MS
  53. # define LENGTH (30)
  54. #else
  55. # define LENGTH (CONFIG_FEATURE_BEEP_LENGTH_MS)
  56. #endif
  57. #define DELAY (0)
  58. #define REPETITIONS (1)
  59. int beep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  60. int beep_main(int argc, char **argv)
  61. {
  62. int speaker = get_console_fd_or_die();
  63. unsigned tickrate_div_freq = tickrate_div_freq; /* for compiler */
  64. unsigned length = length;
  65. unsigned delay = delay;
  66. unsigned rep = rep;
  67. int c;
  68. c = 'n';
  69. while (c != -1) {
  70. if (c == 'n') {
  71. tickrate_div_freq = CLOCK_TICK_RATE / FREQ;
  72. length = LENGTH;
  73. delay = DELAY;
  74. rep = REPETITIONS;
  75. }
  76. c = getopt(argc, argv, "f:l:d:r:n");
  77. /* TODO: -s, -c:
  78. * pipe stdin to stdout, but also beep after each line (-s) or char (-c)
  79. */
  80. switch (c) {
  81. case 'f':
  82. /* TODO: what "-f 0" should do? */
  83. tickrate_div_freq = (unsigned)CLOCK_TICK_RATE / xatou(optarg);
  84. continue;
  85. case 'l':
  86. length = xatou(optarg);
  87. continue;
  88. case 'd':
  89. /* TODO:
  90. * -d N, -D N
  91. * specify a delay of N milliseconds between repetitions.
  92. * -d specifies that this delay should only occur between beeps,
  93. * that is, it should not occur after the last repetition.
  94. * -D indicates that the delay should occur after every repetition
  95. */
  96. delay = xatou(optarg);
  97. continue;
  98. case 'r':
  99. rep = xatou(optarg);
  100. continue;
  101. case 'n':
  102. case -1:
  103. break;
  104. default:
  105. bb_show_usage();
  106. }
  107. while (rep) {
  108. //bb_error_msg("rep[%d] freq=%d, length=%d, delay=%d", rep, freq, length, delay);
  109. xioctl(speaker, KIOCSOUND, (void*)(uintptr_t)tickrate_div_freq);
  110. usleep(1000 * length);
  111. ioctl(speaker, KIOCSOUND, (void*)0);
  112. if (--rep)
  113. usleep(1000 * delay);
  114. }
  115. }
  116. if (ENABLE_FEATURE_CLEAN_UP)
  117. close(speaker);
  118. return EXIT_SUCCESS;
  119. }
  120. /*
  121. * so, e.g. Beethoven's 9th symphony "Ode an die Freude" would be
  122. * something like:
  123. a=$((220*3))
  124. b=$((247*3))
  125. c=$((262*3))
  126. d=$((294*3))
  127. e=$((329*3))
  128. f=$((349*3))
  129. g=$((392*3))
  130. #./beep -f$d -l200 -r2 -n -f$e -l100 -d 10 -n -f$c -l400 -f$g -l200
  131. ./beep -f$e -l200 -r2 \
  132. -n -d 100 -f$f -l200 \
  133. -n -f$g -l200 -r2 \
  134. -n -f$f -l200 \
  135. -n -f$e -l200 \
  136. -n -f$d -l200 \
  137. -n -f$c -l200 -r2 \
  138. -n -f$d -l200 \
  139. -n -f$e -l200 \
  140. -n -f$e -l400 \
  141. -n -f$d -l100 \
  142. -n -f$d -l200 \
  143. */