Forums > MaxMSP

JavaScript to Max/Jitter string parsing issue

August 11, 2009 | 7:29 pm

I’m porting a Flash application over to Max/Jitter. I’ve rebuilt most of the logic in Max, but I have one routine that runs a lot of trig in a for loop that seems to run significantly faster using JavaScript than the uzi object. Basically, it plots points in a wave and sends OpenGL commands to an outlet.

With the LCD object, I could spit out hundreds of messages per frame without a hitch. But now I’m using OpenGL in Jitter and have found that it doesn’t respond well to a ton of messages, but it has no performance issues receiving single messages with hundreds of commands.

That said, I’m building the command string no problem, but when Max receives it, there are quotes around the whole thing. If I remove the quotes with the RegExp object (regexp (") @substitute %0), all the commas are replaced with backslash comma.

Any tips on getting this command over to jit.gl.sketch? Is this something I should be trying in JS or Max?

Here’s my JavaScript:


function bang() {

// Start the command string
var strCommand = "reset, glcolor 1 1 1 1, glbegin line_strip, "

// Decimate the frequency to make the wave prettier
numPitch *= numFactor;

// Loops through width
for (var i = 0; i < numWaveLength; i++) {

// Adjust ramp to add curve
numAmp -= numRamp;

// Adjust Y
numYPosition = numAmp * Math.sin(((i / numWaveLength) * (Math.PI * numPitch)) + numPhase);

// Draw line where amp is over 0
if (numAmp > 0) strCommand += "glvertex " + String(i / numWaveLength) + " " + String((numYPosition + numYOffset) / (numYOffset * 2)) + ", ";

}

outlet(0, strCommand + "glend");

}


August 11, 2009 | 7:39 pm

In javascript using + and " " to format strings will always result in a symbol, you should use ,commas, to create a list. For example this hsould do what you want:

a = "like"
outlet(0,"do","something",a,"this");

lh


August 11, 2009 | 7:45 pm

Right. The problem is that I’m dynamically building these arguments and there are hundreds. I can send these messages to the outlet one at a time, but it kills performance. If I can send them to the outlet in one shot, it flies along nicely. I tried just hard coding in about 400 glvertex commands to test this.

So i guess this is a JavaScript thing… any idea how to build an argument list dynamically? I tried populating an array and passing it as the argument, but no luck there. Hmmmm…


August 11, 2009 | 9:27 pm

Ah so the problem is using multiple outlet() calls against max messages seperated by a comma. I don’t think this should perform any differently but if you can post a stripped down example patch that shows the difference in behaviour then I’ll take a closer look.

lh


August 11, 2009 | 9:37 pm

The attached ZIP is pulled straight from the larger patch. "sine.maxpat" is the one to check out. Just play with the number objects up top to simulate values. Maybe just open it up inside a new patch and use "sine"‘s outlet to debug.

"sine.js" has both "versions" in there. One where it build a long string and sends to the outlet just once (active), and one where it hits the outlet with every command (commented).

Thanks for your help!


August 11, 2009 | 9:40 pm

BTW, the goal is to take data from pitch~ (amplitude and "pitch" (frequency)) along with a few other control variables, to draw the waveform of live audio in OpenGL. Right now it’s just the straight waveform, but once this is working, it will be more stylized.

Here’s an early video of the Flash version (which used fiddle~ — pitch~ is way better)

http://www.youtube.com/watch?v=ZgjgN2LPrAM


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