Various changes for true 24-bit support, changes regarding Jack get_latency, and...
[rtaudio-cdist.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( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
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 unsigned int channels;
68 RtAudio::StreamOptions options;
69 unsigned int frameCounter = 0;
70 bool checkCount = false;
71 unsigned int nFrames = 0;
72 const unsigned int callbackReturnValue = 1;
73
74 //#define USE_INTERLEAVED
75 #if defined( USE_INTERLEAVED )
76
77 // Interleaved buffers
78 int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
79          double streamTime, RtAudioStreamStatus status, void *data )
80 {
81   unsigned int i, j;
82   extern unsigned int channels;
83   MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
84   double *lastValues = (double *) data;
85
86   if ( status )
87     std::cout << "Stream underflow detected!" << std::endl;
88
89   for ( i=0; i<nBufferFrames; i++ ) {
90     for ( j=0; j<channels; j++ ) {
91       *buffer++ = (MY_TYPE) (lastValues[j] * SCALE);
92       lastValues[j] += BASE_RATE * (j+1+(j*0.1));
93       if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
94     }
95   }
96
97   frameCounter += nBufferFrames;
98   if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
99   return 0;
100 }
101
102 #else // Use non-interleaved buffers
103
104 int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
105          double streamTime, RtAudioStreamStatus status, void *data )
106 {
107   unsigned int i, j;
108   extern unsigned int channels;
109   MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
110   double *lastValues = (double *) data;
111
112   if ( status )
113     std::cout << "Stream underflow detected!" << std::endl;
114
115   double increment;
116   for ( j=0; j<channels; j++ ) {
117     increment = BASE_RATE * (j+1+(j*0.1));
118     for ( i=0; i<nBufferFrames; i++ ) {
119       *buffer++ = (MY_TYPE) (lastValues[j] * SCALE);
120       lastValues[j] += increment;
121       if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
122     }
123   }
124
125   frameCounter += nBufferFrames;
126   if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
127   return 0;
128 }
129 #endif
130
131 int main( int argc, char *argv[] )
132 {
133   unsigned int bufferFrames, fs, device = 0, offset = 0;
134
135   // minimal command-line checking
136   if (argc < 3 || argc > 6 ) usage();
137
138   RtAudio dac;
139   if ( dac.getDeviceCount() < 1 ) {
140     std::cout << "\nNo audio devices found!\n";
141     exit( 1 );
142   }
143
144   channels = (unsigned int) atoi( argv[1] );
145   fs = (unsigned int) atoi( argv[2] );
146   if ( argc > 3 )
147     device = (unsigned int) atoi( argv[3] );
148   if ( argc > 4 )
149     offset = (unsigned int) atoi( argv[4] );
150   if ( argc > 5 )
151     nFrames = (unsigned int) (fs * atof( argv[5] ));
152   if ( nFrames > 0 ) checkCount = true;
153
154   double *data = (double *) calloc( channels, sizeof( double ) );
155
156   // Let RtAudio print messages to stderr.
157   dac.showWarnings( true );
158
159   // Set our stream parameters for output only.
160   bufferFrames = 512;
161   RtAudio::StreamParameters oParams;
162   oParams.deviceId = device;
163   oParams.nChannels = channels;
164   oParams.firstChannel = offset;
165
166   options.flags = RTAUDIO_HOG_DEVICE;
167   options.flags |= RTAUDIO_SCHEDULE_REALTIME;
168 #if !defined( USE_INTERLEAVED )
169   options.flags |= RTAUDIO_NONINTERLEAVED;
170 #endif
171   try {
172     dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options );
173     dac.startStream();
174   }
175   catch ( RtError& e ) {
176     e.printMessage();
177     goto cleanup;
178   }
179
180   if ( checkCount ) {
181     while ( dac.isStreamRunning() == true ) SLEEP( 100 );
182   }
183   else {
184     char input;
185     //std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
186     std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
187     std::cin.get( input );
188
189     try {
190       // Stop the stream
191       dac.stopStream();
192     }
193     catch ( RtError& e ) {
194       e.printMessage();
195     }
196   }
197
198  cleanup:
199   if ( dac.isStreamOpen() ) dac.closeStream();
200   free( data );
201
202   return 0;
203 }