Module:Harmonics intro: Difference between revisions
Jump to navigation
Jump to search
Created page with "-- Module:Harmonics intro -- Generates the opening sentences of the "Harmonics n–2n" and "Subharmonics n–2n" pages. -- Called through Template:Harmonics intro and Template:Subharmonics intro. local p = {} local getArgs = require("Module:Arguments").getArgs local EN_DASH = "–" -- "n:(n+1):…:2n", written out in full for small n local function chord(lo, hi) if hi - lo <= 7 then local t = {} for i = lo, hi do t[#t + 1] = i end return table.concat(t, ":"..." |
No edit summary |
||
| (3 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
-- Module:Harmonics intro | -- Module: Harmonics intro | ||
-- Generates the opening sentences of the "Harmonics n–2n" and "Subharmonics n–2n" pages. | -- Generates the opening sentences of the "Harmonics n–2n" and "Subharmonics n–2n" pages. | ||
-- Called through Template:Harmonics intro and Template:Subharmonics intro. | -- Called through Template: Harmonics intro and Template: Subharmonics intro. | ||
local p = {} | local p = {} | ||
local getArgs = require("Module:Arguments").getArgs | local getArgs = require("Module:Arguments").getArgs | ||
| Line 7: | Line 7: | ||
local EN_DASH = "–" | local EN_DASH = "–" | ||
-- "n:(n+1):…:2n", written out in full for small n | -- "n:(n + 1):…:2n", written out in full for small n | ||
local function chord(lo, hi) | local function chord(lo, hi) | ||
if hi - lo <= 7 then | if hi - lo <= 7 then | ||
| Line 27: | Line 27: | ||
local n = tonumber(args[1] or args.n) | local n = tonumber(args[1] or args.n) | ||
if not n or n < 1 or n ~= math.floor(n) then | if not n or n < 1 or n ~= math.floor(n) then | ||
return '<strong class="error">Template:Harmonics intro requires a positive integer, e.g. <code>{{Harmonics intro|12}}</code>.</strong>' | return '<strong class="error">Template: Harmonics intro requires a positive integer, e.g. <code>{{Harmonics intro|12}}</code>.</strong>' | ||
end | end | ||
local | local range = n .. EN_DASH .. 2 * n | ||
local | local twin_title = (sub and "Harmonics " or "Subharmonics ") .. range | ||
-- | -- "harmonics 12–24" is a noun phrase, not the name of a scale: plural, lower case | ||
local | local phrase = (sub and "subharmonics " or "harmonics ") .. range | ||
local out = {} | |||
if sub then | |||
out[#out + 1] = string.format( | |||
"The '''%s''' are a scale that consists of [[subharmonic]]s %d through %d of the [[subharmonic series]]. Above the root they form the ratios %d/%d, %d/%d, …, %d/%d and repeat at the octave. " .. | |||
"Used as a [[tuning system]], it is also called '''%difdo'''. ", | |||
phrase, | |||
n, 2 * n, | |||
2 * n, 2 * n, | |||
2 * n, 2 * n - 1, | |||
2 * n, n, | |||
n | |||
) | |||
else | |||
out[#out + 1] = string.format( | |||
"The '''%s''' (also '''harmonic series segment %d::%d''' or '''harmonic series mode %d''') are a scale that consists of harmonics %d through %d (%s) and repeats at the [[octave]]. " .. | |||
"Used as a [[tuning system]], it is also called '''%dafdo'''. ", | |||
phrase, | |||
n, 2 * n, n, n, 2 * n, | |||
chord(n, 2 * n), n | |||
) | |||
end | |||
-- other names, given as aka="name; name" | |||
local akas = {} | local akas = {} | ||
for name in mw.text.gsplit(args.aka or "", "%s*;%s*") do | for name in mw.text.gsplit(args.aka or "", "%s*;%s*") do | ||
| Line 42: | Line 64: | ||
end | end | ||
end | end | ||
if #akas > 0 then | if #akas > 0 then | ||
out[#out + 1] = "It is also known as " .. table.concat(akas, " and ") .. ". " | |||
end | end | ||
-- inverse | |||
if | if n ~= 2 and page_exists(twin_title) then -- 2–4 is its own inverse | ||
out[#out + 1] = string.format( | out[#out + 1] = string.format( | ||
" | "The inverse consists of [[%s|%s]]. ", | ||
twin_title, | |||
(sub and "harmonics " or "subharmonics ") .. range | |||
) | |||
end | end | ||
return (table.concat(out):gsub("%s+$", "")) | return (table.concat(out):gsub("%s+$", "")) | ||
Latest revision as of 19:51, 11 October 2026
Documentation for this module may be created at Module:Harmonics intro/doc
-- Module: Harmonics intro
-- Generates the opening sentences of the "Harmonics n–2n" and "Subharmonics n–2n" pages.
-- Called through Template: Harmonics intro and Template: Subharmonics intro.
local p = {}
local getArgs = require("Module:Arguments").getArgs
local EN_DASH = "–"
-- "n:(n + 1):…:2n", written out in full for small n
local function chord(lo, hi)
if hi - lo <= 7 then
local t = {}
for i = lo, hi do
t[#t + 1] = i
end
return table.concat(t, ":")
end
return string.format("%d:%d:…:%d", lo, lo + 1, hi)
end
local function page_exists(title)
local t = mw.title.new(title)
return t ~= nil and t.exists
end
local function build(args, sub)
local n = tonumber(args[1] or args.n)
if not n or n < 1 or n ~= math.floor(n) then
return '<strong class="error">Template: Harmonics intro requires a positive integer, e.g. <code>{{Harmonics intro|12}}</code>.</strong>'
end
local range = n .. EN_DASH .. 2 * n
local twin_title = (sub and "Harmonics " or "Subharmonics ") .. range
-- "harmonics 12–24" is a noun phrase, not the name of a scale: plural, lower case
local phrase = (sub and "subharmonics " or "harmonics ") .. range
local out = {}
if sub then
out[#out + 1] = string.format(
"The '''%s''' are a scale that consists of [[subharmonic]]s %d through %d of the [[subharmonic series]]. Above the root they form the ratios %d/%d, %d/%d, …, %d/%d and repeat at the octave. " ..
"Used as a [[tuning system]], it is also called '''%difdo'''. ",
phrase,
n, 2 * n,
2 * n, 2 * n,
2 * n, 2 * n - 1,
2 * n, n,
n
)
else
out[#out + 1] = string.format(
"The '''%s''' (also '''harmonic series segment %d::%d''' or '''harmonic series mode %d''') are a scale that consists of harmonics %d through %d (%s) and repeats at the [[octave]]. " ..
"Used as a [[tuning system]], it is also called '''%dafdo'''. ",
phrase,
n, 2 * n, n, n, 2 * n,
chord(n, 2 * n), n
)
end
-- other names, given as aka="name; name"
local akas = {}
for name in mw.text.gsplit(args.aka or "", "%s*;%s*") do
if name ~= "" then
akas[#akas + 1] = "'''" .. name .. "'''"
end
end
if #akas > 0 then
out[#out + 1] = "It is also known as " .. table.concat(akas, " and ") .. ". "
end
-- inverse
if n ~= 2 and page_exists(twin_title) then -- 2–4 is its own inverse
out[#out + 1] = string.format(
"The inverse consists of [[%s|%s]]. ",
twin_title,
(sub and "harmonics " or "subharmonics ") .. range
)
end
return (table.concat(out):gsub("%s+$", ""))
end
function p.harmonics(frame)
return build(getArgs(frame), false)
end
function p.subharmonics(frame)
return build(getArgs(frame), true)
end
p._build = build
return p