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 28KB

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