Assigning a LUA function to a control - does the control still send a MIDI message based on its type?

Hi all,

Hopefully a quick LUA question.

If I assign a LUA function to a control, does the control still send a MIDI message based on its type?

For example, if I assign a LUA function to a Program Change fader control, does a MIDI program change message still get sent? Or does assigning a LUA function to a control replace its default behavior altogether?

IOW, if I assign a LUA function to a control, does it matter what type, and associated parameters, I give it?

Many thanks,

A specific example:

I dragged a Program Change fader onto my page and set a LUA function for it.

When I twist the knob, does it send a MIDI Program Change message and then call the LUA function? Or maybe it calls the LUA function first and sends a MIDI Program Change message after? Or maybe it doesn’t send a MIDI Program Change message at all, because I assigned a LUA function to it?!

I ask because the LUA function sends a Program Change message. I don’t want 2 program change messages to be generated every time I turn the knob.

I also ask because I’m confused about which control type (and associated parameters) I should set for a control whose sole purpose is to execute a LUA function.

Any clarification would be very much appreciated. Thanks in advance!

1 Like

Hi Ben,

The Lua function is fired in parallel to the MIDI message. If you wanted to run only the Lua function, you can use the “virtual” type.

The Lua function is given info about the value and the valueObject. It means it knows the type of the message but it is fully up to you, what you do with that. There are no restrictions.

1 Like

Thanks for the quick reply, @martin !

A quick follow up if I may:

I have my program change fader set to this:


Does it matter what the parameter number is?

Also, the knob selects all the way up to preset 128, but it only goes down to preset 2! I can’t get it to go to preset 1 unless I change the type to ‘Program change’!

Here’s my LUA function…

function bankAChangePreset (valueObject, value)
    if value==0 then return end
    midi.sendControlChange (PORT_1, 1, 0, 87)
    midi.sendControlChange (PORT_1, 1, 32, 92)
    midi.sendProgramChange (PORT_1, 1, value)

Edit: Confirmed. If I set the type to Virtual, I can’t select preset 1. If I change it to Program change, it’s 100% reliable but looks like it’s sending 2 x program change messages (one from the control and another from my LUA function).
Edit 2: I tried making it value - 1 in my LUA function, which kind of works, but it still gets stuck on preset 2 about 50% of the time (and I also have to change the MAX MIDI value to 128).

My guess is that it is caused by the:

That effectively tells Electra not to send the bank select and program changes messages. Also, note that 0 represents Program 1. Try removing that line.

1 Like

D’oh! User error!

You’d think I would have noticed that since I’m a senior software developer by day!

I got that code from a momentary pad example. I think the idea was to only trigger the program change once when the pad was ‘pressed’, rather than doing it twice - once when pressed and again when released. Obviously, it doesn’t apply to a fader.

Thanks again, @martin :slightly_smiling_face: