Forums > MaxMSP

"if and only if" statement

May 29, 2008 | 3:32 pm

I’m trying to write an if and only if statement to trigger a button. I have three random values coming in and I want the button to turn on only if the three values are present at the same time.

Example:
Random Number: 1-10,
Button On: 3,6,7 or 3,7,6 or 6,3,7 or 6,7,3 or 7,6,3 or 7,3,6.
NOT when just a 3, or 6, or 7 is present [which is what's happening now]

I’m trying to use the if command with &&, but I don’t think it’s working.

Thanks for any help.


May 29, 2008 | 3:47 pm

try using three == objects, then two && objects. would put it together right now, but i’m at a different computer and don’t have max available. but it should be easy enough.


May 29, 2008 | 4:06 pm

Here are two examples, one with the if object, one with the match object

Zachary

max v2;
#N vpatcher 341 44 891 521;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P comment 51 400 152 196617 < -- trigger what you want here;
#P newex 31 324 160 196617 t l clear;
#P newex 31 422 62 196617 print match;
#P newex 117 276 42 196617 print if;
#P comment 200 325 189 196617 2. using the match object (less flexible);
#P newex 366 364 64 196617 match 7 3 6;
#P newex 299 364 64 196617 match 7 6 3;
#P newex 232 364 64 196617 match 6 7 3;
#P newex 165 364 64 196617 match 6 3 7;
#P newex 98 364 64 196617 match 3 7 6;
#P newex 31 398 18 196617 t l;
#P newex 31 364 64 196617 match 3 6 7;
#P newex 117 196 136 196617 pack 0 0 0;
#P number 243 166 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 243 141 27 196617 + 1;
#P newex 243 115 58 196617 random 10;
#P number 180 166 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 180 141 27 196617 + 1;
#P newex 180 115 58 196617 random 10;
#P toggle 117 54 15 0;
#P newex 117 80 58 196617 metro 100;
#P number 117 166 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window linecount 3;
#P newex 117 225 367 196617 if ($i1==3 && $i2==6 && $i3==7) || ($i1==3 && $i2==7 && $i3==6) || ($i1==6 && $i2==3 && $i3==7) || ($i1==6 && $i2==7 && $i3==3) || ($i1==7 && $i2==6 && $i3==3) || ($i1==7 && $i2==3 && $i3==6) then $i1 $i2 $i3;
#P window linecount 1;
#P newex 117 141 27 196617 + 1;
#P newex 117 115 58 196617 random 10;
#P window setfont "Sans Serif" 12.;
#P comment 149 31 368 196620 pass bang or true only when list is comprised of 3 , 6 , and 7;
#P window setfont "Sans Serif" 9.;
#P comment 299 203 100 196617 1. using the if object;
#P connect 14 0 25 0;
#P connect 25 1 15 0;
#P fasten 25 0 15 0 36 352 36 352;
#P connect 15 0 16 0;
#P fasten 17 0 16 0 103 389 36 389;
#P fasten 18 0 16 0 170 389 36 389;
#P fasten 19 0 16 0 237 389 36 389;
#P fasten 20 0 16 0 304 389 36 389;
#P fasten 21 0 16 0 371 389 36 389;
#P connect 16 0 24 0;
#P connect 25 1 17 0;
#P fasten 25 0 17 0 36 352 103 352;
#P connect 7 0 6 0;
#P connect 6 0 2 0;
#P connect 2 0 3 0;
#P connect 3 0 5 0;
#P connect 5 0 14 0;
#P connect 14 0 4 0;
#P connect 4 0 23 0;
#P connect 25 1 18 0;
#P fasten 25 0 18 0 36 352 170 352;
#P fasten 6 0 8 0 122 106 185 106;
#P connect 8 0 9 0;
#P connect 9 0 10 0;
#P connect 10 0 14 1;
#P connect 25 1 19 0;
#P fasten 25 0 19 0 36 352 237 352;
#P fasten 6 0 11 0 122 106 248 106;
#P connect 11 0 12 0;
#P connect 12 0 13 0;
#P connect 13 0 14 2;
#P connect 25 1 20 0;
#P fasten 25 0 20 0 36 352 304 352;
#P connect 25 1 21 0;
#P fasten 25 0 21 0 36 352 371 352;
#P pop;


May 30, 2008 | 9:33 am

Your patch is something of a nightmare, I don’t know if anybody is going to debug it for you. But a couple of things to look at.

- Are you sure there are no right-to-left issues, esp. w/r/t your pack objects?

- Testing floats for equality is Bad Karma. Two floats may be very very close, but an equality test will evaluate to false. Floats are almost never equal.

- Since you’re starting off with ints, you would be *much* better off to stay in the int domain. There’s no point in testing ( < = 27.5) if [random 4000] is *never* going to produce a value in the range 27.5 <=x < 28. So instead of [if $f1 >= 27.5 && $f1 < =53.454 then $f1 else out2 $f1] you could do [if $i1 >= 28 && $i1 < =53 then $i1 else out2 $i1].

- Better still, use [split 28 53]. Isn’t that easier? Use floats if you must, but split is your friend.

- Encapsulate.

Using the above will simplify your patch enormously, making it easier to debug.

After writing the above, my gut instinct is that it’s some of the tests for float equality that are breaking your logic.

I also notice you’re generating frequencies with a flat distribution, although pitch perception is logarithmic. You will get far more high pitches than low pitches. Is that really what you want?

If it *is* what you want, you could use a negative exponential distribution to generate MIDI note numbers and do all your matching in the integer domain, and just convert to frequency before sending to cycle~ or whatever. Mtof will do the conversion, lp.expo will give you the exponential random numbers. Lp.expo is part of Litter Power Pro. You can also get a cheap approximation of the exponential distribution with lp.linnie from the Litter Starter Pack.


May 30, 2008 | 9:44 am

The server just killed a more detailed response:-(

Briefly:

- Testing floats for equality is a recipe for disaster. Don’t do it unless you understand digital floating point arithmetic. This is probably the bug in your patch.

- Most of your ifs would be more idiomatically done with the split object

- You’re starting off with ints, you will be better off to do your tests in the int domain.

So: instead of [if $f1 >= 27.5 && $f1 < =53.454 then $f1 else out2 $f1] do [split 28 53].

- There may or may not be right-to-left issues.

Finally: you’re generating frequencies with a flat distribution, but pitch perception is logarithmic. In short, there will be more high pitches than low pitches.

If that is really what you want, you would be better off to generate MIDI note values with a negative exponential distribution, do your test in the int domain, and convert to frequencies with mtof just before you pass the values on to cycle~ or whatever.

For exponential distributions you can use lp.expo from Litter Power Pro. You can get a cheap (rough) approximation of a negative exponential distribution with lp.linnie from the Litter Starter Pack.

Hope this helps,
Peter


May 30, 2008 | 4:38 pm

I’ve put together a smaller problem example which if solved, should help me on the grander scale.

Like before, I’m using iff statement to identify major and minor triads. The problem is that only the minor a-c-e and a-e-c are identified. I’ve also included a version using only integer numbers, but the problem is the same.

*Also, what exactly is the ‘right-to-left’ issue?

Thanks…

max v2;
#N vpatcher 0 44 1440 896;
#P origin 23 14;
#P window setfont "Sans Serif" 36.;
#P window linecount 1;
#P comment 796 128 159 196644 integer;
#P window setfont "Sans Serif" 9.;
#P comment 1122 435 100 196617 A MINOR;
#P newex 967 432 138 196617 pack 0. 0. 0. 0. 0. 0. 0. 0. 0.;
#P newex 1009 524 42 196617 print if;
#P button 977 520 15 0;
#P window linecount 3;
#P newex 966 461 400 196617 if ($f1==28 && $f5==33 && $f9==41) || ($f1==28 && $f6==41 && $f8==33) || ($f2==33 && $f4==28 && $f9==41) || ($f3==41 && $f4==28 && $f8==33) || ($f2==33 && $f6==41 && $f7==28) || ($f3==41 && $f5==33 && $f7==28) then sad;
#P toggle 1423 204 15 0;
#P toggle 1284 202 15 0;
#P toggle 1141 199 15 0;
#P toggle 1013 199 15 0;
#P toggle 895 204 15 0;
#P window linecount 1;
#P comment 1486 362 33 196617 E[9];
#P comment 1424 364 33 196617 C#[8];
#P comment 1337 363 33 196617 A[7];
#P comment 1390 362 29 196617 C[8];
#P comment 1189 366 33 196617 E[6];
#P comment 1127 368 33 196617 C#[5];
#P comment 1040 367 32 196617 A[4];
#P comment 1093 366 31 196617 C[5];
#P comment 912 364 33 196617 E[3];
#P comment 850 366 33 196617 C#[2];
#P toggle 768 210 15 0;
#P button 1487 312 15 0;
#P button 1427 316 15 0;
#P button 1390 314 15 0;
#P button 1340 316 15 0;
#P button 1189 314 15 0;
#P button 1129 318 15 0;
#P button 1092 316 15 0;
#P button 1042 318 15 0;
#P button 910 310 15 0;
#P button 850 314 15 0;
#P button 813 312 15 0;
#P button 763 314 15 0;
#P message 1420 341 20 196617 35;
#P message 1482 340 20 196617 41;
#P message 1385 341 20 196617 33;
#P message 1336 341 20 196617 28;
#P message 1125 343 20 196617 35;
#P message 1187 342 20 196617 41;
#P message 1090 343 20 196617 33;
#P message 1041 343 20 196617 28;
#P message 850 340 20 196617 35;
#P message 912 339 20 196617 41;
#P message 815 340 20 196617 33;
#P message 766 340 20 196617 28;
#P comment 763 365 27 196617 A[1];
#P comment 816 364 30 196617 C[2];
#P comment 786 207 33 196617 a-c-e;
#P comment 914 204 32 196617 a-e-c;
#P comment 1159 201 34 196617 c-e-a;
#P comment 1303 204 32 196617 e-a-c;
#P comment 1443 205 35 196617 e-c-a;
#P comment 377 624 100 196617 A MAJOR;
#P newex 224 620 138 196617 pack 0. 0. 0. 0. 0. 0. 0. 0. 0.;
#P newex 266 715 42 196617 print if;
#P button 234 711 15 0;
#P window linecount 4;
#P newex 223 652 355 196617 if ($f1==27.5 && $f5==34.65 && $f9==41.2) || ($f1==27.5 && $f8==34.65 && $f6==41.2) || ($f4==27.5 && $f2==34.65 && $f9==41.2) || ($f4==27.5 && $f8==34.65 && $f3==41.2) || ($f7==27.5 && $f2==34.65 && $f6==41.2) || ($f7==27.5 && $f5==34.65 &&$f3==41.2) then happy;
#P toggle 688 388 15 0;
#P toggle 543 387 15 0;
#P toggle 385 390 15 0;
#P toggle 257 393 15 0;
#P toggle 134 396 15 0;
#P toggle 9 397 15 0;
#P window linecount 1;
#P comment 27 396 38 196617 a-c#-e;
#P comment 153 396 41 196617 a-e-c#;
#P comment 275 392 44 196617 c#-a-e;
#P comment 403 392 45 196617 c#-e-a;
#P comment 562 389 47 196617 e-a-c#;
#P comment 708 389 51 196617 e-c#-a;
#P comment 731 558 33 196617 E[9];
#P comment 669 560 33 196617 C#[8];
#P comment 582 559 33 196617 A[7];
#P comment 635 558 29 196617 C[8];
#P comment 434 562 33 196617 E[6];
#P comment 372 564 33 196617 C#[5];
#P comment 285 563 32 196617 A[4];
#P comment 338 562 31 196617 C[5];
#P comment 157 560 33 196617 E[3];
#P comment 95 562 33 196617 C#[2];
#P button 732 508 15 0;
#P button 672 512 15 0;
#P button 635 510 15 0;
#P button 585 512 15 0;
#P button 434 510 15 0;
#P button 374 514 15 0;
#P button 337 512 15 0;
#P button 287 514 15 0;
#P button 155 506 15 0;
#P button 95 510 15 0;
#P button 58 508 15 0;
#P button 8 510 15 0;
#P message 665 537 37 196617 34.65;
#P message 727 536 31 196617 41.2;
#P message 630 537 31 196617 32.7;
#P message 581 537 31 196617 27.5;
#P message 370 539 37 196617 34.65;
#P message 432 538 31 196617 41.2;
#P message 335 539 31 196617 32.7;
#P message 286 539 31 196617 27.5;
#P message 95 536 37 196617 34.65;
#P message 157 535 31 196617 41.2;
#P message 60 536 31 196617 32.7;
#P message 11 536 31 196617 27.5;
#P comment 8 561 27 196617 A[1];
#P comment 61 560 30 196617 C[2];
#P comment 360 258 100 196617 A MINOR;
#P newex 205 255 138 196617 pack 0. 0. 0. 0. 0. 0. 0. 0. 0.;
#P newex 247 347 42 196617 print if;
#P button 215 343 15 0;
#P window linecount 4;
#P newex 204 284 343 196617 if ($f1==27.5 && $f5==32.7 && $f9==41.2) || ($f1==27.5 && $f6==41.2 && $f8==32.7) || ($f2==32.7 && $f4==27.5 && $f9==41.2) || ($f3==41.2 && $f4==27.5 && $f8==32.7) || ($f2==32.7 && $f6==41.2 && $f7==27.5) || ($f3==41.2 && $f5==32.7 && $f7==27.5) then sad;
#P toggle 661 27 15 0;
#P toggle 522 25 15 0;
#P toggle 379 22 15 0;
#P toggle 251 22 15 0;
#P toggle 133 27 15 0;
#P window linecount 1;
#P comment 724 185 33 196617 E[9];
#P comment 662 187 33 196617 C#[8];
#P comment 575 186 33 196617 A[7];
#P comment 628 185 29 196617 C[8];
#P comment 427 189 33 196617 E[6];
#P comment 365 191 33 196617 C#[5];
#P comment 278 190 32 196617 A[4];
#P comment 331 189 31 196617 C[5];
#P comment 150 187 33 196617 E[3];
#P comment 88 189 33 196617 C#[2];
#P toggle 6 33 15 0;
#P button 725 135 15 0;
#P button 665 139 15 0;
#P button 628 137 15 0;
#P button 578 139 15 0;
#P button 427 137 15 0;
#P button 367 141 15 0;
#P button 330 139 15 0;
#P button 280 141 15 0;
#P button 148 133 15 0;
#P button 88 137 15 0;
#P button 51 135 15 0;
#P button 1 137 15 0;
#P message 658 164 37 196617 34.65;
#P message 720 163 31 196617 41.2;
#P message 623 164 31 196617 32.7;
#P message 574 164 31 196617 27.5;
#P message 363 166 37 196617 34.65;
#P message 425 165 31 196617 41.2;
#P message 328 166 31 196617 32.7;
#P message 279 166 31 196617 27.5;
#P message 88 163 37 196617 34.65;
#P message 150 162 31 196617 41.2;
#P message 53 163 31 196617 32.7;
#P message 4 163 31 196617 27.5;
#P comment 1 188 27 196617 A[1];
#P comment 54 187 30 196617 C[2];
#P comment 24 30 33 196617 a-c-e;
#P comment 152 27 32 196617 a-e-c;
#P comment 269 21 32 196617 c-a-e;
#P comment 397 24 34 196617 c-e-a;
#P comment 541 27 32 196617 e-a-c;
#P comment 681 28 35 196617 e-c-a;
#P window setfont "Sans Serif" 36.;
#P comment 17 297 100 196644 float;
#P connect 44 0 21 0;
#P connect 33 0 21 0;
#P connect 21 0 9 0;
#P connect 97 0 68 0;
#P connect 96 0 68 0;
#P connect 68 0 56 0;
#P connect 46 0 22 0;
#P connect 45 0 22 0;
#P connect 22 0 10 0;
#P connect 69 0 57 0;
#P connect 23 0 12 0;
#P connect 99 0 70 0;
#P connect 98 0 70 0;
#P connect 70 0 59 0;
#P connect 48 0 24 0;
#P connect 47 0 24 0;
#P connect 24 0 11 0;
#P connect 101 0 71 0;
#P connect 100 0 71 0;
#P connect 71 0 58 0;
#P connect 52 0 49 0;
#P connect 9 0 52 0;
#P connect 49 0 50 0;
#P connect 10 0 52 1;
#P connect 105 0 102 0;
#P connect 56 0 105 0;
#P connect 102 0 103 0;
#P connect 11 0 52 2;
#P connect 59 0 105 1;
#P connect 49 0 51 0;
#P connect 13 0 52 3;
#P connect 58 0 105 2;
#P connect 102 0 104 0;
#P connect 14 0 52 4;
#P connect 60 0 105 3;
#P connect 25 0 13 0;
#P connect 47 0 25 0;
#P connect 45 0 25 0;
#P connect 15 0 52 5;
#P connect 72 0 60 0;
#P connect 100 0 72 0;
#P connect 98 0 72 0;
#P connect 63 0 105 4;
#P connect 17 0 52 6;
#P connect 62 0 105 5;
#P connect 18 0 52 7;
#P connect 64 0 105 6;
#P connect 26 0 14 0;
#P connect 48 0 26 0;
#P connect 33 0 26 0;
#P connect 19 0 52 8;
#P connect 73 0 61 0;
#P connect 67 0 105 7;
#P connect 66 0 105 8;
#P connect 27 0 16 0;
#P connect 74 0 63 0;
#P connect 101 0 74 0;
#P connect 96 0 74 0;
#P connect 28 0 15 0;
#P connect 46 0 28 0;
#P connect 44 0 28 0;
#P connect 75 0 62 0;
#P connect 99 0 75 0;
#P connect 97 0 75 0;
#P connect 29 0 17 0;
#P connect 48 0 29 0;
#P connect 46 0 29 0;
#P connect 76 0 64 0;
#P connect 101 0 76 0;
#P connect 99 0 76 0;
#P connect 30 0 18 0;
#P connect 47 0 30 0;
#P connect 44 0 30 0;
#P connect 77 0 65 0;
#P connect 31 0 20 0;
#P connect 78 0 67 0;
#P connect 100 0 78 0;
#P connect 97 0 78 0;
#P connect 32 0 19 0;
#P connect 45 0 32 0;
#P connect 33 0 32 0;
#P connect 79 0 66 0;
#P connect 98 0 79 0;
#P connect 96 0 79 0;
#P connect 149 0 126 0;
#P connect 138 0 126 0;
#P connect 126 0 114 0;
#P connect 151 0 127 0;
#P connect 150 0 127 0;
#P connect 127 0 115 0;
#P connect 128 0 117 0;
#P connect 153 0 129 0;
#P connect 152 0 129 0;
#P connect 129 0 116 0;
#P connect 157 0 154 0;
#P connect 114 0 157 0;
#P connect 154 0 155 0;
#P connect 115 0 157 1;
#P connect 116 0 157 2;
#P connect 154 0 156 0;
#P connect 118 0 157 3;
#P connect 119 0 157 4;
#P connect 130 0 118 0;
#P connect 152 0 130 0;
#P connect 150 0 130 0;
#P connect 120 0 157 5;
#P connect 122 0 157 6;
#P connect 123 0 157 7;
#P connect 131 0 119 0;
#P connect 153 0 131 0;
#P connect 138 0 131 0;
#P connect 124 0 157 8;
#P connect 132 0 121 0;
#P connect 133 0 120 0;
#P connect 151 0 133 0;
#P connect 149 0 133 0;
#P connect 134 0 122 0;
#P connect 153 0 134 0;
#P connect 151 0 134 0;
#P connect 135 0 123 0;
#P connect 152 0 135 0;
#P connect 149 0 135 0;
#P connect 136 0 125 0;
#P connect 137 0 124 0;
#P connect 150 0 137 0;
#P connect 138 0 137 0;
#P pop;


May 30, 2008 | 5:14 pm

On May 30, 2008, at 9:38 AM, v1025kernel wrote:

> *Also, what exactly is the ‘right-to-left’ issue?

Max patchers are evaluated starting at the right side of your patcher,
and working across to the left. If two things are in exactly the same
horizontal position, the lower object gets evaluated first.

That said, I think your problem is your use of pack. Like most Max
objects, pack only outputs anything when something comes in its left-
most inlet. You might be better served by pak, which fires when any
inlet changes. Note that the arguments to pak are different from the
arguments to pack.

Chris Muir
cbm@well.com

http://www.xfade.com


May 30, 2008 | 8:15 pm

v1025kernel schrieb:
> thanks for the help. it worked well with the example. How ever, in
> the patch that I’ve written, using more than on pack –> iff
> statement isn’t working.
>
> Can someone tell me why the A MAJOR section isn’t working? I did the
> same thing I had done with the A MINOR section … so it should work.

Chord recognition can be done easier:

list -> zl sort -> match, or

There is no need to test for all possible iterations if you sort then
this if should do it…

if ($i1==3 && $i2==6 && $i3==7) then bang
another solution would be

list -> zl sort -> zl sub 3 6 7 -> bang

or….

– Pasted Max Patch, click to expand. –

Stefan


Stefan Tiedje————x——-
–_____———–|————–
–(_|_ —-|—–|—–()——-
– _|_)—-|—–()————–
———-()——–www.ccmix.com


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