Home   Class/Enum List   File List   Compound Members   C interface  

RtAudio.h
Go to the documentation of this file.
1/************************************************************************/
40/************************************************************************/
41
45
46#ifndef __RTAUDIO_H
47#define __RTAUDIO_H
48
49#define RTAUDIO_VERSION_MAJOR 6
50#define RTAUDIO_VERSION_MINOR 0
51#define RTAUDIO_VERSION_PATCH 1
52#define RTAUDIO_VERSION_BETA 0
53
54#define RTAUDIO_TOSTRING2(n) #n
55#define RTAUDIO_TOSTRING(n) RTAUDIO_TOSTRING2(n)
56
57#if RTAUDIO_VERSION_BETA > 0
58 #define RTAUDIO_VERSION RTAUDIO_TOSTRING(RTAUDIO_VERSION_MAJOR) \
59 "." RTAUDIO_TOSTRING(RTAUDIO_VERSION_MINOR) \
60 "." RTAUDIO_TOSTRING(RTAUDIO_VERSION_PATCH) \
61 "beta" RTAUDIO_TOSTRING(RTAUDIO_VERSION_BETA)
62#else
63 #define RTAUDIO_VERSION RTAUDIO_TOSTRING(RTAUDIO_VERSION_MAJOR) \
64 "." RTAUDIO_TOSTRING(RTAUDIO_VERSION_MINOR) \
65 "." RTAUDIO_TOSTRING(RTAUDIO_VERSION_PATCH)
66#endif
67
68#if defined _WIN32 || defined __CYGWIN__
69 #if defined(RTAUDIO_EXPORT)
70 #define RTAUDIO_DLL_PUBLIC __declspec(dllexport)
71 #else
72 #define RTAUDIO_DLL_PUBLIC
73 #endif
74#else
75 #if __GNUC__ >= 4
76 #define RTAUDIO_DLL_PUBLIC __attribute__( (visibility( "default" )) )
77 #else
78 #define RTAUDIO_DLL_PUBLIC
79 #endif
80#endif
81
82#include <string>
83#include <vector>
84#include <iostream>
85#include <functional>
86#include <memory>
87
88namespace rt {
89namespace audio {
90
109typedef unsigned long RtAudioFormat;
110static const RtAudioFormat RTAUDIO_SINT8 = 0x1; // 8-bit signed integer.
111static const RtAudioFormat RTAUDIO_SINT16 = 0x2; // 16-bit signed integer.
112static const RtAudioFormat RTAUDIO_SINT24 = 0x4; // 24-bit signed integer.
113static const RtAudioFormat RTAUDIO_SINT32 = 0x8; // 32-bit signed integer.
114static const RtAudioFormat RTAUDIO_FLOAT32 = 0x10; // Normalized between plus/minus 1.0.
115static const RtAudioFormat RTAUDIO_FLOAT64 = 0x20; // Normalized between plus/minus 1.0.
116
163typedef unsigned int RtAudioStreamFlags;
164static const RtAudioStreamFlags RTAUDIO_NONINTERLEAVED = 0x1; // Use non-interleaved buffers (default = interleaved).
165static const RtAudioStreamFlags RTAUDIO_MINIMIZE_LATENCY = 0x2; // Attempt to set stream parameters for lowest possible latency.
166static const RtAudioStreamFlags RTAUDIO_HOG_DEVICE = 0x4; // Attempt grab device and prevent use by others.
167static const RtAudioStreamFlags RTAUDIO_SCHEDULE_REALTIME = 0x8; // Try to select realtime scheduling for callback thread.
168static const RtAudioStreamFlags RTAUDIO_ALSA_USE_DEFAULT = 0x10; // Use the "default" PCM device (ALSA only).
169static const RtAudioStreamFlags RTAUDIO_JACK_DONT_CONNECT = 0x20; // Do not automatically connect ports (JACK only).
170
182typedef unsigned int RtAudioStreamStatus;
183static const RtAudioStreamStatus RTAUDIO_INPUT_OVERFLOW = 0x1; // Input data was discarded because of an overflow condition at the driver.
184static const RtAudioStreamStatus RTAUDIO_OUTPUT_UNDERFLOW = 0x2; // The output buffer ran low, likely causing a gap in the output sound.
185
187
226
227typedef std::function<int(void* outputBuffer, void* inputBuffer,
228 unsigned int nFrames,
229 double streamTime,
230 RtAudioStreamStatus status,
231 void* userData)> RtAudioCallback;
232
247
249
253typedef std::function<void(RtAudioErrorType type,
254 const std::string &errorText )>
256
257// **************************************************************** //
258//
259// RtAudio class declaration.
260//
261// RtAudio is a "controller" used to select an available audio i/o
262// interface. It presents a common API for the user to call but all
263// functionality is implemented by the class RtApi and its
264// subclasses. RtAudio creates an instance of an RtApi subclass
265// based on the user's API choice. If no choice is made, RtAudio
266// attempts to make a "logical" API selection.
267//
268// **************************************************************** //
269
270class RtApi;
271
272class RTAUDIO_DLL_PUBLIC RtAudio
273{
274 public:
275
277 enum Api {
278 UNSPECIFIED,
279 MACOSX_CORE,
280 LINUX_ALSA,
281 UNIX_JACK,
282 LINUX_PULSE,
283 LINUX_OSS,
284 WINDOWS_ASIO,
285 WINDOWS_WASAPI,
286 WINDOWS_DS,
287 RTAUDIO_DUMMY,
288 NUM_APIS
289 };
290
292 struct DeviceInfo {
293 unsigned int ID{};
294 std::string name;
295 unsigned int outputChannels{};
296 unsigned int inputChannels{};
297 unsigned int duplexChannels{};
298 bool isDefaultOutput{false};
299 bool isDefaultInput{false};
300 std::vector<unsigned int> sampleRates;
301 unsigned int currentSampleRate{};
302 unsigned int preferredSampleRate{};
304 };
305
308 //std::string deviceName{}; /*!< Device name from device list. */
309 unsigned int deviceId{};
310 unsigned int nChannels{};
311 unsigned int firstChannel{};
312 };
313
315
375 unsigned int numberOfBuffers{};
376 std::string streamName;
377 int priority{};
378 };
379
381 static std::string getVersion( void );
382
384
389 static void getCompiledApi( std::vector<RtAudio::Api> &apis );
390
392
397 static std::string getApiName( RtAudio::Api api );
398
400
404 static std::string getApiDisplayName( RtAudio::Api api );
405
407
412 static RtAudio::Api getCompiledApiByName( const std::string &name );
413
415
420 static RtAudio::Api getCompiledApiByDisplayName( const std::string &name );
421
423
438 RtAudio( RtAudio::Api api=UNSPECIFIED, RtAudioErrorCallback&& errorCallback=0 );
439
441
445 ~RtAudio() { }
446
448 RtAudio::Api getCurrentApi( void );
449
451
457 unsigned int getDeviceCount( void );
458
460
470 std::vector<unsigned int> getDeviceIds( void );
471
473
480 std::vector<std::string> getDeviceNames( void );
481
483
495 RtAudio::DeviceInfo getDeviceInfo( unsigned int deviceId );
496
498
504 unsigned int getDefaultOutputDevice( void );
505
507
513 unsigned int getDefaultInputDevice( void );
514
516
553 RtAudioErrorType openStream( RtAudio::StreamParameters *outputParameters,
554 RtAudio::StreamParameters *inputParameters,
555 RtAudioFormat format, unsigned int sampleRate,
556 unsigned int *bufferFrames, RtAudioCallback callback,
557 void *userData = NULL, RtAudio::StreamOptions *options = NULL );
558
560
565 void closeStream( void );
566
568
573 RtAudioErrorType startStream( void );
574
576
581 RtAudioErrorType stopStream( void );
582
584
589 RtAudioErrorType abortStream( void );
590
592
597 const std::string &getErrorText(void);
598
600 bool isStreamOpen( void ) const;
601
603 bool isStreamRunning( void ) const;
604
606
613 double getStreamTime( void );
614
616 void setStreamTime( double time );
617
619
627 long getStreamLatency( void );
628
630
635 unsigned int getStreamSampleRate( void );
636
638 void setErrorCallback( RtAudioErrorCallback errorCallback );
639
641
646 void showWarnings( bool value = true );
647
648 protected:
649
650 void openRtApi( RtAudio::Api api );
651 std::shared_ptr<RtApi> rtapi_;
652};
653
654} // namespace audio
655} // namespace rt
656
657// Operating system dependent thread functionality.
658#if defined(_MSC_VER)
659
660 #ifndef NOMINMAX
661 #define NOMINMAX
662 #endif
663 #include <windows.h>
664 #include <process.h>
665 #include <stdint.h>
666
667namespace rt {
668namespace audio {
669 typedef uintptr_t ThreadHandle;
670 typedef CRITICAL_SECTION StreamMutex;
671} // namespace audio
672} // namespace rt
673
674#else
675
676 // Using pthread library for various flavors of unix.
677 #include <pthread.h>
678
679namespace rt {
680namespace audio {
681 typedef pthread_t ThreadHandle;
682 typedef pthread_mutex_t StreamMutex;
683} // namespace audio
684} // namespace rt
685
686#endif
687
688// Setup for "dummy" behavior if no apis specified.
689#if !(defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_WASAPI__) \
690 || defined(__LINUX_ALSA__) || defined(__LINUX_PULSE__) || defined(__UNIX_JACK__) \
691 || defined(__LINUX_OSS__) || defined(__MACOSX_CORE__))
692
693 #define __RTAUDIO_DUMMY__
694
695#endif
696
697namespace rt {
698namespace audio {
699
700// This global structure type is used to pass callback information
701// between the private RtAudio stream structure and global callback
702// handling functions.
703struct CallbackInfo {
704 void *object{}; // Used as a "this" pointer.
705 ThreadHandle thread{};
706 RtAudioCallback callback{};
707 void *userData{};
708 void *apiInfo{}; // void pointer for API specific callback information
709 bool isRunning{false};
710 bool doRealtime{false};
711 int priority{};
712 bool deviceDisconnected{false};
713};
714
715// **************************************************************** //
716//
717// RtApi class declaration.
718//
719// Subclasses of RtApi contain all API- and OS-specific code necessary
720// to fully implement the RtAudio API.
721//
722// Note that RtApi is an abstract base class and cannot be
723// explicitly instantiated. The class RtAudio will create an
724// instance of an RtApi subclass (RtApiOss, RtApiAlsa,
725// RtApiJack, RtApiCore, RtApiDs, or RtApiAsio).
726//
727// **************************************************************** //
728
729#pragma pack(push, 1)
730class S24 {
731
732 protected:
733 unsigned char c3[4];
734
735 public:
736 S24() {}
737
738 S24& operator = ( const int& i ) {
739 c3[0] = (unsigned char)(i & 0x000000ff);
740 c3[1] = (unsigned char)((i & 0x0000ff00) >> 8);
741 c3[2] = (unsigned char)((i & 0x00ff0000) >> 16);
742 return *this;
743 }
744
745 S24( const double& d ) { *this = (int) d; }
746 S24( const float& f ) { *this = (int) f; }
747 S24( const signed short& s ) { *this = (int) s; }
748 S24( const char& c ) { *this = (int) c; }
749
750 int asInt() {
751 int i = c3[0] | (c3[1] << 8) | (c3[2] << 16);
752 if (i & 0x800000) i |= ~0xffffff;
753 return i;
754 }
755};
756#pragma pack(pop)
757
758} // namespace audio
759} // namespace rt
760
761#if defined( HAVE_GETTIMEOFDAY )
762 #include <sys/time.h>
763#endif
764
765#include <sstream>
766
767namespace rt {
768namespace audio {
769
770class RTAUDIO_DLL_PUBLIC RtApi
771{
772public:
773
774 RtApi();
775 virtual ~RtApi();
776 virtual RtAudio::Api getCurrentApi( void ) = 0;
777 unsigned int getDeviceCount( void );
778 std::vector<unsigned int> getDeviceIds( void );
779 std::vector<std::string> getDeviceNames( void );
780 RtAudio::DeviceInfo getDeviceInfo( unsigned int deviceId );
781 virtual unsigned int getDefaultInputDevice( void );
782 virtual unsigned int getDefaultOutputDevice( void );
783 RtAudioErrorType openStream( RtAudio::StreamParameters *outputParameters,
784 RtAudio::StreamParameters *inputParameters,
785 RtAudioFormat format, unsigned int sampleRate,
786 unsigned int *bufferFrames, RtAudioCallback callback,
787 void *userData, RtAudio::StreamOptions *options );
788 virtual void closeStream( void );
789 virtual RtAudioErrorType startStream( void ) = 0;
790 virtual RtAudioErrorType stopStream( void ) = 0;
791 virtual RtAudioErrorType abortStream( void ) = 0;
792 const std::string &getErrorText(void) const { return errorText_; }
793 long getStreamLatency( void );
794 unsigned int getStreamSampleRate( void );
795 virtual double getStreamTime( void ) const { return stream_.streamTime; }
796 virtual void setStreamTime( double time );
797 bool isStreamOpen( void ) const { return stream_.state != STREAM_CLOSED; }
798 bool isStreamRunning( void ) const { return stream_.state == STREAM_RUNNING; }
799 void setErrorCallback( RtAudioErrorCallback errorCallback ) { errorCallback_ = errorCallback; }
800 void showWarnings( bool value ) { showWarnings_ = value; }
801
802
803protected:
804
805 static const unsigned int MAX_SAMPLE_RATES;
806 static const unsigned int SAMPLE_RATES[];
807
808 enum { FAILURE, SUCCESS };
809
810 enum StreamState {
811 STREAM_STOPPED,
812 STREAM_STOPPING,
813 STREAM_RUNNING,
814 STREAM_CLOSED = -50
815 };
816
817 enum StreamMode {
818 OUTPUT,
819 INPUT,
820 DUPLEX,
821 UNINITIALIZED = -75
822 };
823
824 // A protected structure used for buffer conversion.
825 struct ConvertInfo {
826 int channels;
827 int inJump, outJump;
828 RtAudioFormat inFormat, outFormat;
829 std::vector<int> inOffset;
830 std::vector<int> outOffset;
831 };
832
833 // A protected structure for audio streams.
834 struct RtApiStream {
835 unsigned int deviceId[2]; // Playback and record, respectively.
836 void *apiHandle; // void pointer for API specific stream handle information
837 StreamMode mode; // OUTPUT, INPUT, or DUPLEX.
838 StreamState state; // STOPPED, RUNNING, or CLOSED
839 char *userBuffer[2]; // Playback and record, respectively.
840 char *deviceBuffer;
841 bool doConvertBuffer[2]; // Playback and record, respectively.
842 bool userInterleaved;
843 bool deviceInterleaved[2]; // Playback and record, respectively.
844 bool doByteSwap[2]; // Playback and record, respectively.
845 unsigned int sampleRate;
846 unsigned int bufferSize;
847 unsigned int nBuffers;
848 unsigned int nUserChannels[2]; // Playback and record, respectively.
849 unsigned int nDeviceChannels[2]; // Playback and record channels, respectively.
850 unsigned int channelOffset[2]; // Playback and record, respectively.
851 unsigned long latency[2]; // Playback and record, respectively.
852 RtAudioFormat userFormat;
853 RtAudioFormat deviceFormat[2]; // Playback and record, respectively.
854 StreamMutex mutex;
855 CallbackInfo callbackInfo;
856 ConvertInfo convertInfo[2];
857 double streamTime; // Number of elapsed seconds since the stream started.
858
859#if defined(HAVE_GETTIMEOFDAY)
860 struct timeval lastTickTimestamp;
861#endif
862
863 RtApiStream()
864 :apiHandle(0), deviceBuffer(0) {} // { device[0] = std::string(); device[1] = std::string(); }
865 };
866
867 typedef S24 Int24;
868 typedef signed short Int16;
869 typedef signed int Int32;
870 typedef float Float32;
871 typedef double Float64;
872
873 std::ostringstream errorStream_;
874 std::string errorText_;
875 RtAudioErrorCallback errorCallback_;
876 bool showWarnings_;
877 std::vector<RtAudio::DeviceInfo> deviceList_;
878 unsigned int currentDeviceId_;
879 RtApiStream stream_;
880
889 virtual void probeDevices( void );
890
898 virtual bool probeDeviceOpen( unsigned int deviceId, StreamMode mode, unsigned int channels,
899 unsigned int firstChannel, unsigned int sampleRate,
900 RtAudioFormat format, unsigned int *bufferSize,
901 RtAudio::StreamOptions *options );
902
904 void tickStreamTime( void );
905
907 void clearStreamInfo();
908
910 RtAudioErrorType error( RtAudioErrorType type );
911
916 void convertBuffer( char *outBuffer, char *inBuffer, ConvertInfo &info );
917
919 void byteSwapBuffer( char *buffer, unsigned int samples, RtAudioFormat format );
920
922 unsigned int formatBytes( RtAudioFormat format );
923
925 void setConvertInfo( StreamMode mode, unsigned int firstChannel );
926};
927
928// **************************************************************** //
929//
930// Inline RtAudio definitions.
931//
932// **************************************************************** //
933
934inline RtAudio::Api RtAudio :: getCurrentApi( void ) { return rtapi_->getCurrentApi(); }
935inline unsigned int RtAudio :: getDeviceCount( void ) { return rtapi_->getDeviceCount(); }
936inline RtAudio::DeviceInfo RtAudio :: getDeviceInfo( unsigned int deviceId ) { return rtapi_->getDeviceInfo( deviceId ); }
937inline std::vector<unsigned int> RtAudio :: getDeviceIds( void ) { return rtapi_->getDeviceIds(); }
938inline std::vector<std::string> RtAudio :: getDeviceNames( void ) { return rtapi_->getDeviceNames(); }
939inline unsigned int RtAudio :: getDefaultInputDevice( void ) { return rtapi_->getDefaultInputDevice(); }
940inline unsigned int RtAudio :: getDefaultOutputDevice( void ) { return rtapi_->getDefaultOutputDevice(); }
941inline void RtAudio :: closeStream( void ) { return rtapi_->closeStream(); }
942inline RtAudioErrorType RtAudio :: startStream( void ) { return rtapi_->startStream(); }
943inline RtAudioErrorType RtAudio :: stopStream( void ) { return rtapi_->stopStream(); }
944inline RtAudioErrorType RtAudio :: abortStream( void ) { return rtapi_->abortStream(); }
945inline const std::string &RtAudio::getErrorText(void) { return rtapi_->getErrorText(); }
946inline bool RtAudio :: isStreamOpen( void ) const { return rtapi_->isStreamOpen(); }
947inline bool RtAudio :: isStreamRunning( void ) const { return rtapi_->isStreamRunning(); }
948inline long RtAudio :: getStreamLatency( void ) { return rtapi_->getStreamLatency(); }
949inline unsigned int RtAudio :: getStreamSampleRate( void ) { return rtapi_->getStreamSampleRate(); }
950inline double RtAudio :: getStreamTime( void ) { return rtapi_->getStreamTime(); }
951inline void RtAudio :: setStreamTime( double time ) { return rtapi_->setStreamTime( time ); }
952inline void RtAudio :: setErrorCallback( RtAudioErrorCallback errorCallback ) { rtapi_->setErrorCallback( errorCallback ); }
953inline void RtAudio :: showWarnings( bool value ) { rtapi_->showWarnings( value ); }
954
955} // namespace audio
956} // namespace rt
957
958#endif
959
960#ifndef RTAUDIO_USE_NAMESPACE
961using namespace rt::audio;
962#endif
963
964// Indentation settings for Vim and Emacs
965//
966// Local Variables:
967// c-basic-offset: 2
968// indent-tabs-mode: nil
969// End:
970//
971// vim: et sts=2 sw=2
std::function< void(RtAudioErrorType type, const std::string &errorText)> RtAudioErrorCallback
RtAudio error callback function prototype.
Definition RtAudio.h:255
unsigned long RtAudioFormat
RtAudio data format type.
Definition RtAudio.h:109
unsigned int RtAudioStreamFlags
RtAudio stream option flags.
Definition RtAudio.h:163
std::function< int(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *userData)> RtAudioCallback
RtAudio callback function prototype.
Definition RtAudio.h:231
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition RtAudio.h:182
RtAudioErrorType
Definition RtAudio.h:233
@ RTAUDIO_DEVICE_DISCONNECT
Definition RtAudio.h:239
@ RTAUDIO_WARNING
Definition RtAudio.h:235
@ RTAUDIO_UNKNOWN_ERROR
Definition RtAudio.h:236
@ RTAUDIO_NO_DEVICES_FOUND
Definition RtAudio.h:237
@ RTAUDIO_SYSTEM_ERROR
Definition RtAudio.h:244
@ RTAUDIO_INVALID_DEVICE
Definition RtAudio.h:238
@ RTAUDIO_DRIVER_ERROR
Definition RtAudio.h:243
@ RTAUDIO_INVALID_PARAMETER
Definition RtAudio.h:241
@ RTAUDIO_THREAD_ERROR
Definition RtAudio.h:245
@ RTAUDIO_NO_ERROR
Definition RtAudio.h:234
@ RTAUDIO_INVALID_USE
Definition RtAudio.h:242
@ RTAUDIO_MEMORY_ERROR
Definition RtAudio.h:240
The public device information structure for returning queried values.
Definition RtAudio.h:292
bool isDefaultOutput
Definition RtAudio.h:298
unsigned int outputChannels
Definition RtAudio.h:295
unsigned int preferredSampleRate
Definition RtAudio.h:302
unsigned int currentSampleRate
Definition RtAudio.h:301
unsigned int inputChannels
Definition RtAudio.h:296
unsigned int duplexChannels
Definition RtAudio.h:297
unsigned int ID
Definition RtAudio.h:293
std::vector< unsigned int > sampleRates
Definition RtAudio.h:300
std::string name
Definition RtAudio.h:294
bool isDefaultInput
Definition RtAudio.h:299
RtAudioFormat nativeFormats
Definition RtAudio.h:303
The structure for specifying stream options.
Definition RtAudio.h:373
RtAudioStreamFlags flags
Definition RtAudio.h:374
int priority
Definition RtAudio.h:377
unsigned int numberOfBuffers
Definition RtAudio.h:375
std::string streamName
Definition RtAudio.h:376
The structure for specifying input or output stream parameters.
Definition RtAudio.h:307
unsigned int nChannels
Definition RtAudio.h:310
unsigned int deviceId
Definition RtAudio.h:309
unsigned int firstChannel
Definition RtAudio.h:311

©2001-2023 Gary P. Scavone, McGill University. All Rights Reserved.
Maintained by Gary P. Scavone.