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 /******************************************/
17 #define FORMAT RTAUDIO_SINT8
20 typedef signed short MY_TYPE;
21 #define FORMAT RTAUDIO_SINT16
24 typedef signed long MY_TYPE;
25 #define FORMAT RTAUDIO_SINT24
26 #define SCALE 8388607.0
28 typedef signed long MY_TYPE;
29 #define FORMAT RTAUDIO_SINT32
30 #define SCALE 2147483647.0
33 typedef float MY_TYPE;
34 #define FORMAT RTAUDIO_FLOAT32
38 typedef double MY_TYPE;
39 #define FORMAT RTAUDIO_FLOAT64
43 // Platform-dependent sleep routines.
44 #if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
46 #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
47 #else // Unix variants
49 #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
53 // Error function in case of incorrect command-line
54 // argument specifications
55 std::cout << "\nuseage: playraw N fs file <device> <channelOffset>\n";
56 std::cout << " where N = number of channels,\n";
57 std::cout << " fs = the sample rate, \n";
58 std::cout << " file = the raw file to play,\n";
59 std::cout << " device = optional device to use (default = 0),\n";
60 std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";
66 unsigned int channels;
69 // Interleaved buffers
70 int output( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
71 double streamTime, RtAudioStreamStatus status, void *data )
73 OutputData *oData = (OutputData*) data;
75 // In general, it's not a good idea to do file input in the audio
76 // callback function but I'm doing it here because I don't know the
77 // length of the file we are reading.
78 unsigned int count = fread( outputBuffer, oData->channels * sizeof( MY_TYPE ), nBufferFrames, oData->fd);
79 if ( count < nBufferFrames ) {
80 unsigned int bytes = (nBufferFrames - count) * oData->channels * sizeof( MY_TYPE );
81 unsigned int startByte = count * oData->channels * sizeof( MY_TYPE );
82 memset( (char *)(outputBuffer)+startByte, 0, bytes );
89 int main( int argc, char *argv[] )
91 unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
94 // minimal command-line checking
95 if ( argc < 4 || argc > 6 ) usage();
98 if ( dac.getDeviceCount() < 1 ) {
99 std::cout << "\nNo audio devices found!\n";
103 channels = (unsigned int) atoi( argv[1]) ;
104 fs = (unsigned int) atoi( argv[2] );
107 device = (unsigned int) atoi( argv[4] );
109 offset = (unsigned int) atoi(argv[5]);
112 data.fd = fopen( file, "rb" );
114 std::cout << "Unable to find or open file!\n";
118 // Set our stream parameters for output only.
120 RtAudio::StreamParameters oParams;
121 oParams.deviceId = device;
122 oParams.nChannels = channels;
123 oParams.firstChannel = offset;
125 data.channels = channels;
127 dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &output, (void *)&data );
130 catch ( RtError& e ) {
131 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
135 std::cout << "\nPlaying raw file " << file << " (buffer frames = " << bufferFrames << ")." << std::endl;
137 SLEEP( 100 ); // wake every 100 ms to check if we're done
138 if ( dac.isStreamRunning() == false ) break;