Module:Ups and downs notation: Difference between revisions

ArrowHead294 (talk | contribs)
mNo edit summary
ArrowHead294 (talk | contribs)
mNo edit summary
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local ET = require("Module:ET")
local ET = require("Module:ET")
local utils = require("Module:Utils")
local utils = require("Module:Utils")
local p = {}
local yesno = require("Module:Yesno")


-- Returns a table of note names
-- Returns a table of note names
Line 20: Line 22:
--  [12] = {"D"}
--  [12] = {"D"}
-- }
-- }
local function round(x)
-- rounds half-integers towards 0.
return x >= 0 and math.ceil(x - 0.5) or math.floor(x + 0.5)
end


function p.get_note_names_table(et, fifth)
function p.get_note_names_table(et, fifth)
Line 63: Line 70:
last_diatonic_note = i
last_diatonic_note = i
else
else
local num_double_sharps = math.floor((i - last_diatonic_note) / (2 * chroma))
local num_double_sharps = 0
local num_sharps = math.floor((i - last_diatonic_note) / chroma) % 2
local num_sharps = round((i - last_diatonic_note) / chroma)
if num_sharps > 1 then
num_double_sharps = math.floor(num_sharps / 2)
num_sharps = num_sharps % 2
end
local num_ups = (i - last_diatonic_note) % chroma
local num_ups = (i - last_diatonic_note) % chroma
local num_downs = 0
if chroma == 0 then
if chroma == 0 then
num_double_sharps = 0
num_double_sharps = 0
num_sharps = 0
num_sharps = 0
num_ups = (i - last_diatonic_note)
num_ups = (i - last_diatonic_note)
end
if num_ups > chroma / 2 then
num_downs = chroma - num_ups
num_ups = 0
end
end
local last_diatonic_names = note_names[last_diatonic_note]
local last_diatonic_names = note_names[last_diatonic_note]
for j = 1, #last_diatonic_names do
for j = 1, #last_diatonic_names do
if num_ups >= 3 then
if num_downs >= 3 then
table.insert(
note_names[i],
"v<sup>"
.. num_downs
.. "</sup>"
.. last_diatonic_names[j]
.. string.rep("♯", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
elseif num_downs > 0 then
table.insert(
note_names[i],
string.rep("v", num_downs)
.. last_diatonic_names[j]
.. string.rep("♯", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
elseif num_ups >= 3 then
table.insert(
table.insert(
note_names[i],
note_names[i],
Line 103: Line 137:
last_diatonic_note = i
last_diatonic_note = i
else
else
local num_flats = math.floor((last_diatonic_note - i) / chroma)
local num_flats = round((last_diatonic_note - i) / chroma)
local num_downs = (last_diatonic_note - i) % chroma
local num_downs = (last_diatonic_note - i) % chroma
local num_ups = 0
if chroma == 0 then
if chroma == 0 then
num_flats = 0
num_flats = 0
num_downs = (last_diatonic_note - i)
num_downs = (last_diatonic_note - i)
end
if num_downs > chroma / 2 then
num_ups = chroma - num_downs
num_downs = 0
end
end
local last_diatonic_names = note_names[last_diatonic_note]
local last_diatonic_names = note_names[last_diatonic_note]
for j = 1, #last_diatonic_names do
for j = 1, #last_diatonic_names do
if num_downs >= 3 then
if num_ups >= 3 then
table.insert(
note_names[i],
"^<sup>" .. num_ups .. "</sup>" .. last_diatonic_names[j] .. string.rep("♭", num_flats)
)
elseif num_ups > 0 then
table.insert(
note_names[i],
string.rep("^", num_ups) .. last_diatonic_names[j] .. string.rep("♭", num_flats)
)
elseif num_downs >= 3 then
table.insert(
table.insert(
note_names[i],
note_names[i],
Line 131: Line 180:
function p.ups_and_downs_note_name(frame)
function p.ups_and_downs_note_name(frame)
local et = ET.parse(frame.args["tuning"]) or ET.parse("12edo")
local et = ET.parse(frame.args["tuning"]) or ET.parse("12edo")
local debugg = yesno(frame.args["debug"])
local fifth = tonumber(frame.args["fifth"])
local fifth = tonumber(frame.args["fifth"])
local step = tonumber(frame.args["step"])
local step = tonumber(frame.args["step"])
return table.concat(p.get_note_names_table(et, fifth)[step], ", "):sub(0, -1)
local result = table.concat(p.get_note_names_table(et, fifth)[step], ", "):sub(0, -1)
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return frame:preprocess(result)
end
end


return p
return p