Module:MOS tuning spectrum: Difference between revisions

Ganaram inukshuk (talk | contribs)
New template because I hated my old code so much
 
Ganaram inukshuk (talk | contribs)
No edit summary
Line 2: Line 2:
local ET = require("Module:ET")
local ET = require("Module:ET")
local rat = require("Module:Rational")
local rat = require("Module:Rational")
local sb = require("Module:SB tree")
local mediants = require("Module:Mediants")
local utils = require("Module:Utils")
local utils = require("Module:Utils")
local yesno = require("Module:yesno")
local yesno = require("Module:yesno")
local p = {}
local p = {}


-- Helper function that parses entries from a semicolon-delimited string and returns them in an array
-- Re-re-rewrite of tuning spectrum
-- TODO: Separate this and parse_pairs into its own module of helper functions, as they're included
function p._mos_tuning_spectrum(args)
-- in various modules at this point
local default_ratios, default_depths
function p.parse_entries(unparsed)
local default_depth = 5
local parsed = {}
default_ratios, default_depths = mediants.find_mediants({{1,1}, {1,0}}, default_depth);
-- for entry in string.gmatch(unparsed, "([^;]+)") do
for entry in string.gmatch(unparsed, "([^%$]+)") do
local input_mos   = args["Input MOS"] or MOS.new(5, 2)
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
local depth       = args["Depth"] or 5
table.insert(parsed, trimmed) -- Add to array
local comments   = args["Comments"] or {}
end
local step_ratios = args["Ratios"] or default_ratios
return parsed
local depths      = args["Depths"] or default_depths
end
 
-- Helper function that parses pairs of elements separated by a colon
-- A pair must be two elements or it will be returned as an empty array
function p.parse_pair(unparsed)
local parsed = {}
for entry in string.gmatch(unparsed, "([^:]+)") do
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
table.insert(parsed, trimmed) -- Add to array
end
if #parsed == 2 then
return parsed
else
return {}
end
end
 
-- Function that takes a list of semicolon-delimited pairs and returns a map
-- (or dictionary or associative array) of key-value pairs
-- Each entry is colon-delimited as key : pair
function p.parse_kv_pairs(unparsed)
-- Tokenize the string of unparsed pairs
local parsed = p.parse_entries(unparsed)
-- Then tokenize the tokens into key-value pairs
local pairs_ = {}
for i = 1, #parsed do
local pair = p.parse_pair(parsed[i])
if #pair == 2 then
pairs_[pair[1]] = pair[2]
end
end
return pairs_
end
 
-- Rewrite of scale tree function (has bugfixes and new formatting)
function p._scale_tree(input_mos, depth, comments)
local input_mos = input_mos or MOS.new(5, 2)
local depth = depth or 5
local comments = comments or {}
local equave = input_mos.equave
local equave = input_mos.equave
Line 63: Line 24:
local n = utils._gcd(L, s) -- Number of periods
local n = utils._gcd(L, s) -- Number of periods
local abstract_bright_gen = MOS.bright_gen(input_mos)
local abstract_bright_gen = MOS.bright_gen(input_mos)
local step_ratios = sb.sb_tree_ratios(depth)
local depths = sb.sb_tree_depths(depth)
-- What is the equave suffix (edo, edt, edf, ed-p/q)
-- What is the equave suffix (edo, edt, edf, ed-p/q)
Line 191: Line 149:
end
end


-- Wrapper function; to be called by template
function p.scale_tree(frame)
function p.scale_tree(frame)
local mos = MOS.parse(frame.args["tuning"])
local args = getArgs(frame)
local depth = frame.args["depth"] or 5
local comments_unparsed = frame.args["Comments"] or ""
-- Parse scalesig
local comments = p.parse_kv_pairs(comments_unparsed) or {}
local input_mos = MOS.parse(args["Scale Signature"])
args["Input MOS"] = input_mos
args["Scale Signature"] = nil
-- Parse depth
local depth = tonumber(args["Depth"])
frame.args["Depth"] = tonumber(args["Depth"])
-- Generate mediants and depths
local ratios, depths
ratios, depths = mediants.find_mediants({{1,1}, {1,0}}, depth)
frame.args["Ratios"] = ratios
frame.args["Depths"] = depths
-- Transfer comments from args to comments
local comments = {}
for i = 1, #ratios do
local key = string.format("%d/%d", ratios[i][1], ratios[i][2])
if args[key] ~= nil then
comments[key] = args[key]
args[key] = nil
end
end
-- Parse debug option
local debugg = yesno(args["debug"])
local out_str = p._scale_tree(mos, depth, comments)
-- Output
local debugg = yesno(frame.args["debug"])
local out_str = p._scale_tree(args)
return frame:preprocess(debugg == true and "<pre>" .. out_str .. "</pre>" or out_str)
return frame:preprocess(debugg == true and "<pre>" .. out_str .. "</pre>" or out_str)
end
end


return p
return p