[PD] Clocks and [pipe] in Lua

Frank Barknecht fbar at footils.org
Thu Mar 13 14:11:42 CET 2008


Hallo,
Claude Heiland-Allen hat gesagt: // Claude Heiland-Allen wrote:

> Frank Barknecht wrote:
> > 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. 
> 
> 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!).

Error checking was deliberately left out. ;) In the end I'd like to
have the delay time up front to be able to use qlist-ish messages like: 
"0 a b c, 1000 x y z, 300 bang".

> 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

Ah, a very elegant solution. It's tested now (attached) and seems to
work really well.

Ciao
-- 
 Frank Barknecht                                     _ ______footils.org__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lpipe-help.pd
Type: application/puredata
Size: 423 bytes
Desc: not available
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20080313/51ea05d8/attachment.bin>
-------------- next part --------------
-- reimplementation of [pipe] for any message
-- claude(+fbar) 2008

local M = pd.Class:new():register("lpipe")

function M:initialize(name, atoms)
    self.inlets = 2
    self.outlets = 1 
    self.nextID = 0
    return true
end

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


More information about the Pd-list mailing list