play.c 927 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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) 2015-2018 K. Lange
  5. *
  6. * play - Play back PCM samples
  7. *
  8. * This needs very specifically-formatted PCM data to function
  9. * properly - 16-bit, signed, stereo, little endian, and 48KHz.
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <sys/ioctl.h>
  16. int main(int argc, char * argv[]) {
  17. int spkr = open("/dev/dsp", O_WRONLY);
  18. int song;
  19. if (!strcmp(argv[1], "-")) {
  20. song = STDIN_FILENO;
  21. } else {
  22. song = open(argv[1], O_RDONLY);
  23. }
  24. if (spkr == -1) {
  25. fprintf(stderr, "no dsp\n");
  26. return 1;
  27. }
  28. if (song == -1) {
  29. fprintf(stderr, "audio file not found\n");
  30. return 2;
  31. }
  32. char buf[0x1000];
  33. int r;
  34. while ((r = read(song, buf, sizeof(buf)))) {
  35. write(spkr, buf, r);
  36. }
  37. return 0;
  38. }