f11f83c28f4b90738ca72cba6773dd30f52234f6
[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 S24 MY_TYPE;
30 #define FORMAT RTAUDIO_SINT24
31 #define SCALE  8388607.0
32
33 typedef signed int  MY_TYPE;
34 #define FORMAT RTAUDIO_SINT32
35 #define SCALE  2147483647.0
36
37 typedef float  MY_TYPE;
38 #define FORMAT RTAUDIO_FLOAT32
39 #define SCALE  1.0;
40
41 typedef double  MY_TYPE;
42 #define FORMAT RTAUDIO_FLOAT64
43 #define SCALE  1.0;
44 */
45
46 // Platform-dependent sleep routines.
47 #if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ ) || defined( __WINDOWS_WASAPI__ )
48   #include <windows.h>
49   #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) 
50 #else // Unix variants
51   #include <unistd.h>
52   #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
53 #endif
54
55 void usage( void ) {
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";
64   exit( 0 );
65 }
66
67 struct OutputData {
68   FILE *fd;
69   unsigned int channels;
70 };
71
72 // Interleaved buffers
73 int output( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
74             double /*streamTime*/, RtAudioStreamStatus /*status*/, void *data )
75 {
76   OutputData *oData = (OutputData*) data;
77
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 );
86     return 1;
87   }
88
89   return 0;
90 }
91
92 int main( int argc, char *argv[] )
93 {
94   unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
95   char *file;
96
97   // minimal command-line checking
98   if ( argc < 4 || argc > 6 ) usage();
99
100   RtAudio dac;
101   if ( dac.getDeviceCount() < 1 ) {
102     std::cout << "\nNo audio devices found!\n";
103     exit( 0 );
104   }
105
106   channels = (unsigned int) atoi( argv[1]) ;
107   fs = (unsigned int) atoi( argv[2] );
108   file = argv[3];
109   if ( argc > 4 )
110     device = (unsigned int) atoi( argv[4] );
111   if ( argc > 5 )
112     offset = (unsigned int) atoi( argv[5] );
113
114   OutputData data;
115   data.fd = fopen( file, "rb" );
116   if ( !data.fd ) {
117     std::cout << "Unable to find or open file!\n";
118     exit( 1 );
119   }
120
121   // Set our stream parameters for output only.
122   bufferFrames = 512;
123   RtAudio::StreamParameters oParams;
124   oParams.deviceId = device;
125   oParams.nChannels = channels;
126   oParams.firstChannel = offset;
127
128   if ( device == 0 )
129     oParams.deviceId = dac.getDefaultOutputDevice();
130
131   data.channels = channels;
132   try {
133     dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &output, (void *)&data );
134     dac.startStream();
135   }
136   catch ( RtAudioError& e ) {
137     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
138     goto cleanup;
139   }
140
141   std::cout << "\nPlaying raw file " << file << " (buffer frames = " << bufferFrames << ")." << std::endl;
142   while ( 1 ) {
143     SLEEP( 100 ); // wake every 100 ms to check if we're done
144     if ( dac.isStreamRunning() == false ) break;
145   }
146
147  cleanup:
148   fclose( data.fd );
149   dac.closeStream();
150
151   return 0;
152 }