Updates to OS-X for multi-stream support (GS).
[rtaudio-cdist.git] / tests / playraw.cpp
1 /******************************************/
2 /*
3   playraw.cpp
4   by Gary P. Scavone, 2007
5
6   Play a specified raw file.  It is necessary
7   that the file be of the same data format as
8   defined below.
9 */
10 /******************************************/
11
12 #include "RtAudio.h"
13 #include <iostream>
14
15 /*
16 typedef char  MY_TYPE;
17 #define FORMAT RTAUDIO_SINT8
18 #define SCALE  127.0
19 */
20
21 typedef signed short  MY_TYPE;
22 #define FORMAT RTAUDIO_SINT16
23 #define SCALE  32767.0
24
25 /*
26 typedef signed long  MY_TYPE;
27 #define FORMAT RTAUDIO_SINT24
28 #define SCALE  8388607.0
29
30 typedef signed long  MY_TYPE;
31 #define FORMAT RTAUDIO_SINT32
32 #define SCALE  2147483647.0
33
34 typedef float  MY_TYPE;
35 #define FORMAT RTAUDIO_FLOAT32
36 #define SCALE  1.0;
37
38 typedef double  MY_TYPE;
39 #define FORMAT RTAUDIO_FLOAT64
40 #define SCALE  1.0;
41 */
42
43 // Platform-dependent sleep routines.
44 #if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
45   #include <windows.h>
46   #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) 
47 #else // Unix variants
48   #include <unistd.h>
49   #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
50 #endif
51
52 void usage( void ) {
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";
61   exit( 0 );
62 }
63
64 struct OutputData {
65   FILE *fd;
66   unsigned int channels;
67 };
68
69 // Interleaved buffers
70 int output( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
71             double streamTime, RtAudioStreamStatus status, void *data )
72 {
73   OutputData *oData = (OutputData*) data;
74
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 );
83     return 1;
84   }
85
86   return 0;
87 }
88
89 int main( int argc, char *argv[] )
90 {
91   unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
92   char *file;
93
94   // minimal command-line checking
95   if ( argc < 4 || argc > 6 ) usage();
96
97   RtAudio dac;
98   if ( dac.getDeviceCount() < 1 ) {
99     std::cout << "\nNo audio devices found!\n";
100     exit( 0 );
101   }
102
103   channels = (unsigned int) atoi( argv[1]) ;
104   fs = (unsigned int) atoi( argv[2] );
105   file = argv[3];
106   if ( argc > 4 )
107     device = (unsigned int) atoi( argv[4] );
108   if ( argc > 5 )
109     offset = (unsigned int) atoi( argv[5] );
110
111   OutputData data;
112   data.fd = fopen( file, "rb" );
113   if ( !data.fd ) {
114     std::cout << "Unable to find or open file!\n";
115     exit( 1 );
116   }
117
118   // Set our stream parameters for output only.
119   bufferFrames = 512;
120   RtAudio::StreamParameters oParams;
121   oParams.deviceId = device;
122   oParams.nChannels = channels;
123   oParams.firstChannel = offset;
124
125   data.channels = channels;
126   try {
127     dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &output, (void *)&data );
128     dac.startStream();
129   }
130   catch ( RtError& e ) {
131     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
132     goto cleanup;
133   }
134
135   std::cout << "\nPlaying raw file " << file << " (buffer frames = " << bufferFrames << ")." << std::endl;
136   while ( 1 ) {
137     SLEEP( 100 ); // wake every 100 ms to check if we're done
138     if ( dac.isStreamRunning() == false ) break;
139   }
140
141  cleanup:
142   fclose( data.fd );
143   dac.closeStream();
144
145   return 0;
146 }