Jump to content

Script to trigger a note when releasing it


dertimo

Recommended Posts

Hello,

I am looking for a script to trigger a sound when releasing the note. In Logics EXS24 you can edit a sampler instrument that it behaves that way. Since I don't want to edit every sampler instrument I am using I's like to solve that with a midi script.

 

My aim is to play an easy drum rhythm on a organ pedal. When I press any note on it there is a kick drum. When I release the pedal I'd like to have a Hihat or something similar.

 

Can you help?

 

Best regards,

dertimo

Link to comment
Share on other sites

Thanks a lot. Is there a way to add other note numbers? I'd like to play a Bass along with the kick and have this effect on every note. And would it be possible to just have ne note off sound (hh) without the note on sound (bd)?

I'm not sure what you mean with the bass?

 

To have the note off without the note on just use this:

 

function HandleMIDI(event) {

   if ((event instanceof NoteOn) && event.pitch == 36) {
           event.send(); 
   }
   else if ((event instanceof NoteOff) && event.pitch == 36) {
       var note = new NoteOn;             
       note.pitch = (42); 
       note.velocity = 100; 
  }
   else {
   		event.send();
   	}
}

Link to comment
Share on other sites

Hm... Seams you deleted the wrong commands. Now it's only playing the note on kick drum. But I solved the problem by trial and error. Now its only playing the note offs. But anyway thank a lot. Here is the script. Sorry I didn't manage to insert it as code as you did.

 

function HandleMIDI(event) {

 

if ((event instanceof NoteOn) && event.pitch == 36) {}

else if ((event instanceof NoteOff) && event.pitch == 36) {

var note = new NoteOn;

note.pitch = (42);

note.velocity = 100;

note.send();

}

else {

event.send();

}

}

 

PS: I wrote another topic. Could you help me with that one too?

Link to comment
Share on other sites

A couple things...

 

First...in order to post your code on this forum in a nicer way, just use the CODE block.. you can see an icon on the toolbar above that looks like this:

 

code.jpg.d5a77e59667b94e53963e8f82998f04d.jpg

 

if you just click that toolbar button, it will insert the following in the editor:

 

[code]
[/code]

 

You can then put some javascript code in there. If you select the javascript code before hitting the toolbar button, it will sandwich it for you, like this:

 

[code]
function HandleMIDI(event) {

   if ((event instanceof NoteOn) && event.pitch == 36) {
       event.send();
   } 
   else if ((event instanceof NoteOff) && event.pitch == 36) {
       var note = new NoteOn;
       note.pitch = (42);
       note.velocity = 100;
       note.send();
   } 
   else {
       event.send();
   }
}
[/code]

 

After you do that, your post will have nicely formatted javascript code. Notice I was able to make my font smaller as well by using the Small font option in the toolbar as well, set to a size of 70. that looks like this in the editor:

 

[size=70][code]
function HandleMIDI(event) {
 // do stuff
}
[/code]

[/size]

 

now on to your code....

Edited by Dewdman42
Link to comment
Share on other sites

Looks to me that you took out the event.send() that David originally had for handling NoteOn. Also don't forget to assign channel to the pitch 42. So here it is, put back, along with the channel:

 

function HandleMIDI(event) {

   if ((event instanceof NoteOn) && event.pitch == 36) {
       event.send();
   } 
   else if ((event instanceof NoteOff) && event.pitch == 36) {
       var note = new NoteOn;
       note.pitch = (42);
       note.velocity = 100;
       note.channel = event.channel;
       note.send();
   } 
   else {
       event.send();
   }
}

 

I want to point out also that you do not have NoteOff events making it to your instrument for each of the two sounds you are intending to use. You will need to make sure to send NoteOff's also otherwise your instrument could become confused with hanging notes..

 

If these are percussive sounds that only need a few ms of duration for each note, then you could do that like this:

 

function HandleMIDI(event) {

   if ((event instanceof NoteOn) && event.pitch == 36) {
       event.send();
   } 
   else if ((event instanceof NoteOff) && event.pitch == 36) {
       event.send();  // first send the Noteoff itself for the original pitch
       var note = new NoteOn;
       note.pitch = (42);
       note.velocity = 100;
       note.channel = event.channel;
       note.send();
       note.velocity = 0;
       note.sendAfterMilliseconds(50);
   } 
   else {
       event.send();
   }
}
Edited by Dewdman42
Link to comment
Share on other sites

To add more pitches to your script, just copy and paste the else if sections for each pitch. For example:

 

function HandleMIDI(event) {

   // Pitch 36 
   if ((event instanceof NoteOn) && event.pitch == 36) {
       event.send();
   } 
   else if ((event instanceof NoteOff) && event.pitch == 36) {
       event.send();
       var note = new NoteOn;
       note.pitch = (42);
       note.velocity = 100;
       note.channel = event.channel;
       note.send();
       note.velocity = 0;
       note.sendAfterMilliseconds(50);
   } 
   
   // Pitch 37
   else if ((event instanceof NoteOn) && event.pitch == 37) {
       event.send();
   } 
   else if ((event instanceof NoteOff) && event.pitch == 37) {
       event.send();
       var note = new NoteOn;
       note.pitch = (43);
       note.velocity = 100;
       note.channel = event.channel;
       note.send();
       note.velocity = 0;
       note.sendAfterMilliseconds(50);
   }     
   
   // everything else
   else {
       event.send();
   }
}
Edited by Dewdman42
Link to comment
Share on other sites

Lastly, you can make it a little easier to read and avoid duplicated code by using a javascript function, like this:

 

function HandleMIDI(event) {

   if (event instanceof NoteOff) {

       if(event.pitch == 36) {
           sendOff( event, 42 );
       }
       else if(event.pitch == 37) {
           sendOff( event, 43);
       }        
       
       // all other NoteOff's
       else {
           event.send();
       }       
   }
   
   // everything else, including NoteOn
   else {
       event.send();
   }
}

function sendOff(event, offPitch) {
   
   event.send();  // send original NoteOff 
   
   var note = new NoteOn;
   note.channel = event.channel;
   note.velocity = 100;
   note.pitch = offPitch;
   note.send();
   note.velocity = 0;
   note.sendAfterMilliseconds(100);
}
Link to comment
Share on other sites

  • 3 weeks later...

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...