From 267170167d52cab9e97f879d9127a1cf04f6bb58 Mon Sep 17 00:00:00 2001 From: Martin Peach Date: Tue, 15 Mar 2011 20:53:57 +0000 Subject: This is a version of Claude Heiland-Allen's lua for Pd. The objects are named pdlua and pdluax instead of lua and luax. So far it seems to work on linux. svn path=/trunk/externals/pdlua/; revision=15030 --- examples/list-unpack.pd_lua | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/list-unpack.pd_lua (limited to 'examples/list-unpack.pd_lua') diff --git a/examples/list-unpack.pd_lua b/examples/list-unpack.pd_lua new file mode 100644 index 0000000..08b635c --- /dev/null +++ b/examples/list-unpack.pd_lua @@ -0,0 +1,57 @@ +--[[ + +list-unpack + Like [unpack] for any kind of type. Argument specifies number of outlets. + pointers untested rsp. not supported. + --Written by Frank Barknecht + --Fixed for pdlua-0.2svn by Clahde Heiland-Allen +--]] + +-- Some footils -- + +-- outlet selectors +local selectors = {"float", "list", "symbol", "bang", "pointer"} + +-- check if item x is in table t: +local function contains(t, x) + for _,v in ipairs(t) do + if v == x then return true end + end + return false +end + +-- Pd class + +local ListUnpack = pd.Class:new():register("list-unpack") + +function ListUnpack:initialize(name, atoms) + self.inlets = 1 + if atoms[1] == nil then + self.outlets = 1 + return true + elseif type(atoms[1]) == "number" and atoms[1] >= 0 then + self.outlets = math.max(atoms[1], 1) + return true + else + pd.post("list-unpack: First arg must be a positive float or empty") + return false + end +end + +function ListUnpack:Outlet(num, atom) + -- a better outlet: automatically selects selector + -- map lua types to pd selectors + local selectormap = {string = "symbol", number="float", userdata="pointer"} + self:outlet(num, selectormap[type(atom)], {atom}) +end + +function ListUnpack:in_1(sel, atoms) + if not contains(selectors, sel) then + -- also unpack selector of anythings + table.insert(atoms, 1, sel) + end + local size = math.min(self.outlets, table.getn(atoms)) + for i=size, 1, -1 do + self:Outlet(i, atoms[i]) + end +end -- cgit v1.2.1