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.

153 lines
3.9KB

  1. /******************************************/
  2. /*
  3. playraw.cpp
  4. by Gary P. Scavone, 2007
  5. Play a specified raw file. It is necessary
  6. that the file be of the same data format as
  7. defined below.
  8. */
  9. /******************************************/
  10. #include "RtAudio.h"
  11. #include <iostream>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <stdio.h>
  15. /*
  16. typedef char MY_TYPE;
  17. #define FORMAT RTAUDIO_SINT8
  18. #define SCALE 127.0
  19. */
  20. typedef signed short MY_TYPE;
  21. #define FORMAT RTAUDIO_SINT16
  22. #define SCALE 32767.0
  23. /*
  24. typedef S24 MY_TYPE;
  25. #define FORMAT RTAUDIO_SINT24
  26. #define SCALE 8388607.0
  27. typedef signed int MY_TYPE;
  28. #define FORMAT RTAUDIO_SINT32
  29. #define SCALE 2147483647.0
  30. typedef float MY_TYPE;
  31. #define FORMAT RTAUDIO_FLOAT32
  32. #define SCALE 1.0;
  33. typedef double MY_TYPE;
  34. #define FORMAT RTAUDIO_FLOAT64
  35. #define SCALE 1.0;
  36. */
  37. // Platform-dependent sleep routines.
  38. #if defined( WIN32 )
  39. #include <windows.h>
  40. #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
  41. #else // Unix variants
  42. #include <unistd.h>
  43. #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
  44. #endif
  45. void usage( void ) {
  46. // Error function in case of incorrect command-line
  47. // argument specifications
  48. std::cout << "\nuseage: playraw N fs file <device> <channelOffset>\n";
  49. std::cout << " where N = number of channels,\n";
  50. std::cout << " fs = the sample rate, \n";
  51. std::cout << " file = the raw file to play,\n";
  52. std::cout << " device = optional device to use (default = 0),\n";
  53. std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";
  54. exit( 0 );
  55. }
  56. struct OutputData {
  57. FILE *fd;
  58. unsigned int channels;
  59. };
  60. // Interleaved buffers
  61. int output( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
  62. double /*streamTime*/, RtAudioStreamStatus /*status*/, void *data )
  63. {
  64. OutputData *oData = (OutputData*) data;
  65. // In general, it's not a good idea to do file input in the audio
  66. // callback function but I'm doing it here because I don't know the
  67. // length of the file we are reading.
  68. unsigned int count = fread( outputBuffer, oData->channels * sizeof( MY_TYPE ), nBufferFrames, oData->fd);
  69. if ( count < nBufferFrames ) {
  70. unsigned int bytes = (nBufferFrames - count) * oData->channels * sizeof( MY_TYPE );
  71. unsigned int startByte = count * oData->channels * sizeof( MY_TYPE );
  72. memset( (char *)(outputBuffer)+startByte, 0, bytes );
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. int main( int argc, char *argv[] )
  78. {
  79. unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
  80. char *file;
  81. // minimal command-line checking
  82. if ( argc < 4 || argc > 6 ) usage();
  83. RtAudio dac;
  84. if ( dac.getDeviceCount() < 1 ) {
  85. std::cout << "\nNo audio devices found!\n";
  86. exit( 0 );
  87. }
  88. channels = (unsigned int) atoi( argv[1]) ;
  89. fs = (unsigned int) atoi( argv[2] );
  90. file = argv[3];
  91. if ( argc > 4 )
  92. device = (unsigned int) atoi( argv[4] );
  93. if ( argc > 5 )
  94. offset = (unsigned int) atoi( argv[5] );
  95. OutputData data;
  96. data.fd = fopen( file, "rb" );
  97. if ( !data.fd ) {
  98. std::cout << "Unable to find or open file!\n";
  99. exit( 1 );
  100. }
  101. // Set our stream parameters for output only.
  102. bufferFrames = 512;
  103. RtAudio::StreamParameters oParams;
  104. oParams.deviceId = device;
  105. oParams.nChannels = channels;
  106. oParams.firstChannel = offset;
  107. if ( device == 0 )
  108. oParams.deviceId = dac.getDefaultOutputDevice();
  109. data.channels = channels;
  110. try {
  111. dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &output, (void *)&data );
  112. dac.startStream();
  113. }
  114. catch ( RtAudioError& e ) {
  115. std::cout << '\n' << e.getMessage() << '\n' << std::endl;
  116. goto cleanup;
  117. }
  118. std::cout << "\nPlaying raw file " << file << " (buffer frames = " << bufferFrames << ")." << std::endl;
  119. while ( 1 ) {
  120. SLEEP( 100 ); // wake every 100 ms to check if we're done
  121. if ( dac.isStreamRunning() == false ) break;
  122. }
  123. cleanup:
  124. fclose( data.fd );
  125. dac.closeStream();
  126. return 0;
  127. }