MPL tutorial: Main section

This Main section starts when you press the Run button.

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

;Find the tempo
A single line of comment preceding each functional unity in the program code, will make things easier to understand, when you start working on your program again after some time.
SetEvent 1
Set the pointer at the start and finish point of chain 1. This is an empty place without info about a MIDI event.
Now the pointer is at the first event of the chain (the loaded MIDI file). The variables Chain, Track, Ticks, Measure, BeatInMeasure, TicksInBeat, Channel, Type, MetaType, DataByte1, DataByte2, NoteLength and Text will now have values related to this event.
DoWhile Pointer > 20
The Pointers (event locations in the chains) 1 to 20 are the start and finish points for the 20 chains. So if the pointer has one of these values, then you know that you have reached the end of your journey thru a chain. Reading chain 1 has typically the following construction:
SetEvent 1
DoWhile Pointer > 20
<place instructions for each event here>
<place instructions for final processing here>
The instructions for each event will be repeated with each event that is encountered in the chain.
If Type = 12
Execute following instructions (until the matchin EndIf) only with meta events. Meta events are events in a MIDI file that are never send thru the MIDI ports but that contain directives for the sequencer.
If MetaType = 81
Do the next instructions only with the tempo meta event.
Let Tempo = DataByte1
The Tempo value becomes available in databyte1. A timesignature would become available in databyte1 (nominator) and databyte2 (denominator). All other meta event values become available in the text variable.
Let ControlValue@1 = Tempo
Also set the value of the control 1 (for mouse operation) to the found tempo. Notice that this array variable can have 5 values (one for each of the 5 control) and that @1 indicates that we are addressing the first one.
GoTo TempoFound
We've found what we were looking for so let's get out of this DoWhile loop.
If MetaType was not 81 then processing continues here.
If Type was not 12 then processing continues here.
We didn't find the tempo event yet so get the next event.
If the condition in DoWhile is still valid (we have not reached the start and finish again) then go back to the instruction after the DoWhile. Otherwise resume with the instructions below.
;No tempo found, insert one
If we had found the tempo then we would not have been here but at the Label TempoFound below. It seems that we have exited the loop because we reached the end of the chain (found a pointer with a value not greater than 20 pointing to the start and finish place) without having encountered a tempo event first. So let insert's a default tempo at the start of the chain.
Let Type = 12
Type 12 is a meta event. This is not how it is coded in a MIDI file but how it is identified in the chain. If you save the chain in a MIDI file then everything will be translated to the MIDI file format.
Let MetaType = 81
Meta event 81 is a tempo meta event.
Let DataByte1 = ControlValue@1
The value of meta event is associated with the predeclared variable Databyte1. Get the default tempo from the value of control 1 for run-time mouse interactions.
Let Ticks = 0
The Tempo event should be placed at the start of the chain
The tempo event is inserted. Actually not quite yet. Like all changes it is just placed in a to-do-list that will be finalised with the UpdatChains instruction.
Let Tempo = ControlValue@1
The tempo value is assigned to the Tempo variable. This will determine how often the MIDI clocks section is executed. There are 24 clocks in a beat. So if the tempo is 120 beats/minute then there will be 2880 clocks in a minute. That means that the MIDI clocks section will be executed 48 times in a second.
Label TempoFound
Processing continues here if a tempo event was found in the previous DoWhile loop.

;Start playing the sequence
SetEvent 1
Set the pointer at the start and finish point of chain 1 again.
Set the pointer at the first event in chain 1. Fill the predeclared variables with values relating to this event.
The internal clock will start ticking at the next StartStandBy instructions. 24 clock tick in a beat and the value of Tempo will determine the number of beats in a minute. For each clock tick the MIDI Clocks section will be executed (after increasing the value of Clock by 1). If you omit this instruction then no clock tick will happen until a start sequencer message is received at one of the opened MIDI In ports. After that a clock tick will happen each time a MIDI clock message is received at one of the MIDI In ports. In other words: if you omit the StartClock before a StartStandBy then the sequencer will be in slave mode.
At this point the processing in the Main section stops. The next instructions will not be executed until a StopStandBy instructions is executed in one of the other sections. Until that happens (or until the stop button is pressed) the program will await things that will trigger the execution of the other sections, like a clock tick, a MIDI message recieved at one of the opened MIDI In ports, a key pressed on the PC keyboard or a mouse interaction with one of the 5 on-screen controls.

;Send all-notes-off before ending the program
Processing will continue here after the StopStandBy instruction is executed in one of the other sections. N.B. Not when the stop button is pressed.
Let Channel = 1
The predeclared variable channel is not just used here to fill the channel value in the SendEvent instruction later on. It is also used to operate the DoWhile construction.
DoWhile Channel <= 16
Repeatedly execute the following instruction until channel is greater than 16.
Let Type = 3 ;controller
MPL doesn't use a status variable but has seperate variables for Type And Channel that are included in the MIDI status variable.
Let DataByte1 = 123 ;all notes off
Let DataByte2 = 0

DataByte1 and DataByte2 have the same contents as in MIDI files and messages.
A MIDI message will be sent using the variables: Type, Channel, DataByte1, DataByte2 and MidiOut. MidiOut is by default the first opened port. Use menu Settings>MIDI Out to open and close ports. The value of variable Text is not used because this is a type 3 (control change) event and not a type 11 (system exclusive)
Let Channel = Add Channel 1
Increase the value of channel by one
If the condition in the preceding DoWhile is still valid (channel not greater thena 16) then go back to the instruction after the DoWhile. Otherwise resume with the instructions below.

;Keep or discard the recording
You are asked if you want to keep the recorded MIDI events in the chain. If you reply 0 (=no) then chain 1 remains unchanged. Reply 1 (=yes) to add all recorded events to chain 1.
Declare KeepRecording Numeric
You have to declare the variable that will contain the answer given at run time. Use a numeric variable that will contain the value 0 for yes and 1 for no.
Display 'Answer 1 to keep the recording or 0 to discard it'
This message will become visible in the dialog area in the upper right of the window.
Input KeepRecording
Get the value of the KeepRecording variable. The name of the KeepRecording variable will displayed in the dialog area followed by a question mark. The cursor is placed after it. When that happens at run time you can type the value and press the Enter key.
If KeepRecording = 1
Yes was answered so the recording is kept.
Modifying a chain while reading it, is like driving on a road that is under construction. You might get lost or crash. To avoid all kinds of compications, MPL will not do the modifications with InsertEvent, DeleteEvent and AddEvent directly but place them on a to-do-list instead. When you're finished processing the chain you can use the UpdateChains instruction to make the modifications in the chains using the to-do-list. If you omit the UpdateChains instruction then the chains remain unchanged. The to-do-list is no longer available after the program has ended. Remember that even after an UpdateChains instructions, you still will need to use the SaveFile instruction or use the menu Chain>Save MIDI file to save the MIDI file to disk.
Processing would continue here if the KeepRecording answer was 0. But there are no more instructions so the programm will stop running and you can use the program editor and menu again that were blocked at run time. You will see a line with the word Finished as last line in the dialog area.

Back to the Mpl main page.