Simple OSC functions for LÖVE game engine
Posted: Wed Sep 22, 2021 5:55 pm
Here's a set of simple functions (not even a library) to package OSC data (sent via the socket layer), for the LÖVE game engine.
https://love2d.org/
Why? LÖVE is supported on many platforms, so it works on the Linux desktop and mobile devices (and other OS's), it's interpreted (code is easy to modify) and if LÖVE runs on your platform, there are no other dependencies (other than GUI libraries, if that's desired).
Plus any other options for OSC controllers (other than TouchOSC or MobMuPlat, etc.) are a good thing, IMHO.
NOTE: there are several lua OSC libraries, but none seem to function in LÖVE 11.3.
The OSC functions:
oscLv.lua
The main.lua file, with the love2d code and the socket lib code:
main.lua
The socket module is included with LÖVE, so it's not an external dependency.
Create a folder, save the first code block as "oscLv.lua", then save the second code block as "main.lua".
CD to the parent dir, then type:
love <folder name>
The contents of the folder (two files) can be archived as a zip, then renamed to something like "demo.love". A double-click in Linux will load love2d and run the demo. A ".love" file is much easier to use on mobile devices, too.
Caveats:
- There's really no error checking (wouldn't call this a library, it's just a proof-of-concept).
- No blobs currently (just f,i,s,S types).
- Symbols are loosely documented (it seems to work). A symbol is truncated at the first space, though.
- No receive functions yet.
This demo just interprets mouse movements above the line, and outputs an OSC message. Clicking below the line quits the app.
I've tested this on the Ubuntu desktop, and an Android tablet. The screen orientation and size are set for portrait for my mobile device - that's easy to change. it's sending to the multicast group on port 20331. I've made some demos with GUI libraries, but his minimal demo is only about 2K of code (less if it's zipped).
I use this to receive and debug the OSC messages:
oscdump 20331
(oscdump is part of the liblo OSC library) Also tested with pd2jack.
USE:
Create a table with the data:
local msgT = {float1, float2)}
Make the packet - Format: (/OSCADDR, 'theDataTypes', the data table) :
local packet = oscPacket('/P2Jcli/0/pp', 'ff', msgT)
Send the packet:
udp:send(packet)
https://love2d.org/
Why? LÖVE is supported on many platforms, so it works on the Linux desktop and mobile devices (and other OS's), it's interpreted (code is easy to modify) and if LÖVE runs on your platform, there are no other dependencies (other than GUI libraries, if that's desired).
Plus any other options for OSC controllers (other than TouchOSC or MobMuPlat, etc.) are a good thing, IMHO.
NOTE: there are several lua OSC libraries, but none seem to function in LÖVE 11.3.
The OSC functions:
oscLv.lua
Code: Select all
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++
-- "oscLv" by Doug Garmon, 2021
-- MINIMAL OSC packing implementation for love2d
-- License: "The Unlicense"
-- For more information, please refer to <https://unlicense.org/>
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++
-- osc packet functions for sockets
local endpad = string.char(0, 0, 0, 0)
local modtab = {0, 3, 2, 1}
function oscString (Str)
local newS, mod
newS = Str..string.char(0x0)
mod = string.len(newS) % 4
return(newS..string.sub(endpad, 1, modtab[mod + 1]))
end
function oscType (Str)
return(oscString(','..Str))
end
function oscSymbol (Str)
local s1, s2 = string.find(Str, " ")
return(oscString(string.sub(Str, 1, s1)))
end
function oscPacket (addrS, typeS, msgTab)
local strl, types
strl = oscString(addrS)..oscType(typeS)
for argC = 1, #msgTab do
types = string.sub(typeS, argC, argC)
if types == 's' then
strl = strl..oscString(msgTab[argC])
elseif types == 'S' then
strl = strl..oscSymbol(msgTab[argC])
elseif types == 'f' then
strl = strl..love.data.pack('string', '>f', msgTab[argC])
elseif types == 'i' then
strl = strl..love.data.pack('string', '>i', msgTab[argC])
end
end
return(strl)
end
-- osc packet functions END
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++
main.lua
Code: Select all
-- "oscLv" demo mail.lua file by Doug Garmon
-- License: "The Unlicense"
-- For more information, please refer to <https://unlicense.org/>
-- Networking code: from love2d.org -- Tutorial:Networking with UDP, client
local socket = require "socket"
local oscLv = require "oscLv"
-- the address and port of the server
local address, port = "224.0.0.1", 20331
local mouse = {0, 0}
local mscale = 596
local lineloc = 400
local font = {}
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++
function math.clamp(low, n, high) return math.min(math.max(n, low), high) end
function love.load()
love.window.setMode(601, 906, {resizable=false})
font = love.graphics.newFont(16)
love.graphics.setFont(font)
udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)
end
function love.update(deltatime)
if love.mouse.isDown(1) then
mouse.x, mouse.y = love.mouse.getPosition()
if lastX ~= mouse.x then
local msgT = {2, math.clamp(0, mouse.x / mscale, 1)}
local packet = oscPacket('/P2Jcli/0/pp', 'ff', msgT)
udp:send(packet)
lastX = mouse.x
end
if mouse.y > lineloc then
love.event.quit()
end
end
end
function love.draw()
love.graphics.setColor(0.5, 0.5, 0.5)
love.graphics.line(10, lineloc, 590, lineloc )
love.graphics.print("Click below line to quit", 100, lineloc + 10)
end
Create a folder, save the first code block as "oscLv.lua", then save the second code block as "main.lua".
CD to the parent dir, then type:
love <folder name>
The contents of the folder (two files) can be archived as a zip, then renamed to something like "demo.love". A double-click in Linux will load love2d and run the demo. A ".love" file is much easier to use on mobile devices, too.
Caveats:
- There's really no error checking (wouldn't call this a library, it's just a proof-of-concept).
- No blobs currently (just f,i,s,S types).
- Symbols are loosely documented (it seems to work). A symbol is truncated at the first space, though.
- No receive functions yet.
This demo just interprets mouse movements above the line, and outputs an OSC message. Clicking below the line quits the app.
I've tested this on the Ubuntu desktop, and an Android tablet. The screen orientation and size are set for portrait for my mobile device - that's easy to change. it's sending to the multicast group on port 20331. I've made some demos with GUI libraries, but his minimal demo is only about 2K of code (less if it's zipped).
I use this to receive and debug the OSC messages:
oscdump 20331
(oscdump is part of the liblo OSC library) Also tested with pd2jack.
USE:
Create a table with the data:
local msgT = {float1, float2)}
Make the packet - Format: (/OSCADDR, 'theDataTypes', the data table) :
local packet = oscPacket('/P2Jcli/0/pp', 'ff', msgT)
Send the packet:
udp:send(packet)