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