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