Module:MOS

From Xenharmonic Wiki
Revision as of 18:43, 29 March 2023 by Inthar (talk | contribs)
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]
This module primarily serves as a library for other modules and has no corresponding template.

This module provides functions for working with MOS scales in Lua code.


Introspection summary for Module:MOS 
Functions provided (5)
Line Function Params
13 new (nL, ns, equave)
21 parse (unparsed)
34 as_string (mos)
39 backslash_ratio (et, steps)
47 cents (et, steps)
Lua modules required (2)
Variable Module Functions used
rat Module:Rational new
parse
as_float
seq Module:Sequence dependency not used

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


local rat = require('Module:Rational')
local seq = require('Module:Sequence')
local p = {}

local common_name = {
	['3/2'] = 'fifth'
}
local common_ratio = {
	['fifth'] = rat.new(3, 2)
}

-- create a MOS structure (nL)L (ns)s <equave>
function p.new(nL, ns, equave)
	nL = nL or 5
	ns = ns or 2
	equave = equave or 2
	return { nL = nL, ns = ns, equave = equave }
end

-- parse a MOS structure
function p.parse(unparsed)
	local nL, ns, equave = unparsed:match('^(%d+[Ll])(%s*)(%d+[Ss])(%s*)(.-equivalent)$')
	if equave == nil then
		equave = 2
	end
	equave = common_ratio[equave] or rat.parse(equave)
	if nL == nil or ns == nil or equave == nil then
		return nil
	end
	return p.new(nL, ns, equave)
end

-- construct a string representation for a MOS structure
function p.as_string(mos)
	return et.size .. et.suffix
end

-- convert steps to a proper ratio (except that it is a float approximation)
function p.backslash_ratio(et, steps)
	if et.size == 0 then
		return 1
	end
	return rat.as_float(et.equave) ^ (steps / et.size)
end

-- convert steps to cents
function p.cents(et, steps)
	if et.size == 0 then
		return 0
	end
	steps = steps or 1
	return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2)
end

return p