Forums > MaxMSP

Good patching techniques. Should I use Javascript for more?

February 7, 2012 | 2:13 am

Hello, recently I have started using the if object in the place of a select object and a message box like I have been doing since I started with Max. I was just wondering, is there a neater or easier way? I think perhaps I have fallen into a bad habit.

ANYWHO, the point of this was to ask if any of you lovely people can make this without having a ger-zillion(sp?) patch lines and taking longer than it has to:

– Pasted Max Patch, click to expand. –

I am mostly interested in the part which is triggered when the input does NOT equal any of those numbers.

I have tried using the If object with this —

[if ($i1 == 8) || ($i1 == 24) ($i1 == 140) || ($i1 == 156) then 0 else 1]

But that doesn’t work. Max hurling abuse at me in the Max window.

Any help is appreciated,

Iyad.


February 7, 2012 | 3:43 am

I can’t see your patch since I’m on an iPad but it seems to me you could use a [route 8 24 140 156] object and just detect output from the rightmost outlet


February 7, 2012 | 8:21 am

there is a typo in it:

[if ($i1 == 8) || ($i1 == 24) ($i1 == 140) || ($i1 == 156) then 0 else 1]

try

[if ($i1 == 8) || ($i1 == 24) || ($i1 == 140) || ($i1 == 156) then 0 else 1]

now it should work.

but if you only need one outlet, you could also use [expr]. this would allow you
to do calculations after the comparison operators, too, which [if] wont allow.
("then (0+$f3) else 5-4")


February 7, 2012 | 8:25 am

[expr ($i1 != 8) || ($i1 != 24) || ($i1 != 140) || ($i1 != 156)]
when i am not wrong.


February 7, 2012 | 10:18 am

I think the expr would be

[expr ($i1 != 8) && ($i1 != 24) && ($i1 != 140) && ($i1 != 156)]

or

[expr ! (($i1 == 8) || ($i1 == 24) || ($i1 == 140) || ($i1 == 156))]


February 7, 2012 | 5:37 pm

Thanks for your responses guys. I think I need to get into the expr object more, as it looks super useful.

Is there a way in any of these objects to output different numbers depending on the number that goes in? I can’t seem to figure this out using the if or the expr objects.

For example

8 -> 0
24 -> 1
56 -> 2
everything else -> 10

Thanks guys.


February 7, 2012 | 5:37 pm

Thanks for your responses guys. I think I need to get into the expr object more, as it looks super useful.

Is there a way in any of these objects to output different numbers depending on the number that goes in? I can’t seem to figure this out using the if or the expr objects.

For example

8 -> 0
24 -> 1
56 -> 2
everything else -> 10

Thanks guys.


February 8, 2012 | 12:34 am

If you want each new number you specify to return numbers that increase by 1 each time then the following example using [zl sub] will work.

– Pasted Max Patch, click to expand. –

February 8, 2012 | 3:43 pm

one thing expr cannot do, is outputting nothing. you will have to use 0 or a placeholder and remove it later with [route 0]


February 8, 2012 | 3:49 pm

24 -> 1
56 -> 2

is no problem:

expr ( ($i1==24) * 1 ) + ( ($i1==56) * 2 )

0 as output is problematic, as you need the 0 to let false results vanish from the addition.


February 10, 2012 | 2:21 pm

Thanks guys, I really need to learn more of this stuff, it is pretty awesome. :)


February 10, 2012 | 3:19 pm

zl sub is definitely the way to go.

However, here’s another way to tackle it using vexpr, which is a good skill to know for other problems like this.

– Pasted Max Patch, click to expand. –

Another tip: avoid using multiple objects or additional inlets to solve problems when you can solve it via a list. Spend some quality time with the zl objects and especially vexpr (like expr, but for lists: @scalarmode 1 is magic.) This will save you tons of time, make your code more dependable, and make your code more flexible. In my experience, code works best when you keep patchcords to a minimum.

Learn to look for general problems where you can and solve them well, then store them for use as abstractions. Yes, there is a time for specific solutions, but specific solutions are rarely reusable, and generally don’t provide ways of expanding on an idea.

– Pasted Max Patch, click to expand. –

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