Welcome to %s forums

BrainModular Users Forum

Login Register

[script]GetDataArrayValue parameter conflict

I need help on a Patch
Post Reply
ethnix73
Member
Posts: 604
Location: France, Caen
Contact:

Unread post by ethnix73 » 08 Dec 2010, 12:52

Trying to dig in script, i face a simple problem, i'm not able to resolve.

I want to get the value of an array (inlet) at a specified index (inlet).
If that value (y)=0, set it to 1.
If that value (y)=1, set it to 0.

But i've got parameter conflict between SINGLE and INTEGER with the function GetDataArrayValue

Does i need to create another array (array of single) and feed it with the array(inlet)?

Code: Select all

//////////////////////////
// 
/////////////////////////
// parameters declaration

var ArrayIn : Tparameter;
var Index : Tparameter;
var ArrayOut : Tparameter;
var Out : Tparameter;
var x,y : single;


// initialisation : create parameters
procedure init;
begin  
ArrayIn :=CreateParam('ArrayIn',ptArray);
SetisOutput(ArrayIn,False);

Index :=CreateParam('Index',ptDataFader);
SetIsOutput(Index,FALSE);

ArrayOut :=CreateParam('ArrayOut',ptArray);
SetisInput(Arrayout,FALSE);

Out :=CreateParam('Out',Ptdatafield);
SetisInput(out,FALSE);
setformat (out,'%.0f');
end;

// Callback procedure
Procedure Callback(N:integer); 
begin
x :=GetValue (Index);
y :=GetDataArrayValue(ArrayIn,x);
end;

// Global variables



//////////////////////////////
// main proc
//////////////////////////////
Procedure Process;
begin
if y=0 then begin
SetValue (out,1)
end;
If y=1 then begin
SetValue (out,0)
end;
end;
Seb.Dub

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 08 Dec 2010, 13:20

to convert a single(float) to integer you need to use either the TRUNC or ROUND functions.
as you would imagine:
trunc(1,56) = 1
round(1,56) = 2

Indexs in arrays are always integers, but using getValue returns floats, so in your case you should use:

X:= trunc(getValue(Index));

and X can be declared as an integer, instead of single using var X : integer; on top of script.

ethnix73
Member
Posts: 604
Location: France, Caen
Contact:

Unread post by ethnix73 » 08 Dec 2010, 13:27

Thanks 23fx23!

i tried to use trunc or round, but i see that i had not used it correctly!
I added another script line after x :=GetValue (Index); like this Trunc(x)

So in script, we can encapsulate function within others with just parenthesis?
Seb.Dub

23fx23
Member
Posts: 2545
Contact:

Unread post by 23fx23 » 08 Dec 2010, 13:33

yes you can conbine with parenthesis to make more compact:
for exemple can also, instead of using two "if 0 set to 1, if 1 set to 0" use "set to (1-value) working for booth cases,
and directly set the array picking index without having separated the two process (but more clear to separate sometimes),
also you can put the code in callback to be computed only on inputs changes:
just as exemple compacted above equivalent:

Procedure Callback (n=integer);
Begin
IF (n=Arrayin) or (n=indexin) then begin
value:= getDataArrayValue( ArrayIn, round ( getValue (indexIN) ) );
setValue(valueOut, 1 - value);
end;
END//callback


and could even compact/combine even more:
IF (n=Arrayin) or (n=indexin) then begin
setValue( valueOut, 1 - getDataArrayValue( ArrayIn, round ( getValue (indexIN) ) ) );
end;


But at some point you can get lost in parenthesis errors and can be difficult to decypher, but i did it as an exemple to
show you can combine.

ethnix73
Member
Posts: 604
Location: France, Caen
Contact:

Unread post by ethnix73 » 09 Dec 2010, 12:57

Thanks for your example and explanation 23fx23...

I'm working on an mlr like patch. Script is really usefull and low on cpu!
Seb.Dub

Post Reply

Who is online

Users browsing this forum: No registered users and 74 guests