TITLE:: MIDIClient
summary:: Basic access to MIDI on your computer
categories:: External Control>MIDI
related:: Classes/MIDIIn, Classes/MIDIOut, Classes/MIDIFunc, Classes/MIDIdef, Guides/MIDI, Guides/UsingMIDI, Classes/MIDIEndPoint

DESCRIPTION::
MIDIClient is the core class that provides access to the MIDI subsystem on your computer.

See the link::Guides/UsingMIDI:: helpfile for practical considerations and techniques for using MIDI in SC.

CLASSMETHODS::

private:: prInit, prDisposeClient, prInitClient, prList


METHOD:: init
Initializes the MIDIClient, checks which available MIDI sources and destinations there are, and opens as many connections as desired.

ARGUMENT:: inports
the number of MIDI input connections to open; if code::nil:: then opens as many inports as there are MIDI sources.

ARGUMENT:: outports
the number of MIDI output connections to open; if code::nil:: then opens as many outports as there are MIDI destinations.

ARGUMENT:: verbose
A flag whether or not to post the MIDI sources and destinations that were found. Default is true.


METHOD:: initialized
A flag that tells whether of not the MIDIClient has been initialized.



METHOD:: disposeClient
Cleans up the MIDIClient. After using this method, you will have to reinitialize the MIDIClient before you can use MIDI again.



METHOD:: list
Refresh the list of available sources and destinations.
If you have connected a MIDI device after MIDIClient initialization, you won't see it until this method is run.

METHOD:: sources
The list of available MIDI sources, including SuperCollider's own sources.

returns:: A code::List:: of code::MIDIEndPoints::

METHOD:: externalSources
The list of available MIDI sources, excluding SuperCollider's own sources. Only on Linux the list of code::sources:: and code::externalSources:: differs.

returns:: A code::List:: of code::MIDIEndPoints::

METHOD:: destinations
The list of available MIDI destinations, including SuperCollider's own destinations.

returns:: A code::List:: of code::MIDIEndPoints::

METHOD:: externalDestinations
The list of available MIDI destinations, excluding SuperCollider's own destinations. Only on Linux the list of code::destinations:: and code::externalDestinations:: differs.

returns:: A code::List:: of code::MIDIEndPoints::

METHOD:: restart
Restart the MIDIClient.


METHOD:: myinports
The number of input ports that SuperCollider created. This is mainly useful to know on the Linux platform.


METHOD:: myoutports
The number of output ports that SuperCollider created. This is mainly useful to know on the Linux platform.

METHOD:: getClientID
Linux only. This gets the client ID by which the MIDIClient is defined in the ALSA subsystem. It can be used to identify whether a port is belonging to this client or another one.

On non-linux systems, it posts a warning and returns nil.

EXAMPLES::

This is Linux specific code.
The function takes care of initializing MIDIClient, detects a MIDI device named SE25 and connects it to port 0 of SuperCollider.

code::
(
~connect_midi_devices = { arg device, name;
	// init or refresh MIDIClient end points
	if(MIDIClient.initialized == false) {
		MIDIClient.init;
	} {
		MIDIClient.list;
	};

	MIDIClient.sources.do({ arg endPoint;
		if(device.notNil and: { name.notNil} and: {endPoint.device == device} and: { endPoint.name == name }) {
			// catch exception thrown when already connected
			try {
				// connect SuperCollider out port 0 to MIDI device
				MIDIOut.connect(0, endPoint.uid);
			};
			try {
				// connect MIDI device to SuperCollider in port 0
				MIDIIn.connect(0, endPoint.uid);
			}
		}
	})

}
)

~connect_midi_devices.("SE25", "SE25 MIDI 1"); // test it now
::


