My js is slowing down

Questions and discussion about building and using Max for Live devices
Post Reply
ytsek
Posts: 193
Joined: Tue Jul 26, 2005 3:22 pm
Location: Rotterdam
Contact:

My js is slowing down

Post by ytsek » Fri Apr 13, 2012 12:53 pm

Hi, I have created a java script that changes clip color of all clips in my set based on the key of to the currently playing clip. It's based on the last characters in the clip name that contains the key code (A01 to B12). I bang the track that has the currently playing track to the inlet of the java script.
The script works really well and fast enough. Only after a while the script is stalling and it gets really slow. Especially when there a lot of clips and scenes in the session.
Anyone knows what I can do. Maybe some sort of reset.
This is the script:


//function bang()

inlets=1

function msg_int(TRACKPLAY)
{
var NoTRACKS = 3;
//var NoCLIPSLOTS = 6;

api = new LiveAPI(this.patcher, "live_set");
var SCENES=api.getcount("scenes");
//post (SCENES,"\n");
NoCLIPSLOTS=SCENES-1


//test="08A";
// post ("test is ");
// post (test,"\n");
// post ( parseInt(test,10), "\n");


//Resolve key of playing clip, last 3 characters of clipname, A01 to B12
var i=0;
for (i=0;i<=NoCLIPSLOTS;i++)
{
api = new LiveAPI(this.patcher, "live_set tracks " + TRACKPLAY + " clip_slots " + i);
if (api.get("has_clip") == 1)
{
api = new LiveAPI(this.patcher, "live_set tracks " + TRACKPLAY + " clip_slots " + i + " clip");
var CLIPISPLAYING=api.get("is_playing");
//post (CLIPISPLAYING,"\n");
if (CLIPISPLAYING==1)
{
var CLIPNAME=api.get("name");
KEYCLIPPLAY = (CLIPNAME.join().slice(-3));
api.set("color", "16712965");
//post (KEYCLIPPLAY,"\n");
i=NoCLIPSLOTS;
}
}
}



//Set color according to key
//tracks
//bugs:
//1A 1B
//1A 12A
for (k=4;k<=NoTRACKS+4;k++)
{
//clip_slots
var j=0;
for (j=0;j<=NoCLIPSLOTS;j++)
{
api = new LiveAPI(this.patcher, "live_set tracks " + k + " clip_slots " + j);
if (api.get("has_clip") == 1)
{
api = new LiveAPI(this.patcher, "live_set tracks " + k + " clip_slots " + j + " clip");
var CLIPNAME=api.get("name");
KEYCLIP = (CLIPNAME.join().slice(-3));
post ("keyclip is ", KEYCLIP,"\n");

if ( api.get("color") == 16712965)
{
}
else if(KEYCLIP==KEYCLIPPLAY)
{
//really green, same key
api.set("color", "1769263");
}
else if (KEYCLIP.slice(0,2)==KEYCLIPPLAY.slice(0,2)&&KEYCLIP.slice(-1)=="A")
{
//dark green, minor
api.set("color", "5480241");
}
else if (KEYCLIP.slice(0,2)==KEYCLIPPLAY.slice(0,2)&&KEYCLIP.slice(-1)=="B")
{
//light green, major
api.set("color", "5480241");
}
else if (parseInt(KEYCLIP,10)==parseInt(KEYCLIPPLAY,10)+1&&KEYCLIP.slice(-1)==KEYCLIPPLAY.slice(-1))
{
//light purple, +1
api.set("color", "12094975");
}

// else if (7==parseInt(KEYCLIPPLAY.slice(0,2))&&KEYCLIP.slice(0,2)==8&&KEYCLIP.slice(-1)==KEYCLIPPLAY.slice(-1))
// {
// //light purple, +1
// api.set("color", "12094975");
// }

else if (parseInt(KEYCLIP,10)==1&&parseInt(KEYCLIPPLAY,10)==12&&KEYCLIP.slice(-1)==KEYCLIPPLAY.slice(-1))
{
//light purple, +1
api.set("color", "12094975");
}

else if (parseInt(KEYCLIP,10)==parseInt(KEYCLIPPLAY,10)-1&&KEYCLIP.slice(-1)==KEYCLIPPLAY.slice(-1))
{
//dark purple, -1
api.set("color", "8940772");
}
else if (parseInt(KEYCLIP,10)==12&&parseInt(KEYCLIPPLAY,10)==1&&KEYCLIP.slice(-1)==KEYCLIPPLAY.slice(-1))
{
//dark purple, -1
api.set("color", "8940772");
}

else
{
//gray
api.set("color", "8092539");
}
}
}
}

// post (KEYCLIP,"\n");
// post ( parseInt(KEYCLIPPLAY.slice(0,2)), "\n");
// post ( parseInt(KEYCLIP.slice(0,2)), "\n");

//post ("keyclip is ", parseInt(KEYCLIP.slice(0,2))+1,"\n");


//post (CLIPCOLOR,"\n");
}



//print: 1769263 light green
//print: 8092539 gray
//print: 16712965 red
//js: 8940772 dark purple
//js: 10927616 light green
//js: 5480241 dark green
//js: 12094975 light purple

ytsek
Posts: 193
Joined: Tue Jul 26, 2005 3:22 pm
Location: Rotterdam
Contact:

Re: My js is slowing down

Post by ytsek » Mon Apr 16, 2012 8:38 pm

Anyone?

After like one hour in a dj set this m4l device gets slower and slower. After each track/clip I play I activate this m4l device and is sets the current playing clip to red and changes all other colors of the clips in the set according to key.
Could this be a memory issue? I can't imagine that because not much data is being used. Can I measure the amount of memory used by a m4l device? I could check the ovarall memory use by max, now I think of it.

S4racen
Posts: 5109
Joined: Fri Aug 24, 2007 4:08 pm
Location: Dunstable
Contact:

Re: My js is slowing down

Post by S4racen » Mon Apr 16, 2012 9:17 pm

Hmmm, pretty sure Vic Demented already did this as advice, think its on maxforlive.com? Might be a help?


Cheers
D

ytsek
Posts: 193
Joined: Tue Jul 26, 2005 3:22 pm
Location: Rotterdam
Contact:

Re: My js is slowing down

Post by ytsek » Tue Apr 17, 2012 11:42 am

S4racen wrote:Hmmm, pretty sure Vic Demented already did this as advice, think its on maxforlive.com? Might be a help?


Cheers
D
Could be a help. Can't find the device though.

When I get home I'll first try the js garbage collector. Found that on the Cycling '74 site.

amounra93
Posts: 431
Joined: Sat Jan 24, 2009 8:16 pm
Location: Arcata, CA
Contact:

Re: My js is slowing down

Post by amounra93 » Tue Apr 17, 2012 6:11 pm

I think the problem that you are having is that you are creating a new API object every time you do something. Instead of using the new LiveAPI() method, you might instead try re-using instances that have already been created and reassigning their paths. This is a little slower on the front-end, but much more resource conservative on the back-end.

For instance, the majority of my m4l javascripts use maybe 4 or 5 API objects total, and will recycle each one as its assignments need to change. I've heard detractions to using this method, but have never SEEN good reasons for them. On the contrary, what I see is sessions slowing down to a crawl from having too many API objects floating around in them (essentially doing nothing). +2cents.

a
http://www.aumhaa.com for Monomod and other m4l goodies.

ST8
Posts: 259
Joined: Mon Jan 26, 2009 12:55 pm

Re: My js is slowing down

Post by ST8 » Tue Apr 17, 2012 7:34 pm

amounra93 wrote:I think the problem that you are having is that you are creating a new API object every time you do something. Instead of using the new LiveAPI() method, you might instead try re-using instances that have already been created and reassigning their paths. This is a little slower on the front-end, but much more resource conservative on the back-end.

For instance, the majority of my m4l javascripts use maybe 4 or 5 API objects total, and will recycle each one as its assignments need to change. I've heard detractions to using this method, but have never SEEN good reasons for them. On the contrary, what I see is sessions slowing down to a crawl from having too many API objects floating around in them (essentially doing nothing). +2cents.

a
I tend to agree, for Kapture i used maybe 2-3 live api objects for the whole project, and reassigned them using ids.

ytsek
Posts: 193
Joined: Tue Jul 26, 2005 3:22 pm
Location: Rotterdam
Contact:

Re: My js is slowing down

Post by ytsek » Tue Apr 17, 2012 8:27 pm

Thanks amounra93 and ST8. I will try that, although I have no idea how yet. This is one of my first javascripts.
Gonna dig to your code a bit.

Post Reply