Module:Ups and downs notation: Difference between revisions
m works! |
ArrowHead294 (talk | contribs) mNo edit summary |
||
| (2 intermediate revisions by 2 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 | local yesno = require("Module:Yesno") | ||
-- Returns a table of note names | -- Returns a table of note names | ||
| Line 21: | Line 23: | ||
-- } | -- } | ||
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 71: | ||
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 92: | ||
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 137: | ||
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 | ||
| Line 171: | 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"]) | ||
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 | ||