Module:EDO intro

From Xenharmonic Wiki
Jump to navigation Jump to search
Icon-Todo.png Todo: Documentation

Documentation transcluded from /doc
Note: Do not invoke this module directly; use the corresponding template instead: Template:EDO intro.

local ord = require("Module:Ordinal")
local utils = require("Module:Utils")
local p = {}

function p.edo_intro(ed)
	ed = ed or 12

	-- Exactly or about?
	-- Check up to three significant figures
	local edostep_size = 1200 / ed

	local edostep_text
	local edostep_size_rounded = utils._round(edostep_size, 3)
	if edostep_size - edostep_size_rounded == 0 then
		edostep_text = " of exactly " .. edostep_size_rounded .. " [[¢]] each."
	else
		edostep_text = " of about " .. edostep_size_rounded .. " [[¢]] each."
	end

	local ordinal = ord._ordinal(ed)

	local intro_text = string.format("'''%d equal divisions of the octave''' (abbreviated '''%dedo'''),", ed, ed)
	intro_text = intro_text
		.. string.format(
			" or '''%d-tone equal temperament''' ('''%dtet'''), '''%d equal temperament''' ('''%det''') when viewed under a [[regular temperament]] perspective, ",
			ed,
			ed,
			ed,
			ed
		)
	intro_text = intro_text
		.. string.format(" is the [[tuning system]] that divides the [[octave]] into %d [[equal]] parts", ed)
	intro_text = intro_text .. edostep_text
	intro_text = intro_text
		.. string.format(
			" Each step represents a [[frequency ratio]] of 2<sup>1/%d</sup>, or the %s root of 2.",
			ed,
			ed,
			ordinal
		)

	return intro_text
end

-- Parse edo from text
function p.parse(unparsed)
	unparsed = unparsed or "12edo"
	local parsed = unparsed:match("^(%d+)edo$") or unparsed:match("^(%d+)$")
	return parsed
end

function p.edo_intro_frame(frame)
	local edo = tonumber(p.parse(frame.args["EDO"]))

	return p.edo_intro(edo)
end

return p