Module:Ups and downs notation: Difference between revisions
m works! |
cleanup and formatting |
||
| Line 21: | Line 21: | ||
-- } | -- } | ||
function | 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 | end | ||
| Line 68: | Line 69: | ||
else | else | ||
local num_double_sharps = 0 | local num_double_sharps = 0 | ||
local num_sharps = | 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>" | ||
.. 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) | ||
.. 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 = | 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 | ||