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;
640 #define __RTAUDIO_DUMMY__
641 typedef int ThreadHandle;
642 typedef int StreamMutex;
662 :object(0), callback(0), userData(0), errorCallback(0), apiInfo(0), isRunning(false), doRealtime(false), priority(0) {}
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>
714class 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 );
869inline RtAudio::Api RtAudio :: getCurrentApi(
void ) {
return rtapi_->getCurrentApi(); }
870inline unsigned int RtAudio :: getDeviceCount(
void ) {
return rtapi_->getDeviceCount(); }
871inline RtAudio::DeviceInfo RtAudio :: getDeviceInfo(
unsigned int device ) {
return rtapi_->getDeviceInfo( device ); }
872inline unsigned int RtAudio :: getDefaultInputDevice(
void ) {
return rtapi_->getDefaultInputDevice(); }
873inline unsigned int RtAudio :: getDefaultOutputDevice(
void ) {
return rtapi_->getDefaultOutputDevice(); }
874inline void RtAudio :: closeStream(
void ) {
return rtapi_->closeStream(); }
875inline void RtAudio :: startStream(
void ) {
return rtapi_->startStream(); }
876inline void RtAudio :: stopStream(
void ) {
return rtapi_->stopStream(); }
877inline void RtAudio :: abortStream(
void ) {
return rtapi_->abortStream(); }
878inline bool RtAudio :: isStreamOpen(
void )
const {
return rtapi_->isStreamOpen(); }
879inline bool RtAudio :: isStreamRunning(
void )
const {
return rtapi_->isStreamRunning(); }
880inline long RtAudio :: getStreamLatency(
void ) {
return rtapi_->getStreamLatency(); }
881inline unsigned int RtAudio :: getStreamSampleRate(
void ) {
return rtapi_->getStreamSampleRate(); }
882inline double RtAudio :: getStreamTime(
void ) {
return rtapi_->getStreamTime(); }
883inline void RtAudio :: setStreamTime(
double time ) {
return rtapi_->setStreamTime( time ); }
884inline void RtAudio :: showWarnings(
bool value ) { rtapi_->showWarnings( value ); }
888#if defined(__MACOSX_CORE__)
890#include <CoreAudio/AudioHardware.h>
892class 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__)
929class 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__)
963class 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__)
998class 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__)
1036struct IMMDeviceEnumerator;
1038class 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__)
1073class 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__)
1107class 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__)
1139class 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__)
1171class 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 ,
void(* RtAudioErrorCallback)(RtAudioError::Type type, const std::string &errorText)
RtAudio error callback function prototype.
Definition RtAudio.h:262
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
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
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
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.
~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.
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
RtAudioFormat nativeFormats
Definition RtAudio.h:309
std::string name
Definition RtAudio.h:301
unsigned int duplexChannels
Definition RtAudio.h:304
bool isDefaultOutput
Definition RtAudio.h:305
unsigned int inputChannels
Definition RtAudio.h:303
bool isDefaultInput
Definition RtAudio.h:306
unsigned int outputChannels
Definition RtAudio.h:302
bool probed
Definition RtAudio.h:300
unsigned int preferredSampleRate
Definition RtAudio.h:308
std::vector< unsigned int > sampleRates
Definition RtAudio.h:307
The structure for specifying stream options.
Definition RtAudio.h:385
RtAudioStreamFlags flags
Definition RtAudio.h:386
std::string streamName
Definition RtAudio.h:388
unsigned int numberOfBuffers
Definition RtAudio.h:387
int priority
Definition RtAudio.h:389
The structure for specifying input or ouput stream parameters.
Definition RtAudio.h:318
unsigned int nChannels
Definition RtAudio.h:320
unsigned int deviceId
Definition RtAudio.h:319
unsigned int firstChannel
Definition RtAudio.h:321