Module:MOS tuning spectrum: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
mNo edit summary
 
(11 intermediate revisions by the same user not shown)
Line 5: Line 5:
local yesno    = require("Module:yesno")
local yesno    = require("Module:yesno")
local getArgs  = require("Module:Arguments").getArgs
local getArgs  = require("Module:Arguments").getArgs
local p = {}
local p = {}


-- TODO:
-- TODO:
-- - Add back old option from previous scale tree: toggle staggering of ratios.
-- - (Low priority): Force-add comments that lie outside searched depth. If such
-- - (Low priority): Force-add comments that lie outside searched depth. If such
--  ratios are added, they're added in either the last column or their own
--  ratios are added, they're added in either the last column or their own
--  column appended after the last one.
--  column appended after the last one. (to be determined)
 
-- Split comments into in-range and out-of-range comments.
-- Iterate through the comments and determine whether the key for a comment is
-- in the table of step ratios. If so, add the corresponding key-value pair into
-- the table of in-comments; if not, add to the table of out-comments.
function p.preprocess_comments(comments, step_ratios, depths)
local in_comments  = {}
local out_comments = {}
local out_ratios  = {}
for key, value in pairs(comments) do
local key_found_in_step_ratios = utils.table_contains(step_ratios, key, function(key, ratio)
return key == string.format("%s/%s", ratio[1], ratio[2]) end
)
if key_found_in_step_ratios then
in_comments[key] = value
else
out_comments[key] = value
table.insert(out_ratios, tip.parse_numeric_pair(key, "/", true))
end
end
mediants.sort_ratios(out_ratios)
return in_comments, out_comments, out_ratios
end


-- Re-re-rewrite of tuning spectrum
-- Re-re-rewrite of tuning spectrum
Line 19: Line 47:
default_ratios, default_depths = mediants.find_mediants({{1,1}, {1,0}}, default_depth)
default_ratios, default_depths = mediants.find_mediants({{1,1}, {1,0}}, default_depth)
-- Extract info from args
local input_mos  = args["Input MOS"  ] or mos.new(5, 2)
local input_mos  = args["Input MOS"  ] or mos.new(5, 2)
local comments    = args["Comments"  ] or {}
local comments    = args["Comments"  ] or {}
local step_ratios = args["Step Ratios"] or default_ratios
local step_ratios = args["Step Ratios"] or default_ratios
local depths      = args["Depths"    ] or default_depths
local depths      = args["Depths"    ] or default_depths
local deepest    = mediants.deepest_depth(depths)
-- Find the deepest searched depth at which ratios were found
local deepest = mediants.deepest_depth(depths)
-- Find mos info
local equave      = input_mos.equave
local equave      = input_mos.equave
local large_steps = input_mos.nL -- Large steps in mos
local large_steps = input_mos.nL -- Large steps in mos
local small_steps = input_mos.ns -- Small steps in mos
local small_steps = input_mos.ns -- Small steps in mos
local periods    = mos.period_count(input_mos) -- Number of periods
local periods    = mos.period_count(input_mos) -- Number of periods
local et_suffix  = mos.et_suffix(input_mos) -- Mos's ET suffix (edo, edt, edf, ed-p/q, etc)
-- What is the et suffix (edo, edt, edf, ed-p/q)?
local et_suffix = mos.et_suffix(input_mos)
-- Default comments for TAMNAMS-named step ratios
-- Default comments for TAMNAMS-named step ratios
Line 67: Line 97:
-- - Step ratio and hardness
-- - Step ratio and hardness
-- - Comments
-- - Comments
local result = "{| class=\"wikitable center-all\"\n"
local result = {}
.. "|+ style=\"font-size: 105%; white-space: nowrap;\" | " .. string.format("Scale tree and tuning spectrum of %s\n", mos_as_string)
table.insert(result, '{| class="wikitable center-all"')
.. "|-\n"
table.insert(result, '|+ style="font-size: 105%; white-space: nowrap;" | Scale tree and tuning spectrum of ' ..  mos_as_string)
.. string.format("! rowspan=\"2\" colspan=\"%d\" | Generator<sup><abbr title=\"In steps of %s.\">(%s)</abbr></sup>\n", deepest + 1, et_suffix, et_suffix)
table.insert(result, '|-')
.. "! colspan=\"2\" | Cents\n"
table.insert(result, string.format('! rowspan="2" colspan="%d" | Generator<sup><abbr title="In steps of %s.">(%s)</abbr></sup>', deepest + 1, et_suffix, et_suffix))
.. "! colspan=\"2\" | Step ratio\n"
table.insert(result, '! colspan="2" | Cents')
.. "! rowspan=\"2\" | " .. comments_header_text .. "\n"
table.insert(result, '! colspan="2" | Step ratio')
.. "|-\n"
table.insert(result, string.format('! rowspan="2" | %s', comments_header_text))
.. "! Bright\n"
table.insert(result, '|-')
.. "! Dark\n"
table.insert(result, '! <abbr title="Chroma-positive generator">Bright</abbr>')
.. "! L:s\n"
table.insert(result, '! <abbr title="Chroma-negative generator">Dark</abbr>')
.. "! Hardness\n"
table.insert(result, '! L:s')
table.insert(result, '! Hardness')


-- Rounding is done using string.format, to 3 decimal places (%.3f)
-- Rounding is done using string.format, to 3 decimal places (%.3f)
Line 84: Line 115:
-- Create each row of the table
-- Create each row of the table
for i = 1, #step_ratios do
for i = 1, #step_ratios do
local step_ratio   = step_ratios[i]
-- Simplify step ratio before using it
local step_ratio = mediants.simplify_ratio(step_ratios[i])
-- Produce current ET as a string
local et_as_string = mos.et_string(input_mos, step_ratio)
local et_as_string = mos.et_string(input_mos, step_ratio)
Line 97: Line 131:
-- New row
-- New row
result = result .. "|-\n"
table.insert(result, "|-")
-- Cells for bright generator, as steps in et
-- Cells for bright generator, as steps in et
local current_depth = depths[i] + 1
-- Bright gen cell is accompanied by cells to the left and right.
for i = 1, deepest + 1 do
-- The variable current_depth is the number of left cells; the number of
result = result .. "| "
-- right cells is deepest minus current_depth.
if i == current_depth then
local current_depth = depths[i]
result = result .. string.format("[[%s|%s]]", et_as_string, bright_generator_string)
local num_right_cells = deepest - current_depth
end
for i = 1, current_depth do
result = result .. "\n"
table.insert(result, "|")
end
table.insert(result, string.format("| [[%s|%s]]", et_as_string, bright_generator_string))
for i = 1, num_right_cells do
table.insert(result, "|")
end
end
-- Cells for generators in cents
-- Cells for generators in cents
result = result .. string.format("| %.3f\n", bright_generator_cents)
table.insert(result, string.format("| %.3f", bright_generator_cents))
result = result .. string.format("| %.3f\n", dark_generator_cents  )
table.insert(result, string.format("| %.3f", dark_generator_cents  ))
-- Cell for step ratio
-- Cell for step ratio
result = result .. string.format("| %d:%d\n", step_ratio[1], step_ratio[2])
table.insert(result, string.format("| %d:%d", step_ratio[1], step_ratio[2]))
-- Cell for hardness, with divide-by-zero check
-- Cell for hardness, with divide-by-zero check
Line 123: Line 161:
hardness = string.format("%.3f", step_ratio[1] / step_ratio[2])
hardness = string.format("%.3f", step_ratio[1] / step_ratio[2])
end
end
result = result .. string.format("| %s\n", hardness)
table.insert(result, string.format("| %s", hardness))
-- The step ratio is (literally and figuratively) the key to add comments!
-- Before using it, simplify it
local large_step = step_ratio[1]
local small_step = step_ratio[2]
local gcd = utils.gcd(large_step, small_step)
local key = large_step / gcd .. "/" .. small_step / gcd
-- Cell for comment
-- Cell for comment
-- Default comments are on their own line before custom comments
-- Default comments are on their own line before custom comments
local key = step_ratio[1] .. "/" .. step_ratio[2] -- The step ratio is (literally and figuratively) the key to add comments!
local comment = ""
local comment = ""
local default_comment = default_comments[key] or ""
local default_comment = default_comments[key] or ""
Line 142: Line 174:
comment = default_comment .. "<br />" .. custom_comment
comment = default_comment .. "<br />" .. custom_comment
end
end
result = result .. string.format("| style=\"text-align: left;\" | %s\n", comment)
table.insert(result, string.format("| style=\"text-align: left;\" | %s", comment))
end
end
result = result .. "|}"
table.insert(result, "|}")
return result
return table.concat(result, "\n")
end
end


Line 175: Line 207:
local initial_ratios = {}
local initial_ratios = {}
if initial_ratios_unparsed ~= nil then
if initial_ratios_unparsed ~= nil then
initial_ratios = tip.parse_numeric_pairs(initial_ratios_unparsed, ",", "/", true)
-- Parse initial ratios, then sort by ascending
initial_ratios = tip.parse_numeric_pairs(initial_ratios_unparsed, ";", "/", true)
mediants.sort_ratios(initial_ratios)
else
else
-- If no initial ratios were passed in, default to 1/1 and 1/0
initial_ratios = {{1,1}, {1,0}}
initial_ratios = {{1,1}, {1,0}}
end
end
Line 183: Line 218:
-- If using depth-based search, parse the depth, then remove it from args
-- If using depth-based search, parse the depth, then remove it from args
-- If using int-limit search, parse the int limit, then remove it from args,
-- If using int-limit search, parse the int limit, then remove it from args,
-- and remove depth for good measure
-- and remove depth in case it was present.
local step_ratios, depths
local step_ratios, depths
local use_int_limit = args["Int Limit"] ~= nil
local use_int_limit = args["Int Limit"] ~= nil
Line 200: Line 235:
step_ratios, depths = mediants.find_mediants(initial_ratios, depth)
step_ratios, depths = mediants.find_mediants(initial_ratios, depth)
end
end
args["Depth"] = nil
args["Depth"     ] = nil
args["Step Ratios"] = step_ratios
args["Step Ratios"] = step_ratios
args["Depths"] = depths
args["Depths"     ] = depths
-- Transfer comments from args to comments
-- Transfer comments from args to comments
Line 228: Line 263:


function p.tester()
function p.tester()
--local args = {}
--return p._mos_tuning_spectrum(args)
local comments = {
["5/4"] = "aaa",
["2/1"] = "bbb"
}
local step_ratios = {
{1,1},
{5,4},
{4,3}
}
local in_comments = {}
local out_comments = {}
in_comments, out_comments, out_ratios = p.preprocess_comments(comments, step_ratios)
local args = {}
local args = {}
--return out_comments
return p._mos_tuning_spectrum(args)
return p._mos_tuning_spectrum(args)
end
end


return p
return p