Module:Halberstadt SVG
From XenReference
Documentation for this module may be created at Module:Halberstadt SVG/doc
--[[
MediaWiki Lua module to generate diagrams of microtonal Halberstadt keyboards using the html <svg> element. If there is more than one "black" key in between two "white" keys, stack the higher ones above the lower ones vertically. Arguments with a 19edo example:
nominals = {"F", "C", "G", "D", "A", "E", "B"} -- natural notes, the "white" keys of the keyboard
period = 19 -- number of keys total
generator = 11 -- number of keys from one nominal to the next, putting each on key number n*generator % period
startIndex = 5 -- index of the nominal on the leftmost key, in this case A
duplicateFirstKey = true -- whether to add an additional key at the right side of the keyboard with the same note as the first key
accidentals = {"b", "#"}
accidentalValues = {-1, 1} -- number of keys that each accidental alters the pitch by
nominalOverride = false -- whether to ignore note names with an accidental if they land on the same key as a nominal; if false, show both names generated
keyDimensions = {1, 20, 80, 10, 40, 20, 60, 10} -- all measured in pixels: stroke width, white key width, white key height, white key font size, vertical offset of black keys, black key width, black key height, black key font size
keyColors = {"000", "fff", "000", "333", "fff", "666", "fff"} -- outline color, "white" key color, "white" key font color, "black" key color, "black" key font color, subsequent values may be used to define the color and font color of any "black" keys above the first row
--]]
local p = {}
-- Convert comma-separated values into Lua table, trim whitespace
local function splitCSV(str)
if not str or str == "" then return {} end
local t = {}
for s in mw.text.gsplit(str, "%s*,%s*") do
table.insert(t, s)
end
return t
end
-- Modulo that handles negative numbers correctly
local function mod(a, b)
return ((a % b) + b) % b
end
-- Core renderer
function p.render(frame)
local args = frame.args
-- Musical parameters
local nominals = splitCSV(args.nominals)
local period = tonumber(args.period)
local generator = tonumber(args.generator)
local startIndex = tonumber(args.startIndex) or 1
local duplicateFirstKey = args.duplicateFirstKey == "yes"
local accidentals = splitCSV(args.accidentals)
local accidentalValues = {}
for _, v in ipairs(splitCSV(args.accidentalValues)) do
table.insert(accidentalValues, tonumber(v))
end
local nominalOverride = args.nominalOverride == "yes"
-- Geometry
local dims = {}
for _, v in ipairs(splitCSV(args.keyDimensions or "1,40,120,20,80,40,120,20")) do
table.insert(dims, tonumber(v))
end
local strokeWidth, wWhite, hWhite, fsWhite, offsetBlack, wBlack, hBlack, fsBlack =
dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8]
-- Colors
local colors = splitCSV(args.keyColors or "#000,#fff,#000,#333,#fff,#666,#fff")
-- Compute pitch classes for nominals
local nominalKeys = {}
for i, name in ipairs(nominals) do
local idx = mod((i - startIndex) * generator, period)
nominalKeys[idx] = nominalKeys[idx] or {}
table.insert(nominalKeys[idx], name)
end
-- Generate all labels per pitch class
local labels = {}
for pc, names in pairs(nominalKeys) do
labels[pc] = labels[pc] or {}
for _, n in ipairs(names) do
table.insert(labels[pc], n)
end
end
for pc, names in pairs(nominalKeys) do
for i, acc in ipairs(accidentals) do
local delta = accidentalValues[i]
local target = mod(pc + delta, period)
if not nominalOverride or not nominalKeys[target] then
labels[target] = labels[target] or {}
for _, n in ipairs(names) do
table.insert(labels[target], n .. acc)
end
end
end
end
-- Determine white keys (nominals only)
local whiteKeys = {}
for pc in pairs(nominalKeys) do
table.insert(whiteKeys, pc)
end
table.sort(whiteKeys)
if duplicateFirstKey then
table.insert(whiteKeys, whiteKeys[1] + period)
end
-- Map pitch class -> x-position
local xPos = {}
for i, pc in ipairs(whiteKeys) do
xPos[pc] = (i - 1) * wWhite
end
local svgWidth = #whiteKeys*wWhite
local svgHeight = hWhite + hBlack + offsetBlack
-- Collect black keys between whites
local blackKeys = {}
for pc, lbls in pairs(labels) do
if not nominalKeys[pc] then
local leftWhite
for i = #whiteKeys, 1, -1 do
if whiteKeys[i] < pc then
leftWhite = whiteKeys[i]
break
end
end
if leftWhite then
blackKeys[leftWhite] = blackKeys[leftWhite] or {}
table.insert(blackKeys[leftWhite], pc)
end
end
end
-- SVG assembly
local out = {}
-- White keys
for _, pc in ipairs(whiteKeys) do
local x = xPos[pc]
table.insert(out,
frame:extensionTag{
name = 'htmltag',
args = {
tagname = 'rect',
x = x,
y = svgHeight - hWhite,
width = wWhite,
height = hWhite,
style = 'fill:' .. colors[2] .. '; stroke:' .. colors[1]
}
}
)
if labels[pc] then
table.insert(out,
frame:extensionTag{
name = 'htmltag',
content = table.concat(labels[pc], "/"),
args = {
tagname = 'text',
x = x + wWhite / 2,
y = svgHeight - hWhite/2,
['text-anchor'] = 'middle',
['font-size'] = fsWhite,
style = 'fill:' .. colors[3]
}
}
)
end
end
-- Black keys (stacked)
for leftWhite, pcs in pairs(blackKeys) do
table.sort(pcs)
for i, pc in ipairs(pcs) do
local x = xPos[leftWhite] + wWhite - wBlack/2
local y = svgHeight - hWhite + i*(offsetBlack-hBlack-strokeWidth)
local fill = colors[(i-1)*2 + 4] or colors[4]
local textCol = colors[(i-1)*2 + 5] or colors[5]
table.insert(out,
frame:extensionTag{
name = 'htmltag',
args = {
tagname = 'rect',
x = x,
y = y,
width = wBlack,
height = hBlack,
style = 'fill:' .. fill .. '; stroke:' .. colors[1]
}
}
)
table.insert(out,
frame:extensionTag{
name = 'htmltag',
content = table.concat(labels[pc], "/"),
args = {
tagname = 'text',
x = x + wBlack/2,
y = y + hBlack/2,
['text-anchor'] = 'middle',
['font-size'] = fsBlack,
style = 'fill:' .. textCol
}
}
)
end
end
return frame:extensionTag{
name = 'htmltag',
content = table.concat(out),
args = {
tagname = 'svg',
xmlns = 'http://wWhitew.w3.org/2000/svg',
width = svgWidth,
height = svgHeight
}
}
end
return p
