One of the key concepts when scripting is to control how often the script will be processed.
Remember that the main procedure is processed at every cycle, in any case.
So, if your script is very light, (some simple math calculation for example), just create you inlets-oultets, your variables, and write the processing part in the main process procedure.
But in many cases, your script will become heavier (processing arrays, text or MIDI data).
Then itʼs better to have the script processed only when you need to (for instance if some input parameter has changed, or when some MIDI data is received, or when the script receives some rythmic pulse, etc.).
A good general strategy is to create a button inlet (call it "tick", or "process", or "has changed", or whatever), and to have the main part of the script processed only when this inlet receives an impulse.
This is how to do it:
//example of a tick inlet Var tick : tParameter; //here we declare the "tick" inlet parameter Procedure init; Begin Tick := CreateParameter(‘tick’, ptButton); SetIsOutput(tick, false); //creation of the "tick" inlet, it’s button, it’s an input //here you create the other parameters End; // main procedure Procedure Process; Begin If GetValue(tick) = 1 then //test to check if tick has received an impulse Begin SetValue(tick, 0); // for security, we immediately reset tick to 0, so that the block isn’t // processed several times // here you write your processing instructions End; //end of the if block, if tick hasn’t received any impulse, // the block will not be processed End;
This is a basic strategy I use in many scripts.
For MIDI scripts, here are a few things you have to know:
A MIDI ﬂow is by defnition not continuous, but made of discrete impulses.
One MIDI message, then another one, etc… (unlike audio which is continous).
Of course several messages can be present simultaneously (a chord, several CCs moved at the same time).
In Usine, remember that the MIDI ﬂow is considered as an array of MIDI messages.
When one message is present, the array length is 1, when 4 messages are present the array length is 4, when no message is present, the array length is 0.
So, basically the best way to deal with MIDI messages in scripts is to do the main processing part only if the length of the MIDI input parameter is greater than 0.
Then, if you want to actually send some messages to the MIDI output,
donʼt forget to set the length of the MIDI output parameter to the number of messages you want to send.
And if there is no message received, donʼt forget to set the length of the MIDI output parameter to 0, or youʼll be sending MIDI messages continuously.
This is how to do it:
// MIDI script example Var MIDIIn : tParameter; Var MIDIout : tParameter; // we declare our MIDI in an MIDI out // here you can declare other parameters Procedure init; Begin MIDIIn := CreateParam(‘MIDI In’, ptMIDI); SetIsOutput(MIDIIn, false); MIDIOut :=CreateParam(‘MIDI Out’, ptMIDI); SetIsInput(MIDIOut, false); // We have Created our MIDI input and output, End; Var NbOfMIDI : integer; // we’ll use it to store the number of incoming MIDI codes Var i : integer; // we’ll use it for loops // main procedure Procedure Process; Begin NbOfMIDI :=GetLength(MIDIIn); // we get the length of the MIDI in array (i.e. the number of incoming MIDI messages) If NbOfMIDI >0 then //check if any message is present Begin For i:=0 to NbOfMIDI-1 do // loop to process each incoming message Begin // here you do your MIDI processing End; //end of the for loop to process each incoming MIDI message Setlength (MIDIOut, NbOfMIDI); // we actually send the codes to MIDIOut End // end of the if (if there are no incoming message the script jumps here) Else // the next block will be processed only if there are no incoming MIDI messages Begin SetLength(MIDIOut, 0); // we make sure we do not send anything to MIDI out End; End;
When you deal with array outputs or with MIDI outputs, never forget to use the SetLength function in you script. This make sure you set the right length for the outgoing array, or to set the correct number of MIDI messages that should go to the MIDI output.
Letʼs say you have an array of single, letʼs call it
Tab1, of length
and you want to send it to the parameter youʼve created called
Var i : integer; Var x : single; For i:=0 to len-1 do Begin X:=tab1[i]; // we store the ith value of tab1 to x SetDataArrayValue(ArrayOut, i, x); // we set the ith value of ArrayOut to the value of x End; // after the loop has been processed from 0 to len-1, //ArrayOut will contain all the values of Tab1 SetLength(ArrayOut, len); // here we make sure ArrayOut has the same length as tab1
Letʼs say you want to send a three notes chord to your parameter MIDIOut, if some condition is true (letʼs call this condition ''MIDIHastoBeSent'').
The chord is stored in an array of ''tMIDI'' of length 3, called ''MIDIChord''.
Var MIDINote : tMIDI; Var i : integer; If (MIDIHasToBeSent) then begin For i:=0 to 2 do Begin MIDINote := MIDIChord[i]; // we store the current note into MIDINote SetMIDIArrayValue(MIDIOut, I, MIDINote); // we send the current note to MIDIOut End; // after the loop, MIDIOut contains the chord SetLength(MIDIOut, 3); // we actually send the chord End Else // in the case MIDIHasToBeSent is false Begin SetLength(MIDIOut, 0); // we make sure nothing is sent End;
version 3.0.153Edit All Pages