MPL tutorial: MIDI Clocks section

This MIDI Clocks section starts with each tick of the MIDI clock. Before the instructions are executed the variable Clock is increased one. If the StartClock instruction was placed before the StartStandBy instruction in the main section, then the internal clock is ticking 24 times in a beat and the tempo in beats/minute is determined by the Tempo variable. If no StartClock instruction was givin then no clock tick will happen until a start sequencer message is received at one of the opened MIDI In ports. After that the clock will tick and execute this section each time a MIDI clock message is received at one of the MIDI In ports.

Below the instructions in (in blue) are explained (in black).

;Play the MIDI chain
The MIDI chain that was set to the first event in the Main section is now played. Each time that this section is executed it will play the notes that are timed to play according to their timing value in the Ticks variable.
Declare ClockTime Numeric
You can declare variables in any section at any place. They will all be declared at run time only once before the program starts running. However you must alway place the declare before the places where it is used or you will get an error when you try to run it.
SetEvent Pointer ;Refresh the variables Ticks Type etc.
The value of pointer will not have changed since the previous NextEvent instruction in this section. However the predeclared variables related to the pointed event may now have the values of an incoming MIDI message in the MIDI In section. Or values may have been assigned to some of them in the PC key section. The SetEvent Pointer will get back the original values related to the pointed event.
Let ClockTime = Multiply Clock Division
Let ClockTime = Divide ClockTime 24

The variable ClockTime contains the same time as the variable Clock, only in an other scale. The Clock has 24 ticks in a beat and the ClockTime has a number of ticks in a beat that corresponds with the Division of the MIDI file loaded in chain 1
DoWhile Ticks <= ClockTime
Send all the events that are in time to be send now according to their Ticks value and the current ClockTime.
If Pointer <= 20
GoTo End
Endif

If the pointer points to a start and finish place (1-20) then we are at the end of the chain and there are no more events to send. So get out of this loop.
SendEvent
A MIDI message is send thru the port defined by the number in MidiOut (default the first on the list that is open), using the variables filled by the NextEvent or SetEvent instructions in this section.
SetNextEvent
Set the pointer at the next event in the chain and fill the predeclared variables with the values associated with this event.
Loop
Check the condition in the previous DoWhile instruction. If the Ticks value of the current event is still not later than the ClockTime then repeat the instructions in the DoWhile loop. Otherwise resume with the next instructions. The current event will get it's turn later when the MidiClocks section is executed again.
Label End
This is the target of the previous GoTo instruction to jump out of the loop.

;Play Metronome ticks
When you record a MIDI file from scratch then you will need metronome ticks so that you can follow the tempo while playing.
Declare ClockInBeat Numeric
Let ClockInBeat = Modulus Clock 24

The Modulus is the remainder after division. So it will range from 0 to 23 in this case. Because Clock starts with 1 not with zero, 1 is the first ClockInBeat in each beat and 0 (that comes after 23) the last.
If ControlValue@4 = 1
The on-screen control 4 is used to switch the metronome on and off. A value of 1 means it is on so continue with the next instructions.
Let Channel = 10
Let DataByte1 = 60
Let DataByte2 = 127

Set the variables to produce drum notes (channel 10). DataByte specifies which drumsound, Databyte1 the velocity (127 is maximum).
If ClockInBeat = 1
Let Type = 1
Let NoteLength = -1
SendEvent
EndIf

Send a note on at the first clock in each beat.
If ClockInBeat = 2
Let Type = 2
SendEvent
EndIf
EndIf
Send a note off at the second clock in each beat.


Back to the Mpl main page.