Module:MOS genchain

From Xenharmonic Wiki
Jump to navigation Jump to search

Documentation transcluded from /doc
Note: Do not invoke this module directly; use the corresponding template instead: Template:MOS genchain.
Icon-Todo.png Todo: Documentation

local mos = require("Module:MOS")
local tamnams = require("Module:TAMNAMS")
local yesno = require("Module:Yesno")
local p = {}

-- TODO
-- - Cell coloring
-- - Mosprefix and mosabbrev entering
-- - Collapse option

-- Global variables for cell colors
-- Colors are as follows:
-- - Orange and blue for small and large sizes, respectively
-- - Darker colors for altered scale degrees
-- - No color for period intervals
p.cell_color_none = "NONE"				-- For cells that don't have a color (default cell color applies)
p.cell_color_perfect_size = "NONE"		-- Only applies for periods, including the root and equave
p.cell_color_lg_altered_size = "#BDD7EE"
p.cell_color_large_size      = "#DDEBF7"
p.cell_color_small_size      = "#FCE4D6"
p.cell_color_sm_altered_size = "#F8CBAD"

function p.preprocess_genchain(input_mos)
	local input_mos = input_mos or mos.new(5,2)
	local num_gens = (2 * (input_mos.nL + input_mos.nL + input_mos.ns))/mos.period_count(input_mos) - 2
	
	local bright_gen = mos.bright_gen(input_mos)
	local gens = mos.interval_mul(bright_gen, math.floor(num_gens/2))
	bright_gen = mos.interval_mul(bright_gen, -1)
	local genchain = { mos.period_reduce(gens, input_mos) }
	for i = 1, num_gens do
		gens = mos.interval_add(gens, bright_gen)
		table.insert(genchain, mos.period_reduce(gens, input_mos))
	end
	return genchain
end

function p._mos_genchain(input_mos, mos_prefix, mos_abbrev, is_collapsed)
	local input_mos = input_mos or mos.new(5,2)
	local mos_prefix = mos_prefix or "mos"
	local mos_abbrev = mos_abbrev or "m"
	local is_collapsed = is_collapsed == true
	
	local num_gens = mos.period_step_count(input_mos)
	
	local genchain = p.preprocess_genchain(input_mos)
	local period_count = mos.period_count(input_mos)
	
	local scale_sig = mos.as_string(input_mos)
	
	-- Begin table
	local result = "{| class=\"wikitable mw-collapsible" .. (is_collapsed and " mw-collapsed\"\n" or "\"\n")
	
	-- Table caption
	result = result .. "|+ style=\"font-size: 105%; white-space: nowrap;\" | " .. string.format("Generator chain of %s\n", scale_sig)

	-- Headers for each period
	result = result .. "! Bright gens"
	for i = 1, period_count do
		result = result
		.. " !! Scale Degree"
		.. " !! Abbrev."
	end
	result = result
		.. "\n"
		.. "|-\n"
		
	-- Add a row for each scale degree, plus their period-shifted counterparts
	for i = 1, #genchain do
		-- Number of generators
		local num_gens = math.ceil(#genchain/2) - i
		result = result .. string.format("| %s", num_gens)
		
		-- Scale degree (full name) and abbrev
		for j = 1, period_count do
			-- If the scale degree corresponds to the root, say it's both the
			-- root and the degree one period up.
			-- If the scale degree is reached by a negative number of gens, then
			-- say it's the degree one period up instead.
			local period_interval = mos.period(input_mos)
			local current_interval = mos.interval_add(genchain[i], mos.interval_mul(period_interval, j - 1))
			local period_raised_interval = mos.interval_add(current_interval, period_interval)
			if num_gens == 0 then
				result = result 
					.. string.format(" || %s<br />%s", tamnams.degree_quality(current_interval, input_mos, "sentence-case", mos_prefix), tamnams.degree_quality(period_raised_interval, input_mos, "sentence-case", mos_prefix))
					.. string.format(" || %s<br />%s", tamnams.degree_quality(current_interval, input_mos, "abbrev"       , mos_abbrev), tamnams.degree_quality(period_raised_interval, input_mos, "abbrev"       , mos_abbrev))
			elseif num_gens < 0 and mos.interval_step_count(current_interval) % mos.period_step_count(input_mos) == 0 then
				result = result 
					.. string.format(" || %s", tamnams.degree_quality(period_raised_interval, input_mos, "sentence-case", mos_prefix))
					.. string.format(" || %s", tamnams.degree_quality(period_raised_interval, input_mos, "abbrev"       , mos_abbrev))
			else
				result = result 
					.. string.format(" || %s", tamnams.degree_quality(current_interval, input_mos, "sentence-case", mos_prefix))
					.. string.format(" || %s", tamnams.degree_quality(current_interval, input_mos, "abbrev"       , mos_abbrev))
			end
		end
		
		-- End of row
		result = result 
			.. "\n"
			.. "|-\n"
	end
	result = result .. "|}"
	
	return result
end

function p.mos_genchain(frame)
	local scalesig = frame.args["Scale Signature"]
	local mos_prefix = frame.args["MOS Prefix"]
	local mos_abbrev = frame.args["MOS Abbrev"]
	local collapsed  = yesno(frame.args["Collapsed"])

	-- Parse scalesig
	local input_mos = mos.parse(scalesig)
	
	-- Verify name/prefix/abbrev
	mos_prefix = tamnams.verify_prefix(input_mos, mos_prefix)
	mos_abbrev = tamnams.verify_abbrev(input_mos, mos_abbrev)
	
	return p._mos_genchain(input_mos, mos_prefix, mos_abbrev, collapsed)
end

return p