Module:MOS gamut: Difference between revisions

Ganaram inukshuk (talk | contribs)
Undo revision 115790 by Ganaram inukshuk (talk) - colors are wip
Tag: Undo
ArrowHead294 (talk | contribs)
mNo edit summary
 
(47 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local mos = require('Module:MOS')
local rat = require('Module:Rational')
local mosm = require('Module:MOS modes')
local et = require('Module:ET')
local p = {}
local p = {}


-- Helper function for creating a genchain, a sequence of named pitches where consecutive
local et = require("Module:ET")
-- pitches are a generator apart. This can only work in one direction at a time, so it's
local mos = require("Module:MOS")
-- necessary to call this twice if both an ascending and descending chain are needed. For
local mosm = require("Module:MOS modes")
-- a multi-period mos, multiple genchains are returned as an array of arrays, where each
local mosnot = require("Module:MOS notation")
-- array has indices denote the number of generators going up (or down) and the element
local rat = require("Module:Rational")
-- denote the named pitch. For the single-period case, it's a size-1 array whose element
local utils = require("Module:Utils")
-- is a single genchain.
local yesno = require("Module:Yesno")
-- As an example, 12edo standard notation is this: 1 is G, 2 is D, 3 is A, 4 is E, etc;
-- for the descending chain, -1 is F, -2 is Bb, -3 is Eb, etc. This example is returned
-- as positive numbers, so it may also be interpreted as dark gens going up instead.
-- Parameters:
-- - input_mos - the mos itself represented as a data structure from Module:MOS
-- - genchain_init - how many named pitches per period are there without accidentals added?
--  This is either the value u or d for the UDP of up|dp.
-- - genchain_length - how many generators should the genchain extend after the root?
-- - note_symbols - the note names entered as a string, such as "CDEFGAB"
-- - chroma_symbol - the symbol for the chroma used, such as "#" (for ascending chain) or
--  "b" (for descending chain)
-- - going_up - bool; whether the genchain is going up or down; true for up, false for down
function p.mos_genchain(input_mos, genchain_init, genchain_length, note_symbols, chroma_symbol, going_up)
-- Default parameters for testing
--[[
local input_mos = input_mos or mos.new(5, 2, 2)
local genchain_init = genchain_init or 5
local genchain_length = genchain_length or 10
local note_symbols = note_symbols or "CDEFGAB"
local chroma_symbol = chroma_symbol or "#"
local going_up = going_up or true
]]--
-- Get the number of mossteps per period and equave
local mossteps_per_equave = input_mos.nL + input_mos.ns
local periods = rat.gcd(input_mos.nL, input_mos.ns)
local mossteps_per_period = mossteps_per_equave / periods
-- Split the note symbols string into subsets
-- This is only necessary if the mos is multi-period
local note_subsets = {}
for i = 1, periods do
local start_index = (i - 1) * mossteps_per_period + 1
local stop_index = i * mossteps_per_period
local substr = string.sub(note_symbols, start_index, stop_index)
table.insert(note_subsets, substr)
end
-- Create the genchain for each period
local genchains = {}
for i = 1, periods do
local note_names = note_subsets[i]
-- Get the size of the generator in mossteps
local gen = mos.bright_gen(input_mos)
local gen_in_mossteps = gen['L'] + gen['s']
-- If the genchain is descending (ie, going_up is false), switch to
-- using the dark gen in mossteps, which is the period complement
-- of the bright gen; going up by the dark gen is the same as going
-- down by the bright gen
if not going_up then
gen_in_mossteps = mossteps_per_period - gen_in_mossteps
end
-- Use this value, with modular arithmteic, as an index to get the note name
local accumulator = 0
-- Create a genchain that initially starts at the root
local root = string.sub(note_names, 1, 1)
local genchain = { root }
for j = 1, genchain_length do
-- Increment the index by the generator
accumulator = accumulator + gen_in_mossteps
-- Convert the accumulator into an index, then add 1 because
-- lua indexing
local index = accumulator % mossteps_per_period + 1
-- Get the note name
local note_name = string.sub(note_names, index, index)
-- Add accidentals
local accidentals_to_add = 0
if j > genchain_init then
accidentals_to_add = math.ceil((j - genchain_init) / mossteps_per_period)
end
note_name = note_name .. string.rep(chroma_symbol, accidentals_to_add)
-- Add the note name
table.insert(genchain, note_name)
end
-- Add the genchain
table.insert(genchains, genchain)
end
return genchains
end


-- Function that produces a gamut, a sequence of note names with accidentals, for an edo
-- Helper function for the function that has "frame" as a parameter
function p.mos_gamut(input_mos, generators_up, step_ratio, note_symbols, chroma_plus_symbol, chroma_minus_symbol)
function p.mos_gamut(input_mos, udp, step_ratio, note_symbols, chroma_plus_symbol, chroma_minus_symbol)
-- Default parameters for input mos and step ratio (5L 2s and 2:1 step ratio)
-- Default parameters for input mos and step ratio (5L 2s and 2:1 step ratio)
local input_mos = input_mos or mos.new(2, 5, 2)
local input_mos = input_mos or mos.new(5, 2, 2)
local step_ratio = step_ratio or { 2, 1 }
local step_ratio = step_ratio or { 2, 1 }
-- Get the number of mossteps per period and equave
-- Get the number of mossteps per period and equave
local mossteps_per_equave = input_mos.nL + input_mos.ns
local mossteps_per_equave = input_mos.nL + input_mos.ns
local periods = rat.gcd(input_mos.nL, input_mos.ns)
local periods_per_equave = utils._gcd(input_mos.nL, input_mos.ns)
local mossteps_per_period = mossteps_per_equave / periods
local mossteps_per_period = mossteps_per_equave / periods_per_equave
-- The default generators_up value corresponds to the brightest mode,
-- Some default params will be different if the scalesig is 5L 2s
-- unless the mos is 5L 2s, then it's the 2nd-brightest mode
local generators_up = generators_up or mossteps_per_equave - periods
local scale_sig = mos.as_string(input_mos)
local scale_sig = mos.as_string(input_mos)
-- The default UDP corresponds to the middle mode. For mosses with an even
-- number of modes, there are two middle modes, so use the brighter of the
-- two instead.
-- If it's 5L 2s, default to the second-brightest mode.
local udp_default = { periods_per_equave * math.ceil((mossteps_per_period - 1)/ 2), periods_per_equave * math.floor((mossteps_per_period - 1) / 2) }
if scale_sig == "5L 2s" then
if scale_sig == "5L 2s" then
generators_up = 5
udp_default = { 5, 1 }
end
end
local udp = udp or udp_default
local generators_up = udp[1]
local generators_down = udp[2]
 
-- The natural note symbols are those that correspond to diamond-mos
-- The natural note symbols are those that correspond to diamond-mos
-- (JKLMN...) unless the mos is 5L 2s, then it's CDEFGAB
-- (JKLMN...) unless the mos is 5L 2s, then it's CDEFGAB
-- If it's diamond-mos, gamut is limited to 17 note names
-- If it's diamond-mos, gamut is limited to 17 note names
local note_symbols_main = "JKLMNOPQRSTUVWXYZ"
local note_symbols_main = "JKLMNOPQRSTUVWXYZ"
local note_symbols = note_symbols or string.sub(note_symbols_main, 1, mossteps_per_equave)
local note_symbols_default = string.sub(note_symbols_main, 1, mossteps_per_equave)
if scale_sig == "5L 2s" then
if scale_sig == "5L 2s" then
note_symbols = "CDEFGAB"
note_symbols_default = "CDEFGAB"
end
end
local note_symbols = note_symbols or note_symbols_default
-- The default accidentals are the amp and at (& and @)
-- The default accidentals are the amp and at (& and @)
-- unless the mos is 5L 2s, then it's sharp and flat (# and b)
-- unless the mos is 5L 2s, then it's sharp and flat (# and b)
local chroma_plus_symbol = chroma_plus_symbol or "&"
local chroma_plus_default = "&"
local chroma_minus_symbol = chroma_minus_symbol or "@"
local chroma_minus_default = "@"
if scale_sig == "5L 2s" then
if scale_sig == "5L 2s" then
chroma_plus_symbol = "#"
chroma_plus_default = "#"
chroma_minus_symbol = "b"
chroma_minus_default = "b"
end
end
local chroma_plus_symbol = chroma_plus_symbol or chroma_plus_default
-- For a mos nxL nys with a given step ratio p/q, the gamut is such that every accidental
local chroma_minus_symbol = chroma_minus_symbol or chroma_minus_default
-- note (what would be the black keys) has at least one name within the edo nxp+nyq.
-- Start with the mode defined by the udp un|dn and, starting at the root (usu. C or J),
-- construct two generator chains that goes up u generators and d generators. For a
-- multi-period mos with n periods, there needs to be a separate genchain pair per period
-- for n genchain pairs.
-- Reconstruct the UDP up|dp (u times p pipe d times p)
-- The generators_up corresponds to up and is given to us, so generators_down should
-- be reconstructed to correspond to dp; dividing either generators_up or generators_down
-- by the number of periods will give the number of generators per period (u and d by
-- themselves)
local generators_down = mossteps_per_equave - generators_up - periods
-- How long is the inital genchain for notes without accidentals?
-- How long is the inital genchain for notes without accidentals?
local gens_up_per_period = generators_up / periods
local gens_up_per_period = generators_up / periods_per_equave
local gens_down_per_period = generators_down / periods
local gens_down_per_period = generators_down / periods_per_equave
-- How long is the genchain extended for per period?
-- Get and simplify the step ratio
-- For nxL nys with a step ratio 2:1, extend the genchains by x generators for each period.
-- For any other step ratio p:q, extend by x(p-2)+y(q-1) gens per genchain per period.
-- If the step ratio p/q is such that p and q are not coprime and share a common
-- factor k, then the gamut produced is one of several gamuts shifted by up to k-1
-- edosteps up or down. For simplicity, this isn't included so the gamut will be that for
-- a step ratio p/q rather than kp/kq.
local kp = step_ratio[1]
local kp = step_ratio[1]
local kq = step_ratio[2]
local kq = step_ratio[2]
local k = rat.gcd(kp, kq)
local k = utils._gcd(kp, kq)
local num = kp / k
local num = kp / k
local den = kq / k
local den = kq / k
-- How many large and small steps per period?
-- How many large and small steps per period?
local x = input_mos.nL / periods -- Large step count
local x = input_mos.nL / periods_per_equave -- Large step count
local y = input_mos.ns / periods -- Small step count
local y = input_mos.ns / periods_per_equave -- Small step count
-- How many esteps are in the equave? Gamut does not include any notes reached by
-- How many esteps are in the equave? Gamut does not include any notes reached by
-- increments smaller than a chroma, so if the step ratio is not simplified, the
-- increments smaller than a chroma, so if the step ratio is not simplified, the
-- gamut will be for a simplified step ratio
-- gamut returned will be for a simplified step ratio
local estedps_per_equave = input_mos.nL * num + input_mos.ns * den
local esteps_per_equave = input_mos.nL * num + input_mos.ns * den
-- Similarly, how many esteps per period?
-- Similarly, how many esteps per period?
local estedps_per_period = x * num + y * den
local esteps_per_period = x * num + y * den
-- How long should the genchain extend after the initial genchain?
-- How long should the genchain extend after the initial genchain?
-- For a basic step ratio 2:1, extend by x
-- For a basic step ratio 2:1, extend by x
-- For a collapsed or equalized step ratio, don't extend at all
-- For any other ratio p:q (simplified), do this calculation:
-- For any other ratio p:q (simplified), do this calculation:
-- x*floor(p/2) + y*floor(q/2)
-- x*floor(p/2) + y*floor(q/2)
-- This is such that each altered note (what would be the black keys on a piano)
-- has names that contain the fewest chromas possible, even if they have more than
-- one name. EG, standard notation has C#/Db have two names, but both names
-- have the fewest possible accidentals
local genchain_extend = 0
local genchain_extend = 0
if num / den == 2 then
if num / den == 2 then
genchain_extend = x
genchain_extend = x
elseif num == den or den == 0 then
genchain_extend = 0
else
else
genchain_extend = x * math.floor(num/2) + y * math.floor(den/2)
genchain_extend = x * math.floor(num/2) + y * math.floor(den/2)
end
end
-- How long are the genchains?
-- How long are the genchains? Length is per period
local ascending_genchain_length = gens_up_per_period + genchain_extend
-- Genchain length counts the root, hence the +1
local descending_genchain_length = gens_down_per_period + genchain_extend
local ascending_genchain_length = gens_up_per_period + genchain_extend + 1
local descending_genchain_length = gens_down_per_period + genchain_extend + 1
-- Get the ascending and descending genchains
-- Get the ascending and descending genchains
local ascending_genchain = p.mos_genchain(input_mos, gens_up_per_period, ascending_genchain_length, note_symbols, chroma_plus_symbol, true)
-- The genchains are notationally agnostic so notation needs to be applied to them
local descending_genchain = p.mos_genchain(input_mos, gens_down_per_period, descending_genchain_length, note_symbols, chroma_minus_symbol, false)
local ascending_genchain = mosnot.mos_nomacc_chain(input_mos, gens_up_per_period, ascending_genchain_length, true)
local descending_genchain = mosnot.mos_nomacc_chain(input_mos, gens_down_per_period, descending_genchain_length, false)
-- Create an empty gamut
-- Create an empty gamut
local gamut = {}
local gamut = {}
for i = 1, estedps_per_equave + 1 do
for i = 1, esteps_per_equave + 1 do
table.insert(gamut, "")
table.insert(gamut, "")
end
end
Line 209: Line 115:
-- How many esteps are the bright and dark generators?
-- How many esteps are the bright and dark generators?
local bright_gen = mos.bright_gen(input_mos)
local bright_gen = mos.bright_gen(input_mos)
local esteps_per_bright_gen = bright_gen['L'] * num + bright_gen['s'] * den
local esteps_per_bright_gen = bright_gen["L"] * num + bright_gen["s"] * den
local esteps_per_dark_gen = estedps_per_period - esteps_per_bright_gen
local esteps_per_dark_gen = esteps_per_period - esteps_per_bright_gen
-- Add the notes to the gamut
-- Add the notes to the gamut
-- Notes with sharps (or diamond-mos equivalent) take precedence
for j = 1, periods_per_equave do
for j = 1, periods do
local bright_accumulator = 0
local bright_accumulator = 0
for i = 1, #ascending_genchain[j] do
for i = 1, #ascending_genchain[j] do
local index = (bright_accumulator % estedps_per_period) + (j - 1) * estedps_per_period + 1
local index = (bright_accumulator % esteps_per_period) + (j - 1) * esteps_per_period + 1
gamut[index] = gamut[index] .. ascending_genchain[j][i]
-- Convert the notationally agnostic form into a form that uses given notation
local note = ascending_genchain[j][i]
local note_symbol = string.sub(note_symbols, note["Mossteps"] + 1, note["Mossteps"] + 1)
local chroma_count = note["Chromas"]
local note_name = note_symbol .. string.rep(chroma_plus_symbol, chroma_count)
gamut[index] = gamut[index] .. note_name
bright_accumulator = bright_accumulator + esteps_per_bright_gen
bright_accumulator = bright_accumulator + esteps_per_bright_gen
end
end
local dark_accumulator = esteps_per_dark_gen
local dark_accumulator = esteps_per_dark_gen
for i = 2, #descending_genchain[j] do
for i = 2, #descending_genchain[j] do
local index = (dark_accumulator % estedps_per_period) + (j - 1) * estedps_per_period + 1
local index = (dark_accumulator % esteps_per_period) + (j - 1) * esteps_per_period + 1
-- Convert the notationally agnostic form into a form that uses given notation
local note = descending_genchain[j][i]
local note_symbol = string.sub(note_symbols, note["Mossteps"] + 1, note["Mossteps"] + 1)
local chroma_count = note["Chromas"] * -1
local note_name = note_symbol .. string.rep(chroma_minus_symbol, chroma_count)
-- Add to gamut
-- If there is a note there already, then append and separate with a slash
if gamut[index] ~= "" then
if gamut[index] ~= "" then
gamut[index] = gamut[index] .. "/" .. descending_genchain[j][i]
gamut[index] = gamut[index] .. "/" .. note_name
else
else
gamut[index] = gamut[index] .. descending_genchain[j][i]
gamut[index] = gamut[index] .. note_name
end
end
dark_accumulator = dark_accumulator + esteps_per_dark_gen
dark_accumulator = dark_accumulator + esteps_per_dark_gen
Line 234: Line 155:
-- Last note in the gamut is the root up one equave
-- Last note in the gamut is the root up one equave
gamut[#gamut] = ascending_genchain[1][1]
gamut[#gamut] = gamut[1]
return gamut
return gamut
Line 241: Line 162:
function p.mos_gamut_frame(frame)
function p.mos_gamut_frame(frame)
-- Default parameters for input mos and step ratio (5L 2s and 2:1 step ratio)
-- Default parameters for input mos and step ratio (5L 2s and 2:1 step ratio)
local input_mos_unparsed = frame.args['Scale Signature']
local input_mos_unparsed = frame.args["Scale Signature"]
local input_mos = mos.parse(input_mos_unparsed) or mos.new(2, 5, 2)
local input_mos = mos.parse(input_mos_unparsed) or mos.new(2, 5, 2)
local large_step_size = tonumber(frame.args['Large Step Size']) or 2
local small_step_size = tonumber(frame.args['Small Step Size']) or 1
-- Step ratio
local step_ratio = { large_step_size, small_step_size }
local step_ratio = { 2, 1 }
if string.len(frame.args["Step Ratio"]) > 0 then
step_ratio = mosnot.parse_step_ratio(frame.args["Step Ratio"])
end
-- Get the number of mossteps per period and equave
-- Get the number of mossteps per period and equave
local mossteps_per_equave = input_mos.nL + input_mos.ns
local mossteps_per_equave = input_mos.nL + input_mos.ns
local periods = rat.gcd(input_mos.nL, input_mos.ns)
local periods_per_equave = utils._gcd(input_mos.nL, input_mos.ns)
local mossteps_per_period = mossteps_per_equave / periods
local mossteps_per_period = mossteps_per_equave / periods_per_equave
-- The default generators_up value corresponds to the brightest mode,
-- If certain params were left blank and the scalesig is 5L 2s, the default
-- unless the mos is 5L 2s, then it's the 2nd-brightest mode
-- params will be for standard notation
local generators_up = tonumber(frame.args['Bright Gens Up']) or mossteps_per_equave - periods
local scale_sig = mos.as_string(input_mos)
local scale_sig = mos.as_string(input_mos)
-- The default UDP corresponds to the middle mode. For mosses with an even
-- number of modes, there are two middle modes, so use the brighter of the
-- two instead.
-- If it's 5L 2s, default to the second-brightest mode.
local udp = { periods_per_equave * math.ceil((mossteps_per_period - 1)/ 2), periods_per_equave * math.floor((mossteps_per_period - 1) / 2) }
if scale_sig == "5L 2s" then
if scale_sig == "5L 2s" then
generators_up = 5
udp = { 5, 1 }
end
end
if string.len(frame.args["UDP"]) > 0 then
-- Default note symbols are that from diamond mos
udp = mosnot.parse_udp(frame.args["UDP"])
local note_symbols_main = "JKLMNOPQRSTUVWXYZ"
local note_symbols = frame.args['Note Symbols']
if string.len(note_symbols) == 0 then
-- If this was blank, default to diamond-mos
-- Limited to 17 note names
note_symbols = string.sub(note_symbols_main, 1, mossteps_per_equave)
elseif scale_sig == "5L 2s" then
-- If it's 5L 2s, then default to standard notation
note_symbols = "CDEFGAB"
end
end
local generators_up = udp[1]
local generators_down = udp[2]
-- The default accidentals are the amp and at (& and @)
-- Get notation: naturals (or nominals), sharp symbol, and flat symbol
-- unless the mos is 5L 2s, then it's sharp and flat (# and b)
local notation_default = { ["Naturals"] = string.sub("JKLMNOPQRSTUVWXYZ", 1, mossteps_per_equave), ["Sharp"] = "&", ["Flat"] = "@" }
local chroma_plus_symbol = frame.args['Sharp Symbol']
local chroma_minus_symbol = frame.args['Flat Symbol']
if string.len(chroma_plus_symbol) == 0 then
chroma_plus_symbol = "&"
end
if string.len(chroma_minus_symbol) == 0 then
chroma_minus_symbol = "@"
end
if scale_sig == "5L 2s" then
if scale_sig == "5L 2s" then
chroma_plus_symbol = "#"
notation_default["Naturals"] = "CDEFGAB"
chroma_minus_symbol = "b"
notation_default["Sharp"] = "#"
notation_default["Flat"] = "b"
end
end
local notation = mosnot.parse_notation(frame.args["Notation"]) or notation_default
local note_symbols = notation["Naturals"]
local chroma_plus_symbol = notation["Sharp"]
local chroma_minus_symbol = notation["Flat"]
-- Get the gamut
-- Get the gamut
local gamut = p.mos_gamut(input_mos, generators_up, step_ratio, note_symbols, chroma_plus_symbol, chroma_minus_symbol)
local gamut = p.mos_gamut(input_mos, udp, step_ratio, note_symbols, chroma_plus_symbol, chroma_minus_symbol)
 
-- Format the gamut as a table
-- Since the gamut on a mos page is just text, so will this
local result = '{| class="wikitable"\n'
-- Formatting options may be explored at a later date
local result = ""
-- Add a table caption; this requires adding an et
for i = 1, #gamut - 1 do
local et_for_mos = et.new(#gamut - 1, input_mos.equave)
-- If the note name does not contain accidentals, it's a natural and should be bold
local result = result .. '|+ Note names for ' .. scale_sig .. " in " .. et.as_string(et_for_mos) .. "\n"
local result = result .. "|-\n"
for i = 1, #gamut do
-- Get the note name
local note_name = gamut[i]
local note_name = gamut[i]
if string.match(note_name, chroma_plus_symbol) or string.match(note_name, chroma_minus_symbol) then
-- If the note name has a slash, replace it with a newline
result = result .. note_name .. ", "
note_name = note_name:gsub("/", "\n")
-- If note name string is one character, it's a natural so the cell is white
-- For anything else, the cell is black (actually gray) to mimic a piano
if string.len(note_name) == 1 then
result = result .. '|bgcolor="white"|'.. note_name .. "\n"
else
else
result = result .. '|bgcolor="gray"|'.. note_name .. "\n"
result = result .. "'''" .. note_name .. "''', "
end
end
end
end
result = result .. "|}"
result = result .. "'''" .. gamut[#gamut] .. "'''"
-- Debugger option
local debugg = yesno(frame.args["debug"])
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return result
return frame:preprocess(result)
end
end


return p
return p