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.

805 lines
30KB

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