RtAudio.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 /************************************************************************/
00059 /************************************************************************/
00060 
00061 #if !defined(__RTAUDIO_H)
00062 #define __RTAUDIO_H
00063 
00064 #include <map>
00065 
00066 #if defined(__LINUX_ALSA__)
00067 
00068   /* alsa 0.9 backward compat for alsa 1.0 */
00069   #define ALSA_PCM_OLD_HW_PARAMS_API
00070   #define ALSA_PCM_OLD_SW_PARAMS_API
00071 
00072   #include <alsa/asoundlib.h>
00073   #include <pthread.h>
00074   #include <unistd.h>
00075 
00076   typedef snd_pcm_t *AUDIO_HANDLE;
00077   typedef int DEVICE_ID;
00078   typedef pthread_t THREAD_HANDLE;
00079   typedef pthread_mutex_t MUTEX;
00080 
00081 #elif defined(__LINUX_OSS__)
00082   #include <pthread.h>
00083   #include <unistd.h>
00084 
00085   typedef int AUDIO_HANDLE;
00086   typedef int DEVICE_ID;
00087   typedef pthread_t THREAD_HANDLE;
00088   typedef pthread_mutex_t MUTEX;
00089 
00090 #elif defined(__WINDOWS_DS__)
00091   #include "CLAM_windows.h"
00092   #include <process.h>
00093 
00094   // The following struct is used to hold the extra variables
00095   // specific to the DirectSound implementation.
00096   typedef struct {
00097     void * object;
00098     void * buffer;
00099     UINT bufferPointer;
00100   } AUDIO_HANDLE;
00101 
00102   typedef LPGUID DEVICE_ID;
00103   typedef unsigned long THREAD_HANDLE;
00104   typedef CRITICAL_SECTION MUTEX;
00105 
00106 #elif defined(__WINDOWS_ASIO__)
00107   #include "CLAM_windows.h"
00108   #include <process.h>
00109 
00110   typedef int AUDIO_HANDLE;
00111   typedef int DEVICE_ID;
00112   typedef unsigned long THREAD_HANDLE;
00113   typedef CRITICAL_SECTION MUTEX;
00114 
00115 #elif defined(__IRIX_AL__)
00116   #include <dmedia/audio.h>
00117   #include <pthread.h>
00118   #include <unistd.h>
00119 
00120   typedef ALport AUDIO_HANDLE;
00121   typedef long DEVICE_ID;
00122   typedef pthread_t THREAD_HANDLE;
00123   typedef pthread_mutex_t MUTEX;
00124 
00125 #elif defined(__MACOSX_CORE__)
00126 
00127   #include <CoreAudio/AudioHardware.h>
00128   #include <pthread.h>
00129 
00130   typedef unsigned int AUDIO_HANDLE;
00131   typedef AudioDeviceID DEVICE_ID;
00132   typedef pthread_t THREAD_HANDLE;
00133   typedef pthread_mutex_t MUTEX;
00134 
00135 #endif
00136 
00137 
00138 /************************************************************************/
00151 /************************************************************************/
00152 
00153 class RtError
00154 {
00155 public:
00157   enum TYPE {
00158     WARNING,
00159     DEBUG_WARNING,
00160     UNSPECIFIED,
00161     NO_DEVICES_FOUND,
00162     INVALID_DEVICE,
00163     INVALID_STREAM,
00164     MEMORY_ERROR,
00165     INVALID_PARAMETER,
00166     DRIVER_ERROR,
00167     SYSTEM_ERROR,
00168     THREAD_ERROR
00169   };
00170 
00171 protected:
00172   char error_message[256];
00173   TYPE type;
00174 
00175 public:
00177   RtError(const char *p, TYPE tipe = RtError::UNSPECIFIED);
00178 
00180   virtual ~RtError(void);
00181 
00183   virtual void printMessage(void);
00184 
00186   virtual const TYPE& getType(void) { return type; }
00187 
00189   virtual const char *getMessage(void) { return error_message; }
00190 };
00191 
00192 
00193 // This public structure type is used to pass callback information
00194 // between the private RtAudio stream structure and global callback
00195 // handling functions.
00196 typedef struct {
00197   void *object;  // Used as a "this" pointer.
00198   int streamId;
00199   DEVICE_ID device[2];
00200   THREAD_HANDLE thread;
00201   void *callback;
00202   void *buffers;
00203   unsigned long waitTime;
00204   bool blockTick;
00205   bool stopStream;
00206   bool usingCallback;
00207   void *userData;
00208 } CALLBACK_INFO;
00209 
00210 
00211 // *************************************************** //
00212 //
00213 // RtAudio class declaration.
00214 //
00215 // *************************************************** //
00216 
00217 class RtAudio
00218 {
00219 public:
00220 
00221   // Support for signed integers and floats.  Audio data fed to/from
00222   // the tickStream() routine is assumed to ALWAYS be in host
00223   // byte order.  The internal routines will automatically take care of
00224   // any necessary byte-swapping between the host format and the
00225   // soundcard.  Thus, endian-ness is not a concern in the following
00226   // format definitions.
00227   typedef unsigned long RTAUDIO_FORMAT;
00228   static const RTAUDIO_FORMAT RTAUDIO_SINT8; 
00229   static const RTAUDIO_FORMAT RTAUDIO_SINT16; 
00230   static const RTAUDIO_FORMAT RTAUDIO_SINT24; 
00231   static const RTAUDIO_FORMAT RTAUDIO_SINT32; 
00232   static const RTAUDIO_FORMAT RTAUDIO_FLOAT32; 
00233   static const RTAUDIO_FORMAT RTAUDIO_FLOAT64; 
00235   //static const int MAX_SAMPLE_RATES = 14;
00236   enum { MAX_SAMPLE_RATES = 14 };
00237 
00238   typedef int (*RTAUDIO_CALLBACK)(char *buffer, int bufferSize, void *userData);
00239 
00241   typedef struct {
00242     char name[128];    
00243     DEVICE_ID id[2];  /* No value reported by getDeviceInfo(). */
00244     bool probed;       
00245     int maxOutputChannels; 
00246     int maxInputChannels;  
00247     int maxDuplexChannels; 
00248     int minOutputChannels; 
00249     int minInputChannels;  
00250     int minDuplexChannels; 
00251     bool hasDuplexSupport; 
00252     bool isDefault;        
00253     int nSampleRates;      
00254     int sampleRates[MAX_SAMPLE_RATES]; 
00255     RTAUDIO_FORMAT nativeFormats;     
00256   } RTAUDIO_DEVICE;
00257 
00259 
00265   RtAudio();
00266 
00268 
00279   RtAudio(int *streamId,
00280           int outputDevice, int outputChannels,
00281           int inputDevice, int inputChannels,
00282           RTAUDIO_FORMAT format, int sampleRate,
00283           int *bufferSize, int numberOfBuffers);
00284 
00286 
00290   ~RtAudio();
00291 
00293 
00320   int openStream(int outputDevice, int outputChannels,
00321                  int inputDevice, int inputChannels,
00322                  RTAUDIO_FORMAT format, int sampleRate,
00323                  int *bufferSize, int numberOfBuffers);
00324 
00326 
00345   void setStreamCallback(int streamId, RTAUDIO_CALLBACK callback, void *userData);
00346 
00348 
00355   void cancelStreamCallback(int streamId);
00356 
00358   int getDeviceCount(void);
00359 
00361 
00369   void getDeviceInfo(int device, RTAUDIO_DEVICE *info);
00370 
00372 
00377   char * const getStreamBuffer(int streamId);
00378 
00380 
00385   void tickStream(int streamId);
00386 
00388 
00392   void closeStream(int streamId);
00393 
00395 
00399   void startStream(int streamId);
00400 
00402 
00406   void stopStream(int streamId);
00407 
00409 
00413   void abortStream(int streamId);
00414 
00416 
00421   int streamWillBlock(int streamId);
00422 
00423 #if (defined(__MACOSX_CORE__) || defined(__WINDOWS_ASIO__))
00424   // This function is intended for internal use only.  It must be
00425   // public because it is called by the internal callback handler,
00426   // which is not a member of RtAudio.  External use of this function
00427   // will most likely produce highly undesireable results!
00428   void callbackEvent(int streamId, DEVICE_ID deviceId, void *inData, void *outData);
00429 #endif
00430 
00431 protected:
00432 
00433 private:
00434 
00435   static const unsigned int SAMPLE_RATES[MAX_SAMPLE_RATES];
00436 
00437   enum { FAILURE, SUCCESS };
00438 
00439   enum STREAM_MODE {
00440     OUTPUT,
00441     INPUT,
00442     DUPLEX,
00443     UNINITIALIZED = -75
00444   };
00445 
00446   enum STREAM_STATE {
00447     STREAM_STOPPED,
00448     STREAM_RUNNING
00449   };
00450 
00451   typedef struct {
00452     int device[2];          // Playback and record, respectively.
00453     STREAM_MODE mode;       // OUTPUT, INPUT, or DUPLEX.
00454     AUDIO_HANDLE handle[2]; // Playback and record handles, respectively.
00455     STREAM_STATE state;     // STOPPED or RUNNING
00456     char *userBuffer;
00457     char *deviceBuffer;
00458     bool doConvertBuffer[2]; // Playback and record, respectively.
00459     bool deInterleave[2];    // Playback and record, respectively.
00460     bool doByteSwap[2];      // Playback and record, respectively.
00461     int sampleRate;
00462     int bufferSize;
00463     int nBuffers;
00464     int nUserChannels[2];    // Playback and record, respectively.
00465     int nDeviceChannels[2];  // Playback and record channels, respectively.
00466     RTAUDIO_FORMAT userFormat;
00467     RTAUDIO_FORMAT deviceFormat[2]; // Playback and record, respectively.
00468     MUTEX mutex;
00469     CALLBACK_INFO callbackInfo;
00470   } RTAUDIO_STREAM;
00471 
00472   typedef signed short INT16;
00473   typedef signed int INT32;
00474   typedef float FLOAT32;
00475   typedef double FLOAT64;
00476 
00477   char message[256];
00478   int nDevices;
00479   RTAUDIO_DEVICE *devices;
00480 
00481   std::map<int, void *> streams;
00482 
00484   void error(RtError::TYPE type);
00485 
00490   void initialize(void);
00491 
00496   int getDefaultInputDevice(void);
00497 
00502   int getDefaultOutputDevice(void);
00503 
00505   void clearDeviceInfo(RTAUDIO_DEVICE *info);
00506 
00514   void probeDeviceInfo(RTAUDIO_DEVICE *info);
00515 
00522   bool probeDeviceOpen(int device, RTAUDIO_STREAM *stream,
00523                        STREAM_MODE mode, int channels, 
00524                        int sampleRate, RTAUDIO_FORMAT format,
00525                        int *bufferSize, int numberOfBuffers);
00526 
00533   void *verifyStream(int streamId);
00534 
00539   void convertStreamBuffer(RTAUDIO_STREAM *stream, STREAM_MODE mode);
00540 
00542   void byteSwapBuffer(char *buffer, int samples, RTAUDIO_FORMAT format);
00543 
00545   int formatBytes(RTAUDIO_FORMAT format);
00546 };
00547 
00548 // Define the following flag to have extra information spewed to stderr.
00549 //#define __RTAUDIO_DEBUG__
00550 
00551 #endif
00552 
Generated by  doxygen 1.6.3