I am having some trouble “converting” my class to a proper luabind class. Specifically, how do I prevent certain keys from the class to be copied into the object?
I am assuming that luabind classes are using 100% of the class as template per default, with no need of the constructor copying the class table with a for loop.
I’m also assuming (wrongly?) that I don’t have to use getters/setters if I don’t really want to.
This is where I am at right now. As seen, line 76- is now obsolete (was only relevant in my old table based class), but tells what I want to achieve. Any idea?
PS: will change to __init() later on
Click to view contents
local xinc -- for testing only
class 'osc'
osc.DEFAULT_TYPE = "min"
osc.DEFAULT_MIN_VALUE = 0
osc.DEFAULT_MAX_VALUE = 255
osc.DEFAULT_PHASE = 0
osc.DEFAULT_RESOLUTION = 1
osc.func = {
{ user_abr = { "sine", "sin" },
calc = function(phase) return (math.sin(math.rad(360 * phase)) / 2 + 0.5) end },
{ user_abr = { "square", "pulse", "sqr", "pul" },
calc = function(phase) if (phase <= 0.5) then return 1 else return 0 end end },
{ user_abr = { "random", "rnd", "noise" },
calc = function(phase) return math.random() end },
{ user_abr = { "tri", "triangle" },
calc = function(phase) return math.abs(((phase+0.75) % 1) - 0.5) * 2 end },
{ user_abr = { "saw", "ramp", "sawtooth" },
calc = function(phase) return phase end },
{ user_abr = { "min" },
calc = function() return 0 end },
{ user_abr = { "max" },
calc = function() return 1 end },
}
osc.get_index = function(user_type)
for i_osc, osc in ipairs(osc.func) do
for i_abr, abr in ipairs(osc.user_abr) do
if (abr == user_type) then return i_osc end
end
end
return osc.DEFAULT_TYPE
end
function osc:runtime()
return xinc - self.start_xinc
end
function osc:reset(phase)
self.start_xinc, self.phase = xinc, phase or self.phase
end
function osc:value(scale_min, scale_max, resolution)
print(self.phase)
local phase = self:runtime() % self.freq / self.freq + self.phase
local min, max, res = scale_min or self.min_value, scale_max or self.max_value, resolution or self.resolution
local value = osc.func[self.type].calc(phase) * (max-min) + min
return math.max(min, math.min(max, math.floor(value/res+0.500001) * res))
end
function osc:__tostring()
return self:value()
end
function osc.create(type, freq, min_value, max_value, phase, resolution)
local object
local args_valid = tonumber(freq) and type and
(min_value == nil or tonumber(min_value)) and
(max_value == nil or tonumber(max_value)) and
(phase == nil or (tonumber(phase) and phase <= 1 and phase >= 0)) and
(resolution == nil or tonumber(resolution))
if args_valid then
object = {
type = osc.get_index(type),
freq = freq,
min_value = min_value or osc.DEFAULT_MIN_VALUE,
max_value = max_value or osc.DEFAULT_MAX_VALUE,
resolution = resolution or osc.DEFAULT_RESOLUTION,
phase = phase or osc.DEFAULT_PHASE,
start_xinc = xinc
}
for k, val in pairs(osc) do
if not (k == "func") or (k == "get_index") then
object[k] = val
end
end
end
return object
end
-- testing below
for i = 1, 32 do xinc = i
if xinc == 1 then my_osc = osc.create("sine", 16, 0, 1) end
print(my_osc)
end