Jump to content

Script for legato notes


Chris of Catford

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

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

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

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