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

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