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) {}
397 static std::string getVersion(
void );
405 static void getCompiledApi( std::vector<RtAudio::Api> &apis );
420 static std::string getApiDisplayName(
RtAudio::Api api );
428 static RtAudio::Api getCompiledApiByName(
const std::string &name );
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 ,
Definition: RtAudio.h:228
unsigned long RtAudioFormat
RtAudio data format type.
Definition: RtAudio.h:86
RtAudioStreamFlags flags
Definition: RtAudio.h:386
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition: RtAudio.h:159
Type
Defined RtAudioError types.
Definition: RtAudio.h:223
unsigned int duplexChannels
Definition: RtAudio.h:304
unsigned int deviceId
Definition: RtAudio.h:319
Definition: RtAudio.h:227
Definition: RtAudio.h:225
Definition: RtAudio.h:230
unsigned int inputChannels
Definition: RtAudio.h:303
RtAudioError(const std::string &message, Type type=RtAudioError::UNSPECIFIED)
The constructor.
Definition: RtAudio.h:238
unsigned int numberOfBuffers
Definition: RtAudio.h:387
virtual const std::string getMessage(void) const
Returns the thrown error message string.
Definition: RtAudio.h:250
int(* RtAudioCallback)(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *userData)
RtAudio callback function prototype.
Definition: RtAudio.h:203
unsigned int firstChannel
Definition: RtAudio.h:321
Definition: RtAudio.h:291
Api
Audio API specifier arguments.
Definition: RtAudio.h:284
Definition: RtAudio.h:285
The public device information structure for returning queried values.
Definition: RtAudio.h:299
void showWarnings(bool value=true)
Specify whether warning messages should be printed to stderr.
Definition: RtAudio.h:884
void stopStream(void)
Stop a stream, allowing any samples remaining in the output queue to be played.
Definition: RtAudio.h:876
void(* RtAudioErrorCallback)(RtAudioError::Type type, const std::string &errorText)
RtAudio error callback function prototype.
Definition: RtAudio.h:262
The structure for specifying stream options.
Definition: RtAudio.h:385
Definition: RtAudio.h:292
std::string name
Definition: RtAudio.h:301
unsigned int outputChannels
Definition: RtAudio.h:302
bool isStreamOpen(void) const
Returns true if a stream is open and false if not.
Definition: RtAudio.h:878
void startStream(void)
A function that starts a stream.
Definition: RtAudio.h:875
long getStreamLatency(void)
Returns the internal stream latency in sample frames.
Definition: RtAudio.h:880
Definition: RtAudio.h:286
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
bool isDefaultOutput
Definition: RtAudio.h:305
virtual void printMessage(void) const
Prints thrown error message to stderr.
Definition: RtAudio.h:243
Definition: RtAudio.h:232
Definition: RtAudio.h:290
Definition: RtAudio.h:231
Definition: RtAudio.h:287
Exception handling class for RtAudio.
Definition: RtAudio.h:219
std::vector< unsigned int > sampleRates
Definition: RtAudio.h:307
unsigned int preferredSampleRate
Definition: RtAudio.h:308
double getStreamTime(void)
Returns the number of elapsed seconds since the stream was started.
Definition: RtAudio.h:882
Definition: RtAudio.h:229
virtual const Type & getType(void) const
Returns the thrown error message type.
Definition: RtAudio.h:247
int priority
Definition: RtAudio.h:389
unsigned int nChannels
Definition: RtAudio.h:320
RtAudio::Api getCurrentApi(void)
Returns the audio API specifier for the current instance of RtAudio.
Definition: RtAudio.h:869
The structure for specifying input or ouput stream parameters.
Definition: RtAudio.h:318
Definition: RtAudio.h:226
void abortStream(void)
Stop a stream, discarding any samples remaining in the input/output queue.
Definition: RtAudio.h:877
Definition: RtAudio.h:233
RtAudio::DeviceInfo getDeviceInfo(unsigned int device)
Return an RtAudio::DeviceInfo structure for a specified device number.
Definition: RtAudio.h:871
bool probed
Definition: RtAudio.h:300
Definition: RtAudio.h:293
Definition: RtAudio.h:294
void closeStream(void)
A function that closes a stream and frees any associated stream memory.
Definition: RtAudio.h:874
Definition: RtAudio.h:288
bool isStreamRunning(void) const
Returns true if the stream is running and false if it is stopped or not open.
Definition: RtAudio.h:879
unsigned int RtAudioStreamFlags
RtAudio stream option flags.
Definition: RtAudio.h:140
Definition: RtAudio.h:224
std::string streamName
Definition: RtAudio.h:388
void setStreamTime(double time)
Set the stream time to a time in seconds greater than or equal to 0.0.
Definition: RtAudio.h:883
bool isDefaultInput
Definition: RtAudio.h:306
Realtime audio i/o C++ classes.
Definition: RtAudio.h:279
unsigned int getDefaultOutputDevice(void)
A function that returns the index of the default output device.
Definition: RtAudio.h:873
Definition: RtAudio.h:289
unsigned int getDeviceCount(void)
A public function that queries for the number of audio devices available.
Definition: RtAudio.h:870
unsigned int getStreamSampleRate(void)
Returns actual sample rate in use by the stream.
Definition: RtAudio.h:881