genexpr not working
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.
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..
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.
On further investigation, the problem only happens when using history.
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. --Copy all of the following text. Then, in Max, select New From Clipboard.----------begin_max5_patcher---------- 1114.3ocyWssbaaCD8YpuBT7PZxLxJ.Th5RcSmou0G5GPmwtiGHPXIjPAvg. xVNYR916R.RJxHJJFGWY8htr6hEm8r2H+xf.7R8NgAi9MzMnffuLHHvIJWPP w+CvaX63ILiyLrgqSEeCOzqJkY4qkpU2kI3VuahlGNhLDENcV9Wiib+iNlLh f92hSIictRu7iWMqzSYL0Jg2CWQGEAGYTT0A3rDNWuU4thvBgpsajpDg07cB 0askRI4B+5fA4eLrmAnR7H.rRXYE6bWJluNGfUAt+Rr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kXA77PfG3OcGmAOSa60LUExEShGsYmde4LtbLLV92r +AW6VFr+dKdmB2M2r7z+JFeOkU7hFGRUM3pivSKZXyAbzg7yAuwSegyhdfF5 kEZBOavYZOfyryFZ5Sgy7yJZnm.MSunPCM5YBGXNvWG7eNV7N4C -----------end_max5_patcher-----------
Ahh! Thanks! I was fooled by
scope~ too. Sorry for the false-alarm.
Forums > MaxMSP