beep.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. int spkr = 0;
  11. struct spkr {
  12. int length;
  13. int frequency;
  14. };
  15. static void note(int length, int frequency) {
  16. struct spkr s = {
  17. .length = length,
  18. .frequency = frequency,
  19. };
  20. write(spkr, &s, sizeof(s));
  21. }
  22. /* Stolen from the Linux tool */
  23. #define DEFAULT_FREQ 440.0
  24. #define DEFAULT_LEN 200
  25. #define DEFAULT_DELAY 100
  26. static int repetitions = 1;
  27. static float frequency = DEFAULT_FREQ;
  28. static int length = DEFAULT_LEN;
  29. static int delay = DEFAULT_DELAY;
  30. static int beep_after = 0;
  31. void beep(void) {
  32. for (int i = 0; i < repetitions; ++i) {
  33. note(length, frequency * 10);
  34. if (delay && ((i != repetitions - 1) || beep_after)) {
  35. usleep(delay * 1000);
  36. }
  37. }
  38. }
  39. int main(int argc, char * argv[]) {
  40. spkr = open("/dev/spkr", O_WRONLY);
  41. if (spkr == -1) {
  42. fprintf(stderr, "%s: could not open speaker\n", argv[0]);
  43. }
  44. int opt;
  45. while ((opt = getopt(argc, argv, "?hr:f:l:d:D:n")) != -1) {
  46. switch (opt) {
  47. case 'h':
  48. case '?':
  49. fprintf(stderr, "usage: %s BEEP...\n"
  50. "Where BEEP consists of:\n"
  51. " -r REPS \033[3mNumber of repetitions.\033[0m\n"
  52. " -f FREQ \033[3mFrequency in Hz. 440 is A4. Supports fractional values.\033[0m\n"
  53. " -l TIME \033[3mDuration in milliseconds.\033[0m\n"
  54. " -d TIME \033[3mDelay between repetitions in milliseconds.\033[0m\n"
  55. " -D TIME \033[3mDelay between, and after, repetitions.\033[0m\n"
  56. " -n \033[3mStart a new beep.\033[0m\n"
  57. "\n"
  58. "The default values are:\n"
  59. " -r 1 -l %d -f %.2f -d %d\n"
  60. "\n"
  61. "A length of -1 will start a sustained beep without blocking.\n"
  62. "A length of 0 will stop a currently playing sustained beep.\n",
  63. argv[0], DEFAULT_LEN, DEFAULT_FREQ, DEFAULT_DELAY);
  64. return 1;
  65. case 'r':
  66. repetitions = atoi(optarg);
  67. break;
  68. case 'l':
  69. length = atoi(optarg);
  70. break;
  71. case 'f':
  72. frequency = atof(optarg);
  73. break;
  74. case 'd':
  75. delay = atoi(optarg);
  76. beep_after = 0;
  77. break;
  78. case 'D':
  79. delay = atoi(optarg);
  80. beep_after = 1;
  81. break;
  82. case 'n':
  83. beep();
  84. repetitions = 1;
  85. frequency = DEFAULT_FREQ;
  86. length = DEFAULT_LEN;
  87. delay = DEFAULT_DELAY;
  88. beep_after = 0;
  89. break;
  90. }
  91. }
  92. beep();
  93. return 0;
  94. }