Module:ET: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 65: | Line 65: | ||
steps = steps or 1 | steps = steps or 1 | ||
return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2) | return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2) | ||
end | |||
-- convert ratio to steps | |||
-- ratio is a float! | |||
-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil) | |||
function p.approximate(et, ratio, towards) | |||
towards = towards or 0 | |||
if et.size == 0 then | |||
return 0 | |||
end | |||
local exact = math.log(ratio) / math.log(rat.as_float(et.equave)) * et.size | |||
if towards < 0 then | |||
return math.floor(exact) | |||
elseif towards > 0 then | |||
return math.ceil(exact) | |||
else | |||
return math.floor(exact + 0.5) | |||
end | |||
end | end | ||
return p | return p |