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
|
local tictoc = pd.Class:new():register("tictoc")
function tictoc:initialize(sel, atoms)
-- inlet 1 takes an on/off flag, inlet 2 the delay time
self.inlets = 2
-- bangs are output alternating between the two outlets
self.outlets = 2
-- the delay time (optional creation argument, 1000 msec by default)
self.delay = type(atoms[1]) == "number" and atoms[1] or 1000
-- we start out on the left outlet
self.left = true
-- initialize the clock
self.clock = pd.Clock:new():register(self, "tictoc")
return true
end
function tictoc:finalize()
self.clock:destruct()
end
-- As with the metro object, nonzero, "bang" and "start" start the clock,
-- zero and "stop" stop it.
function tictoc:in_1_float(state)
if state ~= 0 then
-- output the first tick immediately
self:tictoc()
else
-- stop the clock
self.clock:unset()
end
end
function tictoc:in_1_bang()
self:in_1_float(1)
end
function tictoc:in_1_start()
self:in_1_float(1)
end
function tictoc:in_1_stop()
self:in_1_float(0)
end
-- set the delay (always in msec, we don't convert units)
function tictoc:in_2_float(delay)
-- this will be picked up the next time the clock reschedules itself
self.delay = delay >= 1 and delay or 1
end
-- the clock method: tic, toc, tic, toc ...
function tictoc:tictoc()
-- output a bang, alternate between left and right
self:outlet(self.left and 1 or 2, "bang", {})
self.left = not self.left
-- reschedule
self.clock:delay(self.delay)
end
|