Why is this js script not behaving?

Learn about building and using Max for Live devices.
Post Reply
Surreal
Posts: 560
Joined: Wed May 17, 2006 1:18 am

Why is this js script not behaving?

Post by Surreal » Sat Feb 13, 2010 3:49 am

it says there are the correct number of notes...then it only returns about half in the post. it presents 22 numbers though.



Code: Select all

autowatch = 1;
function bang() {
    watchClip = new LiveAPI(this.patcher, null,  "live_set tracks 0 clip_slots 0 clip");
    watchClip.call("select_all_notes");
	
	clip_notes = watchClip.call("get_selected_notes");
	clip_notes.shift();
	var num_of_notes = clip_notes.shift();
	
	noteArray = new Array();
	
	post("there are:", num_of_notes, " notes\n"); 
	var a;
	var b;
	for (a = 0; a < num_of_notes; a++) {
		post(a);
		noteArray[a] = clip_notes.splice((a*6), 6);
		post(noteArray[a]);
		post();
	}

}

Highmountain
Posts: 9
Joined: Tue Jan 05, 2010 8:15 am

Re: Why is this js script not behaving?

Post by Highmountain » Sat Feb 13, 2010 11:17 am

splice removes the elements so it should be splice(0, 6) instead of splice(a*6, 6). Or, it might be better to use slice(a*6, a*6+5) instead.
Last edited by Highmountain on Sat Feb 13, 2010 11:19 am, edited 1 time in total.

Surreal
Posts: 560
Joined: Wed May 17, 2006 1:18 am

Re: Why is this js script not behaving?

Post by Surreal » Sat Feb 13, 2010 11:19 am

thank you...makes sense

silly me.

cturner
Posts: 40
Joined: Sun Dec 06, 2009 4:09 pm
Location: GWB

Re: Why is this js script not behaving?

Post by cturner » Sat Feb 13, 2010 11:34 am

You rely on the number of notes returned by the call to determine the bounds of your "for" loop, when in reality, the call returned a lot more data.

You could use the result of "clip_notes.length" to set the upper bound, and knowing the size of the chunks you were interested in, ("note 64 14 2 101 0" has six elements), you can iterate through in chunk-sized steps. Also, you can initialize your iterator variable to skip over the first few elements, eliminating the shift() calls in your code. Something like this should work:

Code: Select all

for (a = 2; a < clip_notes.length-1; a += 6) {
    (do stuff...)
}
HTH, Charles

cturner
Posts: 40
Joined: Sun Dec 06, 2009 4:09 pm
Location: GWB

Re: Why is this js script not behaving?

Post by cturner » Sat Feb 13, 2010 7:38 pm

Highmountain wrote:splice removes the elements so it should be splice(0, 6) instead of splice(a*6, 6).
Actually, because you're assigning the values to a new Array, splice()'s "removal" is unnecessary, and only slows down your script. Use slice().

C.

Post Reply