Frank Barknecht wrote:
Hi (Claude),
inspired by Matteo's question I briefly tried to implement a [pipe] clone in pdlua, however I ran into problems which might hint at either some design issues with clock support in pdlua or, more likely, my lack of understanding how clocks work.
[snip]
For the luapipe [lpipe] I thought I'd mimick that approach and collect the scheduled events in a table as member of the pdlua-class, like self.events.
It's easier to use idiomatic Lua, I think.
So the method to add an event could look like this in pseudo-Lua:
function M:in_1(sel, atoms) local deltatime = atoms[1] -- delay local e = event.new() e.payload = atoms e.clock = makeclock(deltatime) table.insert(self.events, e) end
My problem is the hypothetical "makeclock". It should be a clock that somehow is tied to the event. But as I see it, clocks in pdlua are supposed to be tied to a pdclass object itself, and their callback methods don't accept any additional arguments to specify which of the event they should handle.
Any ideas how to work around this?
Lua has lexical scope, and allows you to create closures (ie, functions that reference some part of their context). You don't need a table of events, you can just 'embed' the data in the function for each event.
I changed the interface of your object btw, having an "anything" inlet for messages and a "float" inlet for delay time, which is safer (you had an anything method, but assume the first atom is a float - bug!).
Something like this probably works (untested...):
-- initialise self.nextID to some number in constructor
function M:in_2_float(f) self.deltatime = math.max(0, f) end
function M:in_1(sel, atoms) -- we need unique method names for our clock callbacks self.nextID = self.nextID + 1 local id = "trigger" .. self.nextID local clock = pd.Clock:new():register(self, id) -- the clock callback outlets the data and cleans up self[id] = function(self) self:outlet(1, sel, atoms) clock:destruct() self[id] = nil end -- now start the clock clock:delay(self.deltatime) end
Hope this helps, was an interesting problem,