TouchOSC Snippets
- PUBLISHED: 2024-2-8
- UPDATED: 2024-2-17
A collection of my code snippets for Touch OSC.
Debugging¶
Debug Frame
Debug Lua Table
function debugTableInternal(tableName, table)
  local s = "TABLE: " .. tableName .. " {" 
  for k, v in pairs(table) do
    if(type(v) == "table") then
      local sDeep = debugTable(tostring(k), v)
      if (sDeep ~= nil) then
        s = s .. sDeep 
      end
    end
    s = s .. k .. ": " .. tostring(v) .. ", "
  end
  s = string.sub(s, 1, -3)
  s = s .. "}"
  return s
end
function debugTable(tableName, table)
  print(debugTableInternal(tableName, table))
end
Per Frame Updates¶
Values¶
onValueChanged()
Notify¶
onReceiveNotify()
OSC¶
onReceiveOSC()
sendOSC()
-- -----------------------------------------
-- Send simple OSC messages
--
-- arguments are auto-converted to
-- boolean, float or string (not integer!)
-- -----------------------------------------
-- send on all configured connections (1-5)
sendOSC('/simple')
sendOSC('/ping', 'pong')
sendOSC('/on', true)
sendOSC('/1/fader1', 0.5)
sendOSC('/3/xy1', 0.25, 0.75)
sendOSC('/mixedarguments', 'Hello', 1, true, 'World')
-- send only on connections 1 and 2
sendOSC('/1/fader1', 0.5, { true, true })
-- send only on connections 1 and 3
sendOSC('/3/xy1', 0.25, 0.75, { true, false, true })
-- send only on connections 1 and 5
sendOSC('/mixedarguments', 'Hello', 1, true, 'World', { true, false, false, false, true })
-- -----------------------------------------
-- Send complex OSC messages
-- with argument type tags
-- -----------------------------------------
sendOSC(
  -- message
  {
    -- path
    '/complex',
    -- argument list
    {
      { tag = 'T' },                                     -- true
      { tag = 'F' },                                     -- false
      { tag = 'N' },                                     -- nil
      { tag = 'I' },                                     -- infinitum
      { tag = 'i', value = 42 },                         -- int32
      { tag = 'f', value = 3.14 },                       -- float32
      { tag = 's', value = 'Goodbye Cruel World' },      -- string
      { tag = 'b', value = { 0xC0, 0x00, 0x10, 0xFF } }  -- blob
    }
  },
  -- connections
  {
    true, -- 1
    true, -- 2
    true, -- 3
    true, -- 4
    true  -- 5
  }
)
Math¶
Map: Remap a value to a different range
Tables¶
Deep Table Clear
Find nearest value in table
function findClosestValue(table_index, value)
  local value_dif = 20000
  local num_items = #table_index
  local lowest_index = 0
  for index, table_value in pairs(table_index) do
    local dif = math.abs(table_value - value)
    if(dif < value_dif) then
      value_dif = dif
      lowest_index = index
    end
  end
  return lowest_index
end
Pager¶
Color All Pages in Pager
local tabColorOff = Color(0.2,0.2,0.2) 
local tabColorOn = Color(1,0,0) 
local textColorOn = Color(1,1,1) 
local textColorOff = Color(0,0,0) 
local numPages = #self.children 
for i=1, numPages do 
    local page = self.children[i]
    local props = page.properties
    props.tabColorOff = tabColorOff 
    props.tabColorOn = tabColorOn 
    props.tabColorOff = textColorOn 
    props.tabColorOff = textColorOff 
end
Finding Objects¶
All Object Based On Tag
Find All Objects Based on Tag With Full Path
local objs = root:findAllByProperty("tag", "3/5", true)
-- print all object names and their path to root
for i, obj in ipairs(objs) do
  print(obj.name .. " " .. getPathToRoot(obj))
end
function getPathToRoot(obj)
  local path = obj.name
  local parent = obj.parent
  while parent ~= nil do
    path = parent.name .. "/" .. path
    parent = parent.parent
  end
  return path
end