LogicMachine Forum
rewrite Python code to Lua - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: rewrite Python code to Lua (/showthread.php?tid=4745)



rewrite Python code to Lua - Hadeel - 30.04.2023

Hi!!

I am controlling an audio software with udp.
Follwoing is the python code working on the site.

Code:
from pythonosc.udp_client import SimpleUDPClient ip = "172.28.11.110" to_ableton = 11000 from_ableton = 11001 client = SimpleUDPClient(ip, to_ableton) client.send_message("/topic/of/message", [a, b, c])

I want to execute this from LM so I need to rewrite this in Lua. 
I've tried follwoing code but I got an error "invalid value (table) at index 2 in table for 'concat'".
Could you help me write this code properly? 

Code:
local socket = require("socket") local ip = "172.28.11.110" local to_ableton = 11000 local from_ableton = 11001 local msg = {"/topic/of/message", {a, b, c}} local udp = socket.udp() udp:settimeout(1) udp:setpeername(ip, to_ableton) udp:send(table.concat(msg, " "))

Thank you very much for your help  Smile


RE: rewrite Python code to Lua - admin - 02.05.2023

It's not that simple. You need a library for the OSC protocol: https://github.com/davidgranstrom/losc/

Run this script once to install the library (make sure that LM has valid gateway and DNS settings):
Code:
if not io.exists('/usr/share/lua/losc/') then   os.execute('wget -O /tmp/losc.tar.gz http://dl.openrb.com/misc/losc.tar.gz && ' ..     'tar -xzf /tmp/losc.tar.gz -C /') end

A slight difference from the examples is that the main library should be loaded like this:
Code:
-- instead of local losc = require'losc' local losc = require'losc.init'



RE: rewrite Python code to Lua - Hadeel - 03.05.2023

Wow thank you admin!
I never thought about that.
I executed the installation script as you wrote and now I can successfully send request to Abelton.  Smile

However I am having difficulties with receiving messages from Abelton.
This is what I am trying on resident script with 0 interval but it seems like I'm not receiving anything.
Can you tell if I'm doing something wrong?  Cry

Code:
local losc = require'losc.init' local plugin = require'losc.plugins.udp-socket' local udp = plugin.new {   recvAddr = '127.0.0.1',   recvPort = 11001,   ignore_late = true, -- ignore late bundles } local osc = losc.new {plugin = udp} osc:add_handler('*', function(data)   log(data) end)



RE: rewrite Python code to Lua - admin - 03.05.2023

recvAddr must be your LM IP address. 127.0.0.1 is an internal address which cannot receive anything from remote hosts.
You need to call osc:open() after osc:add_handler(...)


RE: rewrite Python code to Lua - Hadeel - 03.05.2023

Omg it worked!
I cannot describe how thankful I am! 
Thank you for your help even though it's about osc and abelton. 
Much appreciated  Angel