From 4bab8348d2842245d93833a80f0d4713f973069c Mon Sep 17 00:00:00 2001 From: Martin Peach Date: Fri, 20 Apr 2012 19:03:21 +0000 Subject: Two objects to serialize symbols and floats. svn path=/trunk/externals/loaders/pdlua/; revision=16129 --- examples/lfloat2bytes-help.pd | 28 ++++++++++++++++++++ examples/lfloat2bytes.pd_lua | 58 ++++++++++++++++++++++++++++++++++++++++++ examples/lsymbol2bytes-help.pd | 27 ++++++++++++++++++++ examples/lsymbol2bytes.pd_lua | 46 +++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 examples/lfloat2bytes-help.pd create mode 100644 examples/lfloat2bytes.pd_lua create mode 100644 examples/lsymbol2bytes-help.pd create mode 100644 examples/lsymbol2bytes.pd_lua diff --git a/examples/lfloat2bytes-help.pd b/examples/lfloat2bytes-help.pd new file mode 100644 index 0000000..8ca6d46 --- /dev/null +++ b/examples/lfloat2bytes-help.pd @@ -0,0 +1,28 @@ +#N canvas 494 466 616 332 10; +#X declare -lib pdlua; +#X obj 23 8 import pdlua; +#X obj 86 80 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 161 278 print bytes; +#X text 390 282 by Martin Peach 2012_04_20; +#X msg 39 54 3.14159; +#X floatatom 101 59 5 0 0 0 - - -; +#X obj 86 101 f; +#X obj 126 114 atan; +#X msg 126 90 1; +#X obj 126 140 * 4; +#X floatatom 158 157 12 0 0 0 - - -; +#X text 173 242 The optional argument sets the number of significant +digits (default = 6).; +#X text 173 212 [lfloat2bytes] spits out the characters of a string +representation of a float as bytes.; +#X obj 161 193 lfloat2bytes 12; +#X connect 1 0 6 0; +#X connect 4 0 13 0; +#X connect 5 0 6 1; +#X connect 6 0 13 0; +#X connect 7 0 9 0; +#X connect 8 0 7 0; +#X connect 9 0 10 0; +#X connect 9 0 13 0; +#X connect 13 0 2 0; diff --git a/examples/lfloat2bytes.pd_lua b/examples/lfloat2bytes.pd_lua new file mode 100644 index 0000000..7dc8480 --- /dev/null +++ b/examples/lfloat2bytes.pd_lua @@ -0,0 +1,58 @@ +--[[ + lfloat2bytes + Output a float as a sequence of bytes + author Martin Peach 20120420 +--]] + +-- Pd class + +local lfloat2bytes = pd.Class:new():register("lfloat2bytes") +local digitstr = "%g" -- defaut format string gives up to 6 significant digits + +function lfloat2bytes:initialize(name, atoms) + self.inlets = 1 + self.outlets = 1 + if (#atoms > 0) then + --pd.post(#atoms .. " args") + --pd.post("atom 1: " .. atoms[1]) + --pd.post(type(atoms[1])) + if (type(atoms[1]) ~= "number") then + self:error("lfloat2bytes: Digit count not a number") + elseif (atoms[1] > 0) and (atoms[1] <= 24) then + digitstr = "%0." .. atoms[1] .. "g" + else + self:error("lfloat2bytes: Digit count out of range") + end + end + + return true +end + +-- lfloat2bytes:drip: output characters from buf as floats on [0..255] +function lfloat2bytes:drip(buf) + --pd.post(buf.. " size " .. #buf) + local i = 1, b + local len = #buf + + for i = 1, len do + self:outlet(1, "float", {string.byte(buf, i)}) + end +end + +function lfloat2bytes:in_1(sel, atoms) -- anything + --pd.post("sel is " .. sel); + --pd.post("number of atoms is ".. #atoms) + --for i,v in ipairs(atoms) do + -- pd.post(i .. " = " .. v) + --end + local buf + if sel == "float" then -- the usual case + --buf = tostring(atoms[1]) + buf = string.format(digitstr, atoms[1]) + self:drip(buf) + else -- reject non-floats + self:error("lfloat2bytes: input not a float") + end +end +-- end of lfloat2bytes + diff --git a/examples/lsymbol2bytes-help.pd b/examples/lsymbol2bytes-help.pd new file mode 100644 index 0000000..de4fc0f --- /dev/null +++ b/examples/lsymbol2bytes-help.pd @@ -0,0 +1,27 @@ +#N canvas 536 461 616 315 10; +#X declare -lib pdlua; +#X obj 23 8 import pdlua; +#X msg 24 59 what; +#X symbolatom 121 157 10 0 0 0 - - -; +#X obj 91 141 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X msg 73 108 symbol qwertyuiop; +#X msg 45 80 why not; +#X text 98 79 <- but a message is rejected; +#X text 58 59 <- a lone selector is taken to be a symbol; +#X text 216 181 <- unicode should be handled correctly; +#X msg 146 181 symbol zéyé; +#X msg 121 130 set Who; +#X obj 166 218 lsymbol2bytes; +#X text 258 218 [lsymbol2bytes] spits out the characters of a symbol +as bytes.; +#X obj 166 253 print bytes; +#X text 407 261 by Martin Peach 2012_04_20; +#X connect 1 0 11 0; +#X connect 2 0 11 0; +#X connect 3 0 2 0; +#X connect 4 0 11 0; +#X connect 5 0 11 0; +#X connect 9 0 11 0; +#X connect 10 0 2 0; +#X connect 11 0 13 0; diff --git a/examples/lsymbol2bytes.pd_lua b/examples/lsymbol2bytes.pd_lua new file mode 100644 index 0000000..d024072 --- /dev/null +++ b/examples/lsymbol2bytes.pd_lua @@ -0,0 +1,46 @@ +--[[ + lsymbol2bytes + Output a symbol as a sequence of bytes + author Martin Peach 20120420 +--]] + +-- Pd class + +local Lsymbol2Bytes = pd.Class:new():register("lsymbol2bytes") + +function Lsymbol2Bytes:initialize(name, atoms) + self.inlets = 1 + self.outlets = 1 + return true +end + +-- Lsymbol2Bytes:drip: output possibly multibyte characters from buf and output them as floats on [0..255] +function Lsymbol2Bytes:drip(buf) + --pd.post(buf.. " size " .. #buf) + local i = 1, b + local len = #buf + + for i = 1, len do + self:outlet(1, "float", {string.byte(buf, i)}) + end +end + +function Lsymbol2Bytes:in_1(sel, atoms) -- anything + --pd.post("sel is " .. sel); + --pd.post("number of atoms is ".. #atoms) + --for i,v in ipairs(atoms) do + -- pd.post(i .. " = " .. v) + --end + local buf + if sel == "symbol" then -- the usual case + buf = tostring(atoms[1]) + self:drip(buf) + elseif #atoms == 0 then -- a selector + buf = tostring(sel) + self:drip(buf) + else -- reject non-symbols + self:error("lsymbol2bytes: input not a symbol") + end +end +-- end of lsymbol2bytes + -- cgit v1.2.1