You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.4KB

  1. /******************************************/
  2. /*
  3. play_raw.c
  4. by Gary P. Scavone, 2001
  5. Play a raw file. It is necessary that the
  6. file be of the same format as defined below.
  7. Uses blocking functionality.
  8. */
  9. /******************************************/
  10. #include "RtAudio.h"
  11. #include <iostream.h>
  12. #include <stdio.h>
  13. /*
  14. typedef char MY_TYPE;
  15. #define FORMAT RtAudio::RTAUDIO_SINT8
  16. #define SCALE 127.0
  17. typedef signed short MY_TYPE;
  18. #define FORMAT RtAudio::RTAUDIO_SINT16
  19. #define SCALE 32767.0
  20. typedef signed long MY_TYPE;
  21. #define FORMAT RtAudio::RTAUDIO_SINT24
  22. #define SCALE 8388607.0
  23. typedef signed long MY_TYPE;
  24. #define FORMAT RtAudio::RTAUDIO_SINT32
  25. #define SCALE 2147483647.0
  26. typedef float MY_TYPE;
  27. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  28. #define SCALE 1.0;
  29. */
  30. typedef double MY_TYPE;
  31. #define FORMAT RtAudio::RTAUDIO_FLOAT64
  32. #define SCALE 1.0;
  33. void usage(void) {
  34. /* Error function in case of incorrect command-line
  35. argument specifications
  36. */
  37. cout << "\nuseage: play_raw N fs file\n";
  38. cout << " where N = number of channels,\n";
  39. cout << " fs = the sample rate, \n";
  40. cout << " and file = the raw file to play.\n\n";
  41. exit(0);
  42. }
  43. int main(int argc, char *argv[])
  44. {
  45. int chans, fs, device, buffer_size, count, stream;
  46. long counter = 0;
  47. MY_TYPE *buffer;
  48. char *file;
  49. FILE *fd;
  50. RtAudio *audio;
  51. // minimal command-line checking
  52. if (argc != 4) usage();
  53. chans = (int) atoi(argv[1]);
  54. fs = (int) atoi(argv[2]);
  55. file = argv[3];
  56. fd = fopen(file,"rb");
  57. if (!fd) {
  58. cout << "can't find file!\n";
  59. exit(0);
  60. }
  61. // Open the realtime output device
  62. buffer_size = 256;
  63. device = 0; // default device
  64. try {
  65. audio = new RtAudio(&stream, device, chans, 0, 0,
  66. FORMAT, fs, &buffer_size, 2);
  67. }
  68. catch (RtError &) {
  69. fclose(fd);
  70. exit(EXIT_FAILURE);
  71. }
  72. try {
  73. buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
  74. audio->startStream(stream);
  75. }
  76. catch (RtError &) {
  77. goto cleanup;
  78. }
  79. while (1) {
  80. count = fread(buffer, chans * sizeof(MY_TYPE), buffer_size, fd);
  81. if (count == buffer_size) {
  82. try {
  83. audio->tickStream(stream);
  84. }
  85. catch (RtError &) {
  86. goto cleanup;
  87. }
  88. }
  89. else
  90. break;
  91. counter += buffer_size;
  92. }
  93. try {
  94. audio->stopStream(stream);
  95. }
  96. catch (RtError &) {
  97. }
  98. cleanup:
  99. audio->closeStream(stream);
  100. delete audio;
  101. fclose(fd);
  102. return 0;
  103. }