Page 1 of 1

How to add a callback to a button element in Python?

Posted: Mon Dec 17, 2012 4:08 am
by iainduncan
I know this is the M4L section, but I'm hoping there are Python control surface peeps here too.

I'm not sure from looking through examples what the right way is to have an arbitrary python callable get called on some midi message in. It seems to me (correct me if I'm wrong) that the control surface scripts don't have any kind of generic midi receive routine, is that correct? We need to make ButtonElements for each message we want to get? And if I make a button element, what is the best way to have that fire a method that does things with the live API?

thanks!
Iain

Re: How to add a callback to a button element in Python?

Posted: Mon Dec 17, 2012 11:00 pm
by amounra93
It's slightly tricky.

Yes, you have to explicitly forward the id's you want callbacks from using the ControlSurface.install_forwarding(), or do it manually using the Live.MIDIMap.forward_midi_note() method. If doing it the former way, you'll need to have a ButtonElement to assign the callback to.

Also, forwarding is somewhat deferred, so you won't be getting timing accurate calls from incoming MIDI that way.

The easiest way to do this is set the corresponding InputControlElement._report_input property to True and then override the InputControlElement._report_value() method with your own callback. This is how I used to do things before I understood exactly how everything worked.

Re: How to add a callback to a button element in Python?

Posted: Sat Dec 22, 2012 12:21 am
by iainduncan
Thanks. I'd def prefer low latency and don't mind having to do more coding for it. Do you know which approach provides the snappiest timing? Am I better off just making Button objects for each callback I wan't hooked up?

thanks
Iain

Re: How to add a callback to a button element in Python?

Posted: Sat Dec 22, 2012 8:17 am
by amounra93
It will take just as long either way. It's mostly just a matter of which implementation is most convenient for your purposes. If you are only needing to receive notifications from a handful of id's, it's probably easier to set up button elements and override ControlElement._report_value.

Re: How to add a callback to a button element in Python?

Posted: Sat Dec 22, 2012 6:57 pm
by ST8
Heres a quick example on how to listen to a particular midi message in live:

test.py in midi remote scripts/test/

Code: Select all

from _Framework.ControlSurface import ControlSurface
from _Framework.InputControlElement import *
from _Framework.SliderElement import SliderElement

class Test(ControlSurface):
    def __init__(self, c_instance):
        ControlSurface.__init__(self, c_instance)

        channel = 0
        cc_id = 0
        slider = SliderElement(MIDI_CC_TYPE, channel, cc_id)
        slider.add_value_listener(self.yourcallback)

    def yourcallback(self, value):
         self.log_message(str(value))


Re: How to add a callback to a button element in Python?

Posted: Sat Dec 22, 2012 9:44 pm
by amounra93
Hehe....yeah, that's a lot easier.

Re: How to add a callback to a button element in Python?

Posted: Sat Dec 22, 2012 10:58 pm
by iainduncan
Thanks ST8, that's what I've since arrived at from looking at code samples, and wasn't sure if it was the right way to do thing in that the Button objects seem kind of extraneous, but if that's how it's done, so be it!

Thanks for the help, both of you.

Iain