1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
--
-- melisma - extensible and scriptable audio engine
-- Copyright (C) 2026 Ludvig Gunne Lindström
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
--
local M = {}
function M.decode(data)
local addr,ttag,offset = string.unpack('!4 z Xs4 z Xs4',data)
if offset == nil then error('invalid osc message') end
ttag = string.sub(ttag,2)
data = string.sub(data,offset)
local args = {}
for i = 1,#ttag do
local format
local c = string.sub(ttag,i,i)
if c == 's' then format = '!4 z Xs4'
elseif c == 'i' then format = '>i4'
elseif c == 'f' then format = '>f'
else error('invalid osc message') end
local arg,offset = string.unpack(format,data)
if offset == nil then error('invalid osc message') end
table.insert(args,arg)
data = string.sub(data,offset)
end
return {addr=addr, args=args}
end
function M.encode(msg)
local addr = msg.addr
local args = msg.args
local data = ''
local ttag = ','
for _,arg in pairs(args) do
if type(arg) == 'string' then
ttag = ttag..'s'
data = data..string.pack('!4 z Xs4',arg)
elseif type(arg) == 'number' then
if math.type(arg) == 'integer' then
ttag = ttag..'i'
data = data..string.pack('>i4',arg)
elseif math.type(arg) == 'float' then
ttag = ttag..'f'
data = data..string.pack('>f',arg)
else
error('unsupported argument type '..math.type(arg))
end
else
error('unsupported argument type '..type(arg))
end
end
return string.pack('!4 z Xs4',addr)..string.pack('!4 z Xs4',ttag)..data
end
return M
|