Module:Interval table: Difference between revisions
Jump to navigation
Jump to search
CompactStar (talk | contribs) No edit summary |
ArrowHead294 (talk | contribs) mNo edit summary |
||
| (146 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local function | local ET = require("Module:ET") | ||
local rat = require("Module:Rational") | |||
local ud = require("Module:Ups and downs notation") | |||
local utils = require("Module:Utils") | |||
local yesno = require("Module:Yesno") | |||
-- Gets mapping of a monzo in a val | |||
local function mapping(monzo, val) | |||
local result = 0 | |||
for k, _ in pairs(val) do | |||
result = result + val[k] * (monzo[k] or 0) | |||
end | |||
return result | |||
end | end | ||
-- Generates list of ratios up to a max numerator & denominator, | -- Generates list of ratios up to a max numerator & denominator, max ratio size, and prime limit | ||
local function get_ratios_list(max_nd, max_size) | local function get_ratios_list(max_nd, max_size, prime_limit) | ||
local ratios = {} | local ratios = {} | ||
for i=1,max_nd do | local ratio_strings = {} | ||
for j=1,max_nd do | for i = 1, max_nd do | ||
if (i/j) >= 1 and (i/j) <= max_size then | for j = 1, max_nd do | ||
ratios[#ratios + 1] = {i/ | local t = i / utils._gcd(i, j) .. "/" .. j / utils._gcd(i, j) | ||
if | |||
(i / j) >= 1 | |||
and (i / j) <= max_size | |||
and rat.max_prime(rat.new (i, j)) <= prime_limit | |||
and not utils.table_contains(ratio_strings, t) | |||
then | |||
ratios[#ratios + 1] = { i / utils._gcd(i, j), j / utils._gcd(i, j) } | |||
ratio_strings[#ratio_strings + 1] = t | |||
end | end | ||
end | end | ||
end | end | ||
return ratios | |||
end | end | ||
-- Utility | -- Utility function to get specific note name with ud.get_note_names_table | ||
-- (this is essentially what "Template:Ups and downs note name" does) | -- (this is essentially what "Template:Ups and downs note name" does) | ||
local function ud_note(et, fifth, step) | |||
return table.concat(ud.get_note_names_table(et, fifth)[step], ", "):sub(0, -1) | |||
end | |||
local function | -- ??? | ||
local function mysplit(inputstr, sep) | |||
if sep == nil then | |||
sep = "%s" | |||
end | |||
local t = {} | |||
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do | |||
table.insert(t, str) | |||
end | |||
return t | |||
end | end | ||
function p.interval_table(frame) | function p.interval_table(frame) | ||
local tuning = frame.args[ | local additional = frame.args["additional"] or "" | ||
local et = ET.parse(tuning) or ET.parse( | local additional_split = mysplit(additional, "\n") | ||
local | for i = 1, #additional_split do | ||
local | additional_split[i] = mysplit(additional_split[i], ";") | ||
end | |||
local fifth_error = ET.cents(et, fifth) - | local tuning = frame.args["tuning"] | ||
local dual_fifth = math.abs(fifth_error) > (400 / et.size) | local et = ET.parse(tuning) or ET.parse("12edo") | ||
local dual_flat_fifth = ET.approximate(et, 3/2, -1) | local debugg = yesno(frame.args["debug"]) | ||
local dual_sharp_fifth = ET.approximate(et, 3/2, 1) | local patent_val = { | ||
local ratios_list = get_ratios_list( | [2] = ET.approximate(et, 2), | ||
[3] = ET.approximate(et, 3), | |||
[5] = ET.approximate(et, 5), | |||
[7] = ET.approximate(et, 7), | |||
[11] = ET.approximate(et, 11), | |||
[13] = ET.approximate(et, 13), | |||
[17] = ET.approximate(et, 17), | |||
[19] = ET.approximate(et, 19), | |||
[23] = ET.approximate(et, 23), | |||
[29] = ET.approximate(et, 29), | |||
[31] = ET.approximate(et, 31), | |||
[37] = ET.approximate(et, 37), | |||
[41] = ET.approximate(et, 41), | |||
[43] = ET.approximate(et, 43), | |||
[47] = ET.approximate(et, 47), | |||
} -- NOTE: indices are prime numbers | |||
local t_head = "{| class=\"wikitable center-1 right-2" | |||
if et.size > 20 then | |||
t_head = t_head .. " mw-collapsible mw-collapsed" | |||
end | |||
t_head = t_head .. "\"\n" | |||
local fifth = patent_val[3] - patent_val[2] | |||
local fifth_error = ET.cents(et, fifth) - rat.cents(rat.new(3, 2)) | |||
local dual_fifth = (math.abs(fifth_error) > (400 / et.size)) | |||
local dual_flat_fifth = ET.approximate(et, 3 / 2, -1) | |||
local dual_sharp_fifth = ET.approximate(et, 3 / 2, 1) | |||
-- List of all 47-limit ratios with numerator and denominator <= certain cents value and in the range 1/1 - 12/1 | |||
local ratios_list = get_ratios_list((20 + (et.size / 4)), 12, 47) | |||
result = t_head .. | |||
"|-\n" .. | |||
"! Steps\n" .. | |||
"! Cents\n" | |||
if rat.eq(et.equave, 3) then | |||
result = result .. "! [[Hekt]]s\n" | |||
end | |||
result = result .. | |||
"! Approximate ratios\n" | |||
if rat.eq(et.equave, 2) then | if rat.eq(et.equave, 2) then | ||
if dual_fifth then | if dual_fifth then | ||
result = result | |||
.. string.format("! [[Ups and downs notation]]<br />(Dual flat fifth %s\\%s)\n", dual_flat_fifth, et.size) | |||
.. string.format("! [[Ups and downs notation]]<br />(Dual sharp fifth %s\\%s)\n", dual_sharp_fifth, et.size) | |||
else | else | ||
result = result .. "! [[Ups and downs notation]]\n" | |||
end | end | ||
end | end | ||
if #additional_split > 0 then | |||
for i = 1, #additional_split[1] do | |||
for i=0,et.size do | result = result .. string.format("! %s\n", additional_split[1][i]) | ||
end | |||
end | |||
local decimalPlaces = 3 - math.floor(math.log(1200 / et.size, 10)) | |||
if decimalPlaces > 3 then decimalPlaces = 3 end | |||
if decimalPlaces < 1 then decimalPlaces = 1 end | |||
for i = 0, et.size do | |||
result = result .. "|-\n" | |||
.. string.format("| %s\n", i) | |||
.. string.format("| %s\n", utils._round_dec(ET.cents(et, i), decimalPlaces)) | |||
if rat.eq(et.equave, 3) then | |||
result = result .. string.format("| %s\n", utils._round_dec(ET.hekts(et, i), decimalPlaces)) | |||
end | |||
result = result .. "| " | |||
for j = 1, #ratios_list do | |||
-- Go through all ratios. | |||
-- If they are mapped to the current step in the ET's patent val and < specified edostep error, | |||
-- add to approximate ratios column. | |||
local t = mapping(rat.new(ratios_list[j][1], ratios_list[j][2]), patent_val) | |||
if | |||
t == i | |||
and math.abs(ET.cents(et, i) - utils._log(ratios_list[j][1] / ratios_list[j][2], 2) * 1200) | |||
< (1200 / (math.log(et.size) * (et.size + 5))) | |||
then | |||
result = result .. string.format("[[%s/%s]], ", ratios_list[j][1], ratios_list[j][2]) | |||
end | |||
end | |||
-- ??? | |||
if result:sub(-2, -1) == "| " then | |||
result = result .. "\n" | |||
else | |||
result = result:sub(0, -3) .. "\n" | |||
end | |||
if rat.eq(et.equave, 2) then | if rat.eq(et.equave, 2) then | ||
if dual_fifth then | if dual_fifth then | ||
result = result .. string.format("| %s\n", ud_note(et, dual_flat_fifth, i)) | |||
result = result .. string.format("| %s\n", ud_note(et, dual_sharp_fifth, i)) | |||
else | else | ||
result = result .. string.format("| %s\n", ud_note(et, fifth, i)) | |||
end | end | ||
end | end | ||
if (i + 2) <= #additional_split then | |||
for j = 1, #additional_split[i + 2] do | |||
result = result .. string.format("| %s\n", additional_split[i + 2][j]) | |||
end | end | ||
end | end | ||
end | end | ||
result = result .. "|}" | |||
if debugg == true then | |||
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>" | |||
end | |||
return frame:preprocess(result) | |||
return | |||
end | end | ||
return p | return p | ||
Latest revision as of 12:11, 1 June 2025
- This module should not be invoked directly; use its corresponding template instead: Template:Interval table.
This module automatically generates a table of intervals for an equal-step tuning, showing which just intervals are approximated relatively accurately.
| Introspection summary for Module:Interval table | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| ||||||||||||||||||||||||
No function descriptions were provided. The Lua code may have further information.
local p = {}
local ET = require("Module:ET")
local rat = require("Module:Rational")
local ud = require("Module:Ups and downs notation")
local utils = require("Module:Utils")
local yesno = require("Module:Yesno")
-- Gets mapping of a monzo in a val
local function mapping(monzo, val)
local result = 0
for k, _ in pairs(val) do
result = result + val[k] * (monzo[k] or 0)
end
return result
end
-- Generates list of ratios up to a max numerator & denominator, max ratio size, and prime limit
local function get_ratios_list(max_nd, max_size, prime_limit)
local ratios = {}
local ratio_strings = {}
for i = 1, max_nd do
for j = 1, max_nd do
local t = i / utils._gcd(i, j) .. "/" .. j / utils._gcd(i, j)
if
(i / j) >= 1
and (i / j) <= max_size
and rat.max_prime(rat.new (i, j)) <= prime_limit
and not utils.table_contains(ratio_strings, t)
then
ratios[#ratios + 1] = { i / utils._gcd(i, j), j / utils._gcd(i, j) }
ratio_strings[#ratio_strings + 1] = t
end
end
end
return ratios
end
-- Utility function to get specific note name with ud.get_note_names_table
-- (this is essentially what "Template:Ups and downs note name" does)
local function ud_note(et, fifth, step)
return table.concat(ud.get_note_names_table(et, fifth)[step], ", "):sub(0, -1)
end
-- ???
local function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
function p.interval_table(frame)
local additional = frame.args["additional"] or ""
local additional_split = mysplit(additional, "\n")
for i = 1, #additional_split do
additional_split[i] = mysplit(additional_split[i], ";")
end
local tuning = frame.args["tuning"]
local et = ET.parse(tuning) or ET.parse("12edo")
local debugg = yesno(frame.args["debug"])
local patent_val = {
[2] = ET.approximate(et, 2),
[3] = ET.approximate(et, 3),
[5] = ET.approximate(et, 5),
[7] = ET.approximate(et, 7),
[11] = ET.approximate(et, 11),
[13] = ET.approximate(et, 13),
[17] = ET.approximate(et, 17),
[19] = ET.approximate(et, 19),
[23] = ET.approximate(et, 23),
[29] = ET.approximate(et, 29),
[31] = ET.approximate(et, 31),
[37] = ET.approximate(et, 37),
[41] = ET.approximate(et, 41),
[43] = ET.approximate(et, 43),
[47] = ET.approximate(et, 47),
} -- NOTE: indices are prime numbers
local t_head = "{| class=\"wikitable center-1 right-2"
if et.size > 20 then
t_head = t_head .. " mw-collapsible mw-collapsed"
end
t_head = t_head .. "\"\n"
local fifth = patent_val[3] - patent_val[2]
local fifth_error = ET.cents(et, fifth) - rat.cents(rat.new(3, 2))
local dual_fifth = (math.abs(fifth_error) > (400 / et.size))
local dual_flat_fifth = ET.approximate(et, 3 / 2, -1)
local dual_sharp_fifth = ET.approximate(et, 3 / 2, 1)
-- List of all 47-limit ratios with numerator and denominator <= certain cents value and in the range 1/1 - 12/1
local ratios_list = get_ratios_list((20 + (et.size / 4)), 12, 47)
result = t_head ..
"|-\n" ..
"! Steps\n" ..
"! Cents\n"
if rat.eq(et.equave, 3) then
result = result .. "! [[Hekt]]s\n"
end
result = result ..
"! Approximate ratios\n"
if rat.eq(et.equave, 2) then
if dual_fifth then
result = result
.. string.format("! [[Ups and downs notation]]<br />(Dual flat fifth %s\\%s)\n", dual_flat_fifth, et.size)
.. string.format("! [[Ups and downs notation]]<br />(Dual sharp fifth %s\\%s)\n", dual_sharp_fifth, et.size)
else
result = result .. "! [[Ups and downs notation]]\n"
end
end
if #additional_split > 0 then
for i = 1, #additional_split[1] do
result = result .. string.format("! %s\n", additional_split[1][i])
end
end
local decimalPlaces = 3 - math.floor(math.log(1200 / et.size, 10))
if decimalPlaces > 3 then decimalPlaces = 3 end
if decimalPlaces < 1 then decimalPlaces = 1 end
for i = 0, et.size do
result = result .. "|-\n"
.. string.format("| %s\n", i)
.. string.format("| %s\n", utils._round_dec(ET.cents(et, i), decimalPlaces))
if rat.eq(et.equave, 3) then
result = result .. string.format("| %s\n", utils._round_dec(ET.hekts(et, i), decimalPlaces))
end
result = result .. "| "
for j = 1, #ratios_list do
-- Go through all ratios.
-- If they are mapped to the current step in the ET's patent val and < specified edostep error,
-- add to approximate ratios column.
local t = mapping(rat.new(ratios_list[j][1], ratios_list[j][2]), patent_val)
if
t == i
and math.abs(ET.cents(et, i) - utils._log(ratios_list[j][1] / ratios_list[j][2], 2) * 1200)
< (1200 / (math.log(et.size) * (et.size + 5)))
then
result = result .. string.format("[[%s/%s]], ", ratios_list[j][1], ratios_list[j][2])
end
end
-- ???
if result:sub(-2, -1) == "| " then
result = result .. "\n"
else
result = result:sub(0, -3) .. "\n"
end
if rat.eq(et.equave, 2) then
if dual_fifth then
result = result .. string.format("| %s\n", ud_note(et, dual_flat_fifth, i))
result = result .. string.format("| %s\n", ud_note(et, dual_sharp_fifth, i))
else
result = result .. string.format("| %s\n", ud_note(et, fifth, i))
end
end
if (i + 2) <= #additional_split then
for j = 1, #additional_split[i + 2] do
result = result .. string.format("| %s\n", additional_split[i + 2][j])
end
end
end
result = result .. "|}"
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return frame:preprocess(result)
end
return p