A technical support community for Apple Logic Pro users.

 
dertimo
Topic Author
Posts: 10
Joined: Thu Nov 19, 2020 5:40 am

Script to trigger a note when releasing it

Thu Nov 19, 2020 7:20 am

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
 
User avatar
David Nahmani
Site Admin
Posts: 80693
Joined: Sat Mar 05, 2005 12:16 am
Contact:

Re: Script to trigger a note when releasing it

Thu Nov 19, 2020 7:22 am

That one looks like it shouldn't be too difficult, I'm going to give it a try.
David Nahmani
• My Logic Pro X 10.5 book (Apple Pro Training Series)
Contact me for Private Lessons
Logic Pro X 10.6
MacBook Air 1.3 GHz i5 — MacOS X 10.15.1 — 4 GB RAM
iMac 3.2 GHz Quad Core i5 — MacOS X 10.15.4 — 8 GB RAM
 
dertimo
Topic Author
Posts: 10
Joined: Thu Nov 19, 2020 5:40 am

Re: Script to trigger a note when releasing it

Thu Nov 19, 2020 7:24 am

That's great. Thanks.
 
User avatar
David Nahmani
Site Admin
Posts: 80693
Joined: Sat Mar 05, 2005 12:16 am
Contact:

Re: Script to trigger a note when releasing it

Thu Nov 19, 2020 7:35 am

Ok try this project. Push a C1 key to hear a kick then release to hear a Hi-Hat.

Note Off to Note On.zip
(183.91 KiB) Downloaded 3 times
David Nahmani
• My Logic Pro X 10.5 book (Apple Pro Training Series)
Contact me for Private Lessons
Logic Pro X 10.6
MacBook Air 1.3 GHz i5 — MacOS X 10.15.1 — 4 GB RAM
iMac 3.2 GHz Quad Core i5 — MacOS X 10.15.4 — 8 GB RAM
 
dertimo
Topic Author
Posts: 10
Joined: Thu Nov 19, 2020 5:40 am

Re: Script to trigger a note when releasing it

Thu Nov 19, 2020 8:12 am

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)?
 
User avatar
David Nahmani
Site Admin
Posts: 80693
Joined: Sat Mar 05, 2005 12:16 am
Contact:

Re: Script to trigger a note when releasing it

Thu Nov 19, 2020 8:19 am

dertimo wrote:
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();
       }
}
David Nahmani
• My Logic Pro X 10.5 book (Apple Pro Training Series)
Contact me for Private Lessons
Logic Pro X 10.6
MacBook Air 1.3 GHz i5 — MacOS X 10.15.1 — 4 GB RAM
iMac 3.2 GHz Quad Core i5 — MacOS X 10.15.4 — 8 GB RAM
 
dertimo
Topic Author
Posts: 10
Joined: Thu Nov 19, 2020 5:40 am

Re: Script to trigger a note when releasing it

Fri Nov 20, 2020 12:23 pm

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?
 
User avatar
Dewdman42
Posts: 2913
Joined: Tue Sep 09, 2014 3:01 pm
Location: Salt Lake City, UT

Re: Script to trigger a note when releasing it

Fri Nov 20, 2020 3:01 pm

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
code.jpg (3.71 KiB) Viewed 291 times


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....
Last edited by Dewdman42 on Fri Nov 20, 2020 3:07 pm, edited 1 time in total.
5,1 MacPro 3.46ghz x 12 128gb ram, OSX 10.14, Logic Pro 10.5, Mainstage3, Cubase10.5, StudioOne4, Reaper, DP10, VEP7, VSL, too many plugins to list
 
User avatar
Dewdman42
Posts: 2913
Joined: Tue Sep 09, 2014 3:01 pm
Location: Salt Lake City, UT

Re: Script to trigger a note when releasing it

Fri Nov 20, 2020 3:05 pm

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();
    }
}
Last edited by Dewdman42 on Fri Nov 20, 2020 3:33 pm, edited 4 times in total.
5,1 MacPro 3.46ghz x 12 128gb ram, OSX 10.14, Logic Pro 10.5, Mainstage3, Cubase10.5, StudioOne4, Reaper, DP10, VEP7, VSL, too many plugins to list
 
User avatar
Dewdman42
Posts: 2913
Joined: Tue Sep 09, 2014 3:01 pm
Location: Salt Lake City, UT

Re: Script to trigger a note when releasing it

Fri Nov 20, 2020 3:15 pm

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();
    }
}
Last edited by Dewdman42 on Fri Nov 20, 2020 3:51 pm, edited 1 time in total.
5,1 MacPro 3.46ghz x 12 128gb ram, OSX 10.14, Logic Pro 10.5, Mainstage3, Cubase10.5, StudioOne4, Reaper, DP10, VEP7, VSL, too many plugins to list
 
User avatar
Dewdman42
Posts: 2913
Joined: Tue Sep 09, 2014 3:01 pm
Location: Salt Lake City, UT

Re: Script to trigger a note when releasing it

Fri Nov 20, 2020 3:45 pm

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);
}
5,1 MacPro 3.46ghz x 12 128gb ram, OSX 10.14, Logic Pro 10.5, Mainstage3, Cubase10.5, StudioOne4, Reaper, DP10, VEP7, VSL, too many plugins to list
 
dertimo
Topic Author
Posts: 10
Joined: Thu Nov 19, 2020 5:40 am

Re: Script to trigger a note when releasing it

Sat Nov 21, 2020 1:42 am

Thanks for your advice and the detailed comments. Now the script works perfectly well. I posted another topic.

viewtopic.php?f=45&t=153444&p=803675

Maby you can help me with that one too?
 
User avatar
Dewdman42
Posts: 2913
Joined: Tue Sep 09, 2014 3:01 pm
Location: Salt Lake City, UT

Re: Script to trigger a note when releasing it

Sat Nov 21, 2020 1:44 am

that other one you asked about is possible but quite a bit more involved and I'm sorry but I won't have time to do it for you.
5,1 MacPro 3.46ghz x 12 128gb ram, OSX 10.14, Logic Pro 10.5, Mainstage3, Cubase10.5, StudioOne4, Reaper, DP10, VEP7, VSL, too many plugins to list
 
dertimo
Topic Author
Posts: 10
Joined: Thu Nov 19, 2020 5:40 am

Re: Script to trigger a note when releasing it

Sun Nov 22, 2020 11:52 pm

Good to know, that it would be possible and totally comprehensible if you say you don't have the time to write it. Do you know someone I could hire to write it for me? I looked for programmers on fiverr.com or freelancer.com but for now without success.
 
User avatar
David Nahmani
Site Admin
Posts: 80693
Joined: Sat Mar 05, 2005 12:16 am
Contact:

Re: Script to trigger a note when releasing it

Sun Nov 22, 2020 11:54 pm

Let's see if Steve is willing to do it for you as a work for hire. If not, I know someone who may be able to help you out.
David Nahmani
• My Logic Pro X 10.5 book (Apple Pro Training Series)
Contact me for Private Lessons
Logic Pro X 10.6
MacBook Air 1.3 GHz i5 — MacOS X 10.15.1 — 4 GB RAM
iMac 3.2 GHz Quad Core i5 — MacOS X 10.15.4 — 8 GB RAM
 
User avatar
Dewdman42
Posts: 2913
Joined: Tue Sep 09, 2014 3:01 pm
Location: Salt Lake City, UT

Re: Script to trigger a note when releasing it

Sun Nov 22, 2020 11:56 pm

sorry dudes, I'm not available for hire on scripting. Have been asked many times. If I get some time I will take a look to see if I can do it in less then an hour I will, but I can't promise anything and I doubt its less then an hour.
5,1 MacPro 3.46ghz x 12 128gb ram, OSX 10.14, Logic Pro 10.5, Mainstage3, Cubase10.5, StudioOne4, Reaper, DP10, VEP7, VSL, too many plugins to list
 
dertimo
Topic Author
Posts: 10
Joined: Thu Nov 19, 2020 5:40 am

Re: Script to trigger a note when releasing it

Mon Nov 23, 2020 7:15 am

Thanks. I take, what I get.