Module:JI ratios: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
mNo edit summary
Line 4: Line 4:
local med = require("Module:Mediants")
local med = require("Module:Mediants")
p = {}
p = {}
-- TODO:
-- Replace old int-limit search function with new one


-- Template for handling multiple entry of JI ratios into a template, and for
-- Template for handling multiple entry of JI ratios into a template, and for
Line 42: Line 39:
----------------------- INT-LIMIT-BASED SEARCH FUNCTION ------------------------
----------------------- INT-LIMIT-BASED SEARCH FUNCTION ------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Function to be replaced with new one
function p.search_by_int_limit(integer_limit, max_cents)
local max_cents = max_cents or 1200
local integer_limit = integer_limit or DEFAULT_INT_LIMIT
integer_limit = math.max(0, math.min(INT_LIMIT_MAX, integer_limit))
local init_ratios = {{1,1}, {2,1}}
local ratios = med.find_only_mediants_by_int_limit(init_ratios, integer_limit)
-- If the max cents is greater than the octave, duplicate all existing
-- ratios and raise them by the required number of octaves.
if max_cents > 1200 then
local new_ratios = {}
local num_octaves_up = math.ceil(max_cents / 1200)
for j = 1, num_octaves_up do
for i = 2, #ratios do
local num = ratios[i][1] * math.pow(2, j)
local den = ratios[i][2]
local gcd = utils._gcd(num, den)
num = num / gcd
den = den / gcd
if math.max(num, den) <= integer_limit then
table.insert(new_ratios, {num, den})
end
end
end
for i = 1, #new_ratios do
table.insert(ratios, new_ratios[i])
end
end
-- Remove any ratios that exceed the max cents
-- Convert to ratios that Module:Rational can work with
for i = 1, #ratios do
ratios[i] = rat.new(ratios[i][1], ratios[i][2])
end
return ratios
end


-- Int-limit-based search; finds ratios between 1/1 and an equave, within an int
-- Int-limit-based search; finds ratios between 1/1 and an equave, within an int
Line 302: Line 253:
local tenney_height_text = string.format("Ratios shown are within the %s-integer limit", search_params["Int Limit"])
local tenney_height_text = string.format("Ratios shown are within the %s-integer limit", search_params["Int Limit"])
local int_limit_text = search_params["Tenney Height"] ~= nil and string.format(", capped at a Tenney height of %.1f.", search_params["Tenney Height"]) or "."
local int_limit_text = search_params["Tenney Height"] ~= nil and string.format(", capped at a Tenney height of %.1f.", search_params["Tenney Height"]) or "."
result = tenney_height_text .. int_limit_text .. " " .. result
result = tenney_height_text .. ". " .. result
end
end
return result
return result