Module:MOS tunings: Difference between revisions

Ganaram inukshuk (talk | contribs)
todo
Ganaram inukshuk (talk | contribs)
Added "rounding" to step ratio range lookup
Line 11: Line 11:


-- TODO:
-- TODO:
-- - Base step ratio range lookup by whether the largest and smallest ratios
--  fall within the endpoints of a range
-- - Links
-- - Links
-- - Text align
-- - Text align
Line 22: Line 20:
function p.capitalize_first(text)
function p.capitalize_first(text)
return string.upper(string.sub(text, 1, 1)) .. string.sub(text, 2, -1)
return string.upper(string.sub(text, 1, 1)) .. string.sub(text, 2, -1)
end
-- Helper function
-- "Rounds" step ratios up to the nearest named ratio
function p.step_ratio_ceil(step_ratio)
local hardness = step_ratio[1] / step_ratio[2]
local rounded_step_ratio = nil
if hardness > 1/1 and hardness <= 4/3 then
rounded_step_ratio = {4,3}
elseif hardness > 4/3 and hardness <= 3/2 then
rounded_step_ratio = {3,2}
elseif hardness > 3/2 and hardness <= 5/3 then
rounded_step_ratio = {5,3}
elseif hardness > 5/3 and hardness <= 2/1 then
rounded_step_ratio = {2,1}
elseif hardness > 2/1 and hardness <= 5/2 then
rounded_step_ratio = {5,2}
elseif hardness > 5/2 and hardness <= 3/1 then
rounded_step_ratio = {3,1}
elseif hardness > 3/1 and hardness <= 4/1 then
rounded_step_ratio = {4,1}
elseif hardness > 4/1 and hardness <= 1/0 then
rounded_step_ratio = {1,0}
end
return rounded_step_ratio
end
-- Helper function
-- "Rounds" step ratios down to the nearest named ratio
function p.step_ratio_floor(step_ratio)
local hardness = step_ratio[1] / step_ratio[2]
local rounded_step_ratio = nil
if hardness >= 1/1 and hardness < 4/3 then
rounded_step_ratio = {1,1}
elseif hardness >= 4/3 and hardness < 3/2 then
rounded_step_ratio = {4,3}
elseif hardness >= 3/2 and hardness < 5/3 then
rounded_step_ratio = {3,2}
elseif hardness >= 5/3 and hardness < 2/1 then
rounded_step_ratio = {5,3}
elseif hardness >= 2/1 and hardness < 5/2 then
rounded_step_ratio = {2,1}
elseif hardness >= 5/2 and hardness < 3/1 then
rounded_step_ratio = {5,2}
elseif hardness >= 3/1 and hardness < 4/1 then
rounded_step_ratio = {3,1}
elseif hardness >= 4/1 and hardness < 1/0 then
rounded_step_ratio = {4,1}
end
return rounded_step_ratio
end
end


Line 33: Line 83:
-- Sort using selection sort, which is ok for smol datasets.
-- Sort using selection sort, which is ok for smol datasets.
for i = 1, #step_ratios - 1 do
for i = 1, #step_ratios - 1 do
local index_of_smallest = i
local index_of_smallest = i
local current_val = step_ratios[i][1] / step_ratios[i][2]
local current_val = step_ratios[i][1] / step_ratios[i][2]
Line 50: Line 99:
end
end
end
end
return step_ratios
return step_ratios
end
end
Line 76: Line 124:
-- hardness. If there are zero step ratios, then return "Tunings"
-- hardness. If there are zero step ratios, then return "Tunings"
if #step_ratios > 1 then
if #step_ratios > 1 then
step_ratio_range = tamnams.lookup_step_ratio_range(step_ratios[1], step_ratios[#step_ratios])
local lower_ratio = p.step_ratio_floor(step_ratios[1])
local upper_ratio = p.step_ratio_ceil (step_ratios[#step_ratios])
step_ratio_range = tamnams.lookup_step_ratio_range(lower_ratio, upper_ratio)
if step_ratio_range ~= nil then
if step_ratio_range ~= nil then
step_ratio_range = p.capitalize_first(step_ratio_range) .. " Tunings"
step_ratio_range = p.capitalize_first(step_ratio_range) .. " Tunings"
Line 189: Line 239:


function p.tester()
function p.tester()
return p.sort_step_ratios({ {2,1}, {3,1}, {3,2} })
local range, ratios = p.preprocess_step_ratios({ {7,1}, {3,1}, {2,1} })
return range
end
end


return p
return p