Assigning values to variables

Use the Let instruction to assign a value to a variable. The second word of the instruction is the variable name and after that you type an "=" and specify the value. The value can be a literal as in:

Let DataByte2 = 60

It can also be another variable (user-defined or pre-defined):

Let DataByte2 = MyNewValue

The new value may also be specified in a function. The available functions are listed in the help file. You may want to increase the value of databyte2 in selected events:

Let DataByte2 = Add DataByte2 10

The function "Add DataByte12 10" will compute the total of DataByte2 and 10. Of course the value should never be higher than 127. So it should be followed by:

If DataByte2 > 127
DataByte2 = 127

You can run the following program to experience that you can increase the tempo in a style:

Define OriginalTempo Numeric
Let OriginalTempo = Tempo
PrintLine PartName
PrintLine 'Playing normal tempo'
Let Tempo = 250
PrintLine 'Playing fast tempo'
Let Tempo = OriginalTempo

Another way to assign a value to a variable is with the Input instruction. The Input is only allowed for user defined variables. When the Input instruction is encountered you are requested to enter a value for the variable in the upper right corner of the window. Try running the following program:

Define NewTempo Numeric
Input NewTempo
Let Tempo = NewTempo

If constructions

If you already have been programming in other languages, then you have probably used complex If.. And..Or..Else contructions, complete with formulas. In STPL you can do all that differently in one or more of the following ways.

If there are more conditions and all of them should be true, then make nested If's:

If EventType = 1
If DataByte2 > 100
If DataByte2 <= 110
;Instructions inserted here will only be executed on note-on events with velocity 101 thru 110

When you cut and paste examples in this tutorial to the STPL editor and press the Run button, then you will notice that the lines between If..EndIf and between ForEach..Next are automatically indented. This makes it easier to see which lines are captured in these constructions. Each Next must be aligned with the matching ForEach, and each EndIf with its If. If this is not the case then these constructs are either incomplete or overlapping. For example the sequence: If-ForEachTrack-EndIf-NextTrack is not correct. It should be If-ForEachTrack-NextTrack-EndIf.

If there are more conditions and only one has to be true, then define a numeric variable that has the value 0 if false and 1 if true.

Let MainPart = 0
If PartName = 'Main A'
Let MainPart = 1
If PartName = 'Main B'
Let MainPart = 1
If PartName = 'Main C'
Let MainPart = 1
If PartName = 'Main D'
Let MainPart = 1
If MainPart = 1
;Instructions inserted here will only be executed in Main parts

If you want to replace some notes in the drum tracks to other values, you may want to do it like this:

If DataByte1 = 32
Let DataByte1 = 34
If DataByte1 = 33
Let DataByte1 = 40
If DataByte1 = 34
Let DataByte1 = 37

This is not correct! The value 32 is first changed to 34. But then further on the value 34 is changed to 37. That is not what you intended. Only the events that had originally the value 34 should be changed to 37. The events that were originally 32 should be changed to 34 and stay 34. You can accomplish this by defining a numeric variable NewDataByte1 and give that the (invalid) value -1 first .

Let NewDataByte1 = -1
If DataByte1 = 32
Let NewDataByte1 = 34
If DataByte1 = 33
Let NewDataByte1 = 40
If DataByte1 = 34
Let NewDataByte1 = 37
If NewDataByte1 > -1
Let DataByte1 = NewDataByte1

Instead of formulas you must use as many additional Let instructions and user defined variables as needed. So instead of:
If Variable1 = ((Variable2 + Variable3) * Variable4)
You write:

Let AdditionalVariable1 = Add Variable2 Variable3
Let AdditionalVariable1 = Multiply AdditionalVariable1 Variable4
If Variable1 = AdditionalVariable1

Modifying styles

When you modify styles you will change the predefined variables in each style and then save it. Actions upon predefined variables should be done in the associated ForEach loop for that variable, both assigning a value to that variable and using the value in that variable. In general a program will have this construction:

;Define user variables here
;Assign values to StyleName, FolderName, Tempo, BeatsPerMeasure and TicksPerBeat
;Assign values to PartName and MeasuresInPart
;Assign values to Track, TrackName, TrackChannel, TranspositionType, DrumTrack,
;NoteLow, NoteHigh, HighKey, SourceChord, SourceChordType, ChordNote1 thru ChordNote5
;Assign values to EventType, DataByte1, DataByte2, Measure, Beat, Tick
UpdateEvent ; Must be specified or the assigned event values will not be kept OR
InsertEvent ; Alternatively you can insert a new event with the assigned values OR
DeleteEvent ; Will remove the current event

While testing you better remove (or place a semicolon before) the ForEachStyle, SaveStyle and NextStyle instructions. You will be testing with just one style and don't want to save it. If you just want to work with Part variables then the ForEachTrack and ForEachEvent loop can be removed too. When a style is saved then any change you made to the predefined varables is saved too. A special rule is for the Event variables: you must specify UpdateEvent before the changes come in effect. Alternatively you can specify InsertEvent to add an event with the values that you assigned to the predefined event variables. If you did not assign a value then the value form the current event will be copied in the inserted event. DeleteEvent will remove the current event.

It is possible to make the part and track loops inside out: the ForEachPart loop is placed inside the ForEachTrack loop. An example is the sample program AvarageLoudness. This program will stay inside a track while walking tru the parts, evaluating the volume controllers in the track that may occur in different parts. Also notice that besides parts with well known names like "Main A" there is also a part named "Control changes". This contains patches and controllers to setup the synthsizer. In styles converted from non-Yamaha keyboards these patches and controllers are often also present in the other parts.

Always make copies of your styles. You might want to restore the originals.


Back to the One Man Band main page.