Audio plugin host https://kx.studio/carla
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.

146 lines
3.8KB

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