Module:Ups and downs notation: Difference between revisions

TallKite (talk | contribs)
m works!
Sintel (talk | contribs)
cleanup and formatting
Line 21: Line 21:
-- }
-- }


function roundDown(x)
local function round(x)
  return x>=0 and math.ceil(x-0.5) or math.floor(x+0.5)   -- round 1.5 down to 1
-- rounds half-integers towards 0.
return x >= 0 and math.ceil(x - 0.5) or math.floor(x + 0.5)
end
end


Line 68: Line 69:
else
else
local num_double_sharps = 0
local num_double_sharps = 0
local num_sharps = roundDown((i - last_diatonic_note) / chroma)
local num_sharps = round((i - last_diatonic_note) / chroma)
if num_sharps > 1 then
if num_sharps > 1 then
num_double_sharps = math.floor(num_sharps / 2)
num_double_sharps = math.floor(num_sharps / 2)
Line 89: Line 90:
table.insert(
table.insert(
note_names[i],
note_names[i],
"v<sup>" .. num_downs .. "</sup>" .. last_diatonic_names[j]  
"v<sup>"
.. string.rep("♯", num_sharps) .. string.rep("𝄪", num_double_sharps)
.. num_downs
.. "</sup>"
.. last_diatonic_names[j]
.. string.rep("♯", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
)
elseif num_downs > 0 then
elseif num_downs > 0 then
table.insert(
table.insert(
note_names[i],
note_names[i],
string.rep("v", num_downs) .. last_diatonic_names[j]
string.rep("v", num_downs)
.. string.rep("♯", num_sharps) .. string.rep("𝄪", num_double_sharps)
.. last_diatonic_names[j]
.. string.rep("♯", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
)
elseif num_ups >= 3 then
elseif num_ups >= 3 then
Line 128: Line 135:
last_diatonic_note = i
last_diatonic_note = i
else
else
local num_flats = roundDown((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
local num_ups = 0