Module:Infobox ET: Difference between revisions
mNo edit summary |
Refactoring |
||
Line 4: | Line 4: | ||
local rat = require('Module:Rational') | local rat = require('Module:Rational') | ||
local l = require('Module:Limits') | local l = require('Module:Limits') | ||
local ET = require('Module:ET') | |||
-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil) | -- towards is one of: -1 (floor), 0 (nearest), 1 (ceil) | ||
local function approximation(et, interval, towards, precomputed_approx) | |||
local approx = ET.approximate(et, interval, towards or 0) | |||
local function approximation( | |||
local approx = approximate( | |||
if precomputed_approx then | if precomputed_approx then | ||
approx = precomputed_approx | approx = precomputed_approx | ||
end | end | ||
local tuning = size | local tuning = et.size | ||
if not rat.eq(equave, 2) then | if not rat.eq(et.equave, 2) then | ||
tuning = tuning .. suffix | tuning = tuning .. et.suffix | ||
end | end | ||
local ratio = rat.new(approx, size) | local ratio = rat.new(approx, et.size) | ||
if rat.as_table(ratio)[1] ~= approx then | if rat.as_table(ratio)[1] ~= approx then | ||
local link = rat.as_table(ratio)[2] .. suffix | local link = rat.as_table(ratio)[2] .. et.suffix | ||
ratio = ' (→[[' .. link .. '|' .. rat.as_ratio(ratio, '\\') | ratio = ' (→[[' .. link .. '|' .. rat.as_ratio(ratio, '\\') | ||
if not rat.eq(equave, 2) then | if not rat.eq(et.equave, 2) then | ||
ratio = ratio .. suffix | ratio = ratio .. et.suffix | ||
end | end | ||
ratio = ratio .. ']])' | ratio = ratio .. ']])' | ||
Line 44: | Line 27: | ||
ratio = '' | ratio = '' | ||
end | end | ||
local cents = | local cents = u._round(ET.cents(et, approx), 6) | ||
return approx .. '\\' .. tuning .. ' (' .. cents .. '¢)' .. ratio | return approx .. '\\' .. tuning .. ' (' .. cents .. '¢)' .. ratio | ||
end | end | ||
Line 50: | Line 33: | ||
function p.infobox_ET(frame) | function p.infobox_ET(frame) | ||
local tuning = frame.args['tuning'] | local tuning = frame.args['tuning'] | ||
local | local et = ET.parse(tuning) or ET.parse('12edo') | ||
local prime = "" | local prime = "" | ||
if u.is_prime(size) then | if u.is_prime(et.size) then | ||
prime = " (prime)" | prime = " (prime)" | ||
end | end | ||
local prev_one = '' | local prev_one = '' | ||
if size > 0 then | if et.size > 0 then | ||
prev_one = '[[' .. (size - 1) .. suffix .. '|← ' .. (size - 1) .. suffix .. ']]' | prev_one = '[[' .. (et.size - 1) .. et.suffix .. '|← ' .. (et.size - 1) .. et.suffix .. ']]' | ||
end | end | ||
local next_one = '[[' .. (size + 1) .. suffix .. '|' .. (size + 1) .. suffix .. ' →]]' | local next_one = '[[' .. (et.size + 1) .. et.suffix .. '|' .. (et.size + 1) .. et.suffix .. ' →]]' | ||
local step_size = | local step_size = ET.cents(et, 1) | ||
local fifth = approximate( | local fifth = ET.approximate(et, 3/2) | ||
local fifth_error = | local fifth_error = ET.cents(et, fifth) - i._to_cents(3/2) | ||
local dual_fifth = math.abs(fifth_error) > | local dual_fifth = math.abs(fifth_error) > step_size / 3 | ||
local note_12edo = '' | local note_12edo = '' | ||
if rat.eq(equave, 2) and size == 12 then | if rat.eq(et.equave, 2) and et.size == 12 then | ||
note_12edo = '<sup>by definition</sup>' | note_12edo = '<sup>by definition</sup>' | ||
end | end | ||
local octave = approximate( | local octave = ET.approximate(et, 2) | ||
local A1 = 7 * fifth - 4 * octave | local A1 = 7 * fifth - 4 * octave | ||
local m2 = 3 * octave - 5 * fifth | local m2 = 3 * octave - 5 * fifth | ||
local A1_cents = | local A1_cents = u._round(ET.cents(et, A1), 4) | ||
local m2_cents = | local m2_cents = u._round(ET.cents(et, m2), 4) | ||
local infobox_data = {} | local infobox_data = {} | ||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Prime factorization', | 'Prime factorization', | ||
u._prime_factorization(size) .. prime | u._prime_factorization(et.size) .. prime | ||
}) | }) | ||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Step size', | 'Step size', | ||
u._round(step_size, 6) .. '¢' .. note_12edo | |||
}) | }) | ||
if not rat.eq(equave, rat.new(3, 2)) then | if not rat.eq(et.equave, rat.new(3, 2)) then | ||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Fifth', | 'Fifth', | ||
approximation( | approximation(et, 3/2) | ||
}) | }) | ||
end | end | ||
if not rat.eq(equave, 2) then | if not rat.eq(et.equave, 2) then | ||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Octave', | 'Octave', | ||
approximation( | approximation(et, 2) | ||
}) | }) | ||
end | end | ||
Line 104: | Line 86: | ||
A1 .. ':' .. m2 .. ' (' .. A1_cents .. '¢ : ' .. m2_cents .. '¢)' | A1 .. ':' .. m2 .. ' (' .. A1_cents .. '¢ : ' .. m2_cents .. '¢)' | ||
}) | }) | ||
if dual_fifth and size > 0 then | if dual_fifth and et.size > 0 then | ||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Sharp fifth', | 'Sharp fifth', | ||
approximation( | approximation(et, 3/2, 1) | ||
}) | }) | ||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Flat fifth', | 'Flat fifth', | ||
approximation( | approximation(et, 3/2, -1) | ||
}) | }) | ||
local sharp = approximate( | local sharp = ET.approximate(et, 3/2, 1) | ||
local flat = approximate( | local flat = ET.approximate(et, 3/2, -1) | ||
table.insert(infobox_data, { | table.insert(infobox_data, { | ||
'Major 2nd', | 'Major 2nd', | ||
approximation( | approximation(et, 9/8, 0, sharp + flat - octave) | ||
}) | }) | ||
end | end | ||
Line 123: | Line 105: | ||
local consistency = tonumber(frame.args['Consistency']) | local consistency = tonumber(frame.args['Consistency']) | ||
if consistency == nil then | if consistency == nil then | ||
consistency = l.consistency_limit(size, equave, false, 43) | consistency = l.consistency_limit(et.size, et.equave, false, 43) | ||
end | end | ||
if consistency == nil then | if consistency == nil then | ||
Line 136: | Line 118: | ||
local distinct_consistency = tonumber(frame.args['Distinct consistency']) | local distinct_consistency = tonumber(frame.args['Distinct consistency']) | ||
if distinct_consistency == nil then | if distinct_consistency == nil then | ||
distinct_consistency = l.consistency_limit(size, equave, true, 43) | distinct_consistency = l.consistency_limit(et.size, et.equave, true, 43) | ||
end | end | ||
if distinct_consistency == nil then | if distinct_consistency == nil then | ||
Line 153: | Line 135: | ||
.. '</td>' | .. '</td>' | ||
.. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">' | .. '<td style="width: 70%; padding-left: 1em; padding-right: 1em; text-align: center">' | ||
.. | .. tuning | ||
.. '</td>' | .. '</td>' | ||
.. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">' | .. '<td style="width: 15%; text-align: right; white-space: nowrap; font-size: smaller">' |