Module:Chord consistency: Difference between revisions
Dummy index (talk | contribs) equave-free veresion of additively_consistent() |
ArrowHead294 (talk | contribs) mNo edit summary |
||
(7 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
local ET = require('Module:ET') | |||
local rat = require('Module:Rational') | local rat = require('Module:Rational') | ||
local utils = require("Module:Utils") | local utils = require("Module:Utils") | ||
local p = {} | local p = {} | ||
Line 83: | Line 83: | ||
end | end | ||
return true | return true | ||
end | |||
-- determine maximum error | |||
function p.max_error(et, ratios) | |||
local maxe = 0.0 | |||
for a_key, a in pairs(ratios) do | |||
local a_approx = ET.approximate(et, rat.as_float(a)) | |||
local e = math.abs((ET.cents(et, a_approx) - rat.cents(a)) / ET.cents(et, 1)) | |||
if (e > maxe) then | |||
maxe = e | |||
end | |||
end | |||
return maxe | |||
end | |||
function p.consistent_edos(harmonics, distance, ed, maxlen) | |||
distance = distance or 1.0 | |||
ed = ed or 'edo' | |||
local max_n = 72 | |||
maxlen = maxlen or max_n | |||
if max_n < maxlen then max_n = maxlen end | |||
local all_interval = {} | |||
for i, h in ipairs(harmonics) do | |||
-- compute all ratio | |||
for j, g in ipairs(harmonics) do | |||
if j > i then | |||
local a = rat.new(g, h) | |||
all_interval[rat.as_ratio(a)] = a | |||
end | |||
end | |||
end | |||
local vals = {} | |||
for i = 1, max_n do | |||
local et = ET.parse('' .. i .. ed) | |||
local consistent = p.additively_consistent_int(et, all_interval, false, nil) | |||
if consistent then | |||
local maxe = p.max_error(et, all_interval) | |||
if maxe <= 5.0e-11 then | |||
table.insert(vals, "[[" .. i .. ed .. "]]" .. "(just)") | |||
break | |||
end | |||
local dist = 0.5/maxe | |||
local up = (dist >= distance) | |||
local llevel = 0 | |||
while (dist >= 2) do | |||
llevel = llevel + 1 | |||
dist = dist / 2 | |||
end | |||
if up then | |||
if #vals >= maxlen then | |||
table.insert(vals, "…") | |||
break | |||
end | |||
table.insert(vals, "[[" .. i .. ed .. "]]" .. string.rep("*", llevel)) | |||
end | |||
end | |||
end | |||
return table.concat(vals, ", ") | |||
end | end | ||
function p.noinfobox_chord(frame) | function p.noinfobox_chord(frame) | ||
local | local distance = tonumber(frame.args["Distance"]) | ||
local debug_data = "" | local debug_data = "" | ||
local infobox_data = {} | local infobox_data = {} | ||
Line 98: | Line 158: | ||
assert(h > 0, "invalid harmonic") | assert(h > 0, "invalid harmonic") | ||
table.insert(harmonics, h) | table.insert(harmonics, h) | ||
end | |||
if distance == nil then | |||
if #harmonics >= 5 then | |||
distance = 1.5 | |||
elseif #harmonics >= 3 then | |||
distance = 2.0 | |||
else | |||
distance = 3.0 | |||
end | |||
end | end | ||
Line 116: | Line 186: | ||
local root_interval_links = {} | local root_interval_links = {} | ||
local step_interval_links = {} | local step_interval_links = {} | ||
for i, h in ipairs(harmonics) do | for i, h in ipairs(harmonics) do | ||
-- compute ratio of this harmonic relative to the root | -- compute ratio of this harmonic relative to the root | ||
Line 131: | Line 200: | ||
local step_denom = prev / step_gcd | local step_denom = prev / step_gcd | ||
table.insert(step_interval_links, "[[" .. step_numer .. "/" .. step_denom .. "]]") | table.insert(step_interval_links, "[[" .. step_numer .. "/" .. step_denom .. "]]") | ||
end | end | ||
end | end | ||
cat = "(d >= " .. distance .. ") " .. p.consistent_edos(harmonics, distance, 'edo', 4) | |||
--end | --end | ||
return cat | return cat |