Renamed RtError class to RtAudioError and embedded it in RtAudio.h.
[rtaudio-cdist.git] / tests / playsaw.cpp
index 772881c1657c63cc62fb73a2d69980f251ba99e7..a590ca18ddf59f655efe8a8434fc0cce9d77e332 100644 (file)
 
 #include "RtAudio.h"
 #include <iostream>
+#include <cstdlib>
 
 /*
-typedef signed long  MY_TYPE;
-#define FORMAT RTAUDIO_SINT24
-#define SCALE  2147483647.0
-
-typedef char  MY_TYPE;
+typedef char MY_TYPE;
 #define FORMAT RTAUDIO_SINT8
 #define SCALE  127.0
+*/
 
-typedef signed short  MY_TYPE;
+typedef signed short MY_TYPE;
 #define FORMAT RTAUDIO_SINT16
 #define SCALE  32767.0
 
-typedef signed long  MY_TYPE;
+/*
+typedef S24 MY_TYPE;
+#define FORMAT RTAUDIO_SINT24
+#define SCALE  8388607.0
+
+typedef signed long MY_TYPE;
 #define FORMAT RTAUDIO_SINT32
 #define SCALE  2147483647.0
-*/
 
-typedef float  MY_TYPE;
+typedef float MY_TYPE;
 #define FORMAT RTAUDIO_FLOAT32
 #define SCALE  1.0
 
-/*
-typedef double  MY_TYPE;
+typedef double MY_TYPE;
 #define FORMAT RTAUDIO_FLOAT64
 #define SCALE  1.0
 */
 
+// Platform-dependent sleep routines.
+#if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
+  #include <windows.h>
+  #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) 
+#else // Unix variants
+  #include <unistd.h>
+  #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
+#endif
+
 #define BASE_RATE 0.005
 #define TIME   1.0
 
 void usage( void ) {
   // Error function in case of incorrect command-line
   // argument specifications
-  std::cout << "\nuseage: playsaw N fs <device> <channelOffset>\n";
+  std::cout << "\nuseage: playsaw N fs <device> <channelOffset> <time>\n";
   std::cout << "    where N = number of channels,\n";
   std::cout << "    fs = the sample rate,\n";
   std::cout << "    device = optional device to use (default = 0),\n";
-  std::cout << "    and channelOffset = an optional channel offset on the device (default = 0).\n\n";
+  std::cout << "    channelOffset = an optional channel offset on the device (default = 0),\n";
+  std::cout << "    and time = an optional time duration in seconds (default = no limit).\n\n";
   exit( 0 );
 }
 
 unsigned int channels;
 RtAudio::StreamOptions options;
+unsigned int frameCounter = 0;
+bool checkCount = false;
+unsigned int nFrames = 0;
+const unsigned int callbackReturnValue = 1;
 
 //#define USE_INTERLEAVED
 #if defined( USE_INTERLEAVED )
@@ -73,12 +88,14 @@ int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
 
   for ( i=0; i<nBufferFrames; i++ ) {
     for ( j=0; j<channels; j++ ) {
-      *buffer++ = (MY_TYPE) (lastValues[j] * SCALE);
+      *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
       lastValues[j] += BASE_RATE * (j+1+(j*0.1));
       if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
     }
   }
 
+  frameCounter += nBufferFrames;
+  if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
   return 0;
 }
 
@@ -99,12 +116,14 @@ int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
   for ( j=0; j<channels; j++ ) {
     increment = BASE_RATE * (j+1+(j*0.1));
     for ( i=0; i<nBufferFrames; i++ ) {
-      *buffer++ = (MY_TYPE) (lastValues[j] * SCALE);
+      *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
       lastValues[j] += increment;
       if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
     }
   }
 
+  frameCounter += nBufferFrames;
+  if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
   return 0;
 }
 #endif
@@ -114,7 +133,7 @@ int main( int argc, char *argv[] )
   unsigned int bufferFrames, fs, device = 0, offset = 0;
 
   // minimal command-line checking
-  if (argc < 3 || argc > 5 ) usage();
+  if (argc < 3 || argc > 6 ) usage();
 
   RtAudio dac;
   if ( dac.getDeviceCount() < 1 ) {
@@ -128,21 +147,24 @@ int main( int argc, char *argv[] )
     device = (unsigned int) atoi( argv[3] );
   if ( argc > 4 )
     offset = (unsigned int) atoi( argv[4] );
+  if ( argc > 5 )
+    nFrames = (unsigned int) (fs * atof( argv[5] ));
+  if ( nFrames > 0 ) checkCount = true;
 
-  double *data;
-  data = (double *) calloc( channels, sizeof( double ) );
+  double *data = (double *) calloc( channels, sizeof( double ) );
 
   // Let RtAudio print messages to stderr.
   dac.showWarnings( true );
 
   // Set our stream parameters for output only.
-  bufferFrames = 256;
+  bufferFrames = 512;
   RtAudio::StreamParameters oParams;
   oParams.deviceId = device;
   oParams.nChannels = channels;
   oParams.firstChannel = offset;
 
-  options.flags |= RTAUDIO_HOG_DEVICE;
+  options.flags = RTAUDIO_HOG_DEVICE;
+  options.flags |= RTAUDIO_SCHEDULE_REALTIME;
 #if !defined( USE_INTERLEAVED )
   options.flags |= RTAUDIO_NONINTERLEAVED;
 #endif
@@ -150,22 +172,27 @@ int main( int argc, char *argv[] )
     dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options );
     dac.startStream();
   }
-  catch ( RtError& e ) {
+  catch ( RtAudioError& e ) {
     e.printMessage();
     goto cleanup;
   }
 
-  char input;
-  std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
-  std::cin.get( input );
-
-  try {
-    // Stop the stream
-    std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
-    dac.stopStream();
+  if ( checkCount ) {
+    while ( dac.isStreamRunning() == true ) SLEEP( 100 );
   }
-  catch ( RtError& e ) {
-    e.printMessage();
+  else {
+    char input;
+    //std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
+    std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
+    std::cin.get( input );
+
+    try {
+      // Stop the stream
+      dac.stopStream();
+    }
+    catch ( RtAudioError& e ) {
+      e.printMessage();
+    }
   }
 
  cleanup: