Definition of a MIDI code

A MIDI code typically consists of 4 different values each having a length of 1 byte (0-255):

  • The Message value (msg), basically the nature of the MIDI message. See midi-implementation.

    • NOTE ON (144),
    • NOTE OFF (128),
    • CONTROL CHANGE (192),
    • PITCH BEND (224),
    • PROGRAM CHANGE (176), etc.
  • The Data1 value : It depends of the message value (for instance for a NOTE ON message, data1 contains the pitch value (0-127, note C3=60).

  • The Data2 value: It also depends on the msg value (for instance for a NOTE ON message, data1 contains the velocity value (0-127, 0=silence, 127= loudest).

  • The Channel value: It says on which MIDI channel the message is sent.

The type TMIDI

To deal with the multiple-value MIDI message, Usine Script has a built-in type defined as follows:

type  TMIDI = Record 
       Msg   : Byte; 
       Data1 : Byte; 
       Data2 : Byte; 
       Channel : byte; 

''TMIDI'' is a data structure used in Pascal called record.

A record is a variable containing several 'sub-variables'.

Or you can see it as several variables all grouped under a common name.

Handling MIDI data

Let's declare a MIDI variable:

 Var MIDI1 : TMIDI; 
 // MIDI1 will store a MIDI message

To access the different values of the MIDI1 message do like this:

Var m, d1, d2, c : byte;
m := MIDI1.msg;
d1 := MIDI1.data1;
d2 := MIDI1.data2;
c := MIDI1.channel; 
// the different values of MIDI1 are stored to m, d1, d2, c.
// notice the dot between MIDI1 and it's values, this is how you access a record sub-variables.
Example 1 - create a Note On message

Let's store in MIDI1 the message ( NOTE ON, note=C3, vel=90, channel=1):

MIDI1.msg := 144; // Note ON
MIDI1.data1 :=60; // C3
MIDI1.data2 := 90; // vel 90
MIDI1.channel :=1; // Channel 1
Example 2 - transpose

Let's transpose MIDI1 an octave higher:

MIDI1.data1 := MIDI1.data1 + 12; 
Example 3 - arrays of MIDI codes

If we have an array of MIDI codes, it's the same:

Var TabMIDI : array of TMIDI;
TabMIDI[3].msg := 148;
TabMIDI[3].data1 := 60;

See also

version 4.1.200407

Edit All Pages