Module:Infobox ET: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 103: | Line 103: | ||
}) | }) | ||
end | end | ||
local consistency = tonumber(frame.args['Consistency']) | |||
-- 1. When an equave is too close to 1, large powers of this equave explode computations | -- 1. When an equave is too close to 1, large powers of this equave explode computations | ||
-- 2. When an equave has too many factors, the number of those factors explode computations | -- 2. When an equave has too many factors, the number of those factors explode computations | ||
-- 3. When an equave has too large factors, computations explode | -- 3. When an equave has too large factors, computations explode | ||
if rat.geq(equave, rat.new(3, 2)) and rat.is_prime_ratio(equave) and rat.max_prime(equave) <= 47 then | if rat.geq(equave, rat.new(3, 2)) and rat.is_prime_ratio(equave) and rat.max_prime(equave) <= 47 then | ||
if consistency == nil then | if consistency == nil then | ||
consistency = l.consistency_limit(size, equave, false, 17) | consistency = l.consistency_limit(size, equave, false, 17) | ||
Line 114: | Line 115: | ||
consistency = 'at least 17' | consistency = 'at least 17' | ||
end | end | ||
end | |||
if consistency ~= nil then | |||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Consistency limit', | 'Consistency limit', | ||
consistency | consistency | ||
}) | }) | ||
end | |||
local distinct_consistency = tonumber(frame.args['Distinct consistency']) | |||
if rat.geq(equave, rat.new(3, 2)) and rat.is_prime_ratio(equave) and rat.max_prime(equave) <= 47 then | |||
if distinct_consistency == nil then | |||
distinct_consistency = l.consistency_limit(size, equave, true, 29) | |||
end | |||
if distinct_consistency == nil then | |||
distinct_consistency = 'at least 29' | |||
end | |||
end | |||
if distinct_consistency ~= nil then | |||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Distinct consistency limit', | 'Distinct consistency limit', | ||
distinct_consistency | |||
}) | }) | ||
end | end |
Revision as of 19:07, 2 October 2022
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(suffix, size, equave, interval, towards, precomputed_approx)
local approx = approximate(size, equave, interval, towards or 0)
if precomputed_approx then
approx = precomputed_approx
end
local tuning = size
if not rat.eq(equave, 2) then
tuning = tuning .. suffix
end
local ratio = rat.new(approx, size)
if rat.as_table(ratio)[1] ~= approx then
local link = rat.as_table(ratio)[2] .. suffix
ratio = ' (→[[' .. link .. '|' .. rat.as_ratio(ratio, '\\')
if not rat.eq(equave, 2) then
ratio = ratio .. suffix
end
ratio = ratio .. ']])'
else
ratio = ''
end
local cents = i._to_cents(i._backslash_ratio(approx .. '\\' .. tuning), 6)
return approx .. '\\' .. tuning .. ' (' .. cents .. '¢)' .. 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
local suffix = tuning:match('%d+(ed.+)')
local step_size = i._backslash_ratio('1\\' .. tuning)
local fifth = approximate(size, equave, 3/2)
local fifth_error = i._to_cents(i._backslash_ratio(fifth .. '\\' .. tuning)) - i._to_cents(3/2)
local dual_fifth = math.abs(fifth_error) > i._to_cents(step_size) / 3
local note_12edo = ''
if rat.eq(equave, 2) and size == 12 then
note_12edo = '<sup>by definition</sup>'
end
local octave = approximate(size, equave, 2)
local A1 = 7 * fifth - 4 * octave
local m2 = 3 * octave - 5 * fifth
local A1_cents = i._to_cents(i._backslash_ratio(A1 .. '\\' .. tuning), 4)
local m2_cents = i._to_cents(i._backslash_ratio(m2 .. '\\' .. tuning), 4)
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) .. '¢' .. note_12edo
})
table.insert(infobox_data, {
'Fifth',
approximation(suffix, size, equave, 3/2)
})
table.insert(infobox_data, {
'Semitones (A1:m2)',
A1 .. ':' .. m2 .. ' (' .. A1_cents .. '¢ : ' .. m2_cents .. '¢)'
})
if dual_fifth then
table.insert(infobox_data, {
'Sharp fifth',
approximation(suffix, size, equave, 3/2, 1)
})
table.insert(infobox_data, {
'Flat fifth',
approximation(suffix, size, equave, 3/2, -1)
})
local sharp = approximate(size, equave, 3/2, 1)
local flat = approximate(size, equave, 3/2, -1)
table.insert(infobox_data, {
'Major 2nd',
approximation(suffix, size, equave, 9/8, 0, sharp + flat - octave)
})
end
local consistency = tonumber(frame.args['Consistency'])
-- 1. When an equave is too close to 1, large powers of this equave explode computations
-- 2. When an equave has too many factors, the number of those factors explode computations
-- 3. When an equave has too large factors, computations explode
if rat.geq(equave, rat.new(3, 2)) and rat.is_prime_ratio(equave) and rat.max_prime(equave) <= 47 then
if consistency == nil then
consistency = l.consistency_limit(size, equave, false, 17)
end
if consistency == nil then
consistency = 'at least 17'
end
end
if consistency ~= nil then
table.insert(infobox_data, {
'Consistency limit',
consistency
})
end
local distinct_consistency = tonumber(frame.args['Distinct consistency'])
if rat.geq(equave, rat.new(3, 2)) and rat.is_prime_ratio(equave) and rat.max_prime(equave) <= 47 then
if distinct_consistency == nil then
distinct_consistency = l.consistency_limit(size, equave, true, 29)
end
if distinct_consistency == nil then
distinct_consistency = 'at least 29'
end
end
if distinct_consistency ~= nil then
table.insert(infobox_data, {
'Distinct consistency limit',
distinct_consistency
})
end
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