Live API Clarifications

Trevor being Trevor's icon

I wanted to access and use a Live API object across multiple functions. Therefore, I placed the line

var liveAPI = new LiveAPI();

to declare it as a global variable and then I simply used

liveAPI.goto(<desired path>)

in each of my functions and then used get() or set() to access properties as needed. I recently noticed the technical note on the LiveAPI Object documentation page that says...

Technical note: you cannot use the LiveAPI object in JavaScript global code. Use the live.thisdevice object to determine when your Max Device has completely loaded (the object sends a bang from its left outlet when the Device is fully initialized, including the Live API).

Strangely, my code works just fine in Ableton 10. I am encountering issues in 11, but I believe that is related to the Task object right now. Also, the sample code at the bottom of the documentation page seems contradictory to the technical note as well. All that said, here are my questions...

  1. Should I remove my global variable and create a new LiveAPI object in each function I need to use it within? Or is there a better way?

  2. In general, when does it make sense to create a new LiveAPI object over using an existing one and just using the goto() function like I did?

  3. Depending on the answer to question 2, my code has the potential to generate a fair number LiveAPI objects. Should I be concerned?

  4. It feels like I am missing a little bit of fundamental understanding here, is there a resource where I could gain a better understanding of the potential woes of creating a ton of LiveAPI objects?


Thanks for any insight in advance, I always appreciate how helpful this forum is!

tyler mazaika's icon

You can still declare a variable globally but only assign it a LiveAPI object inside a function. (I always assumed that technical note is a cousin of the “you need to wait for live.thisdevice to determine when you can access the LiveAPI” thing in Max). Then you can easily use/reuse the same object In different functions.

there is no official documentation of best practices I’ve ever seen on this website.

goto (and keeping the same LiveAPI object around generally) is useful for cases where you’ve set up an observer/callback or in loops where you just need to get info from lots of LOM objects.

I don’t think you nerd to worry about generating lots of one-off LiveAPI objects as a general rule either. (Except there was a heinous memory leaking bug with LiveAPI a few years ago that has thankfully been fixed).

Trevor being Trevor's icon

Gotcha, that definitely adds a bit of clarity! Yes, I could not find any documentation either. I wondered if there was a resource outside of Max just on the concept in general, but a Max specific one would obviously be helpful.