Page 1 of 1

Why is this js script not behaving?

Posted: Sat Feb 13, 2010 3:49 am
by Surreal
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();
	}

}

Re: Why is this js script not behaving?

Posted: Sat Feb 13, 2010 11:17 am
by Highmountain
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.

Re: Why is this js script not behaving?

Posted: Sat Feb 13, 2010 11:19 am
by Surreal
thank you...makes sense

silly me.

Re: Why is this js script not behaving?

Posted: Sat Feb 13, 2010 11:34 am
by cturner
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

Re: Why is this js script not behaving?

Posted: Sat Feb 13, 2010 7:38 pm
by cturner
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.