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.

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