• No results found

Receiving MIDI.

In document SuperCollider-Step by Step (Page 58-60)

Configuring MIDI input involves two stages: (a) Activating or deactivating a connection, and (b) specifying what to do when a MIDI message is received.

(a) Activating and deactivating MIDI input connections

The ports of the devices that send MIDI to SuperCollider are stored in an array in

MIDIClient.sources. To start receiving MIDI from the first port of the first device in that

array, activate the first MIDIEndPoint of MIDIClient.sources by sending the message connect to MIDIIn, with device argument value 0:

// Activate input from the first port-device in sources: MIDIIn.connect(device: 0);

Once this is done, all MIDI messages sent to the computer on that port will be received by SuperCollider and will trigger the actions stored in MIDIIn, as explained in the following section (b). If you want SuperCollider to stop receiving MIDI from some MIDIEndPoint x, send message disconnect to MIDIIn:

// Deactivate input from port-device with index x in sources: MIDIIn.disconnect(device: x);

A Very Step-by-Step Guide to SuperCollider

59/...124

[email protected]

Communication: MIDI, OSC, HID

MIDI

Essentials of MIDI communication in SuperCollider

Programming MIDI communication in SuperCollider involves three stages:

1. Initialize: Find out what devices exist on the system. This must be done once only at the beginning of a setup, that is, when the MIDI devices are first connected to the system, or whenever there is some change in the number or connection configuration of devices. 2. Connect to specific devices: For MIDI input, activate (or deactivate) a connection to a

device for input. For MIDI output, create an object that can sends MIDI to a specific device. This is also done once only to establish or close a connection to a specific device, it is not done when actually sending or receiving MIDI commands.

3. Handle or send MIDI messages: For input, specify what to do when receiving a specific kind of MIDI message (once only for each specification of a kind of action); for output, send a message to a device (must be done individually for each MIDI message that is sent).

Once all 3 steps above have been completed, the system will automatically call the functions that were stored in the corresponding variables of MIDIin, whenever a MIDI command is

received. The class MIDIResponder provides a convenient way to bind actions to be performed for specific midi-in keys or controller numbers and it is recommended to use this class because of the flexibility that it affords.

Receiving MIDI.

Configuring MIDI input involves two stages: (a) Activating or deactivating a connection, and (b) specifying what to do when a MIDI message is received.

(a) Activating and deactivating MIDI input connections

The ports of the devices that send MIDI to SuperCollider are stored in an array in

MIDIClient.sources. To start receiving MIDI from the first port of the first device in that

array, activate the first MIDIEndPoint of MIDIClient.sources by sending the message connect to MIDIIn, with device argument value 0:

// Activate input from the first port-device in sources: MIDIIn.connect(device: 0);

Once this is done, all MIDI messages sent to the computer on that port will be received by SuperCollider and will trigger the actions stored in MIDIIn, as explained in the following section (b). If you want SuperCollider to stop receiving MIDI from some MIDIEndPoint x, send message disconnect to MIDIIn:

// Deactivate input from port-device with index x in sources: MIDIIn.disconnect(device: x);

(b) Specifying actions that will be performed when a MIDI message is received

To specify what SuperCollider must do when it receives a MIDI message of a certain kind, store some action in the corresponding variable of MIDIIn:

/* Upon receipt of a MIDI note-on message, play a short sound with corresponding frequency and amplitude: */

(

MIDIIn.noteOn = { | src, chan, key, veloc | var freq, amp;

freq = key.midicps; // convert MIDI key to frequency

amp = veloc / 1270; // scale: max. veloc 127 -> amp 0.1

{ SinOsc.ar(freq, 0, EnvGen.kr(Env.perc, levelScale: amp, doneAction: 2)); }.play;

}; )

/* Upon receipt of a MIDI control message, post the names of the device and port from which it was received as well as

the channel number, the controller number, and the control value: */

(

// create a dictionary for accessing the MIDIEndPoints by their uid:

var sourceDict;

sourceDict = IdentityDictionary.new;

MIDIClient.sources.do { |s| sourceDict[s.uid] = s; };

// Store action in MIDIIn.control that find the device and posts details

MIDIIn.control = { | src, chan, num, val | var device;

device = sourceDict[src]; // find the device from its uid // post all the info

Post << "Device: " <<< device.device << " port: "

<<< device.name << " channel: " <<

chan << " number: " << num << " value: " << val << "\r"; }

A Very Step-by-Step Guide to SuperCollider

60/...124

[email protected]

Communication: MIDI, OSC, HID

MIDI

Essentials of MIDI communication in SuperCollider

Programming MIDI communication in SuperCollider involves three stages:

1. Initialize: Find out what devices exist on the system. This must be done once only at the beginning of a setup, that is, when the MIDI devices are first connected to the system, or whenever there is some change in the number or connection configuration of devices. 2. Connect to specific devices: For MIDI input, activate (or deactivate) a connection to a

device for input. For MIDI output, create an object that can sends MIDI to a specific device. This is also done once only to establish or close a connection to a specific device, it is not done when actually sending or receiving MIDI commands.

3. Handle or send MIDI messages: For input, specify what to do when receiving a specific kind of MIDI message (once only for each specification of a kind of action); for output, send a message to a device (must be done individually for each MIDI message that is sent).

Once all 3 steps above have been completed, the system will automatically call the functions that were stored in the corresponding variables of MIDIin, whenever a MIDI command is

received. The class MIDIResponder provides a convenient way to bind actions to be performed for specific midi-in keys or controller numbers and it is recommended to use this class because of the flexibility that it affords.

In document SuperCollider-Step by Step (Page 58-60)