MIDI input to Bitwise Operation?

As promised: Optimized using the shift operator as proposed by @dhj. Some checking before use in production is advised, because I cannot verify all of it.

Var 
    HapaxInput : MidiInBlock
    ScaleValue : Integer
    EmVoice1 : PluginBlock

Initialization

    ScaleValue = 0 

End

On NoteEvent(m : NoteMessage) from HapaxInput

Var
    thisNote : Integer = 0
    
    SendNow(HapaxInput, m)

    thisNote = GetNoteNumber(m)
    
    if IsNoteOn(m)
    then    
        ScaleValue = ScaleValue or (1 << (thisNote % 12))
    else
        ScaleValue = ScaleValue and not (1 << (thisNote % 12))
    end
    
    if GetChannel(m) == 1
        then
            SetParameter(EmVoice1,18, ScaleValue *(1.0/4095.0))
    end
    
End

I won’t come into the details of what you discussed here, but I think it could be a little bit more efficient to use On NoteOnEvent and On NoteOffEvent callbacks here (you always need both when you use one). So, you wouldn’t need to do all the variable declarations, assignments and tests you do here in the On NoteOnEvent callback. :wink:

But a lot of the code would have to be replicated, which contradicts with ‘don’t repeat yourself’ :wink:

Ohh, I stopped to read the code after the first “If IsNoteOn(m)” and didn’t notice that something similar was done in the case of a Note Off message. So, yes in the case, the gain is moderate. :wink:

We have a solution called “user functions” for that problem :stuck_out_tongue_winking_eye:

1 Like

Yup, this all works, thanks for your help everyone!

Yep. And then we put if IsNoteOn(m) in that function :shushing_face:

Huh? No. I though the concern was there was going to be duplication!

Not a real concern. @David-san thinks it can be optimized by using a separate noteon and noteoff eventhandler, but there is some other code involved, so that could be in a function, but there is only a real benefit when gp internally is much more efficient in using separate eventhandlers than gp script is in doing If IsNoteOn(m)

Well, it’s always more efficient to use specific callbacks then to do you own testing.
That’s why, for example, we added the matching clause for some callbacks.

Whether that efficiency matters is a different question and obviously depends on what you’re doing.

1 Like