genexpr not working

Charley Lanusse's icon

Hi,

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.

Charley Lanusse's icon

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..

Wesley Smith's icon

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.

Charley Lanusse's icon

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

Wesley Smith's icon
Max Patch
Copy patch and select New From Clipboard in Max.

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:

Charley Lanusse's icon

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