Module:MOS tunings: Difference between revisions
m todo |
Added step ratio entry |
||
| Line 3: | Line 3: | ||
local et = require("Module:ET") | local et = require("Module:ET") | ||
local yesno = require("Module:Yesno") | local yesno = require("Module:Yesno") | ||
local tip = require("Module:Template input parse") | |||
local p = {} | local p = {} | ||
-- Rewritten/simplified module replacement for Module:MOS degrees | -- Rewritten/simplified module replacement for Module:MOS degrees | ||
-- A new template is chosen because it's a better name than the old one | -- A new template is chosen because it's a better name than the old one and is | ||
-- far easier to maintain than the old one. | |||
-- TODO: | -- TODO: | ||
| Line 15: | Line 17: | ||
-- - JI ratio input?? | -- - JI ratio input?? | ||
-- - Diatonic interval category lookup?!! | -- - Diatonic interval category lookup?!! | ||
-- Helper function | |||
-- Capitalizes the first character of a string | |||
function p.capitalize_first(text) | |||
return string.upper(string.sub(text, 1, 1)) .. string.sub(text, 2, -1) | |||
end | |||
function p.preprocess_step_ratio_range(step_ratios) | |||
local step_ratios = step_ratios or { {6,1} } | |||
local step_ratio_range = "" | |||
-- 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 | |||
if #step_ratios == 3 then | |||
if step_ratios[1][1] == 2 and step_ratios[1][2] == 1 | |||
and step_ratios[2][1] == 3 and step_ratios[2][2] == 1 | |||
and step_ratios[3][1] == 3 and step_ratios[3][2] == 2 then | |||
return "Simple Tunings" | |||
end | |||
end | |||
-- If there are multiple step ratios, find the smallest and largest ratios | |||
-- and find the step ratio range. If there's only one step ratio, then only | |||
-- find the step ratio name for that ratio, if there is one. If there are | |||
-- zero step ratios, then it's a table of scale degrees. | |||
if #step_ratios > 1 then | |||
local index_of_lowest = -1 | |||
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]) or "Tunings" | |||
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" | |||
else | |||
step_ratio_range = "Tunings" | |||
end | |||
-- Capitalize first letter | |||
step_ratio_range = p.capitalize_text(step_ratio_range) | |||
return step_ratio_range | |||
end | |||
-- Main function | -- Main function | ||
| Line 30: | Line 92: | ||
-- Table caption | -- Table caption | ||
result = result .. string.format("|+ style=\"font-size: 105%%; white-space: nowrap;\" | | 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", scale_sig, step_ratio_range) | |||
-- First row of headers | -- First row of headers | ||
| Line 40: | Line 103: | ||
for i = 1, #step_ratios do | for i = 1, #step_ratios do | ||
local step_ratio_as_text = tamnams.lookup_step_ratio(step_ratios[i]) or string.format("%s:%s", step_ratios[i][1], step_ratios[i][2]) | local step_ratio_as_text = tamnams.lookup_step_ratio(step_ratios[i]) or string.format("%s:%s", step_ratios[i][1], step_ratios[i][2]) | ||
step_ratio_as_text = p.capitalize_text(step_ratio_as_text) | |||
local et_as_string = et.as_string(mos.mos_to_et(input_mos, step_ratios[i])) | local et_as_string = et.as_string(mos.mos_to_et(input_mos, step_ratios[i])) | ||
| Line 91: | Line 155: | ||
local mos_abbrev = frame.args["MOS Abbrev"] | local mos_abbrev = frame.args["MOS Abbrev"] | ||
local collapsed = yesno(frame.args["Collapsed"], false) -- Currently does nothing | local collapsed = yesno(frame.args["Collapsed"], false) -- Currently does nothing | ||
local step_ratios = tip.parse_numeric_pairs(frame.args["Step Ratios"]) | |||
-- Parse scalesig | -- Parse scalesig | ||
| Line 100: | Line 165: | ||
return p._mos_tunings(input_mos, mos_prefix, mos_abbrev) | return p._mos_tunings(input_mos, mos_prefix, mos_abbrev) | ||
end | |||
function p.tester() | |||
return tip.parse_numeric_pairs("1/2; 3/4; 5/6") | |||
end | end | ||
return p | return p | ||