beep.c 3.6 KB

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