RtMidi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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-2011 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. requested to send the modifications to the original developer so that
  20. they can be incorporated into the canonical version.
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  25. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  26. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. /**********************************************************************/
  30. // RtMidi: Version 1.0.12
  31. #ifndef RTMIDI_H
  32. #define RTMIDI_H
  33. #include "RtError.h"
  34. #include <string>
  35. class RtMidi
  36. {
  37. public:
  38. //! Pure virtual openPort() function.
  39. virtual void openPort( unsigned int portNumber = 0, const std::string portName = std::string( "RtMidi" ) ) = 0;
  40. //! Pure virtual openVirtualPort() function.
  41. virtual void openVirtualPort( const std::string portName = std::string( "RtMidi" ) ) = 0;
  42. //! Pure virtual getPortCount() function.
  43. virtual unsigned int getPortCount() = 0;
  44. //! Pure virtual getPortName() function.
  45. virtual std::string getPortName( unsigned int portNumber = 0 ) = 0;
  46. //! Pure virtual closePort() function.
  47. virtual void closePort( void ) = 0;
  48. protected:
  49. RtMidi();
  50. virtual ~RtMidi() {};
  51. // A basic error reporting function for internal use in the RtMidi
  52. // subclasses. The behavior of this function can be modified to
  53. // suit specific needs.
  54. void error( RtError::Type type );
  55. void *apiData_;
  56. bool connected_;
  57. std::string errorString_;
  58. };
  59. /**********************************************************************/
  60. /*! \class RtMidiIn
  61. \brief A realtime MIDI input class.
  62. This class provides a common, platform-independent API for
  63. realtime MIDI input. It allows access to a single MIDI input
  64. port. Incoming MIDI messages are either saved to a queue for
  65. retrieval using the getMessage() function or immediately passed to
  66. a user-specified callback function. Create multiple instances of
  67. this class to connect to more than one MIDI device at the same
  68. time. With the OS-X and Linux ALSA MIDI APIs, it is also possible
  69. to open a virtual input port to which other MIDI software clients
  70. can connect.
  71. by Gary P. Scavone, 2003-2008.
  72. */
  73. /**********************************************************************/
  74. #include <vector>
  75. #include <queue>
  76. class RtMidiIn : public RtMidi
  77. {
  78. public:
  79. //! User callback function type definition.
  80. typedef void (*RtMidiCallback)( double timeStamp, std::vector<unsigned char> *message, void *userData);
  81. //! Default constructor that allows an optional client name.
  82. /*!
  83. An exception will be thrown if a MIDI system initialization error occurs.
  84. */
  85. RtMidiIn( const std::string clientName = std::string( "RtMidi Input Client") );
  86. //! If a MIDI connection is still open, it will be closed by the destructor.
  87. ~RtMidiIn();
  88. //! Open a MIDI input connection.
  89. /*!
  90. An optional port number greater than 0 can be specified.
  91. Otherwise, the default or first port found is opened.
  92. */
  93. void openPort( unsigned int portNumber = 0, const std::string Portname = std::string( "RtMidi Input" ) );
  94. //! Create a virtual input port, with optional name, to allow software connections (OS X and ALSA only).
  95. /*!
  96. This function creates a virtual MIDI input port to which other
  97. software applications can connect. This type of functionality
  98. is currently only supported by the Macintosh OS-X and Linux ALSA
  99. APIs (the function does nothing for the other APIs).
  100. */
  101. void openVirtualPort( const std::string portName = std::string( "RtMidi Input" ) );
  102. //! Set a callback function to be invoked for incoming MIDI messages.
  103. /*!
  104. The callback function will be called whenever an incoming MIDI
  105. message is received. While not absolutely necessary, it is best
  106. to set the callback function before opening a MIDI port to avoid
  107. leaving some messages in the queue.
  108. */
  109. void setCallback( RtMidiCallback callback, void *userData = 0 );
  110. //! Cancel use of the current callback function (if one exists).
  111. /*!
  112. Subsequent incoming MIDI messages will be written to the queue
  113. and can be retrieved with the \e getMessage function.
  114. */
  115. void cancelCallback();
  116. //! Close an open MIDI connection (if one exists).
  117. void closePort( void );
  118. //! Return the number of available MIDI input ports.
  119. unsigned int getPortCount();
  120. //! Return a string identifier for the specified MIDI input port number.
  121. /*!
  122. An empty string is returned if an invalid port specifier is provided.
  123. */
  124. std::string getPortName( unsigned int portNumber = 0 );
  125. //! Set the maximum number of MIDI messages to be saved in the queue.
  126. /*!
  127. If the queue size limit is reached, incoming messages will be
  128. ignored. The default limit is 1024.
  129. */
  130. void setQueueSizeLimit( unsigned int queueSize );
  131. //! Specify whether certain MIDI message types should be queued or ignored during input.
  132. /*!
  133. By default, MIDI timing and active sensing messages are ignored
  134. during message input because of their relative high data rates.
  135. MIDI sysex messages are ignored by default as well. Variable
  136. values of "true" imply that the respective message type will be
  137. ignored.
  138. */
  139. void ignoreTypes( bool midiSysex = true, bool midiTime = true, bool midiSense = true );
  140. //! 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.
  141. /*!
  142. This function returns immediately whether a new message is
  143. available or not. A valid message is indicated by a non-zero
  144. vector size. An exception is thrown if an error occurs during
  145. message retrieval or an input connection was not previously
  146. established.
  147. */
  148. double getMessage( std::vector<unsigned char> *message );
  149. // A MIDI structure used internally by the class to store incoming
  150. // messages. Each message represents one and only one MIDI message.
  151. struct MidiMessage {
  152. std::vector<unsigned char> bytes;
  153. double timeStamp;
  154. // Default constructor.
  155. MidiMessage()
  156. :bytes(3), timeStamp(0.0) {}
  157. };
  158. // The RtMidiInData structure is used to pass private class data to
  159. // the MIDI input handling function or thread.
  160. struct RtMidiInData {
  161. std::queue<MidiMessage> queue;
  162. MidiMessage message;
  163. unsigned int queueLimit;
  164. unsigned char ignoreFlags;
  165. bool doInput;
  166. bool firstMessage;
  167. void *apiData;
  168. bool usingCallback;
  169. void *userCallback;
  170. void *userData;
  171. bool continueSysex;
  172. // Default constructor.
  173. RtMidiInData()
  174. : queueLimit(1024), ignoreFlags(7), doInput(false), firstMessage(true),
  175. apiData(0), usingCallback(false), userCallback(0), userData(0),
  176. continueSysex(false) {}
  177. };
  178. private:
  179. void initialize( const std::string& clientName );
  180. RtMidiInData inputData_;
  181. };
  182. /**********************************************************************/
  183. /*! \class RtMidiOut
  184. \brief A realtime MIDI output class.
  185. This class provides a common, platform-independent API for MIDI
  186. output. It allows one to probe available MIDI output ports, to
  187. connect to one such port, and to send MIDI bytes immediately over
  188. the connection. Create multiple instances of this class to
  189. connect to more than one MIDI device at the same time.
  190. by Gary P. Scavone, 2003-2008.
  191. */
  192. /**********************************************************************/
  193. class RtMidiOut : public RtMidi
  194. {
  195. public:
  196. //! Default constructor that allows an optional client name.
  197. /*!
  198. An exception will be thrown if a MIDI system initialization error occurs.
  199. */
  200. RtMidiOut( const std::string clientName = std::string( "RtMidi Output Client" ) );
  201. //! The destructor closes any open MIDI connections.
  202. ~RtMidiOut();
  203. //! Open a MIDI output connection.
  204. /*!
  205. An optional port number greater than 0 can be specified.
  206. Otherwise, the default or first port found is opened. An
  207. exception is thrown if an error occurs while attempting to make
  208. the port connection.
  209. */
  210. void openPort( unsigned int portNumber = 0, const std::string portName = std::string( "RtMidi Output" ) );
  211. //! Close an open MIDI connection (if one exists).
  212. void closePort();
  213. //! Create a virtual output port, with optional name, to allow software connections (OS X and ALSA only).
  214. /*!
  215. This function creates a virtual MIDI output port to which other
  216. software applications can connect. This type of functionality
  217. is currently only supported by the Macintosh OS-X and Linux ALSA
  218. APIs (the function does nothing with the other APIs). An
  219. exception is thrown if an error occurs while attempting to create
  220. the virtual port.
  221. */
  222. void openVirtualPort( const std::string portName = std::string( "RtMidi Output" ) );
  223. //! Return the number of available MIDI output ports.
  224. unsigned int getPortCount();
  225. //! Return a string identifier for the specified MIDI port type and number.
  226. /*!
  227. An empty string is returned if an invalid port specifier is provided.
  228. */
  229. std::string getPortName( unsigned int portNumber = 0 );
  230. //! Immediately send a single message out an open MIDI output port.
  231. /*!
  232. An exception is thrown if an error occurs during output or an
  233. output connection was not previously established.
  234. */
  235. void sendMessage( std::vector<unsigned char> *message );
  236. private:
  237. void initialize( const std::string& clientName );
  238. };
  239. #endif