Module:Infobox interval: 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 23: Line 23:
rational = true
rational = true
small = true
small = true
regular = not ratio.nan and not ratio.inf and not ratio.zero
regular = not ratio.nan and not ratio.inf and not ratio.zero and ratio.sign > 0
cents = rat.cents(ratio)
cents = rat.cents(ratio)
ket = rat.as_ket(ratio, frame)
ket = rat.as_ket(ratio, frame)

Revision as of 14:54, 12 October 2022

Module documentation[view] [edit] [history] [purge]
This module should not be invoked directly; use its corresponding template instead: Template:Infobox interval.

This module generates an infobox providing information about a given interval.

Introspection summary for Module:Infobox interval 
Functions provided (1)
Line Function Params
7 infobox_interval (invokable) (frame)
Lua modules required (4)
Variable Module Functions used
infobox Module:Infobox build
i Module:Interval harmonic_entropy
rat Module:Rational parse
cents
as_ket
as_ratio
from_ket
factorisation
subgroup
as_FJS
tenney_height
weil_height
benedetti_height
u Module:Utils _round

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


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

function p.infobox_interval(frame)
	local debug_mode = frame.args['debug'] ~= nil
	
	local rational = false
	local small = false
	local regular = false
	
	local ratio = nil
	local cents = nil
	local ket = nil
	local ratio_string = nil
	
	-- intervals with relatively small powers
	if frame.args['Ratio'] and #(frame.args['Ratio']) > 0 then
		ratio = rat.parse(frame.args['Ratio'])
		if ratio ~= nil then
			rational = true
			small = true
			regular = not ratio.nan and not ratio.inf and not ratio.zero and ratio.sign > 0
			cents = rat.cents(ratio)
			ket = rat.as_ket(ratio, frame)
			ratio_string = rat.as_ratio(ratio)
		end
	end
	
	-- intervals with large powers
	if ratio == nil and frame.args['Ket'] and #(frame.args['Ket']) > 0 then
		ratio = rat.from_ket(frame.args['Ket'])
		if ratio ~= nil then
			rational = true
			small = false
			regular = true
			cents = rat.cents(ratio)
			ket = rat.as_ket(ratio, frame)
		end
	end
	
	-- irrational intervals
	if ratio == nil and frame.args['Cents'] and #(frame.args['Cents']) > 0 then
		cents = tonumber(frame.args['Cents'])
		if cents ~= nil then
			rational = false
			small = true
			regular = true
			ratio_string = frame.args['Ratio'] or ''
		end
	end
	
	local infobox_data = {}
	local cats = ''
	
	if rational then
		cats = cats .. '[[Category:Rational intervals]]'
	else
		cats = cats .. '[[Category:Irrational intervals]]'
	end
	
	if small then
		table.insert(infobox_data, {
			'Ratio',
			ratio_string
		})
	end
	if rational then
		table.insert(infobox_data, {
			'Factorization',
			rat.factorisation(ratio)
		})
	end
	if regular and rational then
		table.insert(infobox_data, {
			'[[Just intonation subgroup|Subgroup]]',
			rat.subgroup(ratio)
		})
	end
	if regular and rational then
		table.insert(infobox_data, {
			'[[Monzo]]',
			ket
		})
	end
	if regular then
		table.insert(infobox_data, {
			'Size in [[cent]]s',
			u._round(cents, 8) .. '¢'
		})
	end

	local name = frame.args['Name']
	if name and #name > 0 then
		local caption = 'Name'
		if name:match(',') then
			caption = 'Names'
			-- removing manual line breaks
			name = name:gsub('<br/>', '')
			-- removing whitespaces after commas
			name = name:gsub(', +', ',')
			-- placing line breaks after commas
			name = name:gsub(',', ',<br/>')
		end
		table.insert(infobox_data, {
			caption,
			name
		})
	else
		cats = cats .. '[[Category:Todo:add interval name]]'
		table.insert(infobox_data, {
			'Name(s)',
			'<abbr title="missing value for parameter \'Name\'">\'\'missing\'\'</abbr><sup>[[Template:Infobox Interval| ?&nbsp;]]</sup>'
		})
	end
	
	-- TODO: compute?
	local colour_name = frame.args['Color name']
	if colour_name and #colour_name > 0 then
		table.insert(infobox_data, {
			'[[Color notation|Color name]]',
			colour_name
		})
	end
	
	local FJS_name = frame.args['FJS name']
	if (not FJS_name or #FJS_name == 0) and rational then
		FJS_name = rat.as_FJS(ratio)
	end
	if FJS_name and #FJS_name > 0 then
		table.insert(infobox_data, {
			'[[Functional Just System|FJS name]]',
			frame:preprocess('<math>' .. FJS_name .. '</math>')
		})
	end
	
	if rational and regular and small then
		table.insert(infobox_data, {
			'Norms',
			'[[Tenney height|log<sub>2</sub> n⋅d]]: ' .. u._round(rat.tenney_height(ratio), 6) .. '<br/>'
				.. '[[Weil height|max(n, d)]]: ' .. u._round(rat.weil_height(ratio), 6) .. '<br/>'
				.. '[[Benedetti height|n⋅d]]: ' .. u._round(rat.benedetti_height(ratio), 6)
		})
	end
	
	if regular then
		table.insert(infobox_data, {
			'[[Harmonic entropy]]',
			'~' .. u._round(i.harmonic_entropy(cents), 6)
		})
	end
	
	local sound = frame.args['Sound']
	if sound and #sound > 0 then
		cats = cats .. '[[Category:Pages with internal sound examples]]'
		table.insert(infobox_data, {
			'[[File:' .. sound .. '|270px]]<br/><small>[[:File:' .. sound .. '|[sound info]]]</small>'
		})
	end

	if rational and small then	
		table.insert(infobox_data, {
			'<small>[https://www.yacavone.net/xen-calc/?q=' .. ratio_string .. ' open this interval in \'\'xen-calc\'\']</small>'
		})
	end
	
	local s = infobox.build(
		'<u>Interval information</u>',
		infobox_data
	)
	if not debug_mode then
		s = s .. cats
	end
	return s
end

return p