Module:Interval table: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
CompactStar (talk | contribs)
No edit summary
CompactStar (talk | contribs)
No edit summary
Line 6: Line 6:
local ET = require('Module:ET')
local ET = require('Module:ET')


-- Auto-generated list of monzos of 13-limit ratios with numerator and denominator < 50 and within the range 1/1 - 5/1
local monzos_list = {
{0,1,-1,1,0,0},
{0,-1,2,-1,0,0},
{4,0,0,-1,0,0},
{1,-1,0,-1,0,1},
{0,1,1,0,0,-1},
{2,-2,0,1,0,0},
{4,-2,0,0,0,0},
{0,0,1,1,-1,0},
{0,0,0,2,0,-1},
{0,-1,-1,2,0,0},
{0,-2,-1,2,0,0},
{-2,0,0,0,1,0},
{0,-3,0,2,0,0},
{-4,-1,0,2,0,0},
{0,1,1,0,-1,0},
{0,-1,0,2,0,-1},
{2,0,-2,0,1,0},
{0,0,0,2,-1,0},
{2,1,-1,0,0,0},
{-1,3,0,-1,0,0},
{3,1,-1,0,0,0},
{3,-1,0,0,0,0},
{1,2,-1,0,0,0},
{-3,-1,0,2,0,0},
{0,3,0,0,-1,0},
{-5,1,0,0,1,0},
{-2,0,0,0,0,1},
{2,0,0,0,0,0},
{2,-1,-1,0,1,0},
{5,-1,-1,0,0,0},
{1,0,0,0,1,-1},
{4,1,-1,-1,0,0},
{-3,-1,1,1,0,0},
{2,-1,0,0,0,0},
{-5,0,0,2,0,0},
{2,1,0,0,-1,0},
{5,0,0,-1,0,0},
{-1,1,0,0,0,0},
{1,1,1,-1,0,0},
{1,1,0,1,-1,0},
{-1,0,2,-1,0,0},
{5,-2,0,0,0,0},
{-3,0,0,0,0,1},
{0,-1,0,1,0,0},
{4,0,-1,0,0,0},
{-4,0,2,0,0,0},
{1,0,-2,0,0,1},
{3,1,0,-1,0,0},
{-1,3,0,0,0,-1},
{0,1,0,1,0,-1},
{0,1,-2,0,0,1},
{-1,0,0,2,-1,0},
{-2,0,0,2,-1,0},
{-3,1,0,0,1,0},
{2,-1,0,-1,1,0},
{-1,3,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,0,2,0,0},
{-1,-2,0,2,0,0},
{-2,-2,0,2,0,0},
{3,0,1,0,0,-1},
{-3,0,-1,2,0,0},
{1,2,0,-1,0,0},
{1,1,0,1,0,-1},
{0,0,0,0,0,0},
{1,-1,-1,0,1,0},
{3,0,1,0,-1,0},
{-1,2,1,0,-1,0},
{-2,2,1,0,-1,0},
{1,1,-1,0,0,0},
{-1,0,0,1,0,0},
{-2,0,0,1,0,0},
{-1,-1,0,0,1,0},
{1,0,0,1,0,-1},
{5,-3,0,0,0,0},
{0,1,-2,0,1,0},
{-1,0,0,2,0,-1},
{0,-2,2,0,0,0},
{1,-1,-1,0,0,1},
{-1,1,0,-1,0,1},
{0,-1,0,0,0,1},
{0,-2,0,0,0,1},
{-2,1,0,-1,0,1},
{1,0,0,1,-1,0},
{2,-1,-1,1,0,0},
{-1,-1,2,0,0,0},
{-2,-1,2,0,0,0},
{-1,-2,2,0,0,0},
{-4,1,0,0,0,1},
{0,2,-1,0,0,0},
{-3,2,0,0,0,0},
{5,-1,0,-1,0,0},
{1,-1,0,-1,1,0},
{-3,0,1,1,0,0},
{-4,1,0,1,0,0},
{0,0,1,1,0,-1},
{3,-1,1,0,-1,0}
}
local function gcd(a, b)
local function gcd(a, b)
return b==0 and a or gcd(b,a%b)
return b==0 and a or gcd(b,a%b)
Line 250: Line 18:
     end
     end
     return found
     return found
end
-- Generates list of ratios up to a max numerator & denominator, and max ratio size
local function get_ratios_list(max_nd, max_size)
local ratios = {}
local ratio_strings = {}
for i=1,max_nd do
for j=1,max_nd do
t = i/gcd(i,j) .. "/" .. j/gcd(i,j)
if (i/j) >= 1 and (i/j) <= max_size and not table_contains(ratio_strings, t) then
ratios[#ratios + 1] = {i/gcd(i,j),j/gcd(i,j)}
ratio_strings[#ratio_strings + 1] = t
end
end
end
return ratios
end
end


Line 269: Line 54:
local dual_flat_fifth = ET.approximate(et, 3/2, -1)
local dual_flat_fifth = ET.approximate(et, 3/2, -1)
local dual_sharp_fifth = ET.approximate(et, 3/2, 1)
local dual_sharp_fifth = ET.approximate(et, 3/2, 1)
local ratios_list = get_ratios_list(40, 6)


wikitext = wikitext .. '!Steps\n'
wikitext = wikitext .. '!Steps\n'
Line 301: Line 87:
-- In approximate ratios column, show all ratios in the list that are within 1/3 of ET size (33.3 relative cents)
-- In approximate ratios column, show all ratios in the list that are within 1/3 of ET size (33.3 relative cents)
if math.abs(ET.cents(et, i) - (math.log(n/d)/math.log(2)) * 1200) <= (400 / et.size) then
if math.abs(ET.cents(et, i) - (math.log(n/d)/math.log(2)) * 1200) <= (400 / et.size) then
wikitext = wikitext .. '[[' .. n .. '/' .. d .. ']]' .. ', '
wikitext = wikitext .. '[[' .. n .. '/' .. d .. ']]' .. ' '
end
end
end
end
wikitext = wikitext:sub(0, -2) .. '\n'
wikitext = wikitext .. '\n'
end
end

Revision as of 23:28, 3 July 2023

Module documentation[view] [edit] [history] [purge]
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 
Functions provided (1)
Line Function Params
46 interval_table (invokable) (frame)
Lua modules required (5)
Variable Module Functions used
ET Module:ET parse
approximate
cents
iv Module:Interval _to_cents
rat Module:Rational eq
ud Module:Ups and downs notation get_note_names_table
u Module:Utils _round

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


local p = {}
local u = require('Module:Utils')
local iv = require('Module:Interval')
local rat = require('Module:Rational')
local ud = require('Module:Ups and downs notation')
local ET = require('Module:ET')

local function gcd(a, b)
	return b==0 and a or gcd(b,a%b)
end

local function table_contains(tbl, x)
    found = false
    for _, v in pairs(tbl) do
        if v == x then 
            found = true 
        end
    end
    return found
end

-- Generates list of ratios up to a max numerator & denominator, and max ratio size 
local function get_ratios_list(max_nd, max_size)
	local ratios = {}
	local ratio_strings = {}
	for i=1,max_nd do
		for j=1,max_nd do
			t = i/gcd(i,j) .. "/" .. j/gcd(i,j)
			if (i/j) >= 1 and (i/j) <= max_size and not table_contains(ratio_strings, t) then
				ratios[#ratios + 1] = {i/gcd(i,j),j/gcd(i,j)}
				ratio_strings[#ratio_strings + 1] = t
			end
		end
	end
	
	return ratios
end

-- Utility fuunction 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

function p.interval_table(frame)
	local tuning = frame.args['tuning']
	local et = ET.parse(tuning) or ET.parse('12edo')
	local wikitext = '{|class="wikitable"\n'
	local octave = ET.approximate(et, 2)
	local fifth = ET.approximate(et, 3/2)
	local fifth_error = ET.cents(et, fifth) - iv._to_cents(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)
	local ratios_list = get_ratios_list(40, 6)

	wikitext = wikitext .. '!Steps\n'
	wikitext = wikitext .. '!Cents\n'
	if rat.eq(et.equave, 2) then
		if dual_fifth then
			wikitext = wikitext .. '![[Ups and downs notation]]<br>(dual flat fifth ' .. dual_flat_fifth  .. '\\' .. et.size .. ')\n'
			wikitext = wikitext .. '![[Ups and downs notation]]<br>(dual sharp fifth ' .. dual_sharp_fifth .. '\\' .. et.size .. ')\n'
		else
			wikitext = wikitext .. '![[Ups and downs notation]]\n'
		end
	end
	
	wikitext = wikitext .. '!Approximate ratios\n'
	
	for i=0,et.size do
		wikitext = wikitext .. '|-\n'
		wikitext = wikitext .. '|' .. i .. '\n'
		wikitext = wikitext .. '|' .. u._round(ET.cents(et, i), 6) .. '\n'
		if rat.eq(et.equave, 2) then
			if dual_fifth then
				wikitext = wikitext .. '|' .. ud_note(et, dual_flat_fifth, i) .. '\n'
				wikitext = wikitext .. '|' .. ud_note(et, dual_sharp_fifth, i) .. '\n'
			else
				wikitext = wikitext .. '|' .. ud_note(et, fifth, i) .. '\n'
			end
		end
		wikitext = wikitext .. '|'
		for j=1,#ratios_list do
			local n = ratios_list[j][1]
			local d = ratios_list[j][2]
			-- In approximate ratios column, show all ratios in the list that are within 1/3 of ET size (33.3 relative cents)
			if math.abs(ET.cents(et, i) - (math.log(n/d)/math.log(2)) * 1200) <= (400 / et.size) then
				wikitext = wikitext .. '[[' .. n .. '/' .. d .. ']]' .. ' '
			end
		end
		wikitext = wikitext .. '\n'
	end
	
	
	wikitext = wikitext .. '|}'

	return wikitext
end

return p