Forums > Gen

Arguments in GenExpr? Functions returning multiple values?

September 29, 2013 | 12:52 pm

Okay, two quick questions. I’m finally getting around to digging into GenExpr and fighting the extremely sparse and opaque documentation. Apologies if my terminology is incorrect; I don’t have an "actual" coding background per se.

First, the GenExpr vignette says that:

In Gen patchers, some objects have attributes like the Jitter Gen operator sample, which as a boundmode attribute. In GenExpr, function arguments correspond to operator inlets and function return values correspond to outlets. Attributes are set using a key/value style argument. For example:

out = sample(in, norm, boundmode="mirror");

Since this is the case, is it possible to implement similar attributes in my own GenExpr functions? As an simple example case, I’ve tried:

stringNum(x, outNum="zero") {
n = outNum;
if(n = "zero") {
y = 0;
}
else if(n = "one") {
y = 1;
}
else if(n = "two") {
y = 2;
}
return y;
}

Param outNum("zero");

out1 = stringNum(outNum=outNum);

This fails to compile in a codebox; I get "expression missing ‘(‘" on line 3 (the first "if" statement). Am I missing something in terms of syntax?

Second, I’ve read the vignette several times and I’m still not clear on how to define a function in GenExpr that takes one value and returns (for example) three, then call that function in a way that returns only a given one or two of those values.

The specific case I have in mind is the definition of functions representing integrators for filter design which might need to return values from any of three "nodes" depending on the context in which they’re called.

I’m sure that this is remedial stuff for anyone with an proper DSP or coding background, but in the absence of better documentation, I’m afraid that I’m stuck. Any advice would be very much appreciated!


October 7, 2013 | 8:41 am

You can’t add property like that in your function this is only for builtin object.

Here’s an example of function which takes one argument and output 3 values:

<code>

– Pasted Max Patch, click to expand. –

</code>


October 7, 2013 | 10:08 am

You can’t add property like that in your function this is only for builtin object.

No problem; easy enough to live without. Just wondered whether I was missing something.

Here’s an example of function which takes one argument and output 3 values:

Thanks very much for this. The example is clear, but what I was actually after was how to define a function that returns multiple values, then call that function later in the same codebox and get an arbitrary set of those values. Anyway, I’ve worked that out now. You just have to assign every return value you don’t need to "notused" when you call the function.

So with regards to your example patch, if you wanted to call threeRandom and get only the x2 and x3 outputs, you’d do:

notused, out1, out2 = threerandom(in1);

This is alluded to in the GenExpr vignette, but it went a bit over my head at the time as I’m only just getting used to text-based programming.

Anyway, I’ll add an illustrative example that should help anyone who has the same problem in the future. The gen~ defines function threeWave(), which returns three shifted (add + wrap) phasors; outlets 1-3 are assigned to with:

out1, out2, out3 = threeWave();

while outlet 4 is assigned to with:

notused, out4, notused = threeWave();

In the top-level patch, the scopes display the phasors from out1 and out3 plus a null test proving the identity of out2 and out4.

<code>

– Pasted Max Patch, click to expand. –

</code>


Viewing 3 posts - 1 through 3 (of 3 total)