49#define RTAUDIO_VERSION "5.2.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()); }
302 unsigned int outputChannels{};
303 unsigned int inputChannels{};
304 unsigned int duplexChannels{};
305 bool isDefaultOutput{
false};
306 bool isDefaultInput{
false};
308 unsigned int preferredSampleRate{};
314 unsigned int deviceId{};
315 unsigned int nChannels{};
316 unsigned int firstChannel{};
378 unsigned int numberOfBuffers{};
444 unsigned int getDeviceCount(
void );
467 unsigned int getDefaultOutputDevice(
void );
477 unsigned int getDefaultInputDevice(
void );
530 void closeStream(
void );
539 void startStream(
void );
548 void stopStream(
void );
557 void abortStream(
void );
560 bool isStreamOpen(
void )
const;
563 bool isStreamRunning(
void )
const;
569 double getStreamTime(
void );
575 void setStreamTime(
double time );
586 long getStreamLatency(
void );
594 unsigned int getStreamSampleRate(
void );
597 void showWarnings(
bool value =
true );
606#if defined(_WIN32) || defined(__CYGWIN__)
615 typedef uintptr_t ThreadHandle;
616 typedef CRITICAL_SECTION StreamMutex;
623 typedef pthread_t ThreadHandle;
624 typedef pthread_mutex_t StreamMutex;
629#if !(defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_WASAPI__) \
630 || defined(__LINUX_ALSA__) || defined(__LINUX_PULSE__) || defined(__UNIX_JACK__) \
631 || defined(__LINUX_OSS__) || defined(__MACOSX_CORE__))
633 #define __RTAUDIO_DUMMY__
642 ThreadHandle thread{};
645 void *errorCallback{};
647 bool isRunning{
false};
648 bool doRealtime{
false};
675 S24& operator = (
const int& i ) {
676 c3[0] = (
unsigned char)(i & 0x000000ff);
677 c3[1] = (
unsigned char)((i & 0x0000ff00) >> 8);
678 c3[2] = (
unsigned char)((i & 0x00ff0000) >> 16);
682 S24(
const double& d ) { *
this = (int) d; }
683 S24(
const float& f ) { *
this = (int) f; }
684 S24(
const signed short& s ) { *
this = (int) s; }
685 S24(
const char& c ) { *
this = (int) c; }
688 int i = c3[0] | (c3[1] << 8) | (c3[2] << 16);
689 if (i & 0x800000) i |= ~0xffffff;
695#if defined( HAVE_GETTIMEOFDAY )
696 #include <sys/time.h>
701class RTAUDIO_DLL_PUBLIC RtApi
708 virtual unsigned int getDeviceCount(
void ) = 0;
710 virtual unsigned int getDefaultInputDevice(
void );
711 virtual unsigned int getDefaultOutputDevice(
void );
718 virtual void closeStream(
void );
719 virtual void startStream(
void ) = 0;
720 virtual void stopStream(
void ) = 0;
721 virtual void abortStream(
void ) = 0;
722 long getStreamLatency(
void );
723 unsigned int getStreamSampleRate(
void );
724 virtual double getStreamTime(
void );
725 virtual void setStreamTime(
double time );
726 bool isStreamOpen(
void )
const {
return stream_.state != STREAM_CLOSED; }
727 bool isStreamRunning(
void )
const {
return stream_.state == STREAM_RUNNING; }
728 void showWarnings(
bool value ) { showWarnings_ = value; }
733 static const unsigned int MAX_SAMPLE_RATES;
734 static const unsigned int SAMPLE_RATES[];
736 enum { FAILURE, SUCCESS };
757 std::vector<int> inOffset;
758 std::vector<int> outOffset;
763 unsigned int device[2];
769 bool doConvertBuffer[2];
770 bool userInterleaved;
771 bool deviceInterleaved[2];
773 unsigned int sampleRate;
774 unsigned int bufferSize;
775 unsigned int nBuffers;
776 unsigned int nUserChannels[2];
777 unsigned int nDeviceChannels[2];
778 unsigned int channelOffset[2];
779 unsigned long latency[2];
783 CallbackInfo callbackInfo;
784 ConvertInfo convertInfo[2];
787#if defined(HAVE_GETTIMEOFDAY)
788 struct timeval lastTickTimestamp;
792 :apiHandle(0), deviceBuffer(0) { device[0] = 11111; device[1] = 11111; }
796 typedef signed short Int16;
797 typedef signed int Int32;
798 typedef float Float32;
799 typedef double Float64;
801 std::ostringstream errorStream_;
802 std::string errorText_;
805 bool firstErrorOccurred_;
814 virtual bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
815 unsigned int firstChannel,
unsigned int sampleRate,
820 void tickStreamTime(
void );
823 void clearStreamInfo();
829 void verifyStream(
void );
838 void convertBuffer(
char *outBuffer,
char *inBuffer, ConvertInfo &info );
841 void byteSwapBuffer(
char *buffer,
unsigned int samples,
RtAudioFormat format );
847 void setConvertInfo( StreamMode mode,
unsigned int firstChannel );
856inline RtAudio::Api RtAudio :: getCurrentApi(
void ) {
return rtapi_->getCurrentApi(); }
857inline unsigned int RtAudio :: getDeviceCount(
void ) {
return rtapi_->getDeviceCount(); }
858inline RtAudio::DeviceInfo RtAudio :: getDeviceInfo(
unsigned int device ) {
return rtapi_->getDeviceInfo( device ); }
859inline unsigned int RtAudio :: getDefaultInputDevice(
void ) {
return rtapi_->getDefaultInputDevice(); }
860inline unsigned int RtAudio :: getDefaultOutputDevice(
void ) {
return rtapi_->getDefaultOutputDevice(); }
861inline void RtAudio :: closeStream(
void ) {
return rtapi_->closeStream(); }
862inline void RtAudio :: startStream(
void ) {
return rtapi_->startStream(); }
863inline void RtAudio :: stopStream(
void ) {
return rtapi_->stopStream(); }
864inline void RtAudio :: abortStream(
void ) {
return rtapi_->abortStream(); }
865inline bool RtAudio :: isStreamOpen(
void )
const {
return rtapi_->isStreamOpen(); }
866inline bool RtAudio :: isStreamRunning(
void )
const {
return rtapi_->isStreamRunning(); }
867inline long RtAudio :: getStreamLatency(
void ) {
return rtapi_->getStreamLatency(); }
868inline unsigned int RtAudio :: getStreamSampleRate(
void ) {
return rtapi_->getStreamSampleRate(); }
869inline double RtAudio :: getStreamTime(
void ) {
return rtapi_->getStreamTime(); }
870inline void RtAudio :: setStreamTime(
double time ) {
return rtapi_->setStreamTime( time ); }
871inline void RtAudio :: showWarnings(
bool value ) { rtapi_->showWarnings( value ); }
875#if defined(__MACOSX_CORE__)
877#include <CoreAudio/AudioHardware.h>
879class RtApiCore:
public RtApi
886 unsigned int getDeviceCount(
void )
override;
888 unsigned int getDefaultOutputDevice(
void )
override;
889 unsigned int getDefaultInputDevice(
void )
override;
890 void closeStream(
void )
override;
891 void startStream(
void )
override;
892 void stopStream(
void )
override;
893 void abortStream(
void )
override;
899 bool callbackEvent( AudioDeviceID deviceId,
900 const AudioBufferList *inBufferList,
901 const AudioBufferList *outBufferList );
905 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
906 unsigned int firstChannel,
unsigned int sampleRate,
909 static const char* getErrorCode( OSStatus code );
914#if defined(__UNIX_JACK__)
916class RtApiJack:
public RtApi
923 unsigned int getDeviceCount(
void )
override;
925 void closeStream(
void )
override;
926 void startStream(
void )
override;
927 void stopStream(
void )
override;
928 void abortStream(
void )
override;
934 bool callbackEvent(
unsigned long nframes );
938 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
939 unsigned int firstChannel,
unsigned int sampleRate,
943 bool shouldAutoconnect_;
948#if defined(__WINDOWS_ASIO__)
950class RtApiAsio:
public RtApi
957 unsigned int getDeviceCount(
void )
override;
958 unsigned int getDefaultOutputDevice(
void )
override;
959 unsigned int getDefaultInputDevice(
void )
override;
961 void closeStream(
void )
override;
962 void startStream(
void )
override;
963 void stopStream(
void )
override;
964 void abortStream(
void )
override;
970 bool callbackEvent(
long bufferIndex );
974 std::vector<RtAudio::DeviceInfo> devices_;
975 void saveDeviceInfo(
void );
977 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
978 unsigned int firstChannel,
unsigned int sampleRate,
985#if defined(__WINDOWS_DS__)
987class RtApiDs:
public RtApi
994 unsigned int getDeviceCount(
void )
override;
995 unsigned int getDefaultOutputDevice(
void )
override;
996 unsigned int getDefaultInputDevice(
void )
override;
998 void closeStream(
void )
override;
999 void startStream(
void )
override;
1000 void stopStream(
void )
override;
1001 void abortStream(
void )
override;
1007 void callbackEvent(
void );
1011 bool coInitialized_;
1012 bool buffersRolling;
1013 long duplexPrerollBytes;
1014 std::vector<struct DsDevice> dsDevices;
1015 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
1016 unsigned int firstChannel,
unsigned int sampleRate,
1023#if defined(__WINDOWS_WASAPI__)
1025struct IMMDeviceEnumerator;
1027class RtApiWasapi :
public RtApi
1031 virtual ~RtApiWasapi();
1034 unsigned int getDeviceCount(
void )
override;
1036 void closeStream(
void )
override;
1037 void startStream(
void )
override;
1038 void stopStream(
void )
override;
1039 void abortStream(
void )
override;
1042 bool coInitialized_;
1043 IMMDeviceEnumerator* deviceEnumerator_;
1045 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
1046 unsigned int firstChannel,
unsigned int sampleRate,
1050 static DWORD WINAPI runWasapiThread(
void* wasapiPtr );
1051 static DWORD WINAPI stopWasapiThread(
void* wasapiPtr );
1052 static DWORD WINAPI abortWasapiThread(
void* wasapiPtr );
1053 void wasapiThread();
1058#if defined(__LINUX_ALSA__)
1060class RtApiAlsa:
public RtApi
1067 unsigned int getDeviceCount(
void )
override;
1069 void closeStream(
void )
override;
1070 void startStream(
void )
override;
1071 void stopStream(
void )
override;
1072 void abortStream(
void )
override;
1078 void callbackEvent(
void );
1082 std::vector<RtAudio::DeviceInfo> devices_;
1083 void saveDeviceInfo(
void );
1084 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
1085 unsigned int firstChannel,
unsigned int sampleRate,
1092#if defined(__LINUX_PULSE__)
1094class RtApiPulse:
public RtApi
1099 unsigned int getDeviceCount(
void )
override;
1101 void closeStream(
void )
override;
1102 void startStream(
void )
override;
1103 void stopStream(
void )
override;
1104 void abortStream(
void )
override;
1110 void callbackEvent(
void );
1114 void collectDeviceInfo(
void );
1115 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
1116 unsigned int firstChannel,
unsigned int sampleRate,
1123#if defined(__LINUX_OSS__)
1125class RtApiOss:
public RtApi
1132 unsigned int getDeviceCount(
void )
override;
1134 void closeStream(
void )
override;
1135 void startStream(
void )
override;
1136 void stopStream(
void )
override;
1137 void abortStream(
void )
override;
1143 void callbackEvent(
void );
1147 bool probeDeviceOpen(
unsigned int device, StreamMode mode,
unsigned int channels,
1148 unsigned int firstChannel,
unsigned int sampleRate,
1155#if defined(__RTAUDIO_DUMMY__)
1157class RtApiDummy:
public RtApi
1161 RtApiDummy() { errorText_ =
"RtApiDummy: This class provides no functionality."; error(
RtAudioError::WARNING ); }
1163 unsigned int getDeviceCount(
void )
override {
return 0; }
1165 void closeStream(
void )
override {}
1166 void startStream(
void )
override {}
1167 void stopStream(
void )
override {}
1168 void abortStream(
void )
override {}
1172 bool probeDeviceOpen(
unsigned int , StreamMode ,
unsigned int ,
1173 unsigned int ,
unsigned int ,
void(* RtAudioErrorCallback)(RtAudioError::Type type, const std::string &errorText)
RtAudio error callback function prototype.
Definition RtAudio.h:262
int(* RtAudioCallback)(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *userData)
RtAudio callback function prototype.
Definition RtAudio.h:203
unsigned long RtAudioFormat
RtAudio data format type.
Definition RtAudio.h:86
unsigned int RtAudioStreamFlags
RtAudio stream option flags.
Definition RtAudio.h:140
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition RtAudio.h:159
Exception handling class for RtAudio.
Definition RtAudio.h:220
RtAudioError(const std::string &message, Type type=RtAudioError::UNSPECIFIED)
The constructor.
Definition RtAudio.h:238
virtual const std::string getMessage(void) const
Returns the thrown error message string.
Definition RtAudio.h:250
Type
Defined RtAudioError types.
Definition RtAudio.h:223
@ INVALID_DEVICE
Definition RtAudio.h:228
@ MEMORY_ERROR
Definition RtAudio.h:229
@ WARNING
Definition RtAudio.h:224
@ UNSPECIFIED
Definition RtAudio.h:226
@ DRIVER_ERROR
Definition RtAudio.h:232
@ NO_DEVICES_FOUND
Definition RtAudio.h:227
@ INVALID_USE
Definition RtAudio.h:231
@ INVALID_PARAMETER
Definition RtAudio.h:230
@ DEBUG_WARNING
Definition RtAudio.h:225
@ SYSTEM_ERROR
Definition RtAudio.h:233
virtual const Type & getType(void) const
Returns the thrown error message type.
Definition RtAudio.h:247
virtual void printMessage(void) const
Prints thrown error message to stderr.
Definition RtAudio.h:243
Realtime audio i/o C++ classes.
Definition RtAudio.h:280
std::string name
Definition RtAudio.h:301
std::string streamName
Definition RtAudio.h:379
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.
static std::string getApiName(RtAudio::Api api)
Return the name of a specified compiled audio API.
static std::string getApiDisplayName(RtAudio::Api api)
Return the display name of a specified compiled audio API.
bool probed
Definition RtAudio.h:300
~RtAudio()
The destructor.
static RtAudio::Api getCompiledApiByName(const std::string &name)
Return the compiled audio API having the given name.
Api
Audio API specifier arguments.
Definition RtAudio.h:284
@ WINDOWS_ASIO
Definition RtAudio.h:292
@ WINDOWS_DS
Definition RtAudio.h:293
@ LINUX_OSS
Definition RtAudio.h:288
@ UNIX_JACK
Definition RtAudio.h:289
@ MACOSX_CORE
Definition RtAudio.h:290
@ UNSPECIFIED
Definition RtAudio.h:285
@ LINUX_ALSA
Definition RtAudio.h:286
@ RTAUDIO_DUMMY
Definition RtAudio.h:294
@ WINDOWS_WASAPI
Definition RtAudio.h:291
@ LINUX_PULSE
Definition RtAudio.h:287
static void getCompiledApi(std::vector< RtAudio::Api > &apis)
A static function to determine the available compiled audio APIs.
std::vector< unsigned int > sampleRates
Definition RtAudio.h:307
RtAudio(RtAudio::Api api=UNSPECIFIED)
The class constructor.
static std::string getVersion(void)
A static function to determine the current RtAudio version.
The public device information structure for returning queried values.
Definition RtAudio.h:299
The structure for specifying stream options.
Definition RtAudio.h:376
The structure for specifying input or output stream parameters.
Definition RtAudio.h:313