1 /******************************************/
4 by Gary P. Scavone, 2007
6 Play a specified raw file. It is necessary
7 that the file be of the same data format as
10 /******************************************/
20 #define FORMAT RTAUDIO_SINT8
24 typedef signed short MY_TYPE;
25 #define FORMAT RTAUDIO_SINT16
30 #define FORMAT RTAUDIO_SINT24
31 #define SCALE 8388607.0
33 typedef signed int MY_TYPE;
34 #define FORMAT RTAUDIO_SINT32
35 #define SCALE 2147483647.0
37 typedef float MY_TYPE;
38 #define FORMAT RTAUDIO_FLOAT32
41 typedef double MY_TYPE;
42 #define FORMAT RTAUDIO_FLOAT64
46 // Platform-dependent sleep routines.
47 #if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
49 #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
50 #else // Unix variants
52 #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
56 // Error function in case of incorrect command-line
57 // argument specifications
58 std::cout << "\nuseage: playraw N fs file <device> <channelOffset>\n";
59 std::cout << " where N = number of channels,\n";
60 std::cout << " fs = the sample rate, \n";
61 std::cout << " file = the raw file to play,\n";
62 std::cout << " device = optional device to use (default = 0),\n";
63 std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";
69 unsigned int channels;
72 // Interleaved buffers
73 int output( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
74 double streamTime, RtAudioStreamStatus status, void *data )
76 OutputData *oData = (OutputData*) data;
78 // In general, it's not a good idea to do file input in the audio
79 // callback function but I'm doing it here because I don't know the
80 // length of the file we are reading.
81 unsigned int count = fread( outputBuffer, oData->channels * sizeof( MY_TYPE ), nBufferFrames, oData->fd);
82 if ( count < nBufferFrames ) {
83 unsigned int bytes = (nBufferFrames - count) * oData->channels * sizeof( MY_TYPE );
84 unsigned int startByte = count * oData->channels * sizeof( MY_TYPE );
85 memset( (char *)(outputBuffer)+startByte, 0, bytes );
92 int main( int argc, char *argv[] )
94 unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
97 // minimal command-line checking
98 if ( argc < 4 || argc > 6 ) usage();
101 if ( dac.getDeviceCount() < 1 ) {
102 std::cout << "\nNo audio devices found!\n";
106 channels = (unsigned int) atoi( argv[1]) ;
107 fs = (unsigned int) atoi( argv[2] );
110 device = (unsigned int) atoi( argv[4] );
112 offset = (unsigned int) atoi( argv[5] );
115 data.fd = fopen( file, "rb" );
117 std::cout << "Unable to find or open file!\n";
121 // Set our stream parameters for output only.
123 RtAudio::StreamParameters oParams;
124 oParams.deviceId = device;
125 oParams.nChannels = channels;
126 oParams.firstChannel = offset;
128 data.channels = channels;
130 dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &output, (void *)&data );
133 catch ( RtError& e ) {
134 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
138 std::cout << "\nPlaying raw file " << file << " (buffer frames = " << bufferFrames << ")." << std::endl;
140 SLEEP( 100 ); // wake every 100 ms to check if we're done
141 if ( dac.isStreamRunning() == false ) break;