aboutsummaryrefslogtreecommitdiff
path: root/examples/lfloat2bytes.pd_lua
blob: 7dc8480b1e038fab0af3651382c403012c53f7ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--[[ 
  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