00001 /******************************************/ 00002 /* 00003 RtAudio - realtime sound I/O C++ class 00004 Version 2.0 by Gary P. Scavone, 2001-2002. 00005 */ 00006 /******************************************/ 00007 00008 #if !defined(__RtAudio_h) 00009 #define __RtAudio_h 00010 00011 #include <map> 00012 00013 #if defined(__LINUX_ALSA_) 00014 #include <alsa/asoundlib.h> 00015 #include <pthread.h> 00016 #include <unistd.h> 00017 00018 typedef snd_pcm_t *AUDIO_HANDLE; 00019 typedef int DEVICE_ID; 00020 typedef pthread_t THREAD_HANDLE; 00021 typedef pthread_mutex_t MUTEX; 00022 00023 #elif defined(__LINUX_OSS_) 00024 #include <pthread.h> 00025 #include <unistd.h> 00026 00027 typedef int AUDIO_HANDLE; 00028 typedef int DEVICE_ID; 00029 typedef pthread_t THREAD_HANDLE; 00030 typedef pthread_mutex_t MUTEX; 00031 00032 #elif defined(__WINDOWS_DS_) 00033 #include <windows.h> 00034 #include <process.h> 00035 00036 // The following struct is used to hold the extra variables 00037 // specific to the DirectSound implementation. 00038 typedef struct { 00039 void * object; 00040 void * buffer; 00041 UINT bufferPointer; 00042 } AUDIO_HANDLE; 00043 00044 typedef LPGUID DEVICE_ID; 00045 typedef unsigned long THREAD_HANDLE; 00046 typedef CRITICAL_SECTION MUTEX; 00047 00048 #elif defined(__IRIX_AL_) 00049 #include <dmedia/audio.h> 00050 #include <pthread.h> 00051 #include <unistd.h> 00052 00053 typedef ALport AUDIO_HANDLE; 00054 typedef int DEVICE_ID; 00055 typedef pthread_t THREAD_HANDLE; 00056 typedef pthread_mutex_t MUTEX; 00057 00058 #endif 00059 00060 00061 // *************************************************** // 00062 // 00063 // RtAudioError class declaration. 00064 // 00065 // *************************************************** // 00066 00067 class RtAudioError 00068 { 00069 public: 00070 enum TYPE { 00071 WARNING, 00072 DEBUG_WARNING, 00073 UNSPECIFIED, 00074 NO_DEVICES_FOUND, 00075 INVALID_DEVICE, 00076 INVALID_STREAM, 00077 MEMORY_ERROR, 00078 INVALID_PARAMETER, 00079 DRIVER_ERROR, 00080 SYSTEM_ERROR, 00081 THREAD_ERROR 00082 }; 00083 00084 protected: 00085 char error_message[256]; 00086 TYPE type; 00087 00088 public: 00090 RtAudioError(const char *p, TYPE tipe = RtAudioError::UNSPECIFIED); 00091 00093 virtual ~RtAudioError(void); 00094 00096 virtual void printMessage(void); 00097 00099 virtual const TYPE& getType(void) { return type; } 00100 00102 virtual const char *getMessage(void) { return error_message; } 00103 }; 00104 00105 00106 // *************************************************** // 00107 // 00108 // RtAudio class declaration. 00109 // 00110 // *************************************************** // 00111 00112 class RtAudio 00113 { 00114 public: 00115 00116 // Support for signed integers and floats. Audio data fed to/from 00117 // the tickStream() routine is assumed to ALWAYS be in host 00118 // byte order. The internal routines will automatically take care of 00119 // any necessary byte-swapping between the host format and the 00120 // soundcard. Thus, endian-ness is not a concern in the following 00121 // format definitions. 00122 typedef unsigned long RTAUDIO_FORMAT; 00123 static const RTAUDIO_FORMAT RTAUDIO_SINT8; 00124 static const RTAUDIO_FORMAT RTAUDIO_SINT16; 00125 static const RTAUDIO_FORMAT RTAUDIO_SINT24; 00126 static const RTAUDIO_FORMAT RTAUDIO_SINT32; 00127 static const RTAUDIO_FORMAT RTAUDIO_FLOAT32; 00128 static const RTAUDIO_FORMAT RTAUDIO_FLOAT64; 00130 //static const int MAX_SAMPLE_RATES = 14; 00131 enum { MAX_SAMPLE_RATES = 14 }; 00132 00133 typedef int (*RTAUDIO_CALLBACK)(char *buffer, int bufferSize, void *userData); 00134 00135 typedef struct { 00136 char name[128]; 00137 DEVICE_ID id[2]; 00138 bool probed; 00139 int maxOutputChannels; 00140 int maxInputChannels; 00141 int maxDuplexChannels; 00142 int minOutputChannels; 00143 int minInputChannels; 00144 int minDuplexChannels; 00145 bool hasDuplexSupport; 00146 int nSampleRates; 00147 int sampleRates[MAX_SAMPLE_RATES]; 00148 RTAUDIO_FORMAT nativeFormats; 00149 } RTAUDIO_DEVICE; 00150 00152 00159 RtAudio(); 00160 00162 00173 RtAudio(int *streamID, 00174 int outputDevice, int outputChannels, 00175 int inputDevice, int inputChannels, 00176 RTAUDIO_FORMAT format, int sampleRate, 00177 int *bufferSize, int numberOfBuffers); 00178 00180 00184 ~RtAudio(); 00185 00187 00214 int openStream(int outputDevice, int outputChannels, 00215 int inputDevice, int inputChannels, 00216 RTAUDIO_FORMAT format, int sampleRate, 00217 int *bufferSize, int numberOfBuffers); 00218 00220 00239 void setStreamCallback(int streamID, RTAUDIO_CALLBACK callback, void *userData); 00240 00242 00249 void cancelStreamCallback(int streamID); 00250 00252 int getDeviceCount(void); 00253 00255 00265 void getDeviceInfo(int device, RTAUDIO_DEVICE *info); 00266 00268 00273 char * const getStreamBuffer(int streamID); 00274 00276 00281 void tickStream(int streamID); 00282 00284 00288 void closeStream(int streamID); 00289 00291 00295 void startStream(int streamID); 00296 00298 00302 void stopStream(int streamID); 00303 00305 00309 void abortStream(int streamID); 00310 00312 00317 int streamWillBlock(int streamID); 00318 00319 protected: 00320 00321 private: 00322 00323 static const unsigned int SAMPLE_RATES[MAX_SAMPLE_RATES]; 00324 00325 enum { FAILURE, SUCCESS }; 00326 00327 enum STREAM_MODE { 00328 PLAYBACK, 00329 RECORD, 00330 DUPLEX, 00331 UNINITIALIZED = -75 00332 }; 00333 00334 enum STREAM_STATE { 00335 STREAM_STOPPED, 00336 STREAM_RUNNING 00337 }; 00338 00339 typedef struct { 00340 int device[2]; // Playback and record, respectively. 00341 STREAM_MODE mode; // PLAYBACK, RECORD, or DUPLEX. 00342 AUDIO_HANDLE handle[2]; // Playback and record handles, respectively. 00343 STREAM_STATE state; // STOPPED or RUNNING 00344 char *userBuffer; 00345 char *deviceBuffer; 00346 bool doConvertBuffer[2]; // Playback and record, respectively. 00347 bool deInterleave[2]; // Playback and record, respectively. 00348 bool doByteSwap[2]; // Playback and record, respectively. 00349 int sampleRate; 00350 int bufferSize; 00351 int nBuffers; 00352 int nUserChannels[2]; // Playback and record, respectively. 00353 int nDeviceChannels[2]; // Playback and record channels, respectively. 00354 RTAUDIO_FORMAT userFormat; 00355 RTAUDIO_FORMAT deviceFormat[2]; // Playback and record, respectively. 00356 bool usingCallback; 00357 THREAD_HANDLE thread; 00358 MUTEX mutex; 00359 RTAUDIO_CALLBACK callback; 00360 void *userData; 00361 } RTAUDIO_STREAM; 00362 00363 typedef signed short INT16; 00364 typedef signed int INT32; 00365 typedef float FLOAT32; 00366 typedef double FLOAT64; 00367 00368 char message[256]; 00369 int nDevices; 00370 RTAUDIO_DEVICE *devices; 00371 00372 std::map<int, void *> streams; 00373 00375 void error(RtAudioError::TYPE type); 00376 00381 void initialize(void); 00382 00384 void clearDeviceInfo(RTAUDIO_DEVICE *info); 00385 00393 void probeDeviceInfo(RTAUDIO_DEVICE *info); 00394 00401 bool probeDeviceOpen(int device, RTAUDIO_STREAM *stream, 00402 STREAM_MODE mode, int channels, 00403 int sampleRate, RTAUDIO_FORMAT format, 00404 int *bufferSize, int numberOfBuffers); 00405 00412 void *verifyStream(int streamID); 00413 00418 void convertStreamBuffer(RTAUDIO_STREAM *stream, STREAM_MODE mode); 00419 00421 void byteSwapBuffer(char *buffer, int samples, RTAUDIO_FORMAT format); 00422 00424 int formatBytes(RTAUDIO_FORMAT format); 00425 }; 00426 00427 // Uncomment the following definition to have extra information spewed to stderr. 00428 //#define RTAUDIO_DEBUG 00429 00430 #endif
![]() | ©2001-2002 CCRMA, Stanford University. All Rights Reserved. Maintained by Gary P. Scavone, gary@ccrma.stanford.edu
|