Almost complete removal of exceptions for RtApiCore, plus an update for changing...
[rtaudio.git] / tests / playsaw.cpp
1 /******************************************/
2 /*
3   playsaw.cpp
4   by Gary P. Scavone, 2006
5
6   This program will output sawtooth waveforms
7   of different frequencies on each channel.
8 */
9 /******************************************/
10
11 #include "RtAudio.h"
12 #include <iostream>
13 #include <cstdlib>
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 S24 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( WIN32 )
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 #define BASE_RATE 0.005
53 #define TIME   1.0
54
55 void usage( void ) {
56   // Error function in case of incorrect command-line
57   // argument specifications
58   std::cout << "\nuseage: playsaw N fs <device> <channelOffset> <time>\n";
59   std::cout << "    where N = number of channels,\n";
60   std::cout << "    fs = the sample rate,\n";
61   std::cout << "    device = optional device to use (default = 0),\n";
62   std::cout << "    channelOffset = an optional channel offset on the device (default = 0),\n";
63   std::cout << "    and time = an optional time duration in seconds (default = no limit).\n\n";
64   exit( 0 );
65 }
66
67 void errorCallback( RtAudioError::Type /*type*/, const std::string &errorText )
68 {
69   // This example error handling function does exactly the same thing
70   // as the embedded RtAudio::error() function.
71   std::cout << "in errorCallback" << std::endl;
72   //if ( type == RtAudioError::WARNING )
73     std::cerr << '\n' << errorText << "\n\n";
74     //else if ( type != RtAudioError::WARNING )
75     //throw( RtAudioError( errorText, type ) );
76 }
77
78 unsigned int channels;
79 RtAudio::StreamOptions options;
80 unsigned int frameCounter = 0;
81 bool checkCount = false;
82 unsigned int nFrames = 0;
83 const unsigned int callbackReturnValue = 2;
84 double streamTimePrintIncrement = 1.0; // seconds
85 double streamTimePrintTime = 1.0; // seconds
86
87 //#define USE_INTERLEAVED
88 #if defined( USE_INTERLEAVED )
89
90 // Interleaved buffers
91 int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
92          double streamTime, RtAudioStreamStatus status, void *data )
93 {
94   unsigned int i, j;
95   extern unsigned int channels;
96   MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
97   double *lastValues = (double *) data;
98
99   if ( status )
100     std::cout << "Stream underflow detected!" << std::endl;
101
102   if ( streamTime >= streamTimePrintTime ) {
103     std::cout << "streamTime = " << streamTime << std::endl;
104     streamTimePrintTime += streamTimePrintIncrement;
105   }
106
107   for ( i=0; i<nBufferFrames; i++ ) {
108     for ( j=0; j<channels; j++ ) {
109       *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
110       lastValues[j] += BASE_RATE * (j+1+(j*0.1));
111       if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
112     }
113   }
114
115   frameCounter += nBufferFrames;
116   if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
117   return 0;
118 }
119
120 #else // Use non-interleaved buffers
121
122 int saw( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
123          double streamTime, RtAudioStreamStatus status, void *data )
124 {
125   unsigned int i, j;
126   extern unsigned int channels;
127   MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
128   double *lastValues = (double *) data;
129
130   if ( status )
131     std::cout << "Stream underflow detected!" << std::endl;
132
133   if ( streamTime >= streamTimePrintTime ) {
134     std::cout << "streamTime = " << streamTime << std::endl;
135     streamTimePrintTime += streamTimePrintIncrement;
136   }
137   
138   double increment;
139   for ( j=0; j<channels; j++ ) {
140     increment = BASE_RATE * (j+1+(j*0.1));
141     for ( i=0; i<nBufferFrames; i++ ) {
142       *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
143       lastValues[j] += increment;
144       if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
145     }
146   }
147
148   frameCounter += nBufferFrames;
149   if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
150   return 0;
151 }
152 #endif
153
154 int main( int argc, char *argv[] )
155 {
156   unsigned int bufferFrames, fs, device = 0, offset = 0;
157
158   // minimal command-line checking
159   if (argc < 3 || argc > 6 ) usage();
160
161   RtAudio dac;
162   if ( dac.getDeviceCount() < 1 ) {
163     std::cout << "\nNo audio devices found!\n";
164     exit( 1 );
165   }
166
167   channels = (unsigned int) atoi( argv[1] );
168   fs = (unsigned int) atoi( argv[2] );
169   if ( argc > 3 )
170     device = (unsigned int) atoi( argv[3] );
171   if ( argc > 4 )
172     offset = (unsigned int) atoi( argv[4] );
173   if ( argc > 5 )
174     nFrames = (unsigned int) (fs * atof( argv[5] ));
175   if ( nFrames > 0 ) checkCount = true;
176
177   double *data = (double *) calloc( channels, sizeof( double ) );
178
179   // Let RtAudio print messages to stderr.
180   dac.showWarnings( true );
181
182   // Set our stream parameters for output only.
183   bufferFrames = 512;
184   RtAudio::StreamParameters oParams;
185   oParams.deviceId = device;
186   oParams.nChannels = channels;
187   oParams.firstChannel = offset;
188
189   if ( device == 0 )
190     oParams.deviceId = dac.getDefaultOutputDevice();
191
192   options.flags = RTAUDIO_HOG_DEVICE;
193   options.flags |= RTAUDIO_SCHEDULE_REALTIME;
194 #if !defined( USE_INTERLEAVED )
195   options.flags |= RTAUDIO_NONINTERLEAVED;
196 #endif
197   //  try {
198     dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options, &errorCallback );
199     if ( dac.isStreamOpen() == false ) goto cleanup;
200     dac.startStream();
201     //  }
202     //  catch ( RtAudioError& e ) {
203     //    e.printMessage();
204     //goto cleanup;
205     //  }
206
207   if ( checkCount ) {
208     while ( dac.isStreamRunning() == true ) SLEEP( 100 );
209   }
210   else {
211     char input;
212     //std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
213     std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
214     std::cin.get( input );
215
216     //try {
217       // Stop the stream
218       dac.stopStream();
219       //dac.abortStream();
220   }
221       /*
222     catch ( RtAudioError& e ) {
223       e.printMessage();
224     }
225   }
226       */
227
228  cleanup:
229   if ( dac.isStreamOpen() ) dac.closeStream();
230   free( data );
231
232   return 0;
233 }