NiCMidi 1.1.0
A MIDI library derived from J.D.Koftinoff jdksmidi
MIDIInDriver Class Reference

Receives MIDI messages from an hardware MIDI in port. More...

#include <driver.h>

Public Member Functions

 MIDIInDriver (int id, unsigned int queue_size=DEFAULT_QUEUE_SIZE)
 Creates a MIDIInDriver object which can receive MIDI messages from the given hardware in port. More...
 
virtual ~MIDIInDriver ()
 The destructor closes the hardware port and deletes the object. More...
 
virtual void Reset ()
 Resets the driver to default conditions: More...
 
int GetPortId () const
 Returns the id number of the hardware in port. More...
 
std::string GetPortName ()
 Returns the name of the hardware in port. More...
 
bool IsPortOpen () const
 Returns true is the hardware port is open. More...
 
bool CanGet () const
 Returns true if the queue is non-empty. More...
 
unsigned int GetQueueSize () const
 Returns the queue size. More...
 
MIDIProcessorGetProcessor ()
 Returns a pointer to the in processor. More...
 
const MIDIProcessorGetProcessor () const
 Returns a pointer to the in processor. More...
 
virtual void SetProcessor (MIDIProcessor *proc)
 Sets the in processor, which can manipulate all incoming messages (see MIDIProcessor). More...
 
virtual void OpenPort ()
 Opens the hardware in port. More...
 
virtual void ClosePort ()
 Closes the hardware out port. More...
 
void LockQueue ()
 Locks the queue so it cannot be written by other threads (such as the RtMidi callback). More...
 
void UnlockQueue ()
 Unlocks the queue (see LockQueue()). More...
 
void FlushQueue ()
 Empties the queue in a thread-safe way. More...
 
virtual bool InputMessage (MIDIRawMessage &msg)
 Gets the next message in the queue, copying it into msg (the message is deleted from the queue). More...
 
virtual bool ReadMessage (MIDIRawMessage &msg, unsigned int n)
 Gets the n-th message in the queue without deleting it (so the message remains available for other purposes). More...
 

Static Protected Member Functions

static void HardwareMsgIn (double time, std::vector< unsigned char > *msg_bytes, void *p)
 This is the callback function executed by RtMidi when a message arrives to the hardware port. More...
 

Detailed Description

Receives MIDI messages from an hardware MIDI in port.

Every MIDI in port is denoted by a specific id number, enumerated by the RtMidi class, and by a name, given by the OS; this class communicates between the hardware ports and the other library classes. The incoming MIDI messages are stamped with the system time in milliseconds and the port number (see the MIDIRawMessage struct and HardwareMsgIn() for details) and put in an internal queue; you can get them with the InputMessage() and ReadMessage() methods. Moreover you can set a MIDIProcessor for processing them as they arrive.

When the program starts, the MIDIManager searches for all the hardware ports in the system and creates a driver for everyone of them, so you find them ready to use.

Constructor & Destructor Documentation

◆ MIDIInDriver()

MIDIInDriver::MIDIInDriver ( int  id,
unsigned int  queue_size = DEFAULT_QUEUE_SIZE 
)

Creates a MIDIInDriver object which can receive MIDI messages from the given hardware in port.

Parameters
idThe id of the hardware port. Numbers of the ports and their names can be retrieved by the MIDIManager::GetNumMIDIOutPorts() and MIDIManager::GetMIDIOutName() static methods.
queue_sizeThe size of the queue; you could try to change this if you have trouble in receiving MIDI messages from the hardware, otherwise left unchanged (default size is 256).
Note
If id is not valid or the function fails, a dummy port with no functionality is created.
As said in the class description, the drivers are created automatically by the MIDIManager when the program starts, so usually you don't have to create or destroy them by yourself.

◆ ~MIDIInDriver()

virtual MIDIInDriver::~MIDIInDriver ( )
virtual

The destructor closes the hardware port and deletes the object.

Member Function Documentation

◆ Reset()

virtual void MIDIInDriver::Reset ( )
virtual

Resets the driver to default conditions:

  • Hardware MIDI port closed (resets the open count)
  • In queue empty
  • No extra processor (warning: this only sets the processor pointer to 0! The driver doesn't own its processor).

◆ GetPortId()

int MIDIInDriver::GetPortId ( ) const
inline

Returns the id number of the hardware in port.

◆ GetPortName()

std::string MIDIInDriver::GetPortName ( )
inline

Returns the name of the hardware in port.

Examples
test_advancedsequencer.cpp.

◆ IsPortOpen()

bool MIDIInDriver::IsPortOpen ( ) const
inline

Returns true is the hardware port is open.

◆ CanGet()

bool MIDIInDriver::CanGet ( ) const
inline

Returns true if the queue is non-empty.

◆ GetQueueSize()

unsigned int MIDIInDriver::GetQueueSize ( ) const
inline

Returns the queue size.

◆ GetProcessor() [1/2]

MIDIProcessor * MIDIInDriver::GetProcessor ( )
inline

Returns a pointer to the in processor.

◆ GetProcessor() [2/2]

const MIDIProcessor * MIDIInDriver::GetProcessor ( ) const
inline

Returns a pointer to the in processor.

◆ SetProcessor()

virtual void MIDIInDriver::SetProcessor ( MIDIProcessor proc)
virtual

Sets the in processor, which can manipulate all incoming messages (see MIDIProcessor).

If you want to eliminate a processor already set, call it with 0 as parameter (this only sets the processor pointer to 0! The driver doesn't own its processor).

Note
Processing of messages occurs before they are stored in the MIDIRawMessage queue. The driver cannot know their MIDI time (as it depends by musical tempo and division) so it runs on a MIDITimedMessage with time set to 0, which is then stored as a MIDIMessage. So every processing involving the message time is ineffective.

◆ OpenPort()

virtual void MIDIInDriver::OpenPort ( )
virtual

Opens the hardware in port.

This usually requires a noticeable amount of time, so it's better not to immediately start to get messages. If the port is already open the object remembers how many times it was open, so a corresponding number of ClosePort() must be called to effectively close the port.

◆ ClosePort()

virtual void MIDIInDriver::ClosePort ( )
virtual

Closes the hardware out port.

If the port was open more than once it only decrements the count (leaving it open), while it does nothing if the port is already close. If you want to force the closure call Reset().

◆ LockQueue()

void MIDIInDriver::LockQueue ( )
inline

Locks the queue so it cannot be written by other threads (such as the RtMidi callback).

You can then safely inspect and get its data, unlocking it when you have finished and want to get new messages.

◆ UnlockQueue()

void MIDIInDriver::UnlockQueue ( )
inline

Unlocks the queue (see LockQueue()).

◆ FlushQueue()

void MIDIInDriver::FlushQueue ( )

Empties the queue in a thread-safe way.

◆ InputMessage()

virtual bool MIDIInDriver::InputMessage ( MIDIRawMessage msg)
virtual

Gets the next message in the queue, copying it into msg (the message is deleted from the queue).

Parameters
[out]msgthe message got from the queue
Returns
true if the queue was not empty (and msg is valid), otherwise false.

◆ ReadMessage()

virtual bool MIDIInDriver::ReadMessage ( MIDIRawMessage msg,
unsigned int  n 
)
virtual

Gets the n-th message in the queue without deleting it (so the message remains available for other purposes).

Parameters
[out]msgis a direct reference to the queued message, so it's your responsability not to alter it
nthe message index in the in queue
Returns
true if such a message really exists in the queue (and msg is valid), otherwise false.

◆ HardwareMsgIn()

static void MIDIInDriver::HardwareMsgIn ( double  time,
std::vector< unsigned char > *  msg_bytes,
void *  p 
)
staticprotected

This is the callback function executed by RtMidi when a message arrives to the hardware port.

It converts raw data taken from the port into a MIDIRawMessage (eventually processing it with the processor) and pushes it on the internal queue. You must not call it directly.


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