Module:ET: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
mNo edit summary
Line 51: Line 51:
function p.as_string(et)
function p.as_string(et)
return et.size .. et.suffix
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)
steps = steps or 1
return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2)
end
end


return p
return p

Revision as of 18:40, 3 October 2022

Module documentation[view] [edit] [history] [purge]
This module primarily serves as a library for other modules and has no corresponding template.

This module provides helper functions for equal-step tunings.

Introspection summary for Module:ET 
Functions provided (5)
Line Function Params Description
18 new (size, equave, suffix) Returns an array consisting of the components of an equal-step tuning.
37 parse (unparsed) Converts an equal tuning as a string into a Lua table.
51 as_string (et) Returns the ET as a string.
56 backslash_ratio (et, steps) Converts steps to a proper ratio as a floating-point number.
64 cents (et, steps) Converts an interval of the ET into cents,  ¢.
Lua modules required (1)
Variable Module Functions used
rat Module:Rational new
as_pair
as_ratio
parse
as_float

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

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
}

-- create a ET structure <size>ed<equave>
function p.new(size, equave, suffix)
	size = size or 12
	equave = equave or 2
	if suffix == nil then
		local equave_n, equave_m = rat.as_pair(equave)
		local equave_ratio = rat.as_ratio(equave)
		suffix = size .. 'ed'
		if common_suffix[equave_ratio] then
			suffix = suffix .. common_suffix[equave_ratio]
		elseif equave_m == 1 then
			suffix = suffix .. equave_n
		else
			suffix = suffix .. equave_ratio
		end
	end
	return { size = size, equave = equave, suffix = suffix }
end

-- parse a ET structure
function p.parse(unparsed)
	local size, suffix, equave = unparsed:match('^(%d+)([Ee][Dd](.+))$')
	if equave == nil then
		return nil
	end
	size = tonumber(size)
	equave = common_ratio[equave] or rat.parse(equave)
	if size == nil or equave == nil then
		return nil
	end
	return p.new(size, equave, suffix)
end

-- construct a string representation for a ET structure
function p.as_string(et)
	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)
	steps = steps or 1
	return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2)
end

return p