forked from thestk/rtmidi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RtMidi.h
1687 lines (1437 loc) · 59.9 KB
/
RtMidi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************* -*- C++ -*- ****************************************/
/*! \class RtMidi
\brief An abstract base class for realtime MIDI input/output.
This class implements some common functionality for the realtime
MIDI input/output subclasses RtMidiIn and RtMidiOut.
RtMidi WWW site: http://music.mcgill.ca/~gary/rtmidi/
RtMidi: realtime MIDI i/o C++ classes
Copyright ( c ) 2003-2017 Gary P. Scavone
Forked by Tobias Schlemmer, 2014-2018.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
( the "Software" ), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Any person wishing to distribute modifications to the Software is
asked to send the modifications to the original developer so that
they can be incorporated into the canonical version. This is,
however, not a binding provision of this license.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**********************************************************************/
/*!
\file RtMidi.h
*/
#ifndef RTMIDI_H
#define RTMIDI_H
#define RTMIDI_VERSION "4.0.0"
#ifdef RTMIDI_NO_WARN_DEPRECATED
#define RTMIDI_DEPRECATED( func, message ) func
#else
#ifdef __GNUC__
#define RTMIDI_DEPRECATED( func, message ) func __attribute__ ( ( deprecated ( message ) ) )
#elif defined ( _MSC_VER )
#define RTMIDI_DEPRECATED( func, message ) __declspec ( deprecated ( message ) ) func
#else
#pragma message ( "WARNING: You need to implement the macro RTMIDI_DEPRECATED for this compiler if this code doesn't compile" )
#define RTMIDI_DEPRECATED( func, message ) func [[deprecated ( message ) ]]
#endif
#endif
#define rtmidiUnused( x ) do { ( void ) x; } while ( 0 )
// Check for C++11 support
#if defined ( _MSC_VER ) && _MSC_VER >= 1800
// At least Visual Studio 2013
#define RTMIDI_SUPPORTS_CPP11 1
#elif __cplusplus >= 201103L
#define RTMIDI_SUPPORTS_CPP11 1
#else
#define RTMIDI_SUPPORTS_CPP11 0
#endif
#if defined _WIN32 || defined __CYGWIN__
#if defined ( RTMIDI_EXPORT )
#define RTMIDI_DLL_PUBLIC __declspec ( dllexport )
#else
#define RTMIDI_DLL_PUBLIC
#endif
#else
#if __GNUC__ >= 4
#define RTMIDI_DLL_PUBLIC __attribute__ ( ( visibility ( "default" ) ) )
#else
#define RTMIDI_DLL_PUBLIC
#endif
#endif
// some fake libintl implementations may need special headers.
#ifdef RTMIDI_GETTEXT
#include "libintl.h"
#endif
#include <exception>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <memory>
#include <stdexcept>
#include <atomic>
// the following are used in the error constructor
#include <cstdarg>
#include <cstring>
#include <cstdio>
#define gettext_noopt( str ) ( str )
#define RTMIDI_NAMESPACE_START namespace rtmidi {
#define RTMIDI_NAMESPACE_END }
RTMIDI_NAMESPACE_START
#ifdef RTMIDI_GETTEXT
void init_rtmidi_gettext ( );
RTMIDI_DLL_PUBLIC const char * rtmidi_gettext ( const char * s );
#else
#define rtmidi_gettext( arg ) ( arg )
#endif
//! MIDI API specifier arguments.
enum ApiType {
UNSPECIFIED, /*!< Search for a working compiled API. */
MACOSX_CORE, /*!< Macintosh OS-X Core Midi API. */
LINUX_ALSA, /*!< The Advanced Linux Sound Architecture API. */
UNIX_JACK, /*!< The JACK Low-Latency MIDI Server API. */
WINDOWS_MM, /*!< The Microsoft Multimedia MIDI API. */
WINDOWS_KS, /*!< The Microsoft Kernel Streaming MIDI API. */
DUMMY, /*!< A compilable but non-functional API. */
ALL_API, /*!< Use all available APIs for port selection. */
NUM_APIS /*!< Number of values in this enum. */
};
//! C++ style user callback interface.
/*!
This interface class can be used to implement type safe MIDI callbacks.
Every time a MIDI message is received the function \ref MidiInterface::rtmidi_midi_in of the
currently set callback object is called.
*/
struct MidiInterface {
//! Virtual destructor to avoid unexpected behaviour.
virtual ~MidiInterface ( ) {}
//! The MIDI callback function.
/*! This function is called whenever a MIDI packet is received by a
MIDI backend that uses the corresponding callback object.
\param timestamp the timestamp when the MIDI message has been received
\param message the message itself.
*/
virtual void rtmidi_midi_in ( double timestamp, std::vector<unsigned char>& message ) = 0;
//! Delete the object if necessary.
/*! This function allows the user to delete the Midi callback object,
when MIDI backend drops its reference to it. By default it does nothing.
But, callback objects are owned by the MIDI backend. These must be deleted
after the reference to them has been dropped.
\sa CompatibilityMidiInterface
*/
virtual void delete_me ( ) {}
};
/************************************************************************/
/*! \class Error
\brief Exception handling class for RtMidi.
The Error class is quite simple but it does allow errors to be
"caught" by Error::Type. See the RtMidi documentation to know
which methods can throw an Error.
*/
/************************************************************************/
#define RTMIDI_ERROR( message, type ) \
rtmidi::Error ( message, type, \
RTMIDI_CLASSNAME, __FUNCTION__, \
__FILE__, __LINE__ )
#define RTMIDI_ERROR1( message, type, arg1 ) \
rtmidi::Error ( message, type, \
RTMIDI_CLASSNAME, __FUNCTION__, \
__FILE__, __LINE__, arg1 )
class RTMIDI_DLL_PUBLIC Error : public std::exception
{
public:
//! Defined Error types.
enum Type {
WARNING, /*!< A non-critical error. */
DEBUG_WARNING, /*!< A non-critical error which might be useful for debugging. */
UNSPECIFIED, /*!< The default, unspecified error type. */
NO_DEVICES_FOUND, /*!< No devices found on system. */
INVALID_DEVICE, /*!< An invalid device ID was specified. */
MEMORY_ERROR, /*!< An error occured during memory allocation. */
INVALID_PARAMETER, /*!< An invalid parameter was specified to a function. */
INVALID_USE, /*!< The function was called incorrectly. */
DRIVER_ERROR, /*!< A system driver error occured. */
SYSTEM_ERROR, /*!< A system error occured. */
THREAD_ERROR /*!< A thread error occured. */
};
//! The constructor.
Error ( const char * message,
Type type,
const char * class_name,
const char * function_name,
const char * file_name,
int line_number, ... ) throw ( );
//! The destructor.
virtual ~Error ( void ) throw ( ) {}
//! Prints thrown error message to stderr.
virtual void printMessage ( std::ostream& s = std::cerr ) const throw ( ) {
s << std::endl
<< file << ":" << line << ": in function "
<< classname << "::" << function << std::endl
<< message_ << std::endl << std::endl;
}
//! Returns the thrown error message type.
virtual const Type& getType ( void ) const throw ( ) { return type_; }
//! Returns the thrown error message string.
virtual const std::string& getMessage ( void ) const throw ( ) { return message_; }
//! Returns the thrown error message as a c-style string.
virtual const char * what ( void ) const throw ( ) { return message_.c_str ( ); }
protected:
const char * classname;
const char * function;
const char * file;
int line;
std::string message_;
Type type_;
};
struct ErrorInterface {
virtual ~ErrorInterface ( ) {}
virtual void rtmidi_error ( Error e ) = 0;
virtual void delete_me ( ) {};
};
#if !RTMIDI_SUPPORTS_CPP11
class PortDescriptor;
template<class T>
class Pointer {
public:
typedef T datatype;
protected:
struct countPointer {
int count;
datatype * descriptor;
};
public:
Pointer ( )
: ptr ( 0 ) {}
Pointer ( datatype * p )
: ptr ( new countPointer ) {
ptr->count = 1;
ptr->descriptor = p;
}
Pointer ( const Pointer<datatype>& other )
: ptr ( other.ptr ) {
if ( ptr )
ptr->count++;
}
Pointer ( const Pointer<datatype>&& other )
: ptr ( other.ptr ) {
}
~Pointer ( ) {
if ( !ptr ) return;
if ( ! ( -- ( ptr->count ) ) ) {
delete ptr->descriptor;
delete ptr;
}
}
datatype * operator -> ( ) {
if ( !ptr ) return 0;
// this should throw an exception
return ptr->descriptor;
}
datatype& operator * ( ) {
if ( !ptr || !ptr->descriptor ) {
throw std::invalid_argument ( "rtmidi::Pointer: trying to dereference a NULL pointer." );
}
else return ( *ptr->descriptor );
}
const datatype& operator * ( ) const {
if ( !ptr || !ptr->descriptor ) {
throw std::invalid_argument ( "rtmidi::Pointer: trying to dereference a NULL pointer." );
}
else return ( *ptr->descriptor );
}
bool operator ! ( ) {
return ( !ptr || !ptr->descriptor );
}
operator bool ( ) {
return ( ptr && ptr->descriptor );
}
Pointer& operator = ( const Pointer<datatype>& other ) {
if ( ptr ) {
if ( ! ( --ptr->count ) ) {
delete ptr->descriptor;
delete ptr;
}
}
if ( ( ptr = other.ptr ) )
ptr->count++;
return *this;
}
protected:
countPointer * ptr;
};
template <class T, class U>
bool operator== ( const Pointer<T>& lhs, const Pointer<U>& rhs ) {
return ( & ( *lhs ) ) == ( & ( *rhs ) );
}
template <class T, class U>
bool operator!= ( const Pointer<T>& lhs, const Pointer<U>& rhs ) {
return ( & ( *lhs ) ) != ( & ( *rhs ) );
}
#else
template<class T>
using Pointer = std::shared_ptr<T>;
#endif
class MidiApi;
class MidiInApi;
class MidiOutApi;
typedef Pointer<MidiApi> MidiApiPtr;
typedef std::list <MidiApiPtr> MidiApiList;
class PortDescriptor {
public:
//! Flags for formatting a string description of the port.
/*! These flags just mark the requirements that the string
should fulfil. An API may return the same string for
different requirements e.g. the same short and long
name. */
enum NamingType {
SHORT_NAME =0, /*!< A short human readable name
depending on the API
e.g. “Ensoniq AudioPCI” */
LONG_NAME, /*!< A complete human readable
name depending on the API
e.g. "Ensoniq AudioPCI: ES1371" */
SESSION_PATH, /*!< A unique description that can
be used to identify the port
during runtime. It may be a
cryptic string. */
STORAGE_PATH, /*!< A unique description that is
optimised for storage in
configuration files. This is a
more textual representation that
is more robust to small changes in
the surrounding environment than
\ref SESSION_PATH */
NAMING_MASK = 0x0F, /*!< part of the flags
that is concerned with
naming.
*/
UNIQUE_PORT_NAME = 0x10, /*!< Make all names uniqe. This
is usually done by adding
numbers to the end of the
string \note: use #undef UNIQUE_PORT_NAME
on windows in case of any errors */
INCLUDE_API = 0x20 /*!< Add a string describing the
API at the beginning of the
string. */
};
//! Flags describing the capabilities of a given port.
enum PortCapabilities {
INPUT = 1, /*!< Ports that can be read from. */
OUTPUT = 2, /*!< Ports that can be written to. */
INOUTPUT = 3, /*!< Ports that allow reading and writing ( INPUT | OUTPUT ) */
UNLIMITED = 0x10 /*!< Some APIs can filter out certain ports which they consider
not to be useful. This flags supresses this behaviour and
selects all ports that are useable. */
};
//! Default constructor.
/*!
* Derived classes should have a constructor.
*/
PortDescriptor ( ) {};
//! A virtual destructor
/*! As we might have to destruct the object from the application code
* each port id must have a virtual destructor.
*/
virtual ~PortDescriptor ( ) {};
//! Get the MIDI input api for the current port.
/*! This is the only information RtMidi needs to know: Which
* API should handle this object. This can be used to get
* an API which can send data to the given port.
*
* \param queueSizeLimit The limit of the midi queue. This parameter is handled by
* the constructor of the backend API.
*
* \return API that can use this object to connect to an input port or 0
* if no input API can be created.
*/
virtual MidiInApi * getInputApi ( unsigned int queueSizeLimit = 100 ) const = 0;
//! Get the MIDI output api for the current port.
/*! This is the only information RtMidi needs to know: Which
* API should handle this object. This can be used to get
* an API which can receive data from the given port.
*
* \return API that can use this object to connect to an output port or 0
* if no output API can be created.
*/
virtual MidiOutApi * getOutputApi ( ) const = 0;
//! Return the port name
/*!
* \param flags A description of the requirements of the returned name.
* \return A name that is formatted according to \ref flags.
* \sa NamingTypes
*/
virtual std::string getName ( int flags = SHORT_NAME | UNIQUE_PORT_NAME ) = 0;
//! Get capabilities
/*! \return a capabilities flag describing the capabilities of the port.
* \sa PortCapabilities
*/
virtual int getCapabilities ( ) const = 0;
//! Compare two PortDescriptors
/*! \return true if both descriptors describe the same port
*/
virtual bool operator == ( const PortDescriptor& o ) = 0;
};
//! A list of port descriptors.
/*! Port descriptors are stored as shared pointers. This avoids
unnecessary duplication of the data structure and handles automatic
deletion if all references have been removed. */
typedef Pointer<PortDescriptor> PortPointer;
typedef std::list<Pointer<PortDescriptor> > PortList;
/* A deprecated type. See below for the documentation. We
split the definiton into several pieces to work around some
intended warnings. */
typedef void ( *ErrorCallback_t ) ( Error::Type type, const std::string& errorText, void * userdata );
//! RtMidi error callback function prototype.
/*!
\param type Type of error.
\param errorText Error description.
Note that class behaviour is undefined after a critical error ( not
a warning ) is reported.
\sa ErrorInterface
\deprecated
*/
RTMIDI_DEPRECATED ( typedef ErrorCallback_t ErrorCallback, "RtMidi now provides a class MidiInterface for error callbacks" );
/* A deprecated type. See below for the documentation. We
split the definiton into several pieces to work around some
intended warnings. */
#define ErrorCallback ErrorCallback_t
typedef void ( *MidiCallback_t ) ( double timeStamp, std::vector<unsigned char> * message, void * userData );
//! C style user callback function type definition.
/*!
This interface type has been replaced by a MidiInterface class.
\param timeStamp timestamp indicating when the event has been received
\param message a pointer to the binary MIDI message
\param userData a pointer that can be set using setUserdata
\sa MidiIn
\sa MidiInApi
\sa MidiInterface
\deprecated
*/
RTMIDI_DEPRECATED ( typedef MidiCallback_t MidiCallback, "RtMidi now provides a class MidiInterface for MIDI callbacks" );
#define MidiCallback MidiCallback_t
/*! \class Midi
\brief A global class that implements basic backend API handling.
This class enhances \ref MidiApi by some functionality to handle
backend API objects. It serves as base class for the public RtMidi
API.
by Gary P. Scavone, 2003-2014.
*/
/**********************************************************************/
#define RTMIDI_CLASSNAME "Midi"
class RTMIDI_DLL_PUBLIC Midi {
public:
//! A static function to determine the current RtMidi version.
static std::string getVersion ( void ) throw ( );
//! A static function to determine the available compiled MIDI APIs.
/*!
The values returned in the std::vector can be compared against
the enumerated list values. Note that there can be more than one
API compiled for certain operating systems.
\param apis A vector apis must be provided for the
return value. All data in this vector will be
deleted prior to filling in the API data.
\param preferSystem An opitonal boolean parameter
may be provided that tells wheter system or software
APIs shall be prefered. Passing \c true will prefer OS provided APIs
*/
static void getCompiledApi ( std::vector<ApiType>& apis, bool preferSystem = true ) throw ( );
//! A static function to determine the available compiled MIDI APIs.
/*!
The values returned in the std::vector can be compared against
the enumerated list values. Note that there can be more than one
API compiled for certain operating systems.
\param preferSystem An opitonal boolean parameter
may be provided that tells wheter system or software
APIs shall be prefered. Passing \c true will prefer OS provided APIs
*/
static std::vector<ApiType> getCompiledApi ( bool preferSystem = true ) throw ( );
//! Return the name of a specified compiled MIDI API.
/*!
This obtains a short lower-case name used for identification purposes.
This value is guaranteed to remain identical across library versions.
If the API is unknown, this function will return the empty string.
*/
static std::string getApiName ( ApiType api );
//! Return the display name of a specified compiled MIDI API.
/*!
This obtains a long name used for display purposes.
If the API is unknown, this function will return the empty string.
*/
static std::string getApiDisplayName ( ApiType api );
//! Return the compiled MIDI API having the given name.
/*!
A case insensitive comparison will check the specified name
against the list of compiled APIs, and return the one which
matches. On failure, the function returns UNSPECIFIED.
*/
static ApiType getCompiledApiByName ( const std::string& name, bool preferSystem = true );
//! Returns the MIDI API specifier for the current instance of rtmidi::MidiIn.
ApiType getCurrentApi ( void ) throw ( );
//! Pure virtual function to return a port descirptor if the port is open
Pointer<PortDescriptor> getDescriptor ( bool local=false );
//! Return a list of all available ports of the current API.
/*!
\param capabilities an optional parameter that
describes which capabilities the returned devices
must support. The returned devices may have
additional capabilities to those which have been
requested, but not less.
\return This function returns a list of port descriptors.
\note Each API will request additonal
capabilites. An output API will set always add \ref
PortDescriptor::OUTPUT to the mask while an input
device will always add \ref PortDescriptor::OUTPUT.
\note An input API may but need not necessarily
report output devices which cannot be used as input
if \ref PortDescriptor::OUTPUT is passed as \ref
capabilities parameter.
*/
PortList getPortList ( int capabilities = 0 );
//! Close an open MIDI connection ( if one exists ) .
void closePort ( void );
void setClientName ( const std::string& clientName );
void setPortName ( const std::string& portName );
//! Returns true if a port is open and false if not.
/*!
Note that this only applies to connections made with the openPort ( )
function, not to virtual ports.
\retval true if a port is open and
\retval false if the port is not open ( e.g. not opend or closed ) .
*/
bool isPortOpen ( void ) const;
//! Pure virtual function to set an error callback function to be invoked when an error has occured.
/*!
The callback function will be called whenever an error has occured. It is best
to set the error callback function before opening a port.
*/
void setErrorCallback ( ErrorInterface * callback );
//! A basic error reporting function for RtMidi classes.
void error ( Error e );
static constexpr const auto UNSPECIFIED = rtmidi::UNSPECIFIED;
static constexpr const auto MACOSX_CORE = rtmidi::MACOSX_CORE;
static constexpr const auto LINUX_ALSA = rtmidi::LINUX_ALSA;
static constexpr const auto UNIX_JACK = rtmidi::UNIX_JACK;
static constexpr const auto WINDOWS_MM = rtmidi::WINDOWS_MM;
static constexpr const auto RTMIDI_DUMMY = rtmidi::DUMMY;
typedef ApiType Api_t;
/* old functions */
RTMIDI_DEPRECATED ( typedef Api_t Api,
"enum RtMidi::Api has been replaced by enum rtmidi::ApiType" );
#if 0
#define Api Api_t
RTMIDI_DEPRECATED ( static void getCompiledApi ( std::vector<Api>& apis, bool
preferSystem
= true ) throw ( ), "enum RtMidi::Api has been replaced by enum rtmidi::ApiType" );
#endif
//! Compatibilty function for older code
virtual
RTMIDI_DEPRECATED ( void openVirtualPort ( const std::string& portName
= std::string ( "RtMidi virtual port" ) ),
"For better usability you should call this function from a derived class" ) = 0;
//! Pure virtual function to open a MIDI connection given by enumeration number.
/*! \param portNumber An optional port number greater than 0
can be specified. Otherwise, the default or first port
found is opened.
\param portName An optional name for the applicaction port
that will be generated to connect to portId can be
specified.
\deprecated
*/
RTMIDI_DEPRECATED ( void openPort ( unsigned int portNumber = 0,
const std::string& portName = std::string ( "RtMidi" )
), "Port numbers are unreliable. Use port descriptors instead ( see examples for a demonstration ) " );
//! Pure virtual to return the number of available MIDI ports of the current API.
/*!
\return This function returns the number of MIDI ports of
the selected API.
\note Only ports are counted that can be used with the
current API so an input API does ignore all output devices
and vice versa.
\sa getPortName
\deprecated
*/
RTMIDI_DEPRECATED ( unsigned int getPortCount ( ), "Port numbers are unreliable. Use port descriptors instead ( see examples for a demonstration ) " );
//! Pure virtual function to return a string identifier for the specified MIDI port number.
/*!
\param portNumber Number of the device to be referred to.
\return The name of the port with the given Id is returned.
\retval An empty string is returned if an invalid port specifier is provided.
\note Only ports are counted that can be used with the
current API so an input API does ignore all output devices
and vice versa.
\sa getPortCount ( )
\deprecated
*/
RTMIDI_DEPRECATED ( std::string getPortName ( unsigned int portNumber = 0 ), "Port numbers are unreliable. Use port descriptors instead ( see examples for a demonstration ) " );
//! Pure virtual function to set an error callback function to be invoked when an error has occured.
/*!
The callback function will be called whenever an error has occured. It is best
to set the error callback function before opening a port.
*/
RTMIDI_DEPRECATED ( void setErrorCallback ( ErrorCallback errorCallback = NULL, void * userData = 0 ), "setErrorCallback now expects an object of type ErrorInterface" );
protected:
MidiApi * rtapi_;
MidiApiList * list;
bool preferSystem;
std::string clientName;
Midi ( MidiApiList * l,
bool pfsystem,
const std::string& name );
virtual ~Midi ( );
};
inline RTMIDI_DEPRECATED ( std::string getApiName ( Midi::Api type ), "Use rtmidi::ApiType instead of RtMidi::Api" );
inline std::string getApiName ( Midi::Api type )
{
return Midi::getApiName ( ( ApiType ) type );
}
#undef RTMIDI_CLASSNAME
/**********************************************************************/
/*! \class MidiIn
\brief A realtime MIDI input class.
This class provides a common, platform-independent API for
realtime MIDI input. It allows access to a single MIDI input
port. Incoming MIDI messages are either saved to a queue for
retrieval using the getMessage ( ) function or immediately passed to
a user-specified callback function. Create multiple instances of
this class to connect to more than one MIDI device at the same
time. With the OS-X, Linux ALSA, and JACK MIDI APIs, it is also
possible to open a virtual input port to which other MIDI software
clients can connect.
by Gary P. Scavone, 2003-2017.
*/
/**********************************************************************/
// **************************************************************** //
//
// MidiIn and MidiOut class declarations.
//
// MidiIn / MidiOut are "controllers" used to select an available
// MIDI input or output interface. They present common APIs for the
// user to call but all functionality is implemented by the classes
// MidiInApi, MidiOutApi and their subclasses. MidiIn and MidiOut
// each create an instance of a MidiInApi or MidiOutApi subclass based
// on the user's API choice. If no choice is made, they attempt to
// make a "logical" API selection.
//
// **************************************************************** //
#define RTMIDI_CLASSNAME "MidiIn"
class RTMIDI_DLL_PUBLIC MidiIn : public Midi
{
public:
//! Default constructor that allows an optional api, client name and queue size.
/*!
An exception will be thrown if a MIDI system initialization
error occurs. The queue size defines the maximum number of
messages that can be held in the MIDI queue ( when not using a
callback function ) . If the queue size limit is reached,
incoming messages will be ignored.
If no API argument is specified and multiple API support has been
compiled, the default order of use is JACK, ALSA ( Linux ) and CORE,
JACK ( OS-X ) .
\param api An optional API id can be specified.
\param clientName An optional Client name can be specified. This
will be used to group the ports that are created
by the application.
\param queueSizeLimit An optional size of the MIDI input queue can be specified.
\param pfsystem An optional boolean parameter can be
provided to indicate the API preferences of the user
code. If RtMidi is requested to autoselect a backend
this parameter tells which backend should be tried
first. If it is \c true the backend will prefer OS
provieded APIs ( WinMM, ALSA, Core MIDI ) over other
APIs ( JACK ) . If \c false, the order will be vice
versa.
*/
MidiIn ( ApiType api=rtmidi::UNSPECIFIED,
const std::string& clientName = std::string ( "RtMidi Input Client" ),
unsigned int queueSizeLimit = 100,
bool pfsystem = true );
//! If a MIDI connection is still open, it will be closed by the destructor.
~MidiIn ( void ) throw ( );
//! Returns true if we can open virtual ports;
virtual bool hasVirtualPorts ( );
using Midi::openPort;
//! Open a MIDI connection given by a port descriptor.
/*!
\param port A port descriptor of the port must be specified.
\param portName An optional name for the applicaction port that is used to connect to portId can be specified.
*/
void openPort ( const PortDescriptor& port,
const std::string& portName = std::string ( "RtMidi" ) );
//! Open a MIDI connection given by a port descriptor pointer.
/*!
\param port A pointer to a port descriptor of the port must be specified.
\param portName An optional name for the applicaction port that is used to connect to portId can be specified.
\sa openPort ( const PortDescriptor &, const std::string& );
*/
void openPort ( Pointer<PortDescriptor> p,
const std::string& portName = std::string ( "RtMidi" ) );
//! Function to create a virtual port, with optional name.
/*!
This function creates a virtual MIDI port to which other
software applications can connect. This type of functionality
is currently only supported by the Macintosh OS-X, any JACK,
and Linux ALSA APIs ( the function returns an error for the other APIs ) .
\param portName An optional name for the application port that is
used to connect to portId can be specified.
*/
void openVirtualPort ( const std::string& portName = std::string ( "RtMidi virtual input port" ) );
//! Return a list of all available ports of the current API.
/*!
\param capabilities an optional parameter that
describes which capabilities the returned devices
must support. The returned devices may have
additional capabilities to those which have been
requested, but not less.
\return This function returns a list of port descriptors.
\note Each API will request additonal
capabilites. An output API will set always add \ref
PortDescriptor::OUTPUT to the mask while an input
device will always add \ref PortDescriptor::OUTPUT.
\note An input API may but need not necessarily
report output devices which cannot be used as input
if \ref PortDescriptor::OUTPUT is passed as \ref
capabilities parameter.
*/
PortList getPortList ( int capabilities = PortDescriptor::INPUT ) {
return Midi::getPortList ( capabilities );
}
//! Set a callback function to be invoked for incoming MIDI messages.
/*!
The callback function will be called whenever an incoming MIDI
message is received. While not absolutely necessary, it is best
to set the callback function before opening a MIDI port to avoid
leaving some messages in the queue.
\param callback A callback function must be given.
\param userData Opitionally, a pointer to additional data can be
passed to the callback function whenever it is called.
*/
void setCallback ( MidiInterface * callback );
//! Cancel use of the current callback function ( if one exists ) .
/*!
Subsequent incoming MIDI messages will be written to the queue
and can be retrieved with the \e getMessage function.
*/
void cancelCallback ( );
//! Specify whether certain MIDI message types should be queued or ignored during input.
/*!
By default, MIDI timing and active sensing messages are ignored
during message input because of their relative high data rates.
MIDI sysex messages are ignored by default as well. Variable
values of "true" imply that the respective message type will be
ignored.
*/
void ignoreTypes ( bool midiSysex = true,
bool midiTime = true,
bool midiSense = true );
//! Fill the user-provided vector with the data bytes for the next available MIDI message in the input queue and return the event delta-time in seconds.
/*!
This function returns immediately whether a new message is
available or not. A valid message is indicated by a non-zero
vector size. An exception is thrown if an error occurs during
message retrieval or an input connection was not previously
established.
*/
double getMessage ( std::vector<unsigned char>& message );
//! Set a callback function to be invoked for incoming MIDI messages.
/*!
The callback function will be called whenever an incoming MIDI
message is received. While not absolutely necessary, it is best
to set the callback function before opening a MIDI port to avoid
leaving some messages in the queue.
\param callback A callback function must be given.
\param userData Opitionally, a pointer to additional data can be
passed to the callback function whenever it is called.
*/
RTMIDI_DEPRECATED ( void setCallback ( MidiCallback callback, void * userData = 0 ),
"RtMidi now provides a type-safe MidiInterface class." );
//! Fill the user-provided vector with the data bytes for the next available MIDI message in the input queue and return the event delta-time in seconds.
/*!
This function returns immediately whether a new message is
available or not. A valid message is indicated by a non-zero
vector size. An exception is thrown if an error occurs during
message retrieval or an input connection was not previously
established.
\deprecated
*/
RTMIDI_DEPRECATED ( double getMessage ( std::vector<unsigned char> * message ),
"Please, use a C++ style reference to pass the message vector." );
protected:
static MidiApiList queryApis;
int queueSizeLimit;
void openMidiApi ( ApiType api );
};
#undef RTMIDI_CLASSNAME
/**********************************************************************/
/*! \class MidiOut
\brief A realtime MIDI output class.
This class provides a common, platform-independent API for MIDI
output. It allows one to probe available MIDI output ports, to
connect to one such port, and to send MIDI bytes immediately over
the connection. Create multiple instances of this class to
connect to more than one MIDI device at the same time. With the
OS-X, Linux ALSA and JACK MIDI APIs, it is also possible to open a
virtual port to which other MIDI software clients can connect.
by Gary P. Scavone, 2003-2017.
*/
/**********************************************************************/
#define RTMIDI_CLASSNAME "MidiOut"
class RTMIDI_DLL_PUBLIC MidiOut : public Midi
{
public:
//! Default constructor that allows an optional client name.
/*!
An exception will be thrown if a MIDI system initialization error occurs.
If no API argument is specified and multiple API support has been
compiled, the default order of use is JACK, ALSA ( Linux ) and CORE,
JACK ( OS-X ) .
\param api An optional API id can be specified.
\param clientName An optional Client name can be specified. This
will be used to group the ports that are created
by the application.
\param queueSizeLimit An optional size of the MIDI input queue can be specified.
\param pfsystem An optional boolean parameter can be
provided to indicate the API preferences of the user
code. If RtMidi is requested to autoselect a backend
this parameter tells which backend should be tried
first. If it is \c true the backend will prefer OS
provieded APIs ( WinMM, ALSA, Core MIDI ) over other
APIs ( JACK ) . If \c false, the order will be vice
versa.
*/
MidiOut ( ApiType api=rtmidi::UNSPECIFIED,
const std::string& clientName = std::string ( "RtMidi Output Client" ),
bool pfsystem = true );
//! The destructor closes any open MIDI connections.
~MidiOut ( void ) throw ( );
//! Returns true if we can open virtual ports;
virtual bool hasVirtualPorts ( );
using Midi::openPort;
//! Open a MIDI connection given by a port descriptor.
/*!
\param port A port descriptor of the port must be specified.
\param portName An optional name for the applicaction port that is used to connect to portId can be specified.
*/
void openPort ( const PortDescriptor& port,
const std::string& portName = std::string ( "RtMidi" ) );
//! Open a MIDI connection given by a port descriptor pointer.
/*!
\param port A pointer to a port descriptor of the port must be specified.
\param portName An optional name for the applicaction port that is used to connect to portId can be specified.
\sa openPort ( const PortDescriptor& , const std::string& );
*/
void openPort ( Pointer<PortDescriptor> p,
const std::string& portName = std::string ( "RtMidi" ) );
//! Function to create a virtual port, with optional name.
/*!
This function creates a virtual MIDI port to which other
software applications can connect. This type of functionality
is currently only supported by the Macintosh OS-X, any JACK,