Jump to content

Script for legato notes


Recommended Posts

Hi,

I have been trying to make a script that makes notes stay on until another note is played, but that turns off on a repeated note.

I keep ending up with 100 instead of my variable, and I can't work out why.

Here is my code:

//Legato until repeated note on
//Set up note var
var prevNote=0;

function HandleMIDI (event) {	


if (event instanceof NoteOn) {
	
	Trace("Previous note is: "+prevNote);

	//Logic check
	if(event.pitch == prevNote){
		
		//send a note off and stop legato
		var off = new NoteOff(prevNote);  /* make a note off using the note on to initialize its pitch value (to C3) */
 		off.send;  /* send a note off one beat later */
 		off.trace();
		Trace("Sent note off for: "+prevNote+" ..Should stop midi");
		MIDI.allNotesOff();	
		
		//reset the prev variable
		prevNote=0;
	
	}else{
		
		//continue with legato
		Trace("Not the same note so continue.. turn off: "+prevNote);
	
		//Turn off previous note	
		var off = new NoteOff(prevNote.value);  /* make a note off using the note on to initialize its pitch value (to C3) */
 		off.send;  /* send a note off one beat later */
 		off.trace();
 		//MIDI.allNotesOff();
		
		var on = new NoteOn;   /* make a new note on */
 		on.pitch = event.pitch;   /* set its pitch to C3 */
 		on.send();    /* send the note */
 		on.trace();
		//event.send(); //Pass MIDI events through the plug-in.

		//Save previous note
		prevNote=event.pitch;
		Trace("Current note is: "+prevNote); 
		
			
	}

		
	}
}

As you can see, I tried all notes off in desperation, but this isn't desirable, and cancels the noteOn before it occurs too.

Any ideas what my issue is?

This is what my code outputs, making pitch for note off to always be 100. But why??

Previous note is: 0
Not the same note so continue.. turn off: 0
[NoteOff channel:1 pitch:100 [E6] velocity:0]
[NoteOn channel:1 pitch:72 [C4] velocity:100]
Current note is: 72
Previous note is: 72
Not the same note so continue.. turn off: 72
[NoteOff channel:1 pitch:100 [E6] velocity:0]
[NoteOn channel:1 pitch:74 [D4] velocity:100]
Current note is: 74
Previous note is: 74
Not the same note so continue.. turn off: 74
[NoteOff channel:1 pitch:100 [E6] velocity:0]
[NoteOn channel:1 pitch:72 [C4] velocity:100]
Current note is: 72
Previous note is: 72
[NoteOff channel:1 pitch:100 [E6] velocity:0]
Sent note off for: 72 ..Should stop midi

Thanks in advance,

Chris

Link to comment
Share on other sites

Posted (edited)
4 hours ago, Chris of Catford said:
var off = new NoteOff(prevNote.value);

Wouldn't that create a Note Off with the pitch of the prevNote's Velocity (i.e. shouldn't you use pitch instead of value)?  EDIT: but then, maybe not, I just had a quick look and I'm not experienced at all with Scripter

Edited by polanoid

MacBook Pro 16'' 2019, 2,4 GHz 8-Core Intel Core i9 64 GB 2667 MHz DDR4 / macOS Monterey 12.3.1 / Logic 10.7.4

Link to comment
Share on other sites

HI,

Unfortunately whatever I put there I get 100 instead of the actual value, which is returned correctly, and not the actual number set in the variable.

I know I am probably just being dense, but something about that variable doesn't work in this context.

Thanks,

Chris

Link to comment
Share on other sites

Also this is where the variable is being set:

prevNote=event.pitch;

And this is where it is returned correctly:

prevNote=event.pitch;

But when it gets here, it sends noteOff to 100:

var off = new NoteOff(prevNote); 

No matter what the actual variable is.

Link to comment
Share on other sites

It's still strange that in one line it says 

var off = new NoteOff(prevNote);

while in another it says

 

var off = new NoteOff(prevNote.value);

Why are these lines different?

 

 

MacBook Pro 16'' 2019, 2,4 GHz 8-Core Intel Core i9 64 GB 2667 MHz DDR4 / macOS Monterey 12.3.1 / Logic 10.7.4

Link to comment
Share on other sites

That doesn't really answer my question why the two lines are different. Anyway what strikes me is that the pitch of the Note Off is the velocity of the previous note on, so somehow the velocity must be assigned to the pitch. Hence my idea to not use prevNote.value (which is a wild guess of course)

MacBook Pro 16'' 2019, 2,4 GHz 8-Core Intel Core i9 64 GB 2667 MHz DDR4 / macOS Monterey 12.3.1 / Logic 10.7.4

Link to comment
Share on other sites

Posted (edited)

Also, what happens when you set the note off's pitch explicitly, like

 

off.pitch = prevNote

?

 

Edited by polanoid

MacBook Pro 16'' 2019, 2,4 GHz 8-Core Intel Core i9 64 GB 2667 MHz DDR4 / macOS Monterey 12.3.1 / Logic 10.7.4

Link to comment
Share on other sites

Another thing (bear in mind I really don't have any specific knowledge in JS) I noticed: is "send;" supposed to do the same as "send();"? You send the note on with

 

on.send();

i.e., with brackets, but the note off with

 

off.send;

so, no brackets here.

 

 

MacBook Pro 16'' 2019, 2,4 GHz 8-Core Intel Core i9 64 GB 2667 MHz DDR4 / macOS Monterey 12.3.1 / Logic 10.7.4

Link to comment
Share on other sites

Hi,

Got it working (I think):

Quote

 

 

//Legato until repeated note on
//Set up note var
var prevNote=0;

function HandleMIDI (event) {    


if (event instanceof NoteOn) {
    
    Trace("Previous note is: "+prevNote);

    //Logic check
    if(event.pitch == prevNote){
        
        //send a note off and stop legato
        var off = new NoteOff(prevNote);  /* make a note off using the note on to initialize its pitch value (to C3) */
         off.pitch = prevNote;
         off.send(); 
         off.trace();
        Trace("Sent note off for: "+prevNote+" ..Should stop midi");
        //MIDI.allNotesOff();    //Only way I can turn off the note so far
        
        //reset the prev variable
        prevNote=0;
    
    }else{
        
        //continue with legato
        Trace("Not the same note so continue.. turn off: "+prevNote);
    
        //Turn off previous note    
        var off = new NoteOff(prevNote);  /* make a note off using the note on to initialize its pitch value (to C3) */
         off.pitch = prevNote;
         off.velocity=0;
         off.send();  
         off.trace();
         //MIDI.allNotesOff();
        
        
        var on = new NoteOn;  
         on.pitch = event.pitch;   
         on.send();    
         on.trace();
        //event.send(); //Pass MIDI events through the plug-in.

        //Save previous note
        prevNote=event.pitch;
        Trace("Current note is: "+prevNote); 
        
            
    }

        
    }
}

 

 

I think it was because I was sending the event as well as recreating it. Commented that out and that fixed it. Your suggestion of telling it the note was the ticket though.

Thanks again,

Chris

Link to comment
Share on other sites

3 hours ago, Dewdman42 said:

and FYI, MainStage and LogicPro handle events in Scripter exactly the same way.  EXACTLY the same.  honest!

which makes me wonder why the script yields different results in Logic and MainStage

MacBook Pro 16'' 2019, 2,4 GHz 8-Core Intel Core i9 64 GB 2667 MHz DDR4 / macOS Monterey 12.3.1 / Logic 10.7.4

Link to comment
Share on other sites

I didn't follow the exchange closely enough to figure it out, sorry, but I can just say, it works exactly the same in both hosts.  There is zero difference.

OSX 12.x (Monterey) on OpenCore - Logic Pro 10.7.4, VePro7, Mainstage3 - 5,1 MacPro 3.46ghz x 12 96gb ram

Link to comment
Share on other sites

its remotely possible the AllNotesOff doesn't work in MainStage....but its 3am here and I am not going to test it to see.  If so that would be a bug that should be reported to Apple.

OSX 12.x (Monterey) on OpenCore - Logic Pro 10.7.4, VePro7, Mainstage3 - 5,1 MacPro 3.46ghz x 12 96gb ram

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

 Share

×
×
  • Create New...