Jump to content

First note priority


antiabed

Recommended Posts

Hi everybody!

 

I've seen a bunch of scripts creating a last note priority, but I'm trying to make one that does the opposite, Play the first note you played, and not let any other note through until that first note is released.

 

I modified one of the last note priority scripts I found, and it kinda works. But fast notes are sometimes ignored and sometimes it let's a new note through while another is still playing. Can someone help me make this more solid?

 

Here is what I have now:

var hold = [];

function HandleMIDI(e) {
 var a = e instanceof NoteOn && e.velocity !=0;
 var b = e instanceof NoteOff;
 var c = e instanceof NoteOn && e.velocity ==0;

 if (a) {

   switch (hold.length) {

     case 0:
       hold.push(e.pitch);
       e.send(), e.trace();
       break;

     case 1:
       if (hold[0] != e.pitch) {
         hold.pop();
         hold.push(e.pitch);
         break;
       }
    }
 }
 
 if (b || c) {
   
   if (e.pitch != hold[0]) {
   		e.send(), e.trace();
   		hold.pop();
   }
 }
 

 if (!a && !b && !c) {
   e.send(), e.trace();
 }

}

Link to comment
Share on other sites

Try this instead:

 

var gate = -1;

function HandleMIDI(event) {

   // if first note, close gate until Noteoff
   if(event instanceof NoteOn) {
       if (gate < 0) {
           gate = event.pitch;
           event.send();
           return;
       }
       else {
           return;  // ignore until gate open again
       }
   }
   
   // if its the right note off, open the gate again
   if(event instanceof NoteOff) {
       if (event.pitch == gate) {
           event.send();
           gate = -1;
           return;
       }
       else {
           return;  // ignore until gate open again
       }
   }
   
   // all other event types, pass through
   event.send();
}
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...