
Home Information Classes Download Usage Mail List Requirements Tutorial
00001 /******************************************/
00002 /*
00003 RtAudio - realtime sound I/O C++ class
00004 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 #define THREAD_TYPE
00019 typedef snd_pcm_t *AUDIO_HANDLE;
00020 typedef int DEVICE_ID;
00021 typedef pthread_t THREAD_HANDLE;
00022 typedef pthread_mutex_t MUTEX;
00023
00024 #elif defined(__LINUX_OSS__)
00025 #include <pthread.h>
00026 #include <unistd.h>
00027
00028 #define THREAD_TYPE
00029 typedef int AUDIO_HANDLE;
00030 typedef int DEVICE_ID;
00031 typedef pthread_t THREAD_HANDLE;
00032 typedef pthread_mutex_t MUTEX;
00033
00034 #elif defined(__WINDOWS_DS__)
00035 #include <windows.h>
00036 #include <process.h>
00037
00038 // The following struct is used to hold the extra variables
00039 // specific to the DirectSound implementation.
00040 typedef struct {
00041 void * object;
00042 void * buffer;
00043 UINT bufferPointer;
00044 } AUDIO_HANDLE;
00045
00046 #define THREAD_TYPE __stdcall
00047 typedef LPGUID DEVICE_ID;
00048 typedef unsigned long THREAD_HANDLE;
00049 typedef CRITICAL_SECTION MUTEX;
00050
00051 #elif defined(__IRIX_AL__)
00052 #include <dmedia/audio.h>
00053 #include <pthread.h>
00054 #include <unistd.h>
00055
00056 #define THREAD_TYPE
00057 typedef ALport AUDIO_HANDLE;
00058 typedef int DEVICE_ID;
00059 typedef pthread_t THREAD_HANDLE;
00060 typedef pthread_mutex_t MUTEX;
00061
00062 #endif
00063
00064
00065 // *************************************************** //
00066 //
00067 // RtError class declaration.
00068 //
00069 // *************************************************** //
00070
00071 class RtError
00072 {
00073 public:
00074 enum TYPE {
00075 WARNING,
00076 DEBUG_WARNING,
00077 UNSPECIFIED,
00078 NO_DEVICES_FOUND,
00079 INVALID_DEVICE,
00080 INVALID_STREAM,
00081 MEMORY_ERROR,
00082 INVALID_PARAMETER,
00083 DRIVER_ERROR,
00084 SYSTEM_ERROR,
00085 THREAD_ERROR
00086 };
00087
00088 protected:
00089 char error_message[256];
00090 TYPE type;
00091
00092 public:
00094 RtError(const char *p, TYPE tipe = RtError::UNSPECIFIED);
00095
00097 virtual ~RtError(void);
00098
00100 virtual void printMessage(void);
00101
00103 virtual const TYPE& getType(void) { return type; }
00104
00106 virtual const char *getMessage(void) { return error_message; }
00107 };
00108
00109
00110 // *************************************************** //
00111 //
00112 // RtAudio class declaration.
00113 //
00114 // *************************************************** //
00115
00116 class RtAudio
00117 {
00118 public:
00119
00120 // Support for signed integers and floats. Audio data fed to/from
00121 // the tickStream() routine is assumed to ALWAYS be in host
00122 // byte order. The internal routines will automatically take care of
00123 // any necessary byte-swapping between the host format and the
00124 // soundcard. Thus, endian-ness is not a concern in the following
00125 // format definitions.
00126 typedef unsigned long RTAUDIO_FORMAT;
00127 static const RTAUDIO_FORMAT RTAUDIO_SINT8;
00128 static const RTAUDIO_FORMAT RTAUDIO_SINT16;
00129 static const RTAUDIO_FORMAT RTAUDIO_SINT24;
00130 static const RTAUDIO_FORMAT RTAUDIO_SINT32;
00131 static const RTAUDIO_FORMAT RTAUDIO_FLOAT32;
00132 static const RTAUDIO_FORMAT RTAUDIO_FLOAT64;
00133
00134 //static const int MAX_SAMPLE_RATES = 14;
00135 enum { MAX_SAMPLE_RATES = 14 };
00136
00137 typedef int (*RTAUDIO_CALLBACK)(char *buffer, int bufferSize, void *userData);
00138
00139 typedef struct {
00140 char name[128];
00141 DEVICE_ID id[2];
00142 bool probed;
00143 int maxOutputChannels;
00144 int maxInputChannels;
00145 int maxDuplexChannels;
00146 int minOutputChannels;
00147 int minInputChannels;
00148 int minDuplexChannels;
00149 bool hasDuplexSupport;
00150 int nSampleRates;
00151 int sampleRates[MAX_SAMPLE_RATES];
00152 RTAUDIO_FORMAT nativeFormats;
00153 } RTAUDIO_DEVICE;
00154
00156
00163 RtAudio();
00164
00166
00177 RtAudio(int *streamId,
00178 int outputDevice, int outputChannels,
00179 int inputDevice, int inputChannels,
00180 RTAUDIO_FORMAT format, int sampleRate,
00181 int *bufferSize, int numberOfBuffers);
00182
00184
00188 ~RtAudio();
00189
00191
00218 int openStream(int outputDevice, int outputChannels,
00219 int inputDevice, int inputChannels,
00220 RTAUDIO_FORMAT format, int sampleRate,
00221 int *bufferSize, int numberOfBuffers);
00222
00224
00243 void setStreamCallback(int streamId, RTAUDIO_CALLBACK callback, void *userData);
00244
00246
00253 void cancelStreamCallback(int streamId);
00254
00256 int getDeviceCount(void);
00257
00259
00269 void getDeviceInfo(int device, RTAUDIO_DEVICE *info);
00270
00272
00277 char * const getStreamBuffer(int streamId);
00278
00280
00285 void tickStream(int streamId);
00286
00288
00292 void closeStream(int streamId);
00293
00295
00299 void startStream(int streamId);
00300
00302
00306 void stopStream(int streamId);
00307
00309
00313 void abortStream(int streamId);
00314
00316
00321 int streamWillBlock(int streamId);
00322
00323 protected:
00324
00325 private:
00326
00327 static const unsigned int SAMPLE_RATES[MAX_SAMPLE_RATES];
00328
00329 enum { FAILURE, SUCCESS };
00330
00331 enum STREAM_MODE {
00332 PLAYBACK,
00333 RECORD,
00334 DUPLEX,
00335 UNINITIALIZED = -75
00336 };
00337
00338 enum STREAM_STATE {
00339 STREAM_STOPPED,
00340 STREAM_RUNNING
00341 };
00342
00343 typedef struct {
00344 int device[2]; // Playback and record, respectively.
00345 STREAM_MODE mode; // PLAYBACK, RECORD, or DUPLEX.
00346 AUDIO_HANDLE handle[2]; // Playback and record handles, respectively.
00347 STREAM_STATE state; // STOPPED or RUNNING
00348 char *userBuffer;
00349 char *deviceBuffer;
00350 bool doConvertBuffer[2]; // Playback and record, respectively.
00351 bool deInterleave[2]; // Playback and record, respectively.
00352 bool doByteSwap[2]; // Playback and record, respectively.
00353 int sampleRate;
00354 int bufferSize;
00355 int nBuffers;
00356 int nUserChannels[2]; // Playback and record, respectively.
00357 int nDeviceChannels[2]; // Playback and record channels, respectively.
00358 RTAUDIO_FORMAT userFormat;
00359 RTAUDIO_FORMAT deviceFormat[2]; // Playback and record, respectively.
00360 bool usingCallback;
00361 THREAD_HANDLE thread;
00362 MUTEX mutex;
00363 RTAUDIO_CALLBACK callback;
00364 void *userData;
00365 } RTAUDIO_STREAM;
00366
00367 typedef signed short INT16;
00368 typedef signed int INT32;
00369 typedef float FLOAT32;
00370 typedef double FLOAT64;
00371
00372 char message[256];
00373 int nDevices;
00374 RTAUDIO_DEVICE *devices;
00375
00376 std::map<int, void *> streams;
00377
00379 void error(RtError::TYPE type);
00380
00385 void initialize(void);
00386
00388 void clearDeviceInfo(RTAUDIO_DEVICE *info);
00389
00397 void probeDeviceInfo(RTAUDIO_DEVICE *info);
00398
00405 bool probeDeviceOpen(int device, RTAUDIO_STREAM *stream,
00406 STREAM_MODE mode, int channels,
00407 int sampleRate, RTAUDIO_FORMAT format,
00408 int *bufferSize, int numberOfBuffers);
00409
00416 void *verifyStream(int streamId);
00417
00422 void convertStreamBuffer(RTAUDIO_STREAM *stream, STREAM_MODE mode);
00423
00425 void byteSwapBuffer(char *buffer, int samples, RTAUDIO_FORMAT format);
00426
00428 int formatBytes(RTAUDIO_FORMAT format);
00429 };
00430
00431 // Uncomment the following definition to have extra information spewed to stderr.
00432 //#define RTAUDIO_DEBUG
00433
00434 #endif
| The Synthesis ToolKit in C++ (STK) |
| ©1995-2002 Perry R. Cook and Gary P. Scavone. All Rights Reserved. |