Module:MOS tunings: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
ArrowHead294 (talk | contribs)
mNo edit summary
 
(131 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local et = require("Module:ET")
local jira = require("Module:JI ratios")
local mos = require("Module:MOS")
local mos = require("Module:MOS")
local tamnams = require("Module:TAMNAMS")
local tamnams = require("Module:TAMNAMS")
local et = require("Module:ET")
local tip = require("Module:Template input parse")
local rat = require("Module:Rational")
local yesno = require("Module:Yesno")
local yesno = require("Module:Yesno")
local tip = require("Module:Template input parse")
local p = {}


-- Rewritten/simplified module replacement for Module:MOS degrees
-- Rewritten/simplified module replacement for Module:MOS degrees
Line 12: Line 13:


-- TODO:
-- TODO:
-- - JI ratio search and input
-- - Relegate JI ratio input to sorting entered ratios; this is to decouple
-- - Diatonic interval category lookup?!!
--  ratio search from ratio sorting/filtering.
-- - Diatonic interval category lookup?


-- Helper function
-- Helper function
Line 47: Line 49:
end
end
return step_ratios
return step_ratios
end
-- Find JI ratios up to an integer limit via mediants
function p.ji_ratio_search(integer_limit)
local SEARCH_MAX = 120
local integer_limit = integer_limit or SEARCH_MAX
integer_limit = math.max(0, math.min(SEARCH_MAX, integer_limit))
local ratios = {{1,1}, {2,1}}
local new_ratios_added = true
while new_ratios_added do
new_ratios_added = false
local new_ratios = {}
for i = 1, #ratios-1 do
local ratio_1 = ratios[i]
local ratio_2 = ratios[i+1]
local mediant = {ratio_1[1]+ratio_2[1], ratio_1[2]+ratio_2[2]}
local int_max = math.max(mediant[1], mediant[2])
table.insert(new_ratios, ratios[i])
if int_max <= integer_limit then
new_ratios_added = true
table.insert(new_ratios, mediant)
end
end
table.insert(new_ratios, ratios[#ratios])
ratios = new_ratios
end
return ratios
end
end


Line 95: Line 65:
and step_ratios[2][1] == 2 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] == 1 then
and step_ratios[3][1] == 3 and step_ratios[3][2] == 1 then
return "Simple Tunings", {{2,1}, {3,1}, {3,2}}
return "Simple Tunings", {{2, 1}, {3, 1}, {3, 2}}
end
end
end
end
Line 127: Line 97:


-- Preprocess step ratios
-- Preprocess step ratios
function p.preprocess_ji_ratios(input_mos, modal_union, step_ratios, ji_ratios)
function p.preprocess_ji_ratios(input_mos, modal_union, step_ratios, ji_ratios, tolerance)


-- Calculate the avegrage step ratio
local avg_step_ratio = {0, 0}
local avg_step_ratio = {0, 0}
for i = 1, #step_ratios do
for i = 1, #step_ratios do
Line 136: Line 107:
avg_step_ratio[1] = avg_step_ratio[1] / #step_ratios
avg_step_ratio[1] = avg_step_ratio[1] / #step_ratios
avg_step_ratio[2] = avg_step_ratio[2] / #step_ratios
avg_step_ratio[2] = avg_step_ratio[2] / #step_ratios
avg_step_ratio[1] = avg_step_ratio[1] / avg_step_ratio[2]
 
avg_step_ratio[2] = avg_step_ratio[2] / avg_step_ratio[2]
-- Normalize step ratio to be x:1, accounting for 1:0
if avg_step_ratio[2] ~= 0 then
avg_step_ratio[1] = avg_step_ratio[1] / avg_step_ratio[2]
avg_step_ratio[2] = avg_step_ratio[2] / avg_step_ratio[2]
else
avg_step_ratio[1] = 1
avg_step_ratio[2] = 0
end
-- Calculate the tolerance, the range in which ratios can be accepted +/-
-- from an et-step. (ET may be a non-integer value, since the L:s ratio is
-- normalized to x:1.)
-- Tolerance is how many cents away from an et-step a ratio can be. This is
-- by default 30% of the small step size, and maxes out at 30 cent. Can be
-- overridden with a custom tolerance value.
local steps_in_et = input_mos.nL * avg_step_ratio[1] + input_mos.ns * avg_step_ratio[2]
local tolerance = tolerance or math.min((mos.equave_to_cents(input_mos) / steps_in_et) * 0.30, 30)
-- Calculate the cent values for each interval in the modal union
local cent_values = {}
local cent_values = {}
for i = 1, #modal_union do
for i = 1, #modal_union do
Line 144: Line 132:
end
end
local tolerance = (rat.cents(input_mos.equave) / (input_mos.nL * avg_step_ratio[1] + input_mos.ns * avg_step_ratio[2])) * 0.15
local sorted_ratios = jira.sort_by_closeness_to_cent_values(ji_ratios, cent_values, tolerance)
local sorted_ratios = {}
local curr_index = 1 -- Index of current_ratio
for i = 1, #cent_values do
local lower_bound = cent_values[i] - tolerance
local upper_bound = cent_values[i] + tolerance
local cents_within_range = true
local curr_ratios = {}
for j = curr_index, #ji_ratios do
local curr_ratio = ji_ratios[j]
local curr_cents = math.log(curr_ratio[1]/curr_ratio[2])/math.log(2) * 1200
if lower_bound < curr_cents and curr_cents < upper_bound then
table.insert(curr_ratios, curr_ratio)
--elseif curr_cents > upper_bound then
-- curr_index = j
-- break
end
end
table.insert(sorted_ratios, curr_ratios)
end
return sorted_ratios
return sorted_ratios
end
end


-- Main function
-- Main function
function p._mos_tunings(input_mos, mos_prefix, mos_abbrev, step_ratios, ji_ratios)
function p._mos_tunings(input_mos, mos_prefix, mos_abbrev, step_ratios, ji_ratios, tolerance, footnotes, is_collapsed)
local input_mos = input_mos or mos.new(5,2)
local input_mos = input_mos or mos.new(5,2)
local mos_prefix = mos_prefix or "mos"
local mos_prefix = mos_prefix or "mos"
local mos_abbrev = mos_abbrev or "m"
local mos_abbrev = mos_abbrev or "m"
local step_ratios = step_ratios or { {2,1}, {3,1}, {3,2} }
local step_ratios = step_ratios or {{2, 1}, {3, 1}, {3, 2}}
local ji_ratios = ji_ratios or p.ji_ratio_search(30)
local ji_ratios = ji_ratios or {}
local tolerance = tolerance or nil
local is_collapsed = is_collapsed == true
local footnotes = footnotes or "(footnotes here)"
local modal_union = mos.modal_union(input_mos)
-- Scalesig
local scale_sig = mos.as_string(input_mos)
local scale_sig = mos.as_string(input_mos)
Line 187: Line 156:
-- Preprocess JI ratios
-- Preprocess JI ratios
local sorted_ji_ratios = p.preprocess_ji_ratios(input_mos, modal_union, step_ratios, ji_ratios)
local modal_union = mos.modal_union(input_mos)
--local sorted_ji_ratios, search_info = p.preprocess_ji_ratios(input_mos, modal_union, step_ratios, ji_ratios, tolerance)
-- Create table
-- Create table
local result = "{| class=\"wikitable sortable right-all left-1 left-2\"\n"
local result = "{| class=\"wikitable sortable right-all left-1 left-2 mw-collapsible" .. (is_collapsed and " mw-collapsed\"\n" or "\"\n")
-- 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 .. "|+ style=\"font-size: 105%; white-space: nowrap;\" | " .. string.format("%s of %s\n", step_ratio_range, scale_sig)
.. "|-\n"
-- First row of headers
-- First row of headers
-- First two headers span two rows
-- First two headers span two rows
result = result .. "! rowspan=\"2\" class=\"unsortable\" | Scale degree\n"
result = result
result = result .. "! rowspan=\"2\" class=\"unsortable\" | Abbrev.\n"
.. "! rowspan=\"2\" class=\"unsortable\" | Scale degree\n"
.. "! rowspan=\"2\" class=\"unsortable\" | Abbrev.\n"
-- Headers for tunings; these span two cols
-- Headers for tunings; these span two cols
Line 209: Line 181:
end
end
local et_as_string = et.as_string(mos.mos_to_et(input_mos, step_ratios[i]))
local et_as_string = et.as_string(mos.as_et(input_mos, step_ratios[i]))
local header_text = string.format("%s<br>[[%s]]", step_ratio_as_text, et_as_string)
local header_text = string.format("%s<br />[[%s]]", step_ratio_as_text, et_as_string)
result = result .. string.format("! colspan=\"2\" | %s\n", header_text)
result = result .. string.format("! colspan=\"2\" | %s\n", header_text)
end
end
-- Headers for JI ratios; this spans two rows
-- Headers for JI ratios; this spans two rows
result = result .. "! rowspan=\"2\" class=\"unsortable\" | Approx.<br>Ratios\n"
-- Commented out, pending rewrite
--[[
if #ji_ratios ~= 0 then
result = result .. "! rowspan=\"2\" class=\"unsortable\" | Approx. ratios*\n"
end
]]--
result = result .. "|-\n"
result = result .. "|-\n"
-- Second row of headers
-- Second row of headers
for i = 1, #step_ratios do
for i = 1, #step_ratios do
result = result .. "! Steps\n"
result = result .. "! style=\"border-right: none;\" class=\"unsortable\" | Steps\n"
result = result .. "! Cents\n"
result = result .. "! style=\"border-left: none; text-align: right;\" | ¢\n"
end
end
Line 234: Line 211:
result = result
result = result
.. "|-\n"
.. "|-\n"
.. string.format("| %s || %s", degree_name, degree_abbrev)
.. string.format("| %s\n", degree_name)
.. string.format("| %s\n", degree_abbrev)
-- Add cells for each interval's tunings
-- Add cells for each interval's tunings
Line 243: Line 221:
result = result
result = result
.. " "
--.. string.format("\n| %s\\%s\n| %.1f", step_count, input_mos.nL * step_ratio[1] + input_mos.ns * step_ratio[2], cents)
.. string.format("|| %s\\%s || %.1f", step_count, input_mos.nL * step_ratio[1] + input_mos.ns * step_ratio[2], cents)
.. string.format("| style=\"border-right: none;\" | %s\\%s\n", step_count, input_mos.nL * step_ratio[1] + input_mos.ns * step_ratio[2])
.. string.format("| style=\"border-left: none;\" | %.1f\n", cents)
end
end
-- Add cells for JI ratios
-- Add cells for JI ratios
-- TESTING: print only the unison and equave
--[[
local ratios = ""
-- Commented out, pending rewrite
for j = 1, #sorted_ji_ratios[i] do
if #ji_ratios ~= 0 then
if j ~= #sorted_ji_ratios[i] then
-- Ratios link to their respective pages, and are comma-delimited.
ratios = ratios .. string.format("%s/%s, ", sorted_ji_ratios[i][j][1], sorted_ji_ratios[i][j][2])
local ratios_as_text = jira.ratios_as_string(sorted_ji_ratios[i], true, ",&nbsp;")
else
result = result .. "| style=\"text-align: left;\" | " .. string.format("%s\n", ratios_as_text)
ratios = ratios .. string.format("%s/%s", sorted_ji_ratios[i][j][1], sorted_ji_ratios[i][j][2])
end
end
end
result = result .. string.format("|| %s", ratios)
]]--
result = result .. "\n"
end
end
-- End of table
-- End of table, plus footnotes
result = result .. "|}"
result = result .. "|}"
--[[
-- Commented out, pending rewrite
if #ji_ratios ~= 0 then
-- Make footnote text smaller than the rest of the text to avoid confusion with paragraph text
result = result .. string.format("<span style=\"font-size: 0.75em;\">&#42; %s</span>", footnotes)
end
]]--
return result
return result
end
-- Parse step ratios passed into template
-- If the unparsed string is blank, default to the simple tunings.
-- If the unparsed string is any of the step ratio range names, list the named
-- ratios that fall within that range.
-- if the unparsed string is blank, don't show any ratios.
-- If the ratios is a list, parse it.
function p.parse_step_ratios(unparsed)
local parsed = {}
local lookup_table = {
["Central Spectrum"] = {{4, 3}, {3, 2}, {5, 3}, {2, 1}, {5, 2}, {3, 1}, {4, 1}},
["Simple Tunings"]  = {{2, 1}, {3, 1}, {3, 2}},
["Soft-of-basic"]    = {{4, 3}, {3, 2}, {2, 1}},
["Ultrasoft"]        = {{6, 5}, {5, 4}, {4, 3}},
["Parasoft"] = {{4, 3}, {7, 5}, {3, 2}},
["Quasisoft"]        = {{3, 2}, {8, 5}, {5, 3}},
["Minisoft"]        = {{5, 3}, {7, 4}, {2, 1}},
["Hyposoft"]        = {{3, 2}, {5, 3}, {2, 1}},
["Hypohard"]        = {{2, 1}, {5, 2}, {3, 1}},
["Minihard"]        = {{2, 1}, {7, 3}, {5, 2}},
["Quasihard"]        = {{5, 2}, {8, 3}, {3, 1}},
["Parahard"]        = {{3, 1}, {7, 2}, {4, 1}},
["Ultrahard"]        = {{4, 1}, {5, 1}, {6, 1}},
["Hard-of-basic"]    = {{2, 1}, {3, 1}, {4, 1}},
}
if unparsed == "" then
parsed = lookup_table["Simple Tunings"]
elseif unparsed == "NONE" then
parsed = {}
else
parsed = lookup_table[unparsed] or tip.parse_numeric_pairs(unparsed)
end
return parsed
end
-- Parse JI ratios passed into template
-- If the unparsed string corresponds to a list of JI ratios ("a/b; c/d; e/f"),
-- then parse it as a list of ratios. If it's not that, parse it as search
-- args. If the text is "NONE", then there should be no ratios passed in.
-- If the unparsed string is an empty string, return nil. (This is so the
-- wrapper function can go by default search args.)
function p.parse_ji_ratios(unparsed, equave)
local ratios = nil
local search_args = nil
if unparsed == "" then
search_args = {["Int Limit"] = 50, ["Tenney Height"] = 8; ["Complements Only"] = true} -- Defualt search args if no args were passed in
ratios = jira.search_by_args_within_equave(equave, search_args)
elseif unparsed == "NONE" then
search_args = {}
ratios = {}
elseif string.match(unparsed, "Int Limit:") then
search_args = jira.parse_search_args(unparsed) -- Search requires at the absolute least an int limit, so see if there's "Int Limit"
ratios = jira.search_by_args_within_equave(equave, search_args)
else
search_args = {}
ratios = jira.parse_ratios(unparsed)
end
return ratios, jira.search_footnotes(search_args)
end
end


Line 271: Line 317:
function p.mos_tunings(frame)
function p.mos_tunings(frame)
-- Get params
-- Get params
local scalesig   = frame.args["Scale Signature"]
local scalesig     = frame.args["Scale Signature"]
local mos_prefix = frame.args["MOS Prefix"]
local input_mos    = mos.parse(scalesig)
local mos_abbrev = frame.args["MOS Abbrev"]
local mos_prefix   = tamnams.verify_prefix(input_mos, frame.args["MOS Prefix"])
local collapsed  = yesno(frame.args["Collapsed"], false) -- Currently does nothing
local mos_abbrev   = tamnams.verify_abbrev(input_mos, frame.args["MOS Abbrev"])
local step_ratios = tip.parse_numeric_pairs(frame.args["Step Ratios"]) or (frame.args["Step Ratios"] == "Central Spectrum" and {{4,3}, {3,2}, {5,3}, {2,1}, {5,2}, {3,1}, {4,1}} or {{2,1}, {3,1}, {3,2}})
local is_collapsed = yesno(frame.args["Collapsed"], false)
local ji_ratios  = tip.parse_numeric_pairs(frame.args["JI Ratios"] ) or {{1,1}, {3,2}, {2,1}}
local step_ratios = p.parse_step_ratios(frame.args["Step Ratios"])
local tolerance    = tonumber(frame.args["Tolerance"])
local debugg      = yesno(frame.args["debug"])
-- Parse scalesig
--local ji_ratios, footnotes = p.parse_ji_ratios(frame.args["JI Ratios"], input_mos.equave)
local input_mos = mos.parse(scalesig)
 
local result = p._mos_tunings(input_mos, mos_prefix, mos_abbrev, step_ratios, ji_ratios, tolerance, footnotes, is_collapsed)
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
-- Verify name/prefix/abbrev
return frame:preprocess(result)
mos_prefix = tamnams.verify_prefix(input_mos, mos_prefix)
mos_abbrev = tamnams.verify_abbrev(input_mos, mos_abbrev)
 
return p._mos_tunings(input_mos, mos_prefix, mos_abbrev, step_ratios)
end
end


function p.tester()
function p.tester()
local range, ratios = p.preprocess_step_ratios({ {7,1}, {3,1}, {2,1} })
local range, ratios = p.preprocess_step_ratios({{7, 1}, {3, 1}, {2, 1}})
local ji_ratios = p.ji_ratio_search(50)
local input_mos = mos.parse("9L 4s<7/2>")
return p.preprocess_ji_ratios(mos.new(5,2), mos.modal_union(mos.new(5,2)), {{3,2}}, ji_ratios)
--return p.preprocess_ji_ratios(input_mos, mos.modal_union(input_mos), {{2,1}, {3,2}, {5,3}}, ji_ratios)
--return ji_ratios
return p._mos_tunings(input_mos)
end
end


return p
return p