Module:MOS tuning spectrum: Difference between revisions
code cleanup; adopt sorting function for when custom initial ratios are entered |
mNo edit summary |
||
| (6 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 69: | Line 97: | ||
-- - Step ratio and hardness | -- - Step ratio and hardness | ||
-- - Comments | -- - Comments | ||
local result = | local result = {} | ||
table.insert(result, '{| class="wikitable center-all"') | |||
table.insert(result, '|+ style="font-size: 105%; white-space: nowrap;" | Scale tree and tuning spectrum of ' .. mos_as_string) | |||
table.insert(result, '|-') | |||
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)) | |||
table.insert(result, '! colspan="2" | Cents') | |||
table.insert(result, '! colspan="2" | Step ratio') | |||
table.insert(result, string.format('! rowspan="2" | %s', comments_header_text)) | |||
table.insert(result, '|-') | |||
table.insert(result, '! <abbr title="Chroma-positive generator">Bright</abbr>') | |||
table.insert(result, '! <abbr title="Chroma-negative generator">Dark</abbr>') | |||
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 102: | Line 131: | ||
-- New row | -- New row | ||
result | table.insert(result, "|-") | ||
-- Cells for bright generator, as steps in et | -- Cells for bright generator, as steps in et | ||
local current_depth = depths[i] | -- Bright gen cell is accompanied by cells to the left and right. | ||
for i = 1, | -- The variable current_depth is the number of left cells; the number of | ||
result | -- right cells is deepest minus current_depth. | ||
local current_depth = depths[i] | |||
local num_right_cells = deepest - current_depth | |||
for i = 1, current_depth do | |||
result | 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 | table.insert(result, string.format("| %.3f", bright_generator_cents)) | ||
result | table.insert(result, string.format("| %.3f", dark_generator_cents )) | ||
-- Cell for step ratio | -- Cell for step ratio | ||
result | 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 128: | 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 | table.insert(result, string.format("| %s", hardness)) | ||
-- Cell for comment | -- Cell for comment | ||
| Line 141: | Line 174: | ||
comment = default_comment .. "<br />" .. custom_comment | comment = default_comment .. "<br />" .. custom_comment | ||
end | end | ||
result | table.insert(result, string.format("| style=\"text-align: left;\" | %s", comment)) | ||
end | end | ||
result | table.insert(result, "|}") | ||
return result | return table.concat(result, "\n") | ||
end | end | ||
| Line 230: | 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 | ||