1 /******************************************/
4 by Gary P. Scavone, 2006
6 This program will output sawtooth waveforms
7 of different frequencies on each channel.
9 /******************************************/
15 typedef signed long MY_TYPE;
16 #define FORMAT RTAUDIO_SINT24
17 #define SCALE 2147483647.0
20 #define FORMAT RTAUDIO_SINT8
24 typedef signed short MY_TYPE;
25 #define FORMAT RTAUDIO_SINT16
29 typedef signed long MY_TYPE;
30 #define FORMAT RTAUDIO_SINT32
31 #define SCALE 2147483647.0
33 typedef float MY_TYPE;
34 #define FORMAT RTAUDIO_FLOAT32
37 typedef double MY_TYPE;
38 #define FORMAT RTAUDIO_FLOAT64
42 #define BASE_RATE 0.005
46 // Error function in case of incorrect command-line
47 // argument specifications
48 std::cout << "\nuseage: playsaw N fs <device> <channelOffset>\n";
49 std::cout << " where N = number of channels,\n";
50 std::cout << " fs = the sample rate,\n";
51 std::cout << " device = optional device to use (default = 0),\n";
52 std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";
56 unsigned int channels;
57 RtAudio::StreamOptions options;
59 //#define USE_INTERLEAVED
60 #if defined( USE_INTERLEAVED )
62 // Interleaved buffers
63 int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
64 double streamTime, RtAudioStreamStatus status, void *data )
67 extern unsigned int channels;
68 MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
69 double *lastValues = (double *) data;
72 std::cout << "Stream underflow detected!" << std::endl;
74 for ( i=0; i<nBufferFrames; i++ ) {
75 for ( j=0; j<channels; j++ ) {
76 *buffer++ = (MY_TYPE) (lastValues[j] * SCALE);
77 lastValues[j] += BASE_RATE * (j+1+(j*0.1));
78 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
85 #else // Use non-interleaved buffers
87 int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
88 double streamTime, RtAudioStreamStatus status, void *data )
91 extern unsigned int channels;
92 MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
93 double *lastValues = (double *) data;
96 std::cout << "Stream underflow detected!" << std::endl;
99 for ( j=0; j<channels; j++ ) {
100 increment = BASE_RATE * (j+1+(j*0.1));
101 for ( i=0; i<nBufferFrames; i++ ) {
102 *buffer++ = (MY_TYPE) (lastValues[j] * SCALE);
103 lastValues[j] += increment;
104 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
112 int main( int argc, char *argv[] )
114 unsigned int bufferFrames, fs, device = 0, offset = 0;
116 // minimal command-line checking
117 if (argc < 3 || argc > 5 ) usage();
120 if ( dac.getDeviceCount() < 1 ) {
121 std::cout << "\nNo audio devices found!\n";
125 channels = (unsigned int) atoi( argv[1] );
126 fs = (unsigned int) atoi( argv[2] );
128 device = (unsigned int) atoi( argv[3] );
130 offset = (unsigned int) atoi( argv[4] );
132 double *data = (double *) calloc( channels, sizeof( double ) );
134 // Let RtAudio print messages to stderr.
135 dac.showWarnings( true );
137 // Set our stream parameters for output only.
139 RtAudio::StreamParameters oParams;
140 oParams.deviceId = device;
141 oParams.nChannels = channels;
142 oParams.firstChannel = offset;
144 options.flags |= RTAUDIO_HOG_DEVICE;
145 #if !defined( USE_INTERLEAVED )
146 options.flags |= RTAUDIO_NONINTERLEAVED;
149 dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options );
152 catch ( RtError& e ) {
158 //std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
159 std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
160 std::cin.get( input );
166 catch ( RtError& e ) {
171 if ( dac.isStreamOpen() ) dac.closeStream();