Help with alternating notes

Did you look at it? It’s really pretty straight forward once you understand what’s going on. Here’s a commented version

The identifier channel is a variable that represents an integer value and it starts with the value 0

var
   channel : integer = 0         

This is the beginning of a “callback” event. For this particular case, the code here will be triggered whenever a Note On message arrives at the scriptlet

On NoteOnEvent(m : NoteMessage)

MIDI channels have to be between 1 and 16 but because we started with a 0 (for
reasons I will explain in a minute, we have to add 1 to the channel to make it be valid.
So the line here simply says, send out the Note On message that was received EXCEPT
add one to the channel number.

    SendNow( m.WithChannel(channel + 1))

The % character means to divide by some value and keep the remainder. People do this all the time with clocks. 13:00 is 1 (divide 13 by 12 and you get 1 remainder, divide 14 by 12 and you get 2 remainder)

So here, we now add one to the channel, divide by two and take the remainder. When you divide by two and take the remainder, the result can only be 0 or 1 so here, the channel alternates between 0 and 1. (Remember that when we send it out, we add one to the value so the actual channel sent out will alternate between 1 and 2)

    channel = (channel + 1) % 2   

This just says we have finished with the NoteOnEvent callback

End

Now we define another callback, this one will be called whenever a NoteOff is created, i.e. releasing a key on your keyboard.

On NoteOffEvent(m : NoteMessage)

Here we are declaring another variable, just like we did for the channel above but this time our variable represents a MIDI Note Message. We then assign it the incoming MIDI message but we change the velocity to 0. Actually, now that I write this, I realize we don’t need this line since the incoming message is already a Note Off (serves me right for writing the scriptlet too quickly — this declaration can be removed - it’s not even used)

var
   n : NoteMessage = m.WithVelocity(0)

This basically says, send out the incoming NoteOff message twice, but using channel 1 and then channel 2

   SendNow( m.WithChannel(1))
   SendNow( m.WithChannel(2))        
End

Does this make sense?

4 Likes