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

Questions and discussion about building and using Max for Live devices
Post Reply
iainduncan
Posts: 19
Joined: Tue Jun 12, 2012 6:06 pm

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

Post by iainduncan » Mon Dec 17, 2012 4:08 am

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

amounra93
Posts: 431
Joined: Sat Jan 24, 2009 8:16 pm
Location: Arcata, CA
Contact:

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

Post by amounra93 » Mon Dec 17, 2012 11:00 pm

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.
http://www.aumhaa.com for Monomod and other m4l goodies.

iainduncan
Posts: 19
Joined: Tue Jun 12, 2012 6:06 pm

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

Post by iainduncan » Sat Dec 22, 2012 12:21 am

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

amounra93
Posts: 431
Joined: Sat Jan 24, 2009 8:16 pm
Location: Arcata, CA
Contact:

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

Post by amounra93 » Sat Dec 22, 2012 8:17 am

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.
http://www.aumhaa.com for Monomod and other m4l goodies.

ST8
Posts: 259
Joined: Mon Jan 26, 2009 12:55 pm

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

Post by ST8 » Sat Dec 22, 2012 6:57 pm

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))


amounra93
Posts: 431
Joined: Sat Jan 24, 2009 8:16 pm
Location: Arcata, CA
Contact:

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

Post by amounra93 » Sat Dec 22, 2012 9:44 pm

Hehe....yeah, that's a lot easier.
http://www.aumhaa.com for Monomod and other m4l goodies.

iainduncan
Posts: 19
Joined: Tue Jun 12, 2012 6:06 pm

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

Post by iainduncan » Sat Dec 22, 2012 10:58 pm

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

Post Reply