Tutorial   Class/Enum List   File List   Compound Members   C interface  

Public Types | Public Member Functions | List of all members
rt::midi::RtMidiIn Class Reference

A realtime MIDI input class. More...

#include <RtMidi.h>

Inheritance diagram for rt::midi::RtMidiIn:
rt::midi::RtMidi

Public Types

typedef void(* RtMidiCallback) (double timeStamp, std::vector< unsigned char > *message, void *userData)
 User callback function type definition.
 
- Public Types inherited from rt::midi::RtMidi
enum  Api {
  UNSPECIFIED , MACOSX_CORE , LINUX_ALSA , UNIX_JACK ,
  WINDOWS_MM , RTMIDI_DUMMY , WEB_MIDI_API , WINDOWS_UWP ,
  ANDROID_AMIDI , WINDOWS_MIDI_SERVICES , NUM_APIS
}
 MIDI API specifier arguments. More...
 

Public Member Functions

 RtMidiIn (RtMidi::Api api=UNSPECIFIED, const std::string &clientName="RtMidi Input Client", unsigned int queueSizeLimit=100)
 Default constructor that allows an optional api, client name and queue size.
 
 ~RtMidiIn (void) throw ()
 If a MIDI connection is still open, it will be closed by the destructor.
 
RtMidi::Api getCurrentApi (void) throw ()
 Returns the MIDI API specifier for the current instance of RtMidiIn.
 
void openPort (unsigned int portNumber=0, const std::string &portName=std::string("RtMidi Input"))
 Open a MIDI input connection given by enumeration number.
 
void openVirtualPort (const std::string &portName=std::string("RtMidi Input"))
 Create a virtual input port, with optional name, to allow software connections (OS X, JACK and ALSA only).
 
void setCallback (RtMidiCallback callback, void *userData=0)
 Set a callback function to be invoked for incoming MIDI messages.
 
void cancelCallback ()
 Cancel use of the current callback function (if one exists).
 
void closePort (void)
 Close an open MIDI connection (if one exists).
 
virtual bool isPortOpen () const
 Returns true if a port is open and false if not.
 
unsigned int getPortCount ()
 Return the number of available MIDI input ports.
 
std::string getPortName (unsigned int portNumber=0)
 Return a string identifier for the specified MIDI input port number.
 
void ignoreTypes (bool midiSysex=true, bool midiTime=true, bool midiSense=true)
 Specify whether certain MIDI message types should be queued or ignored during input.
 
double getMessage (std::vector< unsigned char > *message)
 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.
 
virtual void setErrorCallback (RtMidiErrorCallback errorCallback=NULL, void *userData=0)
 Set an error callback function to be invoked when an error has occurred.
 
virtual void setBufferSize (unsigned int size, unsigned int count)
 Set maximum expected incoming message size.
 
- Public Member Functions inherited from rt::midi::RtMidi
bool supportsAutoReconnect (void) const
 Whether this backend can restore connections after device removal.
 
void setAutoReconnect (bool enabled)
 Enable or disable automatic recovery (enabled by default when supported).
 

Additional Inherited Members

- Static Public Member Functions inherited from rt::midi::RtMidi
static RtMidiApiAvailability checkApiAvailability (RtMidi::Api api)
 Query whether an API can be used by this build on this system.
 
static std::string getVersion (void) throw ()
 A static function to determine the current RtMidi version.
 
static void getCompiledApi (std::vector< RtMidi::Api > &apis) throw ()
 A static function to determine the available compiled MIDI APIs.
 
static std::string getApiName (RtMidi::Api api)
 Return the name of a specified compiled MIDI API.
 
static std::string getApiDisplayName (RtMidi::Api api)
 Return the display name of a specified compiled MIDI API.
 
static RtMidi::Api getCompiledApiByName (const std::string &name)
 Return the compiled MIDI API having the given name.
 

Detailed Description

A realtime MIDI input class.

This class provides a common, platform-independent API for realtime MIDI input. It allows access to a single MIDI input port. Incoming MIDI messages are either saved to a queue for retrieval using the getMessage() function or immediately passed to a user-specified callback function. Create multiple instances of this class to connect to more than one MIDI device at the same time. With the OS-X, Linux ALSA, and JACK MIDI APIs, it is also possible to open a virtual input port to which other MIDI software clients can connect.

Constructor & Destructor Documentation

◆ RtMidiIn()

rt::midi::RtMidiIn::RtMidiIn ( RtMidi::Api  api = UNSPECIFIED,
const std::string &  clientName = "RtMidi Input Client",
unsigned int  queueSizeLimit = 100 
)

Default constructor that allows an optional api, client name and queue size.

An exception will be thrown if a MIDI system initialization error occurs. The queue size defines the maximum number of messages that can be held in the MIDI queue (when not using a callback function). If the queue size limit is reached, incoming messages will be ignored.

The queue is only used when no callback is set. If a callback is installed with setCallback(), incoming messages go straight to it and the queue is never filled, so queueSizeLimit has no effect on them and the queue-limit warning cannot be reached. A binding that always uses a callback may pass 0 here, which also skips allocating the ring buffer.

If no API argument is specified and multiple API support has been compiled, the first API with at least one port is used, trying them in this order: CoreMIDI, ALSA, JACK, Windows MIDI Services, WinMM, UWP, Web MIDI, Android. If none has a port, the first that opens is used.

Parameters
apiAn optional API id can be specified.
clientNameAn optional client name can be specified. This will be used to group the ports that are created by the application.
queueSizeLimitAn optional size of the MIDI input queue can be specified.

Member Function Documentation

◆ openPort()

void rt::midi::RtMidiIn::openPort ( unsigned int  portNumber = 0,
const std::string &  portName = std::string( "RtMidi Input" ) 
)
inlinevirtual

Open a MIDI input connection given by enumeration number.

Parameters
portNumberAn optional port number greater than 0 can be specified. Otherwise, the default or first port found is opened.
portNameAn optional name for the application port that is used to connect to portId can be specified.

Implements rt::midi::RtMidi.

◆ openVirtualPort()

void rt::midi::RtMidiIn::openVirtualPort ( const std::string &  portName = std::string( "RtMidi Input" ))
inlinevirtual

Create a virtual input port, with optional name, to allow software connections (OS X, JACK and ALSA only).

This function creates a virtual MIDI input port to which other software applications can connect. This type of functionality is currently only supported by the CoreMIDI (macOS and iOS), JACK and ALSA APIs (the other APIs report a warning and open nothing).

Parameters
portNameAn optional name for the application port that is used to connect to portId can be specified.

Implements rt::midi::RtMidi.

◆ setCallback()

void rt::midi::RtMidiIn::setCallback ( RtMidiCallback  callback,
void *  userData = 0 
)
inline

Set a callback function to be invoked for incoming MIDI messages.

The callback function will be called whenever an incoming MIDI message is received. While not absolutely necessary, it is best to set the callback function before opening a MIDI port to avoid leaving some messages in the queue.

Parameters
callbackA callback function must be given.
userDataOptionally, a pointer to additional data can be passed to the callback function whenever it is called.

◆ cancelCallback()

void rt::midi::RtMidiIn::cancelCallback ( void  )
inline

Cancel use of the current callback function (if one exists).

Subsequent incoming MIDI messages will be written to the queue and can be retrieved with the getMessage function.

The callback runs on the backend's input thread, and this function does not wait for it: a message that is being delivered while it is called may still reach the callback after it returns. Keep the callback's user data valid until the port is closed.

◆ isPortOpen()

bool rt::midi::RtMidiIn::isPortOpen ( void  ) const
inlinevirtual

Returns true if a port is open and false if not.

Note that this only applies to connections made with the openPort() function, not to virtual ports.

Implements rt::midi::RtMidi.

◆ getPortCount()

unsigned int rt::midi::RtMidiIn::getPortCount ( void  )
inlinevirtual

Return the number of available MIDI input ports.

Returns
This function returns the number of MIDI ports of the selected API.

Implements rt::midi::RtMidi.

◆ getPortName()

std::string rt::midi::RtMidiIn::getPortName ( unsigned int  portNumber = 0)
inlinevirtual

Return a string identifier for the specified MIDI input port number.

Returns
The name of the port with the given Id is returned.
Return values
Anempty string is returned if an invalid port specifier is provided. User code should assume a UTF-8 encoding.

Implements rt::midi::RtMidi.

◆ ignoreTypes()

void rt::midi::RtMidiIn::ignoreTypes ( bool  midiSysex = true,
bool  midiTime = true,
bool  midiSense = true 
)
inline

Specify whether certain MIDI message types should be queued or ignored during input.

By default, MIDI timing and active sensing messages are ignored during message input because of their relative high data rates. MIDI sysex messages are ignored by default as well. Variable values of "true" imply that the respective message type will be ignored.

This setting belongs to the RtMidiIn instance, not to the port it is connected to, and the defaults apply to a port opened with openVirtualPort() exactly as they do to one opened with openPort(). An instance that should receive SysEx must call this itself; being the virtual port that another application sends to does not change it.

◆ getMessage()

double rt::midi::RtMidiIn::getMessage ( std::vector< unsigned char > *  message)
inline

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.

This function returns immediately whether a new message is available or not. A valid message is indicated by a non-zero vector size. An exception is thrown if an error occurs during message retrieval or an input connection was not previously established.

◆ setErrorCallback()

void rt::midi::RtMidiIn::setErrorCallback ( RtMidiErrorCallback  errorCallback = NULL,
void *  userData = 0 
)
inlinevirtual

Set an error callback function to be invoked when an error has occurred.

The callback function will be called whenever an error has occurred. It is best to set the error callback function before opening a port.

Implements rt::midi::RtMidi.

◆ setBufferSize()

void rt::midi::RtMidiIn::setBufferSize ( unsigned int  size,
unsigned int  count 
)
inlinevirtual

Set maximum expected incoming message size.

For APIs that require manual buffer management, it can be useful to set the buffer size and buffer count when expecting to receive large SysEx messages. Note that currently this function has no effect when called after openPort(). The default buffer size is 1024 with a count of 4 buffers, which should be sufficient for most cases; as mentioned, this does not affect all API backends, since most either support dynamically scalable buffers or take care of buffer handling themselves. It is principally intended for users of the Windows MM backend. There a SysEx longer than one buffer is received a buffer at a time and delivered as one message once its F7 arrives, so the size does not limit the length of a SysEx; more or larger buffers give the driver more room while a long SysEx is being received.


The documentation for this class was generated from the following file:

©2003-2023 Gary P. Scavone, McGill University. All Rights Reserved.
Maintained by Gary P. Scavone, gary at music.mcgill.ca