Module:MOS tunings: Difference between revisions
m update todo |
Step ratios are now sorted by hardness, except for simple step ratios (2/1, 3/1, and 3/2) |
||
| Line 25: | Line 25: | ||
-- Helper function | -- Helper function | ||
-- Finds the step ratio range | -- Sorts step ratios L:s by their hardnesses | ||
function p. | function p.sort_step_ratios(step_ratios) | ||
local step_ratios = step_ratios | if #step_ratios > 2 then | ||
return step_ratios | |||
end | |||
-- Sort using selection sort, which is ok for smol datasets. | |||
for i = 1, #step_ratios - 1 do | |||
local index_of_current = i | |||
local current_val = step_ratios[i][1] / step_ratios[i][2] | |||
-- Find the ratio with the smallest hardness | |||
for j = i + 1, #step_ratios do | |||
if (step_ratios[j][1] / step_ratios[j][2] < current_val) then | |||
index_of_current = j | |||
end | |||
end | |||
if index_of_current ~= j then | |||
local temp = step_ratios[index_of_current] | |||
step_ratios[index_of_current] = step_ratios[j] | |||
step_ratios[j] = temp | |||
end | |||
end | |||
return step_ratios | |||
end | |||
-- Helper function | |||
-- Finds the step ratio range and sorts step ratios | |||
function p.preprocess_step_ratios(step_ratios) | |||
local step_ratios = p.sort_step_ratios(step_ratios) | |||
local step_ratio_range = "" | local step_ratio_range = "" | ||
-- If the step ratios are 2/1, 3/1, and 3/2 in that order, then they are | -- If the step ratios are 2/1, 3/1, and 3/2 in that order, then they are | ||
-- the simple step ratios: basic, hard, and soft | -- the simple step ratios: basic, hard, and soft. | ||
-- These should not be sorted, since the basic-hard-soft sorting is a little | |||
-- more intuitive than sorting by hardness. | |||
if #step_ratios == 3 then | if #step_ratios == 3 then | ||
if step_ratios[1][1] == | if step_ratios[1][1] == 3 and step_ratios[1][2] == 2 | ||
and step_ratios[2][1] == | and step_ratios[2][1] == 2 and step_ratios[2][2] == 1 | ||
and step_ratios[3][1] == 3 and step_ratios[3][2] == | and step_ratios[3][1] == 3 and step_ratios[3][2] == 1 then | ||
return "Simple Tunings" | return "Simple Tunings", {{2,1}, {3,1}, {3,2}} | ||
end | end | ||
end | end | ||
-- If there are multiple step ratios, find the | -- If there are multiple step ratios, find the step ratio range it | ||
-- | -- corresponds to. If there is one step ratio, find the name of that | ||
-- | -- 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]) .. " Tunings") or "Tunings" | |||
step_ratio_range = (tamnams.lookup_step_ratio_range(step_ratios[ | |||
elseif #step_ratios == 1 then | elseif #step_ratios == 1 then | ||
step_ratio_range = (tamnams.lookup_step_ratio(step_ratios[1]) or string.format("%s/%s", step_ratios[1][1], step_ratios[1][2])) .. " Tuning" | step_ratio_range = (tamnams.lookup_step_ratio(step_ratios[1]) or string.format("%s/%s", step_ratios[1][1], step_ratios[1][2])) .. " Tuning" | ||
| Line 77: | Line 86: | ||
step_ratio_range = p.capitalize_first(step_ratio_range) | step_ratio_range = p.capitalize_first(step_ratio_range) | ||
return step_ratio_range | return step_ratio_range, step_ratios | ||
end | end | ||
| Line 89: | Line 98: | ||
local modal_union = mos.modal_union(input_mos) | local modal_union = mos.modal_union(input_mos) | ||
local scale_sig = mos.as_string(input_mos) | local scale_sig = mos.as_string(input_mos) | ||
-- Sort/preprocess step ratios | |||
local step_ratio_range = "" | |||
step_ratio_range, step_ratios = p.preprocess_step_ratios(step_ratios) | |||
-- Create table | -- Create table | ||
| Line 94: | Line 107: | ||
-- Table caption | -- Table caption | ||
result = result .. string.format("|+ style=\"font-size: 105%%; white-space: nowrap;\" | %s of %s\n", step_ratio_range, scale_sig) | result = result .. string.format("|+ style=\"font-size: 105%%; white-space: nowrap;\" | %s of %s\n", step_ratio_range, scale_sig) | ||
| Line 170: | Line 182: | ||
function p.tester() | function p.tester() | ||
return | return p._mos_tunings() | ||
end | end | ||
return p | return p | ||