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.

129 lines
2.5KB

  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. */
  27. typedef float MY_TYPE;
  28. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  29. #define SCALE 1.0;
  30. /*
  31. typedef double MY_TYPE;
  32. #define FORMAT RtAudio::RTAUDIO_FLOAT64
  33. #define SCALE 1.0;
  34. */
  35. void usage(void) {
  36. /* Error function in case of incorrect command-line
  37. argument specifications
  38. */
  39. cout << "\nuseage: play_raw N fs file <device>\n";
  40. cout << " where N = number of channels,\n";
  41. cout << " fs = the sample rate, \n";
  42. cout << " file = the raw file to play,\n";
  43. cout << " and device = the device to use (default = 0).\n\n";
  44. exit(0);
  45. }
  46. int main(int argc, char *argv[])
  47. {
  48. int chans, fs, buffer_size, count, stream, device = 0;
  49. long counter = 0;
  50. MY_TYPE *buffer;
  51. char *file;
  52. FILE *fd;
  53. RtAudio *audio;
  54. // minimal command-line checking
  55. if (argc != 4 && argc != 5 ) usage();
  56. chans = (int) atoi(argv[1]);
  57. fs = (int) atoi(argv[2]);
  58. file = argv[3];
  59. if ( argc == 5 )
  60. device = (int) atoi(argv[4]);
  61. fd = fopen(file,"rb");
  62. if (!fd) {
  63. cout << "can't find file!\n";
  64. exit(0);
  65. }
  66. // Open the realtime output device
  67. buffer_size = 512;
  68. try {
  69. audio = new RtAudio(&stream, device, chans, 0, 0,
  70. FORMAT, fs, &buffer_size, 2);
  71. }
  72. catch (RtError &) {
  73. fclose(fd);
  74. exit(EXIT_FAILURE);
  75. }
  76. try {
  77. buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
  78. audio->startStream(stream);
  79. }
  80. catch (RtError &) {
  81. goto cleanup;
  82. }
  83. while (1) {
  84. count = fread(buffer, chans * sizeof(MY_TYPE), buffer_size, fd);
  85. if (count == buffer_size) {
  86. try {
  87. audio->tickStream(stream);
  88. }
  89. catch (RtError &) {
  90. goto cleanup;
  91. }
  92. }
  93. else
  94. break;
  95. counter += buffer_size;
  96. }
  97. try {
  98. audio->stopStream(stream);
  99. }
  100. catch (RtError &) {
  101. }
  102. cleanup:
  103. audio->closeStream(stream);
  104. delete audio;
  105. fclose(fd);
  106. return 0;
  107. }