Triggering a MIDI note when a switch on the Macro Page is clicked

Hi

Is it possible to send (trigger) a certain midi note to a certain layer when you click on a switch on the Macro Page? Case in point: I have a push-type switch on the Macro Page and when I click on that switch I want Layer 1 (which contains a synth zone) to play note 60 (C3) with velocity 100 for 200ms.

Something similar is implemented in Halion’s B-Box instrument:
img
When you click on that yellow switch a drum sample is triggered.

Maybe something like this could work:

defineParameter("s1", nil, false, function() playKey(60, "s1", 1) end)
defineParameter("s2", nil, false, function() playKey(62, "s2", 2) end)


function PlayNote()
    playNote(note, 100, 200, layer)
end

function playKey(noteNumber, parameter, layerNumber)
    if _G[parameter] then
        note = noteNumber
        if layerNumber then
            layer = this.parent:getLayer(layerNumber)
        end        
        runSync(PlayNote, 1)
        wait(100)
        _G[parameter] = false
    end
end

playkey

1 Like

Yesss! It works! Thank you so much! :partying_face: :hugs:

There’s just one little caveat which I think is due to how the switches behave and not related to the script: the note is not played when I press the “click” button on the mouse (click down) but it’s played when I release the button (click up). And this happens regardless if I set the mode of the switch to push or on/off.

And a side question: what is that _G[parameter] ? It’s the only thing in that script that I don’t know what it is and what it does. Does it refer to the s1 and s2 parameters that are defined at the beginning of the script?

Yes, the parameter changes when you release the mouse button, not when you click. I don’t know a way around this.

_G is a table where lua stores global variables. Parameters created by script are also considered global variables. I used this so that I could use the same function for both parameters.
(Yes, it refers to s1 and s2 parameters)

You can try simple experiment:

a = 5
print(a)
print(_G["a"])

Ah, I understand now. Thanks for the explanation! :+1:

Although I would have preferred to have the note triggered on click, it’s not such a big deal.
I tried all the modes for the switch and it looks like the only ones that trigger the note on click (not on mouse button released) are the increment and decrement modes. But there’s a big problem when using those: if you hold down the mouse button the note is re-triggered again and again until you release it.