Chris of Catford Posted May 25, 2022 Share Posted May 25, 2022 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 Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 (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 May 25, 2022 by polanoid Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 25, 2022 Author Share Posted May 25, 2022 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 Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 25, 2022 Author Share Posted May 25, 2022 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. Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 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? Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 25, 2022 Author Share Posted May 25, 2022 Trying whatever I can remember that changes variable types. Notably, neither works. Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 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) Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 (edited) Also, what happens when you set the note off's pitch explicitly, like off.pitch = prevNote ? Edited May 25, 2022 by polanoid Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 25, 2022 Author Share Posted May 25, 2022 YES! Thanks so much! The noteOff needed a note set. It now broke the repeated noteOff routine, but I think that is a separate issue. Thanks, Chris 1 Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 25, 2022 Author Share Posted May 25, 2022 Having said that, it doesn't work in MainStage. The noteOffs seem to show in Trace, but don't turn the notes off. Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 Well that would be something for the MainStage forum (if there is any such thing), I guess. Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 25, 2022 Author Share Posted May 25, 2022 Yes, that is where I posted it, but admin moved it here 😂 1 Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 I'm pretty sure the event handling of Logic and MainStage is quite different Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 But still, can you post the updated version of your script, just to check? Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 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. Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 25, 2022 Author Share Posted May 25, 2022 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 Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 25, 2022 Share Posted May 25, 2022 You're welcome! Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted May 26, 2022 Share Posted May 26, 2022 and FYI, MainStage and LogicPro handle events in Scripter exactly the same way. EXACTLY the same. honest! Quote Link to comment Share on other sites More sharing options...
polanoid Posted May 26, 2022 Share Posted May 26, 2022 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 Quote Link to comment Share on other sites More sharing options...
Chris of Catford Posted May 26, 2022 Author Share Posted May 26, 2022 I'm guessing that it was because I was using a different virtual instrument. B4, which was the instrument I wanted legato for, is seemingly way more fussy than Sampler on default sound, which was what was loaded in my testbed in Logic. Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted May 26, 2022 Share Posted May 26, 2022 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. Quote Link to comment Share on other sites More sharing options...
Dewdman42 Posted May 26, 2022 Share Posted May 26, 2022 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.