Module:MOS tunings

From Xenharmonic Wiki
Revision as of 01:54, 27 August 2024 by Ganaram inukshuk (talk | contribs) (bugfixes)
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:MOS tunings.

This module displays the cent values of a MOS scale's degrees under various tunings, or step ratios, as well as the nearest JI ratios represented by those scale degrees.

Introspection summary for Module:MOS tunings 
Functions provided (8)
Line Function Params
20 capitalize_first (text)
26 step_ratio_ceil (step_ratio)
52 step_ratio_floor (step_ratio)
78 sort_step_ratios (step_ratios)
106 preprocess_step_ratios (step_ratios)
166 _mos_tunings (main) (input_mos, mos_prefix, mos_abbrev, step_ratios)
261 mos_tunings (invokable) (frame)
280 tester none
Lua modules required (6)
Variable Module Functions used
et Module:ET as_string
mos Module:MOS new
modal_union
as_string
mos_to_et
interval_to_et_steps
interval_to_cents
parse
rat Module:Rational as_ratio
tamnams Module:TAMNAMS lookup_step_ratio_range
lookup_step_ratio
degree_quality
verify_prefix
verify_abbrev
tip Module:Template input parse parse_numeric_pairs
yesno Module:Yesno yesno

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


local mos = require("Module:MOS")
local tamnams = require("Module:TAMNAMS")
local et = require("Module:ET")
local rat = require("Module:Rational")
local yesno = require("Module:Yesno")
local tip = require("Module:Template input parse")
local p = {}

-- Rewritten/simplified module replacement for Module:MOS degrees
-- A new template is chosen because it's a better name than the old one and is
-- far easier to maintain than the old one.

-- TODO:
-- - Transfer round functions and range lookup to tamnams module
-- - JI ratio input??
-- - Diatonic interval category lookup?!!

-- Helper function
-- Capitalizes the first character of a string
function p.capitalize_first(text)
	return string.upper(string.sub(text, 1, 1)) .. string.sub(text, 2, -1)
end

-- Helper function
-- "Rounds" step ratios up to the nearest named ratio
function p.step_ratio_ceil(step_ratio)
	local hardness = step_ratio[1] / step_ratio[2]
	
	local rounded_step_ratio = nil
	if hardness > 1/1 and hardness <= 4/3 then
		rounded_step_ratio = {4,3}
	elseif hardness > 4/3 and hardness <= 3/2 then
		rounded_step_ratio = {3,2}
	elseif hardness > 3/2 and hardness <= 5/3 then
		rounded_step_ratio = {5,3}
	elseif hardness > 5/3 and hardness <= 2/1 then
		rounded_step_ratio = {2,1}
	elseif hardness > 2/1 and hardness <= 5/2 then
		rounded_step_ratio = {5,2}
	elseif hardness > 5/2 and hardness <= 3/1 then
		rounded_step_ratio = {3,1}
	elseif hardness > 3/1 and hardness <= 4/1 then
		rounded_step_ratio = {4,1}
	elseif hardness > 4/1 and hardness <= 1/0 then
		rounded_step_ratio = {1,0}
	end
	return rounded_step_ratio
end

-- Helper function
-- "Rounds" step ratios down to the nearest named ratio
function p.step_ratio_floor(step_ratio)
	local hardness = step_ratio[1] / step_ratio[2]
	
	local rounded_step_ratio = nil
	if hardness >= 1/1 and hardness < 4/3 then
		rounded_step_ratio = {1,1}
	elseif hardness >= 4/3 and hardness < 3/2 then
		rounded_step_ratio = {4,3}
	elseif hardness >= 3/2 and hardness < 5/3 then
		rounded_step_ratio = {3,2}
	elseif hardness >= 5/3 and hardness < 2/1 then
		rounded_step_ratio = {5,3}
	elseif hardness >= 2/1 and hardness < 5/2 then
		rounded_step_ratio = {2,1}
	elseif hardness >= 5/2 and hardness < 3/1 then
		rounded_step_ratio = {5,2}
	elseif hardness >= 3/1 and hardness < 4/1 then
		rounded_step_ratio = {3,1}
	elseif hardness >= 4/1 and hardness < 1/0 then
		rounded_step_ratio = {4,1}
	end
	return rounded_step_ratio
end

-- Helper function
-- Sorts step ratios L:s by their hardnesses
function p.sort_step_ratios(step_ratios)
	if #step_ratios < 2 then
		return step_ratios
	end
	
	-- Sort using selection sort, which is ok for smol datasets.
	for i = 1, #step_ratios - 1 do
		local index_of_smallest = i
		local current_val = step_ratios[i][1] / step_ratios[i][2]
		
		-- Find the ratio with the smallest hardness
		for j = i + 1, #step_ratios do
			if (step_ratios[j][1] / step_ratios[j][2] < current_val) then
				index_of_smallest = j
			end
		end
		
		if index_of_smallest ~= i then
			local temp = step_ratios[index_of_smallest]
			step_ratios[index_of_smallest] = step_ratios[i]
			step_ratios[i] = temp
		end
	end
	return step_ratios
end

-- Helper function
-- Finds the step ratio range and sorts step ratios
function p.preprocess_step_ratios(step_ratios)
	local step_ratios = p.sort_step_ratios(step_ratios)
	local step_ratio_range = ""
	
	-- If the step ratios are 3/2, 2/1, and 3/1 in that order, then they are
	-- the simple step ratios: basic, hard, and soft.
	-- These should not be sorted, since the basic-hard-soft sorting is a little
	-- more intuitive than sorting by hardness.
	if #step_ratios == 3 then
		if step_ratios[1][1] == 3 and step_ratios[1][2] == 2
			and step_ratios[2][1] == 2 and step_ratios[2][2] == 1
			and step_ratios[3][1] == 3 and step_ratios[3][2] == 1 then
			return "Simple Tunings", {{2,1}, {3,1}, {3,2}}
		end
	end
	
	-- If there are multiple step ratios, find the step ratio range it
	-- corresponds to. If there is one step ratio, find the name of that
	-- hardness. If there are zero step ratios, then return "Tunings"
	if #step_ratios > 1 then
		local lower_ratio = p.step_ratio_floor(step_ratios[1])
		local upper_ratio = p.step_ratio_ceil (step_ratios[#step_ratios])
		
		-- If one ratio corresponds to the endpoint of a named hardness range
		-- but the other ratio exceeds that of a smaller range, default to the
		-- largest range that would accommodate it.
		-- 2:1 to (L:s > 3:1) = hard-of-basic
		-- 4:1 and up = ultrahard
		-- (L:s < 3:2) to 2:1 = soft-of-basic
		-- 4:3 and lower = ultrasoft
		if (lower_ratio[1]/lower_ratio[2] == 2/1 and upper_ratio[1]/upper_ratio[2] > 3/1) 
			or lower_ratio[1]/lower_ratio[2] == 4/1 then
			upper_ratio = {1,0}
		elseif (upper_ratio[1]/upper_ratio[2] == 2/1 and lower_ratio[1]/lower_ratio[2] < 3/1)
			or upper_ratio[1]/upper_ratio[2] == 4/3 then
			lower_ratio = {1,1}
		end

		step_ratio_range = tamnams.lookup_step_ratio_range(lower_ratio, upper_ratio)
		
		if step_ratio_range ~= nil then
			step_ratio_range = p.capitalize_first(step_ratio_range) .. " Tunings"
		else
			step_ratio_range = "Tunings"
		end
	elseif #step_ratios == 1 then
		step_ratio_range = tamnams.lookup_step_ratio(step_ratios[1])
		if step_ratio_range ~= nil then
			step_ratio_range = p.capitalize_first(step_ratio_range) .. " Tuning"
		else
			step_ratio_range = string.format("%s/%s", step_ratios[1][1], step_ratios[1][2]) .. " Tuning"
		end
	else
		step_ratio_range = "Tunings"
	end
	
	return step_ratio_range, step_ratios
end

-- Main function
function p._mos_tunings(input_mos, mos_prefix, mos_abbrev, step_ratios)
	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 step_ratios = step_ratios or { {2,1}, {3,1}, {3,2} }
	
	local modal_union = mos.modal_union(input_mos)
	local scale_sig = mos.as_string(input_mos)
	
	-- Sort/preprocess step ratios
	local step_ratio_range = ""
	step_ratio_range, step_ratios = p.preprocess_step_ratios(step_ratios)
	
	-- Create table
	local result = "{| class=\"wikitable sortable right-all left-1 left-2\"\n"
	
	-- Table caption
	result = result	.. string.format("|+ style=\"font-size: 105%%; white-space: nowrap;\" | %s of %s\n", step_ratio_range, scale_sig)
	
	-- First row of headers
	-- First two headers span two rows
	result = result .. "! rowspan=\"2\" class=\"unsortable\" | Scale degree\n"
	result = result .. "! rowspan=\"2\" class=\"unsortable\" | Abbrev.\n"
	
	-- Headers for tunings; these span two cols
	for i = 1, #step_ratios do
		local step_ratio_as_text = tamnams.lookup_step_ratio(step_ratios[i])
		if step_ratio_as_text == nil then
			step_ratio_as_text = string.format("%s:%s", step_ratios[i][1], step_ratios[i][2])
		else
			step_ratio_as_text = p.capitalize_first(step_ratio_as_text) .. string.format(" (%s:%s)", step_ratios[i][1], step_ratios[i][2])
		end
		
		local et_as_string = et.as_string(mos.mos_to_et(input_mos, step_ratios[i]))
		local header_text = string.format("%s<br>[[%s]]", step_ratio_as_text, et_as_string)
		result = result .. string.format("! colspan=\"2\" | %s\n", header_text)
	end
	
	-- Headers for JI ratios; this spans two rows
	result = result .. "! rowspan=\"2\" class=\"unsortable\" | Approx.<br>Ratios\n"
	result = result .. "|-\n"
	
	-- Second row of headers
	for i = 1, #step_ratios do
		result = result .. "! Steps\n"
		result = result .. "! Cents\n"
	end
	
	-- Add a row for each scale degree
	for i = 1, #modal_union do
		local interval = modal_union[i]
		
		-- Add cells for the degree names
		local degree_name   = tamnams.degree_quality(interval, input_mos, "sentence-case", mos_prefix)
		local degree_abbrev = tamnams.degree_quality(interval, input_mos, "abbrev"       , mos_abbrev)
		
		result = result
			.. "|-\n"
			.. string.format("| %s || %s", degree_name, degree_abbrev)
	
		-- Add cells for each interval's tunings
		for j = 1, #step_ratios do
			local step_ratio = step_ratios[j]
			local step_count = mos.interval_to_et_steps(interval, step_ratio)
			local cents = mos.interval_to_cents(interval, input_mos, step_ratio)
			
			result = result
				.. " "
				.. string.format("|| %s\\%s || %.1f", step_count, input_mos.nL * step_ratio[1] + input_mos.ns * step_ratio[2], cents)
		end
		
		-- Add cells for JI ratios
		-- TESTING: print only the unison and equave
		if i == 1 then
			result = result
				.. " || [[1/1]]"
		elseif i == #modal_union then
			result = result
				.. " "
				.. string.format("|| [[%s]]", rat.as_ratio(input_mos.equave))
		else
			result = result .. " || "
		end
		
		
		result = result .. "\n"
	end
	
	-- End of table
	result = result .. "|}"
	
	return result
end

-- Wrapper function; to be called by template
function p.mos_tunings(frame)
	-- Get params
	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"], false)		-- Currently does nothing
	local step_ratios = tip.parse_numeric_pairs(frame.args["Step Ratios"]) or {{2,1}, {3,1}, {3,2}}
	local ji_ratios   = tip.parse_numeric_pairs(frame.args["JI Ratios"]  ) or {{1,1}, {3,2}, {2,1}}
	
	-- 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_tunings(input_mos, mos_prefix, mos_abbrev, step_ratios)
end

function p.tester()
	local range, ratios = p.preprocess_step_ratios({ {7,1}, {3,1}, {2,1} })
	
	return range
end

return p