3
0

beep.c 2.9 KB

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