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.

133 lines
2.6KB

  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>
  12. #include <stdio.h>
  13. /*
  14. typedef char MY_TYPE;
  15. #define FORMAT RTAUDIO_SINT8
  16. #define SCALE 127.0
  17. typedef signed short MY_TYPE;
  18. #define FORMAT RTAUDIO_SINT16
  19. #define SCALE 32767.0
  20. typedef signed long MY_TYPE;
  21. #define FORMAT RTAUDIO_SINT24
  22. #define SCALE 8388607.0
  23. typedef signed long MY_TYPE;
  24. #define FORMAT RTAUDIO_SINT32
  25. #define SCALE 2147483647.0
  26. */
  27. typedef float MY_TYPE;
  28. #define FORMAT RTAUDIO_FLOAT32
  29. #define SCALE 1.0;
  30. /*
  31. typedef double MY_TYPE;
  32. #define FORMAT 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. std::cout << "\nuseage: play_raw N fs file <device>\n";
  40. std::cout << " where N = number of channels,\n";
  41. std::cout << " fs = the sample rate, \n";
  42. std::cout << " file = the raw file to play,\n";
  43. std::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, device = 0;
  49. long counter = 0;
  50. MY_TYPE *buffer;
  51. char *file;
  52. FILE *fd;
  53. RtAudio *audio = 0;
  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. std::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(device, chans, 0, 0,
  70. FORMAT, fs, &buffer_size, 2);
  71. }
  72. catch (RtError &error) {
  73. fclose(fd);
  74. error.printMessage();
  75. exit(EXIT_FAILURE);
  76. }
  77. try {
  78. buffer = (MY_TYPE *) audio->getStreamBuffer();
  79. audio->startStream();
  80. }
  81. catch (RtError &error) {
  82. error.printMessage();
  83. goto cleanup;
  84. }
  85. while (1) {
  86. count = fread(buffer, chans * sizeof(MY_TYPE), buffer_size, fd);
  87. if (count == buffer_size) {
  88. try {
  89. audio->tickStream();
  90. }
  91. catch (RtError &error) {
  92. error.printMessage();
  93. goto cleanup;
  94. }
  95. }
  96. else
  97. break;
  98. counter += buffer_size;
  99. }
  100. try {
  101. audio->stopStream();
  102. }
  103. catch (RtError &error) {
  104. error.printMessage();
  105. }
  106. cleanup:
  107. audio->closeStream();
  108. delete audio;
  109. fclose(fd);
  110. return 0;
  111. }