 
    
   
Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial
Go to the documentation of this file.
   49 #define RTAUDIO_VERSION "5.1.0" 
   51 #if defined _WIN32 || defined __CYGWIN__ 
   52   #if defined(RTAUDIO_EXPORT) 
   53     #define RTAUDIO_DLL_PUBLIC __declspec(dllexport) 
   55     #define RTAUDIO_DLL_PUBLIC 
   59     #define RTAUDIO_DLL_PUBLIC __attribute__( (visibility( "default" )) ) 
   61     #define RTAUDIO_DLL_PUBLIC 
  204                                 unsigned int nFrames,
 
  240     : std::runtime_error(message), type_(type) {}
 
  244     { std::cerr << 
'\n' << what() << 
"\n\n"; }
 
  251     { 
return std::string(what()); }
 
  313       :probed(false), outputChannels(0), inputChannels(0), duplexChannels(0),
 
  314        isDefaultOutput(false), isDefaultInput(false), preferredSampleRate(0), nativeFormats(0) {}
 
  325       : deviceId(0), nChannels(0), firstChannel(0) {}
 
  393     : flags(0), numberOfBuffers(0), priority(0) {}
 
  457   unsigned int getDeviceCount( 
void );
 
  480   unsigned int getDefaultOutputDevice( 
void );
 
  490   unsigned int getDefaultInputDevice( 
void );
 
  543   void closeStream( 
void );
 
  552   void startStream( 
void );
 
  561   void stopStream( 
void );
 
  570   void abortStream( 
void );
 
  573   bool isStreamOpen( 
void ) 
const;
 
  576   bool isStreamRunning( 
void ) 
const;
 
  582   double getStreamTime( 
void );
 
  588   void setStreamTime( 
double time );
 
  599   long getStreamLatency( 
void );
 
  607   unsigned int getStreamSampleRate( 
void );
 
  610   void showWarnings( 
bool value = 
true );
 
  619 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_WASAPI__) 
  628   typedef uintptr_t ThreadHandle;
 
  629   typedef CRITICAL_SECTION StreamMutex;
 
  631 #elif defined(__LINUX_ALSA__) || defined(__LINUX_PULSE__) || defined(__UNIX_JACK__) || defined(__LINUX_OSS__) || defined(__MACOSX_CORE__) 
  635   typedef pthread_t ThreadHandle;
 
  636   typedef pthread_mutex_t StreamMutex;
 
  638 #else // Setup for "dummy" behavior 
  640   #define __RTAUDIO_DUMMY__ 
  641   typedef int ThreadHandle;
 
  642   typedef int StreamMutex;
 
  649 struct CallbackInfo {
 
  662   :object(0), callback(0), userData(0), errorCallback(0), apiInfo(0), isRunning(false), doRealtime(false), priority(0) {}
 
  679 #pragma pack(push, 1) 
  688   S24& operator = ( 
const int& i ) {
 
  689     c3[0] = (i & 0x000000ff);
 
  690     c3[1] = (i & 0x0000ff00) >> 8;
 
  691     c3[2] = (i & 0x00ff0000) >> 16;
 
  695   S24( 
const double& d ) { *
this = (int) d; }
 
  696   S24( 
const float& f ) { *
this = (int) f; }
 
  697   S24( 
const signed short& s ) { *
this = (int) s; }
 
  698   S24( 
const char& c ) { *
this = (int) c; }
 
  701     int i = c3[0] | (c3[1] << 8) | (c3[2] << 16);
 
  702     if (i & 0x800000) i |= ~0xffffff;
 
  708 #if defined( HAVE_GETTIMEOFDAY ) 
  709   #include <sys/time.h> 
  714 class RTAUDIO_DLL_PUBLIC RtApi
 
  721   virtual unsigned int getDeviceCount( 
void ) = 0;
 
  723   virtual unsigned int getDefaultInputDevice( 
void );
 
  724   virtual unsigned int getDefaultOutputDevice( 
void );
 
  731   virtual void closeStream( 
void );
 
  732   virtual void startStream( 
void ) = 0;
 
  733   virtual void stopStream( 
void ) = 0;
 
  734   virtual void abortStream( 
void ) = 0;
 
  735   long getStreamLatency( 
void );
 
  736   unsigned int getStreamSampleRate( 
void );
 
  737   virtual double getStreamTime( 
void );
 
  738   virtual void setStreamTime( 
double time );
 
  739   bool isStreamOpen( 
void )
 const { 
return stream_.state != STREAM_CLOSED; }
 
  740   bool isStreamRunning( 
void )
 const { 
return stream_.state == STREAM_RUNNING; }
 
  741   void showWarnings( 
bool value ) { showWarnings_ = value; }
 
  746   static const unsigned int MAX_SAMPLE_RATES;
 
  747   static const unsigned int SAMPLE_RATES[];
 
  749   enum { FAILURE, SUCCESS };
 
  770     std::vector<int> inOffset;
 
  771     std::vector<int> outOffset;
 
  776     unsigned int device[2];    
 
  782     bool doConvertBuffer[2];   
 
  783     bool userInterleaved;
 
  784     bool deviceInterleaved[2]; 
 
  786     unsigned int sampleRate;
 
  787     unsigned int bufferSize;
 
  788     unsigned int nBuffers;
 
  789     unsigned int nUserChannels[2];    
 
  790     unsigned int nDeviceChannels[2];  
 
  791     unsigned int channelOffset[2];    
 
  792     unsigned long latency[2];         
 
  796     CallbackInfo callbackInfo;
 
  797     ConvertInfo convertInfo[2];
 
  800 #if defined(HAVE_GETTIMEOFDAY) 
  801     struct timeval lastTickTimestamp;
 
  805       :apiHandle(0), deviceBuffer(0) { device[0] = 11111; device[1] = 11111; }
 
  809   typedef signed short Int16;
 
  810   typedef signed int Int32;
 
  811   typedef float Float32;
 
  812   typedef double Float64;
 
  814   std::ostringstream errorStream_;
 
  815   std::string errorText_;
 
  818   bool firstErrorOccurred_;
 
  827   virtual bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels, 
 
  828                                 unsigned int firstChannel, 
unsigned int sampleRate,
 
  833   void tickStreamTime( 
void );
 
  836   void clearStreamInfo();
 
  842   void verifyStream( 
void );
 
  851   void convertBuffer( 
char *outBuffer, 
char *inBuffer, ConvertInfo &info );
 
  854   void byteSwapBuffer( 
char *buffer, 
unsigned int samples, 
RtAudioFormat format );
 
  860   void setConvertInfo( StreamMode mode, 
unsigned int firstChannel );
 
  888 #if defined(__MACOSX_CORE__) 
  890 #include <CoreAudio/AudioHardware.h> 
  892 class RtApiCore: 
public RtApi
 
  899   unsigned int getDeviceCount( 
void );
 
  901   unsigned int getDefaultOutputDevice( 
void );
 
  902   unsigned int getDefaultInputDevice( 
void );
 
  903   void closeStream( 
void );
 
  904   void startStream( 
void );
 
  905   void stopStream( 
void );
 
  906   void abortStream( 
void );
 
  912   bool callbackEvent( AudioDeviceID deviceId,
 
  913                       const AudioBufferList *inBufferList,
 
  914                       const AudioBufferList *outBufferList );
 
  918   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels, 
 
  919                         unsigned int firstChannel, 
unsigned int sampleRate,
 
  922   static const char* getErrorCode( OSStatus code );
 
  927 #if defined(__UNIX_JACK__) 
  929 class RtApiJack: 
public RtApi
 
  936   unsigned int getDeviceCount( 
void );
 
  938   void closeStream( 
void );
 
  939   void startStream( 
void );
 
  940   void stopStream( 
void );
 
  941   void abortStream( 
void );
 
  947   bool callbackEvent( 
unsigned long nframes );
 
  951   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels, 
 
  952                         unsigned int firstChannel, 
unsigned int sampleRate,
 
  956   bool shouldAutoconnect_;
 
  961 #if defined(__WINDOWS_ASIO__) 
  963 class RtApiAsio: 
public RtApi
 
  970   unsigned int getDeviceCount( 
void );
 
  972   void closeStream( 
void );
 
  973   void startStream( 
void );
 
  974   void stopStream( 
void );
 
  975   void abortStream( 
void );
 
  981   bool callbackEvent( 
long bufferIndex );
 
  985   std::vector<RtAudio::DeviceInfo> devices_;
 
  986   void saveDeviceInfo( 
void );
 
  988   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels, 
 
  989                         unsigned int firstChannel, 
unsigned int sampleRate,
 
  996 #if defined(__WINDOWS_DS__) 
  998 class RtApiDs: 
public RtApi
 
 1005   unsigned int getDeviceCount( 
void );
 
 1006   unsigned int getDefaultOutputDevice( 
void );
 
 1007   unsigned int getDefaultInputDevice( 
void );
 
 1009   void closeStream( 
void );
 
 1010   void startStream( 
void );
 
 1011   void stopStream( 
void );
 
 1012   void abortStream( 
void );
 
 1018   void callbackEvent( 
void );
 
 1022   bool coInitialized_;
 
 1023   bool buffersRolling;
 
 1024   long duplexPrerollBytes;
 
 1025   std::vector<struct DsDevice> dsDevices;
 
 1026   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels, 
 
 1027                         unsigned int firstChannel, 
unsigned int sampleRate,
 
 1034 #if defined(__WINDOWS_WASAPI__) 
 1036 struct IMMDeviceEnumerator;
 
 1038 class RtApiWasapi : 
public RtApi
 
 1042   virtual ~RtApiWasapi();
 
 1045   unsigned int getDeviceCount( 
void );
 
 1047   unsigned int getDefaultOutputDevice( 
void );
 
 1048   unsigned int getDefaultInputDevice( 
void );
 
 1049   void closeStream( 
void );
 
 1050   void startStream( 
void );
 
 1051   void stopStream( 
void );
 
 1052   void abortStream( 
void );
 
 1055   bool coInitialized_;
 
 1056   IMMDeviceEnumerator* deviceEnumerator_;
 
 1058   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels,
 
 1059                         unsigned int firstChannel, 
unsigned int sampleRate,
 
 1063   static DWORD WINAPI runWasapiThread( 
void* wasapiPtr );
 
 1064   static DWORD WINAPI stopWasapiThread( 
void* wasapiPtr );
 
 1065   static DWORD WINAPI abortWasapiThread( 
void* wasapiPtr );
 
 1066   void wasapiThread();
 
 1071 #if defined(__LINUX_ALSA__) 
 1073 class RtApiAlsa: 
public RtApi
 
 1080   unsigned int getDeviceCount( 
void );
 
 1082   void closeStream( 
void );
 
 1083   void startStream( 
void );
 
 1084   void stopStream( 
void );
 
 1085   void abortStream( 
void );
 
 1091   void callbackEvent( 
void );
 
 1095   std::vector<RtAudio::DeviceInfo> devices_;
 
 1096   void saveDeviceInfo( 
void );
 
 1097   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels, 
 
 1098                         unsigned int firstChannel, 
unsigned int sampleRate,
 
 1105 #if defined(__LINUX_PULSE__) 
 1107 class RtApiPulse: 
public RtApi
 
 1112   unsigned int getDeviceCount( 
void );
 
 1114   void closeStream( 
void );
 
 1115   void startStream( 
void );
 
 1116   void stopStream( 
void );
 
 1117   void abortStream( 
void );
 
 1123   void callbackEvent( 
void );
 
 1127   std::vector<RtAudio::DeviceInfo> devices_;
 
 1128   void saveDeviceInfo( 
void );
 
 1129   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels,
 
 1130                         unsigned int firstChannel, 
unsigned int sampleRate,
 
 1137 #if defined(__LINUX_OSS__) 
 1139 class RtApiOss: 
public RtApi
 
 1146   unsigned int getDeviceCount( 
void );
 
 1148   void closeStream( 
void );
 
 1149   void startStream( 
void );
 
 1150   void stopStream( 
void );
 
 1151   void abortStream( 
void );
 
 1157   void callbackEvent( 
void );
 
 1161   bool probeDeviceOpen( 
unsigned int device, StreamMode mode, 
unsigned int channels, 
 
 1162                         unsigned int firstChannel, 
unsigned int sampleRate,
 
 1169 #if defined(__RTAUDIO_DUMMY__) 
 1171 class RtApiDummy: 
public RtApi
 
 1175   RtApiDummy() { errorText_ = 
"RtApiDummy: This class provides no functionality."; error( 
RtAudioError::WARNING ); }
 
 1177   unsigned int getDeviceCount( 
void ) { 
return 0; }
 
 1179   void closeStream( 
void ) {}
 
 1180   void startStream( 
void ) {}
 
 1181   void stopStream( 
void ) {}
 
 1182   void abortStream( 
void ) {}
 
 1186   bool probeDeviceOpen( 
unsigned int , StreamMode , 
unsigned int , 
 
 1187                         unsigned int , 
unsigned int ,
 
  
std::vector< unsigned int > sampleRates
Definition: RtAudio.h:307
virtual const std::string getMessage(void) const
Returns the thrown error message string.
Definition: RtAudio.h:250
The public device information structure for returning queried values.
Definition: RtAudio.h:299
@ UNSPECIFIED
Definition: RtAudio.h:285
unsigned int getDeviceCount(void)
A public function that queries for the number of audio devices available.
Definition: RtAudio.h:870
unsigned long RtAudioFormat
RtAudio data format type.
Definition: RtAudio.h:86
bool isStreamOpen(void) const
Returns true if a stream is open and false if not.
Definition: RtAudio.h:878
Exception handling class for RtAudio.
Definition: RtAudio.h:220
void openStream(RtAudio::StreamParameters *outputParameters, RtAudio::StreamParameters *inputParameters, RtAudioFormat format, unsigned int sampleRate, unsigned int *bufferFrames, RtAudioCallback callback, void *userData=NULL, RtAudio::StreamOptions *options=NULL, RtAudioErrorCallback errorCallback=NULL)
A public function for opening a stream with the specified parameters.
@ UNSPECIFIED
Definition: RtAudio.h:226
static std::string getVersion(void)
A static function to determine the current RtAudio version.
static std::string getApiDisplayName(RtAudio::Api api)
Return the display name of a specified compiled audio API.
@ INVALID_PARAMETER
Definition: RtAudio.h:230
bool probed
Definition: RtAudio.h:300
@ WINDOWS_DS
Definition: RtAudio.h:293
int(* RtAudioCallback)(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *userData)
RtAudio callback function prototype.
Definition: RtAudio.h:203
@ LINUX_ALSA
Definition: RtAudio.h:286
bool isStreamRunning(void) const
Returns true if the stream is running and false if it is stopped or not open.
Definition: RtAudio.h:879
virtual void printMessage(void) const
Prints thrown error message to stderr.
Definition: RtAudio.h:243
unsigned int firstChannel
Definition: RtAudio.h:321
unsigned int getDefaultOutputDevice(void)
A function that returns the index of the default output device.
Definition: RtAudio.h:873
static RtAudio::Api getCompiledApiByName(const std::string &name)
Return the compiled audio API having the given name.
void showWarnings(bool value=true)
Specify whether warning messages should be printed to stderr.
Definition: RtAudio.h:884
@ WINDOWS_WASAPI
Definition: RtAudio.h:291
void stopStream(void)
Stop a stream, allowing any samples remaining in the output queue to be played.
Definition: RtAudio.h:876
@ DRIVER_ERROR
Definition: RtAudio.h:232
virtual const Type & getType(void) const
Returns the thrown error message type.
Definition: RtAudio.h:247
double getStreamTime(void)
Returns the number of elapsed seconds since the stream was started.
Definition: RtAudio.h:882
void(* RtAudioErrorCallback)(RtAudioError::Type type, const std::string &errorText)
RtAudio error callback function prototype.
Definition: RtAudio.h:262
@ UNIX_JACK
Definition: RtAudio.h:289
@ RTAUDIO_DUMMY
Definition: RtAudio.h:294
RtAudio::Api getCurrentApi(void)
Returns the audio API specifier for the current instance of RtAudio.
Definition: RtAudio.h:869
@ WINDOWS_ASIO
Definition: RtAudio.h:292
void closeStream(void)
A function that closes a stream and frees any associated stream memory.
Definition: RtAudio.h:874
RtAudio(RtAudio::Api api=UNSPECIFIED)
The class constructor.
bool isDefaultInput
Definition: RtAudio.h:306
@ LINUX_OSS
Definition: RtAudio.h:288
unsigned int duplexChannels
Definition: RtAudio.h:304
static void getCompiledApi(std::vector< RtAudio::Api > &apis)
A static function to determine the available compiled audio APIs.
unsigned int nChannels
Definition: RtAudio.h:320
int priority
Definition: RtAudio.h:389
@ DEBUG_WARNING
Definition: RtAudio.h:225
@ WARNING
Definition: RtAudio.h:224
@ SYSTEM_ERROR
Definition: RtAudio.h:233
@ MEMORY_ERROR
Definition: RtAudio.h:229
unsigned int inputChannels
Definition: RtAudio.h:303
RtAudioStreamFlags flags
Definition: RtAudio.h:386
RtAudio::DeviceInfo getDeviceInfo(unsigned int device)
Return an RtAudio::DeviceInfo structure for a specified device number.
Definition: RtAudio.h:871
void startStream(void)
A function that starts a stream.
Definition: RtAudio.h:875
std::string streamName
Definition: RtAudio.h:388
Api
Audio API specifier arguments.
Definition: RtAudio.h:284
unsigned int RtAudioStreamFlags
RtAudio stream option flags.
Definition: RtAudio.h:140
@ NO_DEVICES_FOUND
Definition: RtAudio.h:227
long getStreamLatency(void)
Returns the internal stream latency in sample frames.
Definition: RtAudio.h:880
~RtAudio()
The destructor.
RtAudioFormat nativeFormats
Definition: RtAudio.h:309
unsigned int getDefaultInputDevice(void)
A function that returns the index of the default input device.
Definition: RtAudio.h:872
std::string name
Definition: RtAudio.h:301
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition: RtAudio.h:159
Realtime audio i/o C++ classes.
Definition: RtAudio.h:280
The structure for specifying input or ouput stream parameters.
Definition: RtAudio.h:318
void setStreamTime(double time)
Set the stream time to a time in seconds greater than or equal to 0.0.
Definition: RtAudio.h:883
Type
Defined RtAudioError types.
Definition: RtAudio.h:223
unsigned int outputChannels
Definition: RtAudio.h:302
@ INVALID_DEVICE
Definition: RtAudio.h:228
@ MACOSX_CORE
Definition: RtAudio.h:290
unsigned int deviceId
Definition: RtAudio.h:319
static std::string getApiName(RtAudio::Api api)
Return the name of a specified compiled audio API.
The structure for specifying stream options.
Definition: RtAudio.h:385
unsigned int getStreamSampleRate(void)
Returns actual sample rate in use by the stream.
Definition: RtAudio.h:881
@ LINUX_PULSE
Definition: RtAudio.h:287
bool isDefaultOutput
Definition: RtAudio.h:305
RtAudioError(const std::string &message, Type type=RtAudioError::UNSPECIFIED)
The constructor.
Definition: RtAudio.h:238
@ INVALID_USE
Definition: RtAudio.h:231
unsigned int preferredSampleRate
Definition: RtAudio.h:308
unsigned int numberOfBuffers
Definition: RtAudio.h:387
void abortStream(void)
Stop a stream, discarding any samples remaining in the input/output queue.
Definition: RtAudio.h:877