diff options
author | Martin Peach <mrpeach@users.sourceforge.net> | 2011-03-15 20:53:57 +0000 |
---|---|---|
committer | Martin Peach <mrpeach@users.sourceforge.net> | 2011-03-15 20:53:57 +0000 |
commit | 267170167d52cab9e97f879d9127a1cf04f6bb58 (patch) | |
tree | 00260a90ce6472e34c5eff41602af57595c8830d /examples |
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.svn2git-root
svn path=/trunk/externals/pdlua/; revision=15030
Diffstat (limited to 'examples')
60 files changed, 3286 insertions, 0 deletions
diff --git a/examples/complex-help.pd b/examples/complex-help.pd new file mode 100644 index 0000000..c78a0be --- /dev/null +++ b/examples/complex-help.pd @@ -0,0 +1,3 @@ +#N canvas 82 285 450 282 10; +#X obj 143 142 complex; +#X obj 144 114 pdluax complex; diff --git a/examples/complex.lua b/examples/complex.lua new file mode 100644 index 0000000..62d6d0e --- /dev/null +++ b/examples/complex.lua @@ -0,0 +1,30 @@ +local P = {} +if _REQUIREDNAME == nil then + complex = P +else + _G[_REQUIREDNAME] = P +end + +-- imports +-- local sqrt = math.sqrt + +-- no more external access after this point +setfenv(1, P) + +function new (r, i) return {r=r, i=i} end + +i = new(0, 1) + +function add(c1, c2) + return new(c1.r + c2.r, c1.i + c2.i) +end + +function sub(c1, c2) + return new(c1.r - c2.r, c1.i - c2.i) +end + +function mul(c1, c2) + return new(c1.r * c2.r - c1.i * c2.i, c1.r * c2.i + c1.i * c2.r) +end + +return P diff --git a/examples/complex.pd_lua b/examples/complex.pd_lua new file mode 100644 index 0000000..a3a3602 --- /dev/null +++ b/examples/complex.pd_lua @@ -0,0 +1,12 @@ +require("complex") + +local C = pd.Class:new():register("complex") + +function C:initialize(sel, atoms) + for k,v in pairs(complex) do + pd.post("complex." .. tostring(k) .. " = " .. tostring(v)) + end + self.inlets = 0 + self.outlets = 0 + return true +end diff --git a/examples/complex.pd_luax b/examples/complex.pd_luax new file mode 100644 index 0000000..78b604e --- /dev/null +++ b/examples/complex.pd_luax @@ -0,0 +1,9 @@ +require("complex") +return function (self, sel, atoms) + for k,v in pairs(complex) do + pd.post("complex." .. tostring(k) .. " = " .. tostring(v)) + end + self.inlets = 0 + self.outlets = 0 + return true +end diff --git a/examples/dispatchertest-help.pd b/examples/dispatchertest-help.pd new file mode 100644 index 0000000..2d65963 --- /dev/null +++ b/examples/dispatchertest-help.pd @@ -0,0 +1,8 @@ +#N canvas 0 0 450 300 10; +#X obj 98 174 dispatchertest; +#X floatatom 98 42 5 0 0 0 - - -; +#X symbolatom 144 75 10 0 0 0 - - -; +#X msg 191 110 a b c 1 2 3; +#X connect 1 0 0 0; +#X connect 2 0 0 1; +#X connect 3 0 0 2; diff --git a/examples/dispatchertest.pd_lua b/examples/dispatchertest.pd_lua new file mode 100644 index 0000000..35db321 --- /dev/null +++ b/examples/dispatchertest.pd_lua @@ -0,0 +1,21 @@ +local DispatcherTest = pd.Class:new():register("dispatchertest") + +function DispatcherTest:initialize(name, atoms) + self.inlets = 3 + return true +end + +function DispatcherTest:in_1_float(f) + pd.post("float: " .. f) +end + +function DispatcherTest:in_2_symbol(s) + pd.post("symbol: " .. s) +end + +function DispatcherTest:in_3(sel, atoms) + pd.post(sel .. ":") + for i,v in ipairs(atoms) do + pd.post(i .. " = " .. v) + end +end diff --git a/examples/dispatchertest.pd_luax b/examples/dispatchertest.pd_luax new file mode 100644 index 0000000..8707d62 --- /dev/null +++ b/examples/dispatchertest.pd_luax @@ -0,0 +1,13 @@ +return function (self, sel, atoms) + self.inlets = 3 + function self:in_1_float(f) pd.post("float: " .. f) end + function self:in_2_symbol(s) pd.post("symbol: " .. s) end + function self:in_3_foo(atoms) + pd.post("foo(") + for i,v in ipairs(atoms) do + pd.post(" " .. i .. " = " .. v) + end + pd.post(")") + end + return true +end diff --git a/examples/dumptypes.pd_luax b/examples/dumptypes.pd_luax new file mode 100644 index 0000000..6ac3e34 --- /dev/null +++ b/examples/dumptypes.pd_luax @@ -0,0 +1,12 @@ +-- dump Lua types for atoms, just to see what we get for pointers +return function(self, sel, atoms) + self.inlets = 1 + function self:in_1(sel, atoms) + pd.post(sel .. "[") + for _,v in ipairs(atoms) do + pd.post(type(v)) + end + pd.post("]") + end + return true +end diff --git a/examples/errors-help.pd b/examples/errors-help.pd new file mode 100644 index 0000000..3342487 --- /dev/null +++ b/examples/errors-help.pd @@ -0,0 +1,10 @@ +#N canvas 0 22 450 300 10; +#X obj 58 171 errors; +#X msg 31 64 moo; +#X msg 100 100 list moo baa; +#X msg 130 132 1; +#X msg 70 67 symbol baa; +#X connect 1 0 0 0; +#X connect 2 0 0 2; +#X connect 3 0 0 3; +#X connect 4 0 0 1; diff --git a/examples/errors.pd_lua b/examples/errors.pd_lua new file mode 100644 index 0000000..4d285e5 --- /dev/null +++ b/examples/errors.pd_lua @@ -0,0 +1,7 @@ +-- no methods, deliberately: test the error reporting! +local errors = pd.Class:new():register("errors") +function errors:initialize(sel, atoms) + self.inlets = 4 + self.outlets = 0 + return true +end diff --git a/examples/ldelay-help.pd b/examples/ldelay-help.pd new file mode 100644 index 0000000..85efb94 --- /dev/null +++ b/examples/ldelay-help.pd @@ -0,0 +1,20 @@ +#N canvas 0 0 450 300 10; +#X obj 187 150 ldelay 1000; +#X msg 259 89 500; +#X msg 272 112 1000; +#X msg 197 107 1500; +#X obj 187 76 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 187 199 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X msg 139 108 stop; +#X text 17 17 Reimplementation of [delay] in Lua to prove clocks work... +; +#X obj 351 265 delay 1000; +#X text 278 266 see also:; +#X connect 0 0 5 0; +#X connect 1 0 0 1; +#X connect 2 0 0 1; +#X connect 3 0 0 0; +#X connect 4 0 0 0; +#X connect 6 0 0 0; diff --git a/examples/ldelay.pd_lua b/examples/ldelay.pd_lua new file mode 100644 index 0000000..4e310e3 --- /dev/null +++ b/examples/ldelay.pd_lua @@ -0,0 +1,43 @@ +-- reimplementation of [delay] in Lua to test clock support + +local LDelay = pd.Class:new():register("ldelay") + +function LDelay:initialize(name, atoms) + if type(atoms[1]) ~= "number" or atoms[1] < 0 then + self.delay = 1000 + else + self.delay = atoms[1] + end + self.inlets = 2 + self.outlets = 1 + return true +end + +function LDelay:postinitialize() + self.clock = pd.Clock:new():register(self, "trigger") +end + +function LDelay:finalize() + self.clock:destruct() +end + +function LDelay:in_2_float(f) + self.delay = math.max(0, f) +end + +function LDelay:in_1_float(f) + self:in_2_float(f) + self:in_1_bang() +end + +function LDelay:in_1_bang() + self.clock:delay(self.delay) +end + +function LDelay:in_1_stop() + self.clock:unset() +end + +function LDelay:trigger() + self:outlet(1, "bang", {}) +end diff --git a/examples/ldelay2-help.pd b/examples/ldelay2-help.pd new file mode 100644 index 0000000..725ddc6 --- /dev/null +++ b/examples/ldelay2-help.pd @@ -0,0 +1,22 @@ +#N canvas 256 192 450 300 10; +#X msg 259 89 500; +#X msg 272 112 1000; +#X msg 197 107 1500; +#X obj 187 76 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 187 199 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X msg 139 108 stop; +#X obj 351 265 delay 1000; +#X text 278 266 see also:; +#X obj 187 150 ldelay2 1000; +#X obj 266 199 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X text 17 17 Testing multiple clocks in one object...; +#X connect 0 0 8 1; +#X connect 1 0 8 1; +#X connect 2 0 8 0; +#X connect 3 0 8 0; +#X connect 5 0 8 0; +#X connect 8 0 4 0; +#X connect 8 1 9 0; diff --git a/examples/ldelay2.pd_lua b/examples/ldelay2.pd_lua new file mode 100644 index 0000000..d6d681f --- /dev/null +++ b/examples/ldelay2.pd_lua @@ -0,0 +1,51 @@ +-- test multiple clocks + +local LDelay = pd.Class:new():register("ldelay2") + +function LDelay:initialize(name, atoms) + if type(atoms[1]) ~= "number" or atoms[1] < 0 then + self.delay = 1000 + else + self.delay = atoms[1] + end + self.inlets = 2 + self.outlets = 2 + return true +end + +function LDelay:postinitialize() + self.clock1 = pd.Clock:new():register(self, "trigger1") + self.clock2 = pd.Clock:new():register(self, "trigger2") +end + +function LDelay:finalize() + self.clock1:destruct() + self.clock2:destruct() +end + +function LDelay:in_2_float(f) + self.delay = math.max(0, f) +end + +function LDelay:in_1_float(f) + self:in_2_float(f) + self:in_1_bang() +end + +function LDelay:in_1_bang() + self.clock1:delay(self.delay) + self.clock2:delay(self.delay * 2) +end + +function LDelay:in_1_stop() + self.clock1:unset() + self.clock2:unset() +end + +function LDelay:trigger1() + self:outlet(1, "bang", {}) +end + +function LDelay:trigger2() + self:outlet(2, "bang", {}) +end diff --git a/examples/ldemux-help.pd b/examples/ldemux-help.pd new file mode 100644 index 0000000..680cc36 --- /dev/null +++ b/examples/ldemux-help.pd @@ -0,0 +1,53 @@ +#N canvas 31 53 510 441 10; +#X floatatom 371 118 5 0 0 0 - - -; +#X floatatom 250 103 5 0 0 0 - - -; +#X msg 252 73 symbol x; +#X msg 252 49 list a b c; +#X msg 251 27 a b c; +#X floatatom 108 212 5 0 0 0 - - -; +#X floatatom 64 258 5 0 0 0 - - -; +#X floatatom 64 214 5 0 0 0 - - -; +#X floatatom 50 92 5 0 0 0 - - -; +#X floatatom 57 137 5 0 0 0 - - -; +#X floatatom 102 141 5 0 0 0 - - -; +#X floatatom 148 139 5 0 0 0 - - -; +#X obj 64 235 ldemux zwei; +#X floatatom 131 262 5 0 0 0 - - -; +#X floatatom 109 309 5 0 0 0 - - -; +#X floatatom 65 355 5 0 0 0 - - -; +#X floatatom 65 308 5 0 0 0 - - -; +#X floatatom 106 356 5 0 0 0 - - -; +#X obj 65 332 ldemux; +#X obj 250 141 ldemux 6 ---------; +#X text 20 72 2nd arg gives default outlet; +#X obj 51 112 ldemux 3 2; +#X obj 219 286 print lu1; +#X obj 286 282 print lu2; +#X obj 307 252 print lu3; +#X obj 324 225 print lu4; +#X obj 329 194 print lu5; +#X obj 366 175 print lu6; +#X text 169 346 outlet number starts from 1 like Lua!; +#X connect 0 0 19 1; +#X connect 1 0 19 0; +#X connect 2 0 19 0; +#X connect 3 0 19 0; +#X connect 4 0 19 0; +#X connect 5 0 12 1; +#X connect 7 0 12 0; +#X connect 8 0 21 0; +#X connect 12 0 6 0; +#X connect 12 1 13 0; +#X connect 14 0 18 1; +#X connect 16 0 18 0; +#X connect 18 0 15 0; +#X connect 18 1 17 0; +#X connect 19 0 22 0; +#X connect 19 1 23 0; +#X connect 19 2 24 0; +#X connect 19 3 25 0; +#X connect 19 4 26 0; +#X connect 19 5 27 0; +#X connect 21 0 9 0; +#X connect 21 1 10 0; +#X connect 21 2 11 0; diff --git a/examples/ldemux.pd_lua b/examples/ldemux.pd_lua new file mode 100644 index 0000000..2c4301f --- /dev/null +++ b/examples/ldemux.pd_lua @@ -0,0 +1,28 @@ +-- contributed by Frank Barknecht + +local LDemux = pd.Class:new():register("ldemux") + +function LDemux:initialize(name, atoms) + local n = atoms[1] or 2 -- default to 2 outlets. + if type(n) ~= "number" or n < 2 then + pd.post("ldemux: wrong outlet-count argument, using 2 outlets instead") + n = 2 + end + self.outlets = n + self.inlets = 2 + self.to = 1 + -- second arg, if a number, selects default outlet + if type(atoms[2]) == "number" then + self:in_2_float(atoms[2]) + end + return true +end + +function LDemux:in_2_float(f) + -- clip selection between left- and rightmost outlet + self.to = math.max(1, math.min(self.outlets, f)) +end + +function LDemux:in_1(s, m) + self:outlet(self.to, s, m) +end diff --git a/examples/lexpr-help.pd b/examples/lexpr-help.pd new file mode 100644 index 0000000..3f2978a --- /dev/null +++ b/examples/lexpr-help.pd @@ -0,0 +1,152 @@ +#N canvas 0 22 936 656 10; +#X floatatom 110 102 5 0 0 0 - - -; +#X floatatom 110 56 5 0 0 0 - - -; +#X floatatom 202 56 5 0 0 0 - - -; +#X floatatom 294 57 5 0 0 0 - - -; +#X obj 72 53 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X floatatom 26 232 5 0 0 0 - - -; +#X floatatom 205 236 5 0 0 0 - - -; +#X floatatom 385 235 5 0 0 0 - - -; +#X floatatom 26 278 5 0 0 0 - - -; +#X obj 26 255 lexpr phi rho gamma -> 100*cos(phi) + gamma*sin(rho) +; +#X msg 115 205 lexpr foo bar baz -> foo * bar / baz; +#X msg 115 181 lexpr x y z -> x+y+z; +#X text 12 11 First come variable names \, then -> surrounded by spaces +\, then an expression (in Lua syntax).; +#X obj 110 77 lexpr a b c -> min(a \, b \, c); +#X text 14 122 Messages can change the expression (provided inlet count +stays the same). This resets the state of all variables. Note: weird +tricks are needed for commas in messages :(; +#X obj 86 441 lexpr a b c -> a + b + c; +#X msg 190 412 hot \$1; +#X obj 190 392 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 270 412 hot \$1; +#X obj 270 392 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 110 412 hot \$1; +#X obj 110 392 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X floatatom 86 367 5 0 0 0 - - -; +#X floatatom 167 367 5 0 0 0 - - -; +#X floatatom 249 367 5 0 0 0 - - -; +#X obj 68 387 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X floatatom 86 469 5 0 0 0 - - -; +#X text 13 305 Eaxh inlet supports a "hot <float>" method \, that makes +the inlet hot or not. By default only the first inlet is hot. Sending +a bang to the first inlet always performs the calculation \, no matter +if that inlet is set to be cold.; +#X text 22 491 Multiple outlets are supported \, with multiple expressions +separated by commas.; +#X obj 82 546 lexpr a b -> a + b \, a - b; +#X floatatom 82 528 5 0 0 0 - - -; +#X floatatom 252 528 5 0 0 0 - - -; +#X floatatom 82 568 5 0 0 0 - - -; +#X floatatom 252 568 5 0 0 0 - - -; +#N canvas 2 22 450 300 \$0-weird-tricks-to-get-commas-in-messages 0 +; +#X obj 58 231 lexpr a b -> max(a \, b) \, min(a \, b); +#X floatatom 32 194 5 0 0 0 - - -; +#X floatatom 33 273 5 0 0 0 - - -; +#X floatatom 284 198 5 0 0 0 - - -; +#X floatatom 284 275 5 0 0 0 - - -; +#X obj 123 148 makefilename %c; +#X msg 123 128 44; +#X obj 123 68 makefilename %c; +#X msg 123 48 44; +#X msg 123 88 lexpr a b -> min(a \$1 b) \$1 max(a \$1 b); +#X msg 123 168 lexpr a b -> max(a \$1 b) \$1 min(a \$1 b); +#X text 28 18 This is really ugly \, but it seems to work...; +#X connect 0 0 2 0; +#X connect 0 1 4 0; +#X connect 1 0 0 0; +#X connect 3 0 0 1; +#X connect 5 0 10 0; +#X connect 6 0 5 0; +#X connect 7 0 9 0; +#X connect 8 0 7 0; +#X connect 9 0 0 0; +#X connect 10 0 0 0; +#X restore 27 606 pd \$0-weird-tricks-to-get-commas-in-messages; +#X text 502 5 New in pdlua-0.5: interaction with Pd [value] objects. +; +#X obj 608 70 value \$0-foo; +#X floatatom 608 45 5 0 0 0 - - -; +#X obj 528 97 lexpr x y -> val("\$0-foo") * x / y; +#X floatatom 528 72 5 0 0 0 - - -; +#X floatatom 763 72 5 0 0 0 - - -; +#X floatatom 528 129 5 0 0 0 - - -; +#X text 504 161 Trying to access a [value] that doesn't exist returns +0:; +#X obj 539 224 lexpr z -> z + val("\$0-bar"); +#X floatatom 539 202 5 0 0 0 - - -; +#X floatatom 539 253 5 0 0 0 - - -; +#X obj 508 384 lexpr scale name -> scale * val(name); +#X obj 782 319 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 672 343 symbol \$0-foo; +#X obj 672 319 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 782 343 symbol \$0-baz; +#X floatatom 792 377 5 0 0 0 - - -; +#X obj 792 399 value \$0-baz; +#X floatatom 508 354 5 0 0 0 - - -; +#X floatatom 508 415 5 0 0 0 - - -; +#X text 505 292 New in lexpr for pdlua-0.5: inputs can be symbols. +; +#X obj 511 531 lexpr left right -> "" .. left .. right; +#X symbolatom 511 497 10 0 0 0 - - -; +#X symbolatom 781 497 10 0 0 0 - - -; +#X symbolatom 511 570 0 0 0 0 - - -; +#X text 501 471; +#X text 494 446 New in lexpr for pdlua-0.5: outputs can be symbols +too. The inital "" is to make sure that we have a string.; +#X floatatom 737 496 5 0 0 0 - - -; +#X floatatom 594 497 5 0 0 0 - - -; +#X connect 1 0 13 0; +#X connect 2 0 13 1; +#X connect 3 0 13 2; +#X connect 4 0 13 0; +#X connect 5 0 9 0; +#X connect 6 0 9 1; +#X connect 7 0 9 2; +#X connect 9 0 8 0; +#X connect 10 0 9 0; +#X connect 11 0 9 0; +#X connect 13 0 0 0; +#X connect 15 0 26 0; +#X connect 16 0 15 1; +#X connect 17 0 16 0; +#X connect 18 0 15 2; +#X connect 19 0 18 0; +#X connect 20 0 15 0; +#X connect 21 0 20 0; +#X connect 22 0 15 0; +#X connect 23 0 15 1; +#X connect 24 0 15 2; +#X connect 25 0 15 0; +#X connect 29 0 32 0; +#X connect 29 1 33 0; +#X connect 30 0 29 0; +#X connect 31 0 29 1; +#X connect 37 0 36 0; +#X connect 38 0 41 0; +#X connect 39 0 38 0; +#X connect 40 0 38 1; +#X connect 43 0 45 0; +#X connect 44 0 43 0; +#X connect 46 0 54 0; +#X connect 47 0 50 0; +#X connect 48 0 46 1; +#X connect 49 0 48 0; +#X connect 50 0 46 1; +#X connect 51 0 52 0; +#X connect 53 0 46 0; +#X connect 56 0 59 0; +#X connect 57 0 56 0; +#X connect 58 0 56 1; +#X connect 62 0 56 1; +#X connect 63 0 56 0; diff --git a/examples/lexpr.pd_lua b/examples/lexpr.pd_lua new file mode 100644 index 0000000..487c87e --- /dev/null +++ b/examples/lexpr.pd_lua @@ -0,0 +1,145 @@ +local lexpr = pd.Class:new():register("lexpr") + +local function sandbox(e, f) -- only supports nullary f() with one return + local g = getfenv(f) + setfenv(f, e) + local r = f() + setfenv(f, g) + return r +end + +local lexpr_globals = { + abs = math.abs, + acos = math.acos, + asin = math.asin, + atan = math.atan, + atan2 = math.atan2, + ceil = math.ceil, + cos = math.cos, + cosh = math.cosh, + deg = math.deg, + exp = math.exp, + floor = math.floor, + fmod = math.fmod, + log = math.log, + log10 = math.log10, + max = math.max, + min = math.min, + int = function (x) i,f = math.modf(x) ; return i end, + wrap = function (x) i,f = math.modf(x) ; return f end, + pi = math.pi, + pow = math.pow, + rad = math.rad, + sin = math.sin, + sinh = math.sinh, + sqrt = math.sqrt, + tan = math.tan, + tanh = math.tanh, + val = function (s) return (pd.getvalue(s) or 0) end, + choose = function (b,t,f) if b then return t else return f end end +} + +function lexpr:readexpr(atoms) + local vname = { } + local context = { } + local expr + local i + local k + local v + local j = 1 + local inlets + local f + local phase = "vars" + for k,v in pairs(lexpr_globals) do + context[k] = v + end + for i,v in ipairs(atoms) do + if phase == "vars" then -- create variables + if v == "->" then + inlets = i - 1 + phase = "expr" + expr = "" + else + if type(v) == "string" then + vname[j] = v + context[v] = 0 + j = j + 1 + else + self:error("lexpr: variable names must be symbols") + return -1 + end + end + else if phase == "expr" then -- build string + expr = expr .. " " .. v + else + self:error("lexpr: internal error parsing expression") + return -1 + end end + end + f = assert(loadstring("return {" .. expr .. " }")) + local outlets = #(sandbox(context, f)) + return inlets, vname, context, f, outlets +end + +function lexpr:initialize(sel, atoms) + self.vname = { } + self.context = { } + self.hot = { } + self.f = function () return 0 end + function self:in_1_bang() + local r = sandbox(self.context, self.f) + local i + for i = self.outlets,1,-1 do + if type(r[i]) == "number" then + self:outlet(i, "float", { r[i] }) + else if type(r[i]) == "string" then + self:outlet(i, "symbol", { r[i] }) + else + self:error("calculated a " .. type(r[i]) .. " but expected a number or a string") + end end + end + end + function self:in_n_float(i, f) + self.context[self.vname[i]] = f + if self.hot[i] then self:in_1_bang() end + end + function self:in_n_symbol(i, s) + self.context[self.vname[i]] = s + if self.hot[i] then self:in_1_bang() end + end + function self:in_n_hot(i, atoms) + if type(atoms[1]) == "number" then + self.hot[i] = atoms[1] ~= 0 + else + self:error("hot method expects a float") + end + end + function self:in_1_lexpr(atoms) + local inlets + local vname + local context + local f + local outlets + inlets, vname, context, f, outlets = self:readexpr(atoms) + if (inlets == self.inlets) and (outlets == self.outlets) then + self.vname = vname + self.context = context + self.f = f + else + self:error("new expression has different inlet/outlet count") + end + end + self.inlets, self.vname, self.context, self.f, self.outlets = self:readexpr(atoms) + if self.inlets < 1 then + pd.post("lexpr: error: would have no inlets") + return false + end + if self.outlets < 1 then + pd.post("lexpr: error: would have no outlets") + return false + end + for i = 1,self.inlets,1 do + self.hot[i] = i == 1 + end + return true +end diff --git a/examples/list-pak-help.pd b/examples/list-pak-help.pd new file mode 100644 index 0000000..e3a02ab --- /dev/null +++ b/examples/list-pak-help.pd @@ -0,0 +1,34 @@ +#N canvas 188 72 556 526 10; +#X obj 106 244 list-pak; +#X obj 106 277 print list-pak; +#X floatatom 106 202 5 0 0 0 - - -; +#X msg 147 197 bang; +#X msg 149 219 symbol x; +#X obj 306 301 print list-pak; +#X floatatom 306 176 5 0 0 0 - - -; +#X msg 306 129 bang; +#X msg 306 152 symbol x; +#X floatatom 338 239 5 0 0 0 - - -; +#X floatatom 383 243 5 0 0 0 - - -; +#X obj 306 268 list-pak 3; +#X msg 380 221 any; +#X msg 338 217 bang; +#X obj 103 385 list-pak -3; +#X text 101 367 Don't do this:; +#X text 76 32 list-pak; +#X text 84 58 Like [pack] for any kind of type. Argument specifies +number of inlets. All inlets are hot as in Max' [pak].; +#X text 83 94 Stored elements default to 0; +#X text 101 467 Frank Barknecht \, 2007; +#X connect 0 0 1 0; +#X connect 2 0 0 0; +#X connect 3 0 0 0; +#X connect 4 0 0 0; +#X connect 6 0 11 0; +#X connect 7 0 11 0; +#X connect 8 0 11 0; +#X connect 9 0 11 1; +#X connect 10 0 11 2; +#X connect 11 0 5 0; +#X connect 12 0 11 2; +#X connect 13 0 11 1; diff --git a/examples/list-pak.pd_lua b/examples/list-pak.pd_lua new file mode 100644 index 0000000..1651582 --- /dev/null +++ b/examples/list-pak.pd_lua @@ -0,0 +1,56 @@ +--[[ + +list-pak + + Like [pack] for any kind of type. Argument specifies number of inlets. All + inlets are hot (as in Max' [pak]) + + --Written by Frank Barknecht + +--]] + +-- Some footils -- + +-- 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 ListPak = pd.Class:new():register("list-pak") + +function ListPak:initialize(name, atoms) + self.outlets = 1 + self.stored = {} + if atoms[1] == nil then + self.inlets = 1 + elseif type(atoms[1]) == "number" and atoms[1] >= 0 then + self.inlets = math.max(atoms[1], 1) + else + pd.post("list-pak: First arg must be a positive float or empty") + return false + end + for i=1,self.inlets do + table.insert(self.stored, 0) + end + return true +end + +function ListPak:in_n(i, sel, atoms) + if not contains(selectors, sel) then + -- insert selector + self.stored[i] = sel + else + if table.getn(atoms) > 0 then + self.stored[i] = atoms[1] + end + end + self:outlet(1, "list", self.stored) +end diff --git a/examples/list-unpack-help.pd b/examples/list-unpack-help.pd new file mode 100644 index 0000000..75f67cf --- /dev/null +++ b/examples/list-unpack-help.pd @@ -0,0 +1,39 @@ +#N canvas 168 175 727 437 10; +#X msg 246 129 1 2 3; +#X msg 149 129 1; +#X obj 149 221 list-unpack 3; +#X obj 149 302 print lu0; +#X obj 192 282 print lu1; +#X obj 235 262 print lu2; +#X msg 389 128 a b c; +#X msg 461 128 1 2 3 4; +#X msg 179 129 symbol x; +#X msg 295 128 list a b c; +#X text 272 103 lists; +#X text 153 106 float; +#X text 192 105 symbol; +#X text 385 105 anything; +#X text 461 105 too long; +#X obj 340 221 list-unpack; +#X obj 429 221 list-unpack 0; +#X obj 339 330 list-unpack -1; +#X text 58 53 Like [unpack] for any kind of type. Argument specifies +number of outlets. Pointers untested rsp. not supported; +#X text 42 25 [list-unpack]; +#X text 64 389 2007 Frank Barknecht; +#X obj 341 267 list-unpack 28 -----------------------------------; +#X text 337 244 Second arg is ignored and can be used for stretching +; +#X text 338 196 Default number of outlets is 1 \, even if you want +0:; +#X text 338 302 But these won't create:; +#X obj 339 353 list-unpack three; +#X connect 0 0 2 0; +#X connect 1 0 2 0; +#X connect 2 0 3 0; +#X connect 2 1 4 0; +#X connect 2 2 5 0; +#X connect 6 0 2 0; +#X connect 7 0 2 0; +#X connect 8 0 2 0; +#X connect 9 0 2 0; diff --git a/examples/list-unpack-test-gem.pd b/examples/list-unpack-test-gem.pd new file mode 100644 index 0000000..0cf8bf6 --- /dev/null +++ b/examples/list-unpack-test-gem.pd @@ -0,0 +1,18 @@ +#N canvas 0 0 450 300 10; +#X obj 22 42 gemhead; +#X obj 222 124 gemwin; +#X msg 266 46 create; +#X msg 274 76 destroy; +#X obj 222 46 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X obj 22 70 list-unpack 3; +#X obj 108 102 print gem3; +#X obj 65 123 print gem2; +#X obj 22 143 print gem1; +#X connect 0 0 5 0; +#X connect 2 0 1 0; +#X connect 3 0 1 0; +#X connect 4 0 1 0; +#X connect 5 0 8 0; +#X connect 5 1 7 0; +#X connect 5 2 6 0; 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 diff --git a/examples/lpipe-help.pd b/examples/lpipe-help.pd new file mode 100644 index 0000000..b3cb51c --- /dev/null +++ b/examples/lpipe-help.pd @@ -0,0 +1,17 @@ +#N canvas 343 338 450 300 10; +#X obj 114 179 lpipe; +#X obj 114 214 print; +#X obj 114 127 unpack 0 0; +#X msg 135 95 0 0 \, 1000 1000 \, 2000 2000 \, 500 500; +#X msg 114 65 4000 4000; +#X msg 56 65 250 250; +#X msg 50 128 a b c d; +#X floatatom 179 183 5 0 0 0 - - -; +#X connect 0 0 1 0; +#X connect 2 0 0 0; +#X connect 2 1 0 1; +#X connect 2 1 7 0; +#X connect 3 0 2 0; +#X connect 4 0 2 0; +#X connect 5 0 2 0; +#X connect 6 0 0 0; diff --git a/examples/lpipe.pd_lua b/examples/lpipe.pd_lua new file mode 100644 index 0000000..3399476 --- /dev/null +++ b/examples/lpipe.pd_lua @@ -0,0 +1,30 @@ +-- 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 diff --git a/examples/lreceive-help.pd b/examples/lreceive-help.pd new file mode 100644 index 0000000..e2e1eca --- /dev/null +++ b/examples/lreceive-help.pd @@ -0,0 +1,22 @@ +#N canvas 0 0 540 307 10; +#X obj 27 41 lreceive lreceive- 25 10; +#X obj 27 76 print lreceive-LEFT; +#X obj 190 76 print lreceive-RIGHT; +#X obj 26 252 send lreceive-25; +#X msg 26 196 foo bar 1 2 3; +#X msg 146 196 123 bar 1 2 3; +#X obj 146 252 send lreceive-27; +#X obj 272 252 send; +#X floatatom 295 204 5 24 35 2 n - -; +#X obj 295 226 makefilename lreceive-%d; +#X symbolatom 272 171 10 0 0 0 - - -; +#X obj 272 143 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X connect 0 0 1 0; +#X connect 0 1 2 0; +#X connect 4 0 3 0; +#X connect 5 0 6 0; +#X connect 8 0 9 0; +#X connect 9 0 7 1; +#X connect 10 0 7 0; +#X connect 11 0 10 0; diff --git a/examples/lreceive.pd_lua b/examples/lreceive.pd_lua new file mode 100644 index 0000000..e75beda --- /dev/null +++ b/examples/lreceive.pd_lua @@ -0,0 +1,44 @@ +-- test receive support + +local LReceive = pd.Class:new():register("lreceive") + +function LReceive:initialize(name, atoms) + if type(atoms[1]) ~= "string" then + pd.post("lreceive needs a prefix for the receive names!") + return false + else + self.prefix = atoms[1] + end + if type(atoms[2]) ~= "number" or atoms[2] < 1 then + self.start = 1 + else + self.start = math.floor(atoms[2]) + end + if type(atoms[3]) ~= "number" or atoms[3] < 1 then + self.count = 1 + else + self.count = math.floor(atoms[3]) + end + self.receives = { } + self.inlets = 0 + self.outlets = 2 + return true +end + +function LReceive:postinitialize() + local i = 0 + while (i < self.count) do + local n = self.start + i + self.receives[i+1] = pd.Receive:new():register(self, self.prefix .. n, "receive_" .. n) + self["receive_" .. n] = function (self, sel, atoms) + self:outlet(2, "float", { n }) + self:outlet(1, sel, atoms) + end + i = i + 1 + end +end + +function LReceive:finalize() + for _,r in ipairs(self.receives) do r:destruct() end +end + diff --git a/examples/lsend-help.pd b/examples/lsend-help.pd new file mode 100644 index 0000000..51b5856 --- /dev/null +++ b/examples/lsend-help.pd @@ -0,0 +1,19 @@ +#N canvas 0 0 450 300 10; +#X obj 55 142 lsend lsend-x; +#X obj 49 188 r lsend-x; +#X obj 158 189 r lsend-y; +#X msg 141 87 symbol lsend-x; +#X msg 149 107 symbol lsend-y; +#X floatatom 69 112 5 0 0 0 - - -; +#X msg 32 57 foo bar 1 2 4 8; +#X obj 48 224 print lsend-x; +#X obj 158 224 print lsend-y; +#X symbolatom 55 87 10 0 0 0 - - -; +#X text 21 23 Test send support...; +#X connect 1 0 7 0; +#X connect 2 0 8 0; +#X connect 3 0 0 1; +#X connect 4 0 0 1; +#X connect 5 0 0 0; +#X connect 6 0 0 0; +#X connect 9 0 0 0; diff --git a/examples/lsend.pd_lua b/examples/lsend.pd_lua new file mode 100644 index 0000000..357459c --- /dev/null +++ b/examples/lsend.pd_lua @@ -0,0 +1,23 @@ +-- test send support + +local LSend = pd.Class:new():register("lsend") + +function LSend:initialize(name, atoms) + if type(atoms[1]) ~= "string" then + pd.post("lsend needs a symbol") + return false + else + self.sendto = atoms[1] + end + self.inlets = 2 + self.outlets = 0 + return true +end + +function LSend:in_2_symbol(s) + self.sendto = s +end + +function LSend:in_1(sel, atoms) + pd.send(self.sendto, sel, atoms) +end diff --git a/examples/ltabdump-help.pd b/examples/ltabdump-help.pd new file mode 100644 index 0000000..eed5d05 --- /dev/null +++ b/examples/ltabdump-help.pd @@ -0,0 +1,72 @@ +#N canvas 0 0 450 300 10; +#X obj 102 201 ltabdump \$0-tab-A; +#X obj 102 241 print ltabdump; +#X floatatom 218 242 5 0 0 0 - - -; +#X obj 11 34 table \$0-tab-B 8; +#X obj 11 14 table \$0-tab-A 4; +#N canvas 18 116 450 300 \$0-fill-tables 0; +#X obj 27 37 inlet; +#X obj 27 61 t b b; +#X obj 220 134 until; +#X obj 220 177 random 16; +#X obj 220 155 t b b; +#X obj 219 205 tabwrite \$0-tab-B; +#X obj 311 166 f 0; +#X obj 350 151 + 1; +#X obj 350 174 mod 8; +#X msg 223 112 8; +#X obj 30 134 until; +#X obj 30 177 random 16; +#X obj 30 155 t b b; +#X obj 121 166 f 0; +#X obj 160 151 + 1; +#X msg 33 112 4; +#X obj 160 174 mod 4; +#X obj 29 205 tabwrite \$0-tab-A; +#X connect 0 0 1 0; +#X connect 1 0 15 0; +#X connect 1 1 9 0; +#X connect 2 0 4 0; +#X connect 3 0 5 0; +#X connect 4 0 3 0; +#X connect 4 1 6 0; +#X connect 6 0 7 0; +#X connect 6 0 5 1; +#X connect 7 0 8 0; +#X connect 8 0 6 1; +#X connect 9 0 2 0; +#X connect 10 0 12 0; +#X connect 11 0 17 0; +#X connect 12 0 11 0; +#X connect 12 1 13 0; +#X connect 13 0 14 0; +#X connect 13 0 17 1; +#X connect 14 0 16 0; +#X connect 15 0 10 0; +#X connect 16 0 13 1; +#X restore 171 39 pd \$0-fill-tables; +#X obj 171 12 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 193 14 loadbang; +#X obj 102 82 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 132 99 symbol \$0-tab-A; +#X obj 132 179 symbol \$0-tab-C; +#X obj 132 161 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 132 121 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 132 81 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 132 139 symbol \$0-tab-B; +#X connect 0 0 1 0; +#X connect 0 1 2 0; +#X connect 6 0 5 0; +#X connect 7 0 5 0; +#X connect 8 0 0 0; +#X connect 9 0 0 0; +#X connect 10 0 0 0; +#X connect 11 0 10 0; +#X connect 12 0 14 0; +#X connect 13 0 9 0; +#X connect 14 0 0 0; diff --git a/examples/ltabdump.pd_lua b/examples/ltabdump.pd_lua new file mode 100644 index 0000000..3c44ef7 --- /dev/null +++ b/examples/ltabdump.pd_lua @@ -0,0 +1,36 @@ +local LTabDump = pd.Class:new():register("ltabdump") + +function LTabDump:initialize(sel, atoms) + self.inlets = 1 + self.outlets = 2 + if type(atoms[1]) == "string" then + self.name = atoms[1] + else + self.name = nil + end + return true +end + +-- output table length and data +function LTabDump:in_1_bang() + -- need to sync each time before accessing if ... + -- ... control-flow has gone outside of our scope + local t = pd.Table:new():sync(self.name) + if t ~= nil then + local l = t:length() + local a = { } + local i + for i = 1,l do + a[i] = t:get(i-1) + end + -- copied above before outlet() to avoid race condition + self:outlet(2, "float", { l }) + self:outlet(1, "list", a) + end +end + +-- choose a table name, then output +function LTabDump:in_1_symbol(s) + self.name = s + self:in_1_bang() +end diff --git a/examples/ltabfill-help.pd b/examples/ltabfill-help.pd new file mode 100644 index 0000000..375b93d --- /dev/null +++ b/examples/ltabfill-help.pd @@ -0,0 +1,27 @@ +#N canvas 0 0 453 328 10; +#X obj 82 239 ltabfill \$0-table w -> sin(2*pi*w*x); +#X obj 33 192 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X floatatom 331 196 5 0 0 2 w - -; +#X obj 82 159 symbol \$0-table-1; +#X obj 94 205 symbol \$0-table-2; +#X obj 94 184 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 82 136 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 254 134 table \$0-table-1 333; +#X obj 254 157 table \$0-table-2 555; +#X text 15 17 [ltabfill] is a clone of [lexpr] that writes a function +to a table. The first argument and inlet is the name of the table \, +then the usual [lexpr] stuff follows. There is an additional implicit +parameter to the expression \, named 'x' \, which ranges from [0..1) +no matter the size of the array. Note: you might have to close/reopen +the table displays for them to refresh correctly.; +#X text 14 280 See also:; +#X obj 53 299 lexpr x -> x; +#X connect 1 0 0 0; +#X connect 2 0 0 1; +#X connect 3 0 0 0; +#X connect 4 0 0 0; +#X connect 5 0 4 0; +#X connect 6 0 3 0; diff --git a/examples/ltabfill.pd_lua b/examples/ltabfill.pd_lua new file mode 100644 index 0000000..56ef81b --- /dev/null +++ b/examples/ltabfill.pd_lua @@ -0,0 +1,156 @@ +local ltabfill = pd.Class:new():register("ltabfill") + +local function sandbox(e, f, x) -- only supports unary f() with one return + local g = getfenv(f) + setfenv(f, e) + local r = f(x) + setfenv(f, g) + return r +end + +local ltabfill_globals = { + abs = math.abs, + acos = math.acos, + asin = math.asin, + atan = math.atan, + atan2 = math.atan2, + ceil = math.ceil, + cos = math.cos, + cosh = math.cosh, + deg = math.deg, + exp = math.exp, + floor = math.floor, + fmod = math.fmod, + log = math.log, + log10 = math.log10, + max = math.max, + min = math.min, + int = function (x) i,f = math.modf(x) ; return i end, + wrap = function (x) i,f = math.modf(x) ; return f end, + pi = math.pi, + pow = math.pow, + rad = math.rad, + sin = math.sin, + sinh = math.sinh, + sqrt = math.sqrt, + tan = math.tan, + tanh = math.tanh, + val = function (s) return (pd.getvalue(s) or 0) end +} + +function ltabfill:readexpr(atoms) + local vname = { } + local context = { } + local expr + local i + local k + local v + local j = 2 + local inlets + local f + local phase = "table" + for k,v in pairs(ltabfill_globals) do + context[k] = v + end + for i,v in ipairs(atoms) do + if phase == "table" then + if type(v) == "string" then + self.tabname = v + phase = "vars" + else + self:error("ltabfill: table name must be a symbol") + return -1 + end + else if phase == "vars" then -- create variables + if v == "->" then + inlets = i - 1 + phase = "expr" + expr = "" + else + if type(v) == "string" then + vname[j] = v + context[v] = 0 + j = j + 1 + else + self:error("ltabfill: variable names must be symbols") + return -1 + end + end + else if phase == "expr" then -- build string + expr = expr .. " " .. v + else + self:error("ltabfill: internal error parsing expression") + return -1 + end end end + end + f = assert(loadstring("return function(x) return " .. expr .. " end"))() + return inlets, vname, context, f, 0 +end + +function ltabfill:initialize(sel, atoms) + self.tabname = nil + self.vname = { } + self.context = { } + self.hot = { } + self.f = function (x) return 0 end + function self:in_1_bang() + if self.tabname ~= nil then + local t = pd.Table:new():sync(self.tabname) + if t ~= nil then + local i + local l = t:length() + for i = 1,l do + local y = sandbox(self.context, self.f, (i-1)/l) + t:set(i-1, y) + end + t:redraw() + end + end + end + function self:in_1_symbol(s) + self.tabname = s + if self.hot[1] then self:in_1_bang() end + end + function self:in_1_float(f) + self:error("table name expected, got a float") + end + function self:in_n_float(i, f) + self.context[self.vname[i]] = f + if self.hot[i] then self:in_1_bang() end + end + function self:in_n_symbol(i, s) + self.context[self.vname[i]] = s + if self.hot[i] then self:in_1_bang() end + end + function self:in_n_hot(i, atoms) + if type(atoms[1]) == "number" then + self.hot[i] = atoms[1] ~= 0 + else + self:error("hot method expects a float") + end + end + function self:in_1_ltabfill(atoms) + local inlets + local vname + local context + local f + local outlets + inlets, vname, context, f, outlets = self:readexpr(atoms) + if (inlets == self.inlets) and (outlets == self.outlets) then + self.vname = vname + self.context = context + self.f = f + else + self:error("new expression has different inlet/outlet count") + end + end + self.inlets, self.vname, self.context, self.f, self.outlets = self:readexpr(atoms) + if self.inlets < 1 then + pd.post("ltabfill: error: would have no inlets") + return false + end + for i = 1,self.inlets,1 do + self.hot[i] = i == 1 + end + return true +end diff --git a/examples/luametro-help.pd b/examples/luametro-help.pd new file mode 100644 index 0000000..48eda4b --- /dev/null +++ b/examples/luametro-help.pd @@ -0,0 +1,59 @@ +#N canvas 0 0 636 570 10; +#X obj 144 423 t b b; +#X obj 144 449 timer; +#X floatatom 144 474 5 0 0 0 - - -; +#X obj 144 319 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 95 311 stop; +#X msg 96 288 bang; +#X obj 169 395 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X floatatom 293 335 5 0 0 0 - - -; +#X msg 293 312 1000; +#X msg 332 311 500; +#X msg 367 420 1000 a 2000; +#X msg 365 399 list error bug; +#X msg 366 334 750 250 500 250; +#X obj 144 498 print; +#X text 369 377 Only floats allowed in period list; +#X text 43 14 lmetro; +#X msg 96 333 start; +#X text 294 290 lists or floats set new periods; +#X obj 307 457 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X msg 174 320 mode alea; +#X msg 173 293 mode junk; +#X obj 144 360 luametro 500 125 500 250; +#X msg 176 339 mode 2; +#X text 89 34 Like [metro] in Pd \, but allows more than one period. +The periods will be evaluated in sequence from left to right as default. +Min. period length is 0.01 msec.; +#X text 86 90 With "mode" you can set the mode the metro uses to select +delay periods. Four modes are available:; +#X text 115 128 1: alea - select randomly; +#X text 114 148 2: series - select randomly without repetitions; +#X text 113 167 3: sequence - select in sequence from left to right +(default mode); +#X text 113 197 4: rota - go left to right \, then turn around and +select right to left.; +#X text 94 244 "mode 2" or "mode series" work both.; +#X connect 0 0 1 0; +#X connect 0 1 1 1; +#X connect 1 0 2 0; +#X connect 2 0 13 0; +#X connect 3 0 21 0; +#X connect 4 0 21 0; +#X connect 5 0 21 0; +#X connect 7 0 21 1; +#X connect 8 0 7 0; +#X connect 9 0 7 0; +#X connect 10 0 21 1; +#X connect 11 0 21 1; +#X connect 12 0 21 1; +#X connect 16 0 21 0; +#X connect 19 0 21 0; +#X connect 20 0 21 0; +#X connect 21 0 6 0; +#X connect 21 0 0 0; +#X connect 21 1 18 0; +#X connect 22 0 21 0; diff --git a/examples/luametro.pd_lua b/examples/luametro.pd_lua new file mode 100644 index 0000000..8041e6e --- /dev/null +++ b/examples/luametro.pd_lua @@ -0,0 +1,161 @@ +-- reimplementation of [metro] with a twist +-- fbar 2007 + +-- we need an urn: +local urn = {} + +function urn.new(size) + assert(size > 0, "Error: size of urn must be greater than 0") + local t = {size=size, last=size} + for i=1,size do + t[i] = i + end + return t +end + +function urn.get(u) + if u.last > 0 then + local i = math.random(u.last) + local res = u[i] + u[i] = u[u.last] + u.last = u.last - 1 + return res + else + return nil + end +end + +function urn.reset(u) + u.last = u.size + for i=1,u.size do + u[i] = i + end +end + +local modes = {"alea", "series", "sequence", "rota"} + +local function contains(t,x) + for k,v in pairs(t) do + if v == x then return true end + end + return false +end + +-- pd: + +local M = pd.Class:new():register("luametro") + +function M:initialize(name, atoms) + self.periods = {} + self.i = 1 + self.mode = "sequence" + self.direction = 1 + self.u = urn.new(math.max(1, #atoms)) + if not self:setperiods(atoms) then return false end + self.inlets = 2 + self.outlets = 2 + return true +end + +function M:postinitialize() + self.clock = pd.Clock:new():register(self, "tick") +end + +function M:finalize() + self.clock:destruct() +end + +function M:setperiods(periods) + self.periods = {} + for i,j in ipairs(periods) do + if type(j) == "number" then + self.periods[i] = math.max(0.01, j) + else + self:error("non-number in period list!") + return false + end + end + -- start over at front: + self.i = 1 + return true +end + +function M:in_2(sel, atoms) + if sel == "list" or sel == "float" then + self:setperiods(atoms) + end +end + +function M:in_1_float(f) + if f ~= 0 then + self:tick() + else + self.clock:unset() + end +end + +function M:in_1_bang() + self.i = 1 + self:tick() +end + +function M:in_1_start() + self.i = 1 + self:tick() +end + +function M:in_1_stop() + self.clock:unset() +end + +function M:in_1_mode(atoms) + if type(atoms[1]) == "number" then + self.mode = modes[atoms[1]] + if self.mode == "series" then + self.u = urn.new(#self.periods) + end + self.i = 1 + elseif type(atoms[1]) == "string" then + if contains(modes, atoms[1]) then + self.mode = atoms[1] + else + self:error("no such mode: " .. atoms[1]) + end + end + pd.post("Mode set to: " .. self.mode) +end + + +function M:tick() + self:outlet(1, "bang", {}) + -- pd.post("selection: " .. tostring(self.i)) + if type(self.periods[self.i]) == "number" then + self.clock:delay(self.periods[self.i]) + end + if self.mode == "sequence" then + self.i = self.i + 1 + if self.i > #self.periods then + self.i = 1 + self:outlet(2, "bang", {}) + end + elseif self.mode == "alea" then + self.i = math.random(#self.periods) + elseif self.mode == "series" then + local f = urn.get(self.u) + if not f then + urn.reset(self.u) + f = urn.get(self.u) + self:outlet(2, "bang", {}) + end + self.i = f + elseif self.mode == "rota" then + self.i = self.i + self.direction + if self.i > #self.periods or self.i < 1 then + self.direction = -self.direction + self.i = self.i + self.direction + self:outlet(2, "bang", {}) + end + else + self:error("Unknown mode") + end +end diff --git a/examples/lurn-help.pd b/examples/lurn-help.pd new file mode 100644 index 0000000..56ddd40 --- /dev/null +++ b/examples/lurn-help.pd @@ -0,0 +1,188 @@ +#N canvas 457 204 823 609 10; +#X msg 81 156 bang; +#X obj 104 275 print urne; +#X obj 125 251 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 +-1; +#X msg 155 158 seed 10; +#X msg 157 181 clear; +#X floatatom 139 231 5 0 0 0 - - -; +#X text 78 41 generates random numbers without repetition; +#X text 37 23 urne: Unique Random Generator; +#X text 352 177 bang: output next random number without repetitions. +; +#X text 351 200 clear: put all numbers back into urn.; +#X text 351 224 seed NUM: seed random number generator; +#X text 331 157 Inlet 0:; +#X text 332 258 Inlet 1:; +#X text 335 319 Outlet 0:; +#X text 361 338 random number; +#X text 337 371 Outlet 1:; +#X text 373 279 int - set range of random numbers and reset the urn. +; +#X text 333 125 Arguments: int - range of random numbers in urn (optional) +; +#X text 83 68 Clone of the Max object [urn]. Stops when all values +have been chosen and bangs right outlet. Send "clear" message to make +it start over.; +#X msg 83 371 bang; +#N canvas 0 0 450 300 display 0; +#X obj 46 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 63 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 80 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 97 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 114 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 156 135 list append 0; +#X obj 46 134 list append 1; +#N canvas 0 0 450 300 count 0; +#X obj 158 193 + 1; +#X obj 122 158 until; +#X obj 122 194 f; +#X obj 122 101 inlet; +#X msg 167 157 0; +#X obj 122 120 t a b; +#X obj 122 224 outlet; +#X connect 0 0 2 1; +#X connect 1 0 2 0; +#X connect 2 0 0 0; +#X connect 2 0 6 0; +#X connect 3 0 5 0; +#X connect 4 0 2 1; +#X connect 5 0 1 0; +#X connect 5 1 4 0; +#X restore 156 90 pd count; +#X obj 46 160 route 0 1 2 3 4 5 6 7 8 9; +#X obj 131 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 148 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 165 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 182 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 199 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 46 36 inlet; +#X obj 156 37 inlet; +#X obj 156 64 max 1; +#X connect 5 0 8 0; +#X connect 6 0 8 0; +#X connect 7 0 5 0; +#X connect 8 0 0 0; +#X connect 8 1 1 0; +#X connect 8 2 2 0; +#X connect 8 3 3 0; +#X connect 8 4 4 0; +#X connect 8 5 9 0; +#X connect 8 6 10 0; +#X connect 8 7 11 0; +#X connect 8 8 12 0; +#X connect 8 9 13 0; +#X connect 14 0 6 0; +#X connect 15 0 16 0; +#X connect 16 0 7 0; +#X coords 0 -1 1 1 180 40 1 40 170; +#X restore 83 519 pd display; +#X text 363 389 bang \, when an empty urn receives a bang into first +inlet.; +#X msg 101 395 clear \, bang; +#X obj 127 448 t b b; +#X obj 127 468 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 +-1; +#X obj 256 493 f 10; +#X floatatom 150 425 5 0 0 0 - - -; +#N canvas 0 0 450 300 display 0; +#X obj 46 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 63 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 80 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 97 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 114 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 156 135 list append 0; +#X obj 46 134 list append 1; +#N canvas 0 0 450 300 count 0; +#X obj 158 193 + 1; +#X obj 122 158 until; +#X obj 122 194 f; +#X obj 122 101 inlet; +#X msg 167 157 0; +#X obj 122 120 t a b; +#X obj 122 224 outlet; +#X connect 0 0 2 1; +#X connect 1 0 2 0; +#X connect 2 0 0 0; +#X connect 2 0 6 0; +#X connect 3 0 5 0; +#X connect 4 0 2 1; +#X connect 5 0 1 0; +#X connect 5 1 4 0; +#X restore 156 90 pd count; +#X obj 46 160 route 0 1 2 3 4 5 6 7 8 9; +#X obj 131 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 148 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 165 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 182 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 199 191 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X obj 46 36 inlet; +#X obj 156 37 inlet; +#X obj 156 66 max 1; +#X connect 5 0 8 0; +#X connect 6 0 8 0; +#X connect 7 0 5 0; +#X connect 8 0 0 0; +#X connect 8 1 1 0; +#X connect 8 2 2 0; +#X connect 8 3 3 0; +#X connect 8 4 4 0; +#X connect 8 5 9 0; +#X connect 8 6 10 0; +#X connect 8 7 11 0; +#X connect 8 8 12 0; +#X connect 8 9 13 0; +#X connect 14 0 6 0; +#X connect 15 0 16 0; +#X connect 16 0 7 0; +#X coords 0 -1 1 1 180 40 1 40 170; +#X restore 81 300 pd display; +#X obj 254 273 f 10; +#X obj 254 247 b; +#X text 365 425 Use the second outlet to make the urn refill automatically +if it gets empty as show in the second example with a combined "clear +\, bang" message.; +#X obj 100 492 print urne_auto; +#X obj 81 230 lurn 10; +#X obj 83 425 lurn 10; +#X connect 0 0 32 0; +#X connect 3 0 32 0; +#X connect 4 0 29 0; +#X connect 4 0 32 0; +#X connect 5 0 32 1; +#X connect 19 0 33 0; +#X connect 22 0 33 0; +#X connect 23 0 22 0; +#X connect 23 0 24 0; +#X connect 23 1 25 0; +#X connect 25 0 20 1; +#X connect 26 0 25 0; +#X connect 26 0 33 1; +#X connect 28 0 27 1; +#X connect 29 0 28 0; +#X connect 32 0 1 0; +#X connect 32 0 27 0; +#X connect 32 1 2 0; +#X connect 33 0 20 0; +#X connect 33 0 31 0; +#X connect 33 1 23 0; diff --git a/examples/lurn.pd_lua b/examples/lurn.pd_lua new file mode 100644 index 0000000..09412f3 --- /dev/null +++ b/examples/lurn.pd_lua @@ -0,0 +1,82 @@ +-- urn class: random selection without repetitions. +-- fbar 2007 + +-- urn interface: + +local urn = {} + +function urn.new(size) + assert(size > 0, "Error: size of urn must be greater than 0") + local t = {size=size, last=size} + for i=1,size do + t[i] = i + end + return t +end + +function urn.get(u) + if u.last > 0 then + local i = math.random(u.last) + local res = u[i] + u[i] = u[u.last] + u.last = u.last - 1 + return res + else + return nil + end +end + +function urn.reset(u) + u.last = u.size + for i=1,u.size do + u[i] = i + end +end + +-- Pd class: + +local M = pd.Class:new():register("lurn") + +function M:initialize(name, atoms) + if type(atoms[1]) == "number" and atoms[1] >= 1 then + self.u = urn.new(math.floor(math.max(atoms[1]), 1)) + else + self.u = urn.new(1) + end + self.inlets = 2 + self.outlets = 2 + return true +end + +function M:finalize() + self.u = nil +end + +function M:in_2_float(f) + if f >= 1 then + self.u = urn.new(math.floor(f)) + else + self:error("size of urn too small. needs to be 1 at least") + end +end + +function M:in_1_clear(atoms) + urn.reset(self.u) +end + +function M:in_1_seed(atoms) + if type(atoms[1]) == "number" then + math.randomseed(atoms[1]) + else + self:error("seed needs a number") + end +end + +function M:in_1_bang() + local f = urn.get(self.u) + if type(f) == "number" then + self:outlet(1, "float", {f - 1}) + else + self:outlet(2, "bang", {}) + end +end diff --git a/examples/mutatee.pd_lua b/examples/mutatee.pd_lua new file mode 100644 index 0000000..dcc6d91 --- /dev/null +++ b/examples/mutatee.pd_lua @@ -0,0 +1,10 @@ +Mutatee = pd.Class:new():register("mutatee") + +function Mutatee:initialize() + self.inlets = 1 + return true +end + +function Mutatee:in_1_bang() + pd.post("I'm happy and carefree!") +end diff --git a/examples/mutator-help.pd b/examples/mutator-help.pd new file mode 100644 index 0000000..e99859a --- /dev/null +++ b/examples/mutator-help.pd @@ -0,0 +1,13 @@ +#N canvas 0 22 450 300 10; +#X obj 91 98 mutator; +#X obj 82 150 mutatee; +#X obj 71 13 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 71 39 t b b b b b; +#X obj 171 100 print checkpoint-charlie; +#X connect 2 0 3 0; +#X connect 3 0 1 0; +#X connect 3 1 4 0; +#X connect 3 2 0 0; +#X connect 3 3 4 0; +#X connect 3 4 1 0; diff --git a/examples/mutator.pd_lua b/examples/mutator.pd_lua new file mode 100644 index 0000000..9eec5ad --- /dev/null +++ b/examples/mutator.pd_lua @@ -0,0 +1,13 @@ +Mutator = pd.Class:new():register("mutator") + +function Mutator:initialize() + self.inlets = 1 + return true +end + +function Mutator:in_1_bang() + pd.post("Radiation Alert!") + Mutatee.in_1_bang = function(self) + pd.post("Oh noes! I've been mutated!") + end +end diff --git a/examples/nop-help.pd b/examples/nop-help.pd new file mode 100644 index 0000000..a6723dc --- /dev/null +++ b/examples/nop-help.pd @@ -0,0 +1,11 @@ +#N canvas 0 22 450 300 10; +#X obj 131 138 nop; +#X obj 131 179 print nop; +#X floatatom 94 47 5 0 0 0 - - -; +#X symbolatom 131 80 10 0 0 0 - - -; +#X msg 160 103 foo foo foo foo foo; +#X text 165 139 <---- (should) do nothing!; +#X connect 0 0 1 0; +#X connect 2 0 0 0; +#X connect 3 0 0 0; +#X connect 4 0 0 0; diff --git a/examples/nop-test-gem.pd b/examples/nop-test-gem.pd new file mode 100644 index 0000000..d7f68e0 --- /dev/null +++ b/examples/nop-test-gem.pd @@ -0,0 +1,14 @@ +#N canvas 0 0 450 300 10; +#X obj 73 129 nop; +#X obj 73 76 gemhead; +#X obj 73 194 cube; +#X obj 202 194 gemwin; +#X obj 202 109 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X msg 247 111 create; +#X msg 249 150 destroy; +#X connect 0 0 2 0; +#X connect 1 0 0 0; +#X connect 4 0 3 0; +#X connect 5 0 3 0; +#X connect 6 0 3 0; diff --git a/examples/nop.pd_lua b/examples/nop.pd_lua new file mode 100644 index 0000000..ab9924a --- /dev/null +++ b/examples/nop.pd_lua @@ -0,0 +1,11 @@ +local Nop = pd.Class:new():register("nop") + +function Nop:initialize() + self.inlets = 1 + self.outlets = 1 + return true +end + +function Nop:in_1(s, m) + self:outlet(1, s, m) +end diff --git a/examples/peekbag-help.pd b/examples/peekbag-help.pd new file mode 100644 index 0000000..23a1e36 --- /dev/null +++ b/examples/peekbag-help.pd @@ -0,0 +1,40 @@ +#N canvas 229 223 555 414 10; +#X msg 96 243 60 64; +#X msg 147 243 60 0; +#X msg 191 243 62 64; +#X msg 238 243 62 0; +#X obj 96 370 print; +#X text 141 371 Output is in the printout window.; +#X msg 238 289 clear; +#X text 148 26 - COLLECTION OF NUMBERS; +#X text 32 94 The bag object takes (value \, flag) pairs. If the flag +is true (nonzero) \, the value is added to the collection \; if false +\, it's removed. The collection may have many copies of the same value. +You can output the collection (and empty it) with a "flush" message +\, or just empty it with "clear." You can use this to mimic a sustain +pedal \, for example.; +#X msg 237 266 flush; +#X text 287 243 <-- add or delete elements; +#X text 291 266 <-- output them; +#X text 293 290 <-- start over; +#X obj 96 340 peekbag; +#X obj 65 309 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 86 27 peekbag; +#X text 81 64 like; +#X obj 124 64 bag; +#X text 159 64 but with a bang for taking a peek.; +#X msg 237 309 aslist; +#X text 295 311 <-- get elements in a single list.; +#X text 33 177 Use a "bang" to take a peek at the bag's content without +clearing it \, use "aslist" to get the bag's elements in a single list. +; +#X connect 0 0 13 0; +#X connect 1 0 13 0; +#X connect 2 0 13 0; +#X connect 3 0 13 0; +#X connect 6 0 13 0; +#X connect 9 0 13 0; +#X connect 13 0 4 0; +#X connect 14 0 13 0; +#X connect 19 0 13 0; diff --git a/examples/peekbag.pd_lua b/examples/peekbag.pd_lua new file mode 100644 index 0000000..15e4791 --- /dev/null +++ b/examples/peekbag.pd_lua @@ -0,0 +1,69 @@ +-- contributed by Frank Barknecht + +local PeekBag = pd.Class:new():register("peekbag") + +function PeekBag:initialize(name, atoms) + self.inlets = 2 + self.outlets = 1 + self.bag = {} + self.add = false + return true +end + +function PeekBag:in_1_float(f) + if self.add then + table.insert(self.bag, f) + else + for i=table.getn(self.bag),1,-1 do + if self.bag[i]==f then + table.remove(self.bag, i) + break + end + end + end +end + +function PeekBag:in_1_list(l) + if type(l[2]) == "number" then + self:in_2_float(l[2]) + end + if type(l[1]) == "number" then + self:in_1_float(l[1]) + end +end + +function PeekBag:in_1_clear(l) + self.bag = {} + self.add = false +end + +function PeekBag:in_1_flush(l) + self:in_1_bang() + self:in_1_clear() +end + +function PeekBag:in_2_float(f) + if f == 0 then + self.add = false + else + self.add = true + end +end + +function PeekBag:in_1_bang() + -- print all values of array + for i,v in ipairs(self.bag) do + self:outlet(1, "float", {v}) + end +end + +function PeekBag:in_1_aslist() + -- print all values of array as list + if table.getn(self.bag) == 1 then + self:outlet(1, "float", {self.bag[1]}) + elseif table.getn(self.bag) > 1 then + self:outlet(1, "list", self.bag) + end +end + + diff --git a/examples/requirer-help.pd b/examples/requirer-help.pd new file mode 100644 index 0000000..85b3989 --- /dev/null +++ b/examples/requirer-help.pd @@ -0,0 +1,8 @@ +#N canvas 0 0 450 300 10; +#X obj 144 88 requirer complex; +#X obj 146 173 requirer sqlite3; +#X text 57 27 This should find the package next to the .pd_lua?; +#X text 55 54 Or should it look next to the containing .pd patch?; +#X text 56 142 This only "works" because of a dirty dirty hack...; +#X text 52 222 TODO: some kind of "my location" necessary for .pd_lua +scripts to access?; diff --git a/examples/requirer.pd_lua b/examples/requirer.pd_lua new file mode 100644 index 0000000..4b6c4e9 --- /dev/null +++ b/examples/requirer.pd_lua @@ -0,0 +1,15 @@ +complex = require("complex") +sqlite3 = require("luasql.sqlite3") -- evil hack, below is lame + +local R = pd.Class:new():register("requirer") + +function R:initialize(sel, atoms) + if type(atoms[1]) ~= "string" then return false end + -- require(atoms[1]) -- will this ever work? + for k,v in pairs(_G[atoms[1]]) do + pd.post(atoms[1].. "." .. tostring(k) .. " = " .. tostring(v)) + end + self.inlets = 0 + self.outlets = 0 + return true +end diff --git a/examples/revalue-help.pd b/examples/revalue-help.pd new file mode 100644 index 0000000..ada5118 --- /dev/null +++ b/examples/revalue-help.pd @@ -0,0 +1,52 @@ +#N canvas 0 0 558 379 10; +#X obj 109 166 revalue; +#X obj 155 203 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X floatatom 109 205 5 0 0 0 - - -; +#X obj 83 121 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X floatatom 109 121 5 0 0 0 - - -; +#X floatatom 110 268 5 0 0 0 - - -; +#X obj 83 267 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 110 297 value \$0-foo; +#X floatatom 110 331 5 0 0 0 - - -; +#X floatatom 240 268 5 0 0 0 - - -; +#X obj 213 267 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X floatatom 240 331 5 0 0 0 - - -; +#X obj 240 297 value \$0-bar; +#X obj 155 142 symbol \$0-foo; +#X obj 185 122 symbol \$0-bar; +#X obj 155 120 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 185 100 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 215 80 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 215 102 symbol \$0-oof; +#X obj 294 167 revalue \$0-foo; +#X obj 294 143 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X floatatom 294 199 5 0 0 0 - - -; +#X text 23 23 Interface to Pd's native [value]. Behaves like [value] +but with a settable name \, and another outlet to report if there is +no [value] with the current name.; +#X connect 0 0 2 0; +#X connect 0 1 1 0; +#X connect 3 0 0 0; +#X connect 4 0 0 0; +#X connect 5 0 7 0; +#X connect 6 0 7 0; +#X connect 7 0 8 0; +#X connect 9 0 12 0; +#X connect 10 0 12 0; +#X connect 12 0 11 0; +#X connect 13 0 0 1; +#X connect 14 0 0 1; +#X connect 15 0 13 0; +#X connect 16 0 14 0; +#X connect 17 0 18 0; +#X connect 18 0 0 1; +#X connect 19 0 21 0; +#X connect 20 0 19 0; diff --git a/examples/revalue.pd_lua b/examples/revalue.pd_lua new file mode 100644 index 0000000..e2503df --- /dev/null +++ b/examples/revalue.pd_lua @@ -0,0 +1,39 @@ +local Revalue = pd.Class:new():register("revalue") + +function Revalue:initialize(sel, atoms) + self.inlets = 2 + self.outlets = 2 + if type(atoms[1]) == "string" then + self.name = atoms[1] + else + self.name = nil + end + return true +end + +-- get a value or report if it doesn't exist +function Revalue:in_1_bang() + if self.name ~= nil then + local r = pd.getvalue(self.name) + if r ~= nil then + self:outlet(1, "float", { r }) + return + end + end + self:outlet(2, "bang", { }) +end + +-- set a value or report if it doesn't exist +function Revalue:in_1_float(f) + if self.name ~= nil then + if pd.setvalue(self.name, f) then + return + end + end + self:outlet(2, "bang", { }) +end + +-- set the [value] name +function Revalue:in_2_symbol(s) + self.name = s +end diff --git a/examples/reverb-calculator-help.pd b/examples/reverb-calculator-help.pd new file mode 100644 index 0000000..519618b --- /dev/null +++ b/examples/reverb-calculator-help.pd @@ -0,0 +1,76 @@ +#N canvas 0 0 513 360 10; +#X obj 117 203 reverb-calculator 8; +#X obj 13 229 reverb; +#X floatatom 89 169 5 0 0 0 - - -; +#X obj 15 146 noise~; +#X obj 14 185 *~; +#X obj 62 115 vline~; +#X obj 18 265 dac~; +#X obj 70 263 print reverb; +#X obj 11 7 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1 +; +#X obj 8 60 t b b; +#X obj 158 24 spigot; +#X obj 197 -4 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X obj 25 166 noise~; +#X obj 25 205 *~; +#X obj 10 28 metro 1000; +#X obj 158 47 t b b b; +#X floatatom 158 146 5 0 0 0 - - -; +#X obj 158 172 pack f f f; +#X obj 158 71 random 1000; +#X obj 251 71 random 1000; +#X obj 340 70 random 1000; +#X floatatom 251 146 5 0 0 0 - - -; +#X floatatom 341 148 5 0 0 0 - - -; +#X obj 157 97 expr $f1*$f1/100000+1; +#X obj 251 121 expr $f1*$f1/100000+1; +#X obj 341 101 expr $f1*$f1/100000+1; +#X msg 10 89 0.5 1 \, 0 1 1; +#X obj 84 4 f 0; +#X obj 115 4 + 1; +#X obj 84 62 sel 0; +#X obj 115 24 mod 5; +#X text 255 174 room dimensions in meters; +#X text 79 148 decay (%); +#X text 98 245 delay times in ms; +#X obj 212 22 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X connect 0 0 1 2; +#X connect 0 0 7 0; +#X connect 1 0 6 0; +#X connect 1 1 6 1; +#X connect 2 0 1 3; +#X connect 3 0 4 0; +#X connect 4 0 1 0; +#X connect 5 0 4 1; +#X connect 5 0 13 1; +#X connect 8 0 14 0; +#X connect 9 0 26 0; +#X connect 9 1 27 0; +#X connect 10 0 15 0; +#X connect 11 0 10 1; +#X connect 12 0 13 0; +#X connect 13 0 1 1; +#X connect 14 0 9 0; +#X connect 15 0 18 0; +#X connect 15 1 19 0; +#X connect 15 2 20 0; +#X connect 16 0 17 0; +#X connect 17 0 0 0; +#X connect 18 0 23 0; +#X connect 19 0 24 0; +#X connect 20 0 25 0; +#X connect 21 0 17 1; +#X connect 22 0 17 2; +#X connect 23 0 16 0; +#X connect 24 0 21 0; +#X connect 25 0 22 0; +#X connect 26 0 5 0; +#X connect 27 0 28 0; +#X connect 27 0 29 0; +#X connect 28 0 30 0; +#X connect 29 0 10 0; +#X connect 30 0 27 1; +#X connect 34 0 15 0; diff --git a/examples/reverb-calculator.pd_lua b/examples/reverb-calculator.pd_lua new file mode 100644 index 0000000..6871233 --- /dev/null +++ b/examples/reverb-calculator.pd_lua @@ -0,0 +1,52 @@ +local R = pd.Class:new():register("reverb-calculator") + +function R:initialize(sel, atoms) + if type(atoms[1]) ~= "number" then + return false + end + self.count = math.max(atoms[1], 1) + self.inlets = 1 + self.outlets = 1 + return true +end + +local gcd = function(m, n) + while m ~= 0 do m, n = math.mod(n, m), m end + return n +end + +function R:fundamental(nx, ny, nz) + if (nx == 0 and ny == 0 and nz == 0) then return false end + if (nx == 0 and ny == 0 and nz == 1) then return true end + if (nx == 0 and ny == 1 and nz == 0) then return true end + if (nx == 1 and ny == 0 and nz == 0) then return true end + return gcd(gcd(nx, ny), nz) == 1 +end + +function R:delay(lx, ly, lz, nx, ny, nz) + return 1000 / ((340/2) * math.sqrt((nx*nx)/(lx*lx)+(ny*ny)/(ly*ly)+(nz*nz)/(lz*lz))) +end + +function R:in_1_list(atoms) + local lx = atoms[1] + local ly = atoms[2] + local lz = atoms[3] + local delays = { } + for nx = 0, 16 do + for ny = 0, 16 do + for nz = 0, 16 do + if self:fundamental(nx, ny, nz) then + table.insert(delays, self:delay(lx, ly, lz, nx, ny, nz)) + end + end end end + table.sort(delays, function (a, b) return a > b end) + local out = { } + local j = 1 + for i = 1, self.count do + out[i] = delays[j] + repeat + j = j + 1 + until delays[j] ~= delays[j-1] + end + self:outlet(1, "list", out) +end diff --git a/examples/reverb.pd b/examples/reverb.pd new file mode 100644 index 0000000..0451c30 --- /dev/null +++ b/examples/reverb.pd @@ -0,0 +1,149 @@ +#N canvas 0 0 690 613 10; +#X obj 388 33 inlet~; +#X obj 448 33 inlet~; +#X obj 399 215 outlet~; +#X obj 459 215 outlet~; +#X obj 13 53 delread~ \$0-A 29.4118; +#X obj 32 73 delread~ \$0-B 23.5294; +#X obj 121 94 delread~ \$0-C 18.3734; +#X obj 138 113 delread~ \$0-D 17.6471; +#X obj 205 51 delread~ \$0-E 15.1322; +#X obj 223 73 delread~ \$0-F 14.1176; +#X obj 311 96 delread~ \$0-G 12.7274; +#X obj 327 118 delread~ \$0-H 12.4706; +#X obj 204 221 +~; +#X obj 310 222 +~; +#X obj 246 221 -~; +#X obj 350 221 -~; +#X obj 203 277 +~; +#X obj 247 278 +~; +#X obj 291 279 -~; +#X obj 330 279 -~; +#X obj 14 221 +~; +#X obj 120 222 +~; +#X obj 56 221 -~; +#X obj 160 221 -~; +#X obj 13 277 +~; +#X obj 57 278 +~; +#X obj 101 279 -~; +#X obj 141 278 -~; +#X obj 272 349 -~; +#X obj 311 349 -~; +#X obj 13 347 +~; +#X obj 57 348 +~; +#X obj 101 349 +~; +#X obj 141 348 +~; +#X obj 184 347 -~; +#X obj 228 348 -~; +#X obj 587 53 clip 0 100; +#X obj 512 339 / 282.843; +#X obj 13 397 *~ 0; +#X obj 57 398 *~ 0; +#X obj 101 399 *~ 0; +#X obj 141 398 *~ 0; +#X obj 184 397 *~ 0; +#X obj 228 398 *~ 0; +#X obj 272 399 *~ 0; +#X obj 311 399 *~ 0; +#X obj 587 31 inlet; +#X obj 310 174 +~; +#X obj 341 175 +~; +#X obj 540 28 inlet; +#X obj 125 20 unpack f f f f f f f f; +#X obj 13 435 delwrite~ \$0-A 1000; +#X obj 23 455 delwrite~ \$0-B 1000; +#X obj 33 475 delwrite~ \$0-C 1000; +#X obj 43 495 delwrite~ \$0-D 1000; +#X obj 53 515 delwrite~ \$0-E 1000; +#X obj 63 535 delwrite~ \$0-F 1000; +#X obj 73 555 delwrite~ \$0-G 1000; +#X obj 83 575 delwrite~ \$0-H 1000; +#X connect 0 0 47 1; +#X connect 1 0 48 1; +#X connect 4 0 20 0; +#X connect 4 0 22 0; +#X connect 5 0 20 1; +#X connect 5 0 22 1; +#X connect 6 0 21 0; +#X connect 6 0 23 0; +#X connect 7 0 21 1; +#X connect 7 0 23 1; +#X connect 8 0 12 0; +#X connect 8 0 14 0; +#X connect 9 0 12 1; +#X connect 9 0 14 1; +#X connect 10 0 47 0; +#X connect 11 0 48 0; +#X connect 12 0 16 0; +#X connect 12 0 18 0; +#X connect 13 0 18 1; +#X connect 13 0 16 1; +#X connect 14 0 17 0; +#X connect 14 0 19 0; +#X connect 15 0 17 1; +#X connect 15 0 19 1; +#X connect 16 0 30 1; +#X connect 16 0 34 1; +#X connect 17 0 31 1; +#X connect 17 0 35 1; +#X connect 18 0 32 1; +#X connect 18 0 28 1; +#X connect 19 0 33 1; +#X connect 19 0 29 1; +#X connect 20 0 24 0; +#X connect 20 0 26 0; +#X connect 21 0 26 1; +#X connect 21 0 24 1; +#X connect 22 0 25 0; +#X connect 22 0 27 0; +#X connect 23 0 25 1; +#X connect 23 0 27 1; +#X connect 24 0 30 0; +#X connect 24 0 34 0; +#X connect 25 0 31 0; +#X connect 25 0 35 0; +#X connect 26 0 32 0; +#X connect 26 0 28 0; +#X connect 27 0 33 0; +#X connect 27 0 29 0; +#X connect 28 0 44 0; +#X connect 29 0 45 0; +#X connect 30 0 38 0; +#X connect 31 0 39 0; +#X connect 32 0 40 0; +#X connect 33 0 41 0; +#X connect 34 0 42 0; +#X connect 35 0 43 0; +#X connect 36 0 37 0; +#X connect 37 0 45 1; +#X connect 37 0 44 1; +#X connect 37 0 43 1; +#X connect 37 0 42 1; +#X connect 37 0 41 1; +#X connect 37 0 40 1; +#X connect 37 0 39 1; +#X connect 37 0 38 1; +#X connect 38 0 51 0; +#X connect 39 0 52 0; +#X connect 40 0 53 0; +#X connect 41 0 54 0; +#X connect 42 0 55 0; +#X connect 43 0 56 0; +#X connect 44 0 57 0; +#X connect 45 0 58 0; +#X connect 46 0 36 0; +#X connect 47 0 2 0; +#X connect 47 0 13 0; +#X connect 47 0 15 0; +#X connect 48 0 3 0; +#X connect 48 0 15 1; +#X connect 48 0 13 1; +#X connect 49 0 50 0; +#X connect 50 0 4 0; +#X connect 50 1 5 0; +#X connect 50 2 6 0; +#X connect 50 3 7 0; +#X connect 50 4 8 0; +#X connect 50 5 9 0; +#X connect 50 6 10 0; +#X connect 50 7 11 0; diff --git a/examples/shared-help.pd b/examples/shared-help.pd new file mode 100644 index 0000000..2c4851e --- /dev/null +++ b/examples/shared-help.pd @@ -0,0 +1,45 @@ +#N canvas 0 22 450 300 10; +#X obj 20 71 shared foo; +#X obj 139 72 shared foo; +#X obj 22 191 shared bar; +#X obj 141 194 shared bar; +#X obj 141 241 print bar; +#X obj 139 115 print foo; +#X floatatom 85 28 5 0 0 0 - - -; +#X symbolatom 204 29 10 0 0 0 - - -; +#X obj 139 28 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 20 28 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X floatatom 87 158 5 0 0 0 - - -; +#X symbolatom 206 159 10 0 0 0 - - -; +#X obj 141 158 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 22 158 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 289 72 shared foo; +#X obj 289 28 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X msg 354 27 a b c; +#X obj 291 194 shared bar; +#X obj 291 158 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X msg 356 158 d e f; +#X connect 0 0 5 0; +#X connect 1 0 5 0; +#X connect 2 0 4 0; +#X connect 3 0 4 0; +#X connect 6 0 0 1; +#X connect 7 0 1 1; +#X connect 8 0 1 0; +#X connect 9 0 0 0; +#X connect 10 0 2 1; +#X connect 11 0 3 1; +#X connect 12 0 3 0; +#X connect 13 0 2 0; +#X connect 14 0 5 0; +#X connect 15 0 14 0; +#X connect 16 0 14 1; +#X connect 17 0 4 0; +#X connect 18 0 17 0; +#X connect 19 0 17 1; diff --git a/examples/shared.pd_lua b/examples/shared.pd_lua new file mode 100644 index 0000000..b5d5dcd --- /dev/null +++ b/examples/shared.pd_lua @@ -0,0 +1,34 @@ +local Shared = pd.Class:new():register("shared") +local sharedStore = { } + +function Shared:initialize(sel, atoms) + if type(atoms[1]) == "string" then + if sharedStore[atoms[1]] == nil then + sharedStore[atoms[1]] = { sel = "bang", msg = { }, count = 1 } + else + sharedStore[atoms[1]].count = sharedStore[atoms[1]].count + 1 + end + self.name = atoms[1] + self.inlets = 2 + self.outlets = 1 + return true + else + return false + end +end + +function Shared:in_1_bang() + self:outlet(1, sharedStore[self.name].sel, sharedStore[self.name].msg) +end + +function Shared:in_2(s, m) + local c = sharedStore[self.name].count + sharedStore[self.name] = { sel = s, msg = m, count = c } +end + +function Shared:finalize() + sharedStore[self.name].count = sharedStore[self.name].count - 1 + if sharedStore[self.name].count == 0 then + sharedStore[self.name] = nil + end +end diff --git a/examples/shared.pd_luax b/examples/shared.pd_luax new file mode 100644 index 0000000..93881fe --- /dev/null +++ b/examples/shared.pd_luax @@ -0,0 +1,33 @@ +if sharedluaxstore == nil then + sharedluaxstore = { } -- initialize store only once +end + +return function (self, sel, atoms) + if type(atoms[1]) == "string" then + if sharedluaxstore[atoms[1]] == nil then + sharedluaxstore[atoms[1]] = { sel = "bang", msg = { }, count = 1 } + else + sharedluaxstore[atoms[1]].count = sharedluaxstore[atoms[1]].count + 1 + end + self.name = atoms[1] + self.inlets = 2 + self.outlets = 1 + self.in_1_bang = function(self) + local s = sharedluaxstore[self.name] + self:outlet(1, s.sel, s.msg) + end + self.in_2 = function(self, s, m) + local c = sharedluaxstore[self.name].count + sharedluaxstore[self.name] = { sel = s, msg = m, count = c } + end + self.finalize = function (self) + sharedluaxstore[self.name].count = sharedluaxstore[self.name].count - 1 + if sharedluaxstore[self.name].count == 0 then + sharedluaxstore[self.name] = nil + end + end + return true + else + return false + end +end diff --git a/examples/simplecounter-help.pd b/examples/simplecounter-help.pd new file mode 100644 index 0000000..1fd3f5a --- /dev/null +++ b/examples/simplecounter-help.pd @@ -0,0 +1,42 @@ +#N canvas 0 0 450 300 10; +#X text 21 17 these three are equivalent \, default start count is +0; +#X obj 22 78 simplecounter; +#X floatatom 22 113 5 0 0 0 - - -; +#X obj 22 50 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X floatatom 152 116 5 0 0 0 - - -; +#X obj 152 53 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 152 81 simplecounter 0; +#X floatatom 287 113 5 0 0 0 - - -; +#X obj 287 50 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 287 78 simplecounter foo; +#X text 19 141 the first creation argument (when a float) is start +count; +#X floatatom 17 252 5 0 0 0 - - -; +#X obj 17 189 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X floatatom 140 252 5 0 0 0 - - -; +#X obj 140 189 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 17 217 simplecounter 10; +#X obj 140 217 simplecounter -20; +#X floatatom 284 250 5 0 0 0 - - -; +#X obj 284 187 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 284 215 simplecounter 10 foo; +#X text 18 157 further creation arguments are ignored; +#X connect 1 0 2 0; +#X connect 3 0 1 0; +#X connect 5 0 6 0; +#X connect 6 0 4 0; +#X connect 8 0 9 0; +#X connect 9 0 7 0; +#X connect 12 0 15 0; +#X connect 14 0 16 0; +#X connect 15 0 11 0; +#X connect 16 0 13 0; +#X connect 18 0 19 0; +#X connect 19 0 17 0; diff --git a/examples/simplecounter.pd_lua b/examples/simplecounter.pd_lua new file mode 100644 index 0000000..1d6b7ee --- /dev/null +++ b/examples/simplecounter.pd_lua @@ -0,0 +1,14 @@ +local SimpleCounter = pd.Class:new():register("simplecounter") + +function SimpleCounter:initialize(sel, atoms) + self.inlets = 1 + self.outlets = 1 + self.count = 0 + if type(atoms[1]) == "number" then self.count = atoms[1] end + return true +end + +function SimpleCounter:in_1_bang() + self:outlet(1, "float", {self.count}) + self.count = self.count + 1 +end diff --git a/examples/simplecounter.pd_luax b/examples/simplecounter.pd_luax new file mode 100644 index 0000000..7b9f9e5 --- /dev/null +++ b/examples/simplecounter.pd_luax @@ -0,0 +1,11 @@ +return function (self, sel, atoms) + self.inlets = 1 + self.outlets = 1 + self.count = 0 + if type(atoms[1]) == "number" then self.count = atoms[1] end + function self:in_1_bang() + self:outlet(1, "float", {self.count}) + self.count = self.count + 1 + end + return true +end diff --git a/examples/swarm-help.pd b/examples/swarm-help.pd new file mode 100644 index 0000000..a4d07f9 --- /dev/null +++ b/examples/swarm-help.pd @@ -0,0 +1,656 @@ +#N canvas 0 0 453 370 10; +#N canvas 0 0 450 300 \$0-swarm-visualisation 0; +#X obj 155 111 bng 8 250 50 0 \$0-bang-s \$0-1-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 164 85 bng 8 250 50 0 \$0-bang-s \$0-2-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 150 139 bng 8 250 50 0 \$0-bang-s \$0-3-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 121 102 bng 8 250 50 0 \$0-bang-s \$0-4-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 118 102 bng 8 250 50 0 \$0-bang-s \$0-5-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 129 145 bng 8 250 50 0 \$0-bang-s \$0-6-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 129 147 bng 8 250 50 0 \$0-bang-s \$0-7-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 128 101 bng 8 250 50 0 \$0-bang-s \$0-8-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 128 94 bng 8 250 50 0 \$0-bang-s \$0-9-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 97 124 bng 8 250 50 0 \$0-bang-s \$0-10-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 172 134 bng 8 250 50 0 \$0-bang-s \$0-11-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 125 146 bng 8 250 50 0 \$0-bang-s \$0-12-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 125 96 bng 8 250 50 0 \$0-bang-s \$0-13-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 94 126 bng 8 250 50 0 \$0-bang-s \$0-14-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 116 101 bng 8 250 50 0 \$0-bang-s \$0-15-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 107 107 bng 8 250 50 0 \$0-bang-s \$0-16-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 126 147 bng 8 250 50 0 \$0-bang-s \$0-17-r empty 17 7 0 10 -4034 +-1 -1; +#X obj 125 148 bng 8 250 50 0 \$0-bang-s \$0-18-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 156 119 bng 8 250 50 0 \$0-bang-s \$0-19-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 103 92 bng 8 250 50 0 \$0-bang-s \$0-20-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 156 122 bng 8 250 50 0 \$0-bang-s \$0-21-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 117 146 bng 8 250 50 0 \$0-bang-s \$0-22-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 116 104 bng 8 250 50 0 \$0-bang-s \$0-23-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 130 155 bng 8 250 50 0 \$0-bang-s \$0-24-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 118 100 bng 8 250 50 0 \$0-bang-s \$0-25-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 159 82 bng 8 250 50 0 \$0-bang-s \$0-26-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 126 157 bng 8 250 50 0 \$0-bang-s \$0-27-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 125 157 bng 8 250 50 0 \$0-bang-s \$0-28-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 119 147 bng 8 250 50 0 \$0-bang-s \$0-29-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 150 125 bng 8 250 50 0 \$0-bang-s \$0-30-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 150 125 bng 8 250 50 0 \$0-bang-s \$0-31-r empty 17 7 0 10 -258113 +-1 -1; +#X obj 155 103 bng 8 250 50 0 \$0-bang-s \$0-32-r empty 17 7 0 10 -258113 +-1 -1; +#X coords 0 -1 1 1 200 200 2 0 0; +#X restore 220 31 pd \$0-swarm-visualisation; +#X obj 15 202 + 100; +#X obj 84 201 + 100; +#X obj 25 255 pack f f s; +#X obj 31 231 makefilename \$0-%d-r; +#X msg 25 279 \; \$3 pos \$1 \$2; +#X obj 13 4 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1 1 +; +#X obj 29 57 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X msg 75 72 randomize; +#X obj 85 178 * 100; +#X obj 16 179 * 100; +#X obj 17 132 swarm 2 32; +#X obj 17 157 unpack f f; +#X obj 133 256 list prepend; +#X obj 120 336 dac~; +#N canvas 0 0 1009 708 \$0-audio 0; +#X obj 9 74 unpack f f; +#X obj 7 97 * 200; +#X obj 6 121 + 300; +#X obj 5 143 osc~; +#X obj 71 102 * 0.5; +#X obj 69 129 + 0.5; +#X obj 137 28 route 1 2 3 4; +#X obj 5 174 *~ 0; +#X obj 127 77 unpack f f; +#X obj 125 100 * 200; +#X obj 124 124 + 300; +#X obj 123 146 osc~; +#X obj 189 105 * 0.5; +#X obj 187 132 + 0.5; +#X obj 123 177 *~ 0; +#X obj 198 220 *~ 0.25; +#X obj 236 78 unpack f f; +#X obj 234 101 * 200; +#X obj 233 125 + 300; +#X obj 232 147 osc~; +#X obj 298 106 * 0.5; +#X obj 296 133 + 0.5; +#X obj 232 178 *~ 0; +#X obj 354 81 unpack f f; +#X obj 352 104 * 200; +#X obj 351 128 + 300; +#X obj 350 150 osc~; +#X obj 416 109 * 0.5; +#X obj 414 136 + 0.5; +#X obj 350 181 *~ 0; +#X obj 490 76 unpack f f; +#X obj 488 99 * 200; +#X obj 487 123 + 300; +#X obj 486 145 osc~; +#X obj 552 104 * 0.5; +#X obj 550 131 + 0.5; +#X obj 486 176 *~ 0; +#X obj 608 79 unpack f f; +#X obj 606 102 * 200; +#X obj 605 126 + 300; +#X obj 604 148 osc~; +#X obj 670 107 * 0.5; +#X obj 668 134 + 0.5; +#X obj 604 179 *~ 0; +#X obj 679 222 *~ 0.25; +#X obj 717 80 unpack f f; +#X obj 715 103 * 200; +#X obj 714 127 + 300; +#X obj 713 149 osc~; +#X obj 779 108 * 0.5; +#X obj 777 135 + 0.5; +#X obj 713 180 *~ 0; +#X obj 835 83 unpack f f; +#X obj 833 106 * 200; +#X obj 832 130 + 300; +#X obj 831 152 osc~; +#X obj 897 111 * 0.5; +#X obj 895 138 + 0.5; +#X obj 831 183 *~ 0; +#X obj 618 30 route 5 6 7 8; +#X obj 137 7 inlet; +#X obj 9 294 unpack f f; +#X obj 7 317 * 200; +#X obj 6 341 + 300; +#X obj 5 363 osc~; +#X obj 71 322 * 0.5; +#X obj 69 349 + 0.5; +#X obj 5 394 *~ 0; +#X obj 127 297 unpack f f; +#X obj 125 320 * 200; +#X obj 124 344 + 300; +#X obj 123 366 osc~; +#X obj 189 325 * 0.5; +#X obj 187 352 + 0.5; +#X obj 123 397 *~ 0; +#X obj 198 440 *~ 0.25; +#X obj 236 298 unpack f f; +#X obj 234 321 * 200; +#X obj 233 345 + 300; +#X obj 232 367 osc~; +#X obj 298 326 * 0.5; +#X obj 296 353 + 0.5; +#X obj 232 398 *~ 0; +#X obj 354 301 unpack f f; +#X obj 352 324 * 200; +#X obj 351 348 + 300; +#X obj 350 370 osc~; +#X obj 416 329 * 0.5; +#X obj 414 356 + 0.5; +#X obj 350 401 *~ 0; +#X obj 490 296 unpack f f; +#X obj 488 319 * 200; +#X obj 487 343 + 300; +#X obj 486 365 osc~; +#X obj 552 324 * 0.5; +#X obj 550 351 + 0.5; +#X obj 486 396 *~ 0; +#X obj 608 299 unpack f f; +#X obj 606 322 * 200; +#X obj 605 346 + 300; +#X obj 604 368 osc~; +#X obj 670 327 * 0.5; +#X obj 668 354 + 0.5; +#X obj 604 399 *~ 0; +#X obj 679 442 *~ 0.25; +#X obj 717 300 unpack f f; +#X obj 715 323 * 200; +#X obj 714 347 + 300; +#X obj 713 369 osc~; +#X obj 779 328 * 0.5; +#X obj 777 355 + 0.5; +#X obj 713 400 *~ 0; +#X obj 835 303 unpack f f; +#X obj 833 326 * 200; +#X obj 832 350 + 300; +#X obj 831 372 osc~; +#X obj 897 331 * 0.5; +#X obj 895 358 + 0.5; +#X obj 831 403 *~ 0; +#X obj 137 248 route 9 10 11 12; +#X obj 618 250 route 13 14 15 16; +#X obj 4 514 unpack f f; +#X obj 2 537 * 200; +#X obj 1 561 + 300; +#X obj 0 583 osc~; +#X obj 66 542 * 0.5; +#X obj 64 569 + 0.5; +#X obj 0 614 *~ 0; +#X obj 122 517 unpack f f; +#X obj 120 540 * 200; +#X obj 119 564 + 300; +#X obj 118 586 osc~; +#X obj 184 545 * 0.5; +#X obj 182 572 + 0.5; +#X obj 118 617 *~ 0; +#X obj 193 660 *~ 0.25; +#X obj 231 518 unpack f f; +#X obj 229 541 * 200; +#X obj 228 565 + 300; +#X obj 227 587 osc~; +#X obj 293 546 * 0.5; +#X obj 291 573 + 0.5; +#X obj 227 618 *~ 0; +#X obj 349 521 unpack f f; +#X obj 347 544 * 200; +#X obj 346 568 + 300; +#X obj 345 590 osc~; +#X obj 411 549 * 0.5; +#X obj 409 576 + 0.5; +#X obj 345 621 *~ 0; +#X obj 485 516 unpack f f; +#X obj 483 539 * 200; +#X obj 482 563 + 300; +#X obj 481 585 osc~; +#X obj 547 544 * 0.5; +#X obj 545 571 + 0.5; +#X obj 481 616 *~ 0; +#X obj 603 519 unpack f f; +#X obj 601 542 * 200; +#X obj 600 566 + 300; +#X obj 599 588 osc~; +#X obj 665 547 * 0.5; +#X obj 663 574 + 0.5; +#X obj 599 619 *~ 0; +#X obj 674 662 *~ 0.25; +#X obj 712 520 unpack f f; +#X obj 710 543 * 200; +#X obj 709 567 + 300; +#X obj 708 589 osc~; +#X obj 774 548 * 0.5; +#X obj 772 575 + 0.5; +#X obj 708 620 *~ 0; +#X obj 830 523 unpack f f; +#X obj 828 546 * 200; +#X obj 827 570 + 300; +#X obj 826 592 osc~; +#X obj 892 551 * 0.5; +#X obj 890 578 + 0.5; +#X obj 826 623 *~ 0; +#X obj 4 734 unpack f f; +#X obj 2 757 * 200; +#X obj 1 781 + 300; +#X obj 0 803 osc~; +#X obj 66 762 * 0.5; +#X obj 64 789 + 0.5; +#X obj 0 834 *~ 0; +#X obj 122 737 unpack f f; +#X obj 120 760 * 200; +#X obj 119 784 + 300; +#X obj 118 806 osc~; +#X obj 184 765 * 0.5; +#X obj 182 792 + 0.5; +#X obj 118 837 *~ 0; +#X obj 193 880 *~ 0.25; +#X obj 231 738 unpack f f; +#X obj 229 761 * 200; +#X obj 228 785 + 300; +#X obj 227 807 osc~; +#X obj 293 766 * 0.5; +#X obj 291 793 + 0.5; +#X obj 227 838 *~ 0; +#X obj 349 741 unpack f f; +#X obj 347 764 * 200; +#X obj 346 788 + 300; +#X obj 345 810 osc~; +#X obj 411 769 * 0.5; +#X obj 409 796 + 0.5; +#X obj 345 841 *~ 0; +#X obj 485 736 unpack f f; +#X obj 483 759 * 200; +#X obj 482 783 + 300; +#X obj 481 805 osc~; +#X obj 547 764 * 0.5; +#X obj 545 791 + 0.5; +#X obj 481 836 *~ 0; +#X obj 603 739 unpack f f; +#X obj 601 762 * 200; +#X obj 600 786 + 300; +#X obj 599 808 osc~; +#X obj 665 767 * 0.5; +#X obj 663 794 + 0.5; +#X obj 599 839 *~ 0; +#X obj 674 882 *~ 0.25; +#X obj 712 740 unpack f f; +#X obj 710 763 * 200; +#X obj 709 787 + 300; +#X obj 708 809 osc~; +#X obj 774 768 * 0.5; +#X obj 772 795 + 0.5; +#X obj 708 840 *~ 0; +#X obj 830 743 unpack f f; +#X obj 828 766 * 200; +#X obj 827 790 + 300; +#X obj 826 812 osc~; +#X obj 892 771 * 0.5; +#X obj 890 798 + 0.5; +#X obj 826 843 *~ 0; +#X obj 132 468 route 17 18 19 20; +#X obj 613 470 route 21 22 23 24; +#X obj 132 689 route 25 26 27 28; +#X obj 613 690 route 29 30 31 32; +#X obj 418 910 outlet~; +#X text 360 4 stupid copy and paste \, but whatever...; +#X obj 410 887 *~ 0.125; +#X connect 0 0 1 0; +#X connect 0 1 4 0; +#X connect 1 0 2 0; +#X connect 2 0 3 0; +#X connect 3 0 7 0; +#X connect 4 0 5 0; +#X connect 5 0 7 1; +#X connect 6 0 0 0; +#X connect 6 1 8 0; +#X connect 6 2 16 0; +#X connect 6 3 23 0; +#X connect 6 4 59 0; +#X connect 7 0 15 0; +#X connect 8 0 9 0; +#X connect 8 1 12 0; +#X connect 9 0 10 0; +#X connect 10 0 11 0; +#X connect 11 0 14 0; +#X connect 12 0 13 0; +#X connect 13 0 14 1; +#X connect 14 0 15 0; +#X connect 15 0 243 0; +#X connect 16 0 17 0; +#X connect 16 1 20 0; +#X connect 17 0 18 0; +#X connect 18 0 19 0; +#X connect 19 0 22 0; +#X connect 20 0 21 0; +#X connect 21 0 22 1; +#X connect 22 0 15 0; +#X connect 23 0 24 0; +#X connect 23 1 27 0; +#X connect 24 0 25 0; +#X connect 25 0 26 0; +#X connect 26 0 29 0; +#X connect 27 0 28 0; +#X connect 28 0 29 1; +#X connect 29 0 15 0; +#X connect 30 0 31 0; +#X connect 30 1 34 0; +#X connect 31 0 32 0; +#X connect 32 0 33 0; +#X connect 33 0 36 0; +#X connect 34 0 35 0; +#X connect 35 0 36 1; +#X connect 36 0 44 0; +#X connect 37 0 38 0; +#X connect 37 1 41 0; +#X connect 38 0 39 0; +#X connect 39 0 40 0; +#X connect 40 0 43 0; +#X connect 41 0 42 0; +#X connect 42 0 43 1; +#X connect 43 0 44 0; +#X connect 44 0 243 0; +#X connect 45 0 46 0; +#X connect 45 1 49 0; +#X connect 46 0 47 0; +#X connect 47 0 48 0; +#X connect 48 0 51 0; +#X connect 49 0 50 0; +#X connect 50 0 51 1; +#X connect 51 0 44 0; +#X connect 52 0 53 0; +#X connect 52 1 56 0; +#X connect 53 0 54 0; +#X connect 54 0 55 0; +#X connect 55 0 58 0; +#X connect 56 0 57 0; +#X connect 57 0 58 1; +#X connect 58 0 44 0; +#X connect 59 0 30 0; +#X connect 59 1 37 0; +#X connect 59 2 45 0; +#X connect 59 3 52 0; +#X connect 59 4 119 0; +#X connect 60 0 6 0; +#X connect 61 0 62 0; +#X connect 61 1 65 0; +#X connect 62 0 63 0; +#X connect 63 0 64 0; +#X connect 64 0 67 0; +#X connect 65 0 66 0; +#X connect 66 0 67 1; +#X connect 67 0 75 0; +#X connect 68 0 69 0; +#X connect 68 1 72 0; +#X connect 69 0 70 0; +#X connect 70 0 71 0; +#X connect 71 0 74 0; +#X connect 72 0 73 0; +#X connect 73 0 74 1; +#X connect 74 0 75 0; +#X connect 75 0 243 0; +#X connect 76 0 77 0; +#X connect 76 1 80 0; +#X connect 77 0 78 0; +#X connect 78 0 79 0; +#X connect 79 0 82 0; +#X connect 80 0 81 0; +#X connect 81 0 82 1; +#X connect 82 0 75 0; +#X connect 83 0 84 0; +#X connect 83 1 87 0; +#X connect 84 0 85 0; +#X connect 85 0 86 0; +#X connect 86 0 89 0; +#X connect 87 0 88 0; +#X connect 88 0 89 1; +#X connect 89 0 75 0; +#X connect 90 0 91 0; +#X connect 90 1 94 0; +#X connect 91 0 92 0; +#X connect 92 0 93 0; +#X connect 93 0 96 0; +#X connect 94 0 95 0; +#X connect 95 0 96 1; +#X connect 96 0 104 0; +#X connect 97 0 98 0; +#X connect 97 1 101 0; +#X connect 98 0 99 0; +#X connect 99 0 100 0; +#X connect 100 0 103 0; +#X connect 101 0 102 0; +#X connect 102 0 103 1; +#X connect 103 0 104 0; +#X connect 104 0 243 0; +#X connect 105 0 106 0; +#X connect 105 1 109 0; +#X connect 106 0 107 0; +#X connect 107 0 108 0; +#X connect 108 0 111 0; +#X connect 109 0 110 0; +#X connect 110 0 111 1; +#X connect 111 0 104 0; +#X connect 112 0 113 0; +#X connect 112 1 116 0; +#X connect 113 0 114 0; +#X connect 114 0 115 0; +#X connect 115 0 118 0; +#X connect 116 0 117 0; +#X connect 117 0 118 1; +#X connect 118 0 104 0; +#X connect 119 0 61 0; +#X connect 119 1 68 0; +#X connect 119 2 76 0; +#X connect 119 3 83 0; +#X connect 119 4 120 0; +#X connect 120 0 90 0; +#X connect 120 1 97 0; +#X connect 120 2 105 0; +#X connect 120 3 112 0; +#X connect 120 4 237 0; +#X connect 121 0 122 0; +#X connect 121 1 125 0; +#X connect 122 0 123 0; +#X connect 123 0 124 0; +#X connect 124 0 127 0; +#X connect 125 0 126 0; +#X connect 126 0 127 1; +#X connect 127 0 135 0; +#X connect 128 0 129 0; +#X connect 128 1 132 0; +#X connect 129 0 130 0; +#X connect 130 0 131 0; +#X connect 131 0 134 0; +#X connect 132 0 133 0; +#X connect 133 0 134 1; +#X connect 134 0 135 0; +#X connect 135 0 243 0; +#X connect 136 0 137 0; +#X connect 136 1 140 0; +#X connect 137 0 138 0; +#X connect 138 0 139 0; +#X connect 139 0 142 0; +#X connect 140 0 141 0; +#X connect 141 0 142 1; +#X connect 142 0 135 0; +#X connect 143 0 144 0; +#X connect 143 1 147 0; +#X connect 144 0 145 0; +#X connect 145 0 146 0; +#X connect 146 0 149 0; +#X connect 147 0 148 0; +#X connect 148 0 149 1; +#X connect 149 0 135 0; +#X connect 150 0 151 0; +#X connect 150 1 154 0; +#X connect 151 0 152 0; +#X connect 152 0 153 0; +#X connect 153 0 156 0; +#X connect 154 0 155 0; +#X connect 155 0 156 1; +#X connect 156 0 164 0; +#X connect 157 0 158 0; +#X connect 157 1 161 0; +#X connect 158 0 159 0; +#X connect 159 0 160 0; +#X connect 160 0 163 0; +#X connect 161 0 162 0; +#X connect 162 0 163 1; +#X connect 163 0 164 0; +#X connect 164 0 243 0; +#X connect 165 0 166 0; +#X connect 165 1 169 0; +#X connect 166 0 167 0; +#X connect 167 0 168 0; +#X connect 168 0 171 0; +#X connect 169 0 170 0; +#X connect 170 0 171 1; +#X connect 171 0 164 0; +#X connect 172 0 173 0; +#X connect 172 1 176 0; +#X connect 173 0 174 0; +#X connect 174 0 175 0; +#X connect 175 0 178 0; +#X connect 176 0 177 0; +#X connect 177 0 178 1; +#X connect 178 0 164 0; +#X connect 179 0 180 0; +#X connect 179 1 183 0; +#X connect 180 0 181 0; +#X connect 181 0 182 0; +#X connect 182 0 185 0; +#X connect 183 0 184 0; +#X connect 184 0 185 1; +#X connect 185 0 193 0; +#X connect 186 0 187 0; +#X connect 186 1 190 0; +#X connect 187 0 188 0; +#X connect 188 0 189 0; +#X connect 189 0 192 0; +#X connect 190 0 191 0; +#X connect 191 0 192 1; +#X connect 192 0 193 0; +#X connect 193 0 243 0; +#X connect 194 0 195 0; +#X connect 194 1 198 0; +#X connect 195 0 196 0; +#X connect 196 0 197 0; +#X connect 197 0 200 0; +#X connect 198 0 199 0; +#X connect 199 0 200 1; +#X connect 200 0 193 0; +#X connect 201 0 202 0; +#X connect 201 1 205 0; +#X connect 202 0 203 0; +#X connect 203 0 204 0; +#X connect 204 0 207 0; +#X connect 205 0 206 0; +#X connect 206 0 207 1; +#X connect 207 0 193 0; +#X connect 208 0 209 0; +#X connect 208 1 212 0; +#X connect 209 0 210 0; +#X connect 210 0 211 0; +#X connect 211 0 214 0; +#X connect 212 0 213 0; +#X connect 213 0 214 1; +#X connect 214 0 222 0; +#X connect 215 0 216 0; +#X connect 215 1 219 0; +#X connect 216 0 217 0; +#X connect 217 0 218 0; +#X connect 218 0 221 0; +#X connect 219 0 220 0; +#X connect 220 0 221 1; +#X connect 221 0 222 0; +#X connect 222 0 243 0; +#X connect 223 0 224 0; +#X connect 223 1 227 0; +#X connect 224 0 225 0; +#X connect 225 0 226 0; +#X connect 226 0 229 0; +#X connect 227 0 228 0; +#X connect 228 0 229 1; +#X connect 229 0 222 0; +#X connect 230 0 231 0; +#X connect 230 1 234 0; +#X connect 231 0 232 0; +#X connect 232 0 233 0; +#X connect 233 0 236 0; +#X connect 234 0 235 0; +#X connect 235 0 236 1; +#X connect 236 0 222 0; +#X connect 237 0 121 0; +#X connect 237 1 128 0; +#X connect 237 2 136 0; +#X connect 237 3 143 0; +#X connect 237 4 238 0; +#X connect 238 0 150 0; +#X connect 238 1 157 0; +#X connect 238 2 165 0; +#X connect 238 3 172 0; +#X connect 238 4 239 0; +#X connect 239 0 179 0; +#X connect 239 1 186 0; +#X connect 239 2 194 0; +#X connect 239 3 201 0; +#X connect 239 4 240 0; +#X connect 240 0 208 0; +#X connect 240 1 215 0; +#X connect 240 2 223 0; +#X connect 240 3 230 0; +#X connect 243 0 241 0; +#X restore 133 282 pd \$0-audio; +#X obj 13 33 metro 40; +#X text 181 309 boids-style swarm in pd with Lua; +#X connect 1 0 3 0; +#X connect 2 0 3 1; +#X connect 3 0 5 0; +#X connect 4 0 3 2; +#X connect 6 0 16 0; +#X connect 7 0 11 0; +#X connect 8 0 11 0; +#X connect 9 0 2 0; +#X connect 10 0 1 0; +#X connect 11 0 12 0; +#X connect 11 0 13 0; +#X connect 11 1 4 0; +#X connect 11 1 13 1; +#X connect 12 0 10 0; +#X connect 12 1 9 0; +#X connect 13 0 15 0; +#X connect 15 0 14 0; +#X connect 15 0 14 1; +#X connect 16 0 11 0; diff --git a/examples/swarm.pd_lua b/examples/swarm.pd_lua new file mode 100644 index 0000000..d6e7f0a --- /dev/null +++ b/examples/swarm.pd_lua @@ -0,0 +1,100 @@ +-- see also: http://www.vergenet.net/~conrad/boids/pseudocode.html +local swarm = pd.Class:new():register("swarm") + +function swarm:initialize(sel, atoms) -- constructor + if type(atoms[1]) ~= "number" or atoms[1] < 2 then return false end + if type(atoms[2]) ~= "number" or atoms[2] < 3 then return false end + self.dim = math.floor(atoms[1]) + self.count = math.floor(atoms[2]) + self.cluster = 0.05 -- magic values look ok in the help patch.. + self.distance2 = 0.2 + self.similar2 = 0.1 + self.friction = 0.96 + self.flock = { } + self:in_1_randomize() + self.inlets = 2 + self.outlets = 2 + return true +end + +function swarm:in_1_randomize() -- randomize positions, no movement + for i = 1, self.count do + self.flock[i] = { x = { }, dx = { } } + for j = 1, self.dim do + self.flock[i].x[j] = math.random() - 0.5 + self.flock[i].dx[j] = 0 + end + self.flock[i].w = math.random() + 0.5 + end +end + +function swarm:in_1_bang() -- update and output + local c = self:center() + for i = 1, self.count do + f = self.flock[i] -- update + local v1 = self:rule1(c, f) + local v2 = self:rule2(i, f) + local v3 = self:rule3(i, f) + for k = 1, self.dim do f.dx[k] = f.dx[k] + v1[k] + v2[k] + v3[k] end + for k = 1, self.dim do f.dx[k] = f.dx[k] * self.friction end + for k = 1, self.dim do f.x[k] = f.x[k] + f.dx[k] end + self:outlet(2, "float", { i }) -- output + self:outlet(1, "list", f.x) + end +end + +function swarm:center() -- center of mass + local c = { } + local w = 0 + for k = 1, self.dim do c[k] = 0 end + for i = 1, self.count do + w = w + self.flock[i].w + for k = 1, self.dim do c[k] = c[k] + self.flock[i].w * self.flock[i].x[k] end + end + for k = 1, self.dim do c[k] = c[k] / w end + return c +end + +function swarm:rule1(c, f) -- clustering + local v = { } + for k = 1, self.dim do v[k] = self.cluster * (c[k] - (1 + f.w) * f.x[k]) end + return v +end + +function swarm:rule2(i, f) -- avoidance + local v = { } + for k = 1, self.dim do v[k] = 0 end + for j = 1, self.count do + if i ~= j then + g = self.flock[j] + local d = { } + local m = 0 + for k = 1, self.dim do d[k] = g.x[k] - f.x[k] ; m = m + d[k] * d[k] end + if m < self.distance2 then + for k = 1, self.dim do v[k] = v[k] - d[k] end + end + end + end + for k = 1, self.dim do v[k] = 0.01 * v[k] end + return v +end + +function swarm:rule3(i, f) -- similarity + local v = { } + for k = 1, self.dim do v[k] = 0 end + for j = 1, self.count do + if i ~= j then + g = self.flock[j] + local d = { } + local m = 0 + for k = 1, self.dim do d[k] = g.dx[k] - f.dx[k] ; m = m + d[k] * d[k] end + if m < self.similar2 then + for k = 1, self.dim do v[k] = v[k] + d[k] end + end + end + end + for k = 1, self.dim do v[k] = 0.004 * v[k] end + return v +end + +-- exercise: make the right inlet control individual elements |