Module:MOS: Difference between revisions
et_steps_as_string -> et_string; mos_to_et -> as_et; will momentarily break modules |
mNo edit summary |
||
| (15 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
-- | -- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]] | ||
local et = require("Module:ET") | |||
local rat = require("Module:Rational") | |||
local utils = require("Module:Utils") | |||
local p = {} | local p = {} | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
------------------------------ | ----------------------------- MOS-CREATING FUNCTIONS --------------------------- | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Create a new mos as a table containing the counts for large and small steps, | |||
-- plus the equave. | |||
-- Create a new mos | |||
function p.new(nL, ns, equave) | function p.new(nL, ns, equave) | ||
local nL = nL or 5 | local nL = nL or 5 | ||
| Line 64: | Line 20: | ||
end | end | ||
-- Parse a mos from its scalesig. | -- Parse a mos from its scalesig "xL ys<p/q>" or "xL ys (p/q-equivalent)". | ||
-- If no equave "p/q" is provided, it's assumed to be 2/1-equivalent. | |||
function p.parse(unparsed) | function p.parse(unparsed) | ||
local nL, ns, equave = unparsed:match("^(%d+)[Ll] | local nL, ns, equave = unparsed:match("^(%d+)[Ll].-(%d+)[Ss]%s*(.*)$") | ||
nL = tonumber(nL) | nL = tonumber(nL) | ||
ns = tonumber(ns) | ns = tonumber(ns) | ||
| Line 77: | Line 34: | ||
return p.new(nL, ns, equave) | return p.new(nL, ns, equave) | ||
end | end | ||
-------------------------------------------------------------------------------- | |||
---------------------- VALIDATION AND CHECKING FUNCTIONS ----------------------- | |||
-------------------------------------------------------------------------------- | |||
-- Is the mos xL ys valid (x and y are greater than 0)? | -- Is the mos xL ys valid (x and y are greater than 0)? | ||
function p.is_valid(mos) | function p.is_valid(mos) | ||
return mos.nL > 0 and mos.ns > 0 | return mos.nL > 0 and mos.ns > 0 | ||
end | |||
-- Is the mos xL ys octave-equivalent? | |||
function p.is_octave_equivalent(mos) | |||
return rat.eq(mos.equave, rat.new(2)) | |||
end | |||
-- Is the mos nL ns? (Root mos, with root in the sense of being the root of | |||
-- the scale tree.) | |||
function p.is_root_mos(mos) | |||
return mos.nL == mos.ns | |||
end | end | ||
| Line 88: | Line 60: | ||
-- Construct a string representation (scalesig) for a MOS structure. | -- Construct a string representation (scalesig) for a MOS structure. | ||
-- Scalesig is "xL ys", | -- Scalesig is "xL ys <p/q>" for valid mosses, omitting <p/q> for 2/1 scales. | ||
-- Option to use nbsp is provided using the second param; default is nbsp | -- Degenerate mosses (nL 0s or 0L ns) produce a string for its corresponding | ||
-- et (n-ed-p/q). | |||
-- Option to use nbsp is provided using the second param; default is nbsp. | |||
function p.as_string(mos, use_nbsp) | function p.as_string(mos, use_nbsp) | ||
local use_nbsp = (use_nbsp == nil and true or use_nbsp) | if p.is_valid(mos) then | ||
local use_nbsp = (use_nbsp == nil and true or use_nbsp) | |||
local suffix = "" | |||
if not rat.eq(mos.equave, 2) then | |||
suffix = "⟨" .. rat.as_ratio(mos.equave):lower() .. "⟩" | |||
end | |||
return mos.nL .. "L" .. (use_nbsp and " " or " ") .. mos.ns .. "s" .. suffix | |||
else | |||
return math.max(mos.nL, mos.ns) .. p.et_suffix(mos) | |||
end | end | ||
end | end | ||
-- Construct a longer string representation for a MOS structure. | -- Construct a longer string representation for a MOS structure. | ||
-- Scalesig is "xL ys", or "xL ys (p/q-equivalent)" for nonoctave scales. | -- Scalesig is "xL ys", or "xL ys (p/q-equivalent)" for nonoctave scales. | ||
-- Option to use nbsp is provided using the second param; default is nbsp | -- Degenerate mosses (nL 0s or 0L ns) produce a string for its corresponding | ||
-- et (n-ed-p/q). | |||
-- Option to use nbsp is provided using the second param; default is nbsp. | |||
function p.as_long_string(mos, use_nbsp) | function p.as_long_string(mos, use_nbsp) | ||
local use_nbsp = (use_nbsp ~= nil and use_nbsp or true) | if p.is_valid(mos) then | ||
local use_nbsp = (use_nbsp ~= nil and use_nbsp or true) | |||
local suffix = "" | |||
if not rat.eq(mos.equave, 2) then | |||
suffix = (use_nbsp and " " or " ") .. string.format("(%s-equivalent)", rat.as_ratio(mos.equave):lower()) | |||
end | |||
return mos.nL .. "L" .. (use_nbsp and " " or " ") .. mos.ns .. "s" .. suffix | |||
else | |||
return math.max(mos.nL, mos.ns) .. p.et_suffix(mos) | |||
end | end | ||
end | end | ||
| Line 115: | Line 99: | ||
-- link text as an ed, rather than a mos. | -- link text as an ed, rather than a mos. | ||
function p.as_link(mos) | function p.as_link(mos) | ||
local link = | local link = p.as_long_string(mos) | ||
local text = | local text = p.as_string(mos) | ||
if link == text then | if link == text then | ||
return string.format("[[%s]]", link) | return string.format("[[%s]]", link) | ||
else | else | ||
return string.format("[[%s | %s]]", link, text) | return string.format("[[%s|%s]]", link, text) | ||
end | end | ||
end | end | ||
| Line 127: | Line 111: | ||
-- instead. Degenerate mosses link to the corresponding equal-division page. | -- instead. Degenerate mosses link to the corresponding equal-division page. | ||
function p.as_long_link(mos) | function p.as_long_link(mos) | ||
local link = | local link = p.as_long_string(mos) | ||
return string.format("[[%s]]", link) | return string.format("[[%s]]", link) | ||
end | end | ||
| Line 164: | Line 148: | ||
return L_string .. (interval["s"] > 0 and " + " or " - ") .. s_string | return L_string .. (interval["s"] > 0 and " + " or " - ") .. s_string | ||
end | end | ||
end | |||
-- Return the equave by itself as a string. | |||
function p.equave_as_string(mos) | |||
return rat.as_ratio(mos.equave) | |||
end | |||
-- Return the equave enclosed in brackets. | |||
function p.equave_as_enclosed_string(mos) | |||
return "⟨" .. rat.as_ratio(mos.equave) .. "⟩" | |||
end | end | ||
| Line 170: | Line 164: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Find the parent mos of a mos. May return invalid mosses (nL 0s), meant | -- Find the parent mos of a mos. May return invalid mosses (nL 0s), meant to | ||
-- | -- represent equal divisions of the octave (or arbitrary equave). | ||
function p.parent(mos) | function p.parent(mos) | ||
return p.new(math.min(mos.nL, mos.ns), math.abs(mos.nL-mos.ns), mos.equave) | return p.new(math.min(mos.nL, mos.ns), math.abs(mos.nL-mos.ns), mos.equave) | ||
| Line 205: | Line 199: | ||
function p.interleaved(mos) | function p.interleaved(mos) | ||
return p.new(mos.nL*2+mos.ns, mos.ns, mos.equave), p.new(mos.nL, mos.ns*2+mos.nL, mos.equave) | return p.new(mos.nL*2+mos.ns, mos.ns, mos.equave), p.new(mos.nL, mos.ns*2+mos.nL, mos.equave) | ||
end | end | ||
| Line 284: | Line 255: | ||
-- Given a mos, return a mode based on how it's ranked by modal brightness. | -- Given a mos, return a mode based on how it's ranked by modal brightness. | ||
-- Ordering here is based on the number of | -- Ordering here is based on the number of BRIGHT GENS DOWN PER PERIOD: | ||
-- 0 is the brightest mode, 1 is 2nd brightest, etc... | -- 0 is the brightest mode, 1 is 2nd brightest, etc... | ||
function p. | -- To go by darkness, pass in p-d-1 for the 2nd arg, where p is the period count | ||
-- and d is the number of DARK GENS UP PER PERIOD. | |||
function p.mode_by_brightness(mos, bright_gens_down) | |||
return p.rotate_mode(p.brightest_mode(mos), bright_gens_down * p.bright_gen_step_count(mos)) | return p.rotate_mode(p.brightest_mode(mos), bright_gens_down * p.bright_gen_step_count(mos)) | ||
end | end | ||
-- Given a mos, list all modes in descending order of brightness. | -- Given a mos, list all modes in descending order of brightness. | ||
| Line 309: | Line 278: | ||
end | end | ||
-- List all unique rotations for a mode. Order | -- List all unique rotations for a mode, by order of leftward shifts. Order by | ||
-- rotation will usually give a different order compared to order by brightness, | |||
-- but this is expected if the order isn't by brightness (EG, modmosses). | |||
-- Note: there will always be s/p modes, where s is the number of steps in the | -- Note: there will always be s/p modes, where s is the number of steps in the | ||
-- entered mode, and p is the period of repetition. At most, there will be s | -- entered mode, and p is the period of repetition. At most, there will be s | ||
-- modes, but if there is a substring of length p that repeats within the mode | -- modes, but if there is a substring of length p that repeats within the mode | ||
-- (where p | -- (where s mod p = 0), then there will be p modes. If the mode has one step | ||
-- | -- type, then there is only one mode. | ||
function p.mode_rotations(mode_string) | function p.mode_rotations(mode_string) | ||
local rotations = {} | local rotations = {} | ||
local current_mode = mode_string | local current_mode = mode_string | ||
for i = 1, #mode_string do | for i = 1, #mode_string do | ||
if not | if not utils.table_contains(rotations, current_mode) then | ||
table.insert(rotations, current_mode) | table.insert(rotations, current_mode) | ||
end | end | ||
| Line 329: | Line 299: | ||
-- Rotate a mode by shifting the step sequence to the left. Negative values | -- Rotate a mode by shifting the step sequence to the left. Negative values | ||
-- shift it to the right. Helper function for | -- shift it to the right. Helper function for mode_by_brightness(). | ||
function p.rotate_mode(mode_string, shift_amt) | function p.rotate_mode(mode_string, shift_amt) | ||
local shift_amt = shift_amt == nil and 1 or shift_amt % #mode_string -- Default is 1 | local shift_amt = shift_amt == nil and 1 or shift_amt % #mode_string -- Default is 1 | ||
| Line 351: | Line 321: | ||
return matrix | return matrix | ||
end | end | ||
-- TODO?: replaces mode_to_step_matrices/mode_rotations_to_step_matrices with | |||
-- one function called modes_to_step_matrices? Encompasses functionality of both | |||
-- functions, but step patterns for either are generated into the same function, | |||
-- where the modes as strings are passed in. | |||
-- Given a mos, produce every step matrix for every mode. Modes are listed in | -- Given a mos, produce every step matrix for every mode. Modes are listed in | ||
| Line 403: | Line 380: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
--------------- | --------------- FUNCTIONS FOR GENERATOR AND PERIOD INTERVALS ------------------- | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Compute the bright gen as a vector of L's and s's. | -- Compute the bright gen as a vector of L's and s's. Since all mosstep | ||
-- | -- intervals (excluding the root and period) have two sizes, this returns the | ||
-- | -- large/perfect size. | ||
function p.bright_gen(mos) | function p.bright_gen(mos) | ||
local nL = mos.nL | local nL = mos.nL | ||
| Line 440: | Line 416: | ||
end | end | ||
-- Compute the dark gen as a vector of L's and s's. | -- Compute the dark gen as a vector of L's and s's. Since all mosstep | ||
-- | -- intervals (excluding the root and period) have two sizes, this returns the | ||
-- | -- small/perfect size. | ||
function p.dark_gen(mos) | function p.dark_gen(mos) | ||
local bright_gen = p.bright_gen(mos) | local bright_gen = p.bright_gen(mos) | ||
| Line 449: | Line 424: | ||
end | end | ||
-- Compute the period as a vector of L's and s's. Period intervals only | -- Compute the period as a vector of L's and s's. | ||
-- Period intervals as mossteps only appear as one size. | |||
function p.period(mos) | function p.period(mos) | ||
local gcd = utils._gcd(mos.nL, mos.ns) | local gcd = utils._gcd(mos.nL, mos.ns) | ||
| Line 459: | Line 435: | ||
-- Compute the equave as a vector of L's and s's. | -- Compute the equave as a vector of L's and s's. | ||
-- | -- Equaves as mossteps only appear as one size. For a single-period mos, this | ||
-- the same | -- is the same as p.period(). | ||
function p.equave(mos) | function p.equave(mos) | ||
return { | return { | ||
| Line 469: | Line 445: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
------------------ | ------------------- FUNCTIONS FOR SINGLE-STEP INTERVALS ------------------------ | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Return the unison as a vector of L's and s's. | ||
-- The unison is denoted by moving up from the root by zero steps, and thus does | -- The unison is denoted by moving up from the root by zero steps, and thus does | ||
-- not need a mos as input. It's basically a zero vector. | -- not need a mos as input. It's basically a zero vector. | ||
| Line 480: | Line 456: | ||
end | end | ||
-- | -- Return the vector for a single chroma. It's a large step minus a small step. | ||
-- Adding or subtracting any interval by this interval changes its "size". | -- Adding or subtracting any interval by this interval changes its "size". | ||
function p.chroma() | function p.chroma() | ||
| Line 486: | Line 462: | ||
end | end | ||
-- | -- Return the vector for an augmented step. It's a large step plus a chroma. | ||
function p.augmented_step() | function p.augmented_step() | ||
return { ["L"] = 2, ["s"] = -1 } | return { ["L"] = 2, ["s"] = -1 } | ||
end | end | ||
-- | -- Return the vector for a single large step. | ||
function p.large_step() | function p.large_step() | ||
return { ["L"] = 1, ["s"] = 0 } | return { ["L"] = 1, ["s"] = 0 } | ||
end | end | ||
-- | -- Return the vector for a single small step. | ||
function p.small_step() | function p.small_step() | ||
return { ["L"] = 0, ["s"] = 1 } | return { ["L"] = 0, ["s"] = 1 } | ||
end | end | ||
-- | -- Return the vector for a diminished step. It's a small step minus a chroma. | ||
function p.diminished_step() | function p.diminished_step() | ||
return { ["L"] = -1, ["s"] = 2 } | return { ["L"] = -1, ["s"] = 2 } | ||
| Line 515: | Line 491: | ||
end | end | ||
-- Compute an arbitrary mos interval as a vector of L's and s's. | -- Compute an arbitrary mos interval as a vector of L's and s's. Params: | ||
-- | -- - step_count: the number of steps subtended by the mosstep. | ||
-- - size_offset: denotes whether to return the large size (0) or the small | |||
- | -- size (-1) (or if this is a period interval, the diminished size). Values | ||
-- other than 0 or 1 represent alterations by multiple chromas, such as | |||
-- | -- augmented (1) or diminished (-2). | ||
-- | |||
-- | |||
-- | |||
- | |||
function p.interval_from_mos(mos, step_count, size_offset) | function p.interval_from_mos(mos, step_count, size_offset) | ||
local size_offset = size_offset or 0 -- Optional param; defaults to large size | local size_offset = size_offset or 0 -- Optional param; defaults to large size | ||
| Line 573: | Line 543: | ||
------------------------------- COUNT FUNCTIONS -------------------------------- | ------------------------------- COUNT FUNCTIONS -------------------------------- | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Given a mos, return the number of steps. | |||
function p.step_count(mos) | |||
return mos.nL + mos.ns | |||
end | |||
-- Given a mos, compute the number of steps in its bright gen (L's plus s's). | -- Given a mos, compute the number of steps in its bright gen (L's plus s's). | ||
| Line 590: | Line 565: | ||
end | end | ||
-- | -- TODO: deprecate this since "equave_step_count" is redundant and longer than | ||
-- "step count". | |||
function p.equave_step_count(mos) | function p.equave_step_count(mos) | ||
return mos.nL + mos.ns | return mos.nL + mos.ns | ||
| Line 611: | Line 587: | ||
-- perfect size (for period/root/equave intervals). This requires the mos as | -- perfect size (for period/root/equave intervals). This requires the mos as | ||
-- input. | -- input. | ||
-- | -- size_offset denotes whether to count chromas from the large size; changing | ||
-- this to -1 counts chromas from the small size. Like size_offset for | |||
-- | -- interval_from_mos, this can be used to denote altered mossteps (augmented, | ||
-- | -- diminished, etc). | ||
-- | |||
function p.interval_chroma_count(interval, mos, size_offset) | function p.interval_chroma_count(interval, mos, size_offset) | ||
local size_offset = size_offset or 0 -- Default of 0. | local size_offset = size_offset or 0 -- Default of 0. | ||
| Line 626: | Line 600: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
--------------- INTERVAL ARITHMETIC AND MANIPULATION FUNCTIONS ----------------- | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 645: | Line 619: | ||
end | end | ||
-- | -- Stack an interval, or repeatedly add the same interval to itself. | ||
function p.interval_mul(interval, amt) | function p.interval_mul(interval, amt) | ||
return { | return { | ||
| Line 659: | Line 633: | ||
interval_1["s"] == interval_2["s"] | interval_1["s"] == interval_2["s"] | ||
end | end | ||
-- Given an interval vector and a mos, find its period complement. This is the | -- Given an interval vector and a mos, find its period complement. This is the | ||
| Line 716: | Line 687: | ||
---------------------------- EQUAL-TUNING FUNCTIONS ---------------------------- | ---------------------------- EQUAL-TUNING FUNCTIONS ---------------------------- | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Given a mos and a step ratio, return an equal tuning (or equal division). | -- Given a mos and a step ratio, return an equal tuning (or equal division). | ||
-- The step ratio is entered as a 2-element array to allow non-simplified | -- The step ratio is entered as a 2-element array to allow non-simplified | ||
| Line 770: | Line 742: | ||
-- Given a mos and step ratio, return its equal temperament as a string "{steps}\{division}{suffix}". | -- Given a mos and step ratio, return its equal temperament as a string "{steps}\{division}{suffix}". | ||
function p. | function p.et_string(mos, step_ratio, suffix) | ||
local suffix = suffix or nil | local suffix = suffix or nil | ||
local et_mos = p.as_et(mos, step_ratio, suffix) | local et_mos = p.as_et(mos, step_ratio, suffix) | ||
| Line 799: | Line 771: | ||
-- that's the same as period_count(). | -- that's the same as period_count(). | ||
function p.reduced_period_to_et_string(mos, suffix) | function p.reduced_period_to_et_string(mos, suffix) | ||
return p.interval_to_et_string({["L"] = 1, ["s"] = 1}, p. | return p.interval_to_et_string({["L"] = 1, ["s"] = 1}, p.root(mos), {1,0}, suffix) | ||
end | end | ||
| Line 870: | Line 842: | ||
--return p.as_et(p.new(5,2), {2,1}) | --return p.as_et(p.new(5,2), {2,1}) | ||
return p.as_string(p.new(5,2)) .. "\n" .. p.as_link(p.new(5,2,3)) .. "\n" .. p.as_long_link(p.new(5,2)) .. "\n" .. p.as_long_link(p.new(5, | --[[ | ||
return | |||
p.mode_by_brightness(p.new(5,2), 0) .. " " .. p.mode_by_brightness(p.new(5,2), 6-6) .. "\n" .. | |||
p.mode_by_brightness(p.new(5,2), 1) .. " " .. p.mode_by_brightness(p.new(5,2), 6-5) .. "\n" .. | |||
p.mode_by_brightness(p.new(5,2), 2) .. " " .. p.mode_by_brightness(p.new(5,2), 6-4) .. "\n" .. | |||
p.mode_by_brightness(p.new(5,2), 3) .. " " .. p.mode_by_brightness(p.new(5,2), 6-3) .. "\n" .. | |||
p.mode_by_brightness(p.new(5,2), 4) .. " " .. p.mode_by_brightness(p.new(5,2), 6-2) .. "\n" .. | |||
p.mode_by_brightness(p.new(5,2), 5) .. " " .. p.mode_by_brightness(p.new(5,2), 6-1) .. "\n" .. | |||
p.mode_by_brightness(p.new(5,2), 6) .. " " .. p.mode_by_brightness(p.new(5,2), 6-0) | |||
]]-- | |||
return | |||
p.as_string(p.new(5,2)) .. "\n" .. | |||
p.as_string(p.new(4,5,3)) .. "\n" .. | |||
p.as_long_string(p.new(5,2)) .. "\n" .. | |||
p.as_long_string(p.new(4,5,3)) .. "\n" .. | |||
p.as_link(p.new(5,2)) .. "\n" .. | |||
p.as_link(p.new(4,5,3)) .. "\n" .. | |||
p.as_long_link(p.new(5,2)) .. "\n" .. | |||
p.as_long_link(p.new(4,5,3)) .. "\n" .. | |||
p.as_string(p.new(5,0)) .. "\n" .. | |||
p.as_string(p.new(4,0,3)) .. "\n" .. | |||
p.as_long_string(p.new(5,0)) .. "\n" .. | |||
p.as_long_string(p.new(4,0,3)) .. "\n" .. | |||
p.as_link(p.new(5,0)) .. "\n" .. | |||
p.as_link(p.new(4,0,3)) .. "\n" .. | |||
p.as_long_link(p.new(5,0)) .. "\n" .. | |||
p.as_long_link(p.new(4,0,3)) .. "\n" .. | |||
p.as_string(p.new(0,2)) .. "\n" .. | |||
p.as_string(p.new(0,5,3)) .. "\n" .. | |||
p.as_long_string(p.new(0,2)) .. "\n" .. | |||
p.as_long_string(p.new(0,5,3)) .. "\n" .. | |||
p.as_link(p.new(0,2)) .. "\n" .. | |||
p.as_link(p.new(0,5,3)) .. "\n" .. | |||
p.as_long_link(p.new(0,2)) .. "\n" .. | |||
p.as_long_link(p.new(0,5,3)) | |||
end | end | ||
return p | return p | ||