Module:MOS intervals

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 intervals.
Icon-Todo.png Todo: Documentation

local mos = require("Module:MOS")
local rat = require("Module:Rational")
local tamnams = require("Module:TAMNAMS")
local yesno = require("Module:Yesno")
local getArgs = require("Module:Arguments").getArgs
local p = {}

-- Main function; to be called by wrapper
function p._mos_intervals(args)
	-- Default param for input mos is 5L 2s
	local input_mos    = args["Input MOS"   ] or mos.new(5, 2, 2)
	local mos_prefix   = args["MOS Prefix"  ] or "mos"
	local mos_abbrev   = args["MOS Abbrev"  ] or "m"
	local is_collapsed = args["Is Collapsed"] == true
	
	-- Get the scale sig
	local scale_sig = mos.as_string(input_mos)
	
	-- Get the brightest and darkest modes as step matrices
	local bright_step_matrix = mos.mode_to_step_matrix(mos.brightest_mode(input_mos))
	local dark_step_matrix = mos.mode_to_step_matrix(mos.darkest_mode(input_mos))
	
	-- Get the number of steps per period and equave
	local equave_step_count = mos.equave_step_count(input_mos)
	local period_step_count = mos.period_step_count(input_mos)
	
	-- Get the step counts for the bright and dark generators
	local bright_gen_step_count = mos.bright_gen_step_count(input_mos)
	local dark_gen_step_count = mos.dark_gen_step_count(input_mos)
	
	-- Create the table
	local result = '{| class="wikitable mw-collapsible' .. (is_collapsed and ' mw-collapsed"\n' or '"\n')
	
	-- Create table title
	result = result
		.. '|+ style="font-size: 105%; white-space: nowrap;" | Intervals of ' .. scale_sig .. '\n'
		.. '|-\n'
		
	-- Create table headers
	result = result
		.. '! colspan="3" | Intervals\n'
		.. '! rowspan="2" | Steps<br />subtended\n'
		.. '! rowspan="2" | Range in cents\n'
		.. '|-\n'	-- Start of second row of header cells
		.. '! Generic\n'
		.. '! Specific\n'
		.. '! Abbrev.\n'

	-- Write each row
	for i = 1, #bright_step_matrix do
		-- Compare the bright and dark intervals. If they're the same, then the
		-- current interval class is a period interval.
		local current_bright_interval = bright_step_matrix[i]
		local current_dark_interval = dark_step_matrix[i]
		local is_period = mos.interval_eq(current_bright_interval, current_dark_interval)
		
		-- If it's a period interval, then there is only one row to write.
		-- Otherwise, there are two rows to write, one for each size.
		if is_period then
			local cents = string.format("%.1f¢", mos.interval_to_cents(current_bright_interval, input_mos, {1, 1}))
			
			result = result
				.. "|-\n"
				.. "| '''" .. i-1 .. "-" .. mos_prefix .. "step'''\n"
				.. "| " .. tamnams.interval_quality(current_bright_interval, input_mos, "sentence-case", mos_prefix) .. "\n"
				.. "| " .. tamnams.interval_quality(current_bright_interval, input_mos, "abbrev"       , mos_abbrev) .. "\n"
				.. "| " .. mos.interval_as_string(current_bright_interval) .. "\n"
				.. "| " .. cents .. "\n"
		else
			-- Calculate the cent values min and max for the current intervals
			local sm_min_cents = mos.interval_to_cents(current_dark_interval, input_mos, {1,1})
			local sm_max_cents = mos.interval_to_cents(current_dark_interval, input_mos, {1,0})
			local lg_min_cents = mos.interval_to_cents(current_bright_interval, input_mos, {1,1})
			local lg_max_cents = mos.interval_to_cents(current_bright_interval, input_mos, {1,0})
			
			-- Then sort, as the min and max may be swapped 
			-- This happens if the dark interval has more small steps than large steps
			local dark_interval_range   = string.format("%.1f¢ to %.1f¢", math.min(sm_min_cents, sm_max_cents), math.max(sm_min_cents, sm_max_cents))
			local bright_interval_range = string.format("%.1f¢ to %.1f¢", math.min(lg_min_cents, lg_max_cents), math.max(lg_min_cents, lg_max_cents))
	
			result = result
			.. "|-\n"
			.. '| rowspan="2" | ' .. i-1 .. '-' .. mos_prefix .. 'step\n'
			.. "| " .. tamnams.interval_quality(current_dark_interval, input_mos, "sentence-case", mos_prefix) .. "\n"
			.. "| " .. tamnams.interval_quality(current_dark_interval, input_mos, "abbrev"       , mos_abbrev) .. "\n"
			.. "| " .. mos.interval_as_string(current_dark_interval) .. "\n"
			.. "| " .. dark_interval_range .. "\n"
			.. "|-\n"
			.. "| " .. tamnams.interval_quality(current_bright_interval, input_mos, "sentence-case", mos_prefix) .. "\n"
			.. "| " .. tamnams.interval_quality(current_bright_interval, input_mos, "abbrev"       , mos_abbrev) .. "\n"
			.. "| " .. mos.interval_as_string(current_bright_interval) .. "\n"
			.. "| " .. bright_interval_range .. "\n"
		end
	end
	result = result .. "|}"

	return result
end

-- Wrapper function; to be called by template
function p.mos_intervals(frame)
	local args = getArgs(frame)
	
	-- Preprocess scalesig into input mos
	local input_mos = mos.parse(args["Scale Signature"])
	args["Input MOS"] = input_mos
	args["Scale Signature"] = nil

	-- Preprocess collapse option
	args["Collapsed"] = yesno(args["Collapsed"], false)
	
	-- Preprocess (verify) prefix/abbrev
	args["MOS Prefix"] = tamnams.verify_prefix(input_mos, args["MOS Prefix"])
	args["MOS Abbrev"] = tamnams.verify_abbrev(input_mos, args["MOS Abbrev"])

	return p._mos_intervals(args)
end

return p