Jump to content

Execute a script on consecutive MIDI events


benbravo

Recommended Posts

Hi,

 

I have a midi button that sends two CC values on press.

CC78 = 63 followed by CC77 = some value

Another button sends

CC78 = 127 followed by CC77 = some value

 

I would like to process CC77 a certain way if it is following CC78 of value 63 and another if it’s 127.

 

Is there a way to achieve this ??

 

Thanks

Link to comment
Share on other sites

Try this to get started...

 

var lastEvent = new Event;

function HandleMIDI(event) {
   if(event instanceof ControlChange 
           && event.number == 77
           && lastEvent instanceof ControlChange
           && lastEvent.number == 78) {
           
       handleCC(event);
   }
   else {
       event.send();
   }
   lastEvent = event;
}


function handleCC(event) {   
   
   if(lastEvent.value == 63) {
       //========================
       // DO ACTION HERE
       //========================
   }
   else if(lastEvent.value == 127) {
       //========================
       // DO OTHER ACTION HERE
       //========================        
   }
}
Link to comment
Share on other sites

Thanks Dewdman, working perfectly!

 

In the meantime I had solved it this way, any reason to prefer your code?

 

var i;
function HandleMIDI(event){
if(event instanceof ControlChange 
	&& event.number == 78 
	&& event.value == 127){
		i = 1;
}
else if(event instanceof ControlChange 
	&& event.number == 78 
	&& event.value == 63){
		i = 2;
}	
else if(event instanceof ControlChange 
	&& event.number == 77 && i == 1){
		// ACTION HERE // 
		i = 0;
}
else if(event instanceof ControlChange 
	&& event.number == 77 && i == 2){
		// OTHER ACTION HERE //
		i = 0;
}	
else {
	event.send();	
	i = 0;
}
}

Link to comment
Share on other sites

either way should work, but the main difference I see is that in your case, the 77 and 78 CC's don't have to be right in a row...there could be other events in between them and it should still work...my method actually only works if they are exactly in a row. But its always possible a noteOn event or something could be sandwiched in between the 77 and 78, so I prefer your approach actually or a slight tweak to mine. I don't think it matters either way in terms of performance, if it works, then good job figuring it out!
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...