1 /******************************************/
4 by Gary P. Scavone, 2006
6 This program will output sawtooth waveforms
7 of different frequencies on each channel.
9 /******************************************/
17 #define FORMAT RTAUDIO_SINT8
21 typedef signed short MY_TYPE;
22 #define FORMAT RTAUDIO_SINT16
27 #define FORMAT RTAUDIO_SINT24
28 #define SCALE 8388607.0
30 typedef signed long MY_TYPE;
31 #define FORMAT RTAUDIO_SINT32
32 #define SCALE 2147483647.0
34 typedef float MY_TYPE;
35 #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__ ) || defined( __WINDOWS_WASAPI__ )
46 #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
47 #else // Unix variants
49 #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
52 #define BASE_RATE 0.005
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";
67 void errorCallback( RtAudioError::Type type, const std::string &errorText )
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 ) );
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 = 1;
85 //#define USE_INTERLEAVED
86 #if defined( USE_INTERLEAVED )
88 // Interleaved buffers
89 int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
90 double streamTime, RtAudioStreamStatus status, void *data )
93 extern unsigned int channels;
94 MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
95 double *lastValues = (double *) data;
98 std::cout << "Stream underflow detected!" << std::endl;
100 for ( i=0; i<nBufferFrames; i++ ) {
101 for ( j=0; j<channels; j++ ) {
102 *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
103 lastValues[j] += BASE_RATE * (j+1+(j*0.1));
104 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
108 frameCounter += nBufferFrames;
109 if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
113 #else // Use non-interleaved buffers
115 int saw( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
116 double /*streamTime*/, RtAudioStreamStatus status, void *data )
119 extern unsigned int channels;
120 MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
121 double *lastValues = (double *) data;
124 std::cout << "Stream underflow detected!" << std::endl;
127 for ( j=0; j<channels; j++ ) {
128 increment = BASE_RATE * (j+1+(j*0.1));
129 for ( i=0; i<nBufferFrames; i++ ) {
130 *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
131 lastValues[j] += increment;
132 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
136 frameCounter += nBufferFrames;
137 if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
142 int main( int argc, char *argv[] )
144 unsigned int bufferFrames, fs, device = 0, offset = 0;
146 // minimal command-line checking
147 if (argc < 3 || argc > 6 ) usage();
150 if ( dac.getDeviceCount() < 1 ) {
151 std::cout << "\nNo audio devices found!\n";
155 channels = (unsigned int) atoi( argv[1] );
156 fs = (unsigned int) atoi( argv[2] );
158 device = (unsigned int) atoi( argv[3] );
160 offset = (unsigned int) atoi( argv[4] );
162 nFrames = (unsigned int) (fs * atof( argv[5] ));
163 if ( nFrames > 0 ) checkCount = true;
165 double *data = (double *) calloc( channels, sizeof( double ) );
167 // Let RtAudio print messages to stderr.
168 dac.showWarnings( true );
170 // Set our stream parameters for output only.
172 RtAudio::StreamParameters oParams;
173 oParams.deviceId = device;
174 oParams.nChannels = channels;
175 oParams.firstChannel = offset;
178 oParams.deviceId = dac.getDefaultOutputDevice();
180 options.flags = RTAUDIO_HOG_DEVICE;
181 options.flags |= RTAUDIO_SCHEDULE_REALTIME;
182 #if !defined( USE_INTERLEAVED )
183 options.flags |= RTAUDIO_NONINTERLEAVED;
186 dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options, &errorCallback );
189 catch ( RtAudioError& e ) {
195 while ( dac.isStreamRunning() == true ) SLEEP( 100 );
199 //std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
200 std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
201 std::cin.get( input );
207 catch ( RtAudioError& e ) {
213 if ( dac.isStreamOpen() ) dac.closeStream();