Basic Examples

You will find here a few basic scripting examples.

A+B

//////////////////////////
//Simple A+B example 
/////////////////////////

var InA , InB : Tparameter; // first, we declare the parameters we will need.
var Result    : Tparameter;

////////////////////////////////////// // Init is started on script compilation to create the in/outs
PROCEDURE INIT;
BEGIN                                  // Each procedure start with a Begin

  InA:= CreateParam('InputA',PtDataField); SetIsOUTput(InA,FALSE);  // we can set the displayed name between'', 
                                                                    //and the type, here we use simple PtDataField.
  InB:= CreateParam('InputB',PtDataField); SetIsOUTput(InB,FALSE);  // ...and specify if it's an input or output 
                                                                    //by default they are booth);

  Result:= CreateParam('Result',PtDataField); SetIsINput(Result,FALSE);  // to specify if in or out we set off the
                                                                         //pin we don't want.

END; // end of Init                     // each procedure has also a end, defined by END; 

//////////////////////////////////////
// Callback procedure                   // inputs have an id (n), the callback procedure scans the inputs to detect
                                        // changes, and note the index if detecting.
Procedure Callback(N:integer);          // it's a procedure that will make the result in same bloc as soon as the
                                        // input has changed
BEGIN                                   // start the callback (=changes detection)

 if (n = InA) OR (n = InB) then begin   // we will trig the computation ONLY if callback detected changes on inA or
                                        // inB. As inputs are indexed from 0 to n,
                                        // we could also have used " if (n=0) or (n=1)",  or as we only have 2 inputs
                                        // and they will booth trig computation,
                                        // this is not useful here but more as exemple to trig the code only for those
                                        // specific inputs changes.

  SetValue(Result, getValue(InA) + getValue (InB));   //Our summing code: setvalue(Parameter destination,value);
                                                      //when accessing Parameters, we use GetValue to get the
                                                      // 'variable' value, and SetValue to set.
  end; //sum         end of summing code

END;//Callback
//////////////////////////////////////

Simplest Callback exemple

Check wich is changed

//////////////////////////
//Simplest Callback exemple: check wich in changed
/////////////////////////

var In0 , In1 , in2 : Tparameter;          // first, we declare the parameters we will need.
var inputChanged    : Tparameter;

//////////////////////////////////////     // Init is started on script compilation to create the in/outs
PROCEDURE INIT;
BEGIN                                      // Each procedure start with a Begin

  In1:= CreateParam('Input1',PtDataField); SetIsOUTput(In1,FALSE);  // we can set the displayed name between'', and
                                                                    // the type, here we use simple PtDataField.
  In2:= CreateParam('Input2',PtDataField); SetIsOUTput(In2,FALSE);  // ...and specify if it's an input or output (by
                                                                    // default they are booth);
  In3:= CreateParam('Input3',PtDataField); SetIsOUTput(In3,FALSE);

  InputChanged:= CreateParam('InputChanged',PtDataField); SetIsINput(InputChanged,FALSE);  // to specify if in or out
                                                                                           // we set off the pin we
                                                                                           // don't want.

END; // end of Init                        // each procedure has also a end, defined by END; 
//////////////////////////////////////
// Callback procedure                      // inputs have an id (n) from first (top) to n last, the callback proc
                                           // scans the inputs to detect changes, 
                                           // and notes the index if detecting one.
                                           // that will allows to launchs different process depending on wich kind of
                                           // inputs changed
Procedure Callback(N:integer);             // it's a procedure that will make the result in same bloc as soon as the
                                           // input has changed. 
BEGIN                                      // start the callback (=changes detection)

  SetValue(inputchanged, n);               // set the inputchanged parameter of index of last detected changed input
                                           // when accessing Parameters, we use GetValue to get the variable value,
                                           // and SetValue to set
END;//Callback
//////////////////////////////////////

1 in to N outputs

/////////////////////
// 1 in to N outputs
/////////////////////
const nb-outs = 8;  //define number of outputs all picking input as a constant to be easy accessible.

var OUTPUTS : array of Tparameter;        // to automate the inout creation process we make an array of parameters.
var input  : tparameter;
var i      : integer;
var valtmp : single;

Procedure Init;
begin
  input:= createParam('input',Ptdatafield); 
  setisOutput(input,false);
  setArrayLength(OUTPUTS,nb-outs);          // the first thing to do with array is set their length, here the nb outs we
                                            // want.
  for i:= 0 to nb-outs-1 
  do begin
    OUTPUTS[i] :=  CreateParam('output'+IntToStr(i),PtDataField);  
    SetIsInput(OUTPUTS[i],false);   // will create all the                                                                                              // outs one shot
  end;
  valtmp := 0;                                                
end;// init

Procedure Callback (n:integer);
Begin
    valtmp:= getValue(input);
    for i:= 0 to nb-outs-1 
    do begin      // assign input value to all outputs
        setValue(outputs[i],valtmp);
     end;

end;// callback
////////////////////////////////////////

N in to N outputs

///////////////////////
// N in to N outputs
///////////////////////
const nb-inouts = 8;  //define number of outputs all picking corresponding input value. set as a constant to be easy
                      // accessible.

var INPUTS, OUTPUTS : array of Tparameter;     // to automate the inout creation process we make an array of
                                               // parameters.
var i      : integer;

Procedure Init;
begin
  setArrayLength(INPUTS,nb-inouts);              // the first thing to do with array is set their length, here the nb in
                                                 // we want.
  for i:= 0 to nb-inouts-1 
  do begin
    INPUTS[i] :=  CreateParam('input'+IntToStr(i),PtDataField);  
    SetIsOutput(INPUTS[i],false);   // will create all the                                                                                            // ins one shot
  end;
  setArrayLength(OUTPUTS,nb-inouts);             // the first thing to do with array is set their length, here the nb
                                               // outs we want.
  for i:= 0 to nb-inouts-1 do 
  begin
    OUTPUTS[i]:=  CreateParam('output'+IntToStr(i),PtDataField);  
    SetIsInput(OUTPUTS[i],false);   // will create all the                                                                                            // outs one shot
  end;                                              
end;// init

Procedure Callback (n:integer);
Begin
   setvalue(outputs(n),getvalue(inputs(n)));      // assign a changed value in to same index out.
end;// callback
////////////////////////////////////////

Simple A+B with arrays

//////////////////////////
//Simple A+B with arrays
/////////////////////////

var ArrayInA , ArrayInB : Tparameter;     // first, we declare the parameters we will need.
var Result    : Tparameter;

// Init is started on script compilation to create the in/outs
PROCEDURE INIT;
BEGIN                                     // Each procedure start with a Begin

  ArrayInA:= CreateParam('InputA',PtArray); 
  SetIsOUTput(ArrayInA,FALSE);  // we can set the displayed name between'',
                                // and the type, here we use PtArray.
  ArrayInB:= CreateParam('InputB',PtArray); 
  SetIsOUTput(ArrayInB,FALSE);  // ...and specify if it's an input or output
                                // (by default they are booth);
  Result:= CreateParam('Result',PtArray); 
  SetIsINput(Result,FALSE);     // to specify if in or out we set off the pin we
                                // don't want.

  SetMin(ArrayInA,0); 
  SetMax(ArrayInA,127);          // By default arrays have a min/max range of 0..100, we can change like
                                 // this.
  SetMin(ArrayInB,0); 
  SetMax(ArrayInB,127);        
  SetMin(ArrayInB,0); 
  SetMax(Result,127);
END; 

// Callback procedure                      // inputs have an id (n), the callback procedure scans the inputs to detect
                                           // changes, and note the index if detecting.
var L,LA,LB, i: integer;                   // we declare the variables we will need. i to loop thru the array, L as a
                                           // length variable, booth are entire nb so integers.
var valTmp: single;                        // valtmp will be use to temp store the value to make code more spread and
                                           // clear, here we want a float so single.
Procedure Callback(N:integer);             // it's a procedure that will make the result in same bloc as soon as the
                                           // input has changed
BEGIN                                      // start the callback (=changes detection)

 if (n = ArrayInA) OR (n = ArrayInB) then begin     // we will trig the computation ONLY if callback detected changes
                                                    // on inA or inB. As inputs are indexed from 0 to n,
                                                    // we could also have used " if (n=0) or (n=1)"
   LA:= GetLength(ArrayInA);                        // first we get the length of arrayA and B;
   LB:= GetLength(ArrayInB);                        // As scipt would make errors trying to set an array elemnt that
                                                    // doesn't exist, we ll pick the min length as length.
   L:=  MinI(LA,LB);                                // so L is the min integer beetween length of A and B, and we will
                                                    // set ArrayOut of that lenght and performs operation
   SetLength(Result,L);                             // in that range.

   for i:=0 to L -1 do begin                        // loop scan. as indexes start from 0 and not 1, the loop need to
                                                    // go from 0 to length-1.
     valtmp:= getDataArrayValue(ArrayInA,i) + getDataArrayValue(ArrayInB,i);     // sum the values of a and b to the
                                                                                 // float variable valtmp.
     SetDataArrayValue(Result, i, valtmp);          // and set to same scanned index the arrayout with that value
   end; //loop                                                   
 end; //sum         end of summing code

END;//Callback
//////////////////////////////////////

N inputs to 1 output

//////////////////////////
// N inputs to 1 output. 
//////////////////////////
const nb-ins = 8;  //define number of inputs all going to output.(summed).

var INPUTS : array of Tparameter;
var output : tparameter;
var i      : integer;
var valtmp : single;

Procedure Init;
begin

 setArrayLength(INPUTS,nb-ins);
  for i:= 0 to nb-ins-1 do begin
   INPUTS[i]:=  CreateParam('input'+IntToStr(i),PtDataField);  SetIsOutput(INPUTS[i],false);
  end;
  output:= createParam('output',Ptdatafield); setisInput(output,false);
  valtmp:=0;
end;// init

Procedure Callback (n:integer);
Begin
 if n>=0 then begin
   valtmp:= getValue(inputs[0]);
    for i:= 1 to nb-ins-1 do begin
      valtmp:= valtmp + getValue(Inputs[i]);
    end;
  setValue(output,valtmp);
  end;

end;// callback

find max value in array

Also returns value and pos, module equivalent

////////////////////////////////////////////////////
// find max value in array and return value and pos, module equivalent
// will search for the first occurence of a value and output is position
/////////////////////////////////////////////////////
var Arr, Pos, MaxVAlue : Tparameter;

Procedure Init;
BEGIN
  Arr := CreateParam('Arr',PtArray);           
  setisOutput(Arr,false);
  Pos := CreateParam('pos',PtDataField);       
  setisInput(Pos,false);
  Maxvalue := CreateParam('max value',PtDataField); 
  setisInput(maxvalue,false);
END;

Procedure Callback (n: integer);
var i, maxPos : integer;
var currVal, maxVal : single;
begin

  if (n=Arr) then begin
   maxVal := -1; // assuming the lowest value in the array is above -1
   maxPos := -1;
   for i := 0 to GetLength(arr)-1 do begin  // scan the array elmts
      currVal := GetDataArrayValue(arr, i); // store the scanned elmt as Currentval variable
      if currVal > maxVal then begin        //if value is >-1 it's become the new max compare value
         maxVal := currVal;
         maxPos := i;                       // scanned index correspond to max position
      end;
   setvalue(maxvalue,maxval);      // we set our parameters with results variables
   setvalue(pos,maxPos);
   end;
 end;
END;

Find Value in Array

////////////////////////////////////////////////////
// find value in array
// will search for the first occurence of a value and output is position
/////////////////////////////////////////////////////
var Arr, Pos, Valsearch : Tparameter;

Procedure Init;
BEGIN
  Arr := CreateParam('Arr',PtArray);           
  setisOutput(Arr,false);
  ValSearch:= CreateParam('Valsearch',PtDataField); 
  setisOutput(Valsearch,false); setvalue(valsearch,1);
  Pos := CreateParam('pos',PtDataField);       
  setisInput(Pos,false);
END;

Procedure Callback (n: integer);
var i, firstPos : integer;
begin

  if (n=Arr) then begin
   firstPos := -1;
   i := 0;
   while ((firstPos = -1) and (i < GetLength(arr))) do begin // on the length of array
      if (GetDataArrayValue(arr, i) = getValue(Valsearch)) then  begin       // checking for first occurence with
                                                                             // value = our value.
         firstPos := i;                                      // storing found index
         setvalue(Pos,firstPos);                             // seting pos parameter to that index
      end
      else begin                                             // if not found keep scanning
         i := i + 1;
      end;
   end;
END;

Check if a Value is in the Array

////////////////////////////////////////////////////
// Check if a value is in the array
/////////////////////////////////////////////////////
var Arr, Found, Valsearch : Tparameter;

Procedure Init;
BEGIN
  Arr        := CreateParam('ArrayIn',PtArray);                 
  setisOutput(Arr,false);
  ValSearch  := CreateParam('Value to search',PtDataField); 
  setisOutput(Valsearch,false); setvalue(valsearch,1);
  Found      := CreateParam('value found',PtSwitch);        
  setisInput(Found,false);
END;
//////////////////////////////////////////////////////////////////////
Procedure Callback (n: integer);            // Scan the N inputs, and n pick the index of any input that changed to
                                            // return result in same bloc.
var valFound : boolean;                     // "is our value found?" is a boolean = binary choice TRUE or FALSE type
                                            // of variable
var i : integer;                            // integer used to scan thru the array elements.
BEGIN                                       // Start the callback = check inputs changes

 IF (n=Arr) then begin                      // if changes on Arr input, start
   i := 0;                                  // reset i (=index to scan)
   valfound:=FALSE;                         // by default the value is not found
   while ((not valFound) and (i < GetLength(arr))) do begin           // do the check for each elmt among array
                                                                      // length as long as value isn't found
      valFound := (GetDataArrayValue(arr, i) = getvalue(Valsearch) ); // boolean result of the check we want to do ,
                                                                      // TRUE if the array elmt(i) = our input value
      i := i + 1;                                                     // increment the scanned elmt
   end;
   if valfound then begin                    //if the result of valfound is true, we set parameter to 1
      setvalue(Found,1);
   end 
   else begin                            //otherwise we set it to 0;
      setvalue(Found,0);      
   end;  
 END;              //end of subcheck process
END;              // END of callback

Bloc counter with Max Value and refresh speed

/////////////////////////////////////////////////////////////
//Bloc counter with Max Value and refresh speed
// If start pulse will count incremental till max value is reached
// increment is timespaced of nb-blocs input.
////////////////////////////////////////////////////////
VAR start, counter, maxVal,counting,NB-blocs : tParameter;
VAR count : Integer;

PROCEDURE Init;
BEGIN
   start := CreateParam('start', ptButton);
   SetIsOutput(start, FALSE);
   maxVal := CreateParam('max', ptDataField);
   SetIsOutput(maxVal, FALSE);setValue(maxVal,16);
   NB-blocs := CreateParam('nb-blocs', ptDataField);
   SetIsOutput(NB-blocs, FALSE); setValue(Nb-Blocs,100);
   counter := CreateParam('counter', ptDataField);
   SetIsInput(counter, FALSE);
   counting := CreateParam('counting', ptDataField);
   SetIsInput(counting, FALSE);
END;

//PROCEDURE Callback(n : Integer);
var maxvaltmp : integer;
PROCEDURE Process;
BEGIN

   IF (GetValue(start) = 1) THEN BEGIN                          //if start pulse of button we start the process
      setvalue(start,0);
      maxValtmp:= round(getValue(maxVal)*getValue(NB-blocs));   // we get the maxvalue to count to. we muliply by
                                                                // nb-blocs for the  speed.                            
      count := 0;                                               //we reset count to 0 
      SetValue(counting,1);                                     //we start counting so set the parameter to 1.
      END
      ELSE BEGIN                                                //as soon as the button went back to 0 will count.
          count := count + 1;
      END;

      while count > maxValtmp                              // if count reached maxvalue, it keeps that value
          do begin                                         // and set counting parameter to 0.
          count := maxValtmp;
          SetValue(counting,0);
      END;

   SetValue(counter, count div round(getValue(NB-blocs))); //we set the value to counter output. (we divide by
                                                           // speed. it's the fastest possible clock in usine
                                                           // that will increment each bloc (3ms at 128samples)
                                                           // and ensure a strong jiter free sync over 
                                                           // time counters.
                                                           // it can be used to set some arrayvalue, dispatchers,
                                                           // ask osc datas ect...
END;

Pass If Chg Array

////////////////////////////////////////////////////////////////////////////////////////////
// Pass If Chg Array create an array where only values that changed in array in will pass //
// As it need 2 blocs (one to check, one to store) it relies on Process procedure         //
// The callback will trig a counter. at bloc 0 it will compare new array with previously  //
// stored one. At bloc 1 it will store the array to be compared next                      //
// The values that didn't changed are those where substracting equal 0, will set          //
// corresponding output length to 0, behaviour of passifChange in Usine                   //
////////////////////////////////////////////////////////////////////////////////////////////
const SIZE = 16;          // nb of out wanted = size of array
const PREFIX = 'OUT-';   // PEFIX for outputs names, folowed by Integer;
var ArrayIn: Tparameter;
var OUTPUTS: Array Of Tparameter;     // we will use an array of Tparameters, to allow N creations easily
var i,l,count: integer;               // we declare the variable we will use and their type
var valtmp:    single;
var ArrayLast: Array of single;       // ArrayLast will be used to store array to check wich values changed on new in
                                      // chg.

PROCEDURE INIT;
BEGIN

  ArrayIn :=CreateParam('ArrayIn',PtArray); 
  SetIsOutput(ArrayIn,False);
  setMin(arrayIn,0);setMax(ArrayIn,127);

  SetArrayLength(OUTPUTS,SIZE);       // we set the arraylength to create N outputs of size defined by constant on top
                                      // of script.
   for i:=0 to SIZE -1 do begin
     OUTPUTS[i]:= CreateParam(PREFIX+IntToStr(i),PtDataField); 
     SetIsInput(OUTPUTS[i],false);
   end;   // we create the outputs, names with the prefix followed by their corresponding nb.

   setArrayLength(ArrayLast,SIZE);   //we set the memory array the same size
   setdontsave(arrayIn,false);       // by doing this the arrayin can be saved to Pm or on quiting the patch
END;//Init

PROCEDURE Callback(N:integer); 
BEGIN  
   if (n=arrayIn) then begin
       l:= getlength(ArrayIn);
       count:=-1;                 // if detected arrayin chg, we trig the counter for process to start.
     end;
END;//callback

PROCEDURE PROCESS;
BEGIN

   if count >= -1 
   then begin                    //start counting when callback has set count to -1
   count:=count+1;               // -1+1=0 so at first bloc count is 0
   end;

   if count = 0 
   then begin        // at first bloc we compare all values of new array with previous one.
     for i:=0 to l-1 
     do begin
       if ArrayLast[i]-getDataArrayValue(ArrayIn,i)<>0 
       then begin   // if there is a difference that mean
           valtmp:= getDataArrayValue(ArrayIn,i);                   //this value has changed
           setLength(OUTPUTS[i],1);                         // we make a size 1 to activate data pass
           setValue(OUTPUTS[i],valtmp);                    // and set the value with discovered one
       end
       else begin                                 // otherwise if no differences
          setLength(OUTPUTS[i],0);              // we setLength of 0 = don't pass.
       end;
     end;
   end;
   if count = 1 
   then begin                // on next bloc we store the array to internal memory for next compare.
       for i:=0 to L-1 
       do begin
           ArrayLast[i]:=getdataArrayValue(ArrayIn,i);
           setLength(OUTPUTS[i],0);                // and make no values pass anymore.
       end;
       count:=-2;                                      // and stop the counter as we reset it to -2
   end;

end;

Steped-clocks

/////////////////////////////////////////////////////////////////////////////////////
// PPQ Clocks gen- 23fx2K10- generate clocks from PPQ, each next one beeing divided by 2
// Useful to override module clock directly contrlling their step position.
//
// Enter a nb of max bars, that will correspond to the longest cycle (clock0) ie 4bars
// then clock1 will be clock0/2 ie 2 bars ect.
// to match module enter the nbof steps/bar you want. ie if a line module
//   with a 32 step/bar enter 32.
////////////////////////////////////////////////////////////////////////////////////
const NB-CLOCKS = 5; //    nb of clocks output to generate.

var PPQIN, STEPSperbar, MaxBars, SUBDIV: Tparameter;
var CLOCKS : Array of Tparameter;
var i: integer;
var PREC: single;
/////////////////////////////////////////////////////////
// initialisation : create parameters
procedure init;
BEGIN  
  PPQIN      := createParam('PPQ IN',PtDataField);      
  setIsOutput(PPQIN,false);
  StepsPerBar:= createParam('StepsPerBar',PtDataFader);  
  setIsOutput(StepsPerBar,false); 
  SetMin(StepsPerBar,16);   
  SetMax(StepsPerBar,256);
  MaxBars    := createParam('Max Bars',PtDatafader);    
  setIsOutput(MaxBars,false);     
  SetMin(MaxBars,1);     
  SetMax(MaxBars,32);
  SUBDIV     := createParam('SUBDIV',PtDatafader);      
  setIsOutput(SUBDIV,false);      
  SetMin(SUBDIV,1);     
  SetMax(SUBDIV,32);
  setArrayLength(Clocks,NB-CLOCKS);                 // the array of tparameters will create the n clocks
  for i:=0 to NB-CLOCKS-1 
  do begin
    CLOCKS[i]:= CreateParam('Clock'+IntToStr(i),PtDataField); 
    SetIsInput(Clocks[i],false);
  end;

END;//INIT
////////////////////////////////////////////////
// Callback procedure
var StepsBar,READSTEP,MODSTEP: integer;
Procedure Callback(N:integer); 
BEGIN

  If (n=PPQIn) 
  then begin
      PREC:= MAXS(1,getValue(SUBDIV));              // a mod B only works with integers. we muliply by prec for more
                                                    // precision, it also need to be above 1.
      StepsBar:= round(getValue(stepsPerBar));      // we need to know how many steps per bar, we use rounding as it's
                                                    // an integer and getvalue is float.
         for i:= 0 to NB-Clocks-1 
         do begin
             READSTEP:= round(getValue(PPQIN)*PREC*(StepsBar/4));       // PPQ goes from 0 to 4 a bar, so we divide
                                                                        // by 4 and muliply by steps per bar wanted.
             MODSTEP:= round((getvalue(maxBars)*StepsBar*PREC) / power(2,i)); // first clock will reset once maxbars
                                                                              // is reached, each next divide by 2 is
                                                                              // length
             SetValue(Clocks[i],(READSTEP mod MODSTEP)/PREC); // as we multiplied by prec to be able to mod more values, we
                                                             // also have to divide result
         end;
  end;
END;//Callback
////////////////////////////////////////////////////

See also

version 3.0.150

Edit All Pages