MPL tutorial: PC Key section

The PC Key section starts each time you press or release a key on your PC keyboard. Before the instructions in this section are executed, the variables KeyNumber and KeyAction are filled. KeyNumber specifies the number of the key on your keyboard. To find out which key has which number, place a Display KeyNumber instruction in this section. KeyAction 1 = key down, 2 = key up.

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

;If the Esc key is pressed, the stand by mode is stopped and processing continues in the main section
You can also end the program with the Stop button. But in that case the instructions after StartStandBy in the Main section are not executed.
If KeyAction = 1
Do the following instructions if the key was pressed not when it was released.
If KeyNumber = 27
Do the following instruction if it was the Esc key. You can find out the keynumbers for each key by placing a Display KeyNumber instruction in this section.
When this section is finished processing will continue with the instructions following StartStandBy in the Main section. The other sections will no longer be activated.
If the keynumber was not 27 then processing continues here.
If the keyaction was not 1 then processing continues here.

;Play notes with the PC-keys 1 thru 9
May be handy if you don't have a MIDI keyboard controller connected to your PC.
Declare KeyPitch Numeric
The pitch of the note that has to be played.
Let KeyPitch = 0
The fact that the KeyPitch variable has been declared one line earlier, does not mean that it has an initial value now (zero). All variables in MPL are "global" and none are "local". Each user declared variable will have an initial value (zero or space) when the program starts to run. But once a value has been assigned it will keep that value in all sections until another value is assigned.
If KeyNumber = 49 ;1
Notice that you can also place a remark after an instruction instead of in between the lines. The KeyNumber 49 corresponds with the key 1 on the line of numeric keys on your keyboard below the line of function keys.
Let KeyPitch = 60 ;Middle C
This key is used to play the middle C.
In the followin lines the same construction is used to determine the pitch of key 2 thru 9.
If KeyNumber = 50 ;2
Let KeyPitch = 62 ;D
If KeyNumber = 51 ;3
Let KeyPitch = 64 ;E
If KeyNumber = 52
Let KeyPitch = 65
If KeyNumber = 53
Let KeyPitch = 67
If KeyNumber = 54
Let KeyPitch = 69
If KeyNumber = 55
Let KeyPitch = 71
If KeyNumber = 56
Let KeyPitch = 72
If KeyNumber = 57
Let KeyPitch = 74
If key 1 thru 9 was pressed or released then we will have a value in KeyPitch now. Otherwise it will be zero.
If KeyPitch > 0
The following instructions will only be executed if KeyPitch has a value other then zero.
Let Channel = ControlValue@5
The on-screen control 5 can be used to set the channel number with the mouse. The current value is assigned to the predeclared variable Channel.
Let Type = KeyAction
KeyAction 1 (key pressed) will result in Type 1 (note on). KeyAction 2 (key released) will result in Type 2 (note off).
Let DataByte1 = KeyPitch
In the MIDI note on and note off messages, DataByte1 determines the pitch (0 thru 127).
Let DataByte2 = 127
In the MIDI note on and note off messages, DataByte2 determines the velocity (0 thru 127).
Send the note on or note off message thru the MIDI output port that is determined by the number in the variable MidiOut.
Let Ticks = Multiply Clock Division
Let Ticks = Divide Ticks 24
Convert the scale of Clock (24 ticks in a beat) to the scale of Ticks in the MIDI file (ticks per beat is defined in the predeclared variable Division).
Let NoteLength = -1
You do not want a note off to be inserted with each note on, based on the note length. When note length is set to a negative number then MPL will insert just a note on without a matching note off. The note off will be inserted separately when the key is released and the Type is set to 2.
Add the InsertEvent to the to-do-list. It will be inserted when the UpdateChain instruction is executed.

Back to the Mpl main page.