Module:MOS tunings: Difference between revisions

Ganaram inukshuk (talk | contribs)
m update todo
Ganaram inukshuk (talk | contribs)
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.preprocess_step_ratio_range(step_ratios)
function p.sort_step_ratios(step_ratios)
local step_ratios = step_ratios or { {6,1} }
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] == 2 and step_ratios[1][2] == 1
if step_ratios[1][1] == 3 and step_ratios[1][2] == 2
and step_ratios[2][1] == 3 and step_ratios[2][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] == 2 then
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 smallest and largest ratios
-- If there are multiple step ratios, find the step ratio range it
-- and find the step ratio range. If there's only one step ratio, then only
-- corresponds to. If there is one step ratio, find the name of that
-- find the step ratio name for that ratio, if there is one. If there are  
-- hardness. If there are zero step ratios, then return "Tunings"
-- zero step ratios, then it's a table of scale degrees.
if #step_ratios > 1 then
if #step_ratios > 1 then
local index_of_lowest  = -1
step_ratio_range = (tamnams.lookup_step_ratio_range(step_ratios[1], step_ratios[#step_ratios]) .. " Tunings") or "Tunings"
local index_of_highest = -1
local lowest_hardness  =  1/0
local highest_hardness = -1/0
-- Update lowest step ratio
for i = 1, #step_ratios do
local current_hardness = step_ratios[i][1] / step_ratios [i][2]
-- If the current hardness is smaller than the lowest recorded
-- hardness, update. If the current hardness is larger than the
-- highest recorded hardness, update.
if current_hardness < lowest_hardness then
lowest_hardness = current_hardness
index_of_lowest = i
end
if current_hardness > highest_hardness then
highest_hardness = current_hardness
index_of_highest = i
end
end
step_ratio_range = (tamnams.lookup_step_ratio_range(step_ratios[index_of_lowest], step_ratios[index_of_highest]) .. " Tunings") or "Tunings"
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
local step_ratio_range = p.preprocess_step_ratio_range(step_ratios)
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 tip.parse_numeric_pairs("")
return p._mos_tunings()
end
end


return p
return p