Module:Infobox ET: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Plumtree (talk | contribs)
m Reorganisation
Plumtree (talk | contribs)
m Reorganisation
Line 4: Line 4:
local rat = require('Module:Rational')
local rat = require('Module:Rational')
local l = require('Module:Limits')
local l = require('Module:Limits')
-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
local function approximate(size, equave, interval, towards)
towards = towards or 0
local exact = math.log(interval) / math.log(rat.as_float(equave)) * size
local approx = nil
if towards < 0 then
approx = math.floor(exact)
elseif towards > 0 then
approx = math.ceil(exact)
else
approx = math.floor(exact + 0.5)
end
return approx
end
-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
local function approximation(tuning, size, equave, interval, towards)
local approx = approximate(size, equave, interval, towards or 0)
local ratio = rat.new(approx, size)
if rat.as_table(ratio)[1] ~= approx then
ratio = ' (→' .. rat.as_ratio(ratio, '\\') .. ')'
else
ratio = ''
end
return string.format("%d", approx) .. '\\' .. tuning
.. ' (' .. i._to_cents(i._backslash_ratio(approx .. '\\' .. tuning), 6) .. '¢)'
.. ratio
end


function p.infobox_ET(frame)
function p.infobox_ET(frame)
Line 16: Line 45:
end
end
local step_size = i._backslash_ratio('1\\' .. tuning)
local step_size = i._backslash_ratio('1\\' .. tuning)
local fifth = math.floor(math.log(1.5)/math.log(rat.as_float(equave))*size+0.5)
local fifth_ratio = rat.new(fifth, size)
if rat.as_table(fifth_ratio)[1] ~= fifth then
fifth_ratio = ' (→' .. rat.as_ratio(fifth_ratio, '\\') .. ')'
else
fifth_ratio = ''
end


local infobox_data = {}
local infobox_data = {}
Line 35: Line 57:
table.insert(infobox_data, {
table.insert(infobox_data, {
'Fifth',
'Fifth',
string.format("%d", fifth) .. '\\' .. tuning .. ' (' .. i._to_cents(i._backslash_ratio(fifth .. '\\' .. tuning), 6) .. '¢)' .. fifth_ratio
approximation(tuning, size, equave, 3/2)
})
})
table.insert(infobox_data, {
table.insert(infobox_data, {

Revision as of 19:07, 1 October 2022

Module documentation[view] [edit] [history] [purge]
Note: Do not invoke this module directly; use the corresponding template instead: Template:Infobox ET.

This module automatically fills in information about a specified equal temperament tuning.


local p = {}
local i = require('Module:Interval')
local u = require('Module:Utils')
local rat = require('Module:Rational')
local l = require('Module:Limits')

-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
local function approximate(size, equave, interval, towards)
	towards = towards or 0
	local exact = math.log(interval) / math.log(rat.as_float(equave)) * size
	local approx = nil
	if towards < 0 then
		approx = math.floor(exact)
	elseif towards > 0 then
		approx = math.ceil(exact)
	else
		approx = math.floor(exact + 0.5)
	end
	return approx
end

-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
local function approximation(tuning, size, equave, interval, towards)
	local approx = approximate(size, equave, interval, towards or 0)
	local ratio = rat.new(approx, size)
	if rat.as_table(ratio)[1] ~= approx then
		ratio = ' (→' .. rat.as_ratio(ratio, '\\') .. ')'
	else
		ratio = ''
	end
	return string.format("%d", approx) .. '\\' .. tuning
		.. ' (' .. i._to_cents(i._backslash_ratio(approx .. '\\' .. tuning), 6) .. '¢)'
		.. ratio
end

function p.infobox_ET(frame)
	local tuning = frame.args['tuning']
	local size, equave = i.parse_ET(tuning)
	local prime = ""
	if u.is_prime(size) then
		prime = " (prime)"
	end
	if rat.eq(equave, 2) then
		tuning = size
	end
	local step_size = i._backslash_ratio('1\\' .. tuning)

	local infobox_data = {}
	table.insert(infobox_data, {
		'Prime factorization',
		u._prime_factorization(size) .. prime
	})
	table.insert(infobox_data, {
		'Step size',
		i._to_cents(step_size, 6) .. '¢'
	})
	table.insert(infobox_data, {
		'Fifth',
		approximation(tuning, size, equave, 3/2)
	})
	table.insert(infobox_data, {
		'Consistency limit',
		l.consistency_limit(size, equave)
	})
	table.insert(infobox_data, {
		'Distinct consistency limit',
		l.consistency_limit(size, equave, true)
	})

	local s = '<div style="\n' ..
		'border: 1px solid #999;\n' ..
		'margin: 0;\n' ..
		'margin-left: 1em;\n' ..
		'margin-bottom: 0.5em;\n' ..
		'padding: 0.5em;\n' ..
		'background-color: #f0f0f0;\n' ..
		'min-width: 15em;\n' ..
		'float: right;\n' ..
		'">\n' ..
		'{| width="100%" style="border-collapse: collapse;"\n' ..
		'|+ style="font-weight: bold" | ' .. frame.args['tuning'] .. '\n'
	for i, entry in ipairs(infobox_data) do
		local caption = entry[1]
		local text = entry[2]
		s = s .. '|-\n' ..
			'| style="text-align:right; padding-right: 0.25em" | ' .. caption .. '\n' ..
			'| style="background-color: white; padding-left: 0.25em; font-weight: bold" | ' .. text .. '\n'
	end
	s = s .. '|}</div>'
	return s
end

return p