Forums > MaxMSP

genexpr not working

Dec 06 2011 | 5:35 am


I’m having trouble with a genexpr I’m writing. The genexpr is a simple zero cross detector. I want the gen~ to output a 1 until the input signal has a positive going zero cross at which point I want a single 0 sample output.

Here is the code:

// zero cross

History n1(0);

n = in1;

crossup = ( n >= 0 && n1 < 0 ); // 0 on cross over, 1 otherwise
inverse = 1-crossup;

out1 = crossup; // this works as expected
//out1 = inverse;  // this doesn't work - it outputs a constant 1!

n1 = in1;

Notice the subtraction used to calculate inverse. crossup outputs correctly (0 all the time with a single sample of 1 on zero cross), but inverse stays constantly 1.

Am I doing something wrong?

I'm guessing that because genexpr is 'typeless', the compiler is guessing at types behind the scenes; and crossup is probably evaluated to be a bool. When I try to subtract a 'bool' from 1 something goes wrong with internal type casting and it fails. This is a TOTAL GUESS of course. More likely, I'm just doing something silly.

Dec 06 2011 | 5:44 am

In more testing, I’ve noted that this produces a negative of the signal

crossup = neg ( n >= 0 && n1 < 0 ) ;

But this ALSO fails to work

crossup = neg ( n >= 0 && n1 < 0 ) + 1.0;

There must be something happening when adding or subtracting numbers to bools..

Dec 06 2011 | 7:28 pm

Hi Lanusse,
There’s definitely something funny going on. You’re right that GenExpr is a typeless language and that types are inferred for code generation. In the case of gen~ though, everything is a double so there isn’t a bool. As far as I can tell, the fact that inverse is constant is a bug. We will have this resolved for the next release. Thanks for pointing it out.

Dec 06 2011 | 7:41 pm

On further investigation, the problem only happens when using history.

Dec 06 2011 | 7:41 pm

Actually, there is not bug. gen~ and GenExpr are working fine. I was fooled by scope~. If you hook a change~ to the inverse output, you’ll see that it’s working properly:

-- Pasted Max Patch, click to expand. --

Dec 06 2011 | 9:38 pm

Ahh! Thanks! I was fooled by scope~ too. Sorry for the false-alarm.

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

Forums > MaxMSP