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) {}
243 virtual void printMessage(
void )
const
244 { std::cerr <<
'\n' << what() <<
"\n\n"; }
250 virtual const std::string getMessage(
void)
const
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{};
384 static std::string getVersion(
void );
392 static void getCompiledApi( std::vector<RtAudio::Api> &apis );
407 static std::string getApiDisplayName(
RtAudio::Api api );
415 static RtAudio::Api getCompiledApiByName(
const std::string &name );
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__
640 struct CallbackInfo {
642 ThreadHandle thread{};
645 void *errorCallback{};
647 bool isRunning{
false};
648 bool doRealtime{
false};
666 #pragma pack(push, 1)
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>
701 class 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 );
875 #if defined(__MACOSX_CORE__)
877 #include <CoreAudio/AudioHardware.h>
879 class 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__)
916 class 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__)
950 class 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__)
987 class 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__)
1025 struct IMMDeviceEnumerator;
1027 class 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__)
1060 class 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__)
1094 class 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__)
1125 class 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__)
1157 class 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 ,