Module:Infobox MOS

From Xenharmonic Wiki
Revision as of 19:44, 12 April 2024 by Ganaram inukshuk (talk | contribs) (Removed extra parentheses)
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]
This module should not be invoked directly; use its corresponding template instead: Template:Infobox MOS.

This module generates an infobox providing information about a given moment of symmetry (MOS) scale.

Introspection summary for Module:Infobox MOS 
Functions provided (9)
Line Function Params
25 categorize (tuning)
72 adjacent_links (input_mos)
108 scale_structure (input_mos)
135 generator_ranges (input_mos)
167 tamnams_information (scalesig)
187 related_scales (input_mos)
216 equal_tunings (input_mos)
272 _infobox_mos (main) (tuning)
306 infobox_MOS (invokable) (frame)
Lua modules required (5)
Variable Module Functions used
et Module:ET new
backslash_display
cents
as_string
infobox Module:Infobox build_multisection
mos Module:MOS parse
find_ancestor
as_string
new
brightest_mode
bright_gen
as_long_string
rat Module:Rational new
eq
as_ratio
cents
utils Module:Utils _gcd

No function descriptions were provided. The Lua code may have further information.


local p = {}
local utils = require('Module:Utils')
local rat = require('Module:Rational')
local mos = require('Module:MOS')
local et = require('Module:ET')
--local xp = require('Module:Xenpaper')		-- No xenpaper links for now
local infobox = require('Module:Infobox')

local common_suffix = {
	['3/2'] = 'f',
	['2'] = 'o',
	['2/1'] = 'o',
	['3'] = 't',
	['3/1'] = 't',
}

local common_ratio = {
	['f'] = rat.new(3, 2),
	['o'] = 2,
	['t'] = 3,
}

-- Helper function
-- Adds categories
function p.categorize(tuning)
	local tuning = tuning or "5L 2s"
	local input_mos = mos.parse(tuning)
	
	-- Add to category of abstact mosses
	local categories = "[[Category:Abstract MOS patterns]]"
	
	-- Add tuning category
	--categories = categories .. string.format('[[Category:%s]]', tuning)
	
	-- Add notecount category if the notecount is greater than 3
	local notecount = input_mos.nL + input_mos.ns
	if notecount > 3 then
		categories = categories .. string.format('[[Category:%d-tone scales]]', notecount)
	end
	
	-- If the mos is octave-equivalent, add appropriate tamnams-named categories
	-- Otherwise, add to nonoctave category
	if rat.eq(input_mos.equave, rat.new(2)) then
		-- Caveats:
		-- - Only octave-equivalent mos names are used as categories.
		-- - Monowood and biwood are excluded (for now).
		-- - Mosses whose notecounts > 10 and periods < 5 are categorized under
		--   the closest tamnams-named ancestor.
		-- - Monolarge scales aren't categorized for they're all related to one
		--   another. (For now.)
		-- - Monosmall descendants that descend from 1L 9s aren't categorized
		--   for now.
		local ancestor_mos = mos.find_ancestor(input_mos)
		local tamnams_name = mos.tamnams_name[mos.as_string(ancestor_mos)]
		
		if tamnams_name == "arch(a)eotonic" then
			tamnams_name = "archaeotonic"
		end
		
		if tamnams_name ~= nil then
			categories = categories .. string.format('[[Category:%s]]', tamnams_name)
		end
	else
		categories = categories .. '[[Category:Nonoctave]]'
	end
	
	return categories
end

-- Helper function
-- Creates adjacent links for mos, found by +/-1 large or +/- small steps
function p.adjacent_links(input_mos)
	local input_mos = input_mos or mos.new(5, 2)
	
	local long_equave_as_text = ""
	local equave_as_text = ""
	if not rat.eq(input_mos.equave, 2) then
		long_equave_as_text = string.format(" (%s-equivalent)", rat.as_ratio(input_mos.equave))
		equave_as_text = string.format("⟨%s⟩", rat.as_ratio(input_mos.equave))
	end
	
	local adjacent_links = {
		string.format("[[%dL %ds%s | ↖%dL %ds%s]]", input_mos.nL-1, input_mos.ns-1, long_equave_as_text, input_mos.nL-1, input_mos.ns-1, equave_as_text),
		string.format("[[%dL %ds%s | ↑%dL %ds%s]]", input_mos.nL  , input_mos.ns-1, long_equave_as_text, input_mos.nL  , input_mos.ns-1, equave_as_text),
		string.format("[[%dL %ds%s | %dL %ds%s↗]]", input_mos.nL+1, input_mos.ns-1, long_equave_as_text, input_mos.nL+1, input_mos.ns-1, equave_as_text),
		string.format("[[%dL %ds%s | ←%dL %ds%s]]", input_mos.nL-1, input_mos.ns  , long_equave_as_text, input_mos.nL-1, input_mos.ns  , equave_as_text),
		string.format("[[%dL %ds%s | %dL %ds%s→]]", input_mos.nL+1, input_mos.ns  , long_equave_as_text, input_mos.nL+1, input_mos.ns  , equave_as_text),
		string.format("[[%dL %ds%s | ↙%dL %ds%s]]", input_mos.nL-1, input_mos.ns+1, long_equave_as_text, input_mos.nL-1, input_mos.ns+1, equave_as_text),
		string.format("[[%dL %ds%s | ↓%dL %ds%s]]", input_mos.nL  , input_mos.ns+1, long_equave_as_text, input_mos.nL  , input_mos.ns+1, equave_as_text),
		string.format("[[%dL %ds%s | %dL %ds%s↘]]", input_mos.nL+1, input_mos.ns+1, long_equave_as_text, input_mos.nL+1, input_mos.ns+1, equave_as_text),
	}
	
	for i = 1, #adjacent_links do
		local gcd = utils._gcd(input_mos.nL, input_mos.ns)
		local is_null_large = string.find(adjacent_links[i], "0L") and input_mos.nL == gcd
		local is_null_small = string.find(adjacent_links[i], "0s") and input_mos.ns == gcd
		
		if is_null_large or is_null_small then
			adjacent_links[i] = ""
		end
	end
	
	return adjacent_links
end

-- Helper function
-- Produces section entries for scale sturcture
function p.scale_structure(input_mos)
	local input_mos = input_mos or mos.new(5, 5, 3)
	
	local equave_as_text = rat.as_ratio(input_mos.equave)
	local equave_in_cents = rat.cents(input_mos.equave)
	
	local number_of_periods = utils._gcd(input_mos.nL, input_mos.ns)
	local period_as_text = ""
	if number_of_periods == 1 then
		period_as_text = equave_as_text
	else
		local equave_suffix = common_suffix[equave_as_text] or equave_as_text
		period_as_text = string.format("1\\%ded%s", number_of_periods, equave_suffix)
	end
	local period_in_cents = equave_in_cents / number_of_periods
	
	local scale_structure = {
		{"Brightest mode", mos.brightest_mode(input_mos)},
		{"[[Equave]] (cents)", string.format("%s (%.1f¢)", equave_as_text, equave_in_cents)},
		{"[[Period]] (cents)", string.format("%s (%.1f¢)", period_as_text, period_in_cents)}
	}
	
	return scale_structure
end

-- Helper function
-- Produces generator ranges for scale
function p.generator_ranges(input_mos)
	local input_mos = input_mos or mos.new(5, 2)
	
	local number_of_periods = utils._gcd(input_mos.nL, input_mos.ns)
	
	local bright_gen = mos.bright_gen(input_mos)
	local dark_gen = {
		['L'] = input_mos.nL / number_of_periods - bright_gen['L'],
		['s'] = input_mos.ns / number_of_periods - bright_gen['s']
	}
	
	local equalized_ed = et.new(input_mos.nL + input_mos.ns, input_mos.equave)
	local collapsed_ed = et.new(input_mos.nL, input_mos.equave)
	
	local bright_min_in_steps = et.backslash_display(equalized_ed, bright_gen['L'] + bright_gen['s'])
	local bright_max_in_steps = et.backslash_display(collapsed_ed, bright_gen['L'])
	local dark_min_in_steps   = et.backslash_display(collapsed_ed, dark_gen['L'])
	local dark_max_in_steps   = et.backslash_display(equalized_ed, dark_gen['L'] + dark_gen['s'])
	
	local bright_min_in_cents = et.cents(equalized_ed, bright_gen['L'] + bright_gen['s'])
	local bright_max_in_cents = et.cents(collapsed_ed, bright_gen['L'])
	local dark_min_in_cents   = et.cents(collapsed_ed, dark_gen['L'])
	local dark_max_in_cents   = et.cents(equalized_ed, dark_gen['L'] + dark_gen['s'])
	
	return {
		{"[[Bright | Bright (cents)]]", string.format("%s to %s (%.1d¢ to %.1d¢)", bright_min_in_steps, bright_max_in_steps, bright_min_in_cents, bright_max_in_cents)},
		{"[[Dark | Dark (cents)]]", string.format("%s to %s (%.1d¢ to %.1d¢)", dark_min_in_steps, dark_max_in_steps, dark_min_in_cents, dark_max_in_cents)},
	}
end

-- Helper function
-- Produces section entries for tamnams info
function p.tamnams_information(scalesig)
	local scalesig = scalesig or "5L 2s"
	
	local tamnams_name = mos.tamnams_name[scalesig] or ""
	local tamnams_prefix = mos.tamnams_prefix[scalesig] or ""
	local tamnams_abbrev = mos.tamnams_abbrev[scalesig] or ""
	
	if tamnams_name == "" then
		return nil
	else
		return {
			{"[[TAMNAMS#Mos_pattern_names | Name]]", tamnams_name},
			{"[[TAMNAMS#Mos_pattern_names | Prefix]]", tamnams_prefix},
			{"[[TAMNAMS#Mos_pattern_names | Abbrev.]]", tamnams_abbrev}
		}
	end
end

-- Helper function
-- Produces section for related scales
function p.related_scales(input_mos)
	local input_mos = input_mos or mos.new(5, 2)
	
	local parent_mos = mos.new(math.min(input_mos.nL, input_mos.ns), math.abs(input_mos.nL-input_mos.ns), input_mos.equave)
	local soft_child_mos = mos.new(input_mos.nL+input_mos.ns, input_mos.nL, input_mos.equave)
	local hard_child_mos = mos.new(input_mos.nL, input_mos.nL+input_mos.ns, input_mos.equave)
	local sister_mos = mos.new(input_mos.ns, input_mos.nL, input_mos.equave)
	
	local parent_scalesig = string.format("[[%s | %s]]", mos.as_long_string(parent_mos    ), mos.as_string(parent_mos    ))
	local soft_scalesig   = string.format("[[%s | %s]]", mos.as_long_string(soft_child_mos), mos.as_string(soft_child_mos))
	local hard_scalesig   = string.format("[[%s | %s]]", mos.as_long_string(hard_child_mos), mos.as_string(hard_child_mos))
	local sister_scalesig = string.format("[[%s | %s]]", mos.as_long_string(sister_mos    ), mos.as_string(sister_mos    ))
	
	local gcd = utils._gcd(input_mos.nL, input_mos.ns)
	local is_null_large = string.find(parent_scalesig, "0L") and input_mos.nL == gcd
	local is_null_small = string.find(parent_scalesig, "0s") and input_mos.ns == gcd
	if is_null_large or is_null_small then
		parent_scalesig = "none"
	end

	return {
		{"Parent", parent_scalesig},
		{"[[Operations_on_MOSes#Sister_MOS | Sister]]", sister_scalesig},
		{"Daughters", soft_scalesig .. ", " .. hard_scalesig}
	}
end

-- Helper function
-- Produces simple equal tunings
function p.equal_tunings(input_mos)
	local input_mos = input_mos or mos.new(5, 2)
	
	local bright_gen = mos.bright_gen(input_mos)
	
	local mos_as_vector = {
		['L'] = input_mos.nL,
		['s'] = input_mos.ns
	}
	
	local step_ratios = {
		{ 1, 1 },
		{ 4, 3 },
		{ 3, 2 },
		{ 5, 3 },
		{ 2, 1 },
		{ 5, 2 },
		{ 3, 1 },
		{ 4, 1 },
		{ 1, 0 }
	}
	
	local step_ratio_names = {
		"Equalized",
		"Supersoft",
		"Soft",
		"Semisoft",
		"Basic",
		"Semihard",
		"Hard",
		"Superhard",
		"Collapsed"
	}
	
	local equal_tunings = {}
	for i = 1, #step_ratios do
		local step_ratio = step_ratios[i]
		
		local ed_size = mos_as_vector['L'] * step_ratio[1] + mos_as_vector['s'] * step_ratio[2]
		local gen_size = bright_gen['L'] * step_ratio[1] + bright_gen['s'] * step_ratio[2]
		
		local ed = et.new(ed_size, input_mos.equave)
		local ed_as_text = et.as_string(ed)
		
		local gen_in_steps = et.backslash_display(ed, gen_size)
		local gen_in_cents = et.cents(ed, gen_size)
		
		local caption = string.format("[[%s]] (L:s = %d:%d)", step_ratio_names[i], step_ratio[1], step_ratio[2])
		local text = string.format("[[%s | %s]] (%.1f¢)", ed_as_text, gen_in_steps, gen_in_cents)
		
		table.insert(equal_tunings, { caption, text })
	end
	return equal_tunings
end

-- New "main" function
function p._infobox_mos(tuning)
	local tuning = tuning or "5L 2s"
	local tuning_parsed = mos.parse(tuning)
	
	local sections = {}
	
	local scale_structure_header = "Scale structure"
	local scale_structure_section = p.scale_structure(tuning_parsed)
	table.insert(sections, {scale_structure_header, scale_structure_section})
	
	local gen_ranges_headher = "Generator ranges"
	local gen_ranges_section = p.generator_ranges(tuning_parsed)
	table.insert(sections, {gen_ranges_headher, gen_ranges_section})
	
	local tamnams_info_header = "TAMNAMS information"
	local tamnams_info_section = p.tamnams_information(tuning)
	if tamnams_info_section ~= nil then
		table.insert(sections, {tamnams_info_header, tamnams_info_section})
	end
	
	local related_scales_header = "Related scales"
	local related_scales_section = p.related_scales(tuning_parsed)
	table.insert(sections, {related_scales_header, related_scales_section})
	
	local equal_tunings_header = "Equal tunings"
	local equal_tunings_section = p.equal_tunings(tuning_parsed)
	table.insert(sections, {equal_tunings_header, equal_tunings_section})

	local adjacent_links = p.adjacent_links(tuning_parsed)
	
	return infobox.build_multisection(tuning, sections, adjacent_links)
end

-- Wrapper function
function p.infobox_MOS(frame)
	
	local tuning = frame.args['Tuning']
	local other_names = frame.args['Other names'] or nil
	local debug_mode = tonumber(frame.args['debug']) == 1
	
	local result = p._infobox_mos(tuning)
	if not debug_mode then
		result = result .. p.categorize(tuning)
	end
	
	return result

end

return p