Module:MOS genchain

From Xenharmonic Wiki
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 genchain.

This module produces a table illustrating the generator chain for a MOS scale.

Introspection summary for Module:MOS genchain 
Functions provided (3)
Line Function Params
19 genchain (input_mos, num_gens)
39 preprocess_genchain (input_mos)
53 _mos_genchain (main) (input_mos)
Lua modules required (4)
Variable Module Functions used
mos Module:MOS new
period_step_count
bright_gen
unison
interval_add
interval_mul
equave_reduce
period_count
tamnams Module:TAMNAMS degree_quality
tip Module:Template input parse dependency not used
yesno Module:Yesno dependency not used

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


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

-- 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.genchain(input_mos, num_gens)
	local input_mos = input_mos or mos.new(5,2)
	local num_gens = num_gens or (mos.period_step_count(input_mos) - 1)

	local bright_gen = mos.bright_gen(input_mos)
	local stacked_gens = mos.unison()
	local genchain = { mos.unison() }
	
	local abs_num_gens = math.abs(num_gens)
	local sign = num_gens > 0 and 1 or -1
	
	for i = 1, abs_num_gens do
		stacked_gens = mos.interval_add(stacked_gens, mos.interval_mul(bright_gen, sign))
		table.insert(genchain, stacked_gens)
	end
	
	return genchain
end

-- wip; to replace genchain
function p.preprocess_genchain(input_mos)
	local input_mos = input_mos or mos.new(5,2)
	local num_gens = 2 * mos.period_step_count(input_mos) - 2
	
	local bright_gen = mos.bright_gen(input_mos)
	local gens = mos.interval_mul(bright_gen, -num_gens/2)
	local genchain = { mos.equave_reduce(gens, input_mos) }
	for i = 1, num_gens do
		gens = mos.interval_add(gens, bright_gen)
		table.insert(genchain, mos.equave_reduce(gens, input_mos))
	end
	return genchain
end

function p._mos_genchain(input_mos)
	local input_mos = input_mos or mos.new(5,2)
	
	local num_gens = mos.period_step_count(input_mos)
	
	local genchain = p.genchain(input_mos, num_gens)
	local rev_genchain = p.genchain(input_mos, -num_gens)
	
	-- Begin table
	local result = "{| class=\"wikitable center-all\"\n"
		.. "|-\n"
	
	-- Generators header cell
	result = result .. "! Bright generator count\n"
		
	-- Add a row for the generator counts
	-- Descending genchain
	for i = num_gens - 1, 1, -1 do
		if i == num_gens then
			result = result .. string.format("| %s", -i)
		else
			result = result .. string.format(" || %s", -i)
		end
	end
	-- Ascending genchain
	for i = 1, num_gens do
		result = result .. string.format(" || %s", i - 1)
	end
	result = result .. "\n"
		
	-- Add a row for each period's genchain
	local period_count = mos.period_count(input_mos)
	if period_count == 1 then
		result = result .. "! Degree quality\n"
		for i = num_gens, 1, -1 do
			local current_interval = mos.interval_mul(genchain[i], -1)
			current_interval = mos.equave_reduce(current_interval, input_mos)
			if i == num_gens then
				result = result .. string.format("| %s", tamnams.degree_quality(current_interval, input_mos, "abbrev"))
			else
				result = result .. string.format(" || %s", tamnams.degree_quality(current_interval, input_mos, "abbrev"))
			end	
		end
		for i = 2, num_gens do
			local current_interval = mos.equave_reduce(genchain[i], input_mos)
				result = result .. string.format(" || %s", tamnams.degree_quality(current_interval, input_mos, "abbrev"))
			end
	else
		for i = 1, period_count do
			--wip
		end
	end
	
	result = "Stacking a generator of MOS-INTERVAL from the root produces the following scale degrees. A chain of NOTECOUNT consecutive scale degrees produces one of the modes of SCALESIG, and expanding it to DAUGHTER-NOTECOUNT produces the modes of DAUGHTER-SCALES.\n"
		.. result
	
	return result
end

return p