Class midi_in

Class Documentation

class libremidi::midi_in

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.

by Gary P. Scavone, 2003-2017.

Public Types

using message_callback = std::function<void(const message &message)>

User callback function type definition.

Public Functions

inline midi_in(libremidi::API api = API::UNSPECIFIED, std::string_view 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.

If no API argument is specified and multiple API support has been compiled, the default order of use is ALSA, JACK (Linux) and CORE, JACK (OS-X).

Parameters
  • api: An optional API id can be specified.

  • clientName: An optional client name can be specified. This will be used to group the ports that are created by the application.

  • queueSizeLimit: An optional size of the MIDI input queue can be specified.

inline ~midi_in()

If a MIDI connection is still open, it will be closed by the destructor.

inline libremidi::API get_current_api() const noexcept

Returns the MIDI API specifier for the current instance of RtMidiIn.

inline void open_port(unsigned int portNumber, std::string_view portName)

Open a MIDI input connection given by enumeration number.

Parameters
  • portNumber: A port number greater than 0 can be specified. Otherwise, the default or first port found is opened.

  • portName: A name for the application port that is used to connect to portId can be specified.

inline void open_port()
inline void open_port(unsigned int port)
inline void open_virtual_port(std::string_view portName)

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 Macintosh OS-X, any JACK, and Linux ALSA APIs (the function returns an error for the other APIs).

Parameters
  • portName: An optional name for the application port that is used to connect to portId can be specified.

inline void open_virtual_port()
inline void set_callback(message_callback callback)

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
  • callback: A callback function must be given.

  • userData: Optionally, a pointer to additional data can be passed to the callback function whenever it is called.

inline void cancel_callback()

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.

inline void close_port()

Close an open MIDI connection (if one exists).

inline bool is_port_open() const noexcept

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.

inline unsigned int get_port_count()

Return the number of available MIDI input ports.

Return

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

inline std::string get_port_name(unsigned int portNumber = 0)

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

Return

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

inline void ignore_types(bool midiSysex = true, bool midiTime = true, bool midiSense = true)

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.

inline message get_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.

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.

inline bool get_message(message&)
inline void set_error_callback(midi_error_callback errorCallback)

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

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

inline void set_client_name(std::string_view clientName)
inline void set_port_name(std::string_view portName)