Home   Class/Enum List   File List   Compound Members   C interface  

RtAudio.h
Go to the documentation of this file.
1/************************************************************************/
40/************************************************************************/
41
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
105typedef unsigned long RtAudioFormat;
106static const RtAudioFormat RTAUDIO_SINT8 = 0x1; // 8-bit signed integer.
107static const RtAudioFormat RTAUDIO_SINT16 = 0x2; // 16-bit signed integer.
108static const RtAudioFormat RTAUDIO_SINT24 = 0x4; // 24-bit signed integer.
109static const RtAudioFormat RTAUDIO_SINT32 = 0x8; // 32-bit signed integer.
110static const RtAudioFormat RTAUDIO_FLOAT32 = 0x10; // Normalized between plus/minus 1.0.
111static const RtAudioFormat RTAUDIO_FLOAT64 = 0x20; // Normalized between plus/minus 1.0.
112
159typedef unsigned int RtAudioStreamFlags;
160static const RtAudioStreamFlags RTAUDIO_NONINTERLEAVED = 0x1; // Use non-interleaved buffers (default = interleaved).
161static const RtAudioStreamFlags RTAUDIO_MINIMIZE_LATENCY = 0x2; // Attempt to set stream parameters for lowest possible latency.
162static const RtAudioStreamFlags RTAUDIO_HOG_DEVICE = 0x4; // Attempt grab device and prevent use by others.
163static const RtAudioStreamFlags RTAUDIO_SCHEDULE_REALTIME = 0x8; // Try to select realtime scheduling for callback thread.
164static const RtAudioStreamFlags RTAUDIO_ALSA_USE_DEFAULT = 0x10; // Use the "default" PCM device (ALSA only).
165static const RtAudioStreamFlags RTAUDIO_JACK_DONT_CONNECT = 0x20; // Do not automatically connect ports (JACK only).
166
178typedef unsigned int RtAudioStreamStatus;
179static const RtAudioStreamStatus RTAUDIO_INPUT_OVERFLOW = 0x1; // Input data was discarded because of an overflow condition at the driver.
180static const RtAudioStreamStatus RTAUDIO_OUTPUT_UNDERFLOW = 0x2; // The output buffer ran low, likely causing a gap in the output sound.
181
183
223typedef std::function<int(void* outputBuffer, void* inputBuffer,
224 unsigned int nFrames,
225 double streamTime,
226 RtAudioStreamStatus status,
227 void* userData)> RtAudioCallback;
228
243
245
249typedef std::function<void(RtAudioErrorType type,
250 const std::string &errorText )>
252
253// **************************************************************** //
254//
255// RtAudio class declaration.
256//
257// RtAudio is a "controller" used to select an available audio i/o
258// interface. It presents a common API for the user to call but all
259// functionality is implemented by the class RtApi and its
260// subclasses. RtAudio creates an instance of an RtApi subclass
261// based on the user's API choice. If no choice is made, RtAudio
262// attempts to make a "logical" API selection.
263//
264// **************************************************************** //
265
266class RtApi;
267
268class RTAUDIO_DLL_PUBLIC RtAudio
269{
270 public:
271
286
288 struct DeviceInfo {
289 unsigned int ID{};
290 std::string name;
291 unsigned int outputChannels{};
292 unsigned int inputChannels{};
293 unsigned int duplexChannels{};
294 bool isDefaultOutput{false};
295 bool isDefaultInput{false};
296 std::vector<unsigned int> sampleRates;
297 unsigned int currentSampleRate{};
298 unsigned int preferredSampleRate{};
299 RtAudioFormat nativeFormats{};
300 };
301
304 //std::string deviceName{}; /*!< Device name from device list. */
305 unsigned int deviceId{};
306 unsigned int nChannels{};
307 unsigned int firstChannel{};
308 };
309
311
371 unsigned int numberOfBuffers{};
372 std::string streamName;
373 int priority{};
374 };
375
377 static std::string getVersion( void );
378
380
385 static void getCompiledApi( std::vector<RtAudio::Api> &apis );
386
388
393 static std::string getApiName( RtAudio::Api api );
394
396
400 static std::string getApiDisplayName( RtAudio::Api api );
401
403
408 static RtAudio::Api getCompiledApiByName( const std::string &name );
409
411
416 static RtAudio::Api getCompiledApiByDisplayName( const std::string &name );
417
419
434 RtAudio( RtAudio::Api api=UNSPECIFIED, RtAudioErrorCallback&& errorCallback=0 );
435
437
442
444 RtAudio::Api getCurrentApi( void );
445
447
453 unsigned int getDeviceCount( void );
454
456
466 std::vector<unsigned int> getDeviceIds( void );
467
469
476 std::vector<std::string> getDeviceNames( void );
477
479
491 RtAudio::DeviceInfo getDeviceInfo( unsigned int deviceId );
492
494
500 unsigned int getDefaultOutputDevice( void );
501
503
509 unsigned int getDefaultInputDevice( void );
510
512
550 RtAudio::StreamParameters *inputParameters,
551 RtAudioFormat format, unsigned int sampleRate,
552 unsigned int *bufferFrames, RtAudioCallback callback,
553 void *userData = NULL, RtAudio::StreamOptions *options = NULL );
554
556
561 void closeStream( void );
562
564
569 RtAudioErrorType startStream( void );
570
572
577 RtAudioErrorType stopStream( void );
578
580
585 RtAudioErrorType abortStream( void );
586
588
593 const std::string &getErrorText(void);
594
596 bool isStreamOpen( void ) const;
597
599 bool isStreamRunning( void ) const;
600
602
609 double getStreamTime( void );
610
612 void setStreamTime( double time );
613
615
623 long getStreamLatency( void );
624
626
631 unsigned int getStreamSampleRate( void );
632
634 void setErrorCallback( RtAudioErrorCallback errorCallback );
635
637
642 void showWarnings( bool value = true );
643
644 protected:
645
646 void openRtApi( RtAudio::Api api );
647 RtApi *rtapi_;
648};
649
650// Operating system dependent thread functionality.
651#if defined(_MSC_VER)
652
653 #ifndef NOMINMAX
654 #define NOMINMAX
655 #endif
656 #include <windows.h>
657 #include <process.h>
658 #include <stdint.h>
659
660 typedef uintptr_t ThreadHandle;
661 typedef CRITICAL_SECTION StreamMutex;
662
663#else
664
665 // Using pthread library for various flavors of unix.
666 #include <pthread.h>
667
668 typedef pthread_t ThreadHandle;
669 typedef pthread_mutex_t StreamMutex;
670
671#endif
672
673// Setup for "dummy" behavior if no apis specified.
674#if !(defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_WASAPI__) \
675 || defined(__LINUX_ALSA__) || defined(__LINUX_PULSE__) || defined(__UNIX_JACK__) \
676 || defined(__LINUX_OSS__) || defined(__MACOSX_CORE__))
677
678 #define __RTAUDIO_DUMMY__
679
680#endif
681
682// This global structure type is used to pass callback information
683// between the private RtAudio stream structure and global callback
684// handling functions.
685struct CallbackInfo {
686 void *object{}; // Used as a "this" pointer.
687 ThreadHandle thread{};
688 RtAudioCallback callback{};
689 void *userData{};
690 void *apiInfo{}; // void pointer for API specific callback information
691 bool isRunning{false};
692 bool doRealtime{false};
693 int priority{};
694 bool deviceDisconnected{false};
695};
696
697// **************************************************************** //
698//
699// RtApi class declaration.
700//
701// Subclasses of RtApi contain all API- and OS-specific code necessary
702// to fully implement the RtAudio API.
703//
704// Note that RtApi is an abstract base class and cannot be
705// explicitly instantiated. The class RtAudio will create an
706// instance of an RtApi subclass (RtApiOss, RtApiAlsa,
707// RtApiJack, RtApiCore, RtApiDs, or RtApiAsio).
708//
709// **************************************************************** //
710
711#pragma pack(push, 1)
712class S24 {
713
714 protected:
715 unsigned char c3[4];
716
717 public:
718 S24() {}
719
720 S24& operator = ( const int& i ) {
721 c3[0] = (unsigned char)(i & 0x000000ff);
722 c3[1] = (unsigned char)((i & 0x0000ff00) >> 8);
723 c3[2] = (unsigned char)((i & 0x00ff0000) >> 16);
724 return *this;
725 }
726
727 S24( const double& d ) { *this = (int) d; }
728 S24( const float& f ) { *this = (int) f; }
729 S24( const signed short& s ) { *this = (int) s; }
730 S24( const char& c ) { *this = (int) c; }
731
732 int asInt() {
733 int i = c3[0] | (c3[1] << 8) | (c3[2] << 16);
734 if (i & 0x800000) i |= ~0xffffff;
735 return i;
736 }
737};
738#pragma pack(pop)
739
740#if defined( HAVE_GETTIMEOFDAY )
741 #include <sys/time.h>
742#endif
743
744#include <sstream>
745
746class RTAUDIO_DLL_PUBLIC RtApi
747{
748public:
749
750 RtApi();
751 virtual ~RtApi();
752 virtual RtAudio::Api getCurrentApi( void ) = 0;
753 unsigned int getDeviceCount( void );
754 std::vector<unsigned int> getDeviceIds( void );
755 std::vector<std::string> getDeviceNames( void );
756 RtAudio::DeviceInfo getDeviceInfo( unsigned int deviceId );
757 virtual unsigned int getDefaultInputDevice( void );
758 virtual unsigned int getDefaultOutputDevice( void );
759 RtAudioErrorType openStream( RtAudio::StreamParameters *outputParameters,
760 RtAudio::StreamParameters *inputParameters,
761 RtAudioFormat format, unsigned int sampleRate,
762 unsigned int *bufferFrames, RtAudioCallback callback,
763 void *userData, RtAudio::StreamOptions *options );
764 virtual void closeStream( void );
765 virtual RtAudioErrorType startStream( void ) = 0;
766 virtual RtAudioErrorType stopStream( void ) = 0;
767 virtual RtAudioErrorType abortStream( void ) = 0;
768 const std::string &getErrorText(void) const { return errorText_; }
769 long getStreamLatency( void );
770 unsigned int getStreamSampleRate( void );
771 virtual double getStreamTime( void ) const { return stream_.streamTime; }
772 virtual void setStreamTime( double time );
773 bool isStreamOpen( void ) const { return stream_.state != STREAM_CLOSED; }
774 bool isStreamRunning( void ) const { return stream_.state == STREAM_RUNNING; }
775 void setErrorCallback( RtAudioErrorCallback errorCallback ) { errorCallback_ = errorCallback; }
776 void showWarnings( bool value ) { showWarnings_ = value; }
777
778
779protected:
780
781 static const unsigned int MAX_SAMPLE_RATES;
782 static const unsigned int SAMPLE_RATES[];
783
784 enum { FAILURE, SUCCESS };
785
786 enum StreamState {
787 STREAM_STOPPED,
788 STREAM_STOPPING,
789 STREAM_RUNNING,
790 STREAM_CLOSED = -50
791 };
792
793 enum StreamMode {
794 OUTPUT,
795 INPUT,
796 DUPLEX,
797 UNINITIALIZED = -75
798 };
799
800 // A protected structure used for buffer conversion.
801 struct ConvertInfo {
802 int channels;
803 int inJump, outJump;
804 RtAudioFormat inFormat, outFormat;
805 std::vector<int> inOffset;
806 std::vector<int> outOffset;
807 };
808
809 // A protected structure for audio streams.
810 struct RtApiStream {
811 unsigned int deviceId[2]; // Playback and record, respectively.
812 void *apiHandle; // void pointer for API specific stream handle information
813 StreamMode mode; // OUTPUT, INPUT, or DUPLEX.
814 StreamState state; // STOPPED, RUNNING, or CLOSED
815 char *userBuffer[2]; // Playback and record, respectively.
816 char *deviceBuffer;
817 bool doConvertBuffer[2]; // Playback and record, respectively.
818 bool userInterleaved;
819 bool deviceInterleaved[2]; // Playback and record, respectively.
820 bool doByteSwap[2]; // Playback and record, respectively.
821 unsigned int sampleRate;
822 unsigned int bufferSize;
823 unsigned int nBuffers;
824 unsigned int nUserChannels[2]; // Playback and record, respectively.
825 unsigned int nDeviceChannels[2]; // Playback and record channels, respectively.
826 unsigned int channelOffset[2]; // Playback and record, respectively.
827 unsigned long latency[2]; // Playback and record, respectively.
828 RtAudioFormat userFormat;
829 RtAudioFormat deviceFormat[2]; // Playback and record, respectively.
830 StreamMutex mutex;
831 CallbackInfo callbackInfo;
832 ConvertInfo convertInfo[2];
833 double streamTime; // Number of elapsed seconds since the stream started.
834
835#if defined(HAVE_GETTIMEOFDAY)
836 struct timeval lastTickTimestamp;
837#endif
838
839 RtApiStream()
840 :apiHandle(0), deviceBuffer(0) {} // { device[0] = std::string(); device[1] = std::string(); }
841 };
842
843 typedef S24 Int24;
844 typedef signed short Int16;
845 typedef signed int Int32;
846 typedef float Float32;
847 typedef double Float64;
848
849 std::ostringstream errorStream_;
850 std::string errorText_;
851 RtAudioErrorCallback errorCallback_;
852 bool showWarnings_;
853 std::vector<RtAudio::DeviceInfo> deviceList_;
854 unsigned int currentDeviceId_;
855 RtApiStream stream_;
856
865 virtual void probeDevices( void );
866
874 virtual bool probeDeviceOpen( unsigned int deviceId, StreamMode mode, unsigned int channels,
875 unsigned int firstChannel, unsigned int sampleRate,
876 RtAudioFormat format, unsigned int *bufferSize,
877 RtAudio::StreamOptions *options );
878
880 void tickStreamTime( void );
881
883 void clearStreamInfo();
884
887
892 void convertBuffer( char *outBuffer, char *inBuffer, ConvertInfo &info );
893
895 void byteSwapBuffer( char *buffer, unsigned int samples, RtAudioFormat format );
896
898 unsigned int formatBytes( RtAudioFormat format );
899
901 void setConvertInfo( StreamMode mode, unsigned int firstChannel );
902};
903
904// **************************************************************** //
905//
906// Inline RtAudio definitions.
907//
908// **************************************************************** //
909
910inline RtAudio::Api RtAudio :: getCurrentApi( void ) { return rtapi_->getCurrentApi(); }
911inline unsigned int RtAudio :: getDeviceCount( void ) { return rtapi_->getDeviceCount(); }
912inline RtAudio::DeviceInfo RtAudio :: getDeviceInfo( unsigned int deviceId ) { return rtapi_->getDeviceInfo( deviceId ); }
913inline std::vector<unsigned int> RtAudio :: getDeviceIds( void ) { return rtapi_->getDeviceIds(); }
914inline std::vector<std::string> RtAudio :: getDeviceNames( void ) { return rtapi_->getDeviceNames(); }
915inline unsigned int RtAudio :: getDefaultInputDevice( void ) { return rtapi_->getDefaultInputDevice(); }
916inline unsigned int RtAudio :: getDefaultOutputDevice( void ) { return rtapi_->getDefaultOutputDevice(); }
917inline void RtAudio :: closeStream( void ) { return rtapi_->closeStream(); }
918inline RtAudioErrorType RtAudio :: startStream( void ) { return rtapi_->startStream(); }
919inline RtAudioErrorType RtAudio :: stopStream( void ) { return rtapi_->stopStream(); }
920inline RtAudioErrorType RtAudio :: abortStream( void ) { return rtapi_->abortStream(); }
921inline const std::string &RtAudio::getErrorText(void) { return rtapi_->getErrorText(); }
922inline bool RtAudio :: isStreamOpen( void ) const { return rtapi_->isStreamOpen(); }
923inline bool RtAudio :: isStreamRunning( void ) const { return rtapi_->isStreamRunning(); }
924inline long RtAudio :: getStreamLatency( void ) { return rtapi_->getStreamLatency(); }
925inline unsigned int RtAudio :: getStreamSampleRate( void ) { return rtapi_->getStreamSampleRate(); }
926inline double RtAudio :: getStreamTime( void ) { return rtapi_->getStreamTime(); }
927inline void RtAudio :: setStreamTime( double time ) { return rtapi_->setStreamTime( time ); }
928inline void RtAudio :: setErrorCallback( RtAudioErrorCallback errorCallback ) { rtapi_->setErrorCallback( errorCallback ); }
929inline void RtAudio :: showWarnings( bool value ) { rtapi_->showWarnings( value ); }
930
931#endif
932
933// Indentation settings for Vim and Emacs
934//
935// Local Variables:
936// c-basic-offset: 2
937// indent-tabs-mode: nil
938// End:
939//
940// vim: et sts=2 sw=2
std::function< void(RtAudioErrorType type, const std::string &errorText)> RtAudioErrorCallback
RtAudio error callback function prototype.
Definition RtAudio.h:251
unsigned long RtAudioFormat
RtAudio data format type.
Definition RtAudio.h:105
RtAudioErrorType
Definition RtAudio.h:229
@ RTAUDIO_DEVICE_DISCONNECT
Definition RtAudio.h:235
@ RTAUDIO_DRIVER_ERROR
Definition RtAudio.h:239
@ RTAUDIO_NO_ERROR
Definition RtAudio.h:230
@ RTAUDIO_INVALID_PARAMETER
Definition RtAudio.h:237
@ RTAUDIO_INVALID_DEVICE
Definition RtAudio.h:234
@ RTAUDIO_SYSTEM_ERROR
Definition RtAudio.h:240
@ RTAUDIO_WARNING
Definition RtAudio.h:231
@ RTAUDIO_THREAD_ERROR
Definition RtAudio.h:241
@ RTAUDIO_INVALID_USE
Definition RtAudio.h:238
@ RTAUDIO_UNKNOWN_ERROR
Definition RtAudio.h:232
@ RTAUDIO_NO_DEVICES_FOUND
Definition RtAudio.h:233
@ RTAUDIO_MEMORY_ERROR
Definition RtAudio.h:236
unsigned int RtAudioStreamFlags
RtAudio stream option flags.
Definition RtAudio.h:159
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition RtAudio.h:178
std::function< int(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *userData)> RtAudioCallback
RtAudio callback function prototype.
Definition RtAudio.h:227
Realtime audio i/o C++ classes.
Definition RtAudio.h:269
static RtAudio::Api getCompiledApiByDisplayName(const std::string &name)
Return the compiled audio API having the given display name.
std::string name
Definition RtAudio.h:290
std::string streamName
Definition RtAudio.h:372
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.
RtAudioErrorType openStream(RtAudio::StreamParameters *outputParameters, RtAudio::StreamParameters *inputParameters, RtAudioFormat format, unsigned int sampleRate, unsigned int *bufferFrames, RtAudioCallback callback, void *userData=NULL, RtAudio::StreamOptions *options=NULL)
A public function for opening a stream with the specified parameters.
const std::string & getErrorText(void)
Retrieve the error message corresponding to the last error or warning condition.
Definition RtAudio.h:921
~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:273
@ WINDOWS_ASIO
Definition RtAudio.h:280
@ WINDOWS_DS
Definition RtAudio.h:282
@ LINUX_OSS
Definition RtAudio.h:279
@ UNIX_JACK
Definition RtAudio.h:277
@ MACOSX_CORE
Definition RtAudio.h:275
@ UNSPECIFIED
Definition RtAudio.h:274
@ LINUX_ALSA
Definition RtAudio.h:276
@ RTAUDIO_DUMMY
Definition RtAudio.h:283
@ WINDOWS_WASAPI
Definition RtAudio.h:281
@ LINUX_PULSE
Definition RtAudio.h:278
RtAudio(RtAudio::Api api=UNSPECIFIED, RtAudioErrorCallback &&errorCallback=0)
The class constructor.
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:296
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:288
The structure for specifying stream options.
Definition RtAudio.h:369
The structure for specifying input or output stream parameters.
Definition RtAudio.h:303

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