beep.c 2.7 KB

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