|
| 1 | +local luaobj = pd.Class:new():register("lua") |
| 2 | + |
| 3 | +local bit = require("bit") |
| 4 | + |
| 5 | +local TWO31 = 2147483648 |
| 6 | +local function rand_u31() |
| 7 | + return bit.band( |
| 8 | + bit.tobit(math.random() * 4294967296), -- 2^32 |
| 9 | + 0x7fffffff |
| 10 | + ) |
| 11 | +end |
| 12 | + |
| 13 | +function luaobj:initialize(sel, atoms) |
| 14 | + self.inlets = {} |
| 15 | + self.outlets = {} |
| 16 | + self.gui = 0 |
| 17 | + |
| 18 | + local code_start = 0 |
| 19 | + |
| 20 | + local i = 1 |
| 21 | + while i <= #atoms do |
| 22 | + local atom = atoms[i] |
| 23 | + if i > 1 and atom == ";" then |
| 24 | + code_start = i; |
| 25 | + break -- Stop if we reach the first occurrence of ";" |
| 26 | + end |
| 27 | + |
| 28 | + if type(atom) == "string" then |
| 29 | + if atom == "-in" and i < #atoms and type(atoms[i + 1]) == "number" then |
| 30 | + local in_count = atoms[i + 1] |
| 31 | + for _ = 1, in_count do |
| 32 | + table.insert(self.inlets, DATA) |
| 33 | + end |
| 34 | + i = i + 1 -- Skip the next element since we've used it |
| 35 | + elseif atom == "-sigin" and i < #atoms and type(atoms[i + 1]) == "number" then |
| 36 | + local sigin_count = atoms[i + 1] |
| 37 | + for _ = 1, sigin_count do |
| 38 | + table.insert(self.inlets, SIGNAL, 1) |
| 39 | + end |
| 40 | + i = i + 1 -- Skip the next element since we've used it |
| 41 | + elseif atom == "-out" and i < #atoms and type(atoms[i + 1]) == "number" then |
| 42 | + local out_count = atoms[i + 1] |
| 43 | + for _ = 1, out_count do |
| 44 | + table.insert(self.outlets, DATA) |
| 45 | + end |
| 46 | + i = i + 1 -- Skip the next element since we've used it |
| 47 | + elseif atom == "-sigout" and i < #atoms and type(atoms[i + 1]) == "number" then |
| 48 | + local sigout_count = atoms[i + 1] |
| 49 | + for _ = 1, sigout_count do |
| 50 | + table.insert(self.outlets, SIGNAL, 1) |
| 51 | + end |
| 52 | + i = i + 1 -- Skip the next element since we've used it |
| 53 | + end |
| 54 | + end |
| 55 | + i = i + 1 |
| 56 | + end |
| 57 | + |
| 58 | + for _ = 1, code_start do |
| 59 | + table.remove(atoms, 1) |
| 60 | + end |
| 61 | + |
| 62 | + -- Concatenate atoms into a single string separated by spaces |
| 63 | + local lua_code = table.concat(atoms, " ") |
| 64 | + lua_code = string.gsub(lua_code, ";", "\n") |
| 65 | + |
| 66 | + self.function_prefix = "fn_" .. tostring(rand_u31()) .. "_" |
| 67 | + |
| 68 | + -- Give functions unique names to prevent namespace clashes |
| 69 | + lua_code = string.gsub(lua_code, "function%s+in_(%d+)_(%a+)", function(num, type) |
| 70 | + return "function " .. self.function_prefix .. "in_" .. num .. "_" .. type |
| 71 | + end) |
| 72 | + lua_code = string.gsub(lua_code, "function%s+in_n_(%a+)", function(type) |
| 73 | + return "function " .. self.function_prefix .. "in_n_" .. type |
| 74 | + end) |
| 75 | + lua_code = string.gsub(lua_code, "function%s+in_(%d+)", function(num) |
| 76 | + return "function " .. self.function_prefix .. "in_" .. num |
| 77 | + end) |
| 78 | + lua_code = string.gsub(lua_code, "function%s+in_n", function() |
| 79 | + return "function " .. self.function_prefix .. "in_n" |
| 80 | + end) |
| 81 | + lua_code = string.gsub(lua_code, "function%s$0", function() |
| 82 | + return "function " .. self.function_prefix |
| 83 | + end) |
| 84 | + lua_code = string.gsub(lua_code, "function%sdsp", function() |
| 85 | + return "function " .. self.function_prefix .. "dsp" |
| 86 | + end) |
| 87 | + lua_code = string.gsub(lua_code, "function%sperform", function() |
| 88 | + return "function " .. self.function_prefix .. "perform" |
| 89 | + end) |
| 90 | + |
| 91 | + -- Create a temporary file |
| 92 | + self.temp_name = os.tmpname() |
| 93 | + local temp_file = io.open(self.temp_name, 'w+b') |
| 94 | + |
| 95 | + if temp_file then |
| 96 | + -- Writing the concatenated string to the temp file |
| 97 | + temp_file:write(lua_code) |
| 98 | + |
| 99 | + -- It's important to close the file when you're done |
| 100 | + temp_file:close() |
| 101 | + |
| 102 | + self:dofile(self.temp_name) |
| 103 | + else |
| 104 | + pd.post("Error: could not create temp file") |
| 105 | + end |
| 106 | + |
| 107 | + return true |
| 108 | +end |
| 109 | + |
| 110 | +function luaobj:dsp(sample_rate, block_size) |
| 111 | + local m = _G[self.function_prefix .. "dsp"] |
| 112 | + if type(m) == "function" then |
| 113 | + return m(self, sample_rate, block_size) |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | +function luaobj:perform(...) |
| 118 | + local m = _G[self.function_prefix .. "perform"] |
| 119 | + if type(m) == "function" then |
| 120 | + return m(self, ...) |
| 121 | + end |
| 122 | +end |
| 123 | + |
| 124 | +function luaobj:dispatch(inlet, sel, atoms) |
| 125 | + if sel == "load" then |
| 126 | + self:dofile(atoms[1]) |
| 127 | + return |
| 128 | + end |
| 129 | + |
| 130 | + local m = _G[self.function_prefix .. string.format("in_%d_%s", inlet, sel)] |
| 131 | + if type(m) == "function" then |
| 132 | + if sel == "bang" then return m(self) end |
| 133 | + if sel == "float" then return m(self, atoms[1]) end |
| 134 | + if sel == "symbol" then return m(self, atoms[1]) end |
| 135 | + if sel == "pointer" then return m(self, atoms[1]) end |
| 136 | + if sel == "list" then return m(self, atoms) end |
| 137 | + return m(self, atoms) |
| 138 | + end |
| 139 | + m = self[self.function_prefix .. "in_n_" .. sel] |
| 140 | + if type(m) == "function" then |
| 141 | + if sel == "bang" then return m(self, inlet) end |
| 142 | + if sel == "float" then return m(self, inlet, atoms[1]) end |
| 143 | + if sel == "symbol" then return m(self, inlet, atoms[1]) end |
| 144 | + if sel == "pointer" then return m(self, inlet, atoms[1]) end |
| 145 | + if sel == "list" then return m(self, inlet, atoms) end |
| 146 | + return m(self, inlet, atoms) |
| 147 | + end |
| 148 | + m = self[self.function_prefix .. string.format("in_%d", inlet)] |
| 149 | + if type(m) == "function" then |
| 150 | + return m(self, sel, atoms) |
| 151 | + end |
| 152 | + m = self[self.function_prefix .. "in_n"] |
| 153 | + if type(m) == "function" then |
| 154 | + return m(self, inlet, sel, atoms) |
| 155 | + end |
| 156 | + self:error( |
| 157 | + string.format("no method for `%s' at inlet %d of Lua object `%s'", |
| 158 | + sel, inlet, self._name) |
| 159 | + ) |
| 160 | +end |
| 161 | + |
| 162 | +function luaobj:__gc() |
| 163 | + os.remove(self.temp_name) |
| 164 | +end |
0 commit comments