Defining global functions with TouchOSC

The new version of TouchOSC looks like it might be a decent competitor to Lemur but I’m struggling due to a lack of documentation (and they don’t seem to have a support forum either)

Did anybody here get the new TouchOSC app? If so, did you figure out how to define a global function that will be available to all callbacks?

Available for all global callbacks, or those at a widget/control level?
The only way I’ve found to communicate between the global script and widgets/controls is using the notify function. Everything I’ve tried to reference a custom global function in the widget/control script ends up with: No such property or function.

Some examples based on what I’ve figured out…

This is a button script (named ‘button2’):

function onValueChanged(key)
  if  key == 'touch' and self.values[key] == true then
    self.notify(self.parent, self.name, "Hello World")
  end
end

This is in the global script:

function onReceiveNotify(source, string)
  print(string.." from "..source)
end

Console message, after pressing button2.

You can use the notify function to go back the other way, passing parameters from a global function back to particular child controls.

When sticking within the global script, this part is within the onReceiveOSC callback:

local myNumber = 10
print("Start with: "..myNumber)
print("End with: "..multiplyByTen(myNumber))

This is the multiplyByTen function:

function multiplyByTen(number)
  return (number * 10)
end

This is the console message:

First of all, where did you put a global script?

I want to just put regular functions in there that can be called from multiple widget/control callbacks

Accessed by clicking the background. Then the script box in the right-pane is the global script.

Oh yeah, I had tried that first. I put a trivial function there and tried to call it from a control and got some message about a nil something or other.

After looking at their document, it seems that every control has its own Lua context which is probably why this doesn’t work. Sounds like a bad approach…they should at least have provided some shared area somewhere. I wonder if there is an “include” mechanism that could be used, but even if there is, it seems like there might be a problem controlling all the local controls.

For example, if you have ten faders, how would you write a function that would set them all to zero?

You can easily set the controls’ parameters from the global script.

Have a look at the very last part of the example I posted here:

The key would be to put all the faders in a group. That way you can refer to them by index value.

Yes, I saw that self.children but that seems to be only available inside the OnReceiveOSC callback — how would one have “normal” global functions that can refer to those things?

As for putting faders in a group (which I already did), I probably should have asked my question differently…i.e. how do I set all faders except the one I’m passing as a parameter to 0?

In other words, implement radio button behavior

function onReceiveOSC(message, connections)
  RadioButton('faders', 3, 1.0)
end

function RadioButton(group, index, value)
  for i = 1, #self.children[group].children do
      if i == index then
        self.children[group].children[i].values.x = value
      else
        self.children[group].children[i].values.x = 0.0
      end
    end
end

Right. But I want to be able to call that RadioButton from the script in a control.

Otherwise it looks like one would have to put all the code in that global document and never add code to the individual controls

I have a question more or less related to this topic. I’m trying to create a TouchOSC template to provide personal monitoring control focused on TotalMix Fx, which is the mixer of my RME audio interface. In TM FX a channel can have a different function depending on the current bus mode. You can change the bus mode by sending one of the following OSC messages: “/1/busInput/ 1.0 “, “/1/busPlayback/ 1.0 “ or “/1/busOutput/ 1.0 “. In a personnal mixer you potential have to control a mic level (busInput), a keyboard mix (busPlayback) and a global level (busOutput). When moving a fader, it is easy, you simply have to set the bus you need before sending the fader volume message. But if you want to receive any incoming message you have to filter out those not related to your fader (meter, label, or whatever) by knowing the current bus.

You can know the current bus by receiving one of the OSC bus message and my idea is to store this information in a global variable, such that when receiving an OSC value, I could perhaps use the touchOSC control (widget) script to check if the control/widget value has to be modified because the bus mode related to this particular control/widget is selected.
e.g. if my volume1 control/fader receives the OSC message /1/volume1 x, I only want it to be modified if the current bus mode is /1/busInput.

  • Where is the better place to store this global value?
  • Is the Lua context of the touchOSC global script shared with the local control script? Could it be possible to store this information in each control script, will the scope of a value persist after the local control script has been run?
  • Should I use a hidden control to receive the bus message? There is then probably a way to check this control value from another local control script…

As I am starting with touchOSC, any help will be appreciated… :wink:

Apparently all those contexts are independent - at least they were the last time I looked and I had an email exchange with the developers about it —

1 Like

I finally went to another solution. As I am a TouchOSC newbie, I don’t know if it is the best solution and my definitive one. For each control, I use a script which catches the bus information and accordingly enable/disable the OSC message which receives the value changes. Now, I will try to find a solution to cycle the bus mode in order to receive meter information for each fader, given that they are all in different bus mode. I don’t know if it is doable, but I will try to do it. :nerd_face: