[script] max array
im in my first steps with usine script, so far so good i slowly manage to understand and get what i want.
im just facing a little wall:
is it posible/how to get maxarray position/value by script? is there a build in fonction or a math trick?
i wonder how t deal with position in array.
for example to make it simpler than a max array i scan an array where only one value is 1,all other are 0.
with "for i =0 to L-1" do begin,
how can i know, let's say im making a check if value=1 , the position of the value=1 in the array, if test ok, send i to another variable? edit: mm yes seems working cool,
still, any ideas on max array?
im just facing a little wall:
is it posible/how to get maxarray position/value by script? is there a build in fonction or a math trick?
i wonder how t deal with position in array.
for example to make it simpler than a max array i scan an array where only one value is 1,all other are 0.
with "for i =0 to L-1" do begin,
how can i know, let's say im making a check if value=1 , the position of the value=1 in the array, if test ok, send i to another variable? edit: mm yes seems working cool,
still, any ideas on max array?
Here's an example:
[c]var i, maxPos : integer;
var currVal, maxVal : single;
begin
maxVal := -1; // assuming the lowest value in the array is above -1
maxPos := -1;
for i := 0 to GetLength(arr)-1 do begin
currVal := GetDataArrayValue(arr, i);
if currVal > maxVal then begin
maxVal := currVal;
maxPos := i;
end;
end;[/c]
[c]var i, maxPos : integer;
var currVal, maxVal : single;
begin
maxVal := -1; // assuming the lowest value in the array is above -1
maxPos := -1;
for i := 0 to GetLength(arr)-1 do begin
currVal := GetDataArrayValue(arr, i);
if currVal > maxVal then begin
maxVal := currVal;
maxPos := i;
end;
end;[/c]
Bjørn S
mm oh yeah cool,thanks a lot master Bsork, i will test that asap.
works like a charm, thanks again bsork, that's the clever trick i was missing, override the maxVal.
will try to time space my questions hehe.
the habit i need to get rid off is im always trying to directly set the tparameter variables, wich naturally returns me a type incompatibility error. "tparameter vs single ect."
in fact we need to always have a double "stamp" buffer variable to define it's type that's it if i well understood?
so back to my script explorations, managed to make a color array rgb to bgr i was after, (with help of senso examples) very happy.
damned im starting to love script, my social life is definitively ruined hehe..
will try to time space my questions hehe.
the habit i need to get rid off is im always trying to directly set the tparameter variables, wich naturally returns me a type incompatibility error. "tparameter vs single ect."
in fact we need to always have a double "stamp" buffer variable to define it's type that's it if i well understood?
so back to my script explorations, managed to make a color array rgb to bgr i was after, (with help of senso examples) very happy.
damned im starting to love script, my social life is definitively ruined hehe..
-
Clearscreen
- Member
- Posts: 482
- Location: Australia
- Contact:
For the sake of me learning stuff, would it be possible to post the entire script here? I can't get this to work for the life of me!!:(
trying to get my head around scripts but struggling badly at the moment...
trying to get my head around scripts but struggling badly at the moment...
so here is whole script, with ew comments hope can help, if copy and paste should work directly ...like to see if that's the good way do do it, i usually set some "tmp" variable that only at the end set "tparameters", is that the good way of doing master bsork?
Max array pos/value script:
Max array pos/value script:
//////////////////////////
//
/////////////////////////
// parameters declaration
var arrayin: Tparameter; // here we declare "visible" in-outs/
var maxVal: Tparameter;
var maxPos: Tparameter;
// initialisation : create parameters
procedure init;
begin // start parameters creation.
Arrayin := CreateParam('arrayin',ptArray); // here we choose
SetIsOutput(ArrayIn,false); // if they are in or outs, ie here set output false let only input visible.
SetMax(ArrayIn,MAXINT); // and overide default max value wich is 100 for arrays to maxpossible integer (16777216)
MaxVal := CreateParam('maxval',ptDataField);
SetIsinput(MaxVal,false);
MaxPos := CreateParam('maxPos',ptDataField);
SetIsinput(MaxPos,false);
end; // end paraeters creation.
// Callback procedure
Procedure Callback(n:integer); // here we declare other needed variables and types
var i, maxposTmp : integer; // i and maxpos are integers, as a position is an entire nb ie.
var currVal, maxvalTmp : single; // currval and max val are some float values, so they are "singles"
begin // we start main process
if n = 0 // will process only if 1st input changes (here arrayIn) , to not process constantly.
then begin
maxvalTmp := -1; // assuming the lowest value in the array is above - 1. starting point.
maxPosTmp := -1;
for i := 0 to GetLength(arrayin)-1 do begin // scan whole array from pos 0 to length-1
currVal := GetDataArrayValue(arrayin, i);
if currVal > maxvalTmp then begin //if find a value >-1,
maxvalTmp := currval; // the found value become new max for test
maxPosTmp := i; // and the i value is then maxpos
end;
end;
end;
SetValue (maxVal,maxValTmp); // we set paameters values with the one we found.
SetValue (maxPos,maxPosTmp);
end; // always have good nb of begins and ends.
between, I try to go deeper:
for my liveosc trigger grid, I got a 8x8 grid. live returns me a 64array (8clips of first track then 8 of 2nd track, ect) with their status.
(0 noclip 1, has clip 2 clip is playing). I had made a patch version that extract each sub array (from 0to7, 8to 15 ect and check
max array position so i got a result pertrack of "clip playing nb" position in the column. so, well it's working, i could stick with that...
i try to recreate viascript so i could later easy change grid size as i plan to use matrix, and i may gain cpu probably, and feel
it's a good challenge to understand script logic..
but seems far more complicate, im failing from a few hours.. I don't manage to make a correct loop scaning from 1to8 vertically, (to get max pos of "track" subarray), then continue process for 1-8horizontally, to finnally get a 8array with each track clip playing position...
what i try do deal is making a "for j = 0 to 7" then do "for i = 0 to 7", but all my tries fail., any enlightment or tip to use?
mmm is it related to "bi-dimentional" arrays i also try to test with benjamin pdf, but without success yet, or no just correctly set
the 64 arry with good indexes..arf im lost
i got it working for one subarray using previous script, so i probably could copy/adapt 8x the process, but i feel this could be done in cleaner way using one process only maybe? script masterz? (hehe now benjamin is no more there i think only you bsork remain as a script master, with senso, amiga909,martignasse, woodlsanding? why do they all stay in darkness hehe.
I hope as soon as i got main logic keys i ll stop hammering questions, but at start, it can avoid hours of debugging..
for my liveosc trigger grid, I got a 8x8 grid. live returns me a 64array (8clips of first track then 8 of 2nd track, ect) with their status.
(0 noclip 1, has clip 2 clip is playing). I had made a patch version that extract each sub array (from 0to7, 8to 15 ect and check
max array position so i got a result pertrack of "clip playing nb" position in the column. so, well it's working, i could stick with that...
i try to recreate viascript so i could later easy change grid size as i plan to use matrix, and i may gain cpu probably, and feel
it's a good challenge to understand script logic..
but seems far more complicate, im failing from a few hours.. I don't manage to make a correct loop scaning from 1to8 vertically, (to get max pos of "track" subarray), then continue process for 1-8horizontally, to finnally get a 8array with each track clip playing position...
what i try do deal is making a "for j = 0 to 7" then do "for i = 0 to 7", but all my tries fail., any enlightment or tip to use?
mmm is it related to "bi-dimentional" arrays i also try to test with benjamin pdf, but without success yet, or no just correctly set
the 64 arry with good indexes..arf im lost
i got it working for one subarray using previous script, so i probably could copy/adapt 8x the process, but i feel this could be done in cleaner way using one process only maybe? script masterz? (hehe now benjamin is no more there i think only you bsork remain as a script master, with senso, amiga909,martignasse, woodlsanding? why do they all stay in darkness hehe.
I hope as soon as i got main logic keys i ll stop hammering questions, but at start, it can avoid hours of debugging..
-
Clearscreen
- Member
- Posts: 482
- Location: Australia
- Contact:
Thansk 23fx23!! It works a treat - the notes are very helpful too - I'll be scouring through this later...23fx23 wrote:so here is whole script, with ew comments hope can help, if copy and paste should work directly ...like to see if that's the good way do do it, i usually set some "tmp" variable that only at the end set "tparameters", is that the good way of doing master bsork?
I too use a lot of temporary/internal variables to hold intermediate values, to minimize the use of GetValue/SetValue. I'm pretty sure it can save a bit CPU - or at least spread the load - and it can also make the scripts more readable and easier to maintain.
For example, in scripts with a number of input parameters where nothing much is happening when those parameters change, I practically always have at least two variables; the input parameter and an internal one. Take for instance a bypass switch in a MIDI script. In most cases I would do something like this:
[c]
VAR pBypass : tParameter;
VAR pMidiIn, pMidiOut : tParameter;
...
VAR cntIn : Integer; // number of midi messages received
VAR bypass : Boolean;
...
PROCEDURE Init;
BEGIN
pBypass := CreateParam('bypass', ptSwitch); SetIsOutput(pBypass, FALSE);
pMidiIn := CreateParam('midi in', 'ptMidi); SetIsOutput(pMidiIn, FALSE);
pMidiOut := CreateParam('midi out', 'ptMidi); SetIsInput(pMidiOut, FALSE);
bypass := FALSE;
...
PROCEDURE Callback(n : Integer);
BEGIN
CASE n OF
pBypass : bypass := (GetValue(n) > 0);
pMidiIn : cntIn := GetLength(n);
...
END;
...
[/c]
About the 8*8 matrix, here's an example assuming an input array of size 64 with only whole numbers:
[c]
VAR matrix ARRAY[0..7, 0..7] OF Integer;
...
VAR i, j OF Integer;
FOR i := 0 TO 7 DO BEGIN
FOR j := 0 TO 7 DO BEGIN
matrix[i, j] := round(GetDataArrayValue(inArr, i*8+j));
...
END;
END;
...
[/c]
(In case the only thing you're doing within the loops are getting/setting the value (or any other single statement), you can drop BEGIN..END.)
For example, in scripts with a number of input parameters where nothing much is happening when those parameters change, I practically always have at least two variables; the input parameter and an internal one. Take for instance a bypass switch in a MIDI script. In most cases I would do something like this:
[c]
VAR pBypass : tParameter;
VAR pMidiIn, pMidiOut : tParameter;
...
VAR cntIn : Integer; // number of midi messages received
VAR bypass : Boolean;
...
PROCEDURE Init;
BEGIN
pBypass := CreateParam('bypass', ptSwitch); SetIsOutput(pBypass, FALSE);
pMidiIn := CreateParam('midi in', 'ptMidi); SetIsOutput(pMidiIn, FALSE);
pMidiOut := CreateParam('midi out', 'ptMidi); SetIsInput(pMidiOut, FALSE);
bypass := FALSE;
...
PROCEDURE Callback(n : Integer);
BEGIN
CASE n OF
pBypass : bypass := (GetValue(n) > 0);
pMidiIn : cntIn := GetLength(n);
...
END;
...
[/c]
About the 8*8 matrix, here's an example assuming an input array of size 64 with only whole numbers:
[c]
VAR matrix ARRAY[0..7, 0..7] OF Integer;
...
VAR i, j OF Integer;
FOR i := 0 TO 7 DO BEGIN
FOR j := 0 TO 7 DO BEGIN
matrix[i, j] := round(GetDataArrayValue(inArr, i*8+j));
...
END;
END;
...
[/c]
(In case the only thing you're doing within the loops are getting/setting the value (or any other single statement), you can drop BEGIN..END.)
Bjørn S
-
martignasse
- Site Admin
- Posts: 611
- Location: Lyon, FRANCE
- Contact:
...23fx23 wrote:what i try do deal is making a "for j = 0 to 7" then do "for i = 0 to 7", but all my tries fail., any enlightment or tip to use?
mmm is it related to "bi-dimentional" arrays i also try to test with benjamin pdf, but without success yet, or no just correctly set
the 64 arry with good indexes..arf im lost
maybe this thread can help you to understand array manipulation by scriptbsork wrote:Code: Select all
VAR i, j OF Integer; FOR i := 0 TO 7 DO BEGIN FOR j := 0 TO 7 DO BEGIN matrix[i, j] := round(GetDataArrayValue(inArr, i*8+j)); ... END; END; ...
http://www.sensomusic.com/forums/viewtopic.php?pid=11983#p11983
Martin FLEURENT - Usine Developer - SDK maintainer
woa really cool guys, i think this will deblock me a lot. thanks for sharing knowledge.
While we're on the subject of finding values in arrays, I thought I'd add an example of searching for the first occurence of a value, since it's another loop construct and can be easier on the CPU as it jumps out of the loop when the value is found:
[c]
var i, firstPos : integer;
begin
firstPos := -1;
i := 0;
while ((firstPos = -1) and (i < GetLength(arr))) do
if (GetDataArrayValue(arr, i) = 1.0) then // checking for first occurence with value = 1
firstPos := i
else
i := i + 1;
[/c]
When done this way, you'd probably have a IF-test elsewhere in the program checking whether firstPos >= 0 before doing anything.
Another variety if you're only interested to find whether a value is present in an array without bothering with where it's located, also showing how you can assign values to a boolean without using IF-statements:
[c]
var valFound : boolean;
var i : integer;
begin
i := 0;
while ((not valFound) and (i < GetLength(arr))) do begin
valFound := (GetDataArrayValue(arr, i) = 1.0);
i := i + 1;
end;
[/c]
[c]
var i, firstPos : integer;
begin
firstPos := -1;
i := 0;
while ((firstPos = -1) and (i < GetLength(arr))) do
if (GetDataArrayValue(arr, i) = 1.0) then // checking for first occurence with value = 1
firstPos := i
else
i := i + 1;
[/c]
When done this way, you'd probably have a IF-test elsewhere in the program checking whether firstPos >= 0 before doing anything.
Another variety if you're only interested to find whether a value is present in an array without bothering with where it's located, also showing how you can assign values to a boolean without using IF-statements:
[c]
var valFound : boolean;
var i : integer;
begin
i := 0;
while ((not valFound) and (i < GetLength(arr))) do begin
valFound := (GetDataArrayValue(arr, i) = 1.0);
i := i + 1;
end;
[/c]
Bjørn S
oh really cool bsork, indede in my cases i could generally search for a value instead of searching maxarray, as i know the vaue i search is 2. not yet totally get it clear in mind, but i will test it and try to undersand, cool also for the isvalue present, as i can check if a track is playing in my case, so booth are really helpfull for me.
I just tried the matrix [i,j] and manage to make a 8x extract subarray using it, start to understand, will try to go further checking valuesin subarrays, your help is like god light for me hehe, reaaaallly appreciate it, thanks once more, that rocks..
I just tried the matrix [i,j] and manage to make a 8x extract subarray using it, start to understand, will try to go further checking valuesin subarrays, your help is like god light for me hehe, reaaaallly appreciate it, thanks once more, that rocks..
your tricks works really great, helped me a lot simplify things!
Any tricks on concating array, do we need to manually fill the bigger with "for i to" or there is a function, or is it +?
Any tricks on concating array, do we need to manually fill the bigger with "for i to" or there is a function, or is it +?
"+" only works on strings AFAIK (remember, strings are essentially arrays of characters). I've taken a look at some Delphi documentation and found the Copy command, but I dont' know if that one will work in Usine - it's not listed in the scripting language Wiki.23fx23 wrote:Any tricks on concating array, do we need to manually fill the bigger with "for i to" or there is a function, or is it +?
There are a lot of Delphi resources on the Net, probably of varying quality, but the one I've been using most is Delphi Basics, which I find well written with good tutorials. Not everything in Delphi is available in scripts of course, but it doesn't hurt to try, does it?
[Edit] Sorry, I was too quick when looking at the Copy command: As you cannot specify any target index, the copied elements will always be put into the first elements of the target array. There is a Concat command though, but that one's for strings.
Bjørn S
yup thanks a lot bsork, im gonna check that ressources and test everything i can hehe. cool
really happy i could get your examples in concrete really helping situations. thanks once more.
im quite sure not everything is avaible in scripts, but as well not everything is listed in script reference, as i found some shr or other fonctions not listed, so..exploring!
really happy i could get your examples in concrete really helping situations. thanks once more.
im quite sure not everything is avaible in scripts, but as well not everything is listed in script reference, as i found some shr or other fonctions not listed, so..exploring!
Who is online
Users browsing this forum: No registered users and 130 guests
