On 2012-02-15 16:11, Jonathan Wilkes wrote:
Let's go back:
Instead of time units, let's take the incoming single-selector "pi" and replace
it with
the float value of pi for any object that doesn't have a "pi" method or an
"anything" method (but does have a float method). Furthermore, if a class
defines a
float argument but the argument provided isn't a float, then check first if
what it received
was A_SYMBOL 'pi' and if so replace with the float value of pi.
Is what I wrote above possible to do without causing huge problems to
performance or
creating ambiguities? Is it something people would look at as a feature, or a
nuisance that
makes it harder to learn Pd? If the answer is that something like this
constant expansion
is a bad idea then there's no point discussing macro expansion for time units.
-Jonathan
I think it's a bad idea to make assumptions like that. It would be far
better to interpose a filter when needed. I attach a pdlua script
[str2ms] that will output a float millisecond value given some kind of
string specifying time. It's very easy to extend to other time types and
only needs to be used if you want it, not like some secret apple troll
lurking inside the object that "knows" what I want.
Martin
--[[
str2ms
Tries to convert symbols to milliseconds
MP 20120215
--Written by Martin Peach
--]]
-- Pd class
local str2ms = pd.Class:new():register("str2ms")
local result -- save most recent result for bang
function str2ms:initialize(sel, atoms)
self.inlets = 1
self.outlets = 1
--pd.post("str2ms init")
return true
end
function str2ms.str2ms(s)
local values = {}
local types = {}
local multipliers = {s = 1000, m = 60000, h = 3600000, d = 86400000}
--milliseconds
for w in string.gmatch(s, "%d+") do
table.insert(values, w)
end
for w in string.gmatch(s, "%a+") do
table.insert(types, w)
end
if #values ~= #types then
return "number/type mismatch"
end
for i = 1, #types do
if (multipliers[types[i]]) == nil then
return "unknown time type "..types[i]
end
end
local totalms = 0
for i = 1, #types do
totalms = totalms + (multipliers[types[i]]*values[i])
end
return totalms
end
function str2ms:in_1(sel, atoms)
local ms = 0
local str
--pd.post("str2ms:in1:"..sel..#atoms.." atoms")
if sel == "float" then -- float is already in milliseconds
result = atoms[1]
self:outlet(1, "float", {result})
return
elseif sel == "symbol" then -- symbol is atom[1], may be convertible
str = atoms[1]
elseif sel == "pointer" then -- invalid type
pd.post(self._name..": no method for pointer")
return
elseif sel == "bang" then -- maybe repeat previous output
if type(result) == "number" then -- valid conversion
self:outlet(1, "float", {result})
end
return
elseif #atoms > 1 then -- lists are fraught
pd.post(self._name..": no method for list")
return
elseif #atoms == 0 then -- selector may be convertible to milliseconds
str = sel
end
result = self.str2ms(str) -- try to convert
if type(result) == "number" then -- valid conversion
self:outlet(1, "float", {result})
elseif type(result) == "string" then -- error string
pd.post(self._name..": "..result)
end
end
_______________________________________________
Pd-list@iem.at mailing list
UNSUBSCRIBE and account-management ->
http://lists.puredata.info/listinfo/pd-list