Call a max object/function from JS possible?

cronoklee's icon

Hey all,
Is it possible to call a max function like ftom from Javascript?

Thanks

cronoklee's icon

I guess maybe no one's gonna reply to this. Here's a javascript function that does the same job as ftom

//Frequency to Midi conversion in JS:
function ftom(freq) {
    return(69+12*Math.log(freq/440)/Math.log(2))
}

I'm still interested if the max objects can be accessed from JS so if anyone knows. SHOUT!

Griotspeak's icon

The least roundabout way I can think of to access max values is to use

this.patcher.getnamed("foo").message(440);
var valueAfterMaxObjects = this.patcher.getnamed("bar").getvalueof();

where sending foo a message triggers an update to bar's value.

thouldcroft's icon

Lately I've returned to JS in max, and am doing some work in Jitter with JS, and I love how I can instantiate jitter objects and process matrices with them in the same way I would in the max patcher straight from JavaScript. In the same patch I would like to generate MIDI from the jitter matrices, so I've been desparately trying to find out how to call max objects (say makenote for instance) straight from JavaScript, using max as sort a library of MIDI functions and processors. Do I really need to actually create the objects in the patcher from JS, connect them all, and *then* be able to actually use them in my code?

It is slightly frustrating to become comfortable with coding in JavaScript for max in one way as I do in jitter, but can't extend these techniques to max objects!

Peter Castine's icon

It ain't that easy.

When Jitter was introduced it came with its own Software Developer Kit (SDK) for developing externals. One of its peculiarities (compared to the SDK for plain-vanilla Max objects, and the SDK for MSP objects), was that Jitter externals are supposed to be coded in two parts: a "core" Jitter object and a "Max wrapper" object. The logic behind the division was that it was supposed to allow Jitter externals to be used from "other languages". At the time there weren't any other languages that used Jitter objects, and there weren't any on the horizon, either (at least not from where those outside C74 could see). Since the introduction of Javascript for Max, of course, the division suddenly makes sense. Which doesn't change the fact that it's something of a PITA for 3rd party developers. And, apparently, occasionally so even for developers at Cycling… there are a small number of Jitter objects that aren't supported in the Javascript environment. Don't ask me why, but it is so.

As for Max and MSP objects, most of which were developed before the Jitter "division of labor" was introduced, it would require a pretty major effort to retrofit existing objects with the "core object"/"Max wrapper" split of functionality.

Which isn't to discourage you from trying to lobby for that functionality. But understand that it's not just a matter of flipping a switch and all existing Max/MSP objects exist in Javascript.

Instantiating objects inside JS code is cool, I do it myself. As for the 100-some-odd Max/MSP objects that I've already developed, the prospect of having to go back and code every one almost-but-not-quite from scratch in order to support instantiation from inside Javascript (or anything else) doesn't exactly fill this developer with delight. And until the Max SDK actually supports this sort of thing… well, I don't see that much can happen.

This is just the view from outside the ranch, I don't know if any of the devs inside Cycling wants to say more about this.

Emmanuel Jourdan's icon

@peter you can actually make NOBOX objects that will be available in JavaScript (there's an example in the SDK called simplejs which demonstrates that). Still you'll have to make a BOX object to instantiate your NOBOX object if you want to use it in Max and in JavaScript. I'm not saying it's easy, just that it's possible ;-) This is basically how polybuffer / dict works in the both Max and JS world.

Peter Castine's icon

OK, good to know.

Still, I'll retrofit all of Litter Power to support this when Cycling '74 does it for all the standard Max/MSP objects.

Or do you want to have a race to see who gets done first?-)

Emmanuel Jourdan's icon

One could argue that it would make more sense to have the Litter Power objects in JS than the standard Max objects: they do very specific stuff whereas Max's plus object is very similar to js's plus ;-)

Peter Castine's icon

[Plus] isn't exactly the most sophisticated factory Max object.

Still a good point, indeed a very good one. Seriously need to claw out a good quantity of quality time to get this going.