piano.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * piano - Interactively make beeping noises
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include <termios.h>
  13. int spkr = 0;
  14. struct spkr {
  15. int length;
  16. int frequency;
  17. };
  18. void note(int length, int frequency) {
  19. struct spkr s = {
  20. .length = length,
  21. .frequency = frequency,
  22. };
  23. write(spkr, &s, sizeof(s));
  24. }
  25. struct termios old;
  26. void set_unbuffered() {
  27. tcgetattr(fileno(stdin), &old);
  28. struct termios new = old;
  29. new.c_lflag &= (~ICANON & ~ECHO);
  30. tcsetattr(fileno(stdin), TCSAFLUSH, &new);
  31. }
  32. int main(int argc, char * argv[]) {
  33. spkr = open("/dev/spkr", O_WRONLY);
  34. if (spkr == -1) {
  35. fprintf(stderr, "%s: could not open speaker\n", argv[0]);
  36. return 1;
  37. }
  38. set_unbuffered();
  39. char c;
  40. while ((c = fgetc(stdin))) {
  41. switch (c) {
  42. case 'q': note(0, 1000); return 0;
  43. case 'z': note(0, 1000); return 0;
  44. case ' ': note(0, 1000); break;
  45. case 'a': note(-1, 1308); break;
  46. case 'w': note(-1, 1386); break;
  47. case 's': note(-1, 1468); break;
  48. case 'e': note(-1, 1556); break;
  49. case 'd': note(-1, 1648); break;
  50. case 'f': note(-1, 1746); break;
  51. case 't': note(-1, 1850); break;
  52. case 'g': note(-1, 1960); break;
  53. case 'y': note(-1, 2077); break;
  54. case 'h': note(-1, 2200); break;
  55. case 'u': note(-1, 2331); break;
  56. case 'j': note(-1, 2469); break;
  57. case 'k': note(-1, 2616); break;
  58. case 'o': note(-1, 2772); break;
  59. case 'l': note(-1, 2937); break;
  60. case 'p': note(-1, 3111); break;
  61. case ';': note(-1, 3296); break;
  62. case '\'': note(-1, 3492);break;
  63. }
  64. }
  65. return 0;
  66. }