Module:MOS modes: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
Brought back standard udp formatting of up|dp(p)
ArrowHead294 (talk | contribs)
mNo edit summary
 
(48 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local mos = require('Module:MOS')
local rat = require('Module:Rational')
local utils = require('Module:Utils')
local p = {}
local p = {}


-- Function that takes a mos and produces all of the modes by brightness
local mos = require("Module:MOS")
-- The mos is entered as a data structure provided by Module:MOS
local rat = require("Module:Rational")
function p.modes_by_brightness(input_mos)
local tamnams = require("Module:TAMNAMS")
-- Default parameter, which corresponds to 5L 2s <2/1>
local tip = require("Module:Template input parse")
local input_mos = input_mos or mos.new(5, 2, 2)
local utils = require("Module:Utils")
local yesno = require("Module:Yesno")
-- Get the number of L's, s's, and periods
local nL = input_mos.nL
local ns = input_mos.ns
local periods = utils._gcd(nL, ns)
-- Find its brightest mode as a string of L's and s's
local brightest_mode = mos.brightest_mode(input_mos)
-- Find the size of the generator aned period in mossteps
local gen = mos.bright_gen(input_mos)
local gen_in_mossteps = gen['L'] + gen['s']
local period_size  = round((nL + ns) / periods)
-- For a mos xL ys, there are x+y unique modes that can be obtained by the following process:
-- For a generator g in mossteps (g < x+y) and starting with the brightest mode (as a string
-- of L's and s's), move the first g steps to the end to get the next-brightest mode.
-- Repeat this process with the rotated string to get all modes. The x+y-1th time this is done
-- will be the darkest mode.
-- In the case of a multiperiod mos nxL nys, consider it as the mos for xL ys and duplicate
-- each result n times. This way, the number of rotations needed to be performed is still x+y-1.
local brightest_mode_substr = string.sub(brightest_mode, 1, period_size)
local modes = { brightest_mode }
local current_mode = brightest_mode_substr
for i = 1, period_size - 1 do
-- Move the first g characters from the beginning to the end
local first_substr = string.sub(current_mode, 1, gen_in_mossteps)
local second_substr = string.sub(current_mode, gen_in_mossteps + 1, period_size)
current_mode = second_substr .. first_substr
-- Duplicate the string (just in case) then add it to the array of modes
local current_mode_duplicated = string.rep(current_mode, periods)
table.insert(modes, current_mode_duplicated)
end
return modes
end


-- Helper function that parses entries from a semicolon-delimited string and returns them in an array
-- TODO:
function p.parse_entries(unparsed)
-- - Add ability to autocollapse on large mos pages (say, more than 12 modes)
local parsed = {}
for entry in string.gmatch(unparsed, '([^;]+)') do
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
table.insert(parsed, trimmed) -- Add to array
end
return parsed
end


-- Test function that produces the modes of a mos as a table
-- "Main" function
function p.modes_table(frame)
-- To be called by wrapper
-- Mos is entered as a scale signature "xL ys" or "xL ys<p/q>" since the mos module can parse that format
function p._mos_modes(input_mos, mode_names, headers, entries, is_collapsed)
local scale_sig = frame.args['Scale Signature'] or "5L 2s"
local is_collapsed = is_collapsed == true
local input_mos = mos.parse(scale_sig)
local input_mos = input_mos or mos.new(5,2)
local mode_names = mode_names or {}
local headers = headers or {}
local entries = entries or {}
-- Get the mos's mode names, if given
-- Get UDPs and CPOs
-- Mode names are entered as a semicolon-delimited list
local udps = tamnams.mos_mode_udps(input_mos)
local mode_names = nil
local cpos = tamnams.mos_mode_cpos(input_mos)
if scale_sig == "5L 2s" then
mode_names_unparsed = "Lydian; Ionian (major); Mixolydian; Dorian; Aeolian (minor); Phrygian; Locrian"
mode_names = p.parse_entries(mode_names_unparsed)
else
mode_names_unparsed = frame.args['Mode Names']
mode_names = p.parse_entries(mode_names_unparsed)
end
-- Get the mos's modes and the mode count
-- Get the mos's modes
local mos_modes = p.modes_by_brightness(input_mos)
local mos_modes = mos.modes_by_brightness(input_mos)
local periods = utils._gcd(input_mos.nL, input_mos.ns) -- Needed for UDP


-- This is for entering multiple columns of info, if a single column of mode names isn't enough
-- Check whether to add mode names
-- For n headers, the number of entries must match the number of modes times the number of headers
local add_mode_names = #mode_names == #mos_modes
-- or else column data won't be added
local headers_unparsed = frame.args['Table Headers']
local headers = p.parse_entries(headers_unparsed)
local entries_unparsed = frame.args['Table Entries']
local entries = p.parse_entries(entries_unparsed)


-- To determine whether to add additional columns, determine whether the number of entries
-- Check whether the number of headers times the number of modes equals the
-- and the number of columns are greater than zero, and if so, determine whether the number of entries
-- number of entries. Supplementary info can only be added if this condition
-- is equal to the number of headers times the number of modes
-- is met. Limited to 3 columns of supplementary info.
local add_columns = #headers > 0 and #entries > 0
local add_columns = #headers > 0 and #entries > 0
if add_columns then
if add_columns then
add_columns = add_columns and #mos_modes * #headers == #entries
add_columns = add_columns and #mos_modes * #headers == #entries and #headers <= 3
end
end
-- Make a table with a column for the mode (as a string of L's and s's) and UDP
-- Table caption
local result = '{| class="wikitable"\n'
local scale_sig = mos.as_string(input_mos)
result = result .. "|+ " .. "Modes of " .. mos.as_string(input_mos) .. "\n" -- To create the scale signature with〈〉instead of <>
result = result .. "|-\n"
result = result .. "! [[UDP]]\n"
result = result .. "! Step pattern\n"
-- If there are mode names (if the mode names array is not nil), then add a column for mode names
-- Start of table
if #mode_names == #mos_modes then
local result = "{| class=\"wikitable sortable center-2 center-3 mw-collapsible" .. (is_collapsed and " mw-collapsed\"\n" or "\"\n")
result = result .. "! Mode names\n"
.. "|+ style=\"font-size: 105%; white-space: nowrap;\" | " .. string.format("Modes of %s\n", scale_sig)
.. "|-\n"
-- Table headers
result = result
.. "! [[UDP]]"
.. " !! Cyclic<br />order"
.. " !! Step<br />pattern"
-- Add header for mode names, if provided.
if add_mode_names then
result = result .. " !! class=\"unsortable\" | Mode names"
end
end
-- Add columns
-- Add column headers for supplementary info, if provided.
-- If mode names and columns are used, mode names come first
if add_columns then
if add_columns then
for i = 1, #headers do
for i = 1, #headers do
result = result .. "! " .. headers[i] .. "\n"
result = result .. string.format(" !! class=\"unsortable\" | %s", headers[i])
end
end
end
end
result = result .. "\n"
-- Enter each row
-- Enter each row
-- As of coding, mos mode listings are fairly inconsistent or nonexistent, but consist of
-- the UDP, step pattern, and any mode names(s) in some order
-- This table orders them as UDP, step pattern, and (TODO) mode names, as that's more common
for i = 1, #mos_modes do
for i = 1, #mos_modes do
result = result .. "|-\n"
result = result .. "|-\n"
-- Add the UDP, formatted as up|dp(p), where u is the number of bright generators going up,
-- Add the UDP, brightness order, and the mode's step pattern
-- d is the number of bright generators going down, and p is the number of periods
result = result .. string.format("| %s || %s || %s",
-- Omit p if p = 1
udps[i], cpos[i], mos_modes[i])
local gens_down = (i - 1) * periods
local gens_up = (#mos_modes - i) * periods
local udp_as_text = ""
if periods == 1 then
udp_as_text = gens_up .. '&#124;' .. gens_down
else
udp_as_text = string.format("%d|%d(%d)", gens_up, gens_down, periods)
end
result = result .. "|" .. udp_as_text .. "\n"
 
-- Add the mode's step pattern
result = result .. "|" .. mos_modes[i] .. "\n"
-- Add the mode's name, if given
-- Add the mode's name, if given
if #mode_names == #mos_modes then
if add_mode_names then
result = result .. "|" .. mode_names[i] .. "\n"
result = result .. string.format(" || %s", mode_names[i])
end
end
Line 149: Line 83:
for j = 1, #headers do
for j = 1, #headers do
local index = (i - 1) * #headers + j
local index = (i - 1) * #headers + j
result = result .. "|" .. entries[index] .. "\n"
result = result .. string.format(" || %s", entries[index])
end
end
end
end
result = result .. "\n"
end
end
result = result .. "|}"
result = result .. "|}"
return result
end
-- Wrapper function; to be called by template
function p.modes_table(frame)
local scale_sig = frame.args["Scale Signature"] or "5L 2s"
local input_mos = mos.parse(scale_sig)
-- Get the mos's mode names, if given
-- Mode names are entered as a semicolon-delimited list
-- 5L 2s gets default names
local mode_names = nil
if scale_sig == "5L 2s" then
mode_names = {
"Lydian",
"Ionian (major)",
"Mixolydian",
"Dorian",
"Aeolian (minor)",
"Phrygian",
"Locrian"
}
end
return result
-- Get mode names entered
if #frame.args["Mode Names"] ~= 0 then
mode_names = tip.parse_entries(frame.args["Mode Names"], "$")
end
 
-- Get supplementary info
local headers_unparsed = frame.args["Table Headers"]
local headers = tip.parse_entries(headers_unparsed, "$")
local entries_unparsed = frame.args["Table Entries"]
local entries = tip.parse_entries(entries_unparsed, "$")
local is_collapsed = yesno(frame.args["Collapsed"], false)
local debugg = yesno(frame.args["debug"])
local result = p._mos_modes(input_mos, mode_names, headers, entries, is_collapsed)
-- Current means of adding entries is unmaintainable; to be deprecated.
if headers_unparsed ~= "" and entries_unparsed ~= "" then
result = result .. "[[Category:Pages with deprecated template parameters]]"
end
-- Debugger option
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return frame:preprocess(result)
end
end


return p
return p

Latest revision as of 12:45, 1 June 2025

Module documentation[view] [edit] [history] [purge]
Note: Do not invoke this module directly; use the corresponding template instead: Template:MOS modes.

This template is used for mos pages to automatically generate and list that mos's modes, along with the modes' UDP (up-down-period) notation.


local p = {}

local mos = require("Module:MOS")
local rat = require("Module:Rational")
local tamnams = require("Module:TAMNAMS")
local tip = require("Module:Template input parse")
local utils = require("Module:Utils")
local yesno = require("Module:Yesno")

-- TODO:
-- - Add ability to autocollapse on large mos pages (say, more than 12 modes)

-- "Main" function
-- To be called by wrapper
function p._mos_modes(input_mos, mode_names, headers, entries, is_collapsed)
	local is_collapsed = is_collapsed == true
	local input_mos = input_mos or mos.new(5,2)
	local mode_names = mode_names or {}
	local headers = headers or {}
	local entries = entries or {}
	
	-- Get UDPs and CPOs
	local udps = tamnams.mos_mode_udps(input_mos)
	local cpos = tamnams.mos_mode_cpos(input_mos)
	
	-- Get the mos's modes
	local mos_modes = mos.modes_by_brightness(input_mos)

	-- Check whether to add mode names
	local add_mode_names = #mode_names == #mos_modes

	-- Check whether the number of headers times the number of modes equals the
	-- number of entries. Supplementary info can only be added if this condition
	-- is met. Limited to 3 columns of supplementary info.
	local add_columns = #headers > 0 and #entries > 0
	if add_columns then
		add_columns = add_columns and #mos_modes * #headers == #entries and #headers <= 3
	end
	
	-- Table caption
	local scale_sig = mos.as_string(input_mos)
	
	-- Start of table
	local result = "{| class=\"wikitable sortable center-2 center-3 mw-collapsible" .. (is_collapsed and " mw-collapsed\"\n" or "\"\n")
		.. "|+ style=\"font-size: 105%; white-space: nowrap;\" | " .. string.format("Modes of %s\n", scale_sig)
		.. "|-\n"
		
	-- Table headers
	result = result
		.. "! [[UDP]]"
		.. " !! Cyclic<br />order"
		.. " !! Step<br />pattern"
	
	-- Add header for mode names, if provided.
	if add_mode_names then
		result = result .. " !! class=\"unsortable\" | Mode names"
	end
	
	-- Add column headers for supplementary info, if provided.
	if add_columns then
		for i = 1, #headers do
			result = result .. string.format(" !! class=\"unsortable\" | %s", headers[i])
		end
	end
	
	result = result .. "\n"
	
	-- Enter each row
	for i = 1, #mos_modes do
		result = result .. "|-\n"
		
		-- Add the UDP, brightness order, and the mode's step pattern
		result = result .. string.format("| %s || %s || %s",
			udps[i], cpos[i], mos_modes[i])
		
		-- Add the mode's name, if given
		if add_mode_names  then
			result = result .. string.format(" || %s", mode_names[i])
		end
		
		-- Add columns if given
		if add_columns then
			for j = 1, #headers do
				local index = (i - 1) * #headers + j
				result = result .. string.format(" || %s", entries[index])
			end
		end
		
		result = result .. "\n"
	end
	
	result = result .. "|}"
	return result
end

-- Wrapper function; to be called by template
function p.modes_table(frame)
	local scale_sig = frame.args["Scale Signature"] or "5L 2s"
	local input_mos = mos.parse(scale_sig)
	
	-- Get the mos's mode names, if given
	-- Mode names are entered as a semicolon-delimited list
	-- 5L 2s gets default names
	local mode_names = nil
	if scale_sig == "5L 2s" then
		mode_names = { 
			"Lydian",
			"Ionian (major)",
			"Mixolydian",
			"Dorian",
			"Aeolian (minor)",
			"Phrygian",
			"Locrian"
		}
	end
	
	-- Get mode names entered
	if #frame.args["Mode Names"] ~= 0 then
		mode_names = tip.parse_entries(frame.args["Mode Names"], "$")
	end

	-- Get supplementary info
	local headers_unparsed = frame.args["Table Headers"]
	local headers = tip.parse_entries(headers_unparsed, "$")
	local entries_unparsed = frame.args["Table Entries"]
	local entries = tip.parse_entries(entries_unparsed, "$")
	local is_collapsed = yesno(frame.args["Collapsed"], false)
	local debugg = yesno(frame.args["debug"])
	local result = p._mos_modes(input_mos, mode_names, headers, entries, is_collapsed)
	
	-- Current means of adding entries is unmaintainable; to be deprecated.
	if headers_unparsed ~= "" and entries_unparsed ~= "" then
		result = result .. "[[Category:Pages with deprecated template parameters]]"
	end
	
	-- Debugger option
	if debugg == true then
		result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
	end
	
	return frame:preprocess(result)
end

return p