Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtMidi.h 30KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /**********************************************************************/
  2. /*! \class RtMidi
  3. \brief An abstract base class for realtime MIDI input/output.
  4. This class implements some common functionality for the realtime
  5. MIDI input/output subclasses RtMidiIn and RtMidiOut.
  6. RtMidi WWW site: http://music.mcgill.ca/~gary/rtmidi/
  7. RtMidi: realtime MIDI i/o C++ classes
  8. Copyright (c) 2003-2017 Gary P. Scavone
  9. Permission is hereby granted, free of charge, to any person
  10. obtaining a copy of this software and associated documentation files
  11. (the "Software"), to deal in the Software without restriction,
  12. including without limitation the rights to use, copy, modify, merge,
  13. publish, distribute, sublicense, and/or sell copies of the Software,
  14. and to permit persons to whom the Software is furnished to do so,
  15. subject to the following conditions:
  16. The above copyright notice and this permission notice shall be
  17. included in all copies or substantial portions of the Software.
  18. Any person wishing to distribute modifications to the Software is
  19. asked to send the modifications to the original developer so that
  20. they can be incorporated into the canonical version. This is,
  21. however, not a binding provision of this license.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  25. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  26. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  27. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. /**********************************************************************/
  31. /*!
  32. \file RtMidi.h
  33. */
  34. #ifndef RTMIDI_H
  35. #define RTMIDI_H
  36. #define RTMIDI_DLL_PUBLIC
  37. #define RTMIDI_VERSION "3.0.0"
  38. #include <exception>
  39. #include <iostream>
  40. #include <string>
  41. #include <vector>
  42. /************************************************************************/
  43. /*! \class RtMidiError
  44. \brief Exception handling class for RtMidi.
  45. The RtMidiError class is quite simple but it does allow errors to be
  46. "caught" by RtMidiError::Type. See the RtMidi documentation to know
  47. which methods can throw an RtMidiError.
  48. */
  49. /************************************************************************/
  50. class RTMIDI_DLL_PUBLIC RtMidiError : public std::exception
  51. {
  52. public:
  53. //! Defined RtMidiError types.
  54. enum Type {
  55. WARNING, /*!< A non-critical error. */
  56. DEBUG_WARNING, /*!< A non-critical error which might be useful for debugging. */
  57. UNSPECIFIED, /*!< The default, unspecified error type. */
  58. NO_DEVICES_FOUND, /*!< No devices found on system. */
  59. INVALID_DEVICE, /*!< An invalid device ID was specified. */
  60. MEMORY_ERROR, /*!< An error occured during memory allocation. */
  61. INVALID_PARAMETER, /*!< An invalid parameter was specified to a function. */
  62. INVALID_USE, /*!< The function was called incorrectly. */
  63. DRIVER_ERROR, /*!< A system driver error occured. */
  64. SYSTEM_ERROR, /*!< A system error occured. */
  65. THREAD_ERROR /*!< A thread error occured. */
  66. };
  67. //! The constructor.
  68. RtMidiError( const std::string& message, Type type = RtMidiError::UNSPECIFIED ) throw() : message_(message), type_(type) {}
  69. //! The destructor.
  70. virtual ~RtMidiError( void ) throw() {}
  71. //! Prints thrown error message to stderr.
  72. virtual void printMessage( void ) const throw() { std::cerr << '\n' << message_ << "\n\n"; }
  73. //! Returns the thrown error message type.
  74. virtual const Type& getType(void) const throw() { return type_; }
  75. //! Returns the thrown error message string.
  76. virtual const std::string& getMessage(void) const throw() { return message_; }
  77. //! Returns the thrown error message as a c-style string.
  78. virtual const char* what( void ) const throw() { return message_.c_str(); }
  79. protected:
  80. std::string message_;
  81. Type type_;
  82. };
  83. //! RtMidi error callback function prototype.
  84. /*!
  85. \param type Type of error.
  86. \param errorText Error description.
  87. Note that class behaviour is undefined after a critical error (not
  88. a warning) is reported.
  89. */
  90. typedef void (*RtMidiErrorCallback)( RtMidiError::Type type, const std::string &errorText, void *userData );
  91. class MidiApi;
  92. class RTMIDI_DLL_PUBLIC RtMidi
  93. {
  94. public:
  95. //! MIDI API specifier arguments.
  96. enum Api {
  97. UNSPECIFIED, /*!< Search for a working compiled API. */
  98. MACOSX_CORE, /*!< Macintosh OS-X Core Midi API. */
  99. LINUX_ALSA, /*!< The Advanced Linux Sound Architecture API. */
  100. UNIX_JACK, /*!< The JACK Low-Latency MIDI Server API. */
  101. WINDOWS_MM, /*!< The Microsoft Multimedia MIDI API. */
  102. RTMIDI_DUMMY /*!< A compilable but non-functional API. */
  103. };
  104. //! A static function to determine the current RtMidi version.
  105. static std::string getVersion( void ) throw();
  106. //! A static function to determine the available compiled MIDI APIs.
  107. /*!
  108. The values returned in the std::vector can be compared against
  109. the enumerated list values. Note that there can be more than one
  110. API compiled for certain operating systems.
  111. */
  112. static void getCompiledApi( std::vector<RtMidi::Api> &apis ) throw();
  113. //! Pure virtual openPort() function.
  114. virtual void openPort( unsigned int portNumber = 0, const std::string &portName = std::string( "RtMidi" ) ) = 0;
  115. //! Pure virtual openVirtualPort() function.
  116. virtual void openVirtualPort( const std::string &portName = std::string( "RtMidi" ) ) = 0;
  117. //! Pure virtual getPortCount() function.
  118. virtual unsigned int getPortCount() = 0;
  119. //! Pure virtual getPortName() function.
  120. virtual std::string getPortName( unsigned int portNumber = 0 ) = 0;
  121. //! Pure virtual closePort() function.
  122. virtual void closePort( void ) = 0;
  123. //! Returns true if a port is open and false if not.
  124. /*!
  125. Note that this only applies to connections made with the openPort()
  126. function, not to virtual ports.
  127. */
  128. virtual bool isPortOpen( void ) const = 0;
  129. //! Set an error callback function to be invoked when an error has occured.
  130. /*!
  131. The callback function will be called whenever an error has occured. It is best
  132. to set the error callback function before opening a port.
  133. */
  134. virtual void setErrorCallback( RtMidiErrorCallback errorCallback = NULL, void *userData = 0 ) = 0;
  135. protected:
  136. RtMidi();
  137. virtual ~RtMidi();
  138. MidiApi *rtapi_;
  139. };
  140. /**********************************************************************/
  141. /*! \class RtMidiIn
  142. \brief A realtime MIDI input class.
  143. This class provides a common, platform-independent API for
  144. realtime MIDI input. It allows access to a single MIDI input
  145. port. Incoming MIDI messages are either saved to a queue for
  146. retrieval using the getMessage() function or immediately passed to
  147. a user-specified callback function. Create multiple instances of
  148. this class to connect to more than one MIDI device at the same
  149. time. With the OS-X, Linux ALSA, and JACK MIDI APIs, it is also
  150. possible to open a virtual input port to which other MIDI software
  151. clients can connect.
  152. by Gary P. Scavone, 2003-2017.
  153. */
  154. /**********************************************************************/
  155. // **************************************************************** //
  156. //
  157. // RtMidiIn and RtMidiOut class declarations.
  158. //
  159. // RtMidiIn / RtMidiOut are "controllers" used to select an available
  160. // MIDI input or output interface. They present common APIs for the
  161. // user to call but all functionality is implemented by the classes
  162. // MidiInApi, MidiOutApi and their subclasses. RtMidiIn and RtMidiOut
  163. // each create an instance of a MidiInApi or MidiOutApi subclass based
  164. // on the user's API choice. If no choice is made, they attempt to
  165. // make a "logical" API selection.
  166. //
  167. // **************************************************************** //
  168. class RTMIDI_DLL_PUBLIC RtMidiIn : public RtMidi
  169. {
  170. public:
  171. //! User callback function type definition.
  172. typedef void (*RtMidiCallback)( double timeStamp, std::vector<unsigned char> *message, void *userData);
  173. //! Default constructor that allows an optional api, client name and queue size.
  174. /*!
  175. An exception will be thrown if a MIDI system initialization
  176. error occurs. The queue size defines the maximum number of
  177. messages that can be held in the MIDI queue (when not using a
  178. callback function). If the queue size limit is reached,
  179. incoming messages will be ignored.
  180. If no API argument is specified and multiple API support has been
  181. compiled, the default order of use is ALSA, JACK (Linux) and CORE,
  182. JACK (OS-X).
  183. \param api An optional API id can be specified.
  184. \param clientName An optional client name can be specified. This
  185. will be used to group the ports that are created
  186. by the application.
  187. \param queueSizeLimit An optional size of the MIDI input queue can be specified.
  188. */
  189. RtMidiIn( RtMidi::Api api=UNSPECIFIED,
  190. const std::string& clientName = "RtMidi Input Client",
  191. unsigned int queueSizeLimit = 100 );
  192. //! If a MIDI connection is still open, it will be closed by the destructor.
  193. ~RtMidiIn ( void ) throw();
  194. //! Returns the MIDI API specifier for the current instance of RtMidiIn.
  195. RtMidi::Api getCurrentApi( void ) throw();
  196. //! Open a MIDI input connection given by enumeration number.
  197. /*!
  198. \param portNumber An optional port number greater than 0 can be specified.
  199. Otherwise, the default or first port found is opened.
  200. \param portName An optional name for the application port that is used to connect to portId can be specified.
  201. */
  202. void openPort( unsigned int portNumber = 0, const std::string &portName = std::string( "RtMidi Input" ) );
  203. //! Create a virtual input port, with optional name, to allow software connections (OS X, JACK and ALSA only).
  204. /*!
  205. This function creates a virtual MIDI input port to which other
  206. software applications can connect. This type of functionality
  207. is currently only supported by the Macintosh OS-X, any JACK,
  208. and Linux ALSA APIs (the function returns an error for the other APIs).
  209. \param portName An optional name for the application port that is
  210. used to connect to portId can be specified.
  211. */
  212. void openVirtualPort( const std::string &portName = std::string( "RtMidi Input" ) );
  213. //! Set a callback function to be invoked for incoming MIDI messages.
  214. /*!
  215. The callback function will be called whenever an incoming MIDI
  216. message is received. While not absolutely necessary, it is best
  217. to set the callback function before opening a MIDI port to avoid
  218. leaving some messages in the queue.
  219. \param callback A callback function must be given.
  220. \param userData Optionally, a pointer to additional data can be
  221. passed to the callback function whenever it is called.
  222. */
  223. void setCallback( RtMidiCallback callback, void *userData = 0 );
  224. //! Cancel use of the current callback function (if one exists).
  225. /*!
  226. Subsequent incoming MIDI messages will be written to the queue
  227. and can be retrieved with the \e getMessage function.
  228. */
  229. void cancelCallback();
  230. //! Close an open MIDI connection (if one exists).
  231. void closePort( void );
  232. //! Returns true if a port is open and false if not.
  233. /*!
  234. Note that this only applies to connections made with the openPort()
  235. function, not to virtual ports.
  236. */
  237. virtual bool isPortOpen() const;
  238. //! Return the number of available MIDI input ports.
  239. /*!
  240. \return This function returns the number of MIDI ports of the selected API.
  241. */
  242. unsigned int getPortCount();
  243. //! Return a string identifier for the specified MIDI input port number.
  244. /*!
  245. \return The name of the port with the given Id is returned.
  246. \retval An empty string is returned if an invalid port specifier
  247. is provided. User code should assume a UTF-8 encoding.
  248. */
  249. std::string getPortName( unsigned int portNumber = 0 );
  250. //! Specify whether certain MIDI message types should be queued or ignored during input.
  251. /*!
  252. By default, MIDI timing and active sensing messages are ignored
  253. during message input because of their relative high data rates.
  254. MIDI sysex messages are ignored by default as well. Variable
  255. values of "true" imply that the respective message type will be
  256. ignored.
  257. */
  258. void ignoreTypes( bool midiSysex = true, bool midiTime = true, bool midiSense = true );
  259. //! 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.
  260. /*!
  261. This function returns immediately whether a new message is
  262. available or not. A valid message is indicated by a non-zero
  263. vector size. An exception is thrown if an error occurs during
  264. message retrieval or an input connection was not previously
  265. established.
  266. */
  267. double getMessage( std::vector<unsigned char> *message );
  268. //! Set an error callback function to be invoked when an error has occured.
  269. /*!
  270. The callback function will be called whenever an error has occured. It is best
  271. to set the error callback function before opening a port.
  272. */
  273. virtual void setErrorCallback( RtMidiErrorCallback errorCallback = NULL, void *userData = 0 );
  274. protected:
  275. void openMidiApi( RtMidi::Api api, const std::string &clientName, unsigned int queueSizeLimit );
  276. };
  277. /**********************************************************************/
  278. /*! \class RtMidiOut
  279. \brief A realtime MIDI output class.
  280. This class provides a common, platform-independent API for MIDI
  281. output. It allows one to probe available MIDI output ports, to
  282. connect to one such port, and to send MIDI bytes immediately over
  283. the connection. Create multiple instances of this class to
  284. connect to more than one MIDI device at the same time. With the
  285. OS-X, Linux ALSA and JACK MIDI APIs, it is also possible to open a
  286. virtual port to which other MIDI software clients can connect.
  287. by Gary P. Scavone, 2003-2017.
  288. */
  289. /**********************************************************************/
  290. class RTMIDI_DLL_PUBLIC RtMidiOut : public RtMidi
  291. {
  292. public:
  293. //! Default constructor that allows an optional client name.
  294. /*!
  295. An exception will be thrown if a MIDI system initialization error occurs.
  296. If no API argument is specified and multiple API support has been
  297. compiled, the default order of use is ALSA, JACK (Linux) and CORE,
  298. JACK (OS-X).
  299. */
  300. RtMidiOut( RtMidi::Api api=UNSPECIFIED,
  301. const std::string& clientName = "RtMidi Output Client" );
  302. //! The destructor closes any open MIDI connections.
  303. ~RtMidiOut( void ) throw();
  304. //! Returns the MIDI API specifier for the current instance of RtMidiOut.
  305. RtMidi::Api getCurrentApi( void ) throw();
  306. //! Open a MIDI output connection.
  307. /*!
  308. An optional port number greater than 0 can be specified.
  309. Otherwise, the default or first port found is opened. An
  310. exception is thrown if an error occurs while attempting to make
  311. the port connection.
  312. */
  313. void openPort( unsigned int portNumber = 0, const std::string &portName = std::string( "RtMidi Output" ) );
  314. //! Close an open MIDI connection (if one exists).
  315. void closePort( void );
  316. //! Returns true if a port is open and false if not.
  317. /*!
  318. Note that this only applies to connections made with the openPort()
  319. function, not to virtual ports.
  320. */
  321. virtual bool isPortOpen() const;
  322. //! Create a virtual output port, with optional name, to allow software connections (OS X, JACK and ALSA only).
  323. /*!
  324. This function creates a virtual MIDI output port to which other
  325. software applications can connect. This type of functionality
  326. is currently only supported by the Macintosh OS-X, Linux ALSA
  327. and JACK APIs (the function does nothing with the other APIs).
  328. An exception is thrown if an error occurs while attempting to
  329. create the virtual port.
  330. */
  331. void openVirtualPort( const std::string &portName = std::string( "RtMidi Output" ) );
  332. //! Return the number of available MIDI output ports.
  333. unsigned int getPortCount( void );
  334. //! Return a string identifier for the specified MIDI port type and number.
  335. /*!
  336. \return The name of the port with the given Id is returned.
  337. \retval An empty string is returned if an invalid port specifier
  338. is provided. User code should assume a UTF-8 encoding.
  339. */
  340. std::string getPortName( unsigned int portNumber = 0 );
  341. //! Immediately send a single message out an open MIDI output port.
  342. /*!
  343. An exception is thrown if an error occurs during output or an
  344. output connection was not previously established.
  345. */
  346. void sendMessage( const std::vector<unsigned char> *message );
  347. //! Immediately send a single message out an open MIDI output port.
  348. /*!
  349. An exception is thrown if an error occurs during output or an
  350. output connection was not previously established.
  351. \param message A pointer to the MIDI message as raw bytes
  352. \param size Length of the MIDI message in bytes
  353. */
  354. void sendMessage( const unsigned char *message, size_t size );
  355. //! Set an error callback function to be invoked when an error has occured.
  356. /*!
  357. The callback function will be called whenever an error has occured. It is best
  358. to set the error callback function before opening a port.
  359. */
  360. virtual void setErrorCallback( RtMidiErrorCallback errorCallback = NULL, void *userData = 0 );
  361. protected:
  362. void openMidiApi( RtMidi::Api api, const std::string &clientName );
  363. };
  364. // **************************************************************** //
  365. //
  366. // MidiInApi / MidiOutApi class declarations.
  367. //
  368. // Subclasses of MidiInApi and MidiOutApi contain all API- and
  369. // OS-specific code necessary to fully implement the RtMidi API.
  370. //
  371. // Note that MidiInApi and MidiOutApi are abstract base classes and
  372. // cannot be explicitly instantiated. RtMidiIn and RtMidiOut will
  373. // create instances of a MidiInApi or MidiOutApi subclass.
  374. //
  375. // **************************************************************** //
  376. class RTMIDI_DLL_PUBLIC MidiApi
  377. {
  378. public:
  379. MidiApi();
  380. virtual ~MidiApi();
  381. virtual RtMidi::Api getCurrentApi( void ) = 0;
  382. virtual void openPort( unsigned int portNumber, const std::string &portName ) = 0;
  383. virtual void openVirtualPort( const std::string &portName ) = 0;
  384. virtual void closePort( void ) = 0;
  385. virtual unsigned int getPortCount( void ) = 0;
  386. virtual std::string getPortName( unsigned int portNumber ) = 0;
  387. inline bool isPortOpen() const { return connected_; }
  388. void setErrorCallback( RtMidiErrorCallback errorCallback, void *userData );
  389. //! A basic error reporting function for RtMidi classes.
  390. void error( RtMidiError::Type type, std::string errorString );
  391. protected:
  392. virtual void initialize( const std::string& clientName ) = 0;
  393. void *apiData_;
  394. bool connected_;
  395. std::string errorString_;
  396. RtMidiErrorCallback errorCallback_;
  397. bool firstErrorOccurred_;
  398. void *errorCallbackUserData_;
  399. };
  400. class RTMIDI_DLL_PUBLIC MidiInApi : public MidiApi
  401. {
  402. public:
  403. MidiInApi( unsigned int queueSizeLimit );
  404. virtual ~MidiInApi( void );
  405. void setCallback( RtMidiIn::RtMidiCallback callback, void *userData );
  406. void cancelCallback( void );
  407. virtual void ignoreTypes( bool midiSysex, bool midiTime, bool midiSense );
  408. double getMessage( std::vector<unsigned char> *message );
  409. // A MIDI structure used internally by the class to store incoming
  410. // messages. Each message represents one and only one MIDI message.
  411. struct MidiMessage {
  412. std::vector<unsigned char> bytes;
  413. //! Time in seconds elapsed since the previous message
  414. double timeStamp;
  415. // Default constructor.
  416. MidiMessage()
  417. :bytes(0), timeStamp(0.0) {}
  418. };
  419. struct MidiQueue {
  420. unsigned int front;
  421. unsigned int back;
  422. unsigned int ringSize;
  423. MidiMessage *ring;
  424. // Default constructor.
  425. MidiQueue()
  426. :front(0), back(0), ringSize(0), ring(0) {}
  427. bool push(const MidiMessage&);
  428. bool pop(std::vector<unsigned char>*, double*);
  429. unsigned int size(unsigned int *back=0,
  430. unsigned int *front=0);
  431. };
  432. // The RtMidiInData structure is used to pass private class data to
  433. // the MIDI input handling function or thread.
  434. struct RtMidiInData {
  435. MidiQueue queue;
  436. MidiMessage message;
  437. unsigned char ignoreFlags;
  438. bool doInput;
  439. bool firstMessage;
  440. void *apiData;
  441. bool usingCallback;
  442. RtMidiIn::RtMidiCallback userCallback;
  443. void *userData;
  444. bool continueSysex;
  445. // Default constructor.
  446. RtMidiInData()
  447. : ignoreFlags(7), doInput(false), firstMessage(true),
  448. apiData(0), usingCallback(false), userCallback(0), userData(0),
  449. continueSysex(false) {}
  450. };
  451. protected:
  452. RtMidiInData inputData_;
  453. };
  454. class RTMIDI_DLL_PUBLIC MidiOutApi : public MidiApi
  455. {
  456. public:
  457. MidiOutApi( void );
  458. virtual ~MidiOutApi( void );
  459. virtual void sendMessage( const unsigned char *message, size_t size ) = 0;
  460. };
  461. // **************************************************************** //
  462. //
  463. // Inline RtMidiIn and RtMidiOut definitions.
  464. //
  465. // **************************************************************** //
  466. inline RtMidi::Api RtMidiIn :: getCurrentApi( void ) throw() { return rtapi_->getCurrentApi(); }
  467. inline void RtMidiIn :: openPort( unsigned int portNumber, const std::string &portName ) { rtapi_->openPort( portNumber, portName ); }
  468. inline void RtMidiIn :: openVirtualPort( const std::string &portName ) { rtapi_->openVirtualPort( portName ); }
  469. inline void RtMidiIn :: closePort( void ) { rtapi_->closePort(); }
  470. inline bool RtMidiIn :: isPortOpen() const { return rtapi_->isPortOpen(); }
  471. inline void RtMidiIn :: setCallback( RtMidiCallback callback, void *userData ) { ((MidiInApi *)rtapi_)->setCallback( callback, userData ); }
  472. inline void RtMidiIn :: cancelCallback( void ) { ((MidiInApi *)rtapi_)->cancelCallback(); }
  473. inline unsigned int RtMidiIn :: getPortCount( void ) { return rtapi_->getPortCount(); }
  474. inline std::string RtMidiIn :: getPortName( unsigned int portNumber ) { return rtapi_->getPortName( portNumber ); }
  475. inline void RtMidiIn :: ignoreTypes( bool midiSysex, bool midiTime, bool midiSense ) { ((MidiInApi *)rtapi_)->ignoreTypes( midiSysex, midiTime, midiSense ); }
  476. inline double RtMidiIn :: getMessage( std::vector<unsigned char> *message ) { return ((MidiInApi *)rtapi_)->getMessage( message ); }
  477. inline void RtMidiIn :: setErrorCallback( RtMidiErrorCallback errorCallback, void *userData ) { rtapi_->setErrorCallback(errorCallback, userData); }
  478. inline RtMidi::Api RtMidiOut :: getCurrentApi( void ) throw() { return rtapi_->getCurrentApi(); }
  479. inline void RtMidiOut :: openPort( unsigned int portNumber, const std::string &portName ) { rtapi_->openPort( portNumber, portName ); }
  480. inline void RtMidiOut :: openVirtualPort( const std::string &portName ) { rtapi_->openVirtualPort( portName ); }
  481. inline void RtMidiOut :: closePort( void ) { rtapi_->closePort(); }
  482. inline bool RtMidiOut :: isPortOpen() const { return rtapi_->isPortOpen(); }
  483. inline unsigned int RtMidiOut :: getPortCount( void ) { return rtapi_->getPortCount(); }
  484. inline std::string RtMidiOut :: getPortName( unsigned int portNumber ) { return rtapi_->getPortName( portNumber ); }
  485. inline void RtMidiOut :: sendMessage( const std::vector<unsigned char> *message ) { ((MidiOutApi *)rtapi_)->sendMessage( &message->at(0), message->size() ); }
  486. inline void RtMidiOut :: sendMessage( const unsigned char *message, size_t size ) { ((MidiOutApi *)rtapi_)->sendMessage( message, size ); }
  487. inline void RtMidiOut :: setErrorCallback( RtMidiErrorCallback errorCallback, void *userData ) { rtapi_->setErrorCallback(errorCallback, userData); }
  488. // **************************************************************** //
  489. //
  490. // MidiInApi and MidiOutApi subclass prototypes.
  491. //
  492. // **************************************************************** //
  493. #if !defined(__LINUX_ALSA__) && !defined(__UNIX_JACK__) && !defined(__MACOSX_CORE__) && !defined(__WINDOWS_MM__)
  494. #define __RTMIDI_DUMMY__
  495. #endif
  496. #if defined(__MACOSX_CORE__)
  497. class MidiInCore: public MidiInApi
  498. {
  499. public:
  500. MidiInCore( const std::string &clientName, unsigned int queueSizeLimit );
  501. ~MidiInCore( void );
  502. RtMidi::Api getCurrentApi( void ) { return RtMidi::MACOSX_CORE; };
  503. void openPort( unsigned int portNumber, const std::string &portName );
  504. void openVirtualPort( const std::string &portName );
  505. void closePort( void );
  506. unsigned int getPortCount( void );
  507. std::string getPortName( unsigned int portNumber );
  508. protected:
  509. void initialize( const std::string& clientName );
  510. };
  511. class MidiOutCore: public MidiOutApi
  512. {
  513. public:
  514. MidiOutCore( const std::string &clientName );
  515. ~MidiOutCore( void );
  516. RtMidi::Api getCurrentApi( void ) { return RtMidi::MACOSX_CORE; };
  517. void openPort( unsigned int portNumber, const std::string &portName );
  518. void openVirtualPort( const std::string &portName );
  519. void closePort( void );
  520. unsigned int getPortCount( void );
  521. std::string getPortName( unsigned int portNumber );
  522. void sendMessage( const unsigned char *message, size_t size );
  523. protected:
  524. void initialize( const std::string& clientName );
  525. };
  526. #endif
  527. #if defined(__UNIX_JACK__)
  528. class MidiInJack: public MidiInApi
  529. {
  530. public:
  531. MidiInJack( const std::string &clientName, unsigned int queueSizeLimit );
  532. ~MidiInJack( void );
  533. RtMidi::Api getCurrentApi( void ) { return RtMidi::UNIX_JACK; };
  534. void openPort( unsigned int portNumber, const std::string &portName );
  535. void openVirtualPort( const std::string &portName );
  536. void closePort( void );
  537. unsigned int getPortCount( void );
  538. std::string getPortName( unsigned int portNumber );
  539. protected:
  540. std::string clientName;
  541. void connect( void );
  542. void initialize( const std::string& clientName );
  543. };
  544. class MidiOutJack: public MidiOutApi
  545. {
  546. public:
  547. MidiOutJack( const std::string &clientName );
  548. ~MidiOutJack( void );
  549. RtMidi::Api getCurrentApi( void ) { return RtMidi::UNIX_JACK; };
  550. void openPort( unsigned int portNumber, const std::string &portName );
  551. void openVirtualPort( const std::string &portName );
  552. void closePort( void );
  553. unsigned int getPortCount( void );
  554. std::string getPortName( unsigned int portNumber );
  555. void sendMessage( const unsigned char *message, size_t size );
  556. protected:
  557. std::string clientName;
  558. void connect( void );
  559. void initialize( const std::string& clientName );
  560. };
  561. #endif
  562. #if defined(__LINUX_ALSA__)
  563. class MidiInAlsa: public MidiInApi
  564. {
  565. public:
  566. MidiInAlsa( const std::string &clientName, unsigned int queueSizeLimit );
  567. ~MidiInAlsa( void );
  568. RtMidi::Api getCurrentApi( void ) { return RtMidi::LINUX_ALSA; };
  569. void openPort( unsigned int portNumber, const std::string &portName );
  570. void openVirtualPort( const std::string &portName );
  571. void closePort( void );
  572. unsigned int getPortCount( void );
  573. std::string getPortName( unsigned int portNumber );
  574. protected:
  575. void initialize( const std::string& clientName );
  576. };
  577. class MidiOutAlsa: public MidiOutApi
  578. {
  579. public:
  580. MidiOutAlsa( const std::string &clientName );
  581. ~MidiOutAlsa( void );
  582. RtMidi::Api getCurrentApi( void ) { return RtMidi::LINUX_ALSA; };
  583. void openPort( unsigned int portNumber, const std::string &portName );
  584. void openVirtualPort( const std::string &portName );
  585. void closePort( void );
  586. unsigned int getPortCount( void );
  587. std::string getPortName( unsigned int portNumber );
  588. void sendMessage( const unsigned char *message, size_t size );
  589. protected:
  590. void initialize( const std::string& clientName );
  591. };
  592. #endif
  593. #if defined(__WINDOWS_MM__)
  594. class MidiInWinMM: public MidiInApi
  595. {
  596. public:
  597. MidiInWinMM( const std::string &clientName, unsigned int queueSizeLimit );
  598. ~MidiInWinMM( void );
  599. RtMidi::Api getCurrentApi( void ) { return RtMidi::WINDOWS_MM; };
  600. void openPort( unsigned int portNumber, const std::string &portName );
  601. void openVirtualPort( const std::string &portName );
  602. void closePort( void );
  603. unsigned int getPortCount( void );
  604. std::string getPortName( unsigned int portNumber );
  605. protected:
  606. void initialize( const std::string& clientName );
  607. };
  608. class MidiOutWinMM: public MidiOutApi
  609. {
  610. public:
  611. MidiOutWinMM( const std::string &clientName );
  612. ~MidiOutWinMM( void );
  613. RtMidi::Api getCurrentApi( void ) { return RtMidi::WINDOWS_MM; };
  614. void openPort( unsigned int portNumber, const std::string &portName );
  615. void openVirtualPort( const std::string &portName );
  616. void closePort( void );
  617. unsigned int getPortCount( void );
  618. std::string getPortName( unsigned int portNumber );
  619. void sendMessage( const unsigned char *message, size_t size );
  620. protected:
  621. void initialize( const std::string& clientName );
  622. };
  623. #endif
  624. #if defined(__RTMIDI_DUMMY__)
  625. class MidiInDummy: public MidiInApi
  626. {
  627. public:
  628. MidiInDummy( const std::string &/*clientName*/, unsigned int queueSizeLimit ) : MidiInApi( queueSizeLimit ) { errorString_ = "MidiInDummy: This class provides no functionality."; error( RtMidiError::WARNING, errorString_ ); }
  629. RtMidi::Api getCurrentApi( void ) { return RtMidi::RTMIDI_DUMMY; }
  630. void openPort( unsigned int /*portNumber*/, const std::string &/*portName*/ ) {}
  631. void openVirtualPort( const std::string &/*portName*/ ) {}
  632. void closePort( void ) {}
  633. unsigned int getPortCount( void ) { return 0; }
  634. std::string getPortName( unsigned int /*portNumber*/ ) { return ""; }
  635. protected:
  636. void initialize( const std::string& /*clientName*/ ) {}
  637. };
  638. class MidiOutDummy: public MidiOutApi
  639. {
  640. public:
  641. MidiOutDummy( const std::string &/*clientName*/ ) { errorString_ = "MidiOutDummy: This class provides no functionality."; error( RtMidiError::WARNING, errorString_ ); }
  642. RtMidi::Api getCurrentApi( void ) { return RtMidi::RTMIDI_DUMMY; }
  643. void openPort( unsigned int /*portNumber*/, const std::string &/*portName*/ ) {}
  644. void openVirtualPort( const std::string &/*portName*/ ) {}
  645. void closePort( void ) {}
  646. unsigned int getPortCount( void ) { return 0; }
  647. std::string getPortName( unsigned int /*portNumber*/ ) { return ""; }
  648. void sendMessage( const unsigned char * /*message*/, size_t /*size*/ ) {}
  649. protected:
  650. void initialize( const std::string& /*clientName*/ ) {}
  651. };
  652. #endif
  653. #endif