Version 3.0.2
[rtaudio.git] / RtAudio.h
index fa2d494d297949d2b37b08e357895293d75cde2e..e26f81d186d54e2b7299f0e0290a214577af9a68 100644 (file)
--- a/RtAudio.h
+++ b/RtAudio.h
@@ -9,8 +9,8 @@
 
     RtAudio WWW site: http://music.mcgill.ca/~gary/rtaudio/
 
-    RtAudio: a realtime audio i/o C++ class
-    Copyright (c) 2001-2004 Gary P. Scavone
+    RtAudio: realtime audio i/o C++ classes
+    Copyright (c) 2001-2005 Gary P. Scavone
 
     Permission is hereby granted, free of charge, to any person
     obtaining a copy of this software and associated documentation files
@@ -37,7 +37,7 @@
 */
 /************************************************************************/
 
-// RtAudio: Version 3.0.1, 22 March 2004
+// RtAudio: Version 3.0.2 (14 October 2005)
 
 #ifndef __RTAUDIO_H
 #define __RTAUDIO_H
@@ -127,17 +127,27 @@ class RtApi
 {
 public:
 
+  enum StreamState {
+    STREAM_STOPPED,
+    STREAM_RUNNING
+  };
+
   RtApi();
   virtual ~RtApi();
   void openStream( int outputDevice, int outputChannels,
                    int inputDevice, int inputChannels,
                    RtAudioFormat format, int sampleRate,
                    int *bufferSize, int numberOfBuffers );
+  void openStream( int outputDevice, int outputChannels,
+                   int inputDevice, int inputChannels,
+                   RtAudioFormat format, int sampleRate,
+                   int *bufferSize, int *numberOfBuffers );
   virtual void setStreamCallback( RtAudioCallback callback, void *userData ) = 0;
   virtual void cancelStreamCallback() = 0;
   int getDeviceCount(void);
   RtAudioDeviceInfo getDeviceInfo( int device );
   char * const getStreamBuffer();
+  RtApi::StreamState getStreamState() const;
   virtual void tickStream() = 0;
   virtual void closeStream();
   virtual void startStream() = 0;
@@ -158,9 +168,13 @@ protected:
     UNINITIALIZED = -75
   };
 
-  enum StreamState {
-    STREAM_STOPPED,
-    STREAM_RUNNING
+  // A protected structure used for buffer conversion.
+  struct ConvertInfo {
+    int channels;
+    int inJump, outJump;
+    RtAudioFormat inFormat, outFormat;
+    std::vector<int> inOffset;
+    std::vector<int> outOffset;
   };
 
   // A protected structure for audio streams.
@@ -183,10 +197,10 @@ protected:
     RtAudioFormat deviceFormat[2]; // Playback and record, respectively.
     StreamMutex mutex;
     CallbackInfo callbackInfo;
+    ConvertInfo convertInfo[2];
 
     RtApiStream()
       :apiHandle(0), userBuffer(0), deviceBuffer(0) {}
-    //      mode(UNINITIALIZED), state(STREAM_STOPPED),
   };
 
   // A protected device structure for audio devices.
@@ -217,7 +231,7 @@ protected:
   typedef float Float32;
   typedef double Float64;
 
-  char message_[256];
+  char message_[1024];
   int nDevices_;
   std::vector<RtApiDevice> devices_;
   RtApiStream stream_;
@@ -281,7 +295,7 @@ protected:
     Protected method used to perform format, channel number, and/or interleaving
     conversions between the user and device buffers.
   */
-  void convertStreamBuffer( StreamMode mode );
+  void convertBuffer( char *outBuffer, char *inBuffer, ConvertInfo &info );
 
   //! Protected common method used to perform byte-swapping on buffers.
   void byteSwapBuffer( char *buffer, int samples, RtAudioFormat format );
@@ -350,6 +364,20 @@ public:
            RtAudioFormat format, int sampleRate,
            int *bufferSize, int numberOfBuffers, RtAudioApi api=UNSPECIFIED );
 
+  //! An overloaded constructor which opens a stream and also returns \c numberOfBuffers parameter via pointer argument.
+  /*!
+    See the previous constructor call for details.  This overloaded
+    version differs only in that it takes a pointer argument for the
+    \c numberOfBuffers parameter and returns the value used by the
+    audio device (which may be different from that requested).  Note
+    that the \c numberofBuffers parameter is not used with the Linux
+    Jack, Macintosh CoreAudio, and Windows ASIO APIs.
+  */
+  RtAudio( int outputDevice, int outputChannels,
+           int inputDevice, int inputChannels,
+           RtAudioFormat format, int sampleRate,
+           int *bufferSize, int *numberOfBuffers, RtAudioApi api=UNSPECIFIED );
+
   //! The destructor.
   /*!
     Stops and closes an open stream and devices and deallocates
@@ -389,6 +417,20 @@ public:
                    RtAudioFormat format, int sampleRate,
                    int *bufferSize, int numberOfBuffers );
 
+  //! A public method for opening a stream and also returning \c numberOfBuffers parameter via pointer argument.
+  /*!
+    See the previous function call for details.  This overloaded
+    version differs only in that it takes a pointer argument for the
+    \c numberOfBuffers parameter and returns the value used by the
+    audio device (which may be different from that requested).  Note
+    that the \c numberofBuffers parameter is not used with the Linux
+    Jack, Macintosh CoreAudio, and Windows ASIO APIs.
+  */
+  void openStream( int outputDevice, int outputChannels,
+                   int inputDevice, int inputChannels,
+                   RtAudioFormat format, int sampleRate,
+                   int *bufferSize, int *numberOfBuffers );
+
   //! A public method which sets a user-defined callback function for a given stream.
   /*!
     This method assigns a callback function to a previously opened
@@ -634,6 +676,53 @@ public:
   void setStreamCallback( RtAudioCallback callback, void *userData );
   void cancelStreamCallback();
 
+  public:
+  // \brief Internal structure that provide debug information on the state of a running DSound device.
+  struct RtDsStatistics {
+    // \brief Sample Rate.
+    long sampleRate;
+    // \brief The size of one sample * number of channels on the input device.
+    int inputFrameSize; 
+    // \brief The size of one sample * number of channels on the output device.
+    int outputFrameSize; 
+    /* \brief The number of times the read pointer had to be adjusted to avoid reading from an unsafe buffer position.
+     *
+     * This field is only used when running in DUPLEX mode. INPUT mode devices just wait until the data is 
+     * available.
+     */
+    int numberOfReadOverruns;
+    // \brief The number of times the write pointer had to be adjusted to avoid writing in an unsafe buffer position.
+    int numberOfWriteUnderruns;
+    // \brief Number of bytes by attribute to buffer configuration by which writing must lead the current write pointer.
+    int writeDeviceBufferLeadBytes;
+    // \brief Number of bytes by attributable to the device driver by which writing must lead the current write pointer on this output device.
+    unsigned long writeDeviceSafeLeadBytes;
+    // \brief Number of bytes by which reading must trail the current read pointer on this input device.
+    unsigned long readDeviceSafeLeadBytes; 
+    /* \brief Estimated latency in seconds. 
+    *
+    * For INPUT mode devices, based the latency of the device's safe read pointer, plus one buffer's
+    * worth of additional latency.
+    *
+    * For OUTPUT mode devices, the latency of the device's safe write pointer, plus N buffers of 
+    * additional buffer latency.
+    *
+    * For DUPLEX devices, the sum of latencies for both input and output devices. DUPLEX devices
+    * also back off the read pointers an additional amount in order to maintain synchronization 
+    * between out-of-phase read and write pointers. This time is also included.
+    *
+    * Note that most software packages report latency between the safe write pointer 
+    * and the software lead pointer, excluding the hardware device's safe write pointer 
+    * latency. Figures of 1 or 2ms of latency on Windows audio devices are invariably of this type.
+    * The reality is that hardware devices often have latencies of 30ms or more (often much 
+    * higher for duplex operation).
+    */
+
+    double latency;
+  };
+  // \brief Report on the current state of a running DSound device.
+  static RtDsStatistics getDsStatistics();
+
   private:
 
   void initialize(void);
@@ -641,6 +730,12 @@ public:
   bool probeDeviceOpen( int device, StreamMode mode, int channels, 
                         int sampleRate, RtAudioFormat format,
                         int *bufferSize, int numberOfBuffers );
+
+  bool coInitialized;
+  bool buffersRolling;
+  long duplexPrerollBytes;
+  static RtDsStatistics statistics;
+
 };
 
 #endif
@@ -674,6 +769,9 @@ public:
   bool probeDeviceOpen( int device, StreamMode mode, int channels, 
                         int sampleRate, RtAudioFormat format,
                         int *bufferSize, int numberOfBuffers );
+
+  bool coInitialized;
+
 };
 
 #endif